thread_delegate_posix_unittest.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #include "base/profiler/thread_delegate_posix.h"
  5. #include "base/numerics/clamped_math.h"
  6. #include "base/process/process_handle.h"
  7. #include "build/build_config.h"
  8. #include "build/chromeos_buildflags.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. // ASAN moves local variables outside of the stack extents.
  12. #if defined(ADDRESS_SANITIZER)
  13. #define MAYBE_CurrentThreadBase DISABLED_CurrentThreadBase
  14. #else
  15. #define MAYBE_CurrentThreadBase CurrentThreadBase
  16. #endif
  17. TEST(ThreadDelegatePosixTest, MAYBE_CurrentThreadBase) {
  18. auto delegate =
  19. ThreadDelegatePosix::Create(GetSamplingProfilerCurrentThreadToken());
  20. ASSERT_TRUE(delegate);
  21. uintptr_t base = delegate->GetStackBaseAddress();
  22. EXPECT_GT(base, 0u);
  23. uintptr_t stack_addr = reinterpret_cast<uintptr_t>(&base);
  24. // Check that end of stack is within 4MiB of a current stack address.
  25. EXPECT_LE(base, ClampAdd(stack_addr, 4 * 1024 * 1024));
  26. }
  27. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
  28. TEST(ThreadDelegatePosixTest, MainThreadStackBase) {
  29. // The delegate does not use pthread id for main thread.
  30. auto delegate = ThreadDelegatePosix::Create(
  31. SamplingProfilerThreadToken{GetCurrentProcId(), pthread_t()});
  32. ASSERT_TRUE(delegate);
  33. uintptr_t base = delegate->GetStackBaseAddress();
  34. EXPECT_GT(base, 0u);
  35. }
  36. #endif // BUILDFLAG(IS_ANDROID)
  37. } // namespace base