gtest-death-test.cc revision 848b8605
1848b8605Smrg// Copyright 2005, Google Inc. 2848b8605Smrg// All rights reserved. 3848b8605Smrg// 4848b8605Smrg// Redistribution and use in source and binary forms, with or without 5848b8605Smrg// modification, are permitted provided that the following conditions are 6848b8605Smrg// met: 7848b8605Smrg// 8848b8605Smrg// * Redistributions of source code must retain the above copyright 9848b8605Smrg// notice, this list of conditions and the following disclaimer. 10848b8605Smrg// * Redistributions in binary form must reproduce the above 11848b8605Smrg// copyright notice, this list of conditions and the following disclaimer 12848b8605Smrg// in the documentation and/or other materials provided with the 13848b8605Smrg// distribution. 14848b8605Smrg// * Neither the name of Google Inc. nor the names of its 15848b8605Smrg// contributors may be used to endorse or promote products derived from 16848b8605Smrg// this software without specific prior written permission. 17848b8605Smrg// 18848b8605Smrg// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19848b8605Smrg// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20848b8605Smrg// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21848b8605Smrg// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22848b8605Smrg// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23848b8605Smrg// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24848b8605Smrg// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25848b8605Smrg// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26848b8605Smrg// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27848b8605Smrg// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28848b8605Smrg// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29848b8605Smrg// 30848b8605Smrg// Author: wan@google.com (Zhanyong Wan), vladl@google.com (Vlad Losev) 31848b8605Smrg// 32848b8605Smrg// This file implements death tests. 33848b8605Smrg 34848b8605Smrg#include "gtest/gtest-death-test.h" 35848b8605Smrg#include "gtest/internal/gtest-port.h" 36848b8605Smrg 37848b8605Smrg#if GTEST_HAS_DEATH_TEST 38848b8605Smrg 39848b8605Smrg# if GTEST_OS_MAC 40848b8605Smrg# include <crt_externs.h> 41848b8605Smrg# endif // GTEST_OS_MAC 42848b8605Smrg 43848b8605Smrg# include <errno.h> 44848b8605Smrg# include <fcntl.h> 45848b8605Smrg# include <limits.h> 46848b8605Smrg 47848b8605Smrg# if GTEST_OS_LINUX 48848b8605Smrg# include <signal.h> 49848b8605Smrg# endif // GTEST_OS_LINUX 50848b8605Smrg 51848b8605Smrg# include <stdarg.h> 52848b8605Smrg 53848b8605Smrg# if GTEST_OS_WINDOWS 54848b8605Smrg# include <windows.h> 55848b8605Smrg# else 56848b8605Smrg# include <sys/mman.h> 57848b8605Smrg# include <sys/wait.h> 58848b8605Smrg# endif // GTEST_OS_WINDOWS 59848b8605Smrg 60848b8605Smrg# if GTEST_OS_QNX 61848b8605Smrg# include <spawn.h> 62848b8605Smrg# endif // GTEST_OS_QNX 63848b8605Smrg 64848b8605Smrg#endif // GTEST_HAS_DEATH_TEST 65848b8605Smrg 66848b8605Smrg#include "gtest/gtest-message.h" 67848b8605Smrg#include "gtest/internal/gtest-string.h" 68848b8605Smrg 69848b8605Smrg// Indicates that this translation unit is part of Google Test's 70848b8605Smrg// implementation. It must come before gtest-internal-inl.h is 71848b8605Smrg// included, or there will be a compiler error. This trick is to 72848b8605Smrg// prevent a user from accidentally including gtest-internal-inl.h in 73848b8605Smrg// his code. 74848b8605Smrg#define GTEST_IMPLEMENTATION_ 1 75848b8605Smrg#include "src/gtest-internal-inl.h" 76848b8605Smrg#undef GTEST_IMPLEMENTATION_ 77848b8605Smrg 78848b8605Smrgnamespace testing { 79848b8605Smrg 80848b8605Smrg// Constants. 81848b8605Smrg 82848b8605Smrg// The default death test style. 83848b8605Smrgstatic const char kDefaultDeathTestStyle[] = "fast"; 84848b8605Smrg 85848b8605SmrgGTEST_DEFINE_string_( 86848b8605Smrg death_test_style, 87848b8605Smrg internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle), 88848b8605Smrg "Indicates how to run a death test in a forked child process: " 89848b8605Smrg "\"threadsafe\" (child process re-executes the test binary " 90848b8605Smrg "from the beginning, running only the specific death test) or " 91848b8605Smrg "\"fast\" (child process runs the death test immediately " 92848b8605Smrg "after forking)."); 93848b8605Smrg 94848b8605SmrgGTEST_DEFINE_bool_( 95848b8605Smrg death_test_use_fork, 96848b8605Smrg internal::BoolFromGTestEnv("death_test_use_fork", false), 97848b8605Smrg "Instructs to use fork()/_exit() instead of clone() in death tests. " 98848b8605Smrg "Ignored and always uses fork() on POSIX systems where clone() is not " 99848b8605Smrg "implemented. Useful when running under valgrind or similar tools if " 100848b8605Smrg "those do not support clone(). Valgrind 3.3.1 will just fail if " 101848b8605Smrg "it sees an unsupported combination of clone() flags. " 102848b8605Smrg "It is not recommended to use this flag w/o valgrind though it will " 103848b8605Smrg "work in 99% of the cases. Once valgrind is fixed, this flag will " 104848b8605Smrg "most likely be removed."); 105848b8605Smrg 106848b8605Smrgnamespace internal { 107848b8605SmrgGTEST_DEFINE_string_( 108848b8605Smrg internal_run_death_test, "", 109848b8605Smrg "Indicates the file, line number, temporal index of " 110848b8605Smrg "the single death test to run, and a file descriptor to " 111848b8605Smrg "which a success code may be sent, all separated by " 112848b8605Smrg "the '|' characters. This flag is specified if and only if the current " 113848b8605Smrg "process is a sub-process launched for running a thread-safe " 114848b8605Smrg "death test. FOR INTERNAL USE ONLY."); 115848b8605Smrg} // namespace internal 116848b8605Smrg 117848b8605Smrg#if GTEST_HAS_DEATH_TEST 118848b8605Smrg 119848b8605Smrgnamespace internal { 120848b8605Smrg 121848b8605Smrg// Valid only for fast death tests. Indicates the code is running in the 122848b8605Smrg// child process of a fast style death test. 123848b8605Smrgstatic bool g_in_fast_death_test_child = false; 124848b8605Smrg 125848b8605Smrg// Returns a Boolean value indicating whether the caller is currently 126848b8605Smrg// executing in the context of the death test child process. Tools such as 127848b8605Smrg// Valgrind heap checkers may need this to modify their behavior in death 128848b8605Smrg// tests. IMPORTANT: This is an internal utility. Using it may break the 129848b8605Smrg// implementation of death tests. User code MUST NOT use it. 130848b8605Smrgbool InDeathTestChild() { 131848b8605Smrg# if GTEST_OS_WINDOWS 132848b8605Smrg 133848b8605Smrg // On Windows, death tests are thread-safe regardless of the value of the 134848b8605Smrg // death_test_style flag. 135848b8605Smrg return !GTEST_FLAG(internal_run_death_test).empty(); 136848b8605Smrg 137848b8605Smrg# else 138848b8605Smrg 139848b8605Smrg if (GTEST_FLAG(death_test_style) == "threadsafe") 140848b8605Smrg return !GTEST_FLAG(internal_run_death_test).empty(); 141848b8605Smrg else 142848b8605Smrg return g_in_fast_death_test_child; 143848b8605Smrg#endif 144848b8605Smrg} 145848b8605Smrg 146848b8605Smrg} // namespace internal 147848b8605Smrg 148848b8605Smrg// ExitedWithCode constructor. 149848b8605SmrgExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) { 150848b8605Smrg} 151848b8605Smrg 152848b8605Smrg// ExitedWithCode function-call operator. 153848b8605Smrgbool ExitedWithCode::operator()(int exit_status) const { 154848b8605Smrg# if GTEST_OS_WINDOWS 155848b8605Smrg 156848b8605Smrg return exit_status == exit_code_; 157848b8605Smrg 158848b8605Smrg# else 159848b8605Smrg 160848b8605Smrg return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_; 161848b8605Smrg 162848b8605Smrg# endif // GTEST_OS_WINDOWS 163848b8605Smrg} 164848b8605Smrg 165848b8605Smrg# if !GTEST_OS_WINDOWS 166848b8605Smrg// KilledBySignal constructor. 167848b8605SmrgKilledBySignal::KilledBySignal(int signum) : signum_(signum) { 168848b8605Smrg} 169848b8605Smrg 170848b8605Smrg// KilledBySignal function-call operator. 171848b8605Smrgbool KilledBySignal::operator()(int exit_status) const { 172848b8605Smrg return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_; 173848b8605Smrg} 174848b8605Smrg# endif // !GTEST_OS_WINDOWS 175848b8605Smrg 176848b8605Smrgnamespace internal { 177848b8605Smrg 178848b8605Smrg// Utilities needed for death tests. 179848b8605Smrg 180848b8605Smrg// Generates a textual description of a given exit code, in the format 181848b8605Smrg// specified by wait(2). 182848b8605Smrgstatic std::string ExitSummary(int exit_code) { 183848b8605Smrg Message m; 184848b8605Smrg 185848b8605Smrg# if GTEST_OS_WINDOWS 186848b8605Smrg 187848b8605Smrg m << "Exited with exit status " << exit_code; 188848b8605Smrg 189848b8605Smrg# else 190848b8605Smrg 191848b8605Smrg if (WIFEXITED(exit_code)) { 192848b8605Smrg m << "Exited with exit status " << WEXITSTATUS(exit_code); 193848b8605Smrg } else if (WIFSIGNALED(exit_code)) { 194848b8605Smrg m << "Terminated by signal " << WTERMSIG(exit_code); 195848b8605Smrg } 196848b8605Smrg# ifdef WCOREDUMP 197848b8605Smrg if (WCOREDUMP(exit_code)) { 198848b8605Smrg m << " (core dumped)"; 199848b8605Smrg } 200848b8605Smrg# endif 201848b8605Smrg# endif // GTEST_OS_WINDOWS 202848b8605Smrg 203848b8605Smrg return m.GetString(); 204848b8605Smrg} 205848b8605Smrg 206848b8605Smrg// Returns true if exit_status describes a process that was terminated 207848b8605Smrg// by a signal, or exited normally with a nonzero exit code. 208848b8605Smrgbool ExitedUnsuccessfully(int exit_status) { 209848b8605Smrg return !ExitedWithCode(0)(exit_status); 210848b8605Smrg} 211848b8605Smrg 212848b8605Smrg# if !GTEST_OS_WINDOWS 213848b8605Smrg// Generates a textual failure message when a death test finds more than 214848b8605Smrg// one thread running, or cannot determine the number of threads, prior 215848b8605Smrg// to executing the given statement. It is the responsibility of the 216848b8605Smrg// caller not to pass a thread_count of 1. 217848b8605Smrgstatic std::string DeathTestThreadWarning(size_t thread_count) { 218848b8605Smrg Message msg; 219848b8605Smrg msg << "Death tests use fork(), which is unsafe particularly" 220848b8605Smrg << " in a threaded context. For this test, " << GTEST_NAME_ << " "; 221848b8605Smrg if (thread_count == 0) 222848b8605Smrg msg << "couldn't detect the number of threads."; 223848b8605Smrg else 224848b8605Smrg msg << "detected " << thread_count << " threads."; 225848b8605Smrg return msg.GetString(); 226848b8605Smrg} 227848b8605Smrg# endif // !GTEST_OS_WINDOWS 228848b8605Smrg 229848b8605Smrg// Flag characters for reporting a death test that did not die. 230848b8605Smrgstatic const char kDeathTestLived = 'L'; 231848b8605Smrgstatic const char kDeathTestReturned = 'R'; 232848b8605Smrgstatic const char kDeathTestThrew = 'T'; 233848b8605Smrgstatic const char kDeathTestInternalError = 'I'; 234848b8605Smrg 235848b8605Smrg// An enumeration describing all of the possible ways that a death test can 236848b8605Smrg// conclude. DIED means that the process died while executing the test 237848b8605Smrg// code; LIVED means that process lived beyond the end of the test code; 238848b8605Smrg// RETURNED means that the test statement attempted to execute a return 239848b8605Smrg// statement, which is not allowed; THREW means that the test statement 240848b8605Smrg// returned control by throwing an exception. IN_PROGRESS means the test 241848b8605Smrg// has not yet concluded. 242848b8605Smrg// TODO(vladl@google.com): Unify names and possibly values for 243848b8605Smrg// AbortReason, DeathTestOutcome, and flag characters above. 244848b8605Smrgenum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW }; 245848b8605Smrg 246848b8605Smrg// Routine for aborting the program which is safe to call from an 247848b8605Smrg// exec-style death test child process, in which case the error 248848b8605Smrg// message is propagated back to the parent process. Otherwise, the 249848b8605Smrg// message is simply printed to stderr. In either case, the program 250848b8605Smrg// then exits with status 1. 251848b8605Smrgvoid DeathTestAbort(const std::string& message) { 252848b8605Smrg // On a POSIX system, this function may be called from a threadsafe-style 253848b8605Smrg // death test child process, which operates on a very small stack. Use 254848b8605Smrg // the heap for any additional non-minuscule memory requirements. 255848b8605Smrg const InternalRunDeathTestFlag* const flag = 256848b8605Smrg GetUnitTestImpl()->internal_run_death_test_flag(); 257848b8605Smrg if (flag != NULL) { 258848b8605Smrg FILE* parent = posix::FDOpen(flag->write_fd(), "w"); 259848b8605Smrg fputc(kDeathTestInternalError, parent); 260848b8605Smrg fprintf(parent, "%s", message.c_str()); 261848b8605Smrg fflush(parent); 262848b8605Smrg _exit(1); 263848b8605Smrg } else { 264848b8605Smrg fprintf(stderr, "%s", message.c_str()); 265848b8605Smrg fflush(stderr); 266848b8605Smrg posix::Abort(); 267848b8605Smrg } 268848b8605Smrg} 269848b8605Smrg 270848b8605Smrg// A replacement for CHECK that calls DeathTestAbort if the assertion 271848b8605Smrg// fails. 272848b8605Smrg# define GTEST_DEATH_TEST_CHECK_(expression) \ 273848b8605Smrg do { \ 274848b8605Smrg if (!::testing::internal::IsTrue(expression)) { \ 275848b8605Smrg DeathTestAbort( \ 276848b8605Smrg ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ 277848b8605Smrg + ::testing::internal::StreamableToString(__LINE__) + ": " \ 278848b8605Smrg + #expression); \ 279848b8605Smrg } \ 280848b8605Smrg } while (::testing::internal::AlwaysFalse()) 281848b8605Smrg 282848b8605Smrg// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for 283848b8605Smrg// evaluating any system call that fulfills two conditions: it must return 284848b8605Smrg// -1 on failure, and set errno to EINTR when it is interrupted and 285848b8605Smrg// should be tried again. The macro expands to a loop that repeatedly 286848b8605Smrg// evaluates the expression as long as it evaluates to -1 and sets 287848b8605Smrg// errno to EINTR. If the expression evaluates to -1 but errno is 288848b8605Smrg// something other than EINTR, DeathTestAbort is called. 289848b8605Smrg# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \ 290848b8605Smrg do { \ 291848b8605Smrg int gtest_retval; \ 292848b8605Smrg do { \ 293848b8605Smrg gtest_retval = (expression); \ 294848b8605Smrg } while (gtest_retval == -1 && errno == EINTR); \ 295848b8605Smrg if (gtest_retval == -1) { \ 296848b8605Smrg DeathTestAbort( \ 297848b8605Smrg ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ 298848b8605Smrg + ::testing::internal::StreamableToString(__LINE__) + ": " \ 299848b8605Smrg + #expression + " != -1"); \ 300848b8605Smrg } \ 301848b8605Smrg } while (::testing::internal::AlwaysFalse()) 302848b8605Smrg 303848b8605Smrg// Returns the message describing the last system error in errno. 304848b8605Smrgstd::string GetLastErrnoDescription() { 305848b8605Smrg return errno == 0 ? "" : posix::StrError(errno); 306848b8605Smrg} 307848b8605Smrg 308848b8605Smrg// This is called from a death test parent process to read a failure 309848b8605Smrg// message from the death test child process and log it with the FATAL 310848b8605Smrg// severity. On Windows, the message is read from a pipe handle. On other 311848b8605Smrg// platforms, it is read from a file descriptor. 312848b8605Smrgstatic void FailFromInternalError(int fd) { 313848b8605Smrg Message error; 314848b8605Smrg char buffer[256]; 315848b8605Smrg int num_read; 316848b8605Smrg 317848b8605Smrg do { 318848b8605Smrg while ((num_read = posix::Read(fd, buffer, 255)) > 0) { 319848b8605Smrg buffer[num_read] = '\0'; 320848b8605Smrg error << buffer; 321848b8605Smrg } 322848b8605Smrg } while (num_read == -1 && errno == EINTR); 323848b8605Smrg 324848b8605Smrg if (num_read == 0) { 325848b8605Smrg GTEST_LOG_(FATAL) << error.GetString(); 326848b8605Smrg } else { 327848b8605Smrg const int last_error = errno; 328848b8605Smrg GTEST_LOG_(FATAL) << "Error while reading death test internal: " 329848b8605Smrg << GetLastErrnoDescription() << " [" << last_error << "]"; 330848b8605Smrg } 331848b8605Smrg} 332848b8605Smrg 333848b8605Smrg// Death test constructor. Increments the running death test count 334848b8605Smrg// for the current test. 335848b8605SmrgDeathTest::DeathTest() { 336848b8605Smrg TestInfo* const info = GetUnitTestImpl()->current_test_info(); 337848b8605Smrg if (info == NULL) { 338848b8605Smrg DeathTestAbort("Cannot run a death test outside of a TEST or " 339848b8605Smrg "TEST_F construct"); 340848b8605Smrg } 341848b8605Smrg} 342848b8605Smrg 343848b8605Smrg// Creates and returns a death test by dispatching to the current 344848b8605Smrg// death test factory. 345848b8605Smrgbool DeathTest::Create(const char* statement, const RE* regex, 346848b8605Smrg const char* file, int line, DeathTest** test) { 347848b8605Smrg return GetUnitTestImpl()->death_test_factory()->Create( 348848b8605Smrg statement, regex, file, line, test); 349848b8605Smrg} 350848b8605Smrg 351848b8605Smrgconst char* DeathTest::LastMessage() { 352848b8605Smrg return last_death_test_message_.c_str(); 353848b8605Smrg} 354848b8605Smrg 355848b8605Smrgvoid DeathTest::set_last_death_test_message(const std::string& message) { 356848b8605Smrg last_death_test_message_ = message; 357848b8605Smrg} 358848b8605Smrg 359848b8605Smrgstd::string DeathTest::last_death_test_message_; 360848b8605Smrg 361848b8605Smrg// Provides cross platform implementation for some death functionality. 362848b8605Smrgclass DeathTestImpl : public DeathTest { 363848b8605Smrg protected: 364848b8605Smrg DeathTestImpl(const char* a_statement, const RE* a_regex) 365848b8605Smrg : statement_(a_statement), 366848b8605Smrg regex_(a_regex), 367848b8605Smrg spawned_(false), 368848b8605Smrg status_(-1), 369848b8605Smrg outcome_(IN_PROGRESS), 370848b8605Smrg read_fd_(-1), 371848b8605Smrg write_fd_(-1) {} 372848b8605Smrg 373848b8605Smrg // read_fd_ is expected to be closed and cleared by a derived class. 374848b8605Smrg ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); } 375848b8605Smrg 376848b8605Smrg void Abort(AbortReason reason); 377848b8605Smrg virtual bool Passed(bool status_ok); 378848b8605Smrg 379848b8605Smrg const char* statement() const { return statement_; } 380848b8605Smrg const RE* regex() const { return regex_; } 381848b8605Smrg bool spawned() const { return spawned_; } 382848b8605Smrg void set_spawned(bool is_spawned) { spawned_ = is_spawned; } 383848b8605Smrg int status() const { return status_; } 384848b8605Smrg void set_status(int a_status) { status_ = a_status; } 385848b8605Smrg DeathTestOutcome outcome() const { return outcome_; } 386848b8605Smrg void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; } 387848b8605Smrg int read_fd() const { return read_fd_; } 388848b8605Smrg void set_read_fd(int fd) { read_fd_ = fd; } 389848b8605Smrg int write_fd() const { return write_fd_; } 390848b8605Smrg void set_write_fd(int fd) { write_fd_ = fd; } 391848b8605Smrg 392848b8605Smrg // Called in the parent process only. Reads the result code of the death 393848b8605Smrg // test child process via a pipe, interprets it to set the outcome_ 394848b8605Smrg // member, and closes read_fd_. Outputs diagnostics and terminates in 395848b8605Smrg // case of unexpected codes. 396848b8605Smrg void ReadAndInterpretStatusByte(); 397848b8605Smrg 398848b8605Smrg private: 399848b8605Smrg // The textual content of the code this object is testing. This class 400848b8605Smrg // doesn't own this string and should not attempt to delete it. 401848b8605Smrg const char* const statement_; 402848b8605Smrg // The regular expression which test output must match. DeathTestImpl 403848b8605Smrg // doesn't own this object and should not attempt to delete it. 404848b8605Smrg const RE* const regex_; 405848b8605Smrg // True if the death test child process has been successfully spawned. 406848b8605Smrg bool spawned_; 407848b8605Smrg // The exit status of the child process. 408848b8605Smrg int status_; 409848b8605Smrg // How the death test concluded. 410848b8605Smrg DeathTestOutcome outcome_; 411848b8605Smrg // Descriptor to the read end of the pipe to the child process. It is 412848b8605Smrg // always -1 in the child process. The child keeps its write end of the 413848b8605Smrg // pipe in write_fd_. 414848b8605Smrg int read_fd_; 415848b8605Smrg // Descriptor to the child's write end of the pipe to the parent process. 416848b8605Smrg // It is always -1 in the parent process. The parent keeps its end of the 417848b8605Smrg // pipe in read_fd_. 418848b8605Smrg int write_fd_; 419848b8605Smrg}; 420848b8605Smrg 421848b8605Smrg// Called in the parent process only. Reads the result code of the death 422848b8605Smrg// test child process via a pipe, interprets it to set the outcome_ 423848b8605Smrg// member, and closes read_fd_. Outputs diagnostics and terminates in 424848b8605Smrg// case of unexpected codes. 425848b8605Smrgvoid DeathTestImpl::ReadAndInterpretStatusByte() { 426848b8605Smrg char flag; 427848b8605Smrg int bytes_read; 428848b8605Smrg 429848b8605Smrg // The read() here blocks until data is available (signifying the 430848b8605Smrg // failure of the death test) or until the pipe is closed (signifying 431848b8605Smrg // its success), so it's okay to call this in the parent before 432848b8605Smrg // the child process has exited. 433848b8605Smrg do { 434848b8605Smrg bytes_read = posix::Read(read_fd(), &flag, 1); 435848b8605Smrg } while (bytes_read == -1 && errno == EINTR); 436848b8605Smrg 437848b8605Smrg if (bytes_read == 0) { 438848b8605Smrg set_outcome(DIED); 439848b8605Smrg } else if (bytes_read == 1) { 440848b8605Smrg switch (flag) { 441848b8605Smrg case kDeathTestReturned: 442848b8605Smrg set_outcome(RETURNED); 443848b8605Smrg break; 444848b8605Smrg case kDeathTestThrew: 445848b8605Smrg set_outcome(THREW); 446848b8605Smrg break; 447848b8605Smrg case kDeathTestLived: 448848b8605Smrg set_outcome(LIVED); 449848b8605Smrg break; 450848b8605Smrg case kDeathTestInternalError: 451848b8605Smrg FailFromInternalError(read_fd()); // Does not return. 452848b8605Smrg break; 453848b8605Smrg default: 454848b8605Smrg GTEST_LOG_(FATAL) << "Death test child process reported " 455848b8605Smrg << "unexpected status byte (" 456848b8605Smrg << static_cast<unsigned int>(flag) << ")"; 457848b8605Smrg } 458848b8605Smrg } else { 459848b8605Smrg GTEST_LOG_(FATAL) << "Read from death test child process failed: " 460848b8605Smrg << GetLastErrnoDescription(); 461848b8605Smrg } 462848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); 463848b8605Smrg set_read_fd(-1); 464848b8605Smrg} 465848b8605Smrg 466848b8605Smrg// Signals that the death test code which should have exited, didn't. 467848b8605Smrg// Should be called only in a death test child process. 468848b8605Smrg// Writes a status byte to the child's status file descriptor, then 469848b8605Smrg// calls _exit(1). 470848b8605Smrgvoid DeathTestImpl::Abort(AbortReason reason) { 471848b8605Smrg // The parent process considers the death test to be a failure if 472848b8605Smrg // it finds any data in our pipe. So, here we write a single flag byte 473848b8605Smrg // to the pipe, then exit. 474848b8605Smrg const char status_ch = 475848b8605Smrg reason == TEST_DID_NOT_DIE ? kDeathTestLived : 476848b8605Smrg reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned; 477848b8605Smrg 478848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); 479848b8605Smrg // We are leaking the descriptor here because on some platforms (i.e., 480848b8605Smrg // when built as Windows DLL), destructors of global objects will still 481848b8605Smrg // run after calling _exit(). On such systems, write_fd_ will be 482848b8605Smrg // indirectly closed from the destructor of UnitTestImpl, causing double 483848b8605Smrg // close if it is also closed here. On debug configurations, double close 484848b8605Smrg // may assert. As there are no in-process buffers to flush here, we are 485848b8605Smrg // relying on the OS to close the descriptor after the process terminates 486848b8605Smrg // when the destructors are not run. 487848b8605Smrg _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) 488848b8605Smrg} 489848b8605Smrg 490848b8605Smrg// Returns an indented copy of stderr output for a death test. 491848b8605Smrg// This makes distinguishing death test output lines from regular log lines 492848b8605Smrg// much easier. 493848b8605Smrgstatic ::std::string FormatDeathTestOutput(const ::std::string& output) { 494848b8605Smrg ::std::string ret; 495848b8605Smrg for (size_t at = 0; ; ) { 496848b8605Smrg const size_t line_end = output.find('\n', at); 497848b8605Smrg ret += "[ DEATH ] "; 498848b8605Smrg if (line_end == ::std::string::npos) { 499848b8605Smrg ret += output.substr(at); 500848b8605Smrg break; 501848b8605Smrg } 502848b8605Smrg ret += output.substr(at, line_end + 1 - at); 503848b8605Smrg at = line_end + 1; 504848b8605Smrg } 505848b8605Smrg return ret; 506848b8605Smrg} 507848b8605Smrg 508848b8605Smrg// Assesses the success or failure of a death test, using both private 509848b8605Smrg// members which have previously been set, and one argument: 510848b8605Smrg// 511848b8605Smrg// Private data members: 512848b8605Smrg// outcome: An enumeration describing how the death test 513848b8605Smrg// concluded: DIED, LIVED, THREW, or RETURNED. The death test 514848b8605Smrg// fails in the latter three cases. 515848b8605Smrg// status: The exit status of the child process. On *nix, it is in the 516848b8605Smrg// in the format specified by wait(2). On Windows, this is the 517848b8605Smrg// value supplied to the ExitProcess() API or a numeric code 518848b8605Smrg// of the exception that terminated the program. 519848b8605Smrg// regex: A regular expression object to be applied to 520848b8605Smrg// the test's captured standard error output; the death test 521848b8605Smrg// fails if it does not match. 522848b8605Smrg// 523848b8605Smrg// Argument: 524848b8605Smrg// status_ok: true if exit_status is acceptable in the context of 525848b8605Smrg// this particular death test, which fails if it is false 526848b8605Smrg// 527848b8605Smrg// Returns true iff all of the above conditions are met. Otherwise, the 528848b8605Smrg// first failing condition, in the order given above, is the one that is 529848b8605Smrg// reported. Also sets the last death test message string. 530848b8605Smrgbool DeathTestImpl::Passed(bool status_ok) { 531848b8605Smrg if (!spawned()) 532848b8605Smrg return false; 533848b8605Smrg 534848b8605Smrg const std::string error_message = GetCapturedStderr(); 535848b8605Smrg 536848b8605Smrg bool success = false; 537848b8605Smrg Message buffer; 538848b8605Smrg 539848b8605Smrg buffer << "Death test: " << statement() << "\n"; 540848b8605Smrg switch (outcome()) { 541848b8605Smrg case LIVED: 542848b8605Smrg buffer << " Result: failed to die.\n" 543848b8605Smrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 544848b8605Smrg break; 545848b8605Smrg case THREW: 546848b8605Smrg buffer << " Result: threw an exception.\n" 547848b8605Smrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 548848b8605Smrg break; 549848b8605Smrg case RETURNED: 550848b8605Smrg buffer << " Result: illegal return in test statement.\n" 551848b8605Smrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 552848b8605Smrg break; 553848b8605Smrg case DIED: 554848b8605Smrg if (status_ok) { 555848b8605Smrg const bool matched = RE::PartialMatch(error_message.c_str(), *regex()); 556848b8605Smrg if (matched) { 557848b8605Smrg success = true; 558848b8605Smrg } else { 559848b8605Smrg buffer << " Result: died but not with expected error.\n" 560848b8605Smrg << " Expected: " << regex()->pattern() << "\n" 561848b8605Smrg << "Actual msg:\n" << FormatDeathTestOutput(error_message); 562848b8605Smrg } 563848b8605Smrg } else { 564848b8605Smrg buffer << " Result: died but not with expected exit code:\n" 565848b8605Smrg << " " << ExitSummary(status()) << "\n" 566848b8605Smrg << "Actual msg:\n" << FormatDeathTestOutput(error_message); 567848b8605Smrg } 568848b8605Smrg break; 569848b8605Smrg case IN_PROGRESS: 570848b8605Smrg default: 571848b8605Smrg GTEST_LOG_(FATAL) 572848b8605Smrg << "DeathTest::Passed somehow called before conclusion of test"; 573848b8605Smrg } 574848b8605Smrg 575848b8605Smrg DeathTest::set_last_death_test_message(buffer.GetString()); 576848b8605Smrg return success; 577848b8605Smrg} 578848b8605Smrg 579848b8605Smrg# if GTEST_OS_WINDOWS 580848b8605Smrg// WindowsDeathTest implements death tests on Windows. Due to the 581848b8605Smrg// specifics of starting new processes on Windows, death tests there are 582848b8605Smrg// always threadsafe, and Google Test considers the 583848b8605Smrg// --gtest_death_test_style=fast setting to be equivalent to 584848b8605Smrg// --gtest_death_test_style=threadsafe there. 585848b8605Smrg// 586848b8605Smrg// A few implementation notes: Like the Linux version, the Windows 587848b8605Smrg// implementation uses pipes for child-to-parent communication. But due to 588848b8605Smrg// the specifics of pipes on Windows, some extra steps are required: 589848b8605Smrg// 590848b8605Smrg// 1. The parent creates a communication pipe and stores handles to both 591848b8605Smrg// ends of it. 592848b8605Smrg// 2. The parent starts the child and provides it with the information 593848b8605Smrg// necessary to acquire the handle to the write end of the pipe. 594848b8605Smrg// 3. The child acquires the write end of the pipe and signals the parent 595848b8605Smrg// using a Windows event. 596848b8605Smrg// 4. Now the parent can release the write end of the pipe on its side. If 597848b8605Smrg// this is done before step 3, the object's reference count goes down to 598848b8605Smrg// 0 and it is destroyed, preventing the child from acquiring it. The 599848b8605Smrg// parent now has to release it, or read operations on the read end of 600848b8605Smrg// the pipe will not return when the child terminates. 601848b8605Smrg// 5. The parent reads child's output through the pipe (outcome code and 602848b8605Smrg// any possible error messages) from the pipe, and its stderr and then 603848b8605Smrg// determines whether to fail the test. 604848b8605Smrg// 605848b8605Smrg// Note: to distinguish Win32 API calls from the local method and function 606848b8605Smrg// calls, the former are explicitly resolved in the global namespace. 607848b8605Smrg// 608848b8605Smrgclass WindowsDeathTest : public DeathTestImpl { 609848b8605Smrg public: 610848b8605Smrg WindowsDeathTest(const char* a_statement, 611848b8605Smrg const RE* a_regex, 612848b8605Smrg const char* file, 613848b8605Smrg int line) 614848b8605Smrg : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {} 615848b8605Smrg 616848b8605Smrg // All of these virtual functions are inherited from DeathTest. 617848b8605Smrg virtual int Wait(); 618848b8605Smrg virtual TestRole AssumeRole(); 619848b8605Smrg 620848b8605Smrg private: 621848b8605Smrg // The name of the file in which the death test is located. 622848b8605Smrg const char* const file_; 623848b8605Smrg // The line number on which the death test is located. 624848b8605Smrg const int line_; 625848b8605Smrg // Handle to the write end of the pipe to the child process. 626848b8605Smrg AutoHandle write_handle_; 627848b8605Smrg // Child process handle. 628848b8605Smrg AutoHandle child_handle_; 629848b8605Smrg // Event the child process uses to signal the parent that it has 630848b8605Smrg // acquired the handle to the write end of the pipe. After seeing this 631848b8605Smrg // event the parent can release its own handles to make sure its 632848b8605Smrg // ReadFile() calls return when the child terminates. 633848b8605Smrg AutoHandle event_handle_; 634848b8605Smrg}; 635848b8605Smrg 636848b8605Smrg// Waits for the child in a death test to exit, returning its exit 637848b8605Smrg// status, or 0 if no child process exists. As a side effect, sets the 638848b8605Smrg// outcome data member. 639848b8605Smrgint WindowsDeathTest::Wait() { 640848b8605Smrg if (!spawned()) 641848b8605Smrg return 0; 642848b8605Smrg 643848b8605Smrg // Wait until the child either signals that it has acquired the write end 644848b8605Smrg // of the pipe or it dies. 645848b8605Smrg const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() }; 646848b8605Smrg switch (::WaitForMultipleObjects(2, 647848b8605Smrg wait_handles, 648848b8605Smrg FALSE, // Waits for any of the handles. 649848b8605Smrg INFINITE)) { 650848b8605Smrg case WAIT_OBJECT_0: 651848b8605Smrg case WAIT_OBJECT_0 + 1: 652848b8605Smrg break; 653848b8605Smrg default: 654848b8605Smrg GTEST_DEATH_TEST_CHECK_(false); // Should not get here. 655848b8605Smrg } 656848b8605Smrg 657848b8605Smrg // The child has acquired the write end of the pipe or exited. 658848b8605Smrg // We release the handle on our side and continue. 659848b8605Smrg write_handle_.Reset(); 660848b8605Smrg event_handle_.Reset(); 661848b8605Smrg 662848b8605Smrg ReadAndInterpretStatusByte(); 663848b8605Smrg 664848b8605Smrg // Waits for the child process to exit if it haven't already. This 665848b8605Smrg // returns immediately if the child has already exited, regardless of 666848b8605Smrg // whether previous calls to WaitForMultipleObjects synchronized on this 667848b8605Smrg // handle or not. 668848b8605Smrg GTEST_DEATH_TEST_CHECK_( 669848b8605Smrg WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(), 670848b8605Smrg INFINITE)); 671848b8605Smrg DWORD status_code; 672848b8605Smrg GTEST_DEATH_TEST_CHECK_( 673848b8605Smrg ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE); 674848b8605Smrg child_handle_.Reset(); 675848b8605Smrg set_status(static_cast<int>(status_code)); 676848b8605Smrg return status(); 677848b8605Smrg} 678848b8605Smrg 679848b8605Smrg// The AssumeRole process for a Windows death test. It creates a child 680848b8605Smrg// process with the same executable as the current process to run the 681848b8605Smrg// death test. The child process is given the --gtest_filter and 682848b8605Smrg// --gtest_internal_run_death_test flags such that it knows to run the 683848b8605Smrg// current death test only. 684848b8605SmrgDeathTest::TestRole WindowsDeathTest::AssumeRole() { 685848b8605Smrg const UnitTestImpl* const impl = GetUnitTestImpl(); 686848b8605Smrg const InternalRunDeathTestFlag* const flag = 687848b8605Smrg impl->internal_run_death_test_flag(); 688848b8605Smrg const TestInfo* const info = impl->current_test_info(); 689848b8605Smrg const int death_test_index = info->result()->death_test_count(); 690848b8605Smrg 691848b8605Smrg if (flag != NULL) { 692848b8605Smrg // ParseInternalRunDeathTestFlag() has performed all the necessary 693848b8605Smrg // processing. 694848b8605Smrg set_write_fd(flag->write_fd()); 695848b8605Smrg return EXECUTE_TEST; 696848b8605Smrg } 697848b8605Smrg 698848b8605Smrg // WindowsDeathTest uses an anonymous pipe to communicate results of 699848b8605Smrg // a death test. 700848b8605Smrg SECURITY_ATTRIBUTES handles_are_inheritable = { 701848b8605Smrg sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; 702848b8605Smrg HANDLE read_handle, write_handle; 703848b8605Smrg GTEST_DEATH_TEST_CHECK_( 704848b8605Smrg ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable, 705848b8605Smrg 0) // Default buffer size. 706848b8605Smrg != FALSE); 707848b8605Smrg set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle), 708848b8605Smrg O_RDONLY)); 709848b8605Smrg write_handle_.Reset(write_handle); 710848b8605Smrg event_handle_.Reset(::CreateEvent( 711848b8605Smrg &handles_are_inheritable, 712848b8605Smrg TRUE, // The event will automatically reset to non-signaled state. 713848b8605Smrg FALSE, // The initial state is non-signalled. 714848b8605Smrg NULL)); // The even is unnamed. 715848b8605Smrg GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL); 716848b8605Smrg const std::string filter_flag = 717848b8605Smrg std::string("--") + GTEST_FLAG_PREFIX_ + kFilterFlag + "=" + 718848b8605Smrg info->test_case_name() + "." + info->name(); 719848b8605Smrg const std::string internal_flag = 720848b8605Smrg std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + 721848b8605Smrg "=" + file_ + "|" + StreamableToString(line_) + "|" + 722848b8605Smrg StreamableToString(death_test_index) + "|" + 723848b8605Smrg StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) + 724848b8605Smrg // size_t has the same width as pointers on both 32-bit and 64-bit 725848b8605Smrg // Windows platforms. 726848b8605Smrg // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. 727848b8605Smrg "|" + StreamableToString(reinterpret_cast<size_t>(write_handle)) + 728848b8605Smrg "|" + StreamableToString(reinterpret_cast<size_t>(event_handle_.Get())); 729848b8605Smrg 730848b8605Smrg char executable_path[_MAX_PATH + 1]; // NOLINT 731848b8605Smrg GTEST_DEATH_TEST_CHECK_( 732848b8605Smrg _MAX_PATH + 1 != ::GetModuleFileNameA(NULL, 733848b8605Smrg executable_path, 734848b8605Smrg _MAX_PATH)); 735848b8605Smrg 736848b8605Smrg std::string command_line = 737848b8605Smrg std::string(::GetCommandLineA()) + " " + filter_flag + " \"" + 738848b8605Smrg internal_flag + "\""; 739848b8605Smrg 740848b8605Smrg DeathTest::set_last_death_test_message(""); 741848b8605Smrg 742848b8605Smrg CaptureStderr(); 743848b8605Smrg // Flush the log buffers since the log streams are shared with the child. 744848b8605Smrg FlushInfoLog(); 745848b8605Smrg 746848b8605Smrg // The child process will share the standard handles with the parent. 747848b8605Smrg STARTUPINFOA startup_info; 748848b8605Smrg memset(&startup_info, 0, sizeof(STARTUPINFO)); 749848b8605Smrg startup_info.dwFlags = STARTF_USESTDHANDLES; 750848b8605Smrg startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE); 751848b8605Smrg startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); 752848b8605Smrg startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE); 753848b8605Smrg 754848b8605Smrg PROCESS_INFORMATION process_info; 755848b8605Smrg GTEST_DEATH_TEST_CHECK_(::CreateProcessA( 756848b8605Smrg executable_path, 757848b8605Smrg const_cast<char*>(command_line.c_str()), 758848b8605Smrg NULL, // Retuned process handle is not inheritable. 759848b8605Smrg NULL, // Retuned thread handle is not inheritable. 760848b8605Smrg TRUE, // Child inherits all inheritable handles (for write_handle_). 761848b8605Smrg 0x0, // Default creation flags. 762848b8605Smrg NULL, // Inherit the parent's environment. 763848b8605Smrg UnitTest::GetInstance()->original_working_dir(), 764848b8605Smrg &startup_info, 765848b8605Smrg &process_info) != FALSE); 766848b8605Smrg child_handle_.Reset(process_info.hProcess); 767848b8605Smrg ::CloseHandle(process_info.hThread); 768848b8605Smrg set_spawned(true); 769848b8605Smrg return OVERSEE_TEST; 770848b8605Smrg} 771848b8605Smrg# else // We are not on Windows. 772848b8605Smrg 773848b8605Smrg// ForkingDeathTest provides implementations for most of the abstract 774848b8605Smrg// methods of the DeathTest interface. Only the AssumeRole method is 775848b8605Smrg// left undefined. 776848b8605Smrgclass ForkingDeathTest : public DeathTestImpl { 777848b8605Smrg public: 778848b8605Smrg ForkingDeathTest(const char* statement, const RE* regex); 779848b8605Smrg 780848b8605Smrg // All of these virtual functions are inherited from DeathTest. 781848b8605Smrg virtual int Wait(); 782848b8605Smrg 783848b8605Smrg protected: 784848b8605Smrg void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; } 785848b8605Smrg 786848b8605Smrg private: 787848b8605Smrg // PID of child process during death test; 0 in the child process itself. 788848b8605Smrg pid_t child_pid_; 789848b8605Smrg}; 790848b8605Smrg 791848b8605Smrg// Constructs a ForkingDeathTest. 792848b8605SmrgForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex) 793848b8605Smrg : DeathTestImpl(a_statement, a_regex), 794848b8605Smrg child_pid_(-1) {} 795848b8605Smrg 796848b8605Smrg// Waits for the child in a death test to exit, returning its exit 797848b8605Smrg// status, or 0 if no child process exists. As a side effect, sets the 798848b8605Smrg// outcome data member. 799848b8605Smrgint ForkingDeathTest::Wait() { 800848b8605Smrg if (!spawned()) 801848b8605Smrg return 0; 802848b8605Smrg 803848b8605Smrg ReadAndInterpretStatusByte(); 804848b8605Smrg 805848b8605Smrg int status_value; 806848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0)); 807848b8605Smrg set_status(status_value); 808848b8605Smrg return status_value; 809848b8605Smrg} 810848b8605Smrg 811848b8605Smrg// A concrete death test class that forks, then immediately runs the test 812848b8605Smrg// in the child process. 813848b8605Smrgclass NoExecDeathTest : public ForkingDeathTest { 814848b8605Smrg public: 815848b8605Smrg NoExecDeathTest(const char* a_statement, const RE* a_regex) : 816848b8605Smrg ForkingDeathTest(a_statement, a_regex) { } 817848b8605Smrg virtual TestRole AssumeRole(); 818848b8605Smrg}; 819848b8605Smrg 820848b8605Smrg// The AssumeRole process for a fork-and-run death test. It implements a 821848b8605Smrg// straightforward fork, with a simple pipe to transmit the status byte. 822848b8605SmrgDeathTest::TestRole NoExecDeathTest::AssumeRole() { 823848b8605Smrg const size_t thread_count = GetThreadCount(); 824848b8605Smrg if (thread_count != 1) { 825848b8605Smrg GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count); 826848b8605Smrg } 827848b8605Smrg 828848b8605Smrg int pipe_fd[2]; 829848b8605Smrg GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); 830848b8605Smrg 831848b8605Smrg DeathTest::set_last_death_test_message(""); 832848b8605Smrg CaptureStderr(); 833848b8605Smrg // When we fork the process below, the log file buffers are copied, but the 834848b8605Smrg // file descriptors are shared. We flush all log files here so that closing 835848b8605Smrg // the file descriptors in the child process doesn't throw off the 836848b8605Smrg // synchronization between descriptors and buffers in the parent process. 837848b8605Smrg // This is as close to the fork as possible to avoid a race condition in case 838848b8605Smrg // there are multiple threads running before the death test, and another 839848b8605Smrg // thread writes to the log file. 840848b8605Smrg FlushInfoLog(); 841848b8605Smrg 842848b8605Smrg const pid_t child_pid = fork(); 843848b8605Smrg GTEST_DEATH_TEST_CHECK_(child_pid != -1); 844848b8605Smrg set_child_pid(child_pid); 845848b8605Smrg if (child_pid == 0) { 846848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0])); 847848b8605Smrg set_write_fd(pipe_fd[1]); 848848b8605Smrg // Redirects all logging to stderr in the child process to prevent 849848b8605Smrg // concurrent writes to the log files. We capture stderr in the parent 850848b8605Smrg // process and append the child process' output to a log. 851848b8605Smrg LogToStderr(); 852848b8605Smrg // Event forwarding to the listeners of event listener API mush be shut 853848b8605Smrg // down in death test subprocesses. 854848b8605Smrg GetUnitTestImpl()->listeners()->SuppressEventForwarding(); 855848b8605Smrg g_in_fast_death_test_child = true; 856848b8605Smrg return EXECUTE_TEST; 857848b8605Smrg } else { 858848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); 859848b8605Smrg set_read_fd(pipe_fd[0]); 860848b8605Smrg set_spawned(true); 861848b8605Smrg return OVERSEE_TEST; 862848b8605Smrg } 863848b8605Smrg} 864848b8605Smrg 865848b8605Smrg// A concrete death test class that forks and re-executes the main 866848b8605Smrg// program from the beginning, with command-line flags set that cause 867848b8605Smrg// only this specific death test to be run. 868848b8605Smrgclass ExecDeathTest : public ForkingDeathTest { 869848b8605Smrg public: 870848b8605Smrg ExecDeathTest(const char* a_statement, const RE* a_regex, 871848b8605Smrg const char* file, int line) : 872848b8605Smrg ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { } 873848b8605Smrg virtual TestRole AssumeRole(); 874848b8605Smrg private: 875848b8605Smrg static ::std::vector<testing::internal::string> 876848b8605Smrg GetArgvsForDeathTestChildProcess() { 877848b8605Smrg ::std::vector<testing::internal::string> args = GetInjectableArgvs(); 878848b8605Smrg return args; 879848b8605Smrg } 880848b8605Smrg // The name of the file in which the death test is located. 881848b8605Smrg const char* const file_; 882848b8605Smrg // The line number on which the death test is located. 883848b8605Smrg const int line_; 884848b8605Smrg}; 885848b8605Smrg 886848b8605Smrg// Utility class for accumulating command-line arguments. 887848b8605Smrgclass Arguments { 888848b8605Smrg public: 889848b8605Smrg Arguments() { 890848b8605Smrg args_.push_back(NULL); 891848b8605Smrg } 892848b8605Smrg 893848b8605Smrg ~Arguments() { 894848b8605Smrg for (std::vector<char*>::iterator i = args_.begin(); i != args_.end(); 895848b8605Smrg ++i) { 896848b8605Smrg free(*i); 897848b8605Smrg } 898848b8605Smrg } 899848b8605Smrg void AddArgument(const char* argument) { 900848b8605Smrg args_.insert(args_.end() - 1, posix::StrDup(argument)); 901848b8605Smrg } 902848b8605Smrg 903848b8605Smrg template <typename Str> 904848b8605Smrg void AddArguments(const ::std::vector<Str>& arguments) { 905848b8605Smrg for (typename ::std::vector<Str>::const_iterator i = arguments.begin(); 906848b8605Smrg i != arguments.end(); 907848b8605Smrg ++i) { 908848b8605Smrg args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); 909848b8605Smrg } 910848b8605Smrg } 911848b8605Smrg char* const* Argv() { 912848b8605Smrg return &args_[0]; 913848b8605Smrg } 914848b8605Smrg 915848b8605Smrg private: 916848b8605Smrg std::vector<char*> args_; 917848b8605Smrg}; 918848b8605Smrg 919848b8605Smrg// A struct that encompasses the arguments to the child process of a 920848b8605Smrg// threadsafe-style death test process. 921848b8605Smrgstruct ExecDeathTestArgs { 922848b8605Smrg char* const* argv; // Command-line arguments for the child's call to exec 923848b8605Smrg int close_fd; // File descriptor to close; the read end of a pipe 924848b8605Smrg}; 925848b8605Smrg 926848b8605Smrg# if GTEST_OS_MAC 927848b8605Smrginline char** GetEnviron() { 928848b8605Smrg // When Google Test is built as a framework on MacOS X, the environ variable 929848b8605Smrg // is unavailable. Apple's documentation (man environ) recommends using 930848b8605Smrg // _NSGetEnviron() instead. 931848b8605Smrg return *_NSGetEnviron(); 932848b8605Smrg} 933848b8605Smrg# else 934848b8605Smrg// Some POSIX platforms expect you to declare environ. extern "C" makes 935848b8605Smrg// it reside in the global namespace. 936848b8605Smrgextern "C" char** environ; 937848b8605Smrginline char** GetEnviron() { return environ; } 938848b8605Smrg# endif // GTEST_OS_MAC 939848b8605Smrg 940848b8605Smrg# if !GTEST_OS_QNX 941848b8605Smrg// The main function for a threadsafe-style death test child process. 942848b8605Smrg// This function is called in a clone()-ed process and thus must avoid 943848b8605Smrg// any potentially unsafe operations like malloc or libc functions. 944848b8605Smrgstatic int ExecDeathTestChildMain(void* child_arg) { 945848b8605Smrg ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg); 946848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd)); 947848b8605Smrg 948848b8605Smrg // We need to execute the test program in the same environment where 949848b8605Smrg // it was originally invoked. Therefore we change to the original 950848b8605Smrg // working directory first. 951848b8605Smrg const char* const original_dir = 952848b8605Smrg UnitTest::GetInstance()->original_working_dir(); 953848b8605Smrg // We can safely call chdir() as it's a direct system call. 954848b8605Smrg if (chdir(original_dir) != 0) { 955848b8605Smrg DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + 956848b8605Smrg GetLastErrnoDescription()); 957848b8605Smrg return EXIT_FAILURE; 958848b8605Smrg } 959848b8605Smrg 960848b8605Smrg // We can safely call execve() as it's a direct system call. We 961848b8605Smrg // cannot use execvp() as it's a libc function and thus potentially 962848b8605Smrg // unsafe. Since execve() doesn't search the PATH, the user must 963848b8605Smrg // invoke the test program via a valid path that contains at least 964848b8605Smrg // one path separator. 965848b8605Smrg execve(args->argv[0], args->argv, GetEnviron()); 966848b8605Smrg DeathTestAbort(std::string("execve(") + args->argv[0] + ", ...) in " + 967848b8605Smrg original_dir + " failed: " + 968848b8605Smrg GetLastErrnoDescription()); 969848b8605Smrg return EXIT_FAILURE; 970848b8605Smrg} 971848b8605Smrg# endif // !GTEST_OS_QNX 972848b8605Smrg 973848b8605Smrg// Two utility routines that together determine the direction the stack 974848b8605Smrg// grows. 975848b8605Smrg// This could be accomplished more elegantly by a single recursive 976848b8605Smrg// function, but we want to guard against the unlikely possibility of 977848b8605Smrg// a smart compiler optimizing the recursion away. 978848b8605Smrg// 979848b8605Smrg// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining 980848b8605Smrg// StackLowerThanAddress into StackGrowsDown, which then doesn't give 981848b8605Smrg// correct answer. 982848b8605Smrgvoid StackLowerThanAddress(const void* ptr, bool* result) GTEST_NO_INLINE_; 983848b8605Smrgvoid StackLowerThanAddress(const void* ptr, bool* result) { 984848b8605Smrg int dummy; 985848b8605Smrg *result = (&dummy < ptr); 986848b8605Smrg} 987848b8605Smrg 988848b8605Smrgbool StackGrowsDown() { 989848b8605Smrg int dummy; 990848b8605Smrg bool result; 991848b8605Smrg StackLowerThanAddress(&dummy, &result); 992848b8605Smrg return result; 993848b8605Smrg} 994848b8605Smrg 995848b8605Smrg// Spawns a child process with the same executable as the current process in 996848b8605Smrg// a thread-safe manner and instructs it to run the death test. The 997848b8605Smrg// implementation uses fork(2) + exec. On systems where clone(2) is 998848b8605Smrg// available, it is used instead, being slightly more thread-safe. On QNX, 999848b8605Smrg// fork supports only single-threaded environments, so this function uses 1000848b8605Smrg// spawn(2) there instead. The function dies with an error message if 1001848b8605Smrg// anything goes wrong. 1002848b8605Smrgstatic pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) { 1003848b8605Smrg ExecDeathTestArgs args = { argv, close_fd }; 1004848b8605Smrg pid_t child_pid = -1; 1005848b8605Smrg 1006848b8605Smrg# if GTEST_OS_QNX 1007848b8605Smrg // Obtains the current directory and sets it to be closed in the child 1008848b8605Smrg // process. 1009848b8605Smrg const int cwd_fd = open(".", O_RDONLY); 1010848b8605Smrg GTEST_DEATH_TEST_CHECK_(cwd_fd != -1); 1011848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC)); 1012848b8605Smrg // We need to execute the test program in the same environment where 1013848b8605Smrg // it was originally invoked. Therefore we change to the original 1014848b8605Smrg // working directory first. 1015848b8605Smrg const char* const original_dir = 1016848b8605Smrg UnitTest::GetInstance()->original_working_dir(); 1017848b8605Smrg // We can safely call chdir() as it's a direct system call. 1018848b8605Smrg if (chdir(original_dir) != 0) { 1019848b8605Smrg DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + 1020848b8605Smrg GetLastErrnoDescription()); 1021848b8605Smrg return EXIT_FAILURE; 1022848b8605Smrg } 1023848b8605Smrg 1024848b8605Smrg int fd_flags; 1025848b8605Smrg // Set close_fd to be closed after spawn. 1026848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD)); 1027848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD, 1028848b8605Smrg fd_flags | FD_CLOEXEC)); 1029848b8605Smrg struct inheritance inherit = {0}; 1030848b8605Smrg // spawn is a system call. 1031848b8605Smrg child_pid = spawn(args.argv[0], 0, NULL, &inherit, args.argv, GetEnviron()); 1032848b8605Smrg // Restores the current working directory. 1033848b8605Smrg GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1); 1034848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd)); 1035848b8605Smrg 1036848b8605Smrg# else // GTEST_OS_QNX 1037848b8605Smrg# if GTEST_OS_LINUX 1038848b8605Smrg // When a SIGPROF signal is received while fork() or clone() are executing, 1039848b8605Smrg // the process may hang. To avoid this, we ignore SIGPROF here and re-enable 1040848b8605Smrg // it after the call to fork()/clone() is complete. 1041848b8605Smrg struct sigaction saved_sigprof_action; 1042848b8605Smrg struct sigaction ignore_sigprof_action; 1043848b8605Smrg memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action)); 1044848b8605Smrg sigemptyset(&ignore_sigprof_action.sa_mask); 1045848b8605Smrg ignore_sigprof_action.sa_handler = SIG_IGN; 1046848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction( 1047848b8605Smrg SIGPROF, &ignore_sigprof_action, &saved_sigprof_action)); 1048848b8605Smrg# endif // GTEST_OS_LINUX 1049848b8605Smrg 1050848b8605Smrg# if GTEST_HAS_CLONE 1051848b8605Smrg const bool use_fork = GTEST_FLAG(death_test_use_fork); 1052848b8605Smrg 1053848b8605Smrg if (!use_fork) { 1054848b8605Smrg static const bool stack_grows_down = StackGrowsDown(); 1055848b8605Smrg const size_t stack_size = getpagesize(); 1056848b8605Smrg // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead. 1057848b8605Smrg void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, 1058848b8605Smrg MAP_ANON | MAP_PRIVATE, -1, 0); 1059848b8605Smrg GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED); 1060848b8605Smrg 1061848b8605Smrg // Maximum stack alignment in bytes: For a downward-growing stack, this 1062848b8605Smrg // amount is subtracted from size of the stack space to get an address 1063848b8605Smrg // that is within the stack space and is aligned on all systems we care 1064848b8605Smrg // about. As far as I know there is no ABI with stack alignment greater 1065848b8605Smrg // than 64. We assume stack and stack_size already have alignment of 1066848b8605Smrg // kMaxStackAlignment. 1067848b8605Smrg const size_t kMaxStackAlignment = 64; 1068848b8605Smrg void* const stack_top = 1069848b8605Smrg static_cast<char*>(stack) + 1070848b8605Smrg (stack_grows_down ? stack_size - kMaxStackAlignment : 0); 1071848b8605Smrg GTEST_DEATH_TEST_CHECK_(stack_size > kMaxStackAlignment && 1072848b8605Smrg reinterpret_cast<intptr_t>(stack_top) % kMaxStackAlignment == 0); 1073848b8605Smrg 1074848b8605Smrg child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args); 1075848b8605Smrg 1076848b8605Smrg GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1); 1077848b8605Smrg } 1078848b8605Smrg# else 1079848b8605Smrg const bool use_fork = true; 1080848b8605Smrg# endif // GTEST_HAS_CLONE 1081848b8605Smrg 1082848b8605Smrg if (use_fork && (child_pid = fork()) == 0) { 1083848b8605Smrg ExecDeathTestChildMain(&args); 1084848b8605Smrg _exit(0); 1085848b8605Smrg } 1086848b8605Smrg# endif // GTEST_OS_QNX 1087848b8605Smrg# if GTEST_OS_LINUX 1088848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_( 1089848b8605Smrg sigaction(SIGPROF, &saved_sigprof_action, NULL)); 1090848b8605Smrg# endif // GTEST_OS_LINUX 1091848b8605Smrg 1092848b8605Smrg GTEST_DEATH_TEST_CHECK_(child_pid != -1); 1093848b8605Smrg return child_pid; 1094848b8605Smrg} 1095848b8605Smrg 1096848b8605Smrg// The AssumeRole process for a fork-and-exec death test. It re-executes the 1097848b8605Smrg// main program from the beginning, setting the --gtest_filter 1098848b8605Smrg// and --gtest_internal_run_death_test flags to cause only the current 1099848b8605Smrg// death test to be re-run. 1100848b8605SmrgDeathTest::TestRole ExecDeathTest::AssumeRole() { 1101848b8605Smrg const UnitTestImpl* const impl = GetUnitTestImpl(); 1102848b8605Smrg const InternalRunDeathTestFlag* const flag = 1103848b8605Smrg impl->internal_run_death_test_flag(); 1104848b8605Smrg const TestInfo* const info = impl->current_test_info(); 1105848b8605Smrg const int death_test_index = info->result()->death_test_count(); 1106848b8605Smrg 1107848b8605Smrg if (flag != NULL) { 1108848b8605Smrg set_write_fd(flag->write_fd()); 1109848b8605Smrg return EXECUTE_TEST; 1110848b8605Smrg } 1111848b8605Smrg 1112848b8605Smrg int pipe_fd[2]; 1113848b8605Smrg GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); 1114848b8605Smrg // Clear the close-on-exec flag on the write end of the pipe, lest 1115848b8605Smrg // it be closed when the child process does an exec: 1116848b8605Smrg GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1); 1117848b8605Smrg 1118848b8605Smrg const std::string filter_flag = 1119848b8605Smrg std::string("--") + GTEST_FLAG_PREFIX_ + kFilterFlag + "=" 1120848b8605Smrg + info->test_case_name() + "." + info->name(); 1121848b8605Smrg const std::string internal_flag = 1122848b8605Smrg std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "=" 1123848b8605Smrg + file_ + "|" + StreamableToString(line_) + "|" 1124848b8605Smrg + StreamableToString(death_test_index) + "|" 1125848b8605Smrg + StreamableToString(pipe_fd[1]); 1126848b8605Smrg Arguments args; 1127848b8605Smrg args.AddArguments(GetArgvsForDeathTestChildProcess()); 1128848b8605Smrg args.AddArgument(filter_flag.c_str()); 1129848b8605Smrg args.AddArgument(internal_flag.c_str()); 1130848b8605Smrg 1131848b8605Smrg DeathTest::set_last_death_test_message(""); 1132848b8605Smrg 1133848b8605Smrg CaptureStderr(); 1134848b8605Smrg // See the comment in NoExecDeathTest::AssumeRole for why the next line 1135848b8605Smrg // is necessary. 1136848b8605Smrg FlushInfoLog(); 1137848b8605Smrg 1138848b8605Smrg const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]); 1139848b8605Smrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); 1140848b8605Smrg set_child_pid(child_pid); 1141848b8605Smrg set_read_fd(pipe_fd[0]); 1142848b8605Smrg set_spawned(true); 1143848b8605Smrg return OVERSEE_TEST; 1144848b8605Smrg} 1145848b8605Smrg 1146848b8605Smrg# endif // !GTEST_OS_WINDOWS 1147848b8605Smrg 1148848b8605Smrg// Creates a concrete DeathTest-derived class that depends on the 1149848b8605Smrg// --gtest_death_test_style flag, and sets the pointer pointed to 1150848b8605Smrg// by the "test" argument to its address. If the test should be 1151848b8605Smrg// skipped, sets that pointer to NULL. Returns true, unless the 1152848b8605Smrg// flag is set to an invalid value. 1153848b8605Smrgbool DefaultDeathTestFactory::Create(const char* statement, const RE* regex, 1154848b8605Smrg const char* file, int line, 1155848b8605Smrg DeathTest** test) { 1156848b8605Smrg UnitTestImpl* const impl = GetUnitTestImpl(); 1157848b8605Smrg const InternalRunDeathTestFlag* const flag = 1158848b8605Smrg impl->internal_run_death_test_flag(); 1159848b8605Smrg const int death_test_index = impl->current_test_info() 1160848b8605Smrg ->increment_death_test_count(); 1161848b8605Smrg 1162848b8605Smrg if (flag != NULL) { 1163848b8605Smrg if (death_test_index > flag->index()) { 1164848b8605Smrg DeathTest::set_last_death_test_message( 1165848b8605Smrg "Death test count (" + StreamableToString(death_test_index) 1166848b8605Smrg + ") somehow exceeded expected maximum (" 1167848b8605Smrg + StreamableToString(flag->index()) + ")"); 1168848b8605Smrg return false; 1169848b8605Smrg } 1170848b8605Smrg 1171848b8605Smrg if (!(flag->file() == file && flag->line() == line && 1172848b8605Smrg flag->index() == death_test_index)) { 1173848b8605Smrg *test = NULL; 1174848b8605Smrg return true; 1175848b8605Smrg } 1176848b8605Smrg } 1177848b8605Smrg 1178848b8605Smrg# if GTEST_OS_WINDOWS 1179848b8605Smrg 1180848b8605Smrg if (GTEST_FLAG(death_test_style) == "threadsafe" || 1181848b8605Smrg GTEST_FLAG(death_test_style) == "fast") { 1182848b8605Smrg *test = new WindowsDeathTest(statement, regex, file, line); 1183848b8605Smrg } 1184848b8605Smrg 1185848b8605Smrg# else 1186848b8605Smrg 1187848b8605Smrg if (GTEST_FLAG(death_test_style) == "threadsafe") { 1188848b8605Smrg *test = new ExecDeathTest(statement, regex, file, line); 1189848b8605Smrg } else if (GTEST_FLAG(death_test_style) == "fast") { 1190848b8605Smrg *test = new NoExecDeathTest(statement, regex); 1191848b8605Smrg } 1192848b8605Smrg 1193848b8605Smrg# endif // GTEST_OS_WINDOWS 1194848b8605Smrg 1195848b8605Smrg else { // NOLINT - this is more readable than unbalanced brackets inside #if. 1196848b8605Smrg DeathTest::set_last_death_test_message( 1197848b8605Smrg "Unknown death test style \"" + GTEST_FLAG(death_test_style) 1198848b8605Smrg + "\" encountered"); 1199848b8605Smrg return false; 1200848b8605Smrg } 1201848b8605Smrg 1202848b8605Smrg return true; 1203848b8605Smrg} 1204848b8605Smrg 1205848b8605Smrg// Splits a given string on a given delimiter, populating a given 1206848b8605Smrg// vector with the fields. GTEST_HAS_DEATH_TEST implies that we have 1207848b8605Smrg// ::std::string, so we can use it here. 1208848b8605Smrgstatic void SplitString(const ::std::string& str, char delimiter, 1209848b8605Smrg ::std::vector< ::std::string>* dest) { 1210848b8605Smrg ::std::vector< ::std::string> parsed; 1211848b8605Smrg ::std::string::size_type pos = 0; 1212848b8605Smrg while (::testing::internal::AlwaysTrue()) { 1213848b8605Smrg const ::std::string::size_type colon = str.find(delimiter, pos); 1214848b8605Smrg if (colon == ::std::string::npos) { 1215848b8605Smrg parsed.push_back(str.substr(pos)); 1216848b8605Smrg break; 1217848b8605Smrg } else { 1218848b8605Smrg parsed.push_back(str.substr(pos, colon - pos)); 1219848b8605Smrg pos = colon + 1; 1220848b8605Smrg } 1221848b8605Smrg } 1222848b8605Smrg dest->swap(parsed); 1223848b8605Smrg} 1224848b8605Smrg 1225848b8605Smrg# if GTEST_OS_WINDOWS 1226848b8605Smrg// Recreates the pipe and event handles from the provided parameters, 1227848b8605Smrg// signals the event, and returns a file descriptor wrapped around the pipe 1228848b8605Smrg// handle. This function is called in the child process only. 1229848b8605Smrgint GetStatusFileDescriptor(unsigned int parent_process_id, 1230848b8605Smrg size_t write_handle_as_size_t, 1231848b8605Smrg size_t event_handle_as_size_t) { 1232848b8605Smrg AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, 1233848b8605Smrg FALSE, // Non-inheritable. 1234848b8605Smrg parent_process_id)); 1235848b8605Smrg if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { 1236848b8605Smrg DeathTestAbort("Unable to open parent process " + 1237848b8605Smrg StreamableToString(parent_process_id)); 1238848b8605Smrg } 1239848b8605Smrg 1240848b8605Smrg // TODO(vladl@google.com): Replace the following check with a 1241848b8605Smrg // compile-time assertion when available. 1242848b8605Smrg GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); 1243848b8605Smrg 1244848b8605Smrg const HANDLE write_handle = 1245848b8605Smrg reinterpret_cast<HANDLE>(write_handle_as_size_t); 1246848b8605Smrg HANDLE dup_write_handle; 1247848b8605Smrg 1248848b8605Smrg // The newly initialized handle is accessible only in in the parent 1249848b8605Smrg // process. To obtain one accessible within the child, we need to use 1250848b8605Smrg // DuplicateHandle. 1251848b8605Smrg if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, 1252848b8605Smrg ::GetCurrentProcess(), &dup_write_handle, 1253848b8605Smrg 0x0, // Requested privileges ignored since 1254848b8605Smrg // DUPLICATE_SAME_ACCESS is used. 1255848b8605Smrg FALSE, // Request non-inheritable handler. 1256848b8605Smrg DUPLICATE_SAME_ACCESS)) { 1257848b8605Smrg DeathTestAbort("Unable to duplicate the pipe handle " + 1258848b8605Smrg StreamableToString(write_handle_as_size_t) + 1259848b8605Smrg " from the parent process " + 1260848b8605Smrg StreamableToString(parent_process_id)); 1261848b8605Smrg } 1262848b8605Smrg 1263848b8605Smrg const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t); 1264848b8605Smrg HANDLE dup_event_handle; 1265848b8605Smrg 1266848b8605Smrg if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, 1267848b8605Smrg ::GetCurrentProcess(), &dup_event_handle, 1268848b8605Smrg 0x0, 1269848b8605Smrg FALSE, 1270848b8605Smrg DUPLICATE_SAME_ACCESS)) { 1271848b8605Smrg DeathTestAbort("Unable to duplicate the event handle " + 1272848b8605Smrg StreamableToString(event_handle_as_size_t) + 1273848b8605Smrg " from the parent process " + 1274848b8605Smrg StreamableToString(parent_process_id)); 1275848b8605Smrg } 1276848b8605Smrg 1277848b8605Smrg const int write_fd = 1278848b8605Smrg ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND); 1279848b8605Smrg if (write_fd == -1) { 1280848b8605Smrg DeathTestAbort("Unable to convert pipe handle " + 1281848b8605Smrg StreamableToString(write_handle_as_size_t) + 1282848b8605Smrg " to a file descriptor"); 1283848b8605Smrg } 1284848b8605Smrg 1285848b8605Smrg // Signals the parent that the write end of the pipe has been acquired 1286848b8605Smrg // so the parent can release its own write end. 1287848b8605Smrg ::SetEvent(dup_event_handle); 1288848b8605Smrg 1289848b8605Smrg return write_fd; 1290848b8605Smrg} 1291848b8605Smrg# endif // GTEST_OS_WINDOWS 1292848b8605Smrg 1293848b8605Smrg// Returns a newly created InternalRunDeathTestFlag object with fields 1294848b8605Smrg// initialized from the GTEST_FLAG(internal_run_death_test) flag if 1295848b8605Smrg// the flag is specified; otherwise returns NULL. 1296848b8605SmrgInternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { 1297848b8605Smrg if (GTEST_FLAG(internal_run_death_test) == "") return NULL; 1298848b8605Smrg 1299848b8605Smrg // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we 1300848b8605Smrg // can use it here. 1301848b8605Smrg int line = -1; 1302848b8605Smrg int index = -1; 1303848b8605Smrg ::std::vector< ::std::string> fields; 1304848b8605Smrg SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields); 1305848b8605Smrg int write_fd = -1; 1306848b8605Smrg 1307848b8605Smrg# if GTEST_OS_WINDOWS 1308848b8605Smrg 1309848b8605Smrg unsigned int parent_process_id = 0; 1310848b8605Smrg size_t write_handle_as_size_t = 0; 1311848b8605Smrg size_t event_handle_as_size_t = 0; 1312848b8605Smrg 1313848b8605Smrg if (fields.size() != 6 1314848b8605Smrg || !ParseNaturalNumber(fields[1], &line) 1315848b8605Smrg || !ParseNaturalNumber(fields[2], &index) 1316848b8605Smrg || !ParseNaturalNumber(fields[3], &parent_process_id) 1317848b8605Smrg || !ParseNaturalNumber(fields[4], &write_handle_as_size_t) 1318848b8605Smrg || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) { 1319848b8605Smrg DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + 1320848b8605Smrg GTEST_FLAG(internal_run_death_test)); 1321848b8605Smrg } 1322848b8605Smrg write_fd = GetStatusFileDescriptor(parent_process_id, 1323848b8605Smrg write_handle_as_size_t, 1324848b8605Smrg event_handle_as_size_t); 1325848b8605Smrg# else 1326848b8605Smrg 1327848b8605Smrg if (fields.size() != 4 1328848b8605Smrg || !ParseNaturalNumber(fields[1], &line) 1329848b8605Smrg || !ParseNaturalNumber(fields[2], &index) 1330848b8605Smrg || !ParseNaturalNumber(fields[3], &write_fd)) { 1331848b8605Smrg DeathTestAbort("Bad --gtest_internal_run_death_test flag: " 1332848b8605Smrg + GTEST_FLAG(internal_run_death_test)); 1333848b8605Smrg } 1334848b8605Smrg 1335848b8605Smrg# endif // GTEST_OS_WINDOWS 1336848b8605Smrg 1337848b8605Smrg return new InternalRunDeathTestFlag(fields[0], line, index, write_fd); 1338848b8605Smrg} 1339848b8605Smrg 1340848b8605Smrg} // namespace internal 1341848b8605Smrg 1342848b8605Smrg#endif // GTEST_HAS_DEATH_TEST 1343848b8605Smrg 1344848b8605Smrg} // namespace testing 1345