1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #include "gtest/internal/gtest-port.h"
43 # include <sys/stat.h>
44 # include <map> // Used in ThreadLocal.
50 #endif // GTEST_OS_WINDOWS
53 # include <mach/mach_init.h>
54 # include <mach/task.h>
55 # include <mach/vm_map.h>
56 #endif // GTEST_OS_MAC
58 #if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
59 GTEST_OS_NETBSD || GTEST_OS_OPENBSD
60 # include <sys/sysctl.h>
61 # if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
62 # include <sys/user.h>
69 # include <sys/procfs.h>
70 #endif // GTEST_OS_QNX
73 # include <procinfo.h>
74 # include <sys/types.h>
75 #endif // GTEST_OS_AIX
78 # include <zircon/process.h>
79 # include <zircon/syscalls.h>
80 #endif // GTEST_OS_FUCHSIA
82 #include "gtest/gtest-spi.h"
83 #include "gtest/gtest-message.h"
84 #include "gtest/internal/gtest-internal.h"
85 #include "gtest/internal/gtest-string.h"
86 #include "src/gtest-internal-inl.h"
91 #if defined(_MSC_VER) || defined(__BORLANDC__)
92 // MSVC and C++Builder do not provide a definition of STDERR_FILENO.
93 const int kStdOutFileno
= 1;
94 const int kStdErrFileno
= 2;
96 const int kStdOutFileno
= STDOUT_FILENO
;
97 const int kStdErrFileno
= STDERR_FILENO
;
103 template <typename T
>
104 T
ReadProcFileField(const std::string
& filename
, int field
) {
106 std::ifstream
file(filename
.c_str());
107 while (field
-- > 0) {
116 // Returns the number of active threads, or 0 when there is an error.
117 size_t GetThreadCount() {
118 const std::string filename
=
119 (Message() << "/proc/" << getpid() << "/stat").GetString();
120 return ReadProcFileField
<size_t>(filename
, 19);
125 size_t GetThreadCount() {
126 const task_t task
= mach_task_self();
127 mach_msg_type_number_t thread_count
;
128 thread_act_array_t thread_list
;
129 const kern_return_t status
= task_threads(task
, &thread_list
, &thread_count
);
130 if (status
== KERN_SUCCESS
) {
131 // task_threads allocates resources in thread_list and we need to free them
134 reinterpret_cast<vm_address_t
>(thread_list
),
135 sizeof(thread_t
) * thread_count
);
136 return static_cast<size_t>(thread_count
);
142 #elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
147 #define KERN_PROC KERN_PROC2
148 #define kinfo_proc kinfo_proc2
151 #if GTEST_OS_DRAGONFLY
152 #define KP_NLWP(kp) (kp.kp_nthreads)
153 #elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
154 #define KP_NLWP(kp) (kp.ki_numthreads)
155 #elif GTEST_OS_NETBSD
156 #define KP_NLWP(kp) (kp.p_nlwps)
159 // Returns the number of threads running in the process, or 0 to indicate that
160 // we cannot detect it.
161 size_t GetThreadCount() {
168 sizeof(struct kinfo_proc
),
172 u_int miblen
= sizeof(mib
) / sizeof(mib
[0]);
173 struct kinfo_proc info
;
174 size_t size
= sizeof(info
);
175 if (sysctl(mib
, miblen
, &info
, &size
, NULL
, 0)) {
178 return static_cast<size_t>(KP_NLWP(info
));
180 #elif GTEST_OS_OPENBSD
182 // Returns the number of threads running in the process, or 0 to indicate that
183 // we cannot detect it.
184 size_t GetThreadCount() {
188 KERN_PROC_PID
| KERN_PROC_SHOW_THREADS
,
190 sizeof(struct kinfo_proc
),
193 u_int miblen
= sizeof(mib
) / sizeof(mib
[0]);
195 // get number of structs
197 if (sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0)) {
200 mib
[5] = size
/ mib
[4];
202 // populate array of structs
203 struct kinfo_proc info
[mib
[5]];
204 if (sysctl(mib
, miblen
, &info
, &size
, NULL
, 0)) {
208 // exclude empty members
210 for (int i
= 0; i
< size
/ mib
[4]; i
++) {
211 if (info
[i
].p_tid
!= -1)
219 // Returns the number of threads running in the process, or 0 to indicate that
220 // we cannot detect it.
221 size_t GetThreadCount() {
222 const int fd
= open("/proc/self/as", O_RDONLY
);
226 procfs_info process_info
;
228 devctl(fd
, DCMD_PROC_INFO
, &process_info
, sizeof(process_info
), nullptr);
231 return static_cast<size_t>(process_info
.num_threads
);
239 size_t GetThreadCount() {
240 struct procentry64 entry
;
241 pid_t pid
= getpid();
242 int status
= getprocs64(&entry
, sizeof(entry
), nullptr, 0, &pid
, 1);
244 return entry
.pi_thcount
;
250 #elif GTEST_OS_FUCHSIA
252 size_t GetThreadCount() {
255 zx_status_t status
= zx_object_get_info(
257 ZX_INFO_PROCESS_THREADS
,
262 if (status
== ZX_OK
) {
271 size_t GetThreadCount() {
272 // There's no portable way to detect the number of threads, so we just
273 // return 0 to indicate that we cannot detect it.
277 #endif // GTEST_OS_LINUX
279 #if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
281 void SleepMilliseconds(int n
) {
282 ::Sleep(static_cast<DWORD
>(n
));
285 AutoHandle::AutoHandle()
286 : handle_(INVALID_HANDLE_VALUE
) {}
288 AutoHandle::AutoHandle(Handle handle
)
291 AutoHandle::~AutoHandle() {
295 AutoHandle::Handle
AutoHandle::Get() const {
299 void AutoHandle::Reset() {
300 Reset(INVALID_HANDLE_VALUE
);
303 void AutoHandle::Reset(HANDLE handle
) {
304 // Resetting with the same handle we already own is invalid.
305 if (handle_
!= handle
) {
307 ::CloseHandle(handle_
);
311 GTEST_CHECK_(!IsCloseable())
312 << "Resetting a valid handle to itself is likely a programmer error "
313 "and thus not allowed.";
317 bool AutoHandle::IsCloseable() const {
318 // Different Windows APIs may use either of these values to represent an
320 return handle_
!= nullptr && handle_
!= INVALID_HANDLE_VALUE
;
323 Notification::Notification()
324 : event_(::CreateEvent(nullptr, // Default security attributes.
325 TRUE
, // Do not reset automatically.
326 FALSE
, // Initially unset.
327 nullptr)) { // Anonymous event.
328 GTEST_CHECK_(event_
.Get() != nullptr);
331 void Notification::Notify() {
332 GTEST_CHECK_(::SetEvent(event_
.Get()) != FALSE
);
335 void Notification::WaitForNotification() {
337 ::WaitForSingleObject(event_
.Get(), INFINITE
) == WAIT_OBJECT_0
);
341 : owner_thread_id_(0),
343 critical_section_init_phase_(0),
344 critical_section_(new CRITICAL_SECTION
) {
345 ::InitializeCriticalSection(critical_section_
);
349 // Static mutexes are leaked intentionally. It is not thread-safe to try
351 if (type_
== kDynamic
) {
352 ::DeleteCriticalSection(critical_section_
);
353 delete critical_section_
;
354 critical_section_
= nullptr;
359 ThreadSafeLazyInit();
360 ::EnterCriticalSection(critical_section_
);
361 owner_thread_id_
= ::GetCurrentThreadId();
364 void Mutex::Unlock() {
365 ThreadSafeLazyInit();
366 // We don't protect writing to owner_thread_id_ here, as it's the
367 // caller's responsibility to ensure that the current thread holds the
368 // mutex when this is called.
369 owner_thread_id_
= 0;
370 ::LeaveCriticalSection(critical_section_
);
373 // Does nothing if the current thread holds the mutex. Otherwise, crashes
374 // with high probability.
375 void Mutex::AssertHeld() {
376 ThreadSafeLazyInit();
377 GTEST_CHECK_(owner_thread_id_
== ::GetCurrentThreadId())
378 << "The current thread is not holding the mutex @" << this;
384 // Use the RAII idiom to flag mem allocs that are intentionally never
385 // deallocated. The motivation is to silence the false positive mem leaks
386 // that are reported by the debug version of MS's CRT which can only detect
387 // if an alloc is missing a matching deallocation.
389 // MemoryIsNotDeallocated memory_is_not_deallocated;
390 // critical_section_ = new CRITICAL_SECTION;
392 class MemoryIsNotDeallocated
395 MemoryIsNotDeallocated() : old_crtdbg_flag_(0) {
396 old_crtdbg_flag_
= _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
);
397 // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT
398 // doesn't report mem leak if there's no matching deallocation.
399 _CrtSetDbgFlag(old_crtdbg_flag_
& ~_CRTDBG_ALLOC_MEM_DF
);
402 ~MemoryIsNotDeallocated() {
403 // Restore the original _CRTDBG_ALLOC_MEM_DF flag
404 _CrtSetDbgFlag(old_crtdbg_flag_
);
408 int old_crtdbg_flag_
;
410 GTEST_DISALLOW_COPY_AND_ASSIGN_(MemoryIsNotDeallocated
);
416 // Initializes owner_thread_id_ and critical_section_ in static mutexes.
417 void Mutex::ThreadSafeLazyInit() {
418 // Dynamic mutexes are initialized in the constructor.
419 if (type_
== kStatic
) {
421 ::InterlockedCompareExchange(&critical_section_init_phase_
, 1L, 0L)) {
423 // If critical_section_init_phase_ was 0 before the exchange, we
424 // are the first to test it and need to perform the initialization.
425 owner_thread_id_
= 0;
427 // Use RAII to flag that following mem alloc is never deallocated.
429 MemoryIsNotDeallocated memory_is_not_deallocated
;
431 critical_section_
= new CRITICAL_SECTION
;
433 ::InitializeCriticalSection(critical_section_
);
434 // Updates the critical_section_init_phase_ to 2 to signal
435 // initialization complete.
436 GTEST_CHECK_(::InterlockedCompareExchange(
437 &critical_section_init_phase_
, 2L, 1L) ==
441 // Somebody else is already initializing the mutex; spin until they
443 while (::InterlockedCompareExchange(&critical_section_init_phase_
,
446 // Possibly yields the rest of the thread's time slice to other
453 break; // The mutex is already initialized and ready for use.
457 << "Unexpected value of critical_section_init_phase_ "
458 << "while initializing a static mutex.";
465 class ThreadWithParamSupport
: public ThreadWithParamBase
{
467 static HANDLE
CreateThread(Runnable
* runnable
,
468 Notification
* thread_can_start
) {
469 ThreadMainParam
* param
= new ThreadMainParam(runnable
, thread_can_start
);
471 HANDLE thread_handle
= ::CreateThread(
472 nullptr, // Default security.
473 0, // Default stack size.
474 &ThreadWithParamSupport::ThreadMain
,
475 param
, // Parameter to ThreadMainStatic
476 0x0, // Default creation flags.
477 &thread_id
); // Need a valid pointer for the call to work under Win98.
478 GTEST_CHECK_(thread_handle
!= nullptr)
479 << "CreateThread failed with error " << ::GetLastError() << ".";
480 if (thread_handle
== nullptr) {
483 return thread_handle
;
487 struct ThreadMainParam
{
488 ThreadMainParam(Runnable
* runnable
, Notification
* thread_can_start
)
489 : runnable_(runnable
),
490 thread_can_start_(thread_can_start
) {
492 std::unique_ptr
<Runnable
> runnable_
;
494 Notification
* thread_can_start_
;
497 static DWORD WINAPI
ThreadMain(void* ptr
) {
498 // Transfers ownership.
499 std::unique_ptr
<ThreadMainParam
> param(static_cast<ThreadMainParam
*>(ptr
));
500 if (param
->thread_can_start_
!= nullptr)
501 param
->thread_can_start_
->WaitForNotification();
502 param
->runnable_
->Run();
506 // Prohibit instantiation.
507 ThreadWithParamSupport();
509 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParamSupport
);
514 ThreadWithParamBase::ThreadWithParamBase(Runnable
*runnable
,
515 Notification
* thread_can_start
)
516 : thread_(ThreadWithParamSupport::CreateThread(runnable
,
520 ThreadWithParamBase::~ThreadWithParamBase() {
524 void ThreadWithParamBase::Join() {
525 GTEST_CHECK_(::WaitForSingleObject(thread_
.Get(), INFINITE
) == WAIT_OBJECT_0
)
526 << "Failed to join the thread with error " << ::GetLastError() << ".";
529 // Maps a thread to a set of ThreadIdToThreadLocals that have values
530 // instantiated on that thread and notifies them when the thread exits. A
531 // ThreadLocal instance is expected to persist until all threads it has
532 // values on have terminated.
533 class ThreadLocalRegistryImpl
{
535 // Registers thread_local_instance as having value on the current thread.
536 // Returns a value that can be used to identify the thread from other threads.
537 static ThreadLocalValueHolderBase
* GetValueOnCurrentThread(
538 const ThreadLocalBase
* thread_local_instance
) {
539 DWORD current_thread
= ::GetCurrentThreadId();
540 MutexLock
lock(&mutex_
);
541 ThreadIdToThreadLocals
* const thread_to_thread_locals
=
542 GetThreadLocalsMapLocked();
543 ThreadIdToThreadLocals::iterator thread_local_pos
=
544 thread_to_thread_locals
->find(current_thread
);
545 if (thread_local_pos
== thread_to_thread_locals
->end()) {
546 thread_local_pos
= thread_to_thread_locals
->insert(
547 std::make_pair(current_thread
, ThreadLocalValues())).first
;
548 StartWatcherThreadFor(current_thread
);
550 ThreadLocalValues
& thread_local_values
= thread_local_pos
->second
;
551 ThreadLocalValues::iterator value_pos
=
552 thread_local_values
.find(thread_local_instance
);
553 if (value_pos
== thread_local_values
.end()) {
556 .insert(std::make_pair(
557 thread_local_instance
,
558 std::shared_ptr
<ThreadLocalValueHolderBase
>(
559 thread_local_instance
->NewValueForCurrentThread())))
562 return value_pos
->second
.get();
565 static void OnThreadLocalDestroyed(
566 const ThreadLocalBase
* thread_local_instance
) {
567 std::vector
<std::shared_ptr
<ThreadLocalValueHolderBase
> > value_holders
;
568 // Clean up the ThreadLocalValues data structure while holding the lock, but
569 // defer the destruction of the ThreadLocalValueHolderBases.
571 MutexLock
lock(&mutex_
);
572 ThreadIdToThreadLocals
* const thread_to_thread_locals
=
573 GetThreadLocalsMapLocked();
574 for (ThreadIdToThreadLocals::iterator it
=
575 thread_to_thread_locals
->begin();
576 it
!= thread_to_thread_locals
->end();
578 ThreadLocalValues
& thread_local_values
= it
->second
;
579 ThreadLocalValues::iterator value_pos
=
580 thread_local_values
.find(thread_local_instance
);
581 if (value_pos
!= thread_local_values
.end()) {
582 value_holders
.push_back(value_pos
->second
);
583 thread_local_values
.erase(value_pos
);
584 // This 'if' can only be successful at most once, so theoretically we
585 // could break out of the loop here, but we don't bother doing so.
589 // Outside the lock, let the destructor for 'value_holders' deallocate the
590 // ThreadLocalValueHolderBases.
593 static void OnThreadExit(DWORD thread_id
) {
594 GTEST_CHECK_(thread_id
!= 0) << ::GetLastError();
595 std::vector
<std::shared_ptr
<ThreadLocalValueHolderBase
> > value_holders
;
596 // Clean up the ThreadIdToThreadLocals data structure while holding the
597 // lock, but defer the destruction of the ThreadLocalValueHolderBases.
599 MutexLock
lock(&mutex_
);
600 ThreadIdToThreadLocals
* const thread_to_thread_locals
=
601 GetThreadLocalsMapLocked();
602 ThreadIdToThreadLocals::iterator thread_local_pos
=
603 thread_to_thread_locals
->find(thread_id
);
604 if (thread_local_pos
!= thread_to_thread_locals
->end()) {
605 ThreadLocalValues
& thread_local_values
= thread_local_pos
->second
;
606 for (ThreadLocalValues::iterator value_pos
=
607 thread_local_values
.begin();
608 value_pos
!= thread_local_values
.end();
610 value_holders
.push_back(value_pos
->second
);
612 thread_to_thread_locals
->erase(thread_local_pos
);
615 // Outside the lock, let the destructor for 'value_holders' deallocate the
616 // ThreadLocalValueHolderBases.
620 // In a particular thread, maps a ThreadLocal object to its value.
621 typedef std::map
<const ThreadLocalBase
*,
622 std::shared_ptr
<ThreadLocalValueHolderBase
> >
624 // Stores all ThreadIdToThreadLocals having values in a thread, indexed by
626 typedef std::map
<DWORD
, ThreadLocalValues
> ThreadIdToThreadLocals
;
628 // Holds the thread id and thread handle that we pass from
629 // StartWatcherThreadFor to WatcherThreadFunc.
630 typedef std::pair
<DWORD
, HANDLE
> ThreadIdAndHandle
;
632 static void StartWatcherThreadFor(DWORD thread_id
) {
633 // The returned handle will be kept in thread_map and closed by
634 // watcher_thread in WatcherThreadFunc.
635 HANDLE thread
= ::OpenThread(SYNCHRONIZE
| THREAD_QUERY_INFORMATION
,
638 GTEST_CHECK_(thread
!= nullptr);
639 // We need to pass a valid thread ID pointer into CreateThread for it
640 // to work correctly under Win98.
641 DWORD watcher_thread_id
;
642 HANDLE watcher_thread
= ::CreateThread(
643 nullptr, // Default security.
644 0, // Default stack size
645 &ThreadLocalRegistryImpl::WatcherThreadFunc
,
646 reinterpret_cast<LPVOID
>(new ThreadIdAndHandle(thread_id
, thread
)),
647 CREATE_SUSPENDED
, &watcher_thread_id
);
648 GTEST_CHECK_(watcher_thread
!= nullptr);
649 // Give the watcher thread the same priority as ours to avoid being
651 ::SetThreadPriority(watcher_thread
,
652 ::GetThreadPriority(::GetCurrentThread()));
653 ::ResumeThread(watcher_thread
);
654 ::CloseHandle(watcher_thread
);
657 // Monitors exit from a given thread and notifies those
658 // ThreadIdToThreadLocals about thread termination.
659 static DWORD WINAPI
WatcherThreadFunc(LPVOID param
) {
660 const ThreadIdAndHandle
* tah
=
661 reinterpret_cast<const ThreadIdAndHandle
*>(param
);
663 ::WaitForSingleObject(tah
->second
, INFINITE
) == WAIT_OBJECT_0
);
664 OnThreadExit(tah
->first
);
665 ::CloseHandle(tah
->second
);
670 // Returns map of thread local instances.
671 static ThreadIdToThreadLocals
* GetThreadLocalsMapLocked() {
674 MemoryIsNotDeallocated memory_is_not_deallocated
;
676 static ThreadIdToThreadLocals
* map
= new ThreadIdToThreadLocals();
680 // Protects access to GetThreadLocalsMapLocked() and its return value.
682 // Protects access to GetThreadMapLocked() and its return value.
683 static Mutex thread_map_mutex_
;
686 Mutex
ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex
);
687 Mutex
ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex
);
689 ThreadLocalValueHolderBase
* ThreadLocalRegistry::GetValueOnCurrentThread(
690 const ThreadLocalBase
* thread_local_instance
) {
691 return ThreadLocalRegistryImpl::GetValueOnCurrentThread(
692 thread_local_instance
);
695 void ThreadLocalRegistry::OnThreadLocalDestroyed(
696 const ThreadLocalBase
* thread_local_instance
) {
697 ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance
);
700 #endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
702 #if GTEST_USES_POSIX_RE
704 // Implements RE. Currently only needed for death tests.
708 // regfree'ing an invalid regex might crash because the content
709 // of the regex is undefined. Since the regex's are essentially
710 // the same, one cannot be valid (or invalid) without the other
712 regfree(&partial_regex_
);
713 regfree(&full_regex_
);
715 free(const_cast<char*>(pattern_
));
718 // Returns true if and only if regular expression re matches the entire str.
719 bool RE::FullMatch(const char* str
, const RE
& re
) {
720 if (!re
.is_valid_
) return false;
723 return regexec(&re
.full_regex_
, str
, 1, &match
, 0) == 0;
726 // Returns true if and only if regular expression re matches a substring of
727 // str (including str itself).
728 bool RE::PartialMatch(const char* str
, const RE
& re
) {
729 if (!re
.is_valid_
) return false;
732 return regexec(&re
.partial_regex_
, str
, 1, &match
, 0) == 0;
735 // Initializes an RE from its string representation.
736 void RE::Init(const char* regex
) {
737 pattern_
= posix::StrDup(regex
);
739 // Reserves enough bytes to hold the regular expression used for a
741 const size_t full_regex_len
= strlen(regex
) + 10;
742 char* const full_pattern
= new char[full_regex_len
];
744 snprintf(full_pattern
, full_regex_len
, "^(%s)$", regex
);
745 is_valid_
= regcomp(&full_regex_
, full_pattern
, REG_EXTENDED
) == 0;
746 // We want to call regcomp(&partial_regex_, ...) even if the
747 // previous expression returns false. Otherwise partial_regex_ may
748 // not be properly initialized can may cause trouble when it's
751 // Some implementation of POSIX regex (e.g. on at least some
752 // versions of Cygwin) doesn't accept the empty string as a valid
753 // regex. We change it to an equivalent form "()" to be safe.
755 const char* const partial_regex
= (*regex
== '\0') ? "()" : regex
;
756 is_valid_
= regcomp(&partial_regex_
, partial_regex
, REG_EXTENDED
) == 0;
758 EXPECT_TRUE(is_valid_
)
759 << "Regular expression \"" << regex
760 << "\" is not a valid POSIX Extended regular expression.";
762 delete[] full_pattern
;
765 #elif GTEST_USES_SIMPLE_RE
767 // Returns true if and only if ch appears anywhere in str (excluding the
768 // terminating '\0' character).
769 bool IsInSet(char ch
, const char* str
) {
770 return ch
!= '\0' && strchr(str
, ch
) != nullptr;
773 // Returns true if and only if ch belongs to the given classification.
774 // Unlike similar functions in <ctype.h>, these aren't affected by the
776 bool IsAsciiDigit(char ch
) { return '0' <= ch
&& ch
<= '9'; }
777 bool IsAsciiPunct(char ch
) {
778 return IsInSet(ch
, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
780 bool IsRepeat(char ch
) { return IsInSet(ch
, "?*+"); }
781 bool IsAsciiWhiteSpace(char ch
) { return IsInSet(ch
, " \f\n\r\t\v"); }
782 bool IsAsciiWordChar(char ch
) {
783 return ('a' <= ch
&& ch
<= 'z') || ('A' <= ch
&& ch
<= 'Z') ||
784 ('0' <= ch
&& ch
<= '9') || ch
== '_';
787 // Returns true if and only if "\\c" is a supported escape sequence.
788 bool IsValidEscape(char c
) {
789 return (IsAsciiPunct(c
) || IsInSet(c
, "dDfnrsStvwW"));
792 // Returns true if and only if the given atom (specified by escaped and
793 // pattern) matches ch. The result is undefined if the atom is invalid.
794 bool AtomMatchesChar(bool escaped
, char pattern_char
, char ch
) {
795 if (escaped
) { // "\\p" where p is pattern_char.
796 switch (pattern_char
) {
797 case 'd': return IsAsciiDigit(ch
);
798 case 'D': return !IsAsciiDigit(ch
);
799 case 'f': return ch
== '\f';
800 case 'n': return ch
== '\n';
801 case 'r': return ch
== '\r';
802 case 's': return IsAsciiWhiteSpace(ch
);
803 case 'S': return !IsAsciiWhiteSpace(ch
);
804 case 't': return ch
== '\t';
805 case 'v': return ch
== '\v';
806 case 'w': return IsAsciiWordChar(ch
);
807 case 'W': return !IsAsciiWordChar(ch
);
809 return IsAsciiPunct(pattern_char
) && pattern_char
== ch
;
812 return (pattern_char
== '.' && ch
!= '\n') || pattern_char
== ch
;
815 // Helper function used by ValidateRegex() to format error messages.
816 static std::string
FormatRegexSyntaxError(const char* regex
, int index
) {
817 return (Message() << "Syntax error at index " << index
818 << " in simple regular expression \"" << regex
<< "\": ").GetString();
821 // Generates non-fatal failures and returns false if regex is invalid;
822 // otherwise returns true.
823 bool ValidateRegex(const char* regex
) {
824 if (regex
== nullptr) {
825 ADD_FAILURE() << "NULL is not a valid simple regular expression.";
829 bool is_valid
= true;
831 // True if and only if ?, *, or + can follow the previous atom.
832 bool prev_repeatable
= false;
833 for (int i
= 0; regex
[i
]; i
++) {
834 if (regex
[i
] == '\\') { // An escape sequence
836 if (regex
[i
] == '\0') {
837 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
- 1)
838 << "'\\' cannot appear at the end.";
842 if (!IsValidEscape(regex
[i
])) {
843 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
- 1)
844 << "invalid escape sequence \"\\" << regex
[i
] << "\".";
847 prev_repeatable
= true;
848 } else { // Not an escape sequence.
849 const char ch
= regex
[i
];
851 if (ch
== '^' && i
> 0) {
852 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
)
853 << "'^' can only appear at the beginning.";
855 } else if (ch
== '$' && regex
[i
+ 1] != '\0') {
856 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
)
857 << "'$' can only appear at the end.";
859 } else if (IsInSet(ch
, "()[]{}|")) {
860 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
)
861 << "'" << ch
<< "' is unsupported.";
863 } else if (IsRepeat(ch
) && !prev_repeatable
) {
864 ADD_FAILURE() << FormatRegexSyntaxError(regex
, i
)
865 << "'" << ch
<< "' can only follow a repeatable token.";
869 prev_repeatable
= !IsInSet(ch
, "^$?*+");
876 // Matches a repeated regex atom followed by a valid simple regular
877 // expression. The regex atom is defined as c if escaped is false,
878 // or \c otherwise. repeat is the repetition meta character (?, *,
879 // or +). The behavior is undefined if str contains too many
880 // characters to be indexable by size_t, in which case the test will
881 // probably time out anyway. We are fine with this limitation as
882 // std::string has it too.
883 bool MatchRepetitionAndRegexAtHead(
884 bool escaped
, char c
, char repeat
, const char* regex
,
886 const size_t min_count
= (repeat
== '+') ? 1 : 0;
887 const size_t max_count
= (repeat
== '?') ? 1 :
888 static_cast<size_t>(-1) - 1;
889 // We cannot call numeric_limits::max() as it conflicts with the
890 // max() macro on Windows.
892 for (size_t i
= 0; i
<= max_count
; ++i
) {
893 // We know that the atom matches each of the first i characters in str.
894 if (i
>= min_count
&& MatchRegexAtHead(regex
, str
+ i
)) {
895 // We have enough matches at the head, and the tail matches too.
896 // Since we only care about *whether* the pattern matches str
897 // (as opposed to *how* it matches), there is no need to find a
901 if (str
[i
] == '\0' || !AtomMatchesChar(escaped
, c
, str
[i
]))
907 // Returns true if and only if regex matches a prefix of str. regex must
908 // be a valid simple regular expression and not start with "^", or the
909 // result is undefined.
910 bool MatchRegexAtHead(const char* regex
, const char* str
) {
911 if (*regex
== '\0') // An empty regex matches a prefix of anything.
914 // "$" only matches the end of a string. Note that regex being
915 // valid guarantees that there's nothing after "$" in it.
919 // Is the first thing in regex an escape sequence?
920 const bool escaped
= *regex
== '\\';
923 if (IsRepeat(regex
[1])) {
924 // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so
925 // here's an indirect recursion. It terminates as the regex gets
926 // shorter in each recursion.
927 return MatchRepetitionAndRegexAtHead(
928 escaped
, regex
[0], regex
[1], regex
+ 2, str
);
930 // regex isn't empty, isn't "$", and doesn't start with a
931 // repetition. We match the first atom of regex with the first
932 // character of str and recurse.
933 return (*str
!= '\0') && AtomMatchesChar(escaped
, *regex
, *str
) &&
934 MatchRegexAtHead(regex
+ 1, str
+ 1);
938 // Returns true if and only if regex matches any substring of str. regex must
939 // be a valid simple regular expression, or the result is undefined.
941 // The algorithm is recursive, but the recursion depth doesn't exceed
942 // the regex length, so we won't need to worry about running out of
943 // stack space normally. In rare cases the time complexity can be
944 // exponential with respect to the regex length + the string length,
945 // but usually it's must faster (often close to linear).
946 bool MatchRegexAnywhere(const char* regex
, const char* str
) {
947 if (regex
== nullptr || str
== nullptr) return false;
950 return MatchRegexAtHead(regex
+ 1, str
);
952 // A successful match can be anywhere in str.
954 if (MatchRegexAtHead(regex
, str
))
956 } while (*str
++ != '\0');
960 // Implements the RE class.
963 free(const_cast<char*>(pattern_
));
964 free(const_cast<char*>(full_pattern_
));
967 // Returns true if and only if regular expression re matches the entire str.
968 bool RE::FullMatch(const char* str
, const RE
& re
) {
969 return re
.is_valid_
&& MatchRegexAnywhere(re
.full_pattern_
, str
);
972 // Returns true if and only if regular expression re matches a substring of
973 // str (including str itself).
974 bool RE::PartialMatch(const char* str
, const RE
& re
) {
975 return re
.is_valid_
&& MatchRegexAnywhere(re
.pattern_
, str
);
978 // Initializes an RE from its string representation.
979 void RE::Init(const char* regex
) {
980 pattern_
= full_pattern_
= nullptr;
981 if (regex
!= nullptr) {
982 pattern_
= posix::StrDup(regex
);
985 is_valid_
= ValidateRegex(regex
);
987 // No need to calculate the full pattern when the regex is invalid.
991 const size_t len
= strlen(regex
);
992 // Reserves enough bytes to hold the regular expression used for a
993 // full match: we need space to prepend a '^', append a '$', and
994 // terminate the string with '\0'.
995 char* buffer
= static_cast<char*>(malloc(len
+ 3));
996 full_pattern_
= buffer
;
999 *buffer
++ = '^'; // Makes sure full_pattern_ starts with '^'.
1001 // We don't use snprintf or strncpy, as they trigger a warning when
1002 // compiled with VC++ 8.0.
1003 memcpy(buffer
, regex
, len
);
1006 if (len
== 0 || regex
[len
- 1] != '$')
1007 *buffer
++ = '$'; // Makes sure full_pattern_ ends with '$'.
1012 #endif // GTEST_USES_POSIX_RE
1014 const char kUnknownFile
[] = "unknown file";
1016 // Formats a source file path and a line number as they would appear
1017 // in an error message from the compiler used to compile this code.
1018 GTEST_API_ ::std::string
FormatFileLocation(const char* file
, int line
) {
1019 const std::string
file_name(file
== nullptr ? kUnknownFile
: file
);
1022 return file_name
+ ":";
1025 return file_name
+ "(" + StreamableToString(line
) + "):";
1027 return file_name
+ ":" + StreamableToString(line
) + ":";
1031 // Formats a file location for compiler-independent XML output.
1032 // Although this function is not platform dependent, we put it next to
1033 // FormatFileLocation in order to contrast the two functions.
1034 // Note that FormatCompilerIndependentFileLocation() does NOT append colon
1035 // to the file location it produces, unlike FormatFileLocation().
1036 GTEST_API_ ::std::string
FormatCompilerIndependentFileLocation(
1037 const char* file
, int line
) {
1038 const std::string
file_name(file
== nullptr ? kUnknownFile
: file
);
1043 return file_name
+ ":" + StreamableToString(line
);
1046 GTestLog::GTestLog(GTestLogSeverity severity
, const char* file
, int line
)
1047 : severity_(severity
) {
1048 const char* const marker
=
1049 severity
== GTEST_INFO
? "[ INFO ]" :
1050 severity
== GTEST_WARNING
? "[WARNING]" :
1051 severity
== GTEST_ERROR
? "[ ERROR ]" : "[ FATAL ]";
1052 GetStream() << ::std::endl
<< marker
<< " "
1053 << FormatFileLocation(file
, line
).c_str() << ": ";
1056 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1057 GTestLog::~GTestLog() {
1058 GetStream() << ::std::endl
;
1059 if (severity_
== GTEST_FATAL
) {
1065 // Disable Microsoft deprecation warnings for POSIX functions called from
1066 // this class (creat, dup, dup2, and close)
1067 GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
1069 #if GTEST_HAS_STREAM_REDIRECTION
1071 // Object that captures an output stream (stdout/stderr).
1072 class CapturedStream
{
1074 // The ctor redirects the stream to a temporary file.
1075 explicit CapturedStream(int fd
) : fd_(fd
), uncaptured_fd_(dup(fd
)) {
1076 # if GTEST_OS_WINDOWS
1077 char temp_dir_path
[MAX_PATH
+ 1] = { '\0' }; // NOLINT
1078 char temp_file_path
[MAX_PATH
+ 1] = { '\0' }; // NOLINT
1080 ::GetTempPathA(sizeof(temp_dir_path
), temp_dir_path
);
1081 const UINT success
= ::GetTempFileNameA(temp_dir_path
,
1083 0, // Generate unique file name.
1085 GTEST_CHECK_(success
!= 0)
1086 << "Unable to create a temporary file in " << temp_dir_path
;
1087 const int captured_fd
= creat(temp_file_path
, _S_IREAD
| _S_IWRITE
);
1088 GTEST_CHECK_(captured_fd
!= -1) << "Unable to open temporary file "
1090 filename_
= temp_file_path
;
1092 // There's no guarantee that a test has write access to the current
1093 // directory, so we create the temporary file in the /tmp directory
1094 // instead. We use /tmp on most systems, and /sdcard on Android.
1095 // That's because Android doesn't have /tmp.
1096 # if GTEST_OS_LINUX_ANDROID
1097 // Note: Android applications are expected to call the framework's
1098 // Context.getExternalStorageDirectory() method through JNI to get
1099 // the location of the world-writable SD Card directory. However,
1100 // this requires a Context handle, which cannot be retrieved
1101 // globally from native code. Doing so also precludes running the
1102 // code as part of a regular standalone executable, which doesn't
1103 // run in a Dalvik process (e.g. when running it through 'adb shell').
1105 // The location /data/local/tmp is directly accessible from native code.
1106 // '/sdcard' and other variants cannot be relied on, as they are not
1107 // guaranteed to be mounted, or may have a delay in mounting.
1108 char name_template
[] = "/data/local/tmp/gtest_captured_stream.XXXXXX";
1110 char name_template
[] = "/tmp/captured_stream.XXXXXX";
1111 # endif // GTEST_OS_LINUX_ANDROID
1112 const int captured_fd
= mkstemp(name_template
);
1113 if (captured_fd
== -1) {
1115 << "Failed to create tmp file " << name_template
1116 << " for test; does the test have access to the /tmp directory?";
1118 filename_
= name_template
;
1119 # endif // GTEST_OS_WINDOWS
1121 dup2(captured_fd
, fd_
);
1126 remove(filename_
.c_str());
1129 std::string
GetCapturedString() {
1130 if (uncaptured_fd_
!= -1) {
1131 // Restores the original stream.
1133 dup2(uncaptured_fd_
, fd_
);
1134 close(uncaptured_fd_
);
1135 uncaptured_fd_
= -1;
1138 FILE* const file
= posix::FOpen(filename_
.c_str(), "r");
1139 if (file
== nullptr) {
1140 GTEST_LOG_(FATAL
) << "Failed to open tmp file " << filename_
1141 << " for capturing stream.";
1143 const std::string content
= ReadEntireFile(file
);
1144 posix::FClose(file
);
1149 const int fd_
; // A stream to capture.
1151 // Name of the temporary file holding the stderr output.
1152 ::std::string filename_
;
1154 GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream
);
1157 GTEST_DISABLE_MSC_DEPRECATED_POP_()
1159 static CapturedStream
* g_captured_stderr
= nullptr;
1160 static CapturedStream
* g_captured_stdout
= nullptr;
1162 // Starts capturing an output stream (stdout/stderr).
1163 static void CaptureStream(int fd
, const char* stream_name
,
1164 CapturedStream
** stream
) {
1165 if (*stream
!= nullptr) {
1166 GTEST_LOG_(FATAL
) << "Only one " << stream_name
1167 << " capturer can exist at a time.";
1169 *stream
= new CapturedStream(fd
);
1172 // Stops capturing the output stream and returns the captured string.
1173 static std::string
GetCapturedStream(CapturedStream
** captured_stream
) {
1174 const std::string content
= (*captured_stream
)->GetCapturedString();
1176 delete *captured_stream
;
1177 *captured_stream
= nullptr;
1182 // Starts capturing stdout.
1183 void CaptureStdout() {
1184 CaptureStream(kStdOutFileno
, "stdout", &g_captured_stdout
);
1187 // Starts capturing stderr.
1188 void CaptureStderr() {
1189 CaptureStream(kStdErrFileno
, "stderr", &g_captured_stderr
);
1192 // Stops capturing stdout and returns the captured string.
1193 std::string
GetCapturedStdout() {
1194 return GetCapturedStream(&g_captured_stdout
);
1197 // Stops capturing stderr and returns the captured string.
1198 std::string
GetCapturedStderr() {
1199 return GetCapturedStream(&g_captured_stderr
);
1202 #endif // GTEST_HAS_STREAM_REDIRECTION
1208 size_t GetFileSize(FILE* file
) {
1209 fseek(file
, 0, SEEK_END
);
1210 return static_cast<size_t>(ftell(file
));
1213 std::string
ReadEntireFile(FILE* file
) {
1214 const size_t file_size
= GetFileSize(file
);
1215 char* const buffer
= new char[file_size
];
1217 size_t bytes_last_read
= 0; // # of bytes read in the last fread()
1218 size_t bytes_read
= 0; // # of bytes read so far
1220 fseek(file
, 0, SEEK_SET
);
1222 // Keeps reading the file until we cannot read further or the
1223 // pre-determined file size is reached.
1225 bytes_last_read
= fread(buffer
+bytes_read
, 1, file_size
-bytes_read
, file
);
1226 bytes_read
+= bytes_last_read
;
1227 } while (bytes_last_read
> 0 && bytes_read
< file_size
);
1229 const std::string
content(buffer
, bytes_read
);
1235 #if GTEST_HAS_DEATH_TEST
1236 static const std::vector
<std::string
>* g_injected_test_argvs
=
1239 std::vector
<std::string
> GetInjectableArgvs() {
1240 if (g_injected_test_argvs
!= nullptr) {
1241 return *g_injected_test_argvs
;
1246 void SetInjectableArgvs(const std::vector
<std::string
>* new_argvs
) {
1247 if (g_injected_test_argvs
!= new_argvs
) delete g_injected_test_argvs
;
1248 g_injected_test_argvs
= new_argvs
;
1251 void SetInjectableArgvs(const std::vector
<std::string
>& new_argvs
) {
1253 new std::vector
<std::string
>(new_argvs
.begin(), new_argvs
.end()));
1256 void ClearInjectableArgvs() {
1257 delete g_injected_test_argvs
;
1258 g_injected_test_argvs
= nullptr;
1260 #endif // GTEST_HAS_DEATH_TEST
1262 #if GTEST_OS_WINDOWS_MOBILE
1266 TerminateProcess(GetCurrentProcess(), 1);
1268 } // namespace posix
1269 #endif // GTEST_OS_WINDOWS_MOBILE
1271 // Returns the name of the environment variable corresponding to the
1272 // given flag. For example, FlagToEnvVar("foo") will return
1273 // "GTEST_FOO" in the open-source version.
1274 static std::string
FlagToEnvVar(const char* flag
) {
1275 const std::string full_flag
=
1276 (Message() << GTEST_FLAG_PREFIX_
<< flag
).GetString();
1279 for (size_t i
= 0; i
!= full_flag
.length(); i
++) {
1280 env_var
<< ToUpper(full_flag
.c_str()[i
]);
1283 return env_var
.GetString();
1286 // Parses 'str' for a 32-bit signed integer. If successful, writes
1287 // the result to *value and returns true; otherwise leaves *value
1288 // unchanged and returns false.
1289 bool ParseInt32(const Message
& src_text
, const char* str
, Int32
* value
) {
1290 // Parses the environment variable as a decimal integer.
1291 char* end
= nullptr;
1292 const long long_value
= strtol(str
, &end
, 10); // NOLINT
1294 // Has strtol() consumed all characters in the string?
1296 // No - an invalid character was encountered.
1298 msg
<< "WARNING: " << src_text
1299 << " is expected to be a 32-bit integer, but actually"
1300 << " has value \"" << str
<< "\".\n";
1301 printf("%s", msg
.GetString().c_str());
1306 // Is the parsed value in the range of an Int32?
1307 const Int32 result
= static_cast<Int32
>(long_value
);
1308 if (long_value
== LONG_MAX
|| long_value
== LONG_MIN
||
1309 // The parsed value overflows as a long. (strtol() returns
1310 // LONG_MAX or LONG_MIN when the input overflows.)
1311 result
!= long_value
1312 // The parsed value overflows as an Int32.
1315 msg
<< "WARNING: " << src_text
1316 << " is expected to be a 32-bit integer, but actually"
1317 << " has value " << str
<< ", which overflows.\n";
1318 printf("%s", msg
.GetString().c_str());
1327 // Reads and returns the Boolean environment variable corresponding to
1328 // the given flag; if it's not set, returns default_value.
1330 // The value is considered true if and only if it's not "0".
1331 bool BoolFromGTestEnv(const char* flag
, bool default_value
) {
1332 #if defined(GTEST_GET_BOOL_FROM_ENV_)
1333 return GTEST_GET_BOOL_FROM_ENV_(flag
, default_value
);
1335 const std::string env_var
= FlagToEnvVar(flag
);
1336 const char* const string_value
= posix::GetEnv(env_var
.c_str());
1337 return string_value
== nullptr ? default_value
1338 : strcmp(string_value
, "0") != 0;
1339 #endif // defined(GTEST_GET_BOOL_FROM_ENV_)
1342 // Reads and returns a 32-bit integer stored in the environment
1343 // variable corresponding to the given flag; if it isn't set or
1344 // doesn't represent a valid 32-bit integer, returns default_value.
1345 Int32
Int32FromGTestEnv(const char* flag
, Int32 default_value
) {
1346 #if defined(GTEST_GET_INT32_FROM_ENV_)
1347 return GTEST_GET_INT32_FROM_ENV_(flag
, default_value
);
1349 const std::string env_var
= FlagToEnvVar(flag
);
1350 const char* const string_value
= posix::GetEnv(env_var
.c_str());
1351 if (string_value
== nullptr) {
1352 // The environment variable is not set.
1353 return default_value
;
1356 Int32 result
= default_value
;
1357 if (!ParseInt32(Message() << "Environment variable " << env_var
,
1358 string_value
, &result
)) {
1359 printf("The default value %s is used.\n",
1360 (Message() << default_value
).GetString().c_str());
1362 return default_value
;
1366 #endif // defined(GTEST_GET_INT32_FROM_ENV_)
1369 // As a special case for the 'output' flag, if GTEST_OUTPUT is not
1370 // set, we look for XML_OUTPUT_FILE, which is set by the Bazel build
1371 // system. The value of XML_OUTPUT_FILE is a filename without the
1372 // "xml:" prefix of GTEST_OUTPUT.
1373 // Note that this is meant to be called at the call site so it does
1374 // not check that the flag is 'output'
1375 // In essence this checks an env variable called XML_OUTPUT_FILE
1376 // and if it is set we prepend "xml:" to its value, if it not set we return ""
1377 std::string
OutputFlagAlsoCheckEnvVar(){
1378 std::string default_value_for_output_flag
= "";
1379 const char* xml_output_file_env
= posix::GetEnv("XML_OUTPUT_FILE");
1380 if (nullptr != xml_output_file_env
) {
1381 default_value_for_output_flag
= std::string("xml:") + xml_output_file_env
;
1383 return default_value_for_output_flag
;
1386 // Reads and returns the string environment variable corresponding to
1387 // the given flag; if it's not set, returns default_value.
1388 const char* StringFromGTestEnv(const char* flag
, const char* default_value
) {
1389 #if defined(GTEST_GET_STRING_FROM_ENV_)
1390 return GTEST_GET_STRING_FROM_ENV_(flag
, default_value
);
1392 const std::string env_var
= FlagToEnvVar(flag
);
1393 const char* const value
= posix::GetEnv(env_var
.c_str());
1394 return value
== nullptr ? default_value
: value
;
1395 #endif // defined(GTEST_GET_STRING_FROM_ENV_)
1398 } // namespace internal
1399 } // namespace testing