From c23acb9b5636e7b908fba03d6b3584d8f80dba6d Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Wed, 12 Apr 2023 13:26:48 -0700 Subject: Synchronization: Consolidate the logic for whether steady clocks are supported for relative timeouts PiperOrigin-RevId: 523789416 Change-Id: Ide4cfdcae9ea7bffca3355c80ea9c8833a9536e6 --- absl/synchronization/internal/kernel_timeout_test.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'absl/synchronization/internal/kernel_timeout_test.cc') diff --git a/absl/synchronization/internal/kernel_timeout_test.cc b/absl/synchronization/internal/kernel_timeout_test.cc index c853c4bc..e3286f7f 100644 --- a/absl/synchronization/internal/kernel_timeout_test.cc +++ b/absl/synchronization/internal/kernel_timeout_test.cc @@ -14,14 +14,34 @@ #include "absl/synchronization/internal/kernel_timeout.h" +#include #include // NOLINT(build/c++11) #include +#include "absl/random/random.h" #include "gtest/gtest.h" #include "absl/base/config.h" #include "absl/time/clock.h" #include "absl/time/time.h" +// Test go/btm support by randomizing the value clock_gettime() for +// CLOCK_MONOTONIC. This works by overriding a weak symbol in glibc. +// We should be resistant to this randomization when !SupportsSteadyClock(). +#ifdef __GOOGLE_GRTE_VERSION__ +extern "C" int __clock_gettime(clockid_t c, struct timespec* ts); + +extern "C" int clock_gettime(clockid_t c, struct timespec* ts) { + if (c == CLOCK_MONOTONIC && + !absl::synchronization_internal::KernelTimeout::SupportsSteadyClock()) { + absl::SharedBitGen gen; + ts->tv_sec = absl::Uniform(gen, 0, 1'000'000'000); + ts->tv_nsec = absl::Uniform(gen, 0, 1'000'000'000); + return 0; + } + return __clock_gettime(c, ts); +} +#endif // __GOOGLE_GRTE_VERSION__ + namespace { #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ -- cgit v1.2.3