1*9880d681SAndroid Build Coastguard Worker // Copyright 2005, Google Inc.
2*9880d681SAndroid Build Coastguard Worker // All rights reserved.
3*9880d681SAndroid Build Coastguard Worker //
4*9880d681SAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without
5*9880d681SAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are
6*9880d681SAndroid Build Coastguard Worker // met:
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker // * Redistributions of source code must retain the above copyright
9*9880d681SAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer.
10*9880d681SAndroid Build Coastguard Worker // * Redistributions in binary form must reproduce the above
11*9880d681SAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following disclaimer
12*9880d681SAndroid Build Coastguard Worker // in the documentation and/or other materials provided with the
13*9880d681SAndroid Build Coastguard Worker // distribution.
14*9880d681SAndroid Build Coastguard Worker // * Neither the name of Google Inc. nor the names of its
15*9880d681SAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived from
16*9880d681SAndroid Build Coastguard Worker // this software without specific prior written permission.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*9880d681SAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*9880d681SAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*9880d681SAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*9880d681SAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*9880d681SAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*9880d681SAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*9880d681SAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*9880d681SAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*9880d681SAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*9880d681SAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*9880d681SAndroid Build Coastguard Worker //
30*9880d681SAndroid Build Coastguard Worker // Author: [email protected] (Zhanyong Wan), [email protected] (Vlad Losev)
31*9880d681SAndroid Build Coastguard Worker //
32*9880d681SAndroid Build Coastguard Worker // This file implements death tests.
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest-death-test.h"
35*9880d681SAndroid Build Coastguard Worker #include "gtest/internal/gtest-port.h"
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker #if GTEST_HAS_DEATH_TEST
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_MAC
40*9880d681SAndroid Build Coastguard Worker # include <crt_externs.h>
41*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_MAC
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker # include <errno.h>
44*9880d681SAndroid Build Coastguard Worker # include <fcntl.h>
45*9880d681SAndroid Build Coastguard Worker # include <limits.h>
46*9880d681SAndroid Build Coastguard Worker # include <stdarg.h>
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
49*9880d681SAndroid Build Coastguard Worker # include <windows.h>
50*9880d681SAndroid Build Coastguard Worker # else
51*9880d681SAndroid Build Coastguard Worker # include <sys/mman.h>
52*9880d681SAndroid Build Coastguard Worker # include <sys/wait.h>
53*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker #endif // GTEST_HAS_DEATH_TEST
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest-message.h"
58*9880d681SAndroid Build Coastguard Worker #include "gtest/internal/gtest-string.h"
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker // Indicates that this translation unit is part of Google Test's
61*9880d681SAndroid Build Coastguard Worker // implementation. It must come before gtest-internal-inl.h is
62*9880d681SAndroid Build Coastguard Worker // included, or there will be a compiler error. This trick is to
63*9880d681SAndroid Build Coastguard Worker // prevent a user from accidentally including gtest-internal-inl.h in
64*9880d681SAndroid Build Coastguard Worker // his code.
65*9880d681SAndroid Build Coastguard Worker #define GTEST_IMPLEMENTATION_ 1
66*9880d681SAndroid Build Coastguard Worker #include "src/gtest-internal-inl.h"
67*9880d681SAndroid Build Coastguard Worker #undef GTEST_IMPLEMENTATION_
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker namespace testing {
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker // Constants.
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // The default death test style.
74*9880d681SAndroid Build Coastguard Worker static const char kDefaultDeathTestStyle[] = "fast";
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker GTEST_DEFINE_string_(
77*9880d681SAndroid Build Coastguard Worker death_test_style,
78*9880d681SAndroid Build Coastguard Worker internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle),
79*9880d681SAndroid Build Coastguard Worker "Indicates how to run a death test in a forked child process: "
80*9880d681SAndroid Build Coastguard Worker "\"threadsafe\" (child process re-executes the test binary "
81*9880d681SAndroid Build Coastguard Worker "from the beginning, running only the specific death test) or "
82*9880d681SAndroid Build Coastguard Worker "\"fast\" (child process runs the death test immediately "
83*9880d681SAndroid Build Coastguard Worker "after forking).");
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker GTEST_DEFINE_bool_(
86*9880d681SAndroid Build Coastguard Worker death_test_use_fork,
87*9880d681SAndroid Build Coastguard Worker internal::BoolFromGTestEnv("death_test_use_fork", false),
88*9880d681SAndroid Build Coastguard Worker "Instructs to use fork()/_exit() instead of clone() in death tests. "
89*9880d681SAndroid Build Coastguard Worker "Ignored and always uses fork() on POSIX systems where clone() is not "
90*9880d681SAndroid Build Coastguard Worker "implemented. Useful when running under valgrind or similar tools if "
91*9880d681SAndroid Build Coastguard Worker "those do not support clone(). Valgrind 3.3.1 will just fail if "
92*9880d681SAndroid Build Coastguard Worker "it sees an unsupported combination of clone() flags. "
93*9880d681SAndroid Build Coastguard Worker "It is not recommended to use this flag w/o valgrind though it will "
94*9880d681SAndroid Build Coastguard Worker "work in 99% of the cases. Once valgrind is fixed, this flag will "
95*9880d681SAndroid Build Coastguard Worker "most likely be removed.");
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker namespace internal {
98*9880d681SAndroid Build Coastguard Worker GTEST_DEFINE_string_(
99*9880d681SAndroid Build Coastguard Worker internal_run_death_test, "",
100*9880d681SAndroid Build Coastguard Worker "Indicates the file, line number, temporal index of "
101*9880d681SAndroid Build Coastguard Worker "the single death test to run, and a file descriptor to "
102*9880d681SAndroid Build Coastguard Worker "which a success code may be sent, all separated by "
103*9880d681SAndroid Build Coastguard Worker "colons. This flag is specified if and only if the current "
104*9880d681SAndroid Build Coastguard Worker "process is a sub-process launched for running a thread-safe "
105*9880d681SAndroid Build Coastguard Worker "death test. FOR INTERNAL USE ONLY.");
106*9880d681SAndroid Build Coastguard Worker } // namespace internal
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker #if GTEST_HAS_DEATH_TEST
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker // ExitedWithCode constructor.
ExitedWithCode(int exit_code)111*9880d681SAndroid Build Coastguard Worker ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker // ExitedWithCode function-call operator.
operator ()(int exit_status) const115*9880d681SAndroid Build Coastguard Worker bool ExitedWithCode::operator()(int exit_status) const {
116*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker return exit_status == exit_code_;
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker # else
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_;
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker # if !GTEST_OS_WINDOWS
128*9880d681SAndroid Build Coastguard Worker // KilledBySignal constructor.
KilledBySignal(int signum)129*9880d681SAndroid Build Coastguard Worker KilledBySignal::KilledBySignal(int signum) : signum_(signum) {
130*9880d681SAndroid Build Coastguard Worker }
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker // KilledBySignal function-call operator.
operator ()(int exit_status) const133*9880d681SAndroid Build Coastguard Worker bool KilledBySignal::operator()(int exit_status) const {
134*9880d681SAndroid Build Coastguard Worker return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_;
135*9880d681SAndroid Build Coastguard Worker }
136*9880d681SAndroid Build Coastguard Worker # endif // !GTEST_OS_WINDOWS
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker namespace internal {
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Utilities needed for death tests.
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker // Generates a textual description of a given exit code, in the format
143*9880d681SAndroid Build Coastguard Worker // specified by wait(2).
ExitSummary(int exit_code)144*9880d681SAndroid Build Coastguard Worker static String ExitSummary(int exit_code) {
145*9880d681SAndroid Build Coastguard Worker Message m;
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker m << "Exited with exit status " << exit_code;
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker # else
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker if (WIFEXITED(exit_code)) {
154*9880d681SAndroid Build Coastguard Worker m << "Exited with exit status " << WEXITSTATUS(exit_code);
155*9880d681SAndroid Build Coastguard Worker } else if (WIFSIGNALED(exit_code)) {
156*9880d681SAndroid Build Coastguard Worker m << "Terminated by signal " << WTERMSIG(exit_code);
157*9880d681SAndroid Build Coastguard Worker }
158*9880d681SAndroid Build Coastguard Worker # ifdef WCOREDUMP
159*9880d681SAndroid Build Coastguard Worker if (WCOREDUMP(exit_code)) {
160*9880d681SAndroid Build Coastguard Worker m << " (core dumped)";
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker # endif
163*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker return m.GetString();
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker // Returns true if exit_status describes a process that was terminated
169*9880d681SAndroid Build Coastguard Worker // by a signal, or exited normally with a nonzero exit code.
ExitedUnsuccessfully(int exit_status)170*9880d681SAndroid Build Coastguard Worker bool ExitedUnsuccessfully(int exit_status) {
171*9880d681SAndroid Build Coastguard Worker return !ExitedWithCode(0)(exit_status);
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker # if !GTEST_OS_WINDOWS
175*9880d681SAndroid Build Coastguard Worker // Generates a textual failure message when a death test finds more than
176*9880d681SAndroid Build Coastguard Worker // one thread running, or cannot determine the number of threads, prior
177*9880d681SAndroid Build Coastguard Worker // to executing the given statement. It is the responsibility of the
178*9880d681SAndroid Build Coastguard Worker // caller not to pass a thread_count of 1.
DeathTestThreadWarning(size_t thread_count)179*9880d681SAndroid Build Coastguard Worker static String DeathTestThreadWarning(size_t thread_count) {
180*9880d681SAndroid Build Coastguard Worker Message msg;
181*9880d681SAndroid Build Coastguard Worker msg << "Death tests use fork(), which is unsafe particularly"
182*9880d681SAndroid Build Coastguard Worker << " in a threaded context. For this test, " << GTEST_NAME_ << " ";
183*9880d681SAndroid Build Coastguard Worker if (thread_count == 0)
184*9880d681SAndroid Build Coastguard Worker msg << "couldn't detect the number of threads.";
185*9880d681SAndroid Build Coastguard Worker else
186*9880d681SAndroid Build Coastguard Worker msg << "detected " << thread_count << " threads.";
187*9880d681SAndroid Build Coastguard Worker return msg.GetString();
188*9880d681SAndroid Build Coastguard Worker }
189*9880d681SAndroid Build Coastguard Worker # endif // !GTEST_OS_WINDOWS
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker // Flag characters for reporting a death test that did not die.
192*9880d681SAndroid Build Coastguard Worker static const char kDeathTestLived = 'L';
193*9880d681SAndroid Build Coastguard Worker static const char kDeathTestReturned = 'R';
194*9880d681SAndroid Build Coastguard Worker static const char kDeathTestThrew = 'T';
195*9880d681SAndroid Build Coastguard Worker static const char kDeathTestInternalError = 'I';
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker // An enumeration describing all of the possible ways that a death test can
198*9880d681SAndroid Build Coastguard Worker // conclude. DIED means that the process died while executing the test
199*9880d681SAndroid Build Coastguard Worker // code; LIVED means that process lived beyond the end of the test code;
200*9880d681SAndroid Build Coastguard Worker // RETURNED means that the test statement attempted to execute a return
201*9880d681SAndroid Build Coastguard Worker // statement, which is not allowed; THREW means that the test statement
202*9880d681SAndroid Build Coastguard Worker // returned control by throwing an exception. IN_PROGRESS means the test
203*9880d681SAndroid Build Coastguard Worker // has not yet concluded.
204*9880d681SAndroid Build Coastguard Worker // TODO([email protected]): Unify names and possibly values for
205*9880d681SAndroid Build Coastguard Worker // AbortReason, DeathTestOutcome, and flag characters above.
206*9880d681SAndroid Build Coastguard Worker enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker // Routine for aborting the program which is safe to call from an
209*9880d681SAndroid Build Coastguard Worker // exec-style death test child process, in which case the error
210*9880d681SAndroid Build Coastguard Worker // message is propagated back to the parent process. Otherwise, the
211*9880d681SAndroid Build Coastguard Worker // message is simply printed to stderr. In either case, the program
212*9880d681SAndroid Build Coastguard Worker // then exits with status 1.
DeathTestAbort(const String & message)213*9880d681SAndroid Build Coastguard Worker void DeathTestAbort(const String& message) {
214*9880d681SAndroid Build Coastguard Worker // On a POSIX system, this function may be called from a threadsafe-style
215*9880d681SAndroid Build Coastguard Worker // death test child process, which operates on a very small stack. Use
216*9880d681SAndroid Build Coastguard Worker // the heap for any additional non-minuscule memory requirements.
217*9880d681SAndroid Build Coastguard Worker const InternalRunDeathTestFlag* const flag =
218*9880d681SAndroid Build Coastguard Worker GetUnitTestImpl()->internal_run_death_test_flag();
219*9880d681SAndroid Build Coastguard Worker if (flag != NULL) {
220*9880d681SAndroid Build Coastguard Worker FILE* parent = posix::FDOpen(flag->write_fd(), "w");
221*9880d681SAndroid Build Coastguard Worker fputc(kDeathTestInternalError, parent);
222*9880d681SAndroid Build Coastguard Worker fprintf(parent, "%s", message.c_str());
223*9880d681SAndroid Build Coastguard Worker fflush(parent);
224*9880d681SAndroid Build Coastguard Worker _exit(1);
225*9880d681SAndroid Build Coastguard Worker } else {
226*9880d681SAndroid Build Coastguard Worker fprintf(stderr, "%s", message.c_str());
227*9880d681SAndroid Build Coastguard Worker fflush(stderr);
228*9880d681SAndroid Build Coastguard Worker posix::Abort();
229*9880d681SAndroid Build Coastguard Worker }
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker // A replacement for CHECK that calls DeathTestAbort if the assertion
233*9880d681SAndroid Build Coastguard Worker // fails.
234*9880d681SAndroid Build Coastguard Worker # define GTEST_DEATH_TEST_CHECK_(expression) \
235*9880d681SAndroid Build Coastguard Worker do { \
236*9880d681SAndroid Build Coastguard Worker if (!::testing::internal::IsTrue(expression)) { \
237*9880d681SAndroid Build Coastguard Worker DeathTestAbort(::testing::internal::String::Format( \
238*9880d681SAndroid Build Coastguard Worker "CHECK failed: File %s, line %d: %s", \
239*9880d681SAndroid Build Coastguard Worker __FILE__, __LINE__, #expression)); \
240*9880d681SAndroid Build Coastguard Worker } \
241*9880d681SAndroid Build Coastguard Worker } while (::testing::internal::AlwaysFalse())
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker // This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for
244*9880d681SAndroid Build Coastguard Worker // evaluating any system call that fulfills two conditions: it must return
245*9880d681SAndroid Build Coastguard Worker // -1 on failure, and set errno to EINTR when it is interrupted and
246*9880d681SAndroid Build Coastguard Worker // should be tried again. The macro expands to a loop that repeatedly
247*9880d681SAndroid Build Coastguard Worker // evaluates the expression as long as it evaluates to -1 and sets
248*9880d681SAndroid Build Coastguard Worker // errno to EINTR. If the expression evaluates to -1 but errno is
249*9880d681SAndroid Build Coastguard Worker // something other than EINTR, DeathTestAbort is called.
250*9880d681SAndroid Build Coastguard Worker # define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \
251*9880d681SAndroid Build Coastguard Worker do { \
252*9880d681SAndroid Build Coastguard Worker int gtest_retval; \
253*9880d681SAndroid Build Coastguard Worker do { \
254*9880d681SAndroid Build Coastguard Worker gtest_retval = (expression); \
255*9880d681SAndroid Build Coastguard Worker } while (gtest_retval == -1 && errno == EINTR); \
256*9880d681SAndroid Build Coastguard Worker if (gtest_retval == -1) { \
257*9880d681SAndroid Build Coastguard Worker DeathTestAbort(::testing::internal::String::Format( \
258*9880d681SAndroid Build Coastguard Worker "CHECK failed: File %s, line %d: %s != -1", \
259*9880d681SAndroid Build Coastguard Worker __FILE__, __LINE__, #expression)); \
260*9880d681SAndroid Build Coastguard Worker } \
261*9880d681SAndroid Build Coastguard Worker } while (::testing::internal::AlwaysFalse())
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker // Returns the message describing the last system error in errno.
GetLastErrnoDescription()264*9880d681SAndroid Build Coastguard Worker String GetLastErrnoDescription() {
265*9880d681SAndroid Build Coastguard Worker return String(errno == 0 ? "" : posix::StrError(errno));
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker // This is called from a death test parent process to read a failure
269*9880d681SAndroid Build Coastguard Worker // message from the death test child process and log it with the FATAL
270*9880d681SAndroid Build Coastguard Worker // severity. On Windows, the message is read from a pipe handle. On other
271*9880d681SAndroid Build Coastguard Worker // platforms, it is read from a file descriptor.
FailFromInternalError(int fd)272*9880d681SAndroid Build Coastguard Worker static void FailFromInternalError(int fd) {
273*9880d681SAndroid Build Coastguard Worker Message error;
274*9880d681SAndroid Build Coastguard Worker char buffer[256];
275*9880d681SAndroid Build Coastguard Worker int num_read;
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker do {
278*9880d681SAndroid Build Coastguard Worker while ((num_read = posix::Read(fd, buffer, 255)) > 0) {
279*9880d681SAndroid Build Coastguard Worker buffer[num_read] = '\0';
280*9880d681SAndroid Build Coastguard Worker error << buffer;
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker } while (num_read == -1 && errno == EINTR);
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker if (num_read == 0) {
285*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(FATAL) << error.GetString();
286*9880d681SAndroid Build Coastguard Worker } else {
287*9880d681SAndroid Build Coastguard Worker const int last_error = errno;
288*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(FATAL) << "Error while reading death test internal: "
289*9880d681SAndroid Build Coastguard Worker << GetLastErrnoDescription() << " [" << last_error << "]";
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker // Death test constructor. Increments the running death test count
294*9880d681SAndroid Build Coastguard Worker // for the current test.
DeathTest()295*9880d681SAndroid Build Coastguard Worker DeathTest::DeathTest() {
296*9880d681SAndroid Build Coastguard Worker TestInfo* const info = GetUnitTestImpl()->current_test_info();
297*9880d681SAndroid Build Coastguard Worker if (info == NULL) {
298*9880d681SAndroid Build Coastguard Worker DeathTestAbort("Cannot run a death test outside of a TEST or "
299*9880d681SAndroid Build Coastguard Worker "TEST_F construct");
300*9880d681SAndroid Build Coastguard Worker }
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker // Pin the vtable to this file.
~DeathTest()304*9880d681SAndroid Build Coastguard Worker DeathTest::~DeathTest() {}
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker // Creates and returns a death test by dispatching to the current
307*9880d681SAndroid Build Coastguard Worker // death test factory.
Create(const char * statement,const RE * regex,const char * file,int line,DeathTest ** test)308*9880d681SAndroid Build Coastguard Worker bool DeathTest::Create(const char* statement, const RE* regex,
309*9880d681SAndroid Build Coastguard Worker const char* file, int line, DeathTest** test) {
310*9880d681SAndroid Build Coastguard Worker return GetUnitTestImpl()->death_test_factory()->Create(
311*9880d681SAndroid Build Coastguard Worker statement, regex, file, line, test);
312*9880d681SAndroid Build Coastguard Worker }
313*9880d681SAndroid Build Coastguard Worker
LastMessage()314*9880d681SAndroid Build Coastguard Worker const char* DeathTest::LastMessage() {
315*9880d681SAndroid Build Coastguard Worker return last_death_test_message_.c_str();
316*9880d681SAndroid Build Coastguard Worker }
317*9880d681SAndroid Build Coastguard Worker
set_last_death_test_message(const String & message)318*9880d681SAndroid Build Coastguard Worker void DeathTest::set_last_death_test_message(const String& message) {
319*9880d681SAndroid Build Coastguard Worker last_death_test_message_ = message;
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker String DeathTest::last_death_test_message_;
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker // Provides cross platform implementation for some death functionality.
325*9880d681SAndroid Build Coastguard Worker class DeathTestImpl : public DeathTest {
326*9880d681SAndroid Build Coastguard Worker protected:
DeathTestImpl(const char * a_statement,const RE * a_regex)327*9880d681SAndroid Build Coastguard Worker DeathTestImpl(const char* a_statement, const RE* a_regex)
328*9880d681SAndroid Build Coastguard Worker : statement_(a_statement),
329*9880d681SAndroid Build Coastguard Worker regex_(a_regex),
330*9880d681SAndroid Build Coastguard Worker spawned_(false),
331*9880d681SAndroid Build Coastguard Worker status_(-1),
332*9880d681SAndroid Build Coastguard Worker outcome_(IN_PROGRESS),
333*9880d681SAndroid Build Coastguard Worker read_fd_(-1),
334*9880d681SAndroid Build Coastguard Worker write_fd_(-1) {}
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker // read_fd_ is expected to be closed and cleared by a derived class.
~DeathTestImpl()337*9880d681SAndroid Build Coastguard Worker ~DeathTestImpl() override { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); }
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard Worker void Abort(AbortReason reason) override;
340*9880d681SAndroid Build Coastguard Worker bool Passed(bool status_ok) override;
341*9880d681SAndroid Build Coastguard Worker
statement() const342*9880d681SAndroid Build Coastguard Worker const char* statement() const { return statement_; }
regex() const343*9880d681SAndroid Build Coastguard Worker const RE* regex() const { return regex_; }
spawned() const344*9880d681SAndroid Build Coastguard Worker bool spawned() const { return spawned_; }
set_spawned(bool is_spawned)345*9880d681SAndroid Build Coastguard Worker void set_spawned(bool is_spawned) { spawned_ = is_spawned; }
status() const346*9880d681SAndroid Build Coastguard Worker int status() const { return status_; }
set_status(int a_status)347*9880d681SAndroid Build Coastguard Worker void set_status(int a_status) { status_ = a_status; }
outcome() const348*9880d681SAndroid Build Coastguard Worker DeathTestOutcome outcome() const { return outcome_; }
set_outcome(DeathTestOutcome an_outcome)349*9880d681SAndroid Build Coastguard Worker void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; }
read_fd() const350*9880d681SAndroid Build Coastguard Worker int read_fd() const { return read_fd_; }
set_read_fd(int fd)351*9880d681SAndroid Build Coastguard Worker void set_read_fd(int fd) { read_fd_ = fd; }
write_fd() const352*9880d681SAndroid Build Coastguard Worker int write_fd() const { return write_fd_; }
set_write_fd(int fd)353*9880d681SAndroid Build Coastguard Worker void set_write_fd(int fd) { write_fd_ = fd; }
354*9880d681SAndroid Build Coastguard Worker
355*9880d681SAndroid Build Coastguard Worker // Called in the parent process only. Reads the result code of the death
356*9880d681SAndroid Build Coastguard Worker // test child process via a pipe, interprets it to set the outcome_
357*9880d681SAndroid Build Coastguard Worker // member, and closes read_fd_. Outputs diagnostics and terminates in
358*9880d681SAndroid Build Coastguard Worker // case of unexpected codes.
359*9880d681SAndroid Build Coastguard Worker void ReadAndInterpretStatusByte();
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker private:
362*9880d681SAndroid Build Coastguard Worker // The textual content of the code this object is testing. This class
363*9880d681SAndroid Build Coastguard Worker // doesn't own this string and should not attempt to delete it.
364*9880d681SAndroid Build Coastguard Worker const char* const statement_;
365*9880d681SAndroid Build Coastguard Worker // The regular expression which test output must match. DeathTestImpl
366*9880d681SAndroid Build Coastguard Worker // doesn't own this object and should not attempt to delete it.
367*9880d681SAndroid Build Coastguard Worker const RE* const regex_;
368*9880d681SAndroid Build Coastguard Worker // True if the death test child process has been successfully spawned.
369*9880d681SAndroid Build Coastguard Worker bool spawned_;
370*9880d681SAndroid Build Coastguard Worker // The exit status of the child process.
371*9880d681SAndroid Build Coastguard Worker int status_;
372*9880d681SAndroid Build Coastguard Worker // How the death test concluded.
373*9880d681SAndroid Build Coastguard Worker DeathTestOutcome outcome_;
374*9880d681SAndroid Build Coastguard Worker // Descriptor to the read end of the pipe to the child process. It is
375*9880d681SAndroid Build Coastguard Worker // always -1 in the child process. The child keeps its write end of the
376*9880d681SAndroid Build Coastguard Worker // pipe in write_fd_.
377*9880d681SAndroid Build Coastguard Worker int read_fd_;
378*9880d681SAndroid Build Coastguard Worker // Descriptor to the child's write end of the pipe to the parent process.
379*9880d681SAndroid Build Coastguard Worker // It is always -1 in the parent process. The parent keeps its end of the
380*9880d681SAndroid Build Coastguard Worker // pipe in read_fd_.
381*9880d681SAndroid Build Coastguard Worker int write_fd_;
382*9880d681SAndroid Build Coastguard Worker };
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker // Called in the parent process only. Reads the result code of the death
385*9880d681SAndroid Build Coastguard Worker // test child process via a pipe, interprets it to set the outcome_
386*9880d681SAndroid Build Coastguard Worker // member, and closes read_fd_. Outputs diagnostics and terminates in
387*9880d681SAndroid Build Coastguard Worker // case of unexpected codes.
ReadAndInterpretStatusByte()388*9880d681SAndroid Build Coastguard Worker void DeathTestImpl::ReadAndInterpretStatusByte() {
389*9880d681SAndroid Build Coastguard Worker char flag;
390*9880d681SAndroid Build Coastguard Worker int bytes_read;
391*9880d681SAndroid Build Coastguard Worker
392*9880d681SAndroid Build Coastguard Worker // The read() here blocks until data is available (signifying the
393*9880d681SAndroid Build Coastguard Worker // failure of the death test) or until the pipe is closed (signifying
394*9880d681SAndroid Build Coastguard Worker // its success), so it's okay to call this in the parent before
395*9880d681SAndroid Build Coastguard Worker // the child process has exited.
396*9880d681SAndroid Build Coastguard Worker do {
397*9880d681SAndroid Build Coastguard Worker bytes_read = posix::Read(read_fd(), &flag, 1);
398*9880d681SAndroid Build Coastguard Worker } while (bytes_read == -1 && errno == EINTR);
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker if (bytes_read == 0) {
401*9880d681SAndroid Build Coastguard Worker set_outcome(DIED);
402*9880d681SAndroid Build Coastguard Worker } else if (bytes_read == 1) {
403*9880d681SAndroid Build Coastguard Worker switch (flag) {
404*9880d681SAndroid Build Coastguard Worker case kDeathTestReturned:
405*9880d681SAndroid Build Coastguard Worker set_outcome(RETURNED);
406*9880d681SAndroid Build Coastguard Worker break;
407*9880d681SAndroid Build Coastguard Worker case kDeathTestThrew:
408*9880d681SAndroid Build Coastguard Worker set_outcome(THREW);
409*9880d681SAndroid Build Coastguard Worker break;
410*9880d681SAndroid Build Coastguard Worker case kDeathTestLived:
411*9880d681SAndroid Build Coastguard Worker set_outcome(LIVED);
412*9880d681SAndroid Build Coastguard Worker break;
413*9880d681SAndroid Build Coastguard Worker case kDeathTestInternalError:
414*9880d681SAndroid Build Coastguard Worker FailFromInternalError(read_fd()); // Does not return.
415*9880d681SAndroid Build Coastguard Worker break;
416*9880d681SAndroid Build Coastguard Worker default:
417*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(FATAL) << "Death test child process reported "
418*9880d681SAndroid Build Coastguard Worker << "unexpected status byte ("
419*9880d681SAndroid Build Coastguard Worker << static_cast<unsigned int>(flag) << ")";
420*9880d681SAndroid Build Coastguard Worker }
421*9880d681SAndroid Build Coastguard Worker } else {
422*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(FATAL) << "Read from death test child process failed: "
423*9880d681SAndroid Build Coastguard Worker << GetLastErrnoDescription();
424*9880d681SAndroid Build Coastguard Worker }
425*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd()));
426*9880d681SAndroid Build Coastguard Worker set_read_fd(-1);
427*9880d681SAndroid Build Coastguard Worker }
428*9880d681SAndroid Build Coastguard Worker
429*9880d681SAndroid Build Coastguard Worker // Signals that the death test code which should have exited, didn't.
430*9880d681SAndroid Build Coastguard Worker // Should be called only in a death test child process.
431*9880d681SAndroid Build Coastguard Worker // Writes a status byte to the child's status file descriptor, then
432*9880d681SAndroid Build Coastguard Worker // calls _exit(1).
Abort(AbortReason reason)433*9880d681SAndroid Build Coastguard Worker void DeathTestImpl::Abort(AbortReason reason) {
434*9880d681SAndroid Build Coastguard Worker // The parent process considers the death test to be a failure if
435*9880d681SAndroid Build Coastguard Worker // it finds any data in our pipe. So, here we write a single flag byte
436*9880d681SAndroid Build Coastguard Worker // to the pipe, then exit.
437*9880d681SAndroid Build Coastguard Worker const char status_ch =
438*9880d681SAndroid Build Coastguard Worker reason == TEST_DID_NOT_DIE ? kDeathTestLived :
439*9880d681SAndroid Build Coastguard Worker reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned;
440*9880d681SAndroid Build Coastguard Worker
441*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));
442*9880d681SAndroid Build Coastguard Worker // We are leaking the descriptor here because on some platforms (i.e.,
443*9880d681SAndroid Build Coastguard Worker // when built as Windows DLL), destructors of global objects will still
444*9880d681SAndroid Build Coastguard Worker // run after calling _exit(). On such systems, write_fd_ will be
445*9880d681SAndroid Build Coastguard Worker // indirectly closed from the destructor of UnitTestImpl, causing double
446*9880d681SAndroid Build Coastguard Worker // close if it is also closed here. On debug configurations, double close
447*9880d681SAndroid Build Coastguard Worker // may assert. As there are no in-process buffers to flush here, we are
448*9880d681SAndroid Build Coastguard Worker // relying on the OS to close the descriptor after the process terminates
449*9880d681SAndroid Build Coastguard Worker // when the destructors are not run.
450*9880d681SAndroid Build Coastguard Worker _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash)
451*9880d681SAndroid Build Coastguard Worker }
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker // Returns an indented copy of stderr output for a death test.
454*9880d681SAndroid Build Coastguard Worker // This makes distinguishing death test output lines from regular log lines
455*9880d681SAndroid Build Coastguard Worker // much easier.
FormatDeathTestOutput(const::std::string & output)456*9880d681SAndroid Build Coastguard Worker static ::std::string FormatDeathTestOutput(const ::std::string& output) {
457*9880d681SAndroid Build Coastguard Worker ::std::string ret;
458*9880d681SAndroid Build Coastguard Worker for (size_t at = 0; ; ) {
459*9880d681SAndroid Build Coastguard Worker const size_t line_end = output.find('\n', at);
460*9880d681SAndroid Build Coastguard Worker ret += "[ DEATH ] ";
461*9880d681SAndroid Build Coastguard Worker if (line_end == ::std::string::npos) {
462*9880d681SAndroid Build Coastguard Worker ret += output.substr(at);
463*9880d681SAndroid Build Coastguard Worker break;
464*9880d681SAndroid Build Coastguard Worker }
465*9880d681SAndroid Build Coastguard Worker ret += output.substr(at, line_end + 1 - at);
466*9880d681SAndroid Build Coastguard Worker at = line_end + 1;
467*9880d681SAndroid Build Coastguard Worker }
468*9880d681SAndroid Build Coastguard Worker return ret;
469*9880d681SAndroid Build Coastguard Worker }
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker // Assesses the success or failure of a death test, using both private
472*9880d681SAndroid Build Coastguard Worker // members which have previously been set, and one argument:
473*9880d681SAndroid Build Coastguard Worker //
474*9880d681SAndroid Build Coastguard Worker // Private data members:
475*9880d681SAndroid Build Coastguard Worker // outcome: An enumeration describing how the death test
476*9880d681SAndroid Build Coastguard Worker // concluded: DIED, LIVED, THREW, or RETURNED. The death test
477*9880d681SAndroid Build Coastguard Worker // fails in the latter three cases.
478*9880d681SAndroid Build Coastguard Worker // status: The exit status of the child process. On *nix, it is in the
479*9880d681SAndroid Build Coastguard Worker // in the format specified by wait(2). On Windows, this is the
480*9880d681SAndroid Build Coastguard Worker // value supplied to the ExitProcess() API or a numeric code
481*9880d681SAndroid Build Coastguard Worker // of the exception that terminated the program.
482*9880d681SAndroid Build Coastguard Worker // regex: A regular expression object to be applied to
483*9880d681SAndroid Build Coastguard Worker // the test's captured standard error output; the death test
484*9880d681SAndroid Build Coastguard Worker // fails if it does not match.
485*9880d681SAndroid Build Coastguard Worker //
486*9880d681SAndroid Build Coastguard Worker // Argument:
487*9880d681SAndroid Build Coastguard Worker // status_ok: true if exit_status is acceptable in the context of
488*9880d681SAndroid Build Coastguard Worker // this particular death test, which fails if it is false
489*9880d681SAndroid Build Coastguard Worker //
490*9880d681SAndroid Build Coastguard Worker // Returns true iff all of the above conditions are met. Otherwise, the
491*9880d681SAndroid Build Coastguard Worker // first failing condition, in the order given above, is the one that is
492*9880d681SAndroid Build Coastguard Worker // reported. Also sets the last death test message string.
Passed(bool status_ok)493*9880d681SAndroid Build Coastguard Worker bool DeathTestImpl::Passed(bool status_ok) {
494*9880d681SAndroid Build Coastguard Worker if (!spawned())
495*9880d681SAndroid Build Coastguard Worker return false;
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker const String error_message = GetCapturedStderr();
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker bool success = false;
500*9880d681SAndroid Build Coastguard Worker Message buffer;
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker buffer << "Death test: " << statement() << "\n";
503*9880d681SAndroid Build Coastguard Worker switch (outcome()) {
504*9880d681SAndroid Build Coastguard Worker case LIVED:
505*9880d681SAndroid Build Coastguard Worker buffer << " Result: failed to die.\n"
506*9880d681SAndroid Build Coastguard Worker << " Error msg:\n" << FormatDeathTestOutput(error_message);
507*9880d681SAndroid Build Coastguard Worker break;
508*9880d681SAndroid Build Coastguard Worker case THREW:
509*9880d681SAndroid Build Coastguard Worker buffer << " Result: threw an exception.\n"
510*9880d681SAndroid Build Coastguard Worker << " Error msg:\n" << FormatDeathTestOutput(error_message);
511*9880d681SAndroid Build Coastguard Worker break;
512*9880d681SAndroid Build Coastguard Worker case RETURNED:
513*9880d681SAndroid Build Coastguard Worker buffer << " Result: illegal return in test statement.\n"
514*9880d681SAndroid Build Coastguard Worker << " Error msg:\n" << FormatDeathTestOutput(error_message);
515*9880d681SAndroid Build Coastguard Worker break;
516*9880d681SAndroid Build Coastguard Worker case DIED:
517*9880d681SAndroid Build Coastguard Worker if (status_ok) {
518*9880d681SAndroid Build Coastguard Worker const bool matched = RE::PartialMatch(error_message.c_str(), *regex());
519*9880d681SAndroid Build Coastguard Worker if (matched) {
520*9880d681SAndroid Build Coastguard Worker success = true;
521*9880d681SAndroid Build Coastguard Worker } else {
522*9880d681SAndroid Build Coastguard Worker buffer << " Result: died but not with expected error.\n"
523*9880d681SAndroid Build Coastguard Worker << " Expected: " << regex()->pattern() << "\n"
524*9880d681SAndroid Build Coastguard Worker << "Actual msg:\n" << FormatDeathTestOutput(error_message);
525*9880d681SAndroid Build Coastguard Worker }
526*9880d681SAndroid Build Coastguard Worker } else {
527*9880d681SAndroid Build Coastguard Worker buffer << " Result: died but not with expected exit code:\n"
528*9880d681SAndroid Build Coastguard Worker << " " << ExitSummary(status()) << "\n"
529*9880d681SAndroid Build Coastguard Worker << "Actual msg:\n" << FormatDeathTestOutput(error_message);
530*9880d681SAndroid Build Coastguard Worker }
531*9880d681SAndroid Build Coastguard Worker break;
532*9880d681SAndroid Build Coastguard Worker case IN_PROGRESS:
533*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(FATAL)
534*9880d681SAndroid Build Coastguard Worker << "DeathTest::Passed somehow called before conclusion of test";
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message(buffer.GetString());
538*9880d681SAndroid Build Coastguard Worker return success;
539*9880d681SAndroid Build Coastguard Worker }
540*9880d681SAndroid Build Coastguard Worker
541*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
542*9880d681SAndroid Build Coastguard Worker // WindowsDeathTest implements death tests on Windows. Due to the
543*9880d681SAndroid Build Coastguard Worker // specifics of starting new processes on Windows, death tests there are
544*9880d681SAndroid Build Coastguard Worker // always threadsafe, and Google Test considers the
545*9880d681SAndroid Build Coastguard Worker // --gtest_death_test_style=fast setting to be equivalent to
546*9880d681SAndroid Build Coastguard Worker // --gtest_death_test_style=threadsafe there.
547*9880d681SAndroid Build Coastguard Worker //
548*9880d681SAndroid Build Coastguard Worker // A few implementation notes: Like the Linux version, the Windows
549*9880d681SAndroid Build Coastguard Worker // implementation uses pipes for child-to-parent communication. But due to
550*9880d681SAndroid Build Coastguard Worker // the specifics of pipes on Windows, some extra steps are required:
551*9880d681SAndroid Build Coastguard Worker //
552*9880d681SAndroid Build Coastguard Worker // 1. The parent creates a communication pipe and stores handles to both
553*9880d681SAndroid Build Coastguard Worker // ends of it.
554*9880d681SAndroid Build Coastguard Worker // 2. The parent starts the child and provides it with the information
555*9880d681SAndroid Build Coastguard Worker // necessary to acquire the handle to the write end of the pipe.
556*9880d681SAndroid Build Coastguard Worker // 3. The child acquires the write end of the pipe and signals the parent
557*9880d681SAndroid Build Coastguard Worker // using a Windows event.
558*9880d681SAndroid Build Coastguard Worker // 4. Now the parent can release the write end of the pipe on its side. If
559*9880d681SAndroid Build Coastguard Worker // this is done before step 3, the object's reference count goes down to
560*9880d681SAndroid Build Coastguard Worker // 0 and it is destroyed, preventing the child from acquiring it. The
561*9880d681SAndroid Build Coastguard Worker // parent now has to release it, or read operations on the read end of
562*9880d681SAndroid Build Coastguard Worker // the pipe will not return when the child terminates.
563*9880d681SAndroid Build Coastguard Worker // 5. The parent reads child's output through the pipe (outcome code and
564*9880d681SAndroid Build Coastguard Worker // any possible error messages) from the pipe, and its stderr and then
565*9880d681SAndroid Build Coastguard Worker // determines whether to fail the test.
566*9880d681SAndroid Build Coastguard Worker //
567*9880d681SAndroid Build Coastguard Worker // Note: to distinguish Win32 API calls from the local method and function
568*9880d681SAndroid Build Coastguard Worker // calls, the former are explicitly resolved in the global namespace.
569*9880d681SAndroid Build Coastguard Worker //
570*9880d681SAndroid Build Coastguard Worker class WindowsDeathTest : public DeathTestImpl {
571*9880d681SAndroid Build Coastguard Worker public:
WindowsDeathTest(const char * a_statement,const RE * a_regex,const char * file,int line)572*9880d681SAndroid Build Coastguard Worker WindowsDeathTest(const char* a_statement,
573*9880d681SAndroid Build Coastguard Worker const RE* a_regex,
574*9880d681SAndroid Build Coastguard Worker const char* file,
575*9880d681SAndroid Build Coastguard Worker int line)
576*9880d681SAndroid Build Coastguard Worker : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}
577*9880d681SAndroid Build Coastguard Worker
578*9880d681SAndroid Build Coastguard Worker // All of these virtual functions are inherited from DeathTest.
579*9880d681SAndroid Build Coastguard Worker virtual int Wait();
580*9880d681SAndroid Build Coastguard Worker virtual TestRole AssumeRole();
581*9880d681SAndroid Build Coastguard Worker
582*9880d681SAndroid Build Coastguard Worker private:
583*9880d681SAndroid Build Coastguard Worker // The name of the file in which the death test is located.
584*9880d681SAndroid Build Coastguard Worker const char* const file_;
585*9880d681SAndroid Build Coastguard Worker // The line number on which the death test is located.
586*9880d681SAndroid Build Coastguard Worker const int line_;
587*9880d681SAndroid Build Coastguard Worker // Handle to the write end of the pipe to the child process.
588*9880d681SAndroid Build Coastguard Worker AutoHandle write_handle_;
589*9880d681SAndroid Build Coastguard Worker // Child process handle.
590*9880d681SAndroid Build Coastguard Worker AutoHandle child_handle_;
591*9880d681SAndroid Build Coastguard Worker // Event the child process uses to signal the parent that it has
592*9880d681SAndroid Build Coastguard Worker // acquired the handle to the write end of the pipe. After seeing this
593*9880d681SAndroid Build Coastguard Worker // event the parent can release its own handles to make sure its
594*9880d681SAndroid Build Coastguard Worker // ReadFile() calls return when the child terminates.
595*9880d681SAndroid Build Coastguard Worker AutoHandle event_handle_;
596*9880d681SAndroid Build Coastguard Worker };
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard Worker // Waits for the child in a death test to exit, returning its exit
599*9880d681SAndroid Build Coastguard Worker // status, or 0 if no child process exists. As a side effect, sets the
600*9880d681SAndroid Build Coastguard Worker // outcome data member.
Wait()601*9880d681SAndroid Build Coastguard Worker int WindowsDeathTest::Wait() {
602*9880d681SAndroid Build Coastguard Worker if (!spawned())
603*9880d681SAndroid Build Coastguard Worker return 0;
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard Worker // Wait until the child either signals that it has acquired the write end
606*9880d681SAndroid Build Coastguard Worker // of the pipe or it dies.
607*9880d681SAndroid Build Coastguard Worker const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() };
608*9880d681SAndroid Build Coastguard Worker switch (::WaitForMultipleObjects(2,
609*9880d681SAndroid Build Coastguard Worker wait_handles,
610*9880d681SAndroid Build Coastguard Worker FALSE, // Waits for any of the handles.
611*9880d681SAndroid Build Coastguard Worker INFINITE)) {
612*9880d681SAndroid Build Coastguard Worker case WAIT_OBJECT_0:
613*9880d681SAndroid Build Coastguard Worker case WAIT_OBJECT_0 + 1:
614*9880d681SAndroid Build Coastguard Worker break;
615*9880d681SAndroid Build Coastguard Worker default:
616*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(false); // Should not get here.
617*9880d681SAndroid Build Coastguard Worker }
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker // The child has acquired the write end of the pipe or exited.
620*9880d681SAndroid Build Coastguard Worker // We release the handle on our side and continue.
621*9880d681SAndroid Build Coastguard Worker write_handle_.Reset();
622*9880d681SAndroid Build Coastguard Worker event_handle_.Reset();
623*9880d681SAndroid Build Coastguard Worker
624*9880d681SAndroid Build Coastguard Worker ReadAndInterpretStatusByte();
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard Worker // Waits for the child process to exit if it haven't already. This
627*9880d681SAndroid Build Coastguard Worker // returns immediately if the child has already exited, regardless of
628*9880d681SAndroid Build Coastguard Worker // whether previous calls to WaitForMultipleObjects synchronized on this
629*9880d681SAndroid Build Coastguard Worker // handle or not.
630*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(
631*9880d681SAndroid Build Coastguard Worker WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(),
632*9880d681SAndroid Build Coastguard Worker INFINITE));
633*9880d681SAndroid Build Coastguard Worker DWORD status_code;
634*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(
635*9880d681SAndroid Build Coastguard Worker ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE);
636*9880d681SAndroid Build Coastguard Worker child_handle_.Reset();
637*9880d681SAndroid Build Coastguard Worker set_status(static_cast<int>(status_code));
638*9880d681SAndroid Build Coastguard Worker return status();
639*9880d681SAndroid Build Coastguard Worker }
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard Worker // The AssumeRole process for a Windows death test. It creates a child
642*9880d681SAndroid Build Coastguard Worker // process with the same executable as the current process to run the
643*9880d681SAndroid Build Coastguard Worker // death test. The child process is given the --gtest_filter and
644*9880d681SAndroid Build Coastguard Worker // --gtest_internal_run_death_test flags such that it knows to run the
645*9880d681SAndroid Build Coastguard Worker // current death test only.
AssumeRole()646*9880d681SAndroid Build Coastguard Worker DeathTest::TestRole WindowsDeathTest::AssumeRole() {
647*9880d681SAndroid Build Coastguard Worker const UnitTestImpl* const impl = GetUnitTestImpl();
648*9880d681SAndroid Build Coastguard Worker const InternalRunDeathTestFlag* const flag =
649*9880d681SAndroid Build Coastguard Worker impl->internal_run_death_test_flag();
650*9880d681SAndroid Build Coastguard Worker const TestInfo* const info = impl->current_test_info();
651*9880d681SAndroid Build Coastguard Worker const int death_test_index = info->result()->death_test_count();
652*9880d681SAndroid Build Coastguard Worker
653*9880d681SAndroid Build Coastguard Worker if (flag != NULL) {
654*9880d681SAndroid Build Coastguard Worker // ParseInternalRunDeathTestFlag() has performed all the necessary
655*9880d681SAndroid Build Coastguard Worker // processing.
656*9880d681SAndroid Build Coastguard Worker set_write_fd(flag->write_fd());
657*9880d681SAndroid Build Coastguard Worker return EXECUTE_TEST;
658*9880d681SAndroid Build Coastguard Worker }
659*9880d681SAndroid Build Coastguard Worker
660*9880d681SAndroid Build Coastguard Worker // WindowsDeathTest uses an anonymous pipe to communicate results of
661*9880d681SAndroid Build Coastguard Worker // a death test.
662*9880d681SAndroid Build Coastguard Worker SECURITY_ATTRIBUTES handles_are_inheritable = {
663*9880d681SAndroid Build Coastguard Worker sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
664*9880d681SAndroid Build Coastguard Worker HANDLE read_handle, write_handle;
665*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(
666*9880d681SAndroid Build Coastguard Worker ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable,
667*9880d681SAndroid Build Coastguard Worker 0) // Default buffer size.
668*9880d681SAndroid Build Coastguard Worker != FALSE);
669*9880d681SAndroid Build Coastguard Worker set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle),
670*9880d681SAndroid Build Coastguard Worker O_RDONLY));
671*9880d681SAndroid Build Coastguard Worker write_handle_.Reset(write_handle);
672*9880d681SAndroid Build Coastguard Worker event_handle_.Reset(::CreateEvent(
673*9880d681SAndroid Build Coastguard Worker &handles_are_inheritable,
674*9880d681SAndroid Build Coastguard Worker TRUE, // The event will automatically reset to non-signaled state.
675*9880d681SAndroid Build Coastguard Worker FALSE, // The initial state is non-signalled.
676*9880d681SAndroid Build Coastguard Worker NULL)); // The even is unnamed.
677*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL);
678*9880d681SAndroid Build Coastguard Worker const String filter_flag = String::Format("--%s%s=%s.%s",
679*9880d681SAndroid Build Coastguard Worker GTEST_FLAG_PREFIX_, kFilterFlag,
680*9880d681SAndroid Build Coastguard Worker info->test_case_name(),
681*9880d681SAndroid Build Coastguard Worker info->name());
682*9880d681SAndroid Build Coastguard Worker const String internal_flag = String::Format(
683*9880d681SAndroid Build Coastguard Worker "--%s%s=%s|%d|%d|%u|%Iu|%Iu",
684*9880d681SAndroid Build Coastguard Worker GTEST_FLAG_PREFIX_,
685*9880d681SAndroid Build Coastguard Worker kInternalRunDeathTestFlag,
686*9880d681SAndroid Build Coastguard Worker file_, line_,
687*9880d681SAndroid Build Coastguard Worker death_test_index,
688*9880d681SAndroid Build Coastguard Worker static_cast<unsigned int>(::GetCurrentProcessId()),
689*9880d681SAndroid Build Coastguard Worker // size_t has the same with as pointers on both 32-bit and 64-bit
690*9880d681SAndroid Build Coastguard Worker // Windows platforms.
691*9880d681SAndroid Build Coastguard Worker // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx.
692*9880d681SAndroid Build Coastguard Worker reinterpret_cast<size_t>(write_handle),
693*9880d681SAndroid Build Coastguard Worker reinterpret_cast<size_t>(event_handle_.Get()));
694*9880d681SAndroid Build Coastguard Worker
695*9880d681SAndroid Build Coastguard Worker char executable_path[_MAX_PATH + 1]; // NOLINT
696*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(
697*9880d681SAndroid Build Coastguard Worker _MAX_PATH + 1 != ::GetModuleFileNameA(NULL,
698*9880d681SAndroid Build Coastguard Worker executable_path,
699*9880d681SAndroid Build Coastguard Worker _MAX_PATH));
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker String command_line = String::Format("%s %s \"%s\"",
702*9880d681SAndroid Build Coastguard Worker ::GetCommandLineA(),
703*9880d681SAndroid Build Coastguard Worker filter_flag.c_str(),
704*9880d681SAndroid Build Coastguard Worker internal_flag.c_str());
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message("");
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker CaptureStderr();
709*9880d681SAndroid Build Coastguard Worker // Flush the log buffers since the log streams are shared with the child.
710*9880d681SAndroid Build Coastguard Worker FlushInfoLog();
711*9880d681SAndroid Build Coastguard Worker
712*9880d681SAndroid Build Coastguard Worker // The child process will share the standard handles with the parent.
713*9880d681SAndroid Build Coastguard Worker STARTUPINFOA startup_info;
714*9880d681SAndroid Build Coastguard Worker memset(&startup_info, 0, sizeof(STARTUPINFO));
715*9880d681SAndroid Build Coastguard Worker startup_info.dwFlags = STARTF_USESTDHANDLES;
716*9880d681SAndroid Build Coastguard Worker startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
717*9880d681SAndroid Build Coastguard Worker startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
718*9880d681SAndroid Build Coastguard Worker startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard Worker PROCESS_INFORMATION process_info;
721*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(::CreateProcessA(
722*9880d681SAndroid Build Coastguard Worker executable_path,
723*9880d681SAndroid Build Coastguard Worker const_cast<char*>(command_line.c_str()),
724*9880d681SAndroid Build Coastguard Worker NULL, // Retuned process handle is not inheritable.
725*9880d681SAndroid Build Coastguard Worker NULL, // Retuned thread handle is not inheritable.
726*9880d681SAndroid Build Coastguard Worker TRUE, // Child inherits all inheritable handles (for write_handle_).
727*9880d681SAndroid Build Coastguard Worker 0x0, // Default creation flags.
728*9880d681SAndroid Build Coastguard Worker NULL, // Inherit the parent's environment.
729*9880d681SAndroid Build Coastguard Worker UnitTest::GetInstance()->original_working_dir(),
730*9880d681SAndroid Build Coastguard Worker &startup_info,
731*9880d681SAndroid Build Coastguard Worker &process_info) != FALSE);
732*9880d681SAndroid Build Coastguard Worker child_handle_.Reset(process_info.hProcess);
733*9880d681SAndroid Build Coastguard Worker ::CloseHandle(process_info.hThread);
734*9880d681SAndroid Build Coastguard Worker set_spawned(true);
735*9880d681SAndroid Build Coastguard Worker return OVERSEE_TEST;
736*9880d681SAndroid Build Coastguard Worker }
737*9880d681SAndroid Build Coastguard Worker # else // We are not on Windows.
738*9880d681SAndroid Build Coastguard Worker
739*9880d681SAndroid Build Coastguard Worker // ForkingDeathTest provides implementations for most of the abstract
740*9880d681SAndroid Build Coastguard Worker // methods of the DeathTest interface. Only the AssumeRole method is
741*9880d681SAndroid Build Coastguard Worker // left undefined.
742*9880d681SAndroid Build Coastguard Worker class ForkingDeathTest : public DeathTestImpl {
743*9880d681SAndroid Build Coastguard Worker public:
744*9880d681SAndroid Build Coastguard Worker ForkingDeathTest(const char* statement, const RE* regex);
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker // All of these virtual functions are inherited from DeathTest.
747*9880d681SAndroid Build Coastguard Worker int Wait() override;
748*9880d681SAndroid Build Coastguard Worker
749*9880d681SAndroid Build Coastguard Worker protected:
set_child_pid(pid_t child_pid)750*9880d681SAndroid Build Coastguard Worker void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; }
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard Worker private:
753*9880d681SAndroid Build Coastguard Worker // PID of child process during death test; 0 in the child process itself.
754*9880d681SAndroid Build Coastguard Worker pid_t child_pid_;
755*9880d681SAndroid Build Coastguard Worker };
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard Worker // Constructs a ForkingDeathTest.
ForkingDeathTest(const char * a_statement,const RE * a_regex)758*9880d681SAndroid Build Coastguard Worker ForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex)
759*9880d681SAndroid Build Coastguard Worker : DeathTestImpl(a_statement, a_regex),
760*9880d681SAndroid Build Coastguard Worker child_pid_(-1) {}
761*9880d681SAndroid Build Coastguard Worker
762*9880d681SAndroid Build Coastguard Worker // Waits for the child in a death test to exit, returning its exit
763*9880d681SAndroid Build Coastguard Worker // status, or 0 if no child process exists. As a side effect, sets the
764*9880d681SAndroid Build Coastguard Worker // outcome data member.
Wait()765*9880d681SAndroid Build Coastguard Worker int ForkingDeathTest::Wait() {
766*9880d681SAndroid Build Coastguard Worker if (!spawned())
767*9880d681SAndroid Build Coastguard Worker return 0;
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker ReadAndInterpretStatusByte();
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard Worker int status_value;
772*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0));
773*9880d681SAndroid Build Coastguard Worker set_status(status_value);
774*9880d681SAndroid Build Coastguard Worker return status_value;
775*9880d681SAndroid Build Coastguard Worker }
776*9880d681SAndroid Build Coastguard Worker
777*9880d681SAndroid Build Coastguard Worker // A concrete death test class that forks, then immediately runs the test
778*9880d681SAndroid Build Coastguard Worker // in the child process.
779*9880d681SAndroid Build Coastguard Worker class NoExecDeathTest : public ForkingDeathTest {
780*9880d681SAndroid Build Coastguard Worker public:
NoExecDeathTest(const char * a_statement,const RE * a_regex)781*9880d681SAndroid Build Coastguard Worker NoExecDeathTest(const char* a_statement, const RE* a_regex) :
782*9880d681SAndroid Build Coastguard Worker ForkingDeathTest(a_statement, a_regex) { }
783*9880d681SAndroid Build Coastguard Worker TestRole AssumeRole() override;
784*9880d681SAndroid Build Coastguard Worker };
785*9880d681SAndroid Build Coastguard Worker
786*9880d681SAndroid Build Coastguard Worker // The AssumeRole process for a fork-and-run death test. It implements a
787*9880d681SAndroid Build Coastguard Worker // straightforward fork, with a simple pipe to transmit the status byte.
AssumeRole()788*9880d681SAndroid Build Coastguard Worker DeathTest::TestRole NoExecDeathTest::AssumeRole() {
789*9880d681SAndroid Build Coastguard Worker const size_t thread_count = GetThreadCount();
790*9880d681SAndroid Build Coastguard Worker if (thread_count != 1) {
791*9880d681SAndroid Build Coastguard Worker GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);
792*9880d681SAndroid Build Coastguard Worker }
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker int pipe_fd[2];
795*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
796*9880d681SAndroid Build Coastguard Worker
797*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message("");
798*9880d681SAndroid Build Coastguard Worker CaptureStderr();
799*9880d681SAndroid Build Coastguard Worker // When we fork the process below, the log file buffers are copied, but the
800*9880d681SAndroid Build Coastguard Worker // file descriptors are shared. We flush all log files here so that closing
801*9880d681SAndroid Build Coastguard Worker // the file descriptors in the child process doesn't throw off the
802*9880d681SAndroid Build Coastguard Worker // synchronization between descriptors and buffers in the parent process.
803*9880d681SAndroid Build Coastguard Worker // This is as close to the fork as possible to avoid a race condition in case
804*9880d681SAndroid Build Coastguard Worker // there are multiple threads running before the death test, and another
805*9880d681SAndroid Build Coastguard Worker // thread writes to the log file.
806*9880d681SAndroid Build Coastguard Worker FlushInfoLog();
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker const pid_t child_pid = fork();
809*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(child_pid != -1);
810*9880d681SAndroid Build Coastguard Worker set_child_pid(child_pid);
811*9880d681SAndroid Build Coastguard Worker if (child_pid == 0) {
812*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0]));
813*9880d681SAndroid Build Coastguard Worker set_write_fd(pipe_fd[1]);
814*9880d681SAndroid Build Coastguard Worker // Redirects all logging to stderr in the child process to prevent
815*9880d681SAndroid Build Coastguard Worker // concurrent writes to the log files. We capture stderr in the parent
816*9880d681SAndroid Build Coastguard Worker // process and append the child process' output to a log.
817*9880d681SAndroid Build Coastguard Worker LogToStderr();
818*9880d681SAndroid Build Coastguard Worker // Event forwarding to the listeners of event listener API mush be shut
819*9880d681SAndroid Build Coastguard Worker // down in death test subprocesses.
820*9880d681SAndroid Build Coastguard Worker GetUnitTestImpl()->listeners()->SuppressEventForwarding();
821*9880d681SAndroid Build Coastguard Worker return EXECUTE_TEST;
822*9880d681SAndroid Build Coastguard Worker } else {
823*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
824*9880d681SAndroid Build Coastguard Worker set_read_fd(pipe_fd[0]);
825*9880d681SAndroid Build Coastguard Worker set_spawned(true);
826*9880d681SAndroid Build Coastguard Worker return OVERSEE_TEST;
827*9880d681SAndroid Build Coastguard Worker }
828*9880d681SAndroid Build Coastguard Worker }
829*9880d681SAndroid Build Coastguard Worker
830*9880d681SAndroid Build Coastguard Worker // A concrete death test class that forks and re-executes the main
831*9880d681SAndroid Build Coastguard Worker // program from the beginning, with command-line flags set that cause
832*9880d681SAndroid Build Coastguard Worker // only this specific death test to be run.
833*9880d681SAndroid Build Coastguard Worker class ExecDeathTest : public ForkingDeathTest {
834*9880d681SAndroid Build Coastguard Worker public:
ExecDeathTest(const char * a_statement,const RE * a_regex,const char * file,int line)835*9880d681SAndroid Build Coastguard Worker ExecDeathTest(const char* a_statement, const RE* a_regex,
836*9880d681SAndroid Build Coastguard Worker const char* file, int line) :
837*9880d681SAndroid Build Coastguard Worker ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { }
838*9880d681SAndroid Build Coastguard Worker TestRole AssumeRole() override;
839*9880d681SAndroid Build Coastguard Worker
840*9880d681SAndroid Build Coastguard Worker private:
841*9880d681SAndroid Build Coastguard Worker // The name of the file in which the death test is located.
842*9880d681SAndroid Build Coastguard Worker const char* const file_;
843*9880d681SAndroid Build Coastguard Worker // The line number on which the death test is located.
844*9880d681SAndroid Build Coastguard Worker const int line_;
845*9880d681SAndroid Build Coastguard Worker };
846*9880d681SAndroid Build Coastguard Worker
847*9880d681SAndroid Build Coastguard Worker // Utility class for accumulating command-line arguments.
848*9880d681SAndroid Build Coastguard Worker class Arguments {
849*9880d681SAndroid Build Coastguard Worker public:
Arguments()850*9880d681SAndroid Build Coastguard Worker Arguments() {
851*9880d681SAndroid Build Coastguard Worker args_.push_back(NULL);
852*9880d681SAndroid Build Coastguard Worker }
853*9880d681SAndroid Build Coastguard Worker
~Arguments()854*9880d681SAndroid Build Coastguard Worker ~Arguments() {
855*9880d681SAndroid Build Coastguard Worker for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
856*9880d681SAndroid Build Coastguard Worker ++i) {
857*9880d681SAndroid Build Coastguard Worker free(*i);
858*9880d681SAndroid Build Coastguard Worker }
859*9880d681SAndroid Build Coastguard Worker }
AddArgument(const char * argument)860*9880d681SAndroid Build Coastguard Worker void AddArgument(const char* argument) {
861*9880d681SAndroid Build Coastguard Worker args_.insert(args_.end() - 1, posix::StrDup(argument));
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker
864*9880d681SAndroid Build Coastguard Worker template <typename Str>
AddArguments(const::std::vector<Str> & arguments)865*9880d681SAndroid Build Coastguard Worker void AddArguments(const ::std::vector<Str>& arguments) {
866*9880d681SAndroid Build Coastguard Worker for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
867*9880d681SAndroid Build Coastguard Worker i != arguments.end();
868*9880d681SAndroid Build Coastguard Worker ++i) {
869*9880d681SAndroid Build Coastguard Worker args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
870*9880d681SAndroid Build Coastguard Worker }
871*9880d681SAndroid Build Coastguard Worker }
Argv()872*9880d681SAndroid Build Coastguard Worker char* const* Argv() {
873*9880d681SAndroid Build Coastguard Worker return &args_[0];
874*9880d681SAndroid Build Coastguard Worker }
875*9880d681SAndroid Build Coastguard Worker private:
876*9880d681SAndroid Build Coastguard Worker std::vector<char*> args_;
877*9880d681SAndroid Build Coastguard Worker };
878*9880d681SAndroid Build Coastguard Worker
879*9880d681SAndroid Build Coastguard Worker // A struct that encompasses the arguments to the child process of a
880*9880d681SAndroid Build Coastguard Worker // threadsafe-style death test process.
881*9880d681SAndroid Build Coastguard Worker struct ExecDeathTestArgs {
882*9880d681SAndroid Build Coastguard Worker char* const* argv; // Command-line arguments for the child's call to exec
883*9880d681SAndroid Build Coastguard Worker int close_fd; // File descriptor to close; the read end of a pipe
884*9880d681SAndroid Build Coastguard Worker };
885*9880d681SAndroid Build Coastguard Worker
886*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_MAC
GetEnviron()887*9880d681SAndroid Build Coastguard Worker inline char** GetEnviron() {
888*9880d681SAndroid Build Coastguard Worker // When Google Test is built as a framework on MacOS X, the environ variable
889*9880d681SAndroid Build Coastguard Worker // is unavailable. Apple's documentation (man environ) recommends using
890*9880d681SAndroid Build Coastguard Worker // _NSGetEnviron() instead.
891*9880d681SAndroid Build Coastguard Worker return *_NSGetEnviron();
892*9880d681SAndroid Build Coastguard Worker }
893*9880d681SAndroid Build Coastguard Worker # else
894*9880d681SAndroid Build Coastguard Worker // Some POSIX platforms expect you to declare environ. extern "C" makes
895*9880d681SAndroid Build Coastguard Worker // it reside in the global namespace.
896*9880d681SAndroid Build Coastguard Worker extern "C" char** environ;
GetEnviron()897*9880d681SAndroid Build Coastguard Worker inline char** GetEnviron() { return environ; }
898*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_MAC
899*9880d681SAndroid Build Coastguard Worker
900*9880d681SAndroid Build Coastguard Worker // The main function for a threadsafe-style death test child process.
901*9880d681SAndroid Build Coastguard Worker // This function is called in a clone()-ed process and thus must avoid
902*9880d681SAndroid Build Coastguard Worker // any potentially unsafe operations like malloc or libc functions.
ExecDeathTestChildMain(void * child_arg)903*9880d681SAndroid Build Coastguard Worker static int ExecDeathTestChildMain(void* child_arg) {
904*9880d681SAndroid Build Coastguard Worker ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg);
905*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd));
906*9880d681SAndroid Build Coastguard Worker
907*9880d681SAndroid Build Coastguard Worker // We need to execute the test program in the same environment where
908*9880d681SAndroid Build Coastguard Worker // it was originally invoked. Therefore we change to the original
909*9880d681SAndroid Build Coastguard Worker // working directory first.
910*9880d681SAndroid Build Coastguard Worker const char* const original_dir =
911*9880d681SAndroid Build Coastguard Worker UnitTest::GetInstance()->original_working_dir();
912*9880d681SAndroid Build Coastguard Worker // We can safely call chdir() as it's a direct system call.
913*9880d681SAndroid Build Coastguard Worker if (chdir(original_dir) != 0) {
914*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format("chdir(\"%s\") failed: %s",
915*9880d681SAndroid Build Coastguard Worker original_dir,
916*9880d681SAndroid Build Coastguard Worker GetLastErrnoDescription().c_str()));
917*9880d681SAndroid Build Coastguard Worker return EXIT_FAILURE;
918*9880d681SAndroid Build Coastguard Worker }
919*9880d681SAndroid Build Coastguard Worker
920*9880d681SAndroid Build Coastguard Worker // We can safely call execve() as it's a direct system call. We
921*9880d681SAndroid Build Coastguard Worker // cannot use execvp() as it's a libc function and thus potentially
922*9880d681SAndroid Build Coastguard Worker // unsafe. Since execve() doesn't search the PATH, the user must
923*9880d681SAndroid Build Coastguard Worker // invoke the test program via a valid path that contains at least
924*9880d681SAndroid Build Coastguard Worker // one path separator.
925*9880d681SAndroid Build Coastguard Worker execve(args->argv[0], args->argv, GetEnviron());
926*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format("execve(%s, ...) in %s failed: %s",
927*9880d681SAndroid Build Coastguard Worker args->argv[0],
928*9880d681SAndroid Build Coastguard Worker original_dir,
929*9880d681SAndroid Build Coastguard Worker GetLastErrnoDescription().c_str()));
930*9880d681SAndroid Build Coastguard Worker return EXIT_FAILURE;
931*9880d681SAndroid Build Coastguard Worker }
932*9880d681SAndroid Build Coastguard Worker
933*9880d681SAndroid Build Coastguard Worker // Two utility routines that together determine the direction the stack
934*9880d681SAndroid Build Coastguard Worker // grows.
935*9880d681SAndroid Build Coastguard Worker // This could be accomplished more elegantly by a single recursive
936*9880d681SAndroid Build Coastguard Worker // function, but we want to guard against the unlikely possibility of
937*9880d681SAndroid Build Coastguard Worker // a smart compiler optimizing the recursion away.
938*9880d681SAndroid Build Coastguard Worker //
939*9880d681SAndroid Build Coastguard Worker // GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining
940*9880d681SAndroid Build Coastguard Worker // StackLowerThanAddress into StackGrowsDown, which then doesn't give
941*9880d681SAndroid Build Coastguard Worker // correct answer.
942*9880d681SAndroid Build Coastguard Worker bool StackLowerThanAddress(const void* ptr) GTEST_NO_INLINE_;
StackLowerThanAddress(const void * ptr)943*9880d681SAndroid Build Coastguard Worker bool StackLowerThanAddress(const void* ptr) {
944*9880d681SAndroid Build Coastguard Worker int dummy;
945*9880d681SAndroid Build Coastguard Worker return &dummy < ptr;
946*9880d681SAndroid Build Coastguard Worker }
947*9880d681SAndroid Build Coastguard Worker
StackGrowsDown()948*9880d681SAndroid Build Coastguard Worker bool StackGrowsDown() {
949*9880d681SAndroid Build Coastguard Worker int dummy;
950*9880d681SAndroid Build Coastguard Worker return StackLowerThanAddress(&dummy);
951*9880d681SAndroid Build Coastguard Worker }
952*9880d681SAndroid Build Coastguard Worker
953*9880d681SAndroid Build Coastguard Worker // A threadsafe implementation of fork(2) for threadsafe-style death tests
954*9880d681SAndroid Build Coastguard Worker // that uses clone(2). It dies with an error message if anything goes
955*9880d681SAndroid Build Coastguard Worker // wrong.
ExecDeathTestFork(char * const * argv,int close_fd)956*9880d681SAndroid Build Coastguard Worker static pid_t ExecDeathTestFork(char* const* argv, int close_fd) {
957*9880d681SAndroid Build Coastguard Worker ExecDeathTestArgs args = { argv, close_fd };
958*9880d681SAndroid Build Coastguard Worker pid_t child_pid = -1;
959*9880d681SAndroid Build Coastguard Worker
960*9880d681SAndroid Build Coastguard Worker # if GTEST_HAS_CLONE
961*9880d681SAndroid Build Coastguard Worker const bool use_fork = GTEST_FLAG(death_test_use_fork);
962*9880d681SAndroid Build Coastguard Worker
963*9880d681SAndroid Build Coastguard Worker if (!use_fork) {
964*9880d681SAndroid Build Coastguard Worker static const bool stack_grows_down = StackGrowsDown();
965*9880d681SAndroid Build Coastguard Worker const size_t stack_size = getpagesize();
966*9880d681SAndroid Build Coastguard Worker // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead.
967*9880d681SAndroid Build Coastguard Worker void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
968*9880d681SAndroid Build Coastguard Worker MAP_ANON | MAP_PRIVATE, -1, 0);
969*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED);
970*9880d681SAndroid Build Coastguard Worker void* const stack_top =
971*9880d681SAndroid Build Coastguard Worker static_cast<char*>(stack) + (stack_grows_down ? stack_size : 0);
972*9880d681SAndroid Build Coastguard Worker
973*9880d681SAndroid Build Coastguard Worker child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args);
974*9880d681SAndroid Build Coastguard Worker
975*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1);
976*9880d681SAndroid Build Coastguard Worker }
977*9880d681SAndroid Build Coastguard Worker # else
978*9880d681SAndroid Build Coastguard Worker const bool use_fork = true;
979*9880d681SAndroid Build Coastguard Worker # endif // GTEST_HAS_CLONE
980*9880d681SAndroid Build Coastguard Worker
981*9880d681SAndroid Build Coastguard Worker if (use_fork && (child_pid = fork()) == 0) {
982*9880d681SAndroid Build Coastguard Worker ExecDeathTestChildMain(&args);
983*9880d681SAndroid Build Coastguard Worker _exit(0);
984*9880d681SAndroid Build Coastguard Worker }
985*9880d681SAndroid Build Coastguard Worker
986*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(child_pid != -1);
987*9880d681SAndroid Build Coastguard Worker return child_pid;
988*9880d681SAndroid Build Coastguard Worker }
989*9880d681SAndroid Build Coastguard Worker
990*9880d681SAndroid Build Coastguard Worker // The AssumeRole process for a fork-and-exec death test. It re-executes the
991*9880d681SAndroid Build Coastguard Worker // main program from the beginning, setting the --gtest_filter
992*9880d681SAndroid Build Coastguard Worker // and --gtest_internal_run_death_test flags to cause only the current
993*9880d681SAndroid Build Coastguard Worker // death test to be re-run.
AssumeRole()994*9880d681SAndroid Build Coastguard Worker DeathTest::TestRole ExecDeathTest::AssumeRole() {
995*9880d681SAndroid Build Coastguard Worker const UnitTestImpl* const impl = GetUnitTestImpl();
996*9880d681SAndroid Build Coastguard Worker const InternalRunDeathTestFlag* const flag =
997*9880d681SAndroid Build Coastguard Worker impl->internal_run_death_test_flag();
998*9880d681SAndroid Build Coastguard Worker const TestInfo* const info = impl->current_test_info();
999*9880d681SAndroid Build Coastguard Worker const int death_test_index = info->result()->death_test_count();
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker if (flag != NULL) {
1002*9880d681SAndroid Build Coastguard Worker set_write_fd(flag->write_fd());
1003*9880d681SAndroid Build Coastguard Worker return EXECUTE_TEST;
1004*9880d681SAndroid Build Coastguard Worker }
1005*9880d681SAndroid Build Coastguard Worker
1006*9880d681SAndroid Build Coastguard Worker int pipe_fd[2];
1007*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
1008*9880d681SAndroid Build Coastguard Worker // Clear the close-on-exec flag on the write end of the pipe, lest
1009*9880d681SAndroid Build Coastguard Worker // it be closed when the child process does an exec:
1010*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1);
1011*9880d681SAndroid Build Coastguard Worker
1012*9880d681SAndroid Build Coastguard Worker const String filter_flag =
1013*9880d681SAndroid Build Coastguard Worker String::Format("--%s%s=%s.%s",
1014*9880d681SAndroid Build Coastguard Worker GTEST_FLAG_PREFIX_, kFilterFlag,
1015*9880d681SAndroid Build Coastguard Worker info->test_case_name(), info->name());
1016*9880d681SAndroid Build Coastguard Worker const String internal_flag =
1017*9880d681SAndroid Build Coastguard Worker String::Format("--%s%s=%s|%d|%d|%d",
1018*9880d681SAndroid Build Coastguard Worker GTEST_FLAG_PREFIX_, kInternalRunDeathTestFlag,
1019*9880d681SAndroid Build Coastguard Worker file_, line_, death_test_index, pipe_fd[1]);
1020*9880d681SAndroid Build Coastguard Worker Arguments args;
1021*9880d681SAndroid Build Coastguard Worker args.AddArguments(GetArgvs());
1022*9880d681SAndroid Build Coastguard Worker args.AddArgument(filter_flag.c_str());
1023*9880d681SAndroid Build Coastguard Worker args.AddArgument(internal_flag.c_str());
1024*9880d681SAndroid Build Coastguard Worker
1025*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message("");
1026*9880d681SAndroid Build Coastguard Worker
1027*9880d681SAndroid Build Coastguard Worker CaptureStderr();
1028*9880d681SAndroid Build Coastguard Worker // See the comment in NoExecDeathTest::AssumeRole for why the next line
1029*9880d681SAndroid Build Coastguard Worker // is necessary.
1030*9880d681SAndroid Build Coastguard Worker FlushInfoLog();
1031*9880d681SAndroid Build Coastguard Worker
1032*9880d681SAndroid Build Coastguard Worker const pid_t child_pid = ExecDeathTestFork(args.Argv(), pipe_fd[0]);
1033*9880d681SAndroid Build Coastguard Worker GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
1034*9880d681SAndroid Build Coastguard Worker set_child_pid(child_pid);
1035*9880d681SAndroid Build Coastguard Worker set_read_fd(pipe_fd[0]);
1036*9880d681SAndroid Build Coastguard Worker set_spawned(true);
1037*9880d681SAndroid Build Coastguard Worker return OVERSEE_TEST;
1038*9880d681SAndroid Build Coastguard Worker }
1039*9880d681SAndroid Build Coastguard Worker
1040*9880d681SAndroid Build Coastguard Worker # endif // !GTEST_OS_WINDOWS
1041*9880d681SAndroid Build Coastguard Worker
1042*9880d681SAndroid Build Coastguard Worker // Creates a concrete DeathTest-derived class that depends on the
1043*9880d681SAndroid Build Coastguard Worker // --gtest_death_test_style flag, and sets the pointer pointed to
1044*9880d681SAndroid Build Coastguard Worker // by the "test" argument to its address. If the test should be
1045*9880d681SAndroid Build Coastguard Worker // skipped, sets that pointer to NULL. Returns true, unless the
1046*9880d681SAndroid Build Coastguard Worker // flag is set to an invalid value.
Create(const char * statement,const RE * regex,const char * file,int line,DeathTest ** test)1047*9880d681SAndroid Build Coastguard Worker bool DefaultDeathTestFactory::Create(const char* statement, const RE* regex,
1048*9880d681SAndroid Build Coastguard Worker const char* file, int line,
1049*9880d681SAndroid Build Coastguard Worker DeathTest** test) {
1050*9880d681SAndroid Build Coastguard Worker UnitTestImpl* const impl = GetUnitTestImpl();
1051*9880d681SAndroid Build Coastguard Worker const InternalRunDeathTestFlag* const flag =
1052*9880d681SAndroid Build Coastguard Worker impl->internal_run_death_test_flag();
1053*9880d681SAndroid Build Coastguard Worker const int death_test_index = impl->current_test_info()
1054*9880d681SAndroid Build Coastguard Worker ->increment_death_test_count();
1055*9880d681SAndroid Build Coastguard Worker
1056*9880d681SAndroid Build Coastguard Worker if (flag != NULL) {
1057*9880d681SAndroid Build Coastguard Worker if (death_test_index > flag->index()) {
1058*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message(String::Format(
1059*9880d681SAndroid Build Coastguard Worker "Death test count (%d) somehow exceeded expected maximum (%d)",
1060*9880d681SAndroid Build Coastguard Worker death_test_index, flag->index()));
1061*9880d681SAndroid Build Coastguard Worker return false;
1062*9880d681SAndroid Build Coastguard Worker }
1063*9880d681SAndroid Build Coastguard Worker
1064*9880d681SAndroid Build Coastguard Worker if (!(flag->file() == file && flag->line() == line &&
1065*9880d681SAndroid Build Coastguard Worker flag->index() == death_test_index)) {
1066*9880d681SAndroid Build Coastguard Worker *test = NULL;
1067*9880d681SAndroid Build Coastguard Worker return true;
1068*9880d681SAndroid Build Coastguard Worker }
1069*9880d681SAndroid Build Coastguard Worker }
1070*9880d681SAndroid Build Coastguard Worker
1071*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
1072*9880d681SAndroid Build Coastguard Worker
1073*9880d681SAndroid Build Coastguard Worker if (GTEST_FLAG(death_test_style) == "threadsafe" ||
1074*9880d681SAndroid Build Coastguard Worker GTEST_FLAG(death_test_style) == "fast") {
1075*9880d681SAndroid Build Coastguard Worker *test = new WindowsDeathTest(statement, regex, file, line);
1076*9880d681SAndroid Build Coastguard Worker }
1077*9880d681SAndroid Build Coastguard Worker
1078*9880d681SAndroid Build Coastguard Worker # else
1079*9880d681SAndroid Build Coastguard Worker
1080*9880d681SAndroid Build Coastguard Worker if (GTEST_FLAG(death_test_style) == "threadsafe") {
1081*9880d681SAndroid Build Coastguard Worker *test = new ExecDeathTest(statement, regex, file, line);
1082*9880d681SAndroid Build Coastguard Worker } else if (GTEST_FLAG(death_test_style) == "fast") {
1083*9880d681SAndroid Build Coastguard Worker *test = new NoExecDeathTest(statement, regex);
1084*9880d681SAndroid Build Coastguard Worker }
1085*9880d681SAndroid Build Coastguard Worker
1086*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
1087*9880d681SAndroid Build Coastguard Worker
1088*9880d681SAndroid Build Coastguard Worker else { // NOLINT - this is more readable than unbalanced brackets inside #if.
1089*9880d681SAndroid Build Coastguard Worker DeathTest::set_last_death_test_message(String::Format(
1090*9880d681SAndroid Build Coastguard Worker "Unknown death test style \"%s\" encountered",
1091*9880d681SAndroid Build Coastguard Worker GTEST_FLAG(death_test_style).c_str()));
1092*9880d681SAndroid Build Coastguard Worker return false;
1093*9880d681SAndroid Build Coastguard Worker }
1094*9880d681SAndroid Build Coastguard Worker
1095*9880d681SAndroid Build Coastguard Worker return true;
1096*9880d681SAndroid Build Coastguard Worker }
1097*9880d681SAndroid Build Coastguard Worker
1098*9880d681SAndroid Build Coastguard Worker // Pin the vtable to this file.
~DeathTestFactory()1099*9880d681SAndroid Build Coastguard Worker DeathTestFactory::~DeathTestFactory() {}
1100*9880d681SAndroid Build Coastguard Worker
1101*9880d681SAndroid Build Coastguard Worker // Splits a given string on a given delimiter, populating a given
1102*9880d681SAndroid Build Coastguard Worker // vector with the fields. GTEST_HAS_DEATH_TEST implies that we have
1103*9880d681SAndroid Build Coastguard Worker // ::std::string, so we can use it here.
SplitString(const::std::string & str,char delimiter,::std::vector<::std::string> * dest)1104*9880d681SAndroid Build Coastguard Worker static void SplitString(const ::std::string& str, char delimiter,
1105*9880d681SAndroid Build Coastguard Worker ::std::vector< ::std::string>* dest) {
1106*9880d681SAndroid Build Coastguard Worker ::std::vector< ::std::string> parsed;
1107*9880d681SAndroid Build Coastguard Worker ::std::string::size_type pos = 0;
1108*9880d681SAndroid Build Coastguard Worker while (::testing::internal::AlwaysTrue()) {
1109*9880d681SAndroid Build Coastguard Worker const ::std::string::size_type colon = str.find(delimiter, pos);
1110*9880d681SAndroid Build Coastguard Worker if (colon == ::std::string::npos) {
1111*9880d681SAndroid Build Coastguard Worker parsed.push_back(str.substr(pos));
1112*9880d681SAndroid Build Coastguard Worker break;
1113*9880d681SAndroid Build Coastguard Worker } else {
1114*9880d681SAndroid Build Coastguard Worker parsed.push_back(str.substr(pos, colon - pos));
1115*9880d681SAndroid Build Coastguard Worker pos = colon + 1;
1116*9880d681SAndroid Build Coastguard Worker }
1117*9880d681SAndroid Build Coastguard Worker }
1118*9880d681SAndroid Build Coastguard Worker dest->swap(parsed);
1119*9880d681SAndroid Build Coastguard Worker }
1120*9880d681SAndroid Build Coastguard Worker
1121*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
1122*9880d681SAndroid Build Coastguard Worker // Recreates the pipe and event handles from the provided parameters,
1123*9880d681SAndroid Build Coastguard Worker // signals the event, and returns a file descriptor wrapped around the pipe
1124*9880d681SAndroid Build Coastguard Worker // handle. This function is called in the child process only.
GetStatusFileDescriptor(unsigned int parent_process_id,size_t write_handle_as_size_t,size_t event_handle_as_size_t)1125*9880d681SAndroid Build Coastguard Worker int GetStatusFileDescriptor(unsigned int parent_process_id,
1126*9880d681SAndroid Build Coastguard Worker size_t write_handle_as_size_t,
1127*9880d681SAndroid Build Coastguard Worker size_t event_handle_as_size_t) {
1128*9880d681SAndroid Build Coastguard Worker AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE,
1129*9880d681SAndroid Build Coastguard Worker FALSE, // Non-inheritable.
1130*9880d681SAndroid Build Coastguard Worker parent_process_id));
1131*9880d681SAndroid Build Coastguard Worker if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) {
1132*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format("Unable to open parent process %u",
1133*9880d681SAndroid Build Coastguard Worker parent_process_id));
1134*9880d681SAndroid Build Coastguard Worker }
1135*9880d681SAndroid Build Coastguard Worker
1136*9880d681SAndroid Build Coastguard Worker // TODO([email protected]): Replace the following check with a
1137*9880d681SAndroid Build Coastguard Worker // compile-time assertion when available.
1138*9880d681SAndroid Build Coastguard Worker GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t));
1139*9880d681SAndroid Build Coastguard Worker
1140*9880d681SAndroid Build Coastguard Worker const HANDLE write_handle =
1141*9880d681SAndroid Build Coastguard Worker reinterpret_cast<HANDLE>(write_handle_as_size_t);
1142*9880d681SAndroid Build Coastguard Worker HANDLE dup_write_handle;
1143*9880d681SAndroid Build Coastguard Worker
1144*9880d681SAndroid Build Coastguard Worker // The newly initialized handle is accessible only in in the parent
1145*9880d681SAndroid Build Coastguard Worker // process. To obtain one accessible within the child, we need to use
1146*9880d681SAndroid Build Coastguard Worker // DuplicateHandle.
1147*9880d681SAndroid Build Coastguard Worker if (!::DuplicateHandle(parent_process_handle.Get(), write_handle,
1148*9880d681SAndroid Build Coastguard Worker ::GetCurrentProcess(), &dup_write_handle,
1149*9880d681SAndroid Build Coastguard Worker 0x0, // Requested privileges ignored since
1150*9880d681SAndroid Build Coastguard Worker // DUPLICATE_SAME_ACCESS is used.
1151*9880d681SAndroid Build Coastguard Worker FALSE, // Request non-inheritable handler.
1152*9880d681SAndroid Build Coastguard Worker DUPLICATE_SAME_ACCESS)) {
1153*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format(
1154*9880d681SAndroid Build Coastguard Worker "Unable to duplicate the pipe handle %Iu from the parent process %u",
1155*9880d681SAndroid Build Coastguard Worker write_handle_as_size_t, parent_process_id));
1156*9880d681SAndroid Build Coastguard Worker }
1157*9880d681SAndroid Build Coastguard Worker
1158*9880d681SAndroid Build Coastguard Worker const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t);
1159*9880d681SAndroid Build Coastguard Worker HANDLE dup_event_handle;
1160*9880d681SAndroid Build Coastguard Worker
1161*9880d681SAndroid Build Coastguard Worker if (!::DuplicateHandle(parent_process_handle.Get(), event_handle,
1162*9880d681SAndroid Build Coastguard Worker ::GetCurrentProcess(), &dup_event_handle,
1163*9880d681SAndroid Build Coastguard Worker 0x0,
1164*9880d681SAndroid Build Coastguard Worker FALSE,
1165*9880d681SAndroid Build Coastguard Worker DUPLICATE_SAME_ACCESS)) {
1166*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format(
1167*9880d681SAndroid Build Coastguard Worker "Unable to duplicate the event handle %Iu from the parent process %u",
1168*9880d681SAndroid Build Coastguard Worker event_handle_as_size_t, parent_process_id));
1169*9880d681SAndroid Build Coastguard Worker }
1170*9880d681SAndroid Build Coastguard Worker
1171*9880d681SAndroid Build Coastguard Worker const int write_fd =
1172*9880d681SAndroid Build Coastguard Worker ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND);
1173*9880d681SAndroid Build Coastguard Worker if (write_fd == -1) {
1174*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format(
1175*9880d681SAndroid Build Coastguard Worker "Unable to convert pipe handle %Iu to a file descriptor",
1176*9880d681SAndroid Build Coastguard Worker write_handle_as_size_t));
1177*9880d681SAndroid Build Coastguard Worker }
1178*9880d681SAndroid Build Coastguard Worker
1179*9880d681SAndroid Build Coastguard Worker // Signals the parent that the write end of the pipe has been acquired
1180*9880d681SAndroid Build Coastguard Worker // so the parent can release its own write end.
1181*9880d681SAndroid Build Coastguard Worker ::SetEvent(dup_event_handle);
1182*9880d681SAndroid Build Coastguard Worker
1183*9880d681SAndroid Build Coastguard Worker return write_fd;
1184*9880d681SAndroid Build Coastguard Worker }
1185*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
1186*9880d681SAndroid Build Coastguard Worker
1187*9880d681SAndroid Build Coastguard Worker // Returns a newly created InternalRunDeathTestFlag object with fields
1188*9880d681SAndroid Build Coastguard Worker // initialized from the GTEST_FLAG(internal_run_death_test) flag if
1189*9880d681SAndroid Build Coastguard Worker // the flag is specified; otherwise returns NULL.
ParseInternalRunDeathTestFlag()1190*9880d681SAndroid Build Coastguard Worker InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
1191*9880d681SAndroid Build Coastguard Worker if (GTEST_FLAG(internal_run_death_test) == "") return NULL;
1192*9880d681SAndroid Build Coastguard Worker
1193*9880d681SAndroid Build Coastguard Worker // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we
1194*9880d681SAndroid Build Coastguard Worker // can use it here.
1195*9880d681SAndroid Build Coastguard Worker int line = -1;
1196*9880d681SAndroid Build Coastguard Worker int index = -1;
1197*9880d681SAndroid Build Coastguard Worker ::std::vector< ::std::string> fields;
1198*9880d681SAndroid Build Coastguard Worker SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields);
1199*9880d681SAndroid Build Coastguard Worker int write_fd = -1;
1200*9880d681SAndroid Build Coastguard Worker
1201*9880d681SAndroid Build Coastguard Worker # if GTEST_OS_WINDOWS
1202*9880d681SAndroid Build Coastguard Worker
1203*9880d681SAndroid Build Coastguard Worker unsigned int parent_process_id = 0;
1204*9880d681SAndroid Build Coastguard Worker size_t write_handle_as_size_t = 0;
1205*9880d681SAndroid Build Coastguard Worker size_t event_handle_as_size_t = 0;
1206*9880d681SAndroid Build Coastguard Worker
1207*9880d681SAndroid Build Coastguard Worker if (fields.size() != 6
1208*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[1], &line)
1209*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[2], &index)
1210*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[3], &parent_process_id)
1211*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[4], &write_handle_as_size_t)
1212*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) {
1213*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format(
1214*9880d681SAndroid Build Coastguard Worker "Bad --gtest_internal_run_death_test flag: %s",
1215*9880d681SAndroid Build Coastguard Worker GTEST_FLAG(internal_run_death_test).c_str()));
1216*9880d681SAndroid Build Coastguard Worker }
1217*9880d681SAndroid Build Coastguard Worker write_fd = GetStatusFileDescriptor(parent_process_id,
1218*9880d681SAndroid Build Coastguard Worker write_handle_as_size_t,
1219*9880d681SAndroid Build Coastguard Worker event_handle_as_size_t);
1220*9880d681SAndroid Build Coastguard Worker # else
1221*9880d681SAndroid Build Coastguard Worker
1222*9880d681SAndroid Build Coastguard Worker if (fields.size() != 4
1223*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[1], &line)
1224*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[2], &index)
1225*9880d681SAndroid Build Coastguard Worker || !ParseNaturalNumber(fields[3], &write_fd)) {
1226*9880d681SAndroid Build Coastguard Worker DeathTestAbort(String::Format(
1227*9880d681SAndroid Build Coastguard Worker "Bad --gtest_internal_run_death_test flag: %s",
1228*9880d681SAndroid Build Coastguard Worker GTEST_FLAG(internal_run_death_test).c_str()));
1229*9880d681SAndroid Build Coastguard Worker }
1230*9880d681SAndroid Build Coastguard Worker
1231*9880d681SAndroid Build Coastguard Worker # endif // GTEST_OS_WINDOWS
1232*9880d681SAndroid Build Coastguard Worker
1233*9880d681SAndroid Build Coastguard Worker return new InternalRunDeathTestFlag(fields[0], line, index, write_fd);
1234*9880d681SAndroid Build Coastguard Worker }
1235*9880d681SAndroid Build Coastguard Worker
1236*9880d681SAndroid Build Coastguard Worker } // namespace internal
1237*9880d681SAndroid Build Coastguard Worker
1238*9880d681SAndroid Build Coastguard Worker #endif // GTEST_HAS_DEATH_TEST
1239*9880d681SAndroid Build Coastguard Worker
1240*9880d681SAndroid Build Coastguard Worker } // namespace testing
1241