1af69d88dSmrg// Copyright 2005, Google Inc. 2af69d88dSmrg// All rights reserved. 3af69d88dSmrg// 4af69d88dSmrg// Redistribution and use in source and binary forms, with or without 5af69d88dSmrg// modification, are permitted provided that the following conditions are 6af69d88dSmrg// met: 7af69d88dSmrg// 8af69d88dSmrg// * Redistributions of source code must retain the above copyright 9af69d88dSmrg// notice, this list of conditions and the following disclaimer. 10af69d88dSmrg// * Redistributions in binary form must reproduce the above 11af69d88dSmrg// copyright notice, this list of conditions and the following disclaimer 12af69d88dSmrg// in the documentation and/or other materials provided with the 13af69d88dSmrg// distribution. 14af69d88dSmrg// * Neither the name of Google Inc. nor the names of its 15af69d88dSmrg// contributors may be used to endorse or promote products derived from 16af69d88dSmrg// this software without specific prior written permission. 17af69d88dSmrg// 18af69d88dSmrg// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19af69d88dSmrg// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20af69d88dSmrg// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21af69d88dSmrg// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22af69d88dSmrg// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23af69d88dSmrg// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24af69d88dSmrg// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25af69d88dSmrg// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26af69d88dSmrg// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27af69d88dSmrg// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28af69d88dSmrg// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 297ec681f3Smrg 30af69d88dSmrg// 31af69d88dSmrg// This file implements death tests. 32af69d88dSmrg 33af69d88dSmrg#include "gtest/gtest-death-test.h" 347ec681f3Smrg 357ec681f3Smrg#include <utility> 367ec681f3Smrg 37af69d88dSmrg#include "gtest/internal/gtest-port.h" 3801e04c3fSmrg#include "gtest/internal/custom/gtest.h" 39af69d88dSmrg 40af69d88dSmrg#if GTEST_HAS_DEATH_TEST 41af69d88dSmrg 42af69d88dSmrg# if GTEST_OS_MAC 43af69d88dSmrg# include <crt_externs.h> 44af69d88dSmrg# endif // GTEST_OS_MAC 45af69d88dSmrg 46af69d88dSmrg# include <errno.h> 47af69d88dSmrg# include <fcntl.h> 48af69d88dSmrg# include <limits.h> 49af69d88dSmrg 50af69d88dSmrg# if GTEST_OS_LINUX 51af69d88dSmrg# include <signal.h> 52af69d88dSmrg# endif // GTEST_OS_LINUX 53af69d88dSmrg 54af69d88dSmrg# include <stdarg.h> 55af69d88dSmrg 56af69d88dSmrg# if GTEST_OS_WINDOWS 57af69d88dSmrg# include <windows.h> 58af69d88dSmrg# else 59af69d88dSmrg# include <sys/mman.h> 60af69d88dSmrg# include <sys/wait.h> 61af69d88dSmrg# endif // GTEST_OS_WINDOWS 62af69d88dSmrg 63af69d88dSmrg# if GTEST_OS_QNX 64af69d88dSmrg# include <spawn.h> 65af69d88dSmrg# endif // GTEST_OS_QNX 66af69d88dSmrg 677ec681f3Smrg# if GTEST_OS_FUCHSIA 687ec681f3Smrg# include <lib/fdio/fd.h> 697ec681f3Smrg# include <lib/fdio/io.h> 707ec681f3Smrg# include <lib/fdio/spawn.h> 717ec681f3Smrg# include <lib/zx/channel.h> 727ec681f3Smrg# include <lib/zx/port.h> 737ec681f3Smrg# include <lib/zx/process.h> 747ec681f3Smrg# include <lib/zx/socket.h> 757ec681f3Smrg# include <zircon/processargs.h> 767ec681f3Smrg# include <zircon/syscalls.h> 777ec681f3Smrg# include <zircon/syscalls/policy.h> 787ec681f3Smrg# include <zircon/syscalls/port.h> 797ec681f3Smrg# endif // GTEST_OS_FUCHSIA 807ec681f3Smrg 81af69d88dSmrg#endif // GTEST_HAS_DEATH_TEST 82af69d88dSmrg 83af69d88dSmrg#include "gtest/gtest-message.h" 84af69d88dSmrg#include "gtest/internal/gtest-string.h" 85af69d88dSmrg#include "src/gtest-internal-inl.h" 86af69d88dSmrg 87af69d88dSmrgnamespace testing { 88af69d88dSmrg 89af69d88dSmrg// Constants. 90af69d88dSmrg 91af69d88dSmrg// The default death test style. 927ec681f3Smrg// 937ec681f3Smrg// This is defined in internal/gtest-port.h as "fast", but can be overridden by 947ec681f3Smrg// a definition in internal/custom/gtest-port.h. The recommended value, which is 957ec681f3Smrg// used internally at Google, is "threadsafe". 967ec681f3Smrgstatic const char kDefaultDeathTestStyle[] = GTEST_DEFAULT_DEATH_TEST_STYLE; 97af69d88dSmrg 98af69d88dSmrgGTEST_DEFINE_string_( 99af69d88dSmrg death_test_style, 100af69d88dSmrg internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle), 101af69d88dSmrg "Indicates how to run a death test in a forked child process: " 102af69d88dSmrg "\"threadsafe\" (child process re-executes the test binary " 103af69d88dSmrg "from the beginning, running only the specific death test) or " 104af69d88dSmrg "\"fast\" (child process runs the death test immediately " 105af69d88dSmrg "after forking)."); 106af69d88dSmrg 107af69d88dSmrgGTEST_DEFINE_bool_( 108af69d88dSmrg death_test_use_fork, 109af69d88dSmrg internal::BoolFromGTestEnv("death_test_use_fork", false), 110af69d88dSmrg "Instructs to use fork()/_exit() instead of clone() in death tests. " 111af69d88dSmrg "Ignored and always uses fork() on POSIX systems where clone() is not " 112af69d88dSmrg "implemented. Useful when running under valgrind or similar tools if " 113af69d88dSmrg "those do not support clone(). Valgrind 3.3.1 will just fail if " 114af69d88dSmrg "it sees an unsupported combination of clone() flags. " 115af69d88dSmrg "It is not recommended to use this flag w/o valgrind though it will " 116af69d88dSmrg "work in 99% of the cases. Once valgrind is fixed, this flag will " 117af69d88dSmrg "most likely be removed."); 118af69d88dSmrg 119af69d88dSmrgnamespace internal { 120af69d88dSmrgGTEST_DEFINE_string_( 121af69d88dSmrg internal_run_death_test, "", 122af69d88dSmrg "Indicates the file, line number, temporal index of " 123af69d88dSmrg "the single death test to run, and a file descriptor to " 124af69d88dSmrg "which a success code may be sent, all separated by " 1257ec681f3Smrg "the '|' characters. This flag is specified if and only if the " 1267ec681f3Smrg "current process is a sub-process launched for running a thread-safe " 127af69d88dSmrg "death test. FOR INTERNAL USE ONLY."); 128af69d88dSmrg} // namespace internal 129af69d88dSmrg 130af69d88dSmrg#if GTEST_HAS_DEATH_TEST 131af69d88dSmrg 132af69d88dSmrgnamespace internal { 133af69d88dSmrg 134af69d88dSmrg// Valid only for fast death tests. Indicates the code is running in the 135af69d88dSmrg// child process of a fast style death test. 1367ec681f3Smrg# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA 137af69d88dSmrgstatic bool g_in_fast_death_test_child = false; 13801e04c3fSmrg# endif 139af69d88dSmrg 140af69d88dSmrg// Returns a Boolean value indicating whether the caller is currently 141af69d88dSmrg// executing in the context of the death test child process. Tools such as 142af69d88dSmrg// Valgrind heap checkers may need this to modify their behavior in death 143af69d88dSmrg// tests. IMPORTANT: This is an internal utility. Using it may break the 144af69d88dSmrg// implementation of death tests. User code MUST NOT use it. 145af69d88dSmrgbool InDeathTestChild() { 1467ec681f3Smrg# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA 147af69d88dSmrg 1487ec681f3Smrg // On Windows and Fuchsia, death tests are thread-safe regardless of the value 1497ec681f3Smrg // of the death_test_style flag. 150af69d88dSmrg return !GTEST_FLAG(internal_run_death_test).empty(); 151af69d88dSmrg 152af69d88dSmrg# else 153af69d88dSmrg 154af69d88dSmrg if (GTEST_FLAG(death_test_style) == "threadsafe") 155af69d88dSmrg return !GTEST_FLAG(internal_run_death_test).empty(); 156af69d88dSmrg else 157af69d88dSmrg return g_in_fast_death_test_child; 158af69d88dSmrg#endif 159af69d88dSmrg} 160af69d88dSmrg 161af69d88dSmrg} // namespace internal 162af69d88dSmrg 163af69d88dSmrg// ExitedWithCode constructor. 164af69d88dSmrgExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) { 165af69d88dSmrg} 166af69d88dSmrg 167af69d88dSmrg// ExitedWithCode function-call operator. 168af69d88dSmrgbool ExitedWithCode::operator()(int exit_status) const { 1697ec681f3Smrg# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA 170af69d88dSmrg 171af69d88dSmrg return exit_status == exit_code_; 172af69d88dSmrg 173af69d88dSmrg# else 174af69d88dSmrg 175af69d88dSmrg return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_; 176af69d88dSmrg 1777ec681f3Smrg# endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA 178af69d88dSmrg} 179af69d88dSmrg 1807ec681f3Smrg# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA 181af69d88dSmrg// KilledBySignal constructor. 182af69d88dSmrgKilledBySignal::KilledBySignal(int signum) : signum_(signum) { 183af69d88dSmrg} 184af69d88dSmrg 185af69d88dSmrg// KilledBySignal function-call operator. 186af69d88dSmrgbool KilledBySignal::operator()(int exit_status) const { 18701e04c3fSmrg# if defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) 18801e04c3fSmrg { 18901e04c3fSmrg bool result; 19001e04c3fSmrg if (GTEST_KILLED_BY_SIGNAL_OVERRIDE_(signum_, exit_status, &result)) { 19101e04c3fSmrg return result; 19201e04c3fSmrg } 19301e04c3fSmrg } 19401e04c3fSmrg# endif // defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) 195af69d88dSmrg return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_; 196af69d88dSmrg} 1977ec681f3Smrg# endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA 198af69d88dSmrg 199af69d88dSmrgnamespace internal { 200af69d88dSmrg 201af69d88dSmrg// Utilities needed for death tests. 202af69d88dSmrg 203af69d88dSmrg// Generates a textual description of a given exit code, in the format 204af69d88dSmrg// specified by wait(2). 205af69d88dSmrgstatic std::string ExitSummary(int exit_code) { 206af69d88dSmrg Message m; 207af69d88dSmrg 2087ec681f3Smrg# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA 209af69d88dSmrg 210af69d88dSmrg m << "Exited with exit status " << exit_code; 211af69d88dSmrg 212af69d88dSmrg# else 213af69d88dSmrg 214af69d88dSmrg if (WIFEXITED(exit_code)) { 215af69d88dSmrg m << "Exited with exit status " << WEXITSTATUS(exit_code); 216af69d88dSmrg } else if (WIFSIGNALED(exit_code)) { 217af69d88dSmrg m << "Terminated by signal " << WTERMSIG(exit_code); 218af69d88dSmrg } 219af69d88dSmrg# ifdef WCOREDUMP 220af69d88dSmrg if (WCOREDUMP(exit_code)) { 221af69d88dSmrg m << " (core dumped)"; 222af69d88dSmrg } 223af69d88dSmrg# endif 2247ec681f3Smrg# endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA 225af69d88dSmrg 226af69d88dSmrg return m.GetString(); 227af69d88dSmrg} 228af69d88dSmrg 229af69d88dSmrg// Returns true if exit_status describes a process that was terminated 230af69d88dSmrg// by a signal, or exited normally with a nonzero exit code. 231af69d88dSmrgbool ExitedUnsuccessfully(int exit_status) { 232af69d88dSmrg return !ExitedWithCode(0)(exit_status); 233af69d88dSmrg} 234af69d88dSmrg 2357ec681f3Smrg# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA 236af69d88dSmrg// Generates a textual failure message when a death test finds more than 237af69d88dSmrg// one thread running, or cannot determine the number of threads, prior 238af69d88dSmrg// to executing the given statement. It is the responsibility of the 239af69d88dSmrg// caller not to pass a thread_count of 1. 240af69d88dSmrgstatic std::string DeathTestThreadWarning(size_t thread_count) { 241af69d88dSmrg Message msg; 242af69d88dSmrg msg << "Death tests use fork(), which is unsafe particularly" 243af69d88dSmrg << " in a threaded context. For this test, " << GTEST_NAME_ << " "; 2447ec681f3Smrg if (thread_count == 0) { 245af69d88dSmrg msg << "couldn't detect the number of threads."; 2467ec681f3Smrg } else { 247af69d88dSmrg msg << "detected " << thread_count << " threads."; 2487ec681f3Smrg } 2497ec681f3Smrg msg << " See " 2507ec681f3Smrg "https://github.com/google/googletest/blob/master/googletest/docs/" 2517ec681f3Smrg "advanced.md#death-tests-and-threads" 2527ec681f3Smrg << " for more explanation and suggested solutions, especially if" 2537ec681f3Smrg << " this is the last message you see before your test times out."; 254af69d88dSmrg return msg.GetString(); 255af69d88dSmrg} 2567ec681f3Smrg# endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA 257af69d88dSmrg 258af69d88dSmrg// Flag characters for reporting a death test that did not die. 259af69d88dSmrgstatic const char kDeathTestLived = 'L'; 260af69d88dSmrgstatic const char kDeathTestReturned = 'R'; 261af69d88dSmrgstatic const char kDeathTestThrew = 'T'; 262af69d88dSmrgstatic const char kDeathTestInternalError = 'I'; 263af69d88dSmrg 2647ec681f3Smrg#if GTEST_OS_FUCHSIA 2657ec681f3Smrg 2667ec681f3Smrg// File descriptor used for the pipe in the child process. 2677ec681f3Smrgstatic const int kFuchsiaReadPipeFd = 3; 2687ec681f3Smrg 2697ec681f3Smrg#endif 2707ec681f3Smrg 271af69d88dSmrg// An enumeration describing all of the possible ways that a death test can 272af69d88dSmrg// conclude. DIED means that the process died while executing the test 273af69d88dSmrg// code; LIVED means that process lived beyond the end of the test code; 274af69d88dSmrg// RETURNED means that the test statement attempted to execute a return 275af69d88dSmrg// statement, which is not allowed; THREW means that the test statement 276af69d88dSmrg// returned control by throwing an exception. IN_PROGRESS means the test 277af69d88dSmrg// has not yet concluded. 278af69d88dSmrgenum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW }; 279af69d88dSmrg 280af69d88dSmrg// Routine for aborting the program which is safe to call from an 281af69d88dSmrg// exec-style death test child process, in which case the error 282af69d88dSmrg// message is propagated back to the parent process. Otherwise, the 283af69d88dSmrg// message is simply printed to stderr. In either case, the program 284af69d88dSmrg// then exits with status 1. 2857ec681f3Smrgstatic void DeathTestAbort(const std::string& message) { 286af69d88dSmrg // On a POSIX system, this function may be called from a threadsafe-style 287af69d88dSmrg // death test child process, which operates on a very small stack. Use 288af69d88dSmrg // the heap for any additional non-minuscule memory requirements. 289af69d88dSmrg const InternalRunDeathTestFlag* const flag = 290af69d88dSmrg GetUnitTestImpl()->internal_run_death_test_flag(); 2917ec681f3Smrg if (flag != nullptr) { 292af69d88dSmrg FILE* parent = posix::FDOpen(flag->write_fd(), "w"); 293af69d88dSmrg fputc(kDeathTestInternalError, parent); 294af69d88dSmrg fprintf(parent, "%s", message.c_str()); 295af69d88dSmrg fflush(parent); 296af69d88dSmrg _exit(1); 297af69d88dSmrg } else { 298af69d88dSmrg fprintf(stderr, "%s", message.c_str()); 299af69d88dSmrg fflush(stderr); 300af69d88dSmrg posix::Abort(); 301af69d88dSmrg } 302af69d88dSmrg} 303af69d88dSmrg 304af69d88dSmrg// A replacement for CHECK that calls DeathTestAbort if the assertion 305af69d88dSmrg// fails. 306af69d88dSmrg# define GTEST_DEATH_TEST_CHECK_(expression) \ 307af69d88dSmrg do { \ 308af69d88dSmrg if (!::testing::internal::IsTrue(expression)) { \ 309af69d88dSmrg DeathTestAbort( \ 310af69d88dSmrg ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ 311af69d88dSmrg + ::testing::internal::StreamableToString(__LINE__) + ": " \ 312af69d88dSmrg + #expression); \ 313af69d88dSmrg } \ 314af69d88dSmrg } while (::testing::internal::AlwaysFalse()) 315af69d88dSmrg 316af69d88dSmrg// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for 317af69d88dSmrg// evaluating any system call that fulfills two conditions: it must return 318af69d88dSmrg// -1 on failure, and set errno to EINTR when it is interrupted and 319af69d88dSmrg// should be tried again. The macro expands to a loop that repeatedly 320af69d88dSmrg// evaluates the expression as long as it evaluates to -1 and sets 321af69d88dSmrg// errno to EINTR. If the expression evaluates to -1 but errno is 322af69d88dSmrg// something other than EINTR, DeathTestAbort is called. 323af69d88dSmrg# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \ 324af69d88dSmrg do { \ 325af69d88dSmrg int gtest_retval; \ 326af69d88dSmrg do { \ 327af69d88dSmrg gtest_retval = (expression); \ 328af69d88dSmrg } while (gtest_retval == -1 && errno == EINTR); \ 329af69d88dSmrg if (gtest_retval == -1) { \ 330af69d88dSmrg DeathTestAbort( \ 331af69d88dSmrg ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ 332af69d88dSmrg + ::testing::internal::StreamableToString(__LINE__) + ": " \ 333af69d88dSmrg + #expression + " != -1"); \ 334af69d88dSmrg } \ 335af69d88dSmrg } while (::testing::internal::AlwaysFalse()) 336af69d88dSmrg 337af69d88dSmrg// Returns the message describing the last system error in errno. 338af69d88dSmrgstd::string GetLastErrnoDescription() { 339af69d88dSmrg return errno == 0 ? "" : posix::StrError(errno); 340af69d88dSmrg} 341af69d88dSmrg 342af69d88dSmrg// This is called from a death test parent process to read a failure 343af69d88dSmrg// message from the death test child process and log it with the FATAL 344af69d88dSmrg// severity. On Windows, the message is read from a pipe handle. On other 345af69d88dSmrg// platforms, it is read from a file descriptor. 346af69d88dSmrgstatic void FailFromInternalError(int fd) { 347af69d88dSmrg Message error; 348af69d88dSmrg char buffer[256]; 349af69d88dSmrg int num_read; 350af69d88dSmrg 351af69d88dSmrg do { 352af69d88dSmrg while ((num_read = posix::Read(fd, buffer, 255)) > 0) { 353af69d88dSmrg buffer[num_read] = '\0'; 354af69d88dSmrg error << buffer; 355af69d88dSmrg } 356af69d88dSmrg } while (num_read == -1 && errno == EINTR); 357af69d88dSmrg 358af69d88dSmrg if (num_read == 0) { 359af69d88dSmrg GTEST_LOG_(FATAL) << error.GetString(); 360af69d88dSmrg } else { 361af69d88dSmrg const int last_error = errno; 362af69d88dSmrg GTEST_LOG_(FATAL) << "Error while reading death test internal: " 363af69d88dSmrg << GetLastErrnoDescription() << " [" << last_error << "]"; 364af69d88dSmrg } 365af69d88dSmrg} 366af69d88dSmrg 367af69d88dSmrg// Death test constructor. Increments the running death test count 368af69d88dSmrg// for the current test. 369af69d88dSmrgDeathTest::DeathTest() { 370af69d88dSmrg TestInfo* const info = GetUnitTestImpl()->current_test_info(); 3717ec681f3Smrg if (info == nullptr) { 372af69d88dSmrg DeathTestAbort("Cannot run a death test outside of a TEST or " 373af69d88dSmrg "TEST_F construct"); 374af69d88dSmrg } 375af69d88dSmrg} 376af69d88dSmrg 377af69d88dSmrg// Creates and returns a death test by dispatching to the current 378af69d88dSmrg// death test factory. 3797ec681f3Smrgbool DeathTest::Create(const char* statement, 3807ec681f3Smrg Matcher<const std::string&> matcher, const char* file, 3817ec681f3Smrg int line, DeathTest** test) { 382af69d88dSmrg return GetUnitTestImpl()->death_test_factory()->Create( 3837ec681f3Smrg statement, std::move(matcher), file, line, test); 384af69d88dSmrg} 385af69d88dSmrg 386af69d88dSmrgconst char* DeathTest::LastMessage() { 387af69d88dSmrg return last_death_test_message_.c_str(); 388af69d88dSmrg} 389af69d88dSmrg 390af69d88dSmrgvoid DeathTest::set_last_death_test_message(const std::string& message) { 391af69d88dSmrg last_death_test_message_ = message; 392af69d88dSmrg} 393af69d88dSmrg 394af69d88dSmrgstd::string DeathTest::last_death_test_message_; 395af69d88dSmrg 396af69d88dSmrg// Provides cross platform implementation for some death functionality. 397af69d88dSmrgclass DeathTestImpl : public DeathTest { 398af69d88dSmrg protected: 3997ec681f3Smrg DeathTestImpl(const char* a_statement, Matcher<const std::string&> matcher) 400af69d88dSmrg : statement_(a_statement), 4017ec681f3Smrg matcher_(std::move(matcher)), 402af69d88dSmrg spawned_(false), 403af69d88dSmrg status_(-1), 404af69d88dSmrg outcome_(IN_PROGRESS), 405af69d88dSmrg read_fd_(-1), 406af69d88dSmrg write_fd_(-1) {} 407af69d88dSmrg 408af69d88dSmrg // read_fd_ is expected to be closed and cleared by a derived class. 4097ec681f3Smrg ~DeathTestImpl() override { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); } 410af69d88dSmrg 4117ec681f3Smrg void Abort(AbortReason reason) override; 4127ec681f3Smrg bool Passed(bool status_ok) override; 413af69d88dSmrg 414af69d88dSmrg const char* statement() const { return statement_; } 415af69d88dSmrg bool spawned() const { return spawned_; } 416af69d88dSmrg void set_spawned(bool is_spawned) { spawned_ = is_spawned; } 417af69d88dSmrg int status() const { return status_; } 418af69d88dSmrg void set_status(int a_status) { status_ = a_status; } 419af69d88dSmrg DeathTestOutcome outcome() const { return outcome_; } 420af69d88dSmrg void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; } 421af69d88dSmrg int read_fd() const { return read_fd_; } 422af69d88dSmrg void set_read_fd(int fd) { read_fd_ = fd; } 423af69d88dSmrg int write_fd() const { return write_fd_; } 424af69d88dSmrg void set_write_fd(int fd) { write_fd_ = fd; } 425af69d88dSmrg 426af69d88dSmrg // Called in the parent process only. Reads the result code of the death 427af69d88dSmrg // test child process via a pipe, interprets it to set the outcome_ 428af69d88dSmrg // member, and closes read_fd_. Outputs diagnostics and terminates in 429af69d88dSmrg // case of unexpected codes. 430af69d88dSmrg void ReadAndInterpretStatusByte(); 431af69d88dSmrg 4327ec681f3Smrg // Returns stderr output from the child process. 4337ec681f3Smrg virtual std::string GetErrorLogs(); 4347ec681f3Smrg 435af69d88dSmrg private: 436af69d88dSmrg // The textual content of the code this object is testing. This class 437af69d88dSmrg // doesn't own this string and should not attempt to delete it. 438af69d88dSmrg const char* const statement_; 4397ec681f3Smrg // A matcher that's expected to match the stderr output by the child process. 4407ec681f3Smrg Matcher<const std::string&> matcher_; 441af69d88dSmrg // True if the death test child process has been successfully spawned. 442af69d88dSmrg bool spawned_; 443af69d88dSmrg // The exit status of the child process. 444af69d88dSmrg int status_; 445af69d88dSmrg // How the death test concluded. 446af69d88dSmrg DeathTestOutcome outcome_; 447af69d88dSmrg // Descriptor to the read end of the pipe to the child process. It is 448af69d88dSmrg // always -1 in the child process. The child keeps its write end of the 449af69d88dSmrg // pipe in write_fd_. 450af69d88dSmrg int read_fd_; 451af69d88dSmrg // Descriptor to the child's write end of the pipe to the parent process. 452af69d88dSmrg // It is always -1 in the parent process. The parent keeps its end of the 453af69d88dSmrg // pipe in read_fd_. 454af69d88dSmrg int write_fd_; 455af69d88dSmrg}; 456af69d88dSmrg 457af69d88dSmrg// Called in the parent process only. Reads the result code of the death 458af69d88dSmrg// test child process via a pipe, interprets it to set the outcome_ 459af69d88dSmrg// member, and closes read_fd_. Outputs diagnostics and terminates in 460af69d88dSmrg// case of unexpected codes. 461af69d88dSmrgvoid DeathTestImpl::ReadAndInterpretStatusByte() { 462af69d88dSmrg char flag; 463af69d88dSmrg int bytes_read; 464af69d88dSmrg 465af69d88dSmrg // The read() here blocks until data is available (signifying the 466af69d88dSmrg // failure of the death test) or until the pipe is closed (signifying 467af69d88dSmrg // its success), so it's okay to call this in the parent before 468af69d88dSmrg // the child process has exited. 469af69d88dSmrg do { 470af69d88dSmrg bytes_read = posix::Read(read_fd(), &flag, 1); 471af69d88dSmrg } while (bytes_read == -1 && errno == EINTR); 472af69d88dSmrg 473af69d88dSmrg if (bytes_read == 0) { 474af69d88dSmrg set_outcome(DIED); 475af69d88dSmrg } else if (bytes_read == 1) { 476af69d88dSmrg switch (flag) { 477af69d88dSmrg case kDeathTestReturned: 478af69d88dSmrg set_outcome(RETURNED); 479af69d88dSmrg break; 480af69d88dSmrg case kDeathTestThrew: 481af69d88dSmrg set_outcome(THREW); 482af69d88dSmrg break; 483af69d88dSmrg case kDeathTestLived: 484af69d88dSmrg set_outcome(LIVED); 485af69d88dSmrg break; 486af69d88dSmrg case kDeathTestInternalError: 487af69d88dSmrg FailFromInternalError(read_fd()); // Does not return. 488af69d88dSmrg break; 489af69d88dSmrg default: 490af69d88dSmrg GTEST_LOG_(FATAL) << "Death test child process reported " 491af69d88dSmrg << "unexpected status byte (" 492af69d88dSmrg << static_cast<unsigned int>(flag) << ")"; 493af69d88dSmrg } 494af69d88dSmrg } else { 495af69d88dSmrg GTEST_LOG_(FATAL) << "Read from death test child process failed: " 496af69d88dSmrg << GetLastErrnoDescription(); 497af69d88dSmrg } 498af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); 499af69d88dSmrg set_read_fd(-1); 500af69d88dSmrg} 501af69d88dSmrg 5027ec681f3Smrgstd::string DeathTestImpl::GetErrorLogs() { 5037ec681f3Smrg return GetCapturedStderr(); 5047ec681f3Smrg} 5057ec681f3Smrg 506af69d88dSmrg// Signals that the death test code which should have exited, didn't. 507af69d88dSmrg// Should be called only in a death test child process. 508af69d88dSmrg// Writes a status byte to the child's status file descriptor, then 509af69d88dSmrg// calls _exit(1). 510af69d88dSmrgvoid DeathTestImpl::Abort(AbortReason reason) { 511af69d88dSmrg // The parent process considers the death test to be a failure if 512af69d88dSmrg // it finds any data in our pipe. So, here we write a single flag byte 513af69d88dSmrg // to the pipe, then exit. 514af69d88dSmrg const char status_ch = 515af69d88dSmrg reason == TEST_DID_NOT_DIE ? kDeathTestLived : 516af69d88dSmrg reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned; 517af69d88dSmrg 518af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); 519af69d88dSmrg // We are leaking the descriptor here because on some platforms (i.e., 520af69d88dSmrg // when built as Windows DLL), destructors of global objects will still 521af69d88dSmrg // run after calling _exit(). On such systems, write_fd_ will be 522af69d88dSmrg // indirectly closed from the destructor of UnitTestImpl, causing double 523af69d88dSmrg // close if it is also closed here. On debug configurations, double close 524af69d88dSmrg // may assert. As there are no in-process buffers to flush here, we are 525af69d88dSmrg // relying on the OS to close the descriptor after the process terminates 526af69d88dSmrg // when the destructors are not run. 527af69d88dSmrg _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) 528af69d88dSmrg} 529af69d88dSmrg 530af69d88dSmrg// Returns an indented copy of stderr output for a death test. 531af69d88dSmrg// This makes distinguishing death test output lines from regular log lines 532af69d88dSmrg// much easier. 533af69d88dSmrgstatic ::std::string FormatDeathTestOutput(const ::std::string& output) { 534af69d88dSmrg ::std::string ret; 535af69d88dSmrg for (size_t at = 0; ; ) { 536af69d88dSmrg const size_t line_end = output.find('\n', at); 537af69d88dSmrg ret += "[ DEATH ] "; 538af69d88dSmrg if (line_end == ::std::string::npos) { 539af69d88dSmrg ret += output.substr(at); 540af69d88dSmrg break; 541af69d88dSmrg } 542af69d88dSmrg ret += output.substr(at, line_end + 1 - at); 543af69d88dSmrg at = line_end + 1; 544af69d88dSmrg } 545af69d88dSmrg return ret; 546af69d88dSmrg} 547af69d88dSmrg 548af69d88dSmrg// Assesses the success or failure of a death test, using both private 549af69d88dSmrg// members which have previously been set, and one argument: 550af69d88dSmrg// 551af69d88dSmrg// Private data members: 552af69d88dSmrg// outcome: An enumeration describing how the death test 553af69d88dSmrg// concluded: DIED, LIVED, THREW, or RETURNED. The death test 554af69d88dSmrg// fails in the latter three cases. 555af69d88dSmrg// status: The exit status of the child process. On *nix, it is in the 556af69d88dSmrg// in the format specified by wait(2). On Windows, this is the 557af69d88dSmrg// value supplied to the ExitProcess() API or a numeric code 558af69d88dSmrg// of the exception that terminated the program. 5597ec681f3Smrg// matcher_: A matcher that's expected to match the stderr output by the child 5607ec681f3Smrg// process. 561af69d88dSmrg// 562af69d88dSmrg// Argument: 563af69d88dSmrg// status_ok: true if exit_status is acceptable in the context of 564af69d88dSmrg// this particular death test, which fails if it is false 565af69d88dSmrg// 5667ec681f3Smrg// Returns true if and only if all of the above conditions are met. Otherwise, 5677ec681f3Smrg// the first failing condition, in the order given above, is the one that is 568af69d88dSmrg// reported. Also sets the last death test message string. 569af69d88dSmrgbool DeathTestImpl::Passed(bool status_ok) { 570af69d88dSmrg if (!spawned()) 571af69d88dSmrg return false; 572af69d88dSmrg 5737ec681f3Smrg const std::string error_message = GetErrorLogs(); 574af69d88dSmrg 575af69d88dSmrg bool success = false; 576af69d88dSmrg Message buffer; 577af69d88dSmrg 578af69d88dSmrg buffer << "Death test: " << statement() << "\n"; 579af69d88dSmrg switch (outcome()) { 580af69d88dSmrg case LIVED: 581af69d88dSmrg buffer << " Result: failed to die.\n" 582af69d88dSmrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 583af69d88dSmrg break; 584af69d88dSmrg case THREW: 585af69d88dSmrg buffer << " Result: threw an exception.\n" 586af69d88dSmrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 587af69d88dSmrg break; 588af69d88dSmrg case RETURNED: 589af69d88dSmrg buffer << " Result: illegal return in test statement.\n" 590af69d88dSmrg << " Error msg:\n" << FormatDeathTestOutput(error_message); 591af69d88dSmrg break; 592af69d88dSmrg case DIED: 593af69d88dSmrg if (status_ok) { 5947ec681f3Smrg if (matcher_.Matches(error_message)) { 595af69d88dSmrg success = true; 596af69d88dSmrg } else { 5977ec681f3Smrg std::ostringstream stream; 5987ec681f3Smrg matcher_.DescribeTo(&stream); 599af69d88dSmrg buffer << " Result: died but not with expected error.\n" 6007ec681f3Smrg << " Expected: " << stream.str() << "\n" 6017ec681f3Smrg << "Actual msg:\n" 6027ec681f3Smrg << FormatDeathTestOutput(error_message); 603af69d88dSmrg } 604af69d88dSmrg } else { 605af69d88dSmrg buffer << " Result: died but not with expected exit code:\n" 606af69d88dSmrg << " " << ExitSummary(status()) << "\n" 607af69d88dSmrg << "Actual msg:\n" << FormatDeathTestOutput(error_message); 608af69d88dSmrg } 609af69d88dSmrg break; 610af69d88dSmrg case IN_PROGRESS: 611af69d88dSmrg default: 612af69d88dSmrg GTEST_LOG_(FATAL) 613af69d88dSmrg << "DeathTest::Passed somehow called before conclusion of test"; 614af69d88dSmrg } 615af69d88dSmrg 616af69d88dSmrg DeathTest::set_last_death_test_message(buffer.GetString()); 617af69d88dSmrg return success; 618af69d88dSmrg} 619af69d88dSmrg 620af69d88dSmrg# if GTEST_OS_WINDOWS 621af69d88dSmrg// WindowsDeathTest implements death tests on Windows. Due to the 622af69d88dSmrg// specifics of starting new processes on Windows, death tests there are 623af69d88dSmrg// always threadsafe, and Google Test considers the 624af69d88dSmrg// --gtest_death_test_style=fast setting to be equivalent to 625af69d88dSmrg// --gtest_death_test_style=threadsafe there. 626af69d88dSmrg// 627af69d88dSmrg// A few implementation notes: Like the Linux version, the Windows 628af69d88dSmrg// implementation uses pipes for child-to-parent communication. But due to 629af69d88dSmrg// the specifics of pipes on Windows, some extra steps are required: 630af69d88dSmrg// 631af69d88dSmrg// 1. The parent creates a communication pipe and stores handles to both 632af69d88dSmrg// ends of it. 633af69d88dSmrg// 2. The parent starts the child and provides it with the information 634af69d88dSmrg// necessary to acquire the handle to the write end of the pipe. 635af69d88dSmrg// 3. The child acquires the write end of the pipe and signals the parent 636af69d88dSmrg// using a Windows event. 637af69d88dSmrg// 4. Now the parent can release the write end of the pipe on its side. If 638af69d88dSmrg// this is done before step 3, the object's reference count goes down to 639af69d88dSmrg// 0 and it is destroyed, preventing the child from acquiring it. The 640af69d88dSmrg// parent now has to release it, or read operations on the read end of 641af69d88dSmrg// the pipe will not return when the child terminates. 642af69d88dSmrg// 5. The parent reads child's output through the pipe (outcome code and 643af69d88dSmrg// any possible error messages) from the pipe, and its stderr and then 644af69d88dSmrg// determines whether to fail the test. 645af69d88dSmrg// 646af69d88dSmrg// Note: to distinguish Win32 API calls from the local method and function 647af69d88dSmrg// calls, the former are explicitly resolved in the global namespace. 648af69d88dSmrg// 649af69d88dSmrgclass WindowsDeathTest : public DeathTestImpl { 650af69d88dSmrg public: 6517ec681f3Smrg WindowsDeathTest(const char* a_statement, Matcher<const std::string&> matcher, 6527ec681f3Smrg const char* file, int line) 6537ec681f3Smrg : DeathTestImpl(a_statement, std::move(matcher)), 6547ec681f3Smrg file_(file), 6557ec681f3Smrg line_(line) {} 656af69d88dSmrg 657af69d88dSmrg // All of these virtual functions are inherited from DeathTest. 658af69d88dSmrg virtual int Wait(); 659af69d88dSmrg virtual TestRole AssumeRole(); 660af69d88dSmrg 661af69d88dSmrg private: 662af69d88dSmrg // The name of the file in which the death test is located. 663af69d88dSmrg const char* const file_; 664af69d88dSmrg // The line number on which the death test is located. 665af69d88dSmrg const int line_; 666af69d88dSmrg // Handle to the write end of the pipe to the child process. 667af69d88dSmrg AutoHandle write_handle_; 668af69d88dSmrg // Child process handle. 669af69d88dSmrg AutoHandle child_handle_; 670af69d88dSmrg // Event the child process uses to signal the parent that it has 671af69d88dSmrg // acquired the handle to the write end of the pipe. After seeing this 672af69d88dSmrg // event the parent can release its own handles to make sure its 673af69d88dSmrg // ReadFile() calls return when the child terminates. 674af69d88dSmrg AutoHandle event_handle_; 675af69d88dSmrg}; 676af69d88dSmrg 677af69d88dSmrg// Waits for the child in a death test to exit, returning its exit 678af69d88dSmrg// status, or 0 if no child process exists. As a side effect, sets the 679af69d88dSmrg// outcome data member. 680af69d88dSmrgint WindowsDeathTest::Wait() { 681af69d88dSmrg if (!spawned()) 682af69d88dSmrg return 0; 683af69d88dSmrg 684af69d88dSmrg // Wait until the child either signals that it has acquired the write end 685af69d88dSmrg // of the pipe or it dies. 686af69d88dSmrg const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() }; 687af69d88dSmrg switch (::WaitForMultipleObjects(2, 688af69d88dSmrg wait_handles, 689af69d88dSmrg FALSE, // Waits for any of the handles. 690af69d88dSmrg INFINITE)) { 691af69d88dSmrg case WAIT_OBJECT_0: 692af69d88dSmrg case WAIT_OBJECT_0 + 1: 693af69d88dSmrg break; 694af69d88dSmrg default: 695af69d88dSmrg GTEST_DEATH_TEST_CHECK_(false); // Should not get here. 696af69d88dSmrg } 697af69d88dSmrg 698af69d88dSmrg // The child has acquired the write end of the pipe or exited. 699af69d88dSmrg // We release the handle on our side and continue. 700af69d88dSmrg write_handle_.Reset(); 701af69d88dSmrg event_handle_.Reset(); 702af69d88dSmrg 703af69d88dSmrg ReadAndInterpretStatusByte(); 704af69d88dSmrg 705af69d88dSmrg // Waits for the child process to exit if it haven't already. This 706af69d88dSmrg // returns immediately if the child has already exited, regardless of 707af69d88dSmrg // whether previous calls to WaitForMultipleObjects synchronized on this 708af69d88dSmrg // handle or not. 709af69d88dSmrg GTEST_DEATH_TEST_CHECK_( 710af69d88dSmrg WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(), 711af69d88dSmrg INFINITE)); 712af69d88dSmrg DWORD status_code; 713af69d88dSmrg GTEST_DEATH_TEST_CHECK_( 714af69d88dSmrg ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE); 715af69d88dSmrg child_handle_.Reset(); 716af69d88dSmrg set_status(static_cast<int>(status_code)); 717af69d88dSmrg return status(); 718af69d88dSmrg} 719af69d88dSmrg 720af69d88dSmrg// The AssumeRole process for a Windows death test. It creates a child 721af69d88dSmrg// process with the same executable as the current process to run the 722af69d88dSmrg// death test. The child process is given the --gtest_filter and 723af69d88dSmrg// --gtest_internal_run_death_test flags such that it knows to run the 724af69d88dSmrg// current death test only. 725af69d88dSmrgDeathTest::TestRole WindowsDeathTest::AssumeRole() { 726af69d88dSmrg const UnitTestImpl* const impl = GetUnitTestImpl(); 727af69d88dSmrg const InternalRunDeathTestFlag* const flag = 728af69d88dSmrg impl->internal_run_death_test_flag(); 729af69d88dSmrg const TestInfo* const info = impl->current_test_info(); 730af69d88dSmrg const int death_test_index = info->result()->death_test_count(); 731af69d88dSmrg 7327ec681f3Smrg if (flag != nullptr) { 733af69d88dSmrg // ParseInternalRunDeathTestFlag() has performed all the necessary 734af69d88dSmrg // processing. 735af69d88dSmrg set_write_fd(flag->write_fd()); 736af69d88dSmrg return EXECUTE_TEST; 737af69d88dSmrg } 738af69d88dSmrg 739af69d88dSmrg // WindowsDeathTest uses an anonymous pipe to communicate results of 740af69d88dSmrg // a death test. 7417ec681f3Smrg SECURITY_ATTRIBUTES handles_are_inheritable = {sizeof(SECURITY_ATTRIBUTES), 7427ec681f3Smrg nullptr, TRUE}; 743af69d88dSmrg HANDLE read_handle, write_handle; 744af69d88dSmrg GTEST_DEATH_TEST_CHECK_( 745af69d88dSmrg ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable, 746af69d88dSmrg 0) // Default buffer size. 747af69d88dSmrg != FALSE); 748af69d88dSmrg set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle), 749af69d88dSmrg O_RDONLY)); 750af69d88dSmrg write_handle_.Reset(write_handle); 751af69d88dSmrg event_handle_.Reset(::CreateEvent( 752af69d88dSmrg &handles_are_inheritable, 7537ec681f3Smrg TRUE, // The event will automatically reset to non-signaled state. 7547ec681f3Smrg FALSE, // The initial state is non-signalled. 7557ec681f3Smrg nullptr)); // The even is unnamed. 7567ec681f3Smrg GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != nullptr); 7577ec681f3Smrg const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + 7587ec681f3Smrg kFilterFlag + "=" + info->test_suite_name() + 7597ec681f3Smrg "." + info->name(); 760af69d88dSmrg const std::string internal_flag = 761af69d88dSmrg std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + 762af69d88dSmrg "=" + file_ + "|" + StreamableToString(line_) + "|" + 763af69d88dSmrg StreamableToString(death_test_index) + "|" + 764af69d88dSmrg StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) + 765af69d88dSmrg // size_t has the same width as pointers on both 32-bit and 64-bit 766af69d88dSmrg // Windows platforms. 767af69d88dSmrg // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. 768af69d88dSmrg "|" + StreamableToString(reinterpret_cast<size_t>(write_handle)) + 769af69d88dSmrg "|" + StreamableToString(reinterpret_cast<size_t>(event_handle_.Get())); 770af69d88dSmrg 771af69d88dSmrg char executable_path[_MAX_PATH + 1]; // NOLINT 7727ec681f3Smrg GTEST_DEATH_TEST_CHECK_(_MAX_PATH + 1 != ::GetModuleFileNameA(nullptr, 7737ec681f3Smrg executable_path, 7747ec681f3Smrg _MAX_PATH)); 775af69d88dSmrg 776af69d88dSmrg std::string command_line = 777af69d88dSmrg std::string(::GetCommandLineA()) + " " + filter_flag + " \"" + 778af69d88dSmrg internal_flag + "\""; 779af69d88dSmrg 780af69d88dSmrg DeathTest::set_last_death_test_message(""); 781af69d88dSmrg 782af69d88dSmrg CaptureStderr(); 783af69d88dSmrg // Flush the log buffers since the log streams are shared with the child. 784af69d88dSmrg FlushInfoLog(); 785af69d88dSmrg 786af69d88dSmrg // The child process will share the standard handles with the parent. 787af69d88dSmrg STARTUPINFOA startup_info; 788af69d88dSmrg memset(&startup_info, 0, sizeof(STARTUPINFO)); 789af69d88dSmrg startup_info.dwFlags = STARTF_USESTDHANDLES; 790af69d88dSmrg startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE); 791af69d88dSmrg startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); 792af69d88dSmrg startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE); 793af69d88dSmrg 794af69d88dSmrg PROCESS_INFORMATION process_info; 7957ec681f3Smrg GTEST_DEATH_TEST_CHECK_( 7967ec681f3Smrg ::CreateProcessA( 7977ec681f3Smrg executable_path, const_cast<char*>(command_line.c_str()), 7987ec681f3Smrg nullptr, // Retuned process handle is not inheritable. 7997ec681f3Smrg nullptr, // Retuned thread handle is not inheritable. 8007ec681f3Smrg TRUE, // Child inherits all inheritable handles (for write_handle_). 8017ec681f3Smrg 0x0, // Default creation flags. 8027ec681f3Smrg nullptr, // Inherit the parent's environment. 8037ec681f3Smrg UnitTest::GetInstance()->original_working_dir(), &startup_info, 8047ec681f3Smrg &process_info) != FALSE); 805af69d88dSmrg child_handle_.Reset(process_info.hProcess); 806af69d88dSmrg ::CloseHandle(process_info.hThread); 807af69d88dSmrg set_spawned(true); 808af69d88dSmrg return OVERSEE_TEST; 809af69d88dSmrg} 8107ec681f3Smrg 8117ec681f3Smrg# elif GTEST_OS_FUCHSIA 8127ec681f3Smrg 8137ec681f3Smrgclass FuchsiaDeathTest : public DeathTestImpl { 8147ec681f3Smrg public: 8157ec681f3Smrg FuchsiaDeathTest(const char* a_statement, Matcher<const std::string&> matcher, 8167ec681f3Smrg const char* file, int line) 8177ec681f3Smrg : DeathTestImpl(a_statement, std::move(matcher)), 8187ec681f3Smrg file_(file), 8197ec681f3Smrg line_(line) {} 8207ec681f3Smrg 8217ec681f3Smrg // All of these virtual functions are inherited from DeathTest. 8227ec681f3Smrg int Wait() override; 8237ec681f3Smrg TestRole AssumeRole() override; 8247ec681f3Smrg std::string GetErrorLogs() override; 8257ec681f3Smrg 8267ec681f3Smrg private: 8277ec681f3Smrg // The name of the file in which the death test is located. 8287ec681f3Smrg const char* const file_; 8297ec681f3Smrg // The line number on which the death test is located. 8307ec681f3Smrg const int line_; 8317ec681f3Smrg // The stderr data captured by the child process. 8327ec681f3Smrg std::string captured_stderr_; 8337ec681f3Smrg 8347ec681f3Smrg zx::process child_process_; 8357ec681f3Smrg zx::channel exception_channel_; 8367ec681f3Smrg zx::socket stderr_socket_; 8377ec681f3Smrg}; 8387ec681f3Smrg 8397ec681f3Smrg// Utility class for accumulating command-line arguments. 8407ec681f3Smrgclass Arguments { 8417ec681f3Smrg public: 8427ec681f3Smrg Arguments() { args_.push_back(nullptr); } 8437ec681f3Smrg 8447ec681f3Smrg ~Arguments() { 8457ec681f3Smrg for (std::vector<char*>::iterator i = args_.begin(); i != args_.end(); 8467ec681f3Smrg ++i) { 8477ec681f3Smrg free(*i); 8487ec681f3Smrg } 8497ec681f3Smrg } 8507ec681f3Smrg void AddArgument(const char* argument) { 8517ec681f3Smrg args_.insert(args_.end() - 1, posix::StrDup(argument)); 8527ec681f3Smrg } 8537ec681f3Smrg 8547ec681f3Smrg template <typename Str> 8557ec681f3Smrg void AddArguments(const ::std::vector<Str>& arguments) { 8567ec681f3Smrg for (typename ::std::vector<Str>::const_iterator i = arguments.begin(); 8577ec681f3Smrg i != arguments.end(); 8587ec681f3Smrg ++i) { 8597ec681f3Smrg args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); 8607ec681f3Smrg } 8617ec681f3Smrg } 8627ec681f3Smrg char* const* Argv() { 8637ec681f3Smrg return &args_[0]; 8647ec681f3Smrg } 8657ec681f3Smrg 8667ec681f3Smrg int size() { 8677ec681f3Smrg return args_.size() - 1; 8687ec681f3Smrg } 8697ec681f3Smrg 8707ec681f3Smrg private: 8717ec681f3Smrg std::vector<char*> args_; 8727ec681f3Smrg}; 8737ec681f3Smrg 8747ec681f3Smrg// Waits for the child in a death test to exit, returning its exit 8757ec681f3Smrg// status, or 0 if no child process exists. As a side effect, sets the 8767ec681f3Smrg// outcome data member. 8777ec681f3Smrgint FuchsiaDeathTest::Wait() { 8787ec681f3Smrg const int kProcessKey = 0; 8797ec681f3Smrg const int kSocketKey = 1; 8807ec681f3Smrg const int kExceptionKey = 2; 8817ec681f3Smrg 8827ec681f3Smrg if (!spawned()) 8837ec681f3Smrg return 0; 8847ec681f3Smrg 8857ec681f3Smrg // Create a port to wait for socket/task/exception events. 8867ec681f3Smrg zx_status_t status_zx; 8877ec681f3Smrg zx::port port; 8887ec681f3Smrg status_zx = zx::port::create(0, &port); 8897ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 8907ec681f3Smrg 8917ec681f3Smrg // Register to wait for the child process to terminate. 8927ec681f3Smrg status_zx = child_process_.wait_async( 8937ec681f3Smrg port, kProcessKey, ZX_PROCESS_TERMINATED, ZX_WAIT_ASYNC_ONCE); 8947ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 8957ec681f3Smrg 8967ec681f3Smrg // Register to wait for the socket to be readable or closed. 8977ec681f3Smrg status_zx = stderr_socket_.wait_async( 8987ec681f3Smrg port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 8997ec681f3Smrg ZX_WAIT_ASYNC_ONCE); 9007ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9017ec681f3Smrg 9027ec681f3Smrg // Register to wait for an exception. 9037ec681f3Smrg status_zx = exception_channel_.wait_async( 9047ec681f3Smrg port, kExceptionKey, ZX_CHANNEL_READABLE, ZX_WAIT_ASYNC_ONCE); 9057ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9067ec681f3Smrg 9077ec681f3Smrg bool process_terminated = false; 9087ec681f3Smrg bool socket_closed = false; 9097ec681f3Smrg do { 9107ec681f3Smrg zx_port_packet_t packet = {}; 9117ec681f3Smrg status_zx = port.wait(zx::time::infinite(), &packet); 9127ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9137ec681f3Smrg 9147ec681f3Smrg if (packet.key == kExceptionKey) { 9157ec681f3Smrg // Process encountered an exception. Kill it directly rather than 9167ec681f3Smrg // letting other handlers process the event. We will get a kProcessKey 9177ec681f3Smrg // event when the process actually terminates. 9187ec681f3Smrg status_zx = child_process_.kill(); 9197ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9207ec681f3Smrg } else if (packet.key == kProcessKey) { 9217ec681f3Smrg // Process terminated. 9227ec681f3Smrg GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); 9237ec681f3Smrg GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_PROCESS_TERMINATED); 9247ec681f3Smrg process_terminated = true; 9257ec681f3Smrg } else if (packet.key == kSocketKey) { 9267ec681f3Smrg GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); 9277ec681f3Smrg if (packet.signal.observed & ZX_SOCKET_READABLE) { 9287ec681f3Smrg // Read data from the socket. 9297ec681f3Smrg constexpr size_t kBufferSize = 1024; 9307ec681f3Smrg do { 9317ec681f3Smrg size_t old_length = captured_stderr_.length(); 9327ec681f3Smrg size_t bytes_read = 0; 9337ec681f3Smrg captured_stderr_.resize(old_length + kBufferSize); 9347ec681f3Smrg status_zx = stderr_socket_.read( 9357ec681f3Smrg 0, &captured_stderr_.front() + old_length, kBufferSize, 9367ec681f3Smrg &bytes_read); 9377ec681f3Smrg captured_stderr_.resize(old_length + bytes_read); 9387ec681f3Smrg } while (status_zx == ZX_OK); 9397ec681f3Smrg if (status_zx == ZX_ERR_PEER_CLOSED) { 9407ec681f3Smrg socket_closed = true; 9417ec681f3Smrg } else { 9427ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_ERR_SHOULD_WAIT); 9437ec681f3Smrg status_zx = stderr_socket_.wait_async( 9447ec681f3Smrg port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 9457ec681f3Smrg ZX_WAIT_ASYNC_ONCE); 9467ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9477ec681f3Smrg } 9487ec681f3Smrg } else { 9497ec681f3Smrg GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_SOCKET_PEER_CLOSED); 9507ec681f3Smrg socket_closed = true; 9517ec681f3Smrg } 9527ec681f3Smrg } 9537ec681f3Smrg } while (!process_terminated && !socket_closed); 9547ec681f3Smrg 9557ec681f3Smrg ReadAndInterpretStatusByte(); 9567ec681f3Smrg 9577ec681f3Smrg zx_info_process_t buffer; 9587ec681f3Smrg status_zx = child_process_.get_info( 9597ec681f3Smrg ZX_INFO_PROCESS, &buffer, sizeof(buffer), nullptr, nullptr); 9607ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); 9617ec681f3Smrg 9627ec681f3Smrg GTEST_DEATH_TEST_CHECK_(buffer.exited); 9637ec681f3Smrg set_status(buffer.return_code); 9647ec681f3Smrg return status(); 9657ec681f3Smrg} 9667ec681f3Smrg 9677ec681f3Smrg// The AssumeRole process for a Fuchsia death test. It creates a child 9687ec681f3Smrg// process with the same executable as the current process to run the 9697ec681f3Smrg// death test. The child process is given the --gtest_filter and 9707ec681f3Smrg// --gtest_internal_run_death_test flags such that it knows to run the 9717ec681f3Smrg// current death test only. 9727ec681f3SmrgDeathTest::TestRole FuchsiaDeathTest::AssumeRole() { 9737ec681f3Smrg const UnitTestImpl* const impl = GetUnitTestImpl(); 9747ec681f3Smrg const InternalRunDeathTestFlag* const flag = 9757ec681f3Smrg impl->internal_run_death_test_flag(); 9767ec681f3Smrg const TestInfo* const info = impl->current_test_info(); 9777ec681f3Smrg const int death_test_index = info->result()->death_test_count(); 9787ec681f3Smrg 9797ec681f3Smrg if (flag != nullptr) { 9807ec681f3Smrg // ParseInternalRunDeathTestFlag() has performed all the necessary 9817ec681f3Smrg // processing. 9827ec681f3Smrg set_write_fd(kFuchsiaReadPipeFd); 9837ec681f3Smrg return EXECUTE_TEST; 9847ec681f3Smrg } 9857ec681f3Smrg 9867ec681f3Smrg // Flush the log buffers since the log streams are shared with the child. 9877ec681f3Smrg FlushInfoLog(); 9887ec681f3Smrg 9897ec681f3Smrg // Build the child process command line. 9907ec681f3Smrg const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + 9917ec681f3Smrg kFilterFlag + "=" + info->test_suite_name() + 9927ec681f3Smrg "." + info->name(); 9937ec681f3Smrg const std::string internal_flag = 9947ec681f3Smrg std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "=" 9957ec681f3Smrg + file_ + "|" 9967ec681f3Smrg + StreamableToString(line_) + "|" 9977ec681f3Smrg + StreamableToString(death_test_index); 9987ec681f3Smrg Arguments args; 9997ec681f3Smrg args.AddArguments(GetInjectableArgvs()); 10007ec681f3Smrg args.AddArgument(filter_flag.c_str()); 10017ec681f3Smrg args.AddArgument(internal_flag.c_str()); 10027ec681f3Smrg 10037ec681f3Smrg // Build the pipe for communication with the child. 10047ec681f3Smrg zx_status_t status; 10057ec681f3Smrg zx_handle_t child_pipe_handle; 10067ec681f3Smrg int child_pipe_fd; 10077ec681f3Smrg status = fdio_pipe_half(&child_pipe_fd, &child_pipe_handle); 10087ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status == ZX_OK); 10097ec681f3Smrg set_read_fd(child_pipe_fd); 10107ec681f3Smrg 10117ec681f3Smrg // Set the pipe handle for the child. 10127ec681f3Smrg fdio_spawn_action_t spawn_actions[2] = {}; 10137ec681f3Smrg fdio_spawn_action_t* add_handle_action = &spawn_actions[0]; 10147ec681f3Smrg add_handle_action->action = FDIO_SPAWN_ACTION_ADD_HANDLE; 10157ec681f3Smrg add_handle_action->h.id = PA_HND(PA_FD, kFuchsiaReadPipeFd); 10167ec681f3Smrg add_handle_action->h.handle = child_pipe_handle; 10177ec681f3Smrg 10187ec681f3Smrg // Create a socket pair will be used to receive the child process' stderr. 10197ec681f3Smrg zx::socket stderr_producer_socket; 10207ec681f3Smrg status = 10217ec681f3Smrg zx::socket::create(0, &stderr_producer_socket, &stderr_socket_); 10227ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status >= 0); 10237ec681f3Smrg int stderr_producer_fd = -1; 10247ec681f3Smrg status = 10257ec681f3Smrg fdio_fd_create(stderr_producer_socket.release(), &stderr_producer_fd); 10267ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status >= 0); 10277ec681f3Smrg 10287ec681f3Smrg // Make the stderr socket nonblocking. 10297ec681f3Smrg GTEST_DEATH_TEST_CHECK_(fcntl(stderr_producer_fd, F_SETFL, 0) == 0); 10307ec681f3Smrg 10317ec681f3Smrg fdio_spawn_action_t* add_stderr_action = &spawn_actions[1]; 10327ec681f3Smrg add_stderr_action->action = FDIO_SPAWN_ACTION_CLONE_FD; 10337ec681f3Smrg add_stderr_action->fd.local_fd = stderr_producer_fd; 10347ec681f3Smrg add_stderr_action->fd.target_fd = STDERR_FILENO; 10357ec681f3Smrg 10367ec681f3Smrg // Create a child job. 10377ec681f3Smrg zx_handle_t child_job = ZX_HANDLE_INVALID; 10387ec681f3Smrg status = zx_job_create(zx_job_default(), 0, & child_job); 10397ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status == ZX_OK); 10407ec681f3Smrg zx_policy_basic_t policy; 10417ec681f3Smrg policy.condition = ZX_POL_NEW_ANY; 10427ec681f3Smrg policy.policy = ZX_POL_ACTION_ALLOW; 10437ec681f3Smrg status = zx_job_set_policy( 10447ec681f3Smrg child_job, ZX_JOB_POL_RELATIVE, ZX_JOB_POL_BASIC, &policy, 1); 10457ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status == ZX_OK); 10467ec681f3Smrg 10477ec681f3Smrg // Create an exception channel attached to the |child_job|, to allow 10487ec681f3Smrg // us to suppress the system default exception handler from firing. 10497ec681f3Smrg status = 10507ec681f3Smrg zx_task_create_exception_channel( 10517ec681f3Smrg child_job, 0, exception_channel_.reset_and_get_address()); 10527ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status == ZX_OK); 10537ec681f3Smrg 10547ec681f3Smrg // Spawn the child process. 10557ec681f3Smrg status = fdio_spawn_etc( 10567ec681f3Smrg child_job, FDIO_SPAWN_CLONE_ALL, args.Argv()[0], args.Argv(), nullptr, 10577ec681f3Smrg 2, spawn_actions, child_process_.reset_and_get_address(), nullptr); 10587ec681f3Smrg GTEST_DEATH_TEST_CHECK_(status == ZX_OK); 10597ec681f3Smrg 10607ec681f3Smrg set_spawned(true); 10617ec681f3Smrg return OVERSEE_TEST; 10627ec681f3Smrg} 10637ec681f3Smrg 10647ec681f3Smrgstd::string FuchsiaDeathTest::GetErrorLogs() { 10657ec681f3Smrg return captured_stderr_; 10667ec681f3Smrg} 10677ec681f3Smrg 10687ec681f3Smrg#else // We are neither on Windows, nor on Fuchsia. 1069af69d88dSmrg 1070af69d88dSmrg// ForkingDeathTest provides implementations for most of the abstract 1071af69d88dSmrg// methods of the DeathTest interface. Only the AssumeRole method is 1072af69d88dSmrg// left undefined. 1073af69d88dSmrgclass ForkingDeathTest : public DeathTestImpl { 1074af69d88dSmrg public: 10757ec681f3Smrg ForkingDeathTest(const char* statement, Matcher<const std::string&> matcher); 1076af69d88dSmrg 1077af69d88dSmrg // All of these virtual functions are inherited from DeathTest. 10787ec681f3Smrg int Wait() override; 1079af69d88dSmrg 1080af69d88dSmrg protected: 1081af69d88dSmrg void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; } 1082af69d88dSmrg 1083af69d88dSmrg private: 1084af69d88dSmrg // PID of child process during death test; 0 in the child process itself. 1085af69d88dSmrg pid_t child_pid_; 1086af69d88dSmrg}; 1087af69d88dSmrg 1088af69d88dSmrg// Constructs a ForkingDeathTest. 10897ec681f3SmrgForkingDeathTest::ForkingDeathTest(const char* a_statement, 10907ec681f3Smrg Matcher<const std::string&> matcher) 10917ec681f3Smrg : DeathTestImpl(a_statement, std::move(matcher)), child_pid_(-1) {} 1092af69d88dSmrg 1093af69d88dSmrg// Waits for the child in a death test to exit, returning its exit 1094af69d88dSmrg// status, or 0 if no child process exists. As a side effect, sets the 1095af69d88dSmrg// outcome data member. 1096af69d88dSmrgint ForkingDeathTest::Wait() { 1097af69d88dSmrg if (!spawned()) 1098af69d88dSmrg return 0; 1099af69d88dSmrg 1100af69d88dSmrg ReadAndInterpretStatusByte(); 1101af69d88dSmrg 1102af69d88dSmrg int status_value; 1103af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0)); 1104af69d88dSmrg set_status(status_value); 1105af69d88dSmrg return status_value; 1106af69d88dSmrg} 1107af69d88dSmrg 1108af69d88dSmrg// A concrete death test class that forks, then immediately runs the test 1109af69d88dSmrg// in the child process. 1110af69d88dSmrgclass NoExecDeathTest : public ForkingDeathTest { 1111af69d88dSmrg public: 11127ec681f3Smrg NoExecDeathTest(const char* a_statement, Matcher<const std::string&> matcher) 11137ec681f3Smrg : ForkingDeathTest(a_statement, std::move(matcher)) {} 11147ec681f3Smrg TestRole AssumeRole() override; 1115af69d88dSmrg}; 1116af69d88dSmrg 1117af69d88dSmrg// The AssumeRole process for a fork-and-run death test. It implements a 1118af69d88dSmrg// straightforward fork, with a simple pipe to transmit the status byte. 1119af69d88dSmrgDeathTest::TestRole NoExecDeathTest::AssumeRole() { 1120af69d88dSmrg const size_t thread_count = GetThreadCount(); 1121af69d88dSmrg if (thread_count != 1) { 1122af69d88dSmrg GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count); 1123af69d88dSmrg } 1124af69d88dSmrg 1125af69d88dSmrg int pipe_fd[2]; 1126af69d88dSmrg GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); 1127af69d88dSmrg 1128af69d88dSmrg DeathTest::set_last_death_test_message(""); 1129af69d88dSmrg CaptureStderr(); 1130af69d88dSmrg // When we fork the process below, the log file buffers are copied, but the 1131af69d88dSmrg // file descriptors are shared. We flush all log files here so that closing 1132af69d88dSmrg // the file descriptors in the child process doesn't throw off the 1133af69d88dSmrg // synchronization between descriptors and buffers in the parent process. 1134af69d88dSmrg // This is as close to the fork as possible to avoid a race condition in case 1135af69d88dSmrg // there are multiple threads running before the death test, and another 1136af69d88dSmrg // thread writes to the log file. 1137af69d88dSmrg FlushInfoLog(); 1138af69d88dSmrg 1139af69d88dSmrg const pid_t child_pid = fork(); 1140af69d88dSmrg GTEST_DEATH_TEST_CHECK_(child_pid != -1); 1141af69d88dSmrg set_child_pid(child_pid); 1142af69d88dSmrg if (child_pid == 0) { 1143af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0])); 1144af69d88dSmrg set_write_fd(pipe_fd[1]); 1145af69d88dSmrg // Redirects all logging to stderr in the child process to prevent 1146af69d88dSmrg // concurrent writes to the log files. We capture stderr in the parent 1147af69d88dSmrg // process and append the child process' output to a log. 1148af69d88dSmrg LogToStderr(); 1149af69d88dSmrg // Event forwarding to the listeners of event listener API mush be shut 1150af69d88dSmrg // down in death test subprocesses. 1151af69d88dSmrg GetUnitTestImpl()->listeners()->SuppressEventForwarding(); 1152af69d88dSmrg g_in_fast_death_test_child = true; 1153af69d88dSmrg return EXECUTE_TEST; 1154af69d88dSmrg } else { 1155af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); 1156af69d88dSmrg set_read_fd(pipe_fd[0]); 1157af69d88dSmrg set_spawned(true); 1158af69d88dSmrg return OVERSEE_TEST; 1159af69d88dSmrg } 1160af69d88dSmrg} 1161af69d88dSmrg 1162af69d88dSmrg// A concrete death test class that forks and re-executes the main 1163af69d88dSmrg// program from the beginning, with command-line flags set that cause 1164af69d88dSmrg// only this specific death test to be run. 1165af69d88dSmrgclass ExecDeathTest : public ForkingDeathTest { 1166af69d88dSmrg public: 11677ec681f3Smrg ExecDeathTest(const char* a_statement, Matcher<const std::string&> matcher, 11687ec681f3Smrg const char* file, int line) 11697ec681f3Smrg : ForkingDeathTest(a_statement, std::move(matcher)), 11707ec681f3Smrg file_(file), 11717ec681f3Smrg line_(line) {} 11727ec681f3Smrg TestRole AssumeRole() override; 11737ec681f3Smrg 1174af69d88dSmrg private: 11757ec681f3Smrg static ::std::vector<std::string> GetArgvsForDeathTestChildProcess() { 11767ec681f3Smrg ::std::vector<std::string> args = GetInjectableArgvs(); 117701e04c3fSmrg# if defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) 11787ec681f3Smrg ::std::vector<std::string> extra_args = 117901e04c3fSmrg GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_(); 118001e04c3fSmrg args.insert(args.end(), extra_args.begin(), extra_args.end()); 118101e04c3fSmrg# endif // defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) 1182af69d88dSmrg return args; 1183af69d88dSmrg } 1184af69d88dSmrg // The name of the file in which the death test is located. 1185af69d88dSmrg const char* const file_; 1186af69d88dSmrg // The line number on which the death test is located. 1187af69d88dSmrg const int line_; 1188af69d88dSmrg}; 1189af69d88dSmrg 1190af69d88dSmrg// Utility class for accumulating command-line arguments. 1191af69d88dSmrgclass Arguments { 1192af69d88dSmrg public: 11937ec681f3Smrg Arguments() { args_.push_back(nullptr); } 1194af69d88dSmrg 1195af69d88dSmrg ~Arguments() { 1196af69d88dSmrg for (std::vector<char*>::iterator i = args_.begin(); i != args_.end(); 1197af69d88dSmrg ++i) { 1198af69d88dSmrg free(*i); 1199af69d88dSmrg } 1200af69d88dSmrg } 1201af69d88dSmrg void AddArgument(const char* argument) { 1202af69d88dSmrg args_.insert(args_.end() - 1, posix::StrDup(argument)); 1203af69d88dSmrg } 1204af69d88dSmrg 1205af69d88dSmrg template <typename Str> 1206af69d88dSmrg void AddArguments(const ::std::vector<Str>& arguments) { 1207af69d88dSmrg for (typename ::std::vector<Str>::const_iterator i = arguments.begin(); 1208af69d88dSmrg i != arguments.end(); 1209af69d88dSmrg ++i) { 1210af69d88dSmrg args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); 1211af69d88dSmrg } 1212af69d88dSmrg } 1213af69d88dSmrg char* const* Argv() { 1214af69d88dSmrg return &args_[0]; 1215af69d88dSmrg } 1216af69d88dSmrg 1217af69d88dSmrg private: 1218af69d88dSmrg std::vector<char*> args_; 1219af69d88dSmrg}; 1220af69d88dSmrg 1221af69d88dSmrg// A struct that encompasses the arguments to the child process of a 1222af69d88dSmrg// threadsafe-style death test process. 1223af69d88dSmrgstruct ExecDeathTestArgs { 1224af69d88dSmrg char* const* argv; // Command-line arguments for the child's call to exec 1225af69d88dSmrg int close_fd; // File descriptor to close; the read end of a pipe 1226af69d88dSmrg}; 1227af69d88dSmrg 1228af69d88dSmrg# if GTEST_OS_MAC 1229af69d88dSmrginline char** GetEnviron() { 1230af69d88dSmrg // When Google Test is built as a framework on MacOS X, the environ variable 1231af69d88dSmrg // is unavailable. Apple's documentation (man environ) recommends using 1232af69d88dSmrg // _NSGetEnviron() instead. 1233af69d88dSmrg return *_NSGetEnviron(); 1234af69d88dSmrg} 1235af69d88dSmrg# else 1236af69d88dSmrg// Some POSIX platforms expect you to declare environ. extern "C" makes 1237af69d88dSmrg// it reside in the global namespace. 1238af69d88dSmrgextern "C" char** environ; 1239af69d88dSmrginline char** GetEnviron() { return environ; } 1240af69d88dSmrg# endif // GTEST_OS_MAC 1241af69d88dSmrg 1242af69d88dSmrg# if !GTEST_OS_QNX 1243af69d88dSmrg// The main function for a threadsafe-style death test child process. 1244af69d88dSmrg// This function is called in a clone()-ed process and thus must avoid 1245af69d88dSmrg// any potentially unsafe operations like malloc or libc functions. 1246af69d88dSmrgstatic int ExecDeathTestChildMain(void* child_arg) { 1247af69d88dSmrg ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg); 1248af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd)); 1249af69d88dSmrg 1250af69d88dSmrg // We need to execute the test program in the same environment where 1251af69d88dSmrg // it was originally invoked. Therefore we change to the original 1252af69d88dSmrg // working directory first. 1253af69d88dSmrg const char* const original_dir = 1254af69d88dSmrg UnitTest::GetInstance()->original_working_dir(); 1255af69d88dSmrg // We can safely call chdir() as it's a direct system call. 1256af69d88dSmrg if (chdir(original_dir) != 0) { 1257af69d88dSmrg DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + 1258af69d88dSmrg GetLastErrnoDescription()); 1259af69d88dSmrg return EXIT_FAILURE; 1260af69d88dSmrg } 1261af69d88dSmrg 1262af69d88dSmrg // We can safely call execve() as it's a direct system call. We 1263af69d88dSmrg // cannot use execvp() as it's a libc function and thus potentially 1264af69d88dSmrg // unsafe. Since execve() doesn't search the PATH, the user must 1265af69d88dSmrg // invoke the test program via a valid path that contains at least 1266af69d88dSmrg // one path separator. 1267af69d88dSmrg execve(args->argv[0], args->argv, GetEnviron()); 1268af69d88dSmrg DeathTestAbort(std::string("execve(") + args->argv[0] + ", ...) in " + 1269af69d88dSmrg original_dir + " failed: " + 1270af69d88dSmrg GetLastErrnoDescription()); 1271af69d88dSmrg return EXIT_FAILURE; 1272af69d88dSmrg} 1273af69d88dSmrg# endif // !GTEST_OS_QNX 1274af69d88dSmrg 12757ec681f3Smrg# if GTEST_HAS_CLONE 1276af69d88dSmrg// Two utility routines that together determine the direction the stack 1277af69d88dSmrg// grows. 1278af69d88dSmrg// This could be accomplished more elegantly by a single recursive 1279af69d88dSmrg// function, but we want to guard against the unlikely possibility of 1280af69d88dSmrg// a smart compiler optimizing the recursion away. 1281af69d88dSmrg// 1282af69d88dSmrg// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining 1283af69d88dSmrg// StackLowerThanAddress into StackGrowsDown, which then doesn't give 1284af69d88dSmrg// correct answer. 12857ec681f3Smrgstatic void StackLowerThanAddress(const void* ptr, 12867ec681f3Smrg bool* result) GTEST_NO_INLINE_; 12877ec681f3Smrg// HWAddressSanitizer add a random tag to the MSB of the local variable address, 12887ec681f3Smrg// making comparison result unpredictable. 12897ec681f3SmrgGTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 12907ec681f3Smrgstatic void StackLowerThanAddress(const void* ptr, bool* result) { 1291af69d88dSmrg int dummy; 1292af69d88dSmrg *result = (&dummy < ptr); 1293af69d88dSmrg} 1294af69d88dSmrg 129501e04c3fSmrg// Make sure AddressSanitizer does not tamper with the stack here. 129601e04c3fSmrgGTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 12977ec681f3SmrgGTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 12987ec681f3Smrgstatic bool StackGrowsDown() { 1299af69d88dSmrg int dummy; 1300af69d88dSmrg bool result; 1301af69d88dSmrg StackLowerThanAddress(&dummy, &result); 1302af69d88dSmrg return result; 1303af69d88dSmrg} 13047ec681f3Smrg# endif // GTEST_HAS_CLONE 1305af69d88dSmrg 1306af69d88dSmrg// Spawns a child process with the same executable as the current process in 1307af69d88dSmrg// a thread-safe manner and instructs it to run the death test. The 1308af69d88dSmrg// implementation uses fork(2) + exec. On systems where clone(2) is 1309af69d88dSmrg// available, it is used instead, being slightly more thread-safe. On QNX, 1310af69d88dSmrg// fork supports only single-threaded environments, so this function uses 1311af69d88dSmrg// spawn(2) there instead. The function dies with an error message if 1312af69d88dSmrg// anything goes wrong. 1313af69d88dSmrgstatic pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) { 1314af69d88dSmrg ExecDeathTestArgs args = { argv, close_fd }; 1315af69d88dSmrg pid_t child_pid = -1; 1316af69d88dSmrg 1317af69d88dSmrg# if GTEST_OS_QNX 1318af69d88dSmrg // Obtains the current directory and sets it to be closed in the child 1319af69d88dSmrg // process. 1320af69d88dSmrg const int cwd_fd = open(".", O_RDONLY); 1321af69d88dSmrg GTEST_DEATH_TEST_CHECK_(cwd_fd != -1); 1322af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC)); 1323af69d88dSmrg // We need to execute the test program in the same environment where 1324af69d88dSmrg // it was originally invoked. Therefore we change to the original 1325af69d88dSmrg // working directory first. 1326af69d88dSmrg const char* const original_dir = 1327af69d88dSmrg UnitTest::GetInstance()->original_working_dir(); 1328af69d88dSmrg // We can safely call chdir() as it's a direct system call. 1329af69d88dSmrg if (chdir(original_dir) != 0) { 1330af69d88dSmrg DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + 1331af69d88dSmrg GetLastErrnoDescription()); 1332af69d88dSmrg return EXIT_FAILURE; 1333af69d88dSmrg } 1334af69d88dSmrg 1335af69d88dSmrg int fd_flags; 1336af69d88dSmrg // Set close_fd to be closed after spawn. 1337af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD)); 1338af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD, 1339af69d88dSmrg fd_flags | FD_CLOEXEC)); 1340af69d88dSmrg struct inheritance inherit = {0}; 1341af69d88dSmrg // spawn is a system call. 13427ec681f3Smrg child_pid = 13437ec681f3Smrg spawn(args.argv[0], 0, nullptr, &inherit, args.argv, GetEnviron()); 1344af69d88dSmrg // Restores the current working directory. 1345af69d88dSmrg GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1); 1346af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd)); 1347af69d88dSmrg 1348af69d88dSmrg# else // GTEST_OS_QNX 1349af69d88dSmrg# if GTEST_OS_LINUX 1350af69d88dSmrg // When a SIGPROF signal is received while fork() or clone() are executing, 1351af69d88dSmrg // the process may hang. To avoid this, we ignore SIGPROF here and re-enable 1352af69d88dSmrg // it after the call to fork()/clone() is complete. 1353af69d88dSmrg struct sigaction saved_sigprof_action; 1354af69d88dSmrg struct sigaction ignore_sigprof_action; 1355af69d88dSmrg memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action)); 1356af69d88dSmrg sigemptyset(&ignore_sigprof_action.sa_mask); 1357af69d88dSmrg ignore_sigprof_action.sa_handler = SIG_IGN; 1358af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction( 1359af69d88dSmrg SIGPROF, &ignore_sigprof_action, &saved_sigprof_action)); 1360af69d88dSmrg# endif // GTEST_OS_LINUX 1361af69d88dSmrg 1362af69d88dSmrg# if GTEST_HAS_CLONE 1363af69d88dSmrg const bool use_fork = GTEST_FLAG(death_test_use_fork); 1364af69d88dSmrg 1365af69d88dSmrg if (!use_fork) { 1366af69d88dSmrg static const bool stack_grows_down = StackGrowsDown(); 13677ec681f3Smrg const auto stack_size = static_cast<size_t>(getpagesize()); 1368af69d88dSmrg // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead. 13697ec681f3Smrg void* const stack = mmap(nullptr, stack_size, PROT_READ | PROT_WRITE, 1370af69d88dSmrg MAP_ANON | MAP_PRIVATE, -1, 0); 1371af69d88dSmrg GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED); 1372af69d88dSmrg 1373af69d88dSmrg // Maximum stack alignment in bytes: For a downward-growing stack, this 1374af69d88dSmrg // amount is subtracted from size of the stack space to get an address 1375af69d88dSmrg // that is within the stack space and is aligned on all systems we care 1376af69d88dSmrg // about. As far as I know there is no ABI with stack alignment greater 1377af69d88dSmrg // than 64. We assume stack and stack_size already have alignment of 1378af69d88dSmrg // kMaxStackAlignment. 1379af69d88dSmrg const size_t kMaxStackAlignment = 64; 1380af69d88dSmrg void* const stack_top = 1381af69d88dSmrg static_cast<char*>(stack) + 1382af69d88dSmrg (stack_grows_down ? stack_size - kMaxStackAlignment : 0); 13837ec681f3Smrg GTEST_DEATH_TEST_CHECK_( 13847ec681f3Smrg static_cast<size_t>(stack_size) > kMaxStackAlignment && 13857ec681f3Smrg reinterpret_cast<uintptr_t>(stack_top) % kMaxStackAlignment == 0); 1386af69d88dSmrg 1387af69d88dSmrg child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args); 1388af69d88dSmrg 1389af69d88dSmrg GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1); 1390af69d88dSmrg } 1391af69d88dSmrg# else 1392af69d88dSmrg const bool use_fork = true; 1393af69d88dSmrg# endif // GTEST_HAS_CLONE 1394af69d88dSmrg 1395af69d88dSmrg if (use_fork && (child_pid = fork()) == 0) { 1396af69d88dSmrg ExecDeathTestChildMain(&args); 1397af69d88dSmrg _exit(0); 1398af69d88dSmrg } 1399af69d88dSmrg# endif // GTEST_OS_QNX 1400af69d88dSmrg# if GTEST_OS_LINUX 1401af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_( 14027ec681f3Smrg sigaction(SIGPROF, &saved_sigprof_action, nullptr)); 1403af69d88dSmrg# endif // GTEST_OS_LINUX 1404af69d88dSmrg 1405af69d88dSmrg GTEST_DEATH_TEST_CHECK_(child_pid != -1); 1406af69d88dSmrg return child_pid; 1407af69d88dSmrg} 1408af69d88dSmrg 1409af69d88dSmrg// The AssumeRole process for a fork-and-exec death test. It re-executes the 1410af69d88dSmrg// main program from the beginning, setting the --gtest_filter 1411af69d88dSmrg// and --gtest_internal_run_death_test flags to cause only the current 1412af69d88dSmrg// death test to be re-run. 1413af69d88dSmrgDeathTest::TestRole ExecDeathTest::AssumeRole() { 1414af69d88dSmrg const UnitTestImpl* const impl = GetUnitTestImpl(); 1415af69d88dSmrg const InternalRunDeathTestFlag* const flag = 1416af69d88dSmrg impl->internal_run_death_test_flag(); 1417af69d88dSmrg const TestInfo* const info = impl->current_test_info(); 1418af69d88dSmrg const int death_test_index = info->result()->death_test_count(); 1419af69d88dSmrg 14207ec681f3Smrg if (flag != nullptr) { 1421af69d88dSmrg set_write_fd(flag->write_fd()); 1422af69d88dSmrg return EXECUTE_TEST; 1423af69d88dSmrg } 1424af69d88dSmrg 1425af69d88dSmrg int pipe_fd[2]; 1426af69d88dSmrg GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); 1427af69d88dSmrg // Clear the close-on-exec flag on the write end of the pipe, lest 1428af69d88dSmrg // it be closed when the child process does an exec: 1429af69d88dSmrg GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1); 1430af69d88dSmrg 14317ec681f3Smrg const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + 14327ec681f3Smrg kFilterFlag + "=" + info->test_suite_name() + 14337ec681f3Smrg "." + info->name(); 1434af69d88dSmrg const std::string internal_flag = 1435af69d88dSmrg std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "=" 1436af69d88dSmrg + file_ + "|" + StreamableToString(line_) + "|" 1437af69d88dSmrg + StreamableToString(death_test_index) + "|" 1438af69d88dSmrg + StreamableToString(pipe_fd[1]); 1439af69d88dSmrg Arguments args; 1440af69d88dSmrg args.AddArguments(GetArgvsForDeathTestChildProcess()); 1441af69d88dSmrg args.AddArgument(filter_flag.c_str()); 1442af69d88dSmrg args.AddArgument(internal_flag.c_str()); 1443af69d88dSmrg 1444af69d88dSmrg DeathTest::set_last_death_test_message(""); 1445af69d88dSmrg 1446af69d88dSmrg CaptureStderr(); 1447af69d88dSmrg // See the comment in NoExecDeathTest::AssumeRole for why the next line 1448af69d88dSmrg // is necessary. 1449af69d88dSmrg FlushInfoLog(); 1450af69d88dSmrg 1451af69d88dSmrg const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]); 1452af69d88dSmrg GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); 1453af69d88dSmrg set_child_pid(child_pid); 1454af69d88dSmrg set_read_fd(pipe_fd[0]); 1455af69d88dSmrg set_spawned(true); 1456af69d88dSmrg return OVERSEE_TEST; 1457af69d88dSmrg} 1458af69d88dSmrg 1459af69d88dSmrg# endif // !GTEST_OS_WINDOWS 1460af69d88dSmrg 1461af69d88dSmrg// Creates a concrete DeathTest-derived class that depends on the 1462af69d88dSmrg// --gtest_death_test_style flag, and sets the pointer pointed to 1463af69d88dSmrg// by the "test" argument to its address. If the test should be 1464af69d88dSmrg// skipped, sets that pointer to NULL. Returns true, unless the 1465af69d88dSmrg// flag is set to an invalid value. 14667ec681f3Smrgbool DefaultDeathTestFactory::Create(const char* statement, 14677ec681f3Smrg Matcher<const std::string&> matcher, 1468af69d88dSmrg const char* file, int line, 1469af69d88dSmrg DeathTest** test) { 1470af69d88dSmrg UnitTestImpl* const impl = GetUnitTestImpl(); 1471af69d88dSmrg const InternalRunDeathTestFlag* const flag = 1472af69d88dSmrg impl->internal_run_death_test_flag(); 1473af69d88dSmrg const int death_test_index = impl->current_test_info() 1474af69d88dSmrg ->increment_death_test_count(); 1475af69d88dSmrg 14767ec681f3Smrg if (flag != nullptr) { 1477af69d88dSmrg if (death_test_index > flag->index()) { 1478af69d88dSmrg DeathTest::set_last_death_test_message( 1479af69d88dSmrg "Death test count (" + StreamableToString(death_test_index) 1480af69d88dSmrg + ") somehow exceeded expected maximum (" 1481af69d88dSmrg + StreamableToString(flag->index()) + ")"); 1482af69d88dSmrg return false; 1483af69d88dSmrg } 1484af69d88dSmrg 1485af69d88dSmrg if (!(flag->file() == file && flag->line() == line && 1486af69d88dSmrg flag->index() == death_test_index)) { 14877ec681f3Smrg *test = nullptr; 1488af69d88dSmrg return true; 1489af69d88dSmrg } 1490af69d88dSmrg } 1491af69d88dSmrg 1492af69d88dSmrg# if GTEST_OS_WINDOWS 1493af69d88dSmrg 1494af69d88dSmrg if (GTEST_FLAG(death_test_style) == "threadsafe" || 1495af69d88dSmrg GTEST_FLAG(death_test_style) == "fast") { 14967ec681f3Smrg *test = new WindowsDeathTest(statement, std::move(matcher), file, line); 14977ec681f3Smrg } 14987ec681f3Smrg 14997ec681f3Smrg# elif GTEST_OS_FUCHSIA 15007ec681f3Smrg 15017ec681f3Smrg if (GTEST_FLAG(death_test_style) == "threadsafe" || 15027ec681f3Smrg GTEST_FLAG(death_test_style) == "fast") { 15037ec681f3Smrg *test = new FuchsiaDeathTest(statement, std::move(matcher), file, line); 1504af69d88dSmrg } 1505af69d88dSmrg 1506af69d88dSmrg# else 1507af69d88dSmrg 1508af69d88dSmrg if (GTEST_FLAG(death_test_style) == "threadsafe") { 15097ec681f3Smrg *test = new ExecDeathTest(statement, std::move(matcher), file, line); 1510af69d88dSmrg } else if (GTEST_FLAG(death_test_style) == "fast") { 15117ec681f3Smrg *test = new NoExecDeathTest(statement, std::move(matcher)); 1512af69d88dSmrg } 1513af69d88dSmrg 1514af69d88dSmrg# endif // GTEST_OS_WINDOWS 1515af69d88dSmrg 1516af69d88dSmrg else { // NOLINT - this is more readable than unbalanced brackets inside #if. 1517af69d88dSmrg DeathTest::set_last_death_test_message( 1518af69d88dSmrg "Unknown death test style \"" + GTEST_FLAG(death_test_style) 1519af69d88dSmrg + "\" encountered"); 1520af69d88dSmrg return false; 1521af69d88dSmrg } 1522af69d88dSmrg 1523af69d88dSmrg return true; 1524af69d88dSmrg} 1525af69d88dSmrg 1526af69d88dSmrg# if GTEST_OS_WINDOWS 1527af69d88dSmrg// Recreates the pipe and event handles from the provided parameters, 1528af69d88dSmrg// signals the event, and returns a file descriptor wrapped around the pipe 1529af69d88dSmrg// handle. This function is called in the child process only. 15307ec681f3Smrgstatic int GetStatusFileDescriptor(unsigned int parent_process_id, 1531af69d88dSmrg size_t write_handle_as_size_t, 1532af69d88dSmrg size_t event_handle_as_size_t) { 1533af69d88dSmrg AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, 1534af69d88dSmrg FALSE, // Non-inheritable. 1535af69d88dSmrg parent_process_id)); 1536af69d88dSmrg if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { 1537af69d88dSmrg DeathTestAbort("Unable to open parent process " + 1538af69d88dSmrg StreamableToString(parent_process_id)); 1539af69d88dSmrg } 1540af69d88dSmrg 1541af69d88dSmrg GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); 1542af69d88dSmrg 1543af69d88dSmrg const HANDLE write_handle = 1544af69d88dSmrg reinterpret_cast<HANDLE>(write_handle_as_size_t); 1545af69d88dSmrg HANDLE dup_write_handle; 1546af69d88dSmrg 15477ec681f3Smrg // The newly initialized handle is accessible only in the parent 1548af69d88dSmrg // process. To obtain one accessible within the child, we need to use 1549af69d88dSmrg // DuplicateHandle. 1550af69d88dSmrg if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, 1551af69d88dSmrg ::GetCurrentProcess(), &dup_write_handle, 1552af69d88dSmrg 0x0, // Requested privileges ignored since 1553af69d88dSmrg // DUPLICATE_SAME_ACCESS is used. 1554af69d88dSmrg FALSE, // Request non-inheritable handler. 1555af69d88dSmrg DUPLICATE_SAME_ACCESS)) { 1556af69d88dSmrg DeathTestAbort("Unable to duplicate the pipe handle " + 1557af69d88dSmrg StreamableToString(write_handle_as_size_t) + 1558af69d88dSmrg " from the parent process " + 1559af69d88dSmrg StreamableToString(parent_process_id)); 1560af69d88dSmrg } 1561af69d88dSmrg 1562af69d88dSmrg const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t); 1563af69d88dSmrg HANDLE dup_event_handle; 1564af69d88dSmrg 1565af69d88dSmrg if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, 1566af69d88dSmrg ::GetCurrentProcess(), &dup_event_handle, 1567af69d88dSmrg 0x0, 1568af69d88dSmrg FALSE, 1569af69d88dSmrg DUPLICATE_SAME_ACCESS)) { 1570af69d88dSmrg DeathTestAbort("Unable to duplicate the event handle " + 1571af69d88dSmrg StreamableToString(event_handle_as_size_t) + 1572af69d88dSmrg " from the parent process " + 1573af69d88dSmrg StreamableToString(parent_process_id)); 1574af69d88dSmrg } 1575af69d88dSmrg 1576af69d88dSmrg const int write_fd = 1577af69d88dSmrg ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND); 1578af69d88dSmrg if (write_fd == -1) { 1579af69d88dSmrg DeathTestAbort("Unable to convert pipe handle " + 1580af69d88dSmrg StreamableToString(write_handle_as_size_t) + 1581af69d88dSmrg " to a file descriptor"); 1582af69d88dSmrg } 1583af69d88dSmrg 1584af69d88dSmrg // Signals the parent that the write end of the pipe has been acquired 1585af69d88dSmrg // so the parent can release its own write end. 1586af69d88dSmrg ::SetEvent(dup_event_handle); 1587af69d88dSmrg 1588af69d88dSmrg return write_fd; 1589af69d88dSmrg} 1590af69d88dSmrg# endif // GTEST_OS_WINDOWS 1591af69d88dSmrg 1592af69d88dSmrg// Returns a newly created InternalRunDeathTestFlag object with fields 1593af69d88dSmrg// initialized from the GTEST_FLAG(internal_run_death_test) flag if 1594af69d88dSmrg// the flag is specified; otherwise returns NULL. 1595af69d88dSmrgInternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { 15967ec681f3Smrg if (GTEST_FLAG(internal_run_death_test) == "") return nullptr; 1597af69d88dSmrg 1598af69d88dSmrg // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we 1599af69d88dSmrg // can use it here. 1600af69d88dSmrg int line = -1; 1601af69d88dSmrg int index = -1; 1602af69d88dSmrg ::std::vector< ::std::string> fields; 1603af69d88dSmrg SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields); 1604af69d88dSmrg int write_fd = -1; 1605af69d88dSmrg 1606af69d88dSmrg# if GTEST_OS_WINDOWS 1607af69d88dSmrg 1608af69d88dSmrg unsigned int parent_process_id = 0; 1609af69d88dSmrg size_t write_handle_as_size_t = 0; 1610af69d88dSmrg size_t event_handle_as_size_t = 0; 1611af69d88dSmrg 1612af69d88dSmrg if (fields.size() != 6 1613af69d88dSmrg || !ParseNaturalNumber(fields[1], &line) 1614af69d88dSmrg || !ParseNaturalNumber(fields[2], &index) 1615af69d88dSmrg || !ParseNaturalNumber(fields[3], &parent_process_id) 1616af69d88dSmrg || !ParseNaturalNumber(fields[4], &write_handle_as_size_t) 1617af69d88dSmrg || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) { 1618af69d88dSmrg DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + 1619af69d88dSmrg GTEST_FLAG(internal_run_death_test)); 1620af69d88dSmrg } 1621af69d88dSmrg write_fd = GetStatusFileDescriptor(parent_process_id, 1622af69d88dSmrg write_handle_as_size_t, 1623af69d88dSmrg event_handle_as_size_t); 16247ec681f3Smrg 16257ec681f3Smrg# elif GTEST_OS_FUCHSIA 16267ec681f3Smrg 16277ec681f3Smrg if (fields.size() != 3 16287ec681f3Smrg || !ParseNaturalNumber(fields[1], &line) 16297ec681f3Smrg || !ParseNaturalNumber(fields[2], &index)) { 16307ec681f3Smrg DeathTestAbort("Bad --gtest_internal_run_death_test flag: " 16317ec681f3Smrg + GTEST_FLAG(internal_run_death_test)); 16327ec681f3Smrg } 16337ec681f3Smrg 1634af69d88dSmrg# else 1635af69d88dSmrg 1636af69d88dSmrg if (fields.size() != 4 1637af69d88dSmrg || !ParseNaturalNumber(fields[1], &line) 1638af69d88dSmrg || !ParseNaturalNumber(fields[2], &index) 1639af69d88dSmrg || !ParseNaturalNumber(fields[3], &write_fd)) { 1640af69d88dSmrg DeathTestAbort("Bad --gtest_internal_run_death_test flag: " 1641af69d88dSmrg + GTEST_FLAG(internal_run_death_test)); 1642af69d88dSmrg } 1643af69d88dSmrg 1644af69d88dSmrg# endif // GTEST_OS_WINDOWS 1645af69d88dSmrg 1646af69d88dSmrg return new InternalRunDeathTestFlag(fields[0], line, index, write_fd); 1647af69d88dSmrg} 1648af69d88dSmrg 1649af69d88dSmrg} // namespace internal 1650af69d88dSmrg 1651af69d88dSmrg#endif // GTEST_HAS_DEATH_TEST 1652af69d88dSmrg 1653af69d88dSmrg} // namespace testing 1654