This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] Introduce a vDSO aware time function, and use it in the allocator [redo]
ClosedPublic

Authored by cryptoad on Nov 30 2017, 2:38 PM.

Details

Summary

Redo of D40657, which had the initial discussion. The initial code had to move
into a libcdep file, and things had to be shuffled accordingly.

NanoTime is a time sink when checking whether or not to release memory to
the OS. While reducing the amount of calls to said function is in the works,
another solution that was found to be beneficial was to use a timing function
that can leverage the vDSO.

We hit a couple of snags along the way, like the fact that the glibc crashes
when clock_gettime is called from a preinit_array, or the fact that
__vdso_clock_gettime is mangled (for security purposes) and can't be used
directly, and also that clock_gettime can be intercepted.

The proposed solution takes care of all this as far as I can tell, and
significantly improve performances and some Scudo load tests with memory
reclaiming enabled.

@mcgrathr: please feel free to follow up on
https://reviews.llvm.org/D40657#940857 here. I posted a reply at
https://reviews.llvm.org/D40657#940974.

Diff Detail

Event Timeline

cryptoad created this revision.Nov 30 2017, 2:38 PM
krytarowski added inline comments.Nov 30 2017, 4:25 PM
lib/sanitizer_common/sanitizer_common_interceptors.inc
2051

This symbol name needs to be mangled for NetBSD.

lib/sanitizer_common/sanitizer_linux.cc
464

Perhaps we could define clock_gettime_symname similar to sigaction_symname.

cryptoad added inline comments.Dec 1 2017, 10:09 AM
lib/sanitizer_common/sanitizer_common_interceptors.inc
2051

So I don't think I understand what you mean here.
I have never touched a BSD, I am sorry if I am bit dense on this.
Which one has to be mangled and how?
It looks like the interceptor above also calls REAL(clock_gettime), so it would have triggered some NetBSD issue as well?

lib/sanitizer_common/sanitizer_linux.cc
464

Will do.

alekseyshl added inline comments.Dec 1 2017, 10:24 AM
lib/sanitizer_common/sanitizer_linux_libcdep.cc
716

This name is a bit too generic, can we change it to something like CanUseLibcClockGetTime()?

734

Does it mean that our allocator is going to be slow on BSD? Should we turn release to OS off by default there then?

cryptoad updated this revision to Diff 125177.Dec 1 2017, 10:53 AM
cryptoad marked an inline comment as done.

s/CanUseVDSO/CanUseLibcClockGetTime

cryptoad added inline comments.Dec 1 2017, 10:54 AM
lib/sanitizer_common/sanitizer_linux_libcdep.cc
734

Currently BSD doesn't have Scudo, I am not sure it's a big deal for the *San allocators.

krytarowski added inline comments.Dec 1 2017, 11:10 AM
lib/sanitizer_common/sanitizer_common_interceptors.inc
2051

Ah right, good catch. We need to fix more calls in this file: __clock_gettime50, __clock_getres50, __clock_settime50, __setitimer50, __getitimer50 and few more: __opendir30, __readdir30, __time50, __localtime_r50, __gmtime_r50, __gmtime50, __ctime50, __ctime_r50, __mktime50, __getpwnam50, __getpwuid50, __getpwnam_r50, __getpwuid_r50, __getpwent50, __glob30, __wait350, __wait450, __readdir_r30, __setlocale50, __scandir30, __sigtimedwait50, __sigemptyset14, __sigfillset14, __sigpending14, __sigprocmask14, __shmctl50, __times13, __stat50, __getutent50, __getutxent50, __getutxid50, __getutxline50.

Please ignore symbol demangling yourself, I will send a patch myself.

alekseyshl added inline comments.Dec 1 2017, 11:26 AM
lib/sanitizer_common/sanitizer_linux_libcdep.cc
734

Ok then, what's a proper BSD way to get time fast? Is there an alternative to syscall? It's a question to BSD folks, indeed.

krytarowski added inline comments.Dec 1 2017, 11:34 AM
lib/sanitizer_common/sanitizer_linux_libcdep.cc
734

I have got a simple local Scudo/NetBSD port, however I need to port tests to !GLIBC in order to make sure that it works reliably.

There is no vDSO as of now on NetBSD and no quick alternative.

krytarowski added inline comments.Dec 2 2017, 3:56 AM
lib/sanitizer_common/sanitizer_common_interceptors.inc
2051
cryptoad updated this revision to Diff 125584.Dec 5 2017, 11:56 AM
cryptoad marked 7 inline comments as done.

Rebasing.
Use MonotonicNanoTime for the Scudo RSS checks.

cryptoad marked an inline comment as done.Dec 5 2017, 11:57 AM
alekseyshl accepted this revision.Dec 5 2017, 11:59 AM
This revision is now accepted and ready to land.Dec 5 2017, 11:59 AM
cryptoad updated this revision to Diff 125585.Dec 5 2017, 12:05 PM
cryptoad marked 2 inline comments as done.

Moving the NetBSD clock_gettime syscall mangling to
sanitizer_syscall_generic.inc as is the case for others.

@krytarowski: I'd be grateful if you could confirm that this would work on NetBSD. Thanks!

krytarowski accepted this revision.Dec 6 2017, 8:34 AM

Looks fine.

cryptoad updated this revision to Diff 125752.Dec 6 2017, 9:24 AM

Adding a MonotonicNanoTime stub for mac.

cryptoad updated this revision to Diff 126380.Dec 11 2017, 8:56 AM

Rebasing one last time.

Herald added a subscriber: Restricted Project. · View Herald TranscriptDec 11 2017, 8:56 AM
This revision was automatically updated to reflect the committed changes.