1// Copyright 2008, 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#include "gtest/internal/gtest-port.h" 31 32#include <limits.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <string.h> 36 37#include <cstdint> 38#include <fstream> 39#include <memory> 40#include <ostream> 41#include <string> 42#include <utility> 43#include <vector> 44 45#ifdef GTEST_OS_WINDOWS 46#include <io.h> 47#include <sys/stat.h> 48#include <windows.h> 49 50#include <map> // Used in ThreadLocal. 51#ifdef _MSC_VER 52#include <crtdbg.h> 53#endif // _MSC_VER 54#else 55#include <unistd.h> 56#endif // GTEST_OS_WINDOWS 57 58#ifdef GTEST_OS_MAC 59#include <mach/mach_init.h> 60#include <mach/task.h> 61#include <mach/vm_map.h> 62#endif // GTEST_OS_MAC 63 64#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \ 65 defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD) || \ 66 defined(GTEST_OS_OPENBSD) 67#include <sys/sysctl.h> 68#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \ 69 defined(GTEST_OS_GNU_KFREEBSD) 70#include <sys/user.h> 71#endif 72#endif 73 74#ifdef GTEST_OS_QNX 75#include <devctl.h> 76#include <fcntl.h> 77#include <sys/procfs.h> 78#endif // GTEST_OS_QNX 79 80#ifdef GTEST_OS_AIX 81#include <procinfo.h> 82#include <sys/types.h> 83#endif // GTEST_OS_AIX 84 85#ifdef GTEST_OS_FUCHSIA 86#include <zircon/process.h> 87#include <zircon/syscalls.h> 88#endif // GTEST_OS_FUCHSIA 89 90#include "gtest/gtest-message.h" 91#include "gtest/gtest-spi.h" 92#include "gtest/internal/gtest-internal.h" 93#include "gtest/internal/gtest-string.h" 94#include "src/gtest-internal-inl.h" 95 96namespace testing { 97namespace internal { 98 99#if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_HURD) 100 101namespace { 102template <typename T> 103T ReadProcFileField(const std::string& filename, int field) { 104 std::string dummy; 105 std::ifstream file(filename.c_str()); 106 while (field-- > 0) { 107 file >> dummy; 108 } 109 T output = 0; 110 file >> output; 111 return output; 112} 113} // namespace 114 115// Returns the number of active threads, or 0 when there is an error. 116size_t GetThreadCount() { 117 const std::string filename = 118 (Message() << "/proc/" << getpid() << "/stat").GetString(); 119 return ReadProcFileField<size_t>(filename, 19); 120} 121 122#elif defined(GTEST_OS_MAC) 123 124size_t GetThreadCount() { 125 const task_t task = mach_task_self(); 126 mach_msg_type_number_t thread_count; 127 thread_act_array_t thread_list; 128 const kern_return_t status = task_threads(task, &thread_list, &thread_count); 129 if (status == KERN_SUCCESS) { 130 // task_threads allocates resources in thread_list and we need to free them 131 // to avoid leaks. 132 vm_deallocate(task, reinterpret_cast<vm_address_t>(thread_list), 133 sizeof(thread_t) * thread_count); 134 return static_cast<size_t>(thread_count); 135 } else { 136 return 0; 137 } 138} 139 140#elif defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \ 141 defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD) 142 143#ifdef GTEST_OS_NETBSD 144#undef KERN_PROC 145#define KERN_PROC KERN_PROC2 146#define kinfo_proc kinfo_proc2 147#endif 148 149#ifdef GTEST_OS_DRAGONFLY 150#define KP_NLWP(kp) (kp.kp_nthreads) 151#elif defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_GNU_KFREEBSD) 152#define KP_NLWP(kp) (kp.ki_numthreads) 153#elif defined(GTEST_OS_NETBSD) 154#define KP_NLWP(kp) (kp.p_nlwps) 155#endif 156 157// Returns the number of threads running in the process, or 0 to indicate that 158// we cannot detect it. 159size_t GetThreadCount() { 160 int mib[] = { 161 CTL_KERN, 162 KERN_PROC, 163 KERN_PROC_PID, 164 getpid(), 165#ifdef GTEST_OS_NETBSD 166 sizeof(struct kinfo_proc), 167 1, 168#endif 169 }; 170 u_int miblen = sizeof(mib) / sizeof(mib[0]); 171 struct kinfo_proc info; 172 size_t size = sizeof(info); 173 if (sysctl(mib, miblen, &info, &size, NULL, 0)) { 174 return 0; 175 } 176 return static_cast<size_t>(KP_NLWP(info)); 177} 178#elif defined(GTEST_OS_OPENBSD) 179 180// Returns the number of threads running in the process, or 0 to indicate that 181// we cannot detect it. 182size_t GetThreadCount() { 183 int mib[] = { 184 CTL_KERN, 185 KERN_PROC, 186 KERN_PROC_PID | KERN_PROC_SHOW_THREADS, 187 getpid(), 188 sizeof(struct kinfo_proc), 189 0, 190 }; 191 u_int miblen = sizeof(mib) / sizeof(mib[0]); 192 193 // get number of structs 194 size_t size; 195 if (sysctl(mib, miblen, NULL, &size, NULL, 0)) { 196 return 0; 197 } 198 199 mib[5] = static_cast<int>(size / static_cast<size_t>(mib[4])); 200 201 // populate array of structs 202 std::vector<struct kinfo_proc> info(mib[5]); 203 if (sysctl(mib, miblen, info.data(), &size, NULL, 0)) { 204 return 0; 205 } 206 207 // exclude empty members 208 size_t nthreads = 0; 209 for (size_t i = 0; i < size / static_cast<size_t>(mib[4]); i++) { 210 if (info[i].p_tid != -1) nthreads++; 211 } 212 return nthreads; 213} 214 215#elif defined(GTEST_OS_QNX) 216 217// Returns the number of threads running in the process, or 0 to indicate that 218// we cannot detect it. 219size_t GetThreadCount() { 220 const int fd = open("/proc/self/as", O_RDONLY); 221 if (fd < 0) { 222 return 0; 223 } 224 procfs_info process_info; 225 const int status = 226 devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), nullptr); 227 close(fd); 228 if (status == EOK) { 229 return static_cast<size_t>(process_info.num_threads); 230 } else { 231 return 0; 232 } 233} 234 235#elif defined(GTEST_OS_AIX) 236 237size_t GetThreadCount() { 238 struct procentry64 entry; 239 pid_t pid = getpid(); 240 int status = getprocs64(&entry, sizeof(entry), nullptr, 0, &pid, 1); 241 if (status == 1) { 242 return entry.pi_thcount; 243 } else { 244 return 0; 245 } 246} 247 248#elif defined(GTEST_OS_FUCHSIA) 249 250size_t GetThreadCount() { 251 int dummy_buffer; 252 size_t avail; 253 zx_status_t status = 254 zx_object_get_info(zx_process_self(), ZX_INFO_PROCESS_THREADS, 255 &dummy_buffer, 0, nullptr, &avail); 256 if (status == ZX_OK) { 257 return avail; 258 } else { 259 return 0; 260 } 261} 262 263#else 264 265size_t GetThreadCount() { 266 // There's no portable way to detect the number of threads, so we just 267 // return 0 to indicate that we cannot detect it. 268 return 0; 269} 270 271#endif // GTEST_OS_LINUX 272 273#if defined(GTEST_IS_THREADSAFE) && defined(GTEST_OS_WINDOWS) 274 275AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} 276 277AutoHandle::AutoHandle(Handle handle) : handle_(handle) {} 278 279AutoHandle::~AutoHandle() { Reset(); } 280 281AutoHandle::Handle AutoHandle::Get() const { return handle_; } 282 283void AutoHandle::Reset() { Reset(INVALID_HANDLE_VALUE); } 284 285void AutoHandle::Reset(HANDLE handle) { 286 // Resetting with the same handle we already own is invalid. 287 if (handle_ != handle) { 288 if (IsCloseable()) { 289 ::CloseHandle(handle_); 290 } 291 handle_ = handle; 292 } else { 293 GTEST_CHECK_(!IsCloseable()) 294 << "Resetting a valid handle to itself is likely a programmer error " 295 "and thus not allowed."; 296 } 297} 298 299bool AutoHandle::IsCloseable() const { 300 // Different Windows APIs may use either of these values to represent an 301 // invalid handle. 302 return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE; 303} 304 305Mutex::Mutex() 306 : owner_thread_id_(0), 307 type_(kDynamic), 308 critical_section_init_phase_(0), 309 critical_section_(new CRITICAL_SECTION) { 310 ::InitializeCriticalSection(critical_section_); 311} 312 313Mutex::~Mutex() { 314 // Static mutexes are leaked intentionally. It is not thread-safe to try 315 // to clean them up. 316 if (type_ == kDynamic) { 317 ::DeleteCriticalSection(critical_section_); 318 delete critical_section_; 319 critical_section_ = nullptr; 320 } 321} 322 323void Mutex::Lock() { 324 ThreadSafeLazyInit(); 325 ::EnterCriticalSection(critical_section_); 326 owner_thread_id_ = ::GetCurrentThreadId(); 327} 328 329void Mutex::Unlock() { 330 ThreadSafeLazyInit(); 331 // We don't protect writing to owner_thread_id_ here, as it's the 332 // caller's responsibility to ensure that the current thread holds the 333 // mutex when this is called. 334 owner_thread_id_ = 0; 335 ::LeaveCriticalSection(critical_section_); 336} 337 338// Does nothing if the current thread holds the mutex. Otherwise, crashes 339// with high probability. 340void Mutex::AssertHeld() { 341 ThreadSafeLazyInit(); 342 GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId()) 343 << "The current thread is not holding the mutex @" << this; 344} 345 346namespace { 347 348#ifdef _MSC_VER 349// Use the RAII idiom to flag mem allocs that are intentionally never 350// deallocated. The motivation is to silence the false positive mem leaks 351// that are reported by the debug version of MS's CRT which can only detect 352// if an alloc is missing a matching deallocation. 353// Example: 354// MemoryIsNotDeallocated memory_is_not_deallocated; 355// critical_section_ = new CRITICAL_SECTION; 356// 357class MemoryIsNotDeallocated { 358 public: 359 MemoryIsNotDeallocated() : old_crtdbg_flag_(0) { 360 old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); 361 // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT 362 // doesn't report mem leak if there's no matching deallocation. 363 (void)_CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF); 364 } 365 366 ~MemoryIsNotDeallocated() { 367 // Restore the original _CRTDBG_ALLOC_MEM_DF flag 368 (void)_CrtSetDbgFlag(old_crtdbg_flag_); 369 } 370 371 private: 372 int old_crtdbg_flag_; 373 374 MemoryIsNotDeallocated(const MemoryIsNotDeallocated&) = delete; 375 MemoryIsNotDeallocated& operator=(const MemoryIsNotDeallocated&) = delete; 376}; 377#endif // _MSC_VER 378 379} // namespace 380 381// Initializes owner_thread_id_ and critical_section_ in static mutexes. 382void Mutex::ThreadSafeLazyInit() { 383 // Dynamic mutexes are initialized in the constructor. 384 if (type_ == kStatic) { 385 switch ( 386 ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) { 387 case 0: 388 // If critical_section_init_phase_ was 0 before the exchange, we 389 // are the first to test it and need to perform the initialization. 390 owner_thread_id_ = 0; 391 { 392 // Use RAII to flag that following mem alloc is never deallocated. 393#ifdef _MSC_VER 394 MemoryIsNotDeallocated memory_is_not_deallocated; 395#endif // _MSC_VER 396 critical_section_ = new CRITICAL_SECTION; 397 } 398 ::InitializeCriticalSection(critical_section_); 399 // Updates the critical_section_init_phase_ to 2 to signal 400 // initialization complete. 401 GTEST_CHECK_(::InterlockedCompareExchange(&critical_section_init_phase_, 402 2L, 1L) == 1L); 403 break; 404 case 1: 405 // Somebody else is already initializing the mutex; spin until they 406 // are done. 407 while (::InterlockedCompareExchange(&critical_section_init_phase_, 2L, 408 2L) != 2L) { 409 // Possibly yields the rest of the thread's time slice to other 410 // threads. 411 ::Sleep(0); 412 } 413 break; 414 415 case 2: 416 break; // The mutex is already initialized and ready for use. 417 418 default: 419 GTEST_CHECK_(false) 420 << "Unexpected value of critical_section_init_phase_ " 421 << "while initializing a static mutex."; 422 } 423 } 424} 425 426namespace { 427 428class ThreadWithParamSupport : public ThreadWithParamBase { 429 public: 430 static HANDLE CreateThread(Runnable* runnable, 431 Notification* thread_can_start) { 432 ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start); 433 DWORD thread_id; 434 HANDLE thread_handle = ::CreateThread( 435 nullptr, // Default security. 436 0, // Default stack size. 437 &ThreadWithParamSupport::ThreadMain, 438 param, // Parameter to ThreadMainStatic 439 0x0, // Default creation flags. 440 &thread_id); // Need a valid pointer for the call to work under Win98. 441 GTEST_CHECK_(thread_handle != nullptr) 442 << "CreateThread failed with error " << ::GetLastError() << "."; 443 if (thread_handle == nullptr) { 444 delete param; 445 } 446 return thread_handle; 447 } 448 449 private: 450 struct ThreadMainParam { 451 ThreadMainParam(Runnable* runnable, Notification* thread_can_start) 452 : runnable_(runnable), thread_can_start_(thread_can_start) {} 453 std::unique_ptr<Runnable> runnable_; 454 // Does not own. 455 Notification* thread_can_start_; 456 }; 457 458 static DWORD WINAPI ThreadMain(void* ptr) { 459 // Transfers ownership. 460 std::unique_ptr<ThreadMainParam> param(static_cast<ThreadMainParam*>(ptr)); 461 if (param->thread_can_start_ != nullptr) 462 param->thread_can_start_->WaitForNotification(); 463 param->runnable_->Run(); 464 return 0; 465 } 466 467 // Prohibit instantiation. 468 ThreadWithParamSupport(); 469 470 ThreadWithParamSupport(const ThreadWithParamSupport&) = delete; 471 ThreadWithParamSupport& operator=(const ThreadWithParamSupport&) = delete; 472}; 473 474} // namespace 475 476ThreadWithParamBase::ThreadWithParamBase(Runnable* runnable, 477 Notification* thread_can_start) 478 : thread_( 479 ThreadWithParamSupport::CreateThread(runnable, thread_can_start)) {} 480 481ThreadWithParamBase::~ThreadWithParamBase() { Join(); } 482 483void ThreadWithParamBase::Join() { 484 GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0) 485 << "Failed to join the thread with error " << ::GetLastError() << "."; 486} 487 488// Maps a thread to a set of ThreadIdToThreadLocals that have values 489// instantiated on that thread and notifies them when the thread exits. A 490// ThreadLocal instance is expected to persist until all threads it has 491// values on have terminated. 492class ThreadLocalRegistryImpl { 493 public: 494 // Registers thread_local_instance as having value on the current thread. 495 // Returns a value that can be used to identify the thread from other threads. 496 static ThreadLocalValueHolderBase* GetValueOnCurrentThread( 497 const ThreadLocalBase* thread_local_instance) { 498#ifdef _MSC_VER 499 MemoryIsNotDeallocated memory_is_not_deallocated; 500#endif // _MSC_VER 501 DWORD current_thread = ::GetCurrentThreadId(); 502 MutexLock lock(&mutex_); 503 ThreadIdToThreadLocals* const thread_to_thread_locals = 504 GetThreadLocalsMapLocked(); 505 ThreadIdToThreadLocals::iterator thread_local_pos = 506 thread_to_thread_locals->find(current_thread); 507 if (thread_local_pos == thread_to_thread_locals->end()) { 508 thread_local_pos = 509 thread_to_thread_locals 510 ->insert(std::make_pair(current_thread, ThreadLocalValues())) 511 .first; 512 StartWatcherThreadFor(current_thread); 513 } 514 ThreadLocalValues& thread_local_values = thread_local_pos->second; 515 ThreadLocalValues::iterator value_pos = 516 thread_local_values.find(thread_local_instance); 517 if (value_pos == thread_local_values.end()) { 518 value_pos = 519 thread_local_values 520 .insert(std::make_pair( 521 thread_local_instance, 522 std::shared_ptr<ThreadLocalValueHolderBase>( 523 thread_local_instance->NewValueForCurrentThread()))) 524 .first; 525 } 526 return value_pos->second.get(); 527 } 528 529 static void OnThreadLocalDestroyed( 530 const ThreadLocalBase* thread_local_instance) { 531 std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders; 532 // Clean up the ThreadLocalValues data structure while holding the lock, but 533 // defer the destruction of the ThreadLocalValueHolderBases. 534 { 535 MutexLock lock(&mutex_); 536 ThreadIdToThreadLocals* const thread_to_thread_locals = 537 GetThreadLocalsMapLocked(); 538 for (ThreadIdToThreadLocals::iterator it = 539 thread_to_thread_locals->begin(); 540 it != thread_to_thread_locals->end(); ++it) { 541 ThreadLocalValues& thread_local_values = it->second; 542 ThreadLocalValues::iterator value_pos = 543 thread_local_values.find(thread_local_instance); 544 if (value_pos != thread_local_values.end()) { 545 value_holders.push_back(value_pos->second); 546 thread_local_values.erase(value_pos); 547 // This 'if' can only be successful at most once, so theoretically we 548 // could break out of the loop here, but we don't bother doing so. 549 } 550 } 551 } 552 // Outside the lock, let the destructor for 'value_holders' deallocate the 553 // ThreadLocalValueHolderBases. 554 } 555 556 static void OnThreadExit(DWORD thread_id) { 557 GTEST_CHECK_(thread_id != 0) << ::GetLastError(); 558 std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders; 559 // Clean up the ThreadIdToThreadLocals data structure while holding the 560 // lock, but defer the destruction of the ThreadLocalValueHolderBases. 561 { 562 MutexLock lock(&mutex_); 563 ThreadIdToThreadLocals* const thread_to_thread_locals = 564 GetThreadLocalsMapLocked(); 565 ThreadIdToThreadLocals::iterator thread_local_pos = 566 thread_to_thread_locals->find(thread_id); 567 if (thread_local_pos != thread_to_thread_locals->end()) { 568 ThreadLocalValues& thread_local_values = thread_local_pos->second; 569 for (ThreadLocalValues::iterator value_pos = 570 thread_local_values.begin(); 571 value_pos != thread_local_values.end(); ++value_pos) { 572 value_holders.push_back(value_pos->second); 573 } 574 thread_to_thread_locals->erase(thread_local_pos); 575 } 576 } 577 // Outside the lock, let the destructor for 'value_holders' deallocate the 578 // ThreadLocalValueHolderBases. 579 } 580 581 private: 582 // In a particular thread, maps a ThreadLocal object to its value. 583 typedef std::map<const ThreadLocalBase*, 584 std::shared_ptr<ThreadLocalValueHolderBase> > 585 ThreadLocalValues; 586 // Stores all ThreadIdToThreadLocals having values in a thread, indexed by 587 // thread's ID. 588 typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals; 589 590 // Holds the thread id and thread handle that we pass from 591 // StartWatcherThreadFor to WatcherThreadFunc. 592 typedef std::pair<DWORD, HANDLE> ThreadIdAndHandle; 593 594 static void StartWatcherThreadFor(DWORD thread_id) { 595 // The returned handle will be kept in thread_map and closed by 596 // watcher_thread in WatcherThreadFunc. 597 HANDLE thread = 598 ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, thread_id); 599 GTEST_CHECK_(thread != nullptr); 600 // We need to pass a valid thread ID pointer into CreateThread for it 601 // to work correctly under Win98. 602 DWORD watcher_thread_id; 603 HANDLE watcher_thread = ::CreateThread( 604 nullptr, // Default security. 605 0, // Default stack size 606 &ThreadLocalRegistryImpl::WatcherThreadFunc, 607 reinterpret_cast<LPVOID>(new ThreadIdAndHandle(thread_id, thread)), 608 CREATE_SUSPENDED, &watcher_thread_id); 609 GTEST_CHECK_(watcher_thread != nullptr) 610 << "CreateThread failed with error " << ::GetLastError() << "."; 611 // Give the watcher thread the same priority as ours to avoid being 612 // blocked by it. 613 ::SetThreadPriority(watcher_thread, 614 ::GetThreadPriority(::GetCurrentThread())); 615 ::ResumeThread(watcher_thread); 616 ::CloseHandle(watcher_thread); 617 } 618 619 // Monitors exit from a given thread and notifies those 620 // ThreadIdToThreadLocals about thread termination. 621 static DWORD WINAPI WatcherThreadFunc(LPVOID param) { 622 const ThreadIdAndHandle* tah = 623 reinterpret_cast<const ThreadIdAndHandle*>(param); 624 GTEST_CHECK_(::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0); 625 OnThreadExit(tah->first); 626 ::CloseHandle(tah->second); 627 delete tah; 628 return 0; 629 } 630 631 // Returns map of thread local instances. 632 static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() { 633 mutex_.AssertHeld(); 634#ifdef _MSC_VER 635 MemoryIsNotDeallocated memory_is_not_deallocated; 636#endif // _MSC_VER 637 static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals(); 638 return map; 639 } 640 641 // Protects access to GetThreadLocalsMapLocked() and its return value. 642 static Mutex mutex_; 643 // Protects access to GetThreadMapLocked() and its return value. 644 static Mutex thread_map_mutex_; 645}; 646 647Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT 648Mutex ThreadLocalRegistryImpl::thread_map_mutex_( 649 Mutex::kStaticMutex); // NOLINT 650 651ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread( 652 const ThreadLocalBase* thread_local_instance) { 653 return ThreadLocalRegistryImpl::GetValueOnCurrentThread( 654 thread_local_instance); 655} 656 657void ThreadLocalRegistry::OnThreadLocalDestroyed( 658 const ThreadLocalBase* thread_local_instance) { 659 ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance); 660} 661 662#endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS 663 664#ifdef GTEST_USES_POSIX_RE 665 666// Implements RE. Currently only needed for death tests. 667 668RE::~RE() { 669 if (is_valid_) { 670 // regfree'ing an invalid regex might crash because the content 671 // of the regex is undefined. Since the regex's are essentially 672 // the same, one cannot be valid (or invalid) without the other 673 // being so too. 674 regfree(&partial_regex_); 675 regfree(&full_regex_); 676 } 677} 678 679// Returns true if and only if regular expression re matches the entire str. 680bool RE::FullMatch(const char* str, const RE& re) { 681 if (!re.is_valid_) return false; 682 683 regmatch_t match; 684 return regexec(&re.full_regex_, str, 1, &match, 0) == 0; 685} 686 687// Returns true if and only if regular expression re matches a substring of 688// str (including str itself). 689bool RE::PartialMatch(const char* str, const RE& re) { 690 if (!re.is_valid_) return false; 691 692 regmatch_t match; 693 return regexec(&re.partial_regex_, str, 1, &match, 0) == 0; 694} 695 696// Initializes an RE from its string representation. 697void RE::Init(const char* regex) { 698 pattern_ = regex; 699 700 // Reserves enough bytes to hold the regular expression used for a 701 // full match. 702 const size_t full_regex_len = strlen(regex) + 10; 703 char* const full_pattern = new char[full_regex_len]; 704 705 snprintf(full_pattern, full_regex_len, "^(%s)$", regex); 706 is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; 707 // We want to call regcomp(&partial_regex_, ...) even if the 708 // previous expression returns false. Otherwise partial_regex_ may 709 // not be properly initialized can may cause trouble when it's 710 // freed. 711 // 712 // Some implementation of POSIX regex (e.g. on at least some 713 // versions of Cygwin) doesn't accept the empty string as a valid 714 // regex. We change it to an equivalent form "()" to be safe. 715 if (is_valid_) { 716 const char* const partial_regex = (*regex == '\0') ? "()" : regex; 717 is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0; 718 } 719 EXPECT_TRUE(is_valid_) 720 << "Regular expression \"" << regex 721 << "\" is not a valid POSIX Extended regular expression."; 722 723 delete[] full_pattern; 724} 725 726#elif defined(GTEST_USES_SIMPLE_RE) 727 728// Returns true if and only if ch appears anywhere in str (excluding the 729// terminating '\0' character). 730bool IsInSet(char ch, const char* str) { 731 return ch != '\0' && strchr(str, ch) != nullptr; 732} 733 734// Returns true if and only if ch belongs to the given classification. 735// Unlike similar functions in <ctype.h>, these aren't affected by the 736// current locale. 737bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; } 738bool IsAsciiPunct(char ch) { 739 return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"); 740} 741bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); } 742bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); } 743bool IsAsciiWordChar(char ch) { 744 return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || 745 ('0' <= ch && ch <= '9') || ch == '_'; 746} 747 748// Returns true if and only if "\\c" is a supported escape sequence. 749bool IsValidEscape(char c) { 750 return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW")); 751} 752 753// Returns true if and only if the given atom (specified by escaped and 754// pattern) matches ch. The result is undefined if the atom is invalid. 755bool AtomMatchesChar(bool escaped, char pattern_char, char ch) { 756 if (escaped) { // "\\p" where p is pattern_char. 757 switch (pattern_char) { 758 case 'd': 759 return IsAsciiDigit(ch); 760 case 'D': 761 return !IsAsciiDigit(ch); 762 case 'f': 763 return ch == '\f'; 764 case 'n': 765 return ch == '\n'; 766 case 'r': 767 return ch == '\r'; 768 case 's': 769 return IsAsciiWhiteSpace(ch); 770 case 'S': 771 return !IsAsciiWhiteSpace(ch); 772 case 't': 773 return ch == '\t'; 774 case 'v': 775 return ch == '\v'; 776 case 'w': 777 return IsAsciiWordChar(ch); 778 case 'W': 779 return !IsAsciiWordChar(ch); 780 } 781 return IsAsciiPunct(pattern_char) && pattern_char == ch; 782 } 783 784 return (pattern_char == '.' && ch != '\n') || pattern_char == ch; 785} 786 787// Helper function used by ValidateRegex() to format error messages. 788static std::string FormatRegexSyntaxError(const char* regex, int index) { 789 return (Message() << "Syntax error at index " << index 790 << " in simple regular expression \"" << regex << "\": ") 791 .GetString(); 792} 793 794// Generates non-fatal failures and returns false if regex is invalid; 795// otherwise returns true. 796bool ValidateRegex(const char* regex) { 797 if (regex == nullptr) { 798 ADD_FAILURE() << "NULL is not a valid simple regular expression."; 799 return false; 800 } 801 802 bool is_valid = true; 803 804 // True if and only if ?, *, or + can follow the previous atom. 805 bool prev_repeatable = false; 806 for (int i = 0; regex[i]; i++) { 807 if (regex[i] == '\\') { // An escape sequence 808 i++; 809 if (regex[i] == '\0') { 810 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) 811 << "'\\' cannot appear at the end."; 812 return false; 813 } 814 815 if (!IsValidEscape(regex[i])) { 816 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) 817 << "invalid escape sequence \"\\" << regex[i] << "\"."; 818 is_valid = false; 819 } 820 prev_repeatable = true; 821 } else { // Not an escape sequence. 822 const char ch = regex[i]; 823 824 if (ch == '^' && i > 0) { 825 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) 826 << "'^' can only appear at the beginning."; 827 is_valid = false; 828 } else if (ch == '$' && regex[i + 1] != '\0') { 829 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) 830 << "'$' can only appear at the end."; 831 is_valid = false; 832 } else if (IsInSet(ch, "()[]{}|")) { 833 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch 834 << "' is unsupported."; 835 is_valid = false; 836 } else if (IsRepeat(ch) && !prev_repeatable) { 837 ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch 838 << "' can only follow a repeatable token."; 839 is_valid = false; 840 } 841 842 prev_repeatable = !IsInSet(ch, "^$?*+"); 843 } 844 } 845 846 return is_valid; 847} 848 849// Matches a repeated regex atom followed by a valid simple regular 850// expression. The regex atom is defined as c if escaped is false, 851// or \c otherwise. repeat is the repetition meta character (?, *, 852// or +). The behavior is undefined if str contains too many 853// characters to be indexable by size_t, in which case the test will 854// probably time out anyway. We are fine with this limitation as 855// std::string has it too. 856bool MatchRepetitionAndRegexAtHead(bool escaped, char c, char repeat, 857 const char* regex, const char* str) { 858 const size_t min_count = (repeat == '+') ? 1 : 0; 859 const size_t max_count = (repeat == '?') ? 1 : static_cast<size_t>(-1) - 1; 860 // We cannot call numeric_limits::max() as it conflicts with the 861 // max() macro on Windows. 862 863 for (size_t i = 0; i <= max_count; ++i) { 864 // We know that the atom matches each of the first i characters in str. 865 if (i >= min_count && MatchRegexAtHead(regex, str + i)) { 866 // We have enough matches at the head, and the tail matches too. 867 // Since we only care about *whether* the pattern matches str 868 // (as opposed to *how* it matches), there is no need to find a 869 // greedy match. 870 return true; 871 } 872 if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i])) return false; 873 } 874 return false; 875} 876 877// Returns true if and only if regex matches a prefix of str. regex must 878// be a valid simple regular expression and not start with "^", or the 879// result is undefined. 880bool MatchRegexAtHead(const char* regex, const char* str) { 881 if (*regex == '\0') // An empty regex matches a prefix of anything. 882 return true; 883 884 // "$" only matches the end of a string. Note that regex being 885 // valid guarantees that there's nothing after "$" in it. 886 if (*regex == '$') return *str == '\0'; 887 888 // Is the first thing in regex an escape sequence? 889 const bool escaped = *regex == '\\'; 890 if (escaped) ++regex; 891 if (IsRepeat(regex[1])) { 892 // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so 893 // here's an indirect recursion. It terminates as the regex gets 894 // shorter in each recursion. 895 return MatchRepetitionAndRegexAtHead(escaped, regex[0], regex[1], regex + 2, 896 str); 897 } else { 898 // regex isn't empty, isn't "$", and doesn't start with a 899 // repetition. We match the first atom of regex with the first 900 // character of str and recurse. 901 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) && 902 MatchRegexAtHead(regex + 1, str + 1); 903 } 904} 905 906// Returns true if and only if regex matches any substring of str. regex must 907// be a valid simple regular expression, or the result is undefined. 908// 909// The algorithm is recursive, but the recursion depth doesn't exceed 910// the regex length, so we won't need to worry about running out of 911// stack space normally. In rare cases the time complexity can be 912// exponential with respect to the regex length + the string length, 913// but usually it's must faster (often close to linear). 914bool MatchRegexAnywhere(const char* regex, const char* str) { 915 if (regex == nullptr || str == nullptr) return false; 916 917 if (*regex == '^') return MatchRegexAtHead(regex + 1, str); 918 919 // A successful match can be anywhere in str. 920 do { 921 if (MatchRegexAtHead(regex, str)) return true; 922 } while (*str++ != '\0'); 923 return false; 924} 925 926// Implements the RE class. 927 928RE::~RE() = default; 929 930// Returns true if and only if regular expression re matches the entire str. 931bool RE::FullMatch(const char* str, const RE& re) { 932 return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_.c_str(), str); 933} 934 935// Returns true if and only if regular expression re matches a substring of 936// str (including str itself). 937bool RE::PartialMatch(const char* str, const RE& re) { 938 return re.is_valid_ && MatchRegexAnywhere(re.pattern_.c_str(), str); 939} 940 941// Initializes an RE from its string representation. 942void RE::Init(const char* regex) { 943 full_pattern_.clear(); 944 pattern_.clear(); 945 946 if (regex != nullptr) { 947 pattern_ = regex; 948 } 949 950 is_valid_ = ValidateRegex(regex); 951 if (!is_valid_) { 952 // No need to calculate the full pattern when the regex is invalid. 953 return; 954 } 955 956 // Reserves enough bytes to hold the regular expression used for a 957 // full match: we need space to prepend a '^' and append a '$'. 958 full_pattern_.reserve(pattern_.size() + 2); 959 960 if (pattern_.empty() || pattern_.front() != '^') { 961 full_pattern_.push_back('^'); // Makes sure full_pattern_ starts with '^'. 962 } 963 964 full_pattern_.append(pattern_); 965 966 if (pattern_.empty() || pattern_.back() != '$') { 967 full_pattern_.push_back('$'); // Makes sure full_pattern_ ends with '$'. 968 } 969} 970 971#endif // GTEST_USES_POSIX_RE 972 973const char kUnknownFile[] = "unknown file"; 974 975// Formats a source file path and a line number as they would appear 976// in an error message from the compiler used to compile this code. 977GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) { 978 const std::string file_name(file == nullptr ? kUnknownFile : file); 979 980 if (line < 0) { 981 return file_name + ":"; 982 } 983#ifdef _MSC_VER 984 return file_name + "(" + StreamableToString(line) + "):"; 985#else 986 return file_name + ":" + StreamableToString(line) + ":"; 987#endif // _MSC_VER 988} 989 990// Formats a file location for compiler-independent XML output. 991// Although this function is not platform dependent, we put it next to 992// FormatFileLocation in order to contrast the two functions. 993// Note that FormatCompilerIndependentFileLocation() does NOT append colon 994// to the file location it produces, unlike FormatFileLocation(). 995GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, 996 int line) { 997 const std::string file_name(file == nullptr ? kUnknownFile : file); 998 999 if (line < 0) 1000 return file_name; 1001 else 1002 return file_name + ":" + StreamableToString(line); 1003} 1004 1005GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line) 1006 : severity_(severity) { 1007 const char* const marker = severity == GTEST_INFO ? "[ INFO ]" 1008 : severity == GTEST_WARNING ? "[WARNING]" 1009 : severity == GTEST_ERROR ? "[ ERROR ]" 1010 : "[ FATAL ]"; 1011 GetStream() << ::std::endl 1012 << marker << " " << FormatFileLocation(file, line).c_str() 1013 << ": "; 1014} 1015 1016// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 1017GTestLog::~GTestLog() { 1018 GetStream() << ::std::endl; 1019 if (severity_ == GTEST_FATAL) { 1020 fflush(stderr); 1021 posix::Abort(); 1022 } 1023} 1024 1025// Disable Microsoft deprecation warnings for POSIX functions called from 1026// this class (creat, dup, dup2, and close) 1027GTEST_DISABLE_MSC_DEPRECATED_PUSH_() 1028 1029#if GTEST_HAS_STREAM_REDIRECTION 1030 1031// Object that captures an output stream (stdout/stderr). 1032class CapturedStream { 1033 public: 1034 // The ctor redirects the stream to a temporary file. 1035 explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { 1036#ifdef GTEST_OS_WINDOWS 1037 char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT 1038 char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT 1039 1040 ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path); 1041 const UINT success = ::GetTempFileNameA(temp_dir_path, "gtest_redir", 1042 0, // Generate unique file name. 1043 temp_file_path); 1044 GTEST_CHECK_(success != 0) 1045 << "Unable to create a temporary file in " << temp_dir_path; 1046 const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE); 1047 GTEST_CHECK_(captured_fd != -1) 1048 << "Unable to open temporary file " << temp_file_path; 1049 filename_ = temp_file_path; 1050#else 1051 // There's no guarantee that a test has write access to the current 1052 // directory, so we create the temporary file in a temporary directory. 1053 std::string name_template; 1054 1055#ifdef GTEST_OS_LINUX_ANDROID 1056 // Note: Android applications are expected to call the framework's 1057 // Context.getExternalStorageDirectory() method through JNI to get 1058 // the location of the world-writable SD Card directory. However, 1059 // this requires a Context handle, which cannot be retrieved 1060 // globally from native code. Doing so also precludes running the 1061 // code as part of a regular standalone executable, which doesn't 1062 // run in a Dalvik process (e.g. when running it through 'adb shell'). 1063 // 1064 // The location /data/local/tmp is directly accessible from native code. 1065 // '/sdcard' and other variants cannot be relied on, as they are not 1066 // guaranteed to be mounted, or may have a delay in mounting. 1067 name_template = "/data/local/tmp/"; 1068#elif defined(GTEST_OS_IOS) 1069 char user_temp_dir[PATH_MAX + 1]; 1070 1071 // Documented alternative to NSTemporaryDirectory() (for obtaining creating 1072 // a temporary directory) at 1073 // https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10 1074 // 1075 // _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not 1076 // documented in the confstr() man page at 1077 // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr 1078 // but are still available, according to the WebKit patches at 1079 // https://trac.webkit.org/changeset/262004/webkit 1080 // https://trac.webkit.org/changeset/263705/webkit 1081 // 1082 // The confstr() implementation falls back to getenv("TMPDIR"). See 1083 // https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html 1084 ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir)); 1085 1086 name_template = user_temp_dir; 1087 if (name_template.back() != GTEST_PATH_SEP_[0]) 1088 name_template.push_back(GTEST_PATH_SEP_[0]); 1089#else 1090 name_template = "/tmp/"; 1091#endif 1092 name_template.append("gtest_captured_stream.XXXXXX"); 1093 1094 // mkstemp() modifies the string bytes in place, and does not go beyond the 1095 // string's length. This results in well-defined behavior in C++17. 1096 // 1097 // The const_cast is needed below C++17. The constraints on std::string 1098 // implementations in C++11 and above make assumption behind the const_cast 1099 // fairly safe. 1100 const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data())); 1101 if (captured_fd == -1) { 1102 GTEST_LOG_(WARNING) 1103 << "Failed to create tmp file " << name_template 1104 << " for test; does the test have access to the /tmp directory?"; 1105 } 1106 filename_ = std::move(name_template); 1107#endif // GTEST_OS_WINDOWS 1108 fflush(nullptr); 1109 dup2(captured_fd, fd_); 1110 close(captured_fd); 1111 } 1112 1113 ~CapturedStream() { remove(filename_.c_str()); } 1114 1115 std::string GetCapturedString() { 1116 if (uncaptured_fd_ != -1) { 1117 // Restores the original stream. 1118 fflush(nullptr); 1119 dup2(uncaptured_fd_, fd_); 1120 close(uncaptured_fd_); 1121 uncaptured_fd_ = -1; 1122 } 1123 1124 FILE* const file = posix::FOpen(filename_.c_str(), "r"); 1125 if (file == nullptr) { 1126 GTEST_LOG_(FATAL) << "Failed to open tmp file " << filename_ 1127 << " for capturing stream."; 1128 } 1129 const std::string content = ReadEntireFile(file); 1130 posix::FClose(file); 1131 return content; 1132 } 1133 1134 private: 1135 const int fd_; // A stream to capture. 1136 int uncaptured_fd_; 1137 // Name of the temporary file holding the stderr output. 1138 ::std::string filename_; 1139 1140 CapturedStream(const CapturedStream&) = delete; 1141 CapturedStream& operator=(const CapturedStream&) = delete; 1142}; 1143 1144GTEST_DISABLE_MSC_DEPRECATED_POP_() 1145 1146static CapturedStream* g_captured_stderr = nullptr; 1147static CapturedStream* g_captured_stdout = nullptr; 1148 1149// Starts capturing an output stream (stdout/stderr). 1150static void CaptureStream(int fd, const char* stream_name, 1151 CapturedStream** stream) { 1152 if (*stream != nullptr) { 1153 GTEST_LOG_(FATAL) << "Only one " << stream_name 1154 << " capturer can exist at a time."; 1155 } 1156 *stream = new CapturedStream(fd); 1157} 1158 1159// Stops capturing the output stream and returns the captured string. 1160static std::string GetCapturedStream(CapturedStream** captured_stream) { 1161 const std::string content = (*captured_stream)->GetCapturedString(); 1162 1163 delete *captured_stream; 1164 *captured_stream = nullptr; 1165 1166 return content; 1167} 1168 1169#if defined(_MSC_VER) || defined(__BORLANDC__) 1170// MSVC and C++Builder do not provide a definition of STDERR_FILENO. 1171const int kStdOutFileno = 1; 1172const int kStdErrFileno = 2; 1173#else 1174const int kStdOutFileno = STDOUT_FILENO; 1175const int kStdErrFileno = STDERR_FILENO; 1176#endif // defined(_MSC_VER) || defined(__BORLANDC__) 1177 1178// Starts capturing stdout. 1179void CaptureStdout() { 1180 CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout); 1181} 1182 1183// Starts capturing stderr. 1184void CaptureStderr() { 1185 CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr); 1186} 1187 1188// Stops capturing stdout and returns the captured string. 1189std::string GetCapturedStdout() { 1190 return GetCapturedStream(&g_captured_stdout); 1191} 1192 1193// Stops capturing stderr and returns the captured string. 1194std::string GetCapturedStderr() { 1195 return GetCapturedStream(&g_captured_stderr); 1196} 1197 1198#endif // GTEST_HAS_STREAM_REDIRECTION 1199 1200size_t GetFileSize(FILE* file) { 1201 fseek(file, 0, SEEK_END); 1202 return static_cast<size_t>(ftell(file)); 1203} 1204 1205std::string ReadEntireFile(FILE* file) { 1206 const size_t file_size = GetFileSize(file); 1207 char* const buffer = new char[file_size]; 1208 1209 size_t bytes_last_read = 0; // # of bytes read in the last fread() 1210 size_t bytes_read = 0; // # of bytes read so far 1211 1212 fseek(file, 0, SEEK_SET); 1213 1214 // Keeps reading the file until we cannot read further or the 1215 // pre-determined file size is reached. 1216 do { 1217 bytes_last_read = 1218 fread(buffer + bytes_read, 1, file_size - bytes_read, file); 1219 bytes_read += bytes_last_read; 1220 } while (bytes_last_read > 0 && bytes_read < file_size); 1221 1222 const std::string content(buffer, bytes_read); 1223 delete[] buffer; 1224 1225 return content; 1226} 1227 1228#ifdef GTEST_HAS_DEATH_TEST 1229static const std::vector<std::string>* g_injected_test_argvs = 1230 nullptr; // Owned. 1231 1232std::vector<std::string> GetInjectableArgvs() { 1233 if (g_injected_test_argvs != nullptr) { 1234 return *g_injected_test_argvs; 1235 } 1236 return GetArgvs(); 1237} 1238 1239void SetInjectableArgvs(const std::vector<std::string>* new_argvs) { 1240 if (g_injected_test_argvs != new_argvs) delete g_injected_test_argvs; 1241 g_injected_test_argvs = new_argvs; 1242} 1243 1244void SetInjectableArgvs(const std::vector<std::string>& new_argvs) { 1245 SetInjectableArgvs( 1246 new std::vector<std::string>(new_argvs.begin(), new_argvs.end())); 1247} 1248 1249void ClearInjectableArgvs() { 1250 delete g_injected_test_argvs; 1251 g_injected_test_argvs = nullptr; 1252} 1253#endif // GTEST_HAS_DEATH_TEST 1254 1255#ifdef GTEST_OS_WINDOWS_MOBILE 1256namespace posix { 1257void Abort() { 1258 DebugBreak(); 1259 TerminateProcess(GetCurrentProcess(), 1); 1260} 1261} // namespace posix 1262#endif // GTEST_OS_WINDOWS_MOBILE 1263 1264// Returns the name of the environment variable corresponding to the 1265// given flag. For example, FlagToEnvVar("foo") will return 1266// "GTEST_FOO" in the open-source version. 1267static std::string FlagToEnvVar(const char* flag) { 1268 const std::string full_flag = 1269 (Message() << GTEST_FLAG_PREFIX_ << flag).GetString(); 1270 1271 Message env_var; 1272 for (size_t i = 0; i != full_flag.length(); i++) { 1273 env_var << ToUpper(full_flag.c_str()[i]); 1274 } 1275 1276 return env_var.GetString(); 1277} 1278 1279// Parses 'str' for a 32-bit signed integer. If successful, writes 1280// the result to *value and returns true; otherwise leaves *value 1281// unchanged and returns false. 1282bool ParseInt32(const Message& src_text, const char* str, int32_t* value) { 1283 // Parses the environment variable as a decimal integer. 1284 char* end = nullptr; 1285 const long long_value = strtol(str, &end, 10); // NOLINT 1286 1287 // Has strtol() consumed all characters in the string? 1288 if (*end != '\0') { 1289 // No - an invalid character was encountered. 1290 Message msg; 1291 msg << "WARNING: " << src_text 1292 << " is expected to be a 32-bit integer, but actually" 1293 << " has value \"" << str << "\".\n"; 1294 printf("%s", msg.GetString().c_str()); 1295 fflush(stdout); 1296 return false; 1297 } 1298 1299 // Is the parsed value in the range of an int32_t? 1300 const auto result = static_cast<int32_t>(long_value); 1301 if (long_value == LONG_MAX || long_value == LONG_MIN || 1302 // The parsed value overflows as a long. (strtol() returns 1303 // LONG_MAX or LONG_MIN when the input overflows.) 1304 result != long_value 1305 // The parsed value overflows as an int32_t. 1306 ) { 1307 Message msg; 1308 msg << "WARNING: " << src_text 1309 << " is expected to be a 32-bit integer, but actually" 1310 << " has value " << str << ", which overflows.\n"; 1311 printf("%s", msg.GetString().c_str()); 1312 fflush(stdout); 1313 return false; 1314 } 1315 1316 *value = result; 1317 return true; 1318} 1319 1320// Reads and returns the Boolean environment variable corresponding to 1321// the given flag; if it's not set, returns default_value. 1322// 1323// The value is considered true if and only if it's not "0". 1324bool BoolFromGTestEnv(const char* flag, bool default_value) { 1325#if defined(GTEST_GET_BOOL_FROM_ENV_) 1326 return GTEST_GET_BOOL_FROM_ENV_(flag, default_value); 1327#else 1328 const std::string env_var = FlagToEnvVar(flag); 1329 const char* const string_value = posix::GetEnv(env_var.c_str()); 1330 return string_value == nullptr ? default_value 1331 : strcmp(string_value, "0") != 0; 1332#endif // defined(GTEST_GET_BOOL_FROM_ENV_) 1333} 1334 1335// Reads and returns a 32-bit integer stored in the environment 1336// variable corresponding to the given flag; if it isn't set or 1337// doesn't represent a valid 32-bit integer, returns default_value. 1338int32_t Int32FromGTestEnv(const char* flag, int32_t default_value) { 1339#if defined(GTEST_GET_INT32_FROM_ENV_) 1340 return GTEST_GET_INT32_FROM_ENV_(flag, default_value); 1341#else 1342 const std::string env_var = FlagToEnvVar(flag); 1343 const char* const string_value = posix::GetEnv(env_var.c_str()); 1344 if (string_value == nullptr) { 1345 // The environment variable is not set. 1346 return default_value; 1347 } 1348 1349 int32_t result = default_value; 1350 if (!ParseInt32(Message() << "Environment variable " << env_var, string_value, 1351 &result)) { 1352 printf("The default value %s is used.\n", 1353 (Message() << default_value).GetString().c_str()); 1354 fflush(stdout); 1355 return default_value; 1356 } 1357 1358 return result; 1359#endif // defined(GTEST_GET_INT32_FROM_ENV_) 1360} 1361 1362// As a special case for the 'output' flag, if GTEST_OUTPUT is not 1363// set, we look for XML_OUTPUT_FILE, which is set by the Bazel build 1364// system. The value of XML_OUTPUT_FILE is a filename without the 1365// "xml:" prefix of GTEST_OUTPUT. 1366// Note that this is meant to be called at the call site so it does 1367// not check that the flag is 'output' 1368// In essence this checks an env variable called XML_OUTPUT_FILE 1369// and if it is set we prepend "xml:" to its value, if it not set we return "" 1370std::string OutputFlagAlsoCheckEnvVar() { 1371 std::string default_value_for_output_flag = ""; 1372 const char* xml_output_file_env = posix::GetEnv("XML_OUTPUT_FILE"); 1373 if (nullptr != xml_output_file_env) { 1374 default_value_for_output_flag = std::string("xml:") + xml_output_file_env; 1375 } 1376 return default_value_for_output_flag; 1377} 1378 1379// Reads and returns the string environment variable corresponding to 1380// the given flag; if it's not set, returns default_value. 1381const char* StringFromGTestEnv(const char* flag, const char* default_value) { 1382#if defined(GTEST_GET_STRING_FROM_ENV_) 1383 return GTEST_GET_STRING_FROM_ENV_(flag, default_value); 1384#else 1385 const std::string env_var = FlagToEnvVar(flag); 1386 const char* const value = posix::GetEnv(env_var.c_str()); 1387 return value == nullptr ? default_value : value; 1388#endif // defined(GTEST_GET_STRING_FROM_ENV_) 1389} 1390 1391} // namespace internal 1392} // namespace testing 1393