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