gtest-death-test.cc revision 41d0579e8de9ef4ff178fc4991043c61a19943f7
1051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Copyright 2005, Google Inc.
2051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// All rights reserved.
3051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
4051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Redistribution and use in source and binary forms, with or without
5051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// modification, are permitted provided that the following conditions are
6051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// met:
7051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
8051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//     * Redistributions of source code must retain the above copyright
9051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// notice, this list of conditions and the following disclaimer.
10051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//     * Redistributions in binary form must reproduce the above
11051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// copyright notice, this list of conditions and the following disclaimer
12051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// in the documentation and/or other materials provided with the
13051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// distribution.
14cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//     * Neither the name of Google Inc. nor the names of its
15051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// contributors may be used to endorse or promote products derived from
16051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// this software without specific prior written permission.
17051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
18051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
30cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Author: wan@google.com (Zhanyong Wan), vladl@google.com (Vlad Losev)
31cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//
32051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// This file implements death tests.
33051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
34051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#include "gtest/gtest-death-test.h"
35051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#include "gtest/internal/gtest-port.h"
36051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
37051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#if GTEST_HAS_DEATH_TEST
38051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
39051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# if GTEST_OS_MAC
40a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt#  include <crt_externs.h>
41a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt# endif  // GTEST_OS_MAC
42a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
43a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt# include <errno.h>
44051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# include <fcntl.h>
45051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# include <limits.h>
46051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# include <stdarg.h>
47051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
48051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# if GTEST_OS_WINDOWS
49051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#  include <windows.h>
50051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# else
51051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#  include <sys/mman.h>
52051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#  include <sys/wait.h>
5304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt# endif  // GTEST_OS_WINDOWS
5404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
55051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#endif  // GTEST_HAS_DEATH_TEST
5604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
5704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt#include "gtest/gtest-message.h"
5804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt#include "gtest/internal/gtest-string.h"
599866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
6004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// Indicates that this translation unit is part of Google Test's
619866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// implementation.  It must come before gtest-internal-inl.h is
629866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// included, or there will be a compiler error.  This trick is to
639866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// prevent a user from accidentally including gtest-internal-inl.h in
6404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// his code.
6504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt#define GTEST_IMPLEMENTATION_ 1
66051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#include "src/gtest-internal-inl.h"
67051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt#undef GTEST_IMPLEMENTATION_
68051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
69051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtnamespace testing {
70051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
71051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Constants.
72051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
73051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// The default death test style.
74051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtstatic const char kDefaultDeathTestStyle[] = "fast";
75051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
7668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry ShmidtGTEST_DEFINE_string_(
77051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    death_test_style,
7868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle),
7968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "Indicates how to run a death test in a forked child process: "
8068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "\"threadsafe\" (child process re-executes the test binary "
8168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "from the beginning, running only the specific death test) or "
8268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "\"fast\" (child process runs the death test immediately "
8368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "after forking).");
8468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
8568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry ShmidtGTEST_DEFINE_bool_(
8668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    death_test_use_fork,
8768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    internal::BoolFromGTestEnv("death_test_use_fork", false),
8868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "Instructs to use fork()/_exit() instead of clone() in death tests. "
89f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    "Ignored and always uses fork() on POSIX systems where clone() is not "
90f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    "implemented. Useful when running under valgrind or similar tools if "
91f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    "those do not support clone(). Valgrind 3.3.1 will just fail if "
92f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    "it sees an unsupported combination of clone() flags. "
93f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    "It is not recommended to use this flag w/o valgrind though it will "
9468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "work in 99% of the cases. Once valgrind is fixed, this flag will "
9568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "most likely be removed.");
9668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
9768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtnamespace internal {
9868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry ShmidtGTEST_DEFINE_string_(
9968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    internal_run_death_test, "",
10068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "Indicates the file, line number, temporal index of "
10168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "the single death test to run, and a file descriptor to "
10268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "which a success code may be sent, all separated by "
10368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "colons.  This flag is specified if and only if the current "
10468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "process is a sub-process launched for running a thread-safe "
10568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    "death test.  FOR INTERNAL USE ONLY.");
106f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt}  // namespace internal
107f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
108f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#if GTEST_HAS_DEATH_TEST
109f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
11068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt// ExitedWithCode constructor.
11168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry ShmidtExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {
11268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt}
11368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
114051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// ExitedWithCode function-call operator.
11568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidtbool ExitedWithCode::operator()(int exit_status) const {
116051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# if GTEST_OS_WINDOWS
117051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
118051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return exit_status == exit_code_;
119051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
120051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# else
121051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
122051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_;
123051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
124cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# endif  // GTEST_OS_WINDOWS
12504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt}
12604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
127cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# if !GTEST_OS_WINDOWS
128cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// KilledBySignal constructor.
129cce06667447b5aec83452adb0c15100ada531095Dmitry ShmidtKilledBySignal::KilledBySignal(int signum) : signum_(signum) {
130cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
131cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
132cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// KilledBySignal function-call operator.
133cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtbool KilledBySignal::operator()(int exit_status) const {
134cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_;
135cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
136cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# endif  // !GTEST_OS_WINDOWS
137cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
138cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtnamespace internal {
139cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
140cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Utilities needed for death tests.
141cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
14204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// Generates a textual description of a given exit code, in the format
143cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// specified by wait(2).
144cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic String ExitSummary(int exit_code) {
145cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  Message m;
146cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
147cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# if GTEST_OS_WINDOWS
148cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
149cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  m << "Exited with exit status " << exit_code;
1509866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
1519866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt# else
1529866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
1539866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  if (WIFEXITED(exit_code)) {
1549866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    m << "Exited with exit status " << WEXITSTATUS(exit_code);
1559866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  } else if (WIFSIGNALED(exit_code)) {
1569866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    m << "Terminated by signal " << WTERMSIG(exit_code);
1579866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  }
1589866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt#  ifdef WCOREDUMP
1599866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  if (WCOREDUMP(exit_code)) {
1609866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    m << " (core dumped)";
1619866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  }
1629866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt#  endif
1639866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt# endif  // GTEST_OS_WINDOWS
1649866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
1659866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  return m.GetString();
166cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
167cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
168cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Returns true if exit_status describes a process that was terminated
169cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// by a signal, or exited normally with a nonzero exit code.
170cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtbool ExitedUnsuccessfully(int exit_status) {
171cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return !ExitedWithCode(0)(exit_status);
172cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
173cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
174051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# if !GTEST_OS_WINDOWS
17504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// Generates a textual failure message when a death test finds more than
176051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// one thread running, or cannot determine the number of threads, prior
177051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// to executing the given statement.  It is the responsibility of the
178cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// caller not to pass a thread_count of 1.
179a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtstatic String DeathTestThreadWarning(size_t thread_count) {
180051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  Message msg;
181cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  msg << "Death tests use fork(), which is unsafe particularly"
182a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt      << " in a threaded context. For this test, " << GTEST_NAME_ << " ";
183051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (thread_count == 0)
184051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    msg << "couldn't detect the number of threads.";
185051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  else
186051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    msg << "detected " << thread_count << " threads.";
187051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return msg.GetString();
188cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
189cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# endif  // !GTEST_OS_WINDOWS
190cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
191cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Flag characters for reporting a death test that did not die.
192051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtstatic const char kDeathTestLived = 'L';
193051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtstatic const char kDeathTestReturned = 'R';
194cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic const char kDeathTestThrew = 'T';
19504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtstatic const char kDeathTestInternalError = 'I';
196cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
197051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// An enumeration describing all of the possible ways that a death test can
1989866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// conclude.  DIED means that the process died while executing the test
1999866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// code; LIVED means that process lived beyond the end of the test code;
2009866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// RETURNED means that the test statement attempted to execute a return
201051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// statement, which is not allowed; THREW means that the test statement
202051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// returned control by throwing an exception.  IN_PROGRESS means the test
203051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// has not yet concluded.
204051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// TODO(vladl@google.com): Unify names and possibly values for
205051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// AbortReason, DeathTestOutcome, and flag characters above.
206051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtenum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
207051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
208051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Routine for aborting the program which is safe to call from an
209051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// exec-style death test child process, in which case the error
210051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// message is propagated back to the parent process.  Otherwise, the
211051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// message is simply printed to stderr.  In either case, the program
212051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// then exits with status 1.
213cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtvoid DeathTestAbort(const String& message) {
214cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // On a POSIX system, this function may be called from a threadsafe-style
215344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt  // death test child process, which operates on a very small stack.  Use
216cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // the heap for any additional non-minuscule memory requirements.
217cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  const InternalRunDeathTestFlag* const flag =
218051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      GetUnitTestImpl()->internal_run_death_test_flag();
219cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  if (flag != NULL) {
220051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    FILE* parent = posix::FDOpen(flag->write_fd(), "w");
221051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    fputc(kDeathTestInternalError, parent);
222051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    fprintf(parent, "%s", message.c_str());
223051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    fflush(parent);
224051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    _exit(1);
225cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  } else {
226cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    fprintf(stderr, "%s", message.c_str());
227cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    fflush(stderr);
228051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    posix::Abort();
229344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt  }
230cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
231344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt
232cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// A replacement for CHECK that calls DeathTestAbort if the assertion
23368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt// fails.
234cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# define GTEST_DEATH_TEST_CHECK_(expression) \
235051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  do { \
236051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    if (!::testing::internal::IsTrue(expression)) { \
237cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      DeathTestAbort(::testing::internal::String::Format( \
238051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          "CHECK failed: File %s, line %d: %s", \
239051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          __FILE__, __LINE__, #expression)); \
240cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    } \
24168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt  } while (::testing::internal::AlwaysFalse())
242051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
243051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for
2447d5c8f257a74ac0d12828962a492e8b84ef83923Dmitry Shmidt// evaluating any system call that fulfills two conditions: it must return
245051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// -1 on failure, and set errno to EINTR when it is interrupted and
246051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// should be tried again.  The macro expands to a loop that repeatedly
247051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// evaluates the expression as long as it evaluates to -1 and sets
248cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// errno to EINTR.  If the expression evaluates to -1 but errno is
249cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// something other than EINTR, DeathTestAbort is called.
250cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \
251051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  do { \
252051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    int gtest_retval; \
253051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    do { \
254051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      gtest_retval = (expression); \
255a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    } while (gtest_retval == -1 && errno == EINTR); \
256051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    if (gtest_retval == -1) { \
257051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      DeathTestAbort(::testing::internal::String::Format( \
258051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          "CHECK failed: File %s, line %d: %s != -1", \
259cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt          __FILE__, __LINE__, #expression)); \
260051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    } \
261a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  } while (::testing::internal::AlwaysFalse())
262a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
263a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// Returns the message describing the last system error in errno.
264051af73b8f8014eff33330aead0f36944b3403e6Dmitry ShmidtString GetLastErrnoDescription() {
265051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    return String(errno == 0 ? "" : posix::StrError(errno));
266cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
267051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
268051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// This is called from a death test parent process to read a failure
269051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// message from the death test child process and log it with the FATAL
270cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// severity. On Windows, the message is read from a pipe handle. On other
271cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// platforms, it is read from a file descriptor.
272051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtstatic void FailFromInternalError(int fd) {
273051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  Message error;
274051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  char buffer[256];
275051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int num_read;
276cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
277051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  do {
278051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    while ((num_read = posix::Read(fd, buffer, 255)) > 0) {
279051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      buffer[num_read] = '\0';
280cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      error << buffer;
281051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    }
282a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  } while (num_read == -1 && errno == EINTR);
283a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
284a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  if (num_read == 0) {
285a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    GTEST_LOG_(FATAL) << error.GetString();
286a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  } else {
287a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    const int last_error = errno;
288051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    GTEST_LOG_(FATAL) << "Error while reading death test internal: "
289051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                      << GetLastErrnoDescription() << " [" << last_error << "]";
290a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  }
291051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
292051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
293051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Death test constructor.  Increments the running death test count
294051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// for the current test.
295051af73b8f8014eff33330aead0f36944b3403e6Dmitry ShmidtDeathTest::DeathTest() {
296051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  TestInfo* const info = GetUnitTestImpl()->current_test_info();
297cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  if (info == NULL) {
298051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    DeathTestAbort("Cannot run a death test outside of a TEST or "
299051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                   "TEST_F construct");
300051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  }
301051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
302051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
303051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Creates and returns a death test by dispatching to the current
304051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// death test factory.
305051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtbool DeathTest::Create(const char* statement, const RE* regex,
306a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt                       const char* file, int line, DeathTest** test) {
307a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  return GetUnitTestImpl()->death_test_factory()->Create(
308a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt      statement, regex, file, line, test);
309a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt}
310a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
311a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtconst char* DeathTest::LastMessage() {
312a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  return last_death_test_message_.c_str();
313a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt}
314a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
315a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtvoid DeathTest::set_last_death_test_message(const String& message) {
316a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  last_death_test_message_ = message;
317a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt}
318a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
319a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry ShmidtString DeathTest::last_death_test_message_;
320a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
321a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// Provides cross platform implementation for some death functionality.
322a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtclass DeathTestImpl : public DeathTest {
3239866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt protected:
3249866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  DeathTestImpl(const char* a_statement, const RE* a_regex)
3259866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt      : statement_(a_statement),
3269866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        regex_(a_regex),
3279866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        spawned_(false),
3289866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        status_(-1),
3299866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        outcome_(IN_PROGRESS),
3309866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        read_fd_(-1),
3319866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        write_fd_(-1) {}
3329866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
3339866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  // read_fd_ is expected to be closed and cleared by a derived class.
3349866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); }
3359866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
336051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void Abort(AbortReason reason);
337051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  virtual bool Passed(bool status_ok);
338051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
339051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const char* statement() const { return statement_; }
340051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const RE* regex() const { return regex_; }
341051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  bool spawned() const { return spawned_; }
342cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  void set_spawned(bool is_spawned) { spawned_ = is_spawned; }
343cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  int status() const { return status_; }
344051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void set_status(int a_status) { status_ = a_status; }
345051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  DeathTestOutcome outcome() const { return outcome_; }
346051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; }
347051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int read_fd() const { return read_fd_; }
348051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void set_read_fd(int fd) { read_fd_ = fd; }
349cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  int write_fd() const { return write_fd_; }
350051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void set_write_fd(int fd) { write_fd_ = fd; }
351051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
352051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // Called in the parent process only. Reads the result code of the death
353051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // test child process via a pipe, interprets it to set the outcome_
354051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // member, and closes read_fd_.  Outputs diagnostics and terminates in
355051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // case of unexpected codes.
356051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void ReadAndInterpretStatusByte();
357051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
358051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt private:
359051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // The textual content of the code this object is testing.  This class
360051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // doesn't own this string and should not attempt to delete it.
361051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const char* const statement_;
362cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // The regular expression which test output must match.  DeathTestImpl
363051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // doesn't own this object and should not attempt to delete it.
364051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const RE* const regex_;
365051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // True if the death test child process has been successfully spawned.
366051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  bool spawned_;
367051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // The exit status of the child process.
368051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int status_;
369cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // How the death test concluded.
370051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  DeathTestOutcome outcome_;
371f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  // Descriptor to the read end of the pipe to the child process.  It is
372051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // always -1 in the child process.  The child keeps its write end of the
373f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  // pipe in write_fd_.
374f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  int read_fd_;
375f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  // Descriptor to the child's write end of the pipe to the parent process.
376f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  // It is always -1 in the parent process.  The parent keeps its end of the
377f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  // pipe in read_fd_.
378f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  int write_fd_;
379f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt};
380051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
381051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Called in the parent process only. Reads the result code of the death
382051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// test child process via a pipe, interprets it to set the outcome_
383051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// member, and closes read_fd_.  Outputs diagnostics and terminates in
384051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// case of unexpected codes.
385051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtvoid DeathTestImpl::ReadAndInterpretStatusByte() {
386051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  char flag;
387051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int bytes_read;
388051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
389051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // The read() here blocks until data is available (signifying the
390cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // failure of the death test) or until the pipe is closed (signifying
391051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // its success), so it's okay to call this in the parent before
392051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // the child process has exited.
393051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  do {
394051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    bytes_read = posix::Read(read_fd(), &flag, 1);
395051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  } while (bytes_read == -1 && errno == EINTR);
396051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
397051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (bytes_read == 0) {
398cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    set_outcome(DIED);
399051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  } else if (bytes_read == 1) {
400f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    switch (flag) {
401051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      case kDeathTestReturned:
402051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        set_outcome(RETURNED);
403051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        break;
404051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      case kDeathTestThrew:
405051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        set_outcome(THREW);
406051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        break;
407051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      case kDeathTestLived:
408051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        set_outcome(LIVED);
409051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        break;
410051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      case kDeathTestInternalError:
411051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        FailFromInternalError(read_fd());  // Does not return.
412051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        break;
413cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      default:
414cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        GTEST_LOG_(FATAL) << "Death test child process reported "
415cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                          << "unexpected status byte ("
416cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                          << static_cast<unsigned int>(flag) << ")";
41704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    }
41804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  } else {
419051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    GTEST_LOG_(FATAL) << "Read from death test child process failed: "
420051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                      << GetLastErrnoDescription();
421051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  }
422cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd()));
423cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  set_read_fd(-1);
424051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
425051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
426051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Signals that the death test code which should have exited, didn't.
4277d5c8f257a74ac0d12828962a492e8b84ef83923Dmitry Shmidt// Should be called only in a death test child process.
4287d5c8f257a74ac0d12828962a492e8b84ef83923Dmitry Shmidt// Writes a status byte to the child's status file descriptor, then
4297d5c8f257a74ac0d12828962a492e8b84ef83923Dmitry Shmidt// calls _exit(1).
430051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtvoid DeathTestImpl::Abort(AbortReason reason) {
431cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // The parent process considers the death test to be a failure if
432051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // it finds any data in our pipe.  So, here we write a single flag byte
433051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // to the pipe, then exit.
434cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  const char status_ch =
435051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      reason == TEST_DID_NOT_DIE ? kDeathTestLived :
436051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned;
437051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
438cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));
43904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // We are leaking the descriptor here because on some platforms (i.e.,
440cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // when built as Windows DLL), destructors of global objects will still
441cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // run after calling _exit(). On such systems, write_fd_ will be
442051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // indirectly closed from the destructor of UnitTestImpl, causing double
443cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // close if it is also closed here. On debug configurations, double close
444cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // may assert. As there are no in-process buffers to flush here, we are
44504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // relying on the OS to close the descriptor after the process terminates
446cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // when the destructors are not run.
447cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  _exit(1);  // Exits w/o any normal exit hooks (we were supposed to crash)
448cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
449cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
450cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Returns an indented copy of stderr output for a death test.
451cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// This makes distinguishing death test output lines from regular log lines
452cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// much easier.
453cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic ::std::string FormatDeathTestOutput(const ::std::string& output) {
454344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt  ::std::string ret;
455cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  for (size_t at = 0; ; ) {
456cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    const size_t line_end = output.find('\n', at);
457051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    ret += "[  DEATH   ] ";
458051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    if (line_end == ::std::string::npos) {
459051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      ret += output.substr(at);
460051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      break;
461051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    }
462cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    ret += output.substr(at, line_end + 1 - at);
463051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    at = line_end + 1;
464051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  }
465051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return ret;
466051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
467051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
468cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Assesses the success or failure of a death test, using both private
469051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// members which have previously been set, and one argument:
470051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
471051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Private data members:
472051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//   outcome:  An enumeration describing how the death test
473cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//             concluded: DIED, LIVED, THREW, or RETURNED.  The death test
474cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//             fails in the latter three cases.
475051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//   status:   The exit status of the child process. On *nix, it is in the
476051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//             in the format specified by wait(2). On Windows, this is the
477051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//             value supplied to the ExitProcess() API or a numeric code
478051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//             of the exception that terminated the program.
479051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//   regex:    A regular expression object to be applied to
480051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//             the test's captured standard error output; the death test
481051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//             fails if it does not match.
482051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
483051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Argument:
484051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//   status_ok: true if exit_status is acceptable in the context of
485051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//              this particular death test, which fails if it is false
486051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
487051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Returns true iff all of the above conditions are met.  Otherwise, the
488cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// first failing condition, in the order given above, is the one that is
489051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// reported. Also sets the last death test message string.
490051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtbool DeathTestImpl::Passed(bool status_ok) {
491051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (!spawned())
492051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    return false;
493051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
494051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const String error_message = GetCapturedStderr();
495051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
496051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  bool success = false;
497cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  Message buffer;
498051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
499051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  buffer << "Death test: " << statement() << "\n";
500051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  switch (outcome()) {
501051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    case LIVED:
502051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      buffer << "    Result: failed to die.\n"
503051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt             << " Error msg:\n" << FormatDeathTestOutput(error_message);
504051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      break;
505051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    case THREW:
506051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      buffer << "    Result: threw an exception.\n"
507051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt             << " Error msg:\n" << FormatDeathTestOutput(error_message);
508051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      break;
509051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    case RETURNED:
510051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      buffer << "    Result: illegal return in test statement.\n"
511cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt             << " Error msg:\n" << FormatDeathTestOutput(error_message);
512cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      break;
513051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    case DIED:
514051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      if (status_ok) {
515051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        const bool matched = RE::PartialMatch(error_message.c_str(), *regex());
516051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        if (matched) {
517051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          success = true;
518051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        } else {
519051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          buffer << "    Result: died but not with expected error.\n"
520051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                 << "  Expected: " << regex()->pattern() << "\n"
521051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                 << "Actual msg:\n" << FormatDeathTestOutput(error_message);
522051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        }
523051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      } else {
524051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        buffer << "    Result: died but not with expected exit code:\n"
525051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt               << "            " << ExitSummary(status()) << "\n"
526051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt               << "Actual msg:\n" << FormatDeathTestOutput(error_message);
527051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      }
528051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      break;
529051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    case IN_PROGRESS:
530051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    default:
531051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      GTEST_LOG_(FATAL)
532051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt          << "DeathTest::Passed somehow called before conclusion of test";
533051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  }
534051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
535cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  DeathTest::set_last_death_test_message(buffer.GetString());
536051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return success;
537051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
538051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
539051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt# if GTEST_OS_WINDOWS
540051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// WindowsDeathTest implements death tests on Windows. Due to the
541051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// specifics of starting new processes on Windows, death tests there are
542051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// always threadsafe, and Google Test considers the
543cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// --gtest_death_test_style=fast setting to be equivalent to
544051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// --gtest_death_test_style=threadsafe there.
545051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
546a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// A few implementation notes:  Like the Linux version, the Windows
547051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// implementation uses pipes for child-to-parent communication. But due to
548051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// the specifics of pipes on Windows, some extra steps are required:
549a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt//
550051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// 1. The parent creates a communication pipe and stores handles to both
551051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//    ends of it.
552051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// 2. The parent starts the child and provides it with the information
553051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//    necessary to acquire the handle to the write end of the pipe.
554cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// 3. The child acquires the write end of the pipe and signals the parent
555a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt//    using a Windows event.
556a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// 4. Now the parent can release the write end of the pipe on its side. If
557051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//    this is done before step 3, the object's reference count goes down to
55868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt//    0 and it is destroyed, preventing the child from acquiring it. The
559cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//    parent now has to release it, or read operations on the read end of
56068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt//    the pipe will not return when the child terminates.
56168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt// 5. The parent reads child's output through the pipe (outcome code and
562051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//    any possible error messages) from the pipe, and its stderr and then
563051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//    determines whether to fail the test.
564051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
565051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Note: to distinguish Win32 API calls from the local method and function
566051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// calls, the former are explicitly resolved in the global namespace.
567cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt//
568cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtclass WindowsDeathTest : public DeathTestImpl {
569051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt public:
570051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  WindowsDeathTest(const char* a_statement,
571051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                   const RE* a_regex,
572051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                   const char* file,
573051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                   int line)
574051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}
575051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
576051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // All of these virtual functions are inherited from DeathTest.
577051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  virtual int Wait();
578051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  virtual TestRole AssumeRole();
579051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
580051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt private:
581051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // The name of the file in which the death test is located.
582051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const char* const file_;
583051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // The line number on which the death test is located.
584051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const int line_;
585051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // Handle to the write end of the pipe to the child process.
586051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  AutoHandle write_handle_;
587051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // Child process handle.
588051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  AutoHandle child_handle_;
589051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // Event the child process uses to signal the parent that it has
590051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // acquired the handle to the write end of the pipe. After seeing this
591051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // event the parent can release its own handles to make sure its
59268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt  // ReadFile() calls return when the child terminates.
59368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt  AutoHandle event_handle_;
594051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt};
595051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
596051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// Waits for the child in a death test to exit, returning its exit
597051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// status, or 0 if no child process exists.  As a side effect, sets the
598051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// outcome data member.
599051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtint WindowsDeathTest::Wait() {
600051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (!spawned())
601051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    return 0;
602051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
603051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // Wait until the child either signals that it has acquired the write end
604051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // of the pipe or it dies.
605051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() };
606051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  switch (::WaitForMultipleObjects(2,
607051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                   wait_handles,
608df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt                                   FALSE,  // Waits for any of the handles.
609df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt                                   INFINITE)) {
610df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    case WAIT_OBJECT_0:
611df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    case WAIT_OBJECT_0 + 1:
612df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt      break;
613df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    default:
614df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt      GTEST_DEATH_TEST_CHECK_(false);  // Should not get here.
615df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  }
616df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt
617df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // The child has acquired the write end of the pipe or exited.
618df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // We release the handle on our side and continue.
619df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  write_handle_.Reset();
620df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  event_handle_.Reset();
621df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt
622df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  ReadAndInterpretStatusByte();
623df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt
624df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // Waits for the child process to exit if it haven't already. This
625df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // returns immediately if the child has already exited, regardless of
626df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // whether previous calls to WaitForMultipleObjects synchronized on this
627df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // handle or not.
628df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(
629df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt      WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(),
630051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                             INFINITE));
631051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  DWORD status_code;
632051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(
633051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE);
634051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  child_handle_.Reset();
635051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  set_status(static_cast<int>(status_code));
636cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return status();
637051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
638051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
639a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt// The AssumeRole process for a Windows death test.  It creates a child
64004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// process with the same executable as the current process to run the
641051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// death test.  The child process is given the --gtest_filter and
642cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// --gtest_internal_run_death_test flags such that it knows to run the
643cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// current death test only.
644051af73b8f8014eff33330aead0f36944b3403e6Dmitry ShmidtDeathTest::TestRole WindowsDeathTest::AssumeRole() {
645051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const UnitTestImpl* const impl = GetUnitTestImpl();
646a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  const InternalRunDeathTestFlag* const flag =
647a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt      impl->internal_run_death_test_flag();
648051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const TestInfo* const info = impl->current_test_info();
649051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const int death_test_index = info->result()->death_test_count();
650051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
651051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (flag != NULL) {
652a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    // ParseInternalRunDeathTestFlag() has performed all the necessary
653051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    // processing.
654df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    set_write_fd(flag->write_fd());
655df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt    return EXECUTE_TEST;
656df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  }
657df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt
658051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // WindowsDeathTest uses an anonymous pipe to communicate results of
659cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // a death test.
660051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  SECURITY_ATTRIBUTES handles_are_inheritable = {
661051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
662051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  HANDLE read_handle, write_handle;
663051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(
664051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable,
665051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                   0)  // Default buffer size.
666051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      != FALSE);
667cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle),
668051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                O_RDONLY));
669051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  write_handle_.Reset(write_handle);
670051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  event_handle_.Reset(::CreateEvent(
671051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      &handles_are_inheritable,
672051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      TRUE,    // The event will automatically reset to non-signaled state.
673051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      FALSE,   // The initial state is non-signalled.
674051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      NULL));  // The even is unnamed.
675cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL);
676051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const String filter_flag = String::Format("--%s%s=%s.%s",
677051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                            GTEST_FLAG_PREFIX_, kFilterFlag,
678051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                            info->test_case_name(),
679051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                            info->name());
68096be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt  const String internal_flag = String::Format(
68196be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt    "--%s%s=%s|%d|%d|%u|%Iu|%Iu",
682cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      GTEST_FLAG_PREFIX_,
68304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      kInternalRunDeathTestFlag,
68404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      file_, line_,
685051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      death_test_index,
686051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      static_cast<unsigned int>(::GetCurrentProcessId()),
687051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      // size_t has the same with as pointers on both 32-bit and 64-bit
688051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      // Windows platforms.
689cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx.
690cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      reinterpret_cast<size_t>(write_handle),
691cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      reinterpret_cast<size_t>(event_handle_.Get()));
692cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
693cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  char executable_path[_MAX_PATH + 1];  // NOLINT
694cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(
695051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      _MAX_PATH + 1 != ::GetModuleFileNameA(NULL,
696051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                            executable_path,
697051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                            _MAX_PATH));
698051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
699cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  String command_line = String::Format("%s %s \"%s\"",
700cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                                       ::GetCommandLineA(),
701cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                                       filter_flag.c_str(),
702df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt                                       internal_flag.c_str());
703cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
704a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  DeathTest::set_last_death_test_message("");
705a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt
706a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  CaptureStderr();
707df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  // Flush the log buffers since the log streams are shared with the child.
708df5a7e4c5c64890c2425bb47d665bbce4992b676Dmitry Shmidt  FlushInfoLog();
709a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt
710a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  // The child process will share the standard handles with the parent.
711a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  STARTUPINFOA startup_info;
712a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  memset(&startup_info, 0, sizeof(STARTUPINFO));
713a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  startup_info.dwFlags = STARTF_USESTDHANDLES;
714a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
715a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
716a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
717a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt
718a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  PROCESS_INFORMATION process_info;
719a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(::CreateProcessA(
720a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt      executable_path,
721a38abf9af7bec7e89dbfb39ac7bb77223fe47c72Dmitry Shmidt      const_cast<char*>(command_line.c_str()),
722051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      NULL,   // Retuned process handle is not inheritable.
723051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      NULL,   // Retuned thread handle is not inheritable.
724051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      TRUE,   // Child inherits all inheritable handles (for write_handle_).
725051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      0x0,    // Default creation flags.
726051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      NULL,   // Inherit the parent's environment.
727051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      UnitTest::GetInstance()->original_working_dir(),
728051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt      &startup_info,
729cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt      &process_info) != FALSE);
730051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  child_handle_.Reset(process_info.hProcess);
731051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  ::CloseHandle(process_info.hThread);
732051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  set_spawned(true);
733cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return OVERSEE_TEST;
734cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
735cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt# else  // We are not on Windows.
736cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
737051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// ForkingDeathTest provides implementations for most of the abstract
738051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// methods of the DeathTest interface.  Only the AssumeRole method is
739cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// left undefined.
740051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtclass ForkingDeathTest : public DeathTestImpl {
741051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt public:
742cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  ForkingDeathTest(const char* statement, const RE* regex);
743cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
744051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // All of these virtual functions are inherited from DeathTest.
745051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  virtual int Wait();
746051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
747051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt protected:
748051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; }
749051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
75004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt private:
751051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // PID of child process during death test; 0 in the child process itself.
752051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  pid_t child_pid_;
75304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt};
75496be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt
75596be6222df414a7dde5c5b1b72df22e67b1a77fcDmitry Shmidt// Constructs a ForkingDeathTest.
75604f534e89ed127da4077485376f24debc50d80d5Dmitry ShmidtForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex)
757051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    : DeathTestImpl(a_statement, a_regex),
75804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      child_pid_(-1) {}
75904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
76004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// Waits for the child in a death test to exit, returning its exit
76104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// status, or 0 if no child process exists.  As a side effect, sets the
76204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// outcome data member.
76304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtint ForkingDeathTest::Wait() {
76404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  if (!spawned())
76504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    return 0;
76604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
76704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  ReadAndInterpretStatusByte();
76804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
76904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  int status_value;
77004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0));
77104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  set_status(status_value);
77204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  return status_value;
77304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt}
77404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
77504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// A concrete death test class that forks, then immediately runs the test
77604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// in the child process.
77704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtclass NoExecDeathTest : public ForkingDeathTest {
77804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt public:
77904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  NoExecDeathTest(const char* a_statement, const RE* a_regex) :
78004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      ForkingDeathTest(a_statement, a_regex) { }
78104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  virtual TestRole AssumeRole();
78204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt};
78304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
78404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// The AssumeRole process for a fork-and-run death test.  It implements a
78504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// straightforward fork, with a simple pipe to transmit the status byte.
78604f534e89ed127da4077485376f24debc50d80d5Dmitry ShmidtDeathTest::TestRole NoExecDeathTest::AssumeRole() {
78704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  const size_t thread_count = GetThreadCount();
78804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  if (thread_count != 1) {
78904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);
79004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
79104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
792d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  int pipe_fd[2];
793d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
794d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt
795d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  DeathTest::set_last_death_test_message("");
796d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  CaptureStderr();
797d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  // When we fork the process below, the log file buffers are copied, but the
798d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  // file descriptors are shared.  We flush all log files here so that closing
799d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  // the file descriptors in the child process doesn't throw off the
800d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  // synchronization between descriptors and buffers in the parent process.
801d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt  // This is as close to the fork as possible to avoid a race condition in case
80204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // there are multiple threads running before the death test, and another
80304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // thread writes to the log file.
80404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  FlushInfoLog();
805cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
806cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  const pid_t child_pid = fork();
807cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_(child_pid != -1);
80804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  set_child_pid(child_pid);
80904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  if (child_pid == 0) {
810d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0]));
81104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    set_write_fd(pipe_fd[1]);
81204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    // Redirects all logging to stderr in the child process to prevent
813344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt    // concurrent writes to the log files.  We capture stderr in the parent
814344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt    // process and append the child process' output to a log.
815d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt    LogToStderr();
816344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt    // Event forwarding to the listeners of event listener API mush be shut
817344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt    // down in death test subprocesses.
818d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt    GetUnitTestImpl()->listeners()->SuppressEventForwarding();
819344abd362cfe2d03ed956666527352826b67bde5Dmitry Shmidt    return EXECUTE_TEST;
820051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  } else {
82104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
82204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    set_read_fd(pipe_fd[0]);
82304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    set_spawned(true);
82404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    return OVERSEE_TEST;
82504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
826cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
827cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
82804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// A concrete death test class that forks and re-executes the main
82904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// program from the beginning, with command-line flags set that cause
83004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// only this specific death test to be run.
83104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtclass ExecDeathTest : public ForkingDeathTest {
8329866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt public:
8339866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  ExecDeathTest(const char* a_statement, const RE* a_regex,
8349866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt                const char* file, int line) :
8359866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt      ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { }
8369866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  virtual TestRole AssumeRole();
8379866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt private:
8389866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  // The name of the file in which the death test is located.
8399866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  const char* const file_;
8409866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  // The line number on which the death test is located.
8419866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  const int line_;
8429866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt};
8439866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
8449866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt// Utility class for accumulating command-line arguments.
8459866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidtclass Arguments {
8469866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt public:
8479866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  Arguments() {
8489866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    args_.push_back(NULL);
8499866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  }
8509866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
8519866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  ~Arguments() {
8529866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
8539866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt         ++i) {
8549866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt      free(*i);
8559866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    }
85604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
8579866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt  void AddArgument(const char* argument) {
8589866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    args_.insert(args_.end() - 1, posix::StrDup(argument));
85904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
86004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
86104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  template <typename Str>
86204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  void AddArguments(const ::std::vector<Str>& arguments) {
86304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
86404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt         i != arguments.end();
86504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt         ++i) {
86604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
86704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    }
86804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
86904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  char* const* Argv() {
87004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    return &args_[0];
87104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  }
87204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt private:
87304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  std::vector<char*> args_;
87404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt};
87504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
87604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// A struct that encompasses the arguments to the child process of a
87704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// threadsafe-style death test process.
87804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtstruct ExecDeathTestArgs {
87904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  char* const* argv;  // Command-line arguments for the child's call to exec
88004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  int close_fd;       // File descriptor to close; the read end of a pipe
88104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt};
88204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
88304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt#  if GTEST_OS_MAC
88404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidtinline char** GetEnviron() {
88504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // When Google Test is built as a framework on MacOS X, the environ variable
88604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // is unavailable. Apple's documentation (man environ) recommends using
88704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  // _NSGetEnviron() instead.
888cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return *_NSGetEnviron();
889d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt}
890d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt#  else
891d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt// Some POSIX platforms expect you to declare environ. extern "C" makes
892d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidt// it reside in the global namespace.
893d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidtextern "C" char** environ;
894d30ac604c9f6da71a0dd7f46d25be05a2a62cfbbDmitry Shmidtinline char** GetEnviron() { return environ; }
89504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt#  endif  // GTEST_OS_MAC
89604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
89704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt// The main function for a threadsafe-style death test child process.
898cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// This function is called in a clone()-ed process and thus must avoid
899cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// any potentially unsafe operations like malloc or libc functions.
900cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic int ExecDeathTestChildMain(void* child_arg) {
901cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg);
902cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd));
903cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
904cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // We need to execute the test program in the same environment where
905051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // it was originally invoked.  Therefore we change to the original
906cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // working directory first.
90704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt  const char* const original_dir =
90804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt      UnitTest::GetInstance()->original_working_dir();
90968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt  // We can safely call chdir() as it's a direct system call.
910051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  if (chdir(original_dir) != 0) {
91104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    DeathTestAbort(String::Format("chdir(\"%s\") failed: %s",
91204f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt                                  original_dir,
91304f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt                                  GetLastErrnoDescription().c_str()));
91404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    return EXIT_FAILURE;
915051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  }
916051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
917051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // We can safely call execve() as it's a direct system call.  We
918051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // cannot use execvp() as it's a libc function and thus potentially
919cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  // unsafe.  Since execve() doesn't search the PATH, the user must
920051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // invoke the test program via a valid path that contains at least
921051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  // one path separator.
922051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  execve(args->argv[0], args->argv, GetEnviron());
923051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  DeathTestAbort(String::Format("execve(%s, ...) in %s failed: %s",
924051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                args->argv[0],
925cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                                original_dir,
926051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                                GetLastErrnoDescription().c_str()));
927051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return EXIT_FAILURE;
928cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
929cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
930cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// Two utility routines that together determine the direction the stack
931cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// grows.
932051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// This could be accomplished more elegantly by a single recursive
9336aa8ae4b6139bdf5593301474277dcdbc2528190Dmitry Shmidt// function, but we want to guard against the unlikely possibility of
9346aa8ae4b6139bdf5593301474277dcdbc2528190Dmitry Shmidt// a smart compiler optimizing the recursion away.
935051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt//
936051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining
937cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// StackLowerThanAddress into StackGrowsDown, which then doesn't give
938051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// correct answer.
939051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtbool StackLowerThanAddress(const void* ptr) GTEST_NO_INLINE_;
940051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtbool StackLowerThanAddress(const void* ptr) {
941051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int dummy;
942cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  return &dummy < ptr;
943051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt}
944051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
945051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtbool StackGrowsDown() {
946051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  int dummy;
947051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  return StackLowerThanAddress(&dummy);
948cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
949051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
950051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// A threadsafe implementation of fork(2) for threadsafe-style death tests
951051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt// that uses clone(2).  It dies with an error message if anything goes
952cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt// wrong.
953cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtstatic pid_t ExecDeathTestFork(char* const* argv, int close_fd) {
954cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt  ExecDeathTestArgs args = { argv, close_fd };
955051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  pid_t child_pid = -1;
956cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
957cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt#  if GTEST_HAS_CLONE
958051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt  const bool use_fork = GTEST_FLAG(death_test_use_fork);
959051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
960f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  if (!use_fork) {
961f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    static const bool stack_grows_down = StackGrowsDown();
962f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    const size_t stack_size = getpagesize();
963f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead.
964a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
965f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt                             MAP_ANON | MAP_PRIVATE, -1, 0);
96661593f02176862f4880ddefcb1f54cb5f5d9f043Dmitry Shmidt    GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED);
96761593f02176862f4880ddefcb1f54cb5f5d9f043Dmitry Shmidt    void* const stack_top =
96861593f02176862f4880ddefcb1f54cb5f5d9f043Dmitry Shmidt        static_cast<char*>(stack) + (stack_grows_down ? stack_size : 0);
969f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
970f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args);
971a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
972f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1);
973f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  }
974f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt#  else
975f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  const bool use_fork = true;
976a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt#  endif  // GTEST_HAS_CLONE
977f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
978f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt  if (use_fork && (child_pid = fork()) == 0) {
979a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt      ExecDeathTestChildMain(&args);
980a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt      _exit(0);
981a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  }
982a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
983a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  GTEST_DEATH_TEST_CHECK_(child_pid != -1);
984a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt  return child_pid;
985f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt}
986
987// The AssumeRole process for a fork-and-exec death test.  It re-executes the
988// main program from the beginning, setting the --gtest_filter
989// and --gtest_internal_run_death_test flags to cause only the current
990// death test to be re-run.
991DeathTest::TestRole ExecDeathTest::AssumeRole() {
992  const UnitTestImpl* const impl = GetUnitTestImpl();
993  const InternalRunDeathTestFlag* const flag =
994      impl->internal_run_death_test_flag();
995  const TestInfo* const info = impl->current_test_info();
996  const int death_test_index = info->result()->death_test_count();
997
998  if (flag != NULL) {
999    set_write_fd(flag->write_fd());
1000    return EXECUTE_TEST;
1001  }
1002
1003  int pipe_fd[2];
1004  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
1005  // Clear the close-on-exec flag on the write end of the pipe, lest
1006  // it be closed when the child process does an exec:
1007  GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1);
1008
1009  const String filter_flag =
1010      String::Format("--%s%s=%s.%s",
1011                     GTEST_FLAG_PREFIX_, kFilterFlag,
1012                     info->test_case_name(), info->name());
1013  const String internal_flag =
1014      String::Format("--%s%s=%s|%d|%d|%d",
1015                     GTEST_FLAG_PREFIX_, kInternalRunDeathTestFlag,
1016                     file_, line_, death_test_index, pipe_fd[1]);
1017  Arguments args;
1018  args.AddArguments(GetArgvs());
1019  args.AddArgument(filter_flag.c_str());
1020  args.AddArgument(internal_flag.c_str());
1021
1022  DeathTest::set_last_death_test_message("");
1023
1024  CaptureStderr();
1025  // See the comment in NoExecDeathTest::AssumeRole for why the next line
1026  // is necessary.
1027  FlushInfoLog();
1028
1029  const pid_t child_pid = ExecDeathTestFork(args.Argv(), pipe_fd[0]);
1030  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
1031  set_child_pid(child_pid);
1032  set_read_fd(pipe_fd[0]);
1033  set_spawned(true);
1034  return OVERSEE_TEST;
1035}
1036
1037# endif  // !GTEST_OS_WINDOWS
1038
1039// Creates a concrete DeathTest-derived class that depends on the
1040// --gtest_death_test_style flag, and sets the pointer pointed to
1041// by the "test" argument to its address.  If the test should be
1042// skipped, sets that pointer to NULL.  Returns true, unless the
1043// flag is set to an invalid value.
1044bool DefaultDeathTestFactory::Create(const char* statement, const RE* regex,
1045                                     const char* file, int line,
1046                                     DeathTest** test) {
1047  UnitTestImpl* const impl = GetUnitTestImpl();
1048  const InternalRunDeathTestFlag* const flag =
1049      impl->internal_run_death_test_flag();
1050  const int death_test_index = impl->current_test_info()
1051      ->increment_death_test_count();
1052
1053  if (flag != NULL) {
1054    if (death_test_index > flag->index()) {
1055      DeathTest::set_last_death_test_message(String::Format(
1056          "Death test count (%d) somehow exceeded expected maximum (%d)",
1057          death_test_index, flag->index()));
1058      return false;
1059    }
1060
1061    if (!(flag->file() == file && flag->line() == line &&
1062          flag->index() == death_test_index)) {
1063      *test = NULL;
1064      return true;
1065    }
1066  }
1067
1068# if GTEST_OS_WINDOWS
1069
1070  if (GTEST_FLAG(death_test_style) == "threadsafe" ||
1071      GTEST_FLAG(death_test_style) == "fast") {
1072    *test = new WindowsDeathTest(statement, regex, file, line);
1073  }
1074
1075# else
1076
1077  if (GTEST_FLAG(death_test_style) == "threadsafe") {
1078    *test = new ExecDeathTest(statement, regex, file, line);
1079  } else if (GTEST_FLAG(death_test_style) == "fast") {
1080    *test = new NoExecDeathTest(statement, regex);
1081  }
1082
1083# endif  // GTEST_OS_WINDOWS
1084
1085  else {  // NOLINT - this is more readable than unbalanced brackets inside #if.
1086    DeathTest::set_last_death_test_message(String::Format(
1087        "Unknown death test style \"%s\" encountered",
1088        GTEST_FLAG(death_test_style).c_str()));
1089    return false;
1090  }
1091
1092  return true;
1093}
1094
1095// Splits a given string on a given delimiter, populating a given
1096// vector with the fields.  GTEST_HAS_DEATH_TEST implies that we have
1097// ::std::string, so we can use it here.
1098static void SplitString(const ::std::string& str, char delimiter,
1099                        ::std::vector< ::std::string>* dest) {
1100  ::std::vector< ::std::string> parsed;
1101  ::std::string::size_type pos = 0;
1102  while (::testing::internal::AlwaysTrue()) {
1103    const ::std::string::size_type colon = str.find(delimiter, pos);
1104    if (colon == ::std::string::npos) {
1105      parsed.push_back(str.substr(pos));
1106      break;
1107    } else {
1108      parsed.push_back(str.substr(pos, colon - pos));
1109      pos = colon + 1;
1110    }
1111  }
1112  dest->swap(parsed);
1113}
1114
1115# if GTEST_OS_WINDOWS
1116// Recreates the pipe and event handles from the provided parameters,
1117// signals the event, and returns a file descriptor wrapped around the pipe
1118// handle. This function is called in the child process only.
1119int GetStatusFileDescriptor(unsigned int parent_process_id,
1120                            size_t write_handle_as_size_t,
1121                            size_t event_handle_as_size_t) {
1122  AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE,
1123                                                   FALSE,  // Non-inheritable.
1124                                                   parent_process_id));
1125  if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) {
1126    DeathTestAbort(String::Format("Unable to open parent process %u",
1127                                  parent_process_id));
1128  }
1129
1130  // TODO(vladl@google.com): Replace the following check with a
1131  // compile-time assertion when available.
1132  GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t));
1133
1134  const HANDLE write_handle =
1135      reinterpret_cast<HANDLE>(write_handle_as_size_t);
1136  HANDLE dup_write_handle;
1137
1138  // The newly initialized handle is accessible only in in the parent
1139  // process. To obtain one accessible within the child, we need to use
1140  // DuplicateHandle.
1141  if (!::DuplicateHandle(parent_process_handle.Get(), write_handle,
1142                         ::GetCurrentProcess(), &dup_write_handle,
1143                         0x0,    // Requested privileges ignored since
1144                                 // DUPLICATE_SAME_ACCESS is used.
1145                         FALSE,  // Request non-inheritable handler.
1146                         DUPLICATE_SAME_ACCESS)) {
1147    DeathTestAbort(String::Format(
1148        "Unable to duplicate the pipe handle %Iu from the parent process %u",
1149        write_handle_as_size_t, parent_process_id));
1150  }
1151
1152  const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t);
1153  HANDLE dup_event_handle;
1154
1155  if (!::DuplicateHandle(parent_process_handle.Get(), event_handle,
1156                         ::GetCurrentProcess(), &dup_event_handle,
1157                         0x0,
1158                         FALSE,
1159                         DUPLICATE_SAME_ACCESS)) {
1160    DeathTestAbort(String::Format(
1161        "Unable to duplicate the event handle %Iu from the parent process %u",
1162        event_handle_as_size_t, parent_process_id));
1163  }
1164
1165  const int write_fd =
1166      ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND);
1167  if (write_fd == -1) {
1168    DeathTestAbort(String::Format(
1169        "Unable to convert pipe handle %Iu to a file descriptor",
1170        write_handle_as_size_t));
1171  }
1172
1173  // Signals the parent that the write end of the pipe has been acquired
1174  // so the parent can release its own write end.
1175  ::SetEvent(dup_event_handle);
1176
1177  return write_fd;
1178}
1179# endif  // GTEST_OS_WINDOWS
1180
1181// Returns a newly created InternalRunDeathTestFlag object with fields
1182// initialized from the GTEST_FLAG(internal_run_death_test) flag if
1183// the flag is specified; otherwise returns NULL.
1184InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
1185  if (GTEST_FLAG(internal_run_death_test) == "") return NULL;
1186
1187  // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we
1188  // can use it here.
1189  int line = -1;
1190  int index = -1;
1191  ::std::vector< ::std::string> fields;
1192  SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields);
1193  int write_fd = -1;
1194
1195# if GTEST_OS_WINDOWS
1196
1197  unsigned int parent_process_id = 0;
1198  size_t write_handle_as_size_t = 0;
1199  size_t event_handle_as_size_t = 0;
1200
1201  if (fields.size() != 6
1202      || !ParseNaturalNumber(fields[1], &line)
1203      || !ParseNaturalNumber(fields[2], &index)
1204      || !ParseNaturalNumber(fields[3], &parent_process_id)
1205      || !ParseNaturalNumber(fields[4], &write_handle_as_size_t)
1206      || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) {
1207    DeathTestAbort(String::Format(
1208        "Bad --gtest_internal_run_death_test flag: %s",
1209        GTEST_FLAG(internal_run_death_test).c_str()));
1210  }
1211  write_fd = GetStatusFileDescriptor(parent_process_id,
1212                                     write_handle_as_size_t,
1213                                     event_handle_as_size_t);
1214# else
1215
1216  if (fields.size() != 4
1217      || !ParseNaturalNumber(fields[1], &line)
1218      || !ParseNaturalNumber(fields[2], &index)
1219      || !ParseNaturalNumber(fields[3], &write_fd)) {
1220    DeathTestAbort(String::Format(
1221        "Bad --gtest_internal_run_death_test flag: %s",
1222        GTEST_FLAG(internal_run_death_test).c_str()));
1223  }
1224
1225# endif  // GTEST_OS_WINDOWS
1226
1227  return new InternalRunDeathTestFlag(fields[0], line, index, write_fd);
1228}
1229
1230}  // namespace internal
1231
1232#endif  // GTEST_HAS_DEATH_TEST
1233
1234}  // namespace testing
1235