1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/profiler/thread_delegate_posix.h"
7 #include "base/numerics/clamped_math.h"
8 #include "base/process/process_handle.h"
9 #include "build/build_config.h"
10 #include "build/chromeos_buildflags.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 // ASAN moves local variables outside of the stack extents.
16 #if defined(ADDRESS_SANITIZER)
17 #define MAYBE_CurrentThreadBase DISABLED_CurrentThreadBase
18 #elif BUILDFLAG(IS_LINUX)
19 // We don't support getting the stack base address on Linux.
20 // https://crbug.com/1394278
21 #define MAYBE_CurrentThreadBase DISABLED_CurrentThreadBase
23 #define MAYBE_CurrentThreadBase CurrentThreadBase
25 TEST(ThreadDelegatePosixTest, MAYBE_CurrentThreadBase) {
27 ThreadDelegatePosix::Create(GetSamplingProfilerCurrentThreadToken());
28 ASSERT_TRUE(delegate);
29 uintptr_t base = delegate->GetStackBaseAddress();
31 uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&base);
32 // Check that end of stack is within 4MiB of a current stack address.
33 EXPECT_LE(base, ClampAdd(stack_addr, 4 * 1024 * 1024));
36 #if BUILDFLAG(IS_ANDROID)
37 // On ChromeOS, this functionality is tested by
38 // GetThreadStackBaseAddressTest.MainThread.
39 TEST(ThreadDelegatePosixTest, MainThreadStackBase) {
40 // The delegate does not use pthread id for main thread.
41 auto delegate = ThreadDelegatePosix::Create(
42 SamplingProfilerThreadToken{GetCurrentProcId(), pthread_t()});
43 ASSERT_TRUE(delegate);
44 uintptr_t base = delegate->GetStackBaseAddress();
48 #endif // BUILDFLAG(IS_ANDROID)