[compiler-rt] r320594 - [sanitizer] Introduce a vDSO aware timing function
Krzysztof Parzyszek via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 16 14:49:38 PST 2017
I just tried it---it works!
Thanks,
-Krzysztof
On 12/16/2017 2:56 PM, Kostya Kortchinsky wrote:
> Sent out https://reviews.llvm.org/D41325 for review.
> That *should* fix it, but I have no way to test it. Please let me know.
> I am not sure if you can add yourself as a reviewer, if so, please do.
>
>
> On Sat, Dec 16, 2017 at 9:51 AM, Krzysztof Parzyszek
> <kparzysz at codeaurora.org <mailto:kparzysz at codeaurora.org>> wrote:
>
> This broke FreeBSD build:
>
> In file included from
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/lsan_common.cc:15
> <http://llvm.org/projects/compiler-rt/lib/lsan/lsan_common.cc:15>:
> In file included from
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/lsan_common.h:18
> <http://llvm.org/projects/compiler-rt/lib/lsan/lsan_common.h:18>:
> In file included from
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_allocator.h:23
> <http://llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_allocator.h:23>:
> In file included from
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_procmaps.h:23
> <http://llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_procmaps.h:23>:
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_linux.h:49:29
> <http://llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_linux.h:49:29>:
> error: unknown type name '__sanitizer_clockid_t'; did you mean
> '__sanitizer_clock_t'?
> uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp);
> ^~~~~~~~~~~~~~~~~~~~~
> __sanitizer_clock_t
> /w/src/llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_platform_limits_posix.h:530:15
> <http://llvm.org/projects/compiler-rt/lib/lsan/../sanitizer_common/sanitizer_platform_limits_posix.h:530:15>:
> note: '__sanitizer_clock_t' declared here
> typedef int __sanitizer_clock_t;
> ^
>
> -Krzysztof
>
>
>
> On 12/13/2017 10:23 AM, Kostya Kortchinsky via llvm-commits wrote:
>
> Author: cryptoad
> Date: Wed Dec 13 08:23:54 2017
> New Revision: 320594
>
> URL: http://llvm.org/viewvc/llvm-project?rev=320594&view=rev
> <http://llvm.org/viewvc/llvm-project?rev=320594&view=rev>
> Log:
> [sanitizer] Introduce a vDSO aware timing function
>
> Summary:
> See D40657 & D40679 for previous versions of this patch &
> description.
>
> A couple of things were fixed here to have it not break some bots.
> Weak symbols can't be used with `SANITIZER_GO` so the previous
> version was
> breakin TsanGo. I set up some additional local tests and those
> pass now.
>
> I changed the workaround for the glibc vDSO issue: `__progname`
> is initialized
> after the vDSO and is actually public and of known type, unlike
> `__vdso_clock_gettime`. This works better, and with all compilers.
>
> The rest is the same.
>
> Reviewers: alekseyshl
>
> Reviewed By: alekseyshl
>
> Subscribers: srhines, kubamracek, krytarowski, llvm-commits,
> #sanitizers
>
> Differential Revision: https://reviews.llvm.org/D41121
> <https://reviews.llvm.org/D41121>
>
> Modified:
>
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
>
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h
>
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
>
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
> compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
> compiler-rt/trunk/lib/scudo/scudo_tsd.h
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> ---
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
> (original)
> +++
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
> Wed Dec 13 08:23:54 2017
> @@ -697,7 +697,7 @@ class SizeClassAllocator64 {
> // Do it only when the feature is turned on, to avoid
> a potentially
> // extraneous syscall.
> if (ReleaseToOSIntervalMs() >= 0)
> - region->rtoi.last_release_at_ns = NanoTime();
> + region->rtoi.last_release_at_ns = MonotonicNanoTime();
> }
> // Do the mmap for the user memory.
> const uptr user_map_size =
> @@ -827,7 +827,7 @@ class SizeClassAllocator64 {
> return;
> if (region->rtoi.last_release_at_ns + interval_ms *
> 1000000ULL >
> - NanoTime()) {
> + MonotonicNanoTime()) {
> return; // Memory was returned recently.
> }
> }
> @@ -844,6 +844,6 @@ class SizeClassAllocator64 {
> region->rtoi.num_releases +=
> memory_mapper.GetReleasedRangesCount();
> region->rtoi.last_released_bytes =
> memory_mapper.GetReleasedBytes();
> }
> - region->rtoi.last_release_at_ns = NanoTime();
> + region->rtoi.last_release_at_ns = MonotonicNanoTime();
> }
> };
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
> Wed Dec 13 08:23:54 2017
> @@ -295,6 +295,7 @@ uptr GetTlsSize();
> void SleepForSeconds(int seconds);
> void SleepForMillis(int millis);
> u64 NanoTime();
> +u64 MonotonicNanoTime();
> int Atexit(void (*function)(void));
> void SortArray(uptr *array, uptr size);
> void SortArray(u32 *array, uptr size);
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> ---
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
> (original)
> +++
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
> Wed Dec 13 08:23:54 2017
> @@ -2045,6 +2045,13 @@ INTERCEPTOR(int, clock_gettime, u32 clk_
> }
> return res;
> }
> +namespace __sanitizer {
> +extern "C" {
> +int real_clock_gettime(u32 clk_id, void *tp) {
> + return REAL(clock_gettime)(clk_id, tp);
> +}
> +} // extern "C"
> +} // namespace __sanitizer
> INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
> void *ctx;
> COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_fuchsia.cc
> Wed Dec 13 08:23:54 2017
> @@ -51,6 +51,8 @@ unsigned int internal_sleep(unsigned int
> u64 NanoTime() { return _zx_time_get(ZX_CLOCK_UTC); }
> +u64 MonotonicNanoTime() { return
> _zx_time_get(ZX_CLOCK_MONOTONIC); }
> +
> uptr internal_getpid() {
> zx_info_handle_basic_t info;
> zx_status_t status =
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
> Wed Dec 13 08:23:54 2017
> @@ -459,6 +459,10 @@ u64 NanoTime() {
> return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
> }
> +uptr internal_clock_gettime(__sanitizer_clockid_t clk_id,
> void *tp) {
> + return internal_syscall_ptr(SYSCALL(clock_gettime), clk_id, tp);
> +}
> +
> // Like getenv, but reads env directly from /proc (on Linux)
> or parses the
> // 'environ' array (on FreeBSD) and does not use libc. This
> function should be
> // called first inside __asan_init.
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.h Wed
> Dec 13 08:23:54 2017
> @@ -46,6 +46,7 @@ uptr internal_getdents(fd_t fd, struct l
> uptr internal_sigaltstack(const void* ss, void* oss);
> uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
> __sanitizer_sigset_t *oldset);
> +uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void
> *tp);
> // Linux-only syscalls.
> #if SANITIZER_LINUX
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> ---
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc (original)
> +++
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc Wed
> Dec 13 08:23:54 2017
> @@ -707,6 +707,47 @@ void LogMessageOnPrintf(const char *str)
> #endif // SANITIZER_LINUX
> +#if SANITIZER_LINUX && !SANITIZER_GO
> +// glibc crashes when using clock_gettime from a preinit_array
> function as the
> +// vDSO function pointers haven't been initialized yet.
> __progname is
> +// initialized after the vDSO function pointers, so if it
> exists, is not null
> +// and is not empty, we can use clock_gettime.
> +extern "C" SANITIZER_WEAK_ATTRIBUTE char *__progname;
> +INLINE bool CanUseVDSO() {
> + // Bionic is safe, it checks for the vDSO function pointers
> to be initialized.
> + if (SANITIZER_ANDROID)
> + return true;
> + if (&__progname && __progname && *__progname)
> + return true;
> + return false;
> +}
> +
> +// MonotonicNanoTime is a timing function that can leverage the
> vDSO by calling
> +// clock_gettime. real_clock_gettime only exists if
> clock_gettime is
> +// intercepted, so define it weakly and use it if available.
> +extern "C" SANITIZER_WEAK_ATTRIBUTE
> +int real_clock_gettime(u32 clk_id, void *tp);
> +u64 MonotonicNanoTime() {
> + timespec ts;
> + if (CanUseVDSO()) {
> + if (&real_clock_gettime)
> + real_clock_gettime(CLOCK_MONOTONIC, &ts);
> + else
> + clock_gettime(CLOCK_MONOTONIC, &ts);
> + } else {
> + internal_clock_gettime(CLOCK_MONOTONIC, &ts);
> + }
> + return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
> +}
> +#else
> +// Non-Linux & Go always use the syscall.
> +u64 MonotonicNanoTime() {
> + timespec ts;
> + internal_clock_gettime(CLOCK_MONOTONIC, &ts);
> + return (u64)ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
> +}
> +#endif // SANITIZER_LINUX && !SANITIZER_GO
> +
> } // namespace __sanitizer
> -#endif // SANITIZER_FREEBSD || SANITIZER_LINUX
> +#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc Wed
> Dec 13 08:23:54 2017
> @@ -365,6 +365,10 @@ u64 NanoTime() {
> return 0;
> }
> +u64 MonotonicNanoTime() {
> + return 0;
> +}
> +
> uptr GetTlsSize() {
> return 0;
> }
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> ---
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc
> (original)
> +++
> compiler-rt/trunk/lib/sanitizer_common/sanitizer_syscall_generic.inc
> Wed Dec 13 08:23:54 2017
> @@ -36,6 +36,7 @@
> # define SYS_sigaltstack SYS___sigaltstack14
> # define SYS_sigprocmask SYS___sigprocmask14
> # define SYS_nanosleep SYS___nanosleep50
> +# define SYS_clock_gettime SYS___clock_gettime50
> # if SANITIZER_WORDSIZE == 64
> # define internal_syscall_ptr __syscall
> # else
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Wed
> Dec 13 08:23:54 2017
> @@ -505,6 +505,10 @@ u64 NanoTime() {
> return 0;
> }
> +u64 MonotonicNanoTime() {
> + return 0;
> +}
> +
> void Abort() {
> internal__exit(3);
> }
>
> Modified: compiler-rt/trunk/lib/scudo/scudo_allocator.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_allocator.cpp?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_allocator.cpp?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/scudo/scudo_allocator.cpp (original)
> +++ compiler-rt/trunk/lib/scudo/scudo_allocator.cpp Wed Dec 13
> 08:23:54 2017
> @@ -301,7 +301,7 @@ struct ScudoAllocator {
> CheckRssLimit = HardRssLimitMb || SoftRssLimitMb;
> if (CheckRssLimit)
> - atomic_store_relaxed(&RssLastCheckedAtNS, NanoTime());
> + atomic_store_relaxed(&RssLastCheckedAtNS,
> MonotonicNanoTime());
> }
> // Helper function that checks for a valid Scudo chunk.
> nullptr isn't.
> @@ -319,7 +319,7 @@ struct ScudoAllocator {
> // it can, every 100ms, otherwise it will just return the
> current one.
> bool isRssLimitExceeded() {
> u64 LastCheck = atomic_load_relaxed(&RssLastCheckedAtNS);
> - const u64 CurrentCheck = NanoTime();
> + const u64 CurrentCheck = MonotonicNanoTime();
> if (LIKELY(CurrentCheck < LastCheck + (100ULL * 1000000ULL)))
> return atomic_load_relaxed(&RssLimitExceeded);
> if (!atomic_compare_exchange_weak(&RssLastCheckedAtNS,
> &LastCheck,
>
> Modified: compiler-rt/trunk/lib/scudo/scudo_tsd.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd.h?rev=320594&r1=320593&r2=320594&view=diff
> <http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/scudo/scudo_tsd.h?rev=320594&r1=320593&r2=320594&view=diff>
> ==============================================================================
> --- compiler-rt/trunk/lib/scudo/scudo_tsd.h (original)
> +++ compiler-rt/trunk/lib/scudo/scudo_tsd.h Wed Dec 13 08:23:54 2017
> @@ -36,7 +36,7 @@ struct ALIGNED(64) ScudoTSD {
> return true;
> }
> if (atomic_load_relaxed(&Precedence) == 0)
> - atomic_store_relaxed(&Precedence, NanoTime());
> + atomic_store_relaxed(&Precedence, MonotonicNanoTime());
> return false;
> }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>
>
>
> ---
> This email has been checked for viruses by AVG.
> http://www.avg.com
>
>
>
More information about the llvm-commits
mailing list