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