1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/profiler/thread_delegate_posix.h"
6 
7 #include "base/process/process_handle.h"
8 #include "build/build_config.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace base {
12 
13 // ASAN moves local variables outside of the stack extents.
14 // Test is flaky on ChromeOS. crbug.com/1133434.
15 #if defined(ADDRESS_SANITIZER) || defined(OS_CHROMEOS)
16 #define MAYBE_CurrentThreadBase DISABLED_CurrentThreadBase
17 #else
18 #define MAYBE_CurrentThreadBase CurrentThreadBase
19 #endif
TEST(ThreadDelegatePosixTest,MAYBE_CurrentThreadBase)20 TEST(ThreadDelegatePosixTest, MAYBE_CurrentThreadBase) {
21   auto delegate =
22       ThreadDelegatePosix::Create(GetSamplingProfilerCurrentThreadToken());
23   ASSERT_TRUE(delegate);
24   uintptr_t base = delegate->GetStackBaseAddress();
25   EXPECT_GT(base, 0u);
26   uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&base);
27   // Check that end of stack is within 4MiB of a current stack address.
28   EXPECT_LE(base, stack_addr + 4 * 1024 * 1024);
29 }
30 
31 #if defined(OS_ANDROID)
32 
TEST(ThreadDelegatePosixTest,AndroidMainThreadStackBase)33 TEST(ThreadDelegatePosixTest, AndroidMainThreadStackBase) {
34   // The delegate does not use pthread id for main thread.
35   auto delegate = ThreadDelegatePosix::Create(
36       SamplingProfilerThreadToken{GetCurrentProcId(), pthread_t()});
37   ASSERT_TRUE(delegate);
38   uintptr_t base = delegate->GetStackBaseAddress();
39   EXPECT_GT(base, 0u);
40 }
41 
42 #endif  // defined(OS_ANDROID)
43 }  // namespace base
44