This is an archive of the discontinued LLVM Phabricator instance.

[scudo] Move thread local variables into their own files
ClosedPublic

Authored by cryptoad on Apr 24 2017, 10:07 AM.

Details

Summary

This change introduces scudo_tls.h & scudo_tls_linux.cpp, where we move the
thread local variables used by the allocator, namely the cache, quarantine
cache & prng. ScudoThreadContext will hold those. This patch doesn't
introduce any new platform support yet, this will be the object of a later
patch. This also changes the PRNG so that the structure can be POD.

Event Timeline

cryptoad created this revision.Apr 24 2017, 10:07 AM
cryptoad updated this revision to Diff 96458.Apr 24 2017, 1:34 PM

Refactoring a couple of things.

alekseyshl added inline comments.Apr 24 2017, 2:04 PM
lib/scudo/scudo_allocator.h
108

Two more spaces indent.

lib/scudo/scudo_tls_linux.cpp
31

Why those are two separate flags (some combinations are invalid) and not an enum State field in the ThreadLocalContext?

cryptoad marked an inline comment as done.Apr 24 2017, 2:18 PM
cryptoad added inline comments.
lib/scudo/scudo_tls_linux.cpp
31

That is a valid point. I'm going to change it.

cryptoad updated this revision to Diff 96469.Apr 24 2017, 2:29 PM

Addressing Aleksey's comments.

cryptoad marked an inline comment as done.Apr 24 2017, 2:30 PM
alekseyshl accepted this revision.Apr 24 2017, 3:16 PM
This revision is now accepted and ready to land.Apr 24 2017, 3:16 PM
dvyukov edited edge metadata.Apr 26 2017, 11:59 AM

otherwise looks good

lib/scudo/scudo_tls_linux.cpp
60

The fast path needs to be inlinable (defined in header file).

cryptoad added inline comments.Apr 26 2017, 1:46 PM
lib/scudo/scudo_tls_linux.cpp
60

Previous reviews of similar changes have edged towards less ifdef and a clear separation of the platform specific codes in separate cpp, yielding less complicated code. The issue I can see happening is multiplication of ifdefs in this header when SANITIZER_ANDROID comes into play next if defining the fast path in the header. Is this OK?

dvyukov added inline comments.Apr 27 2017, 3:25 AM
lib/scudo/scudo_tls_linux.cpp
60

You can do the same we do for source files. Namely:

// scudo_tls.h
...
#include <scudo_tls_linux.h>
#include <scudo_tls_android.h>
...

// scudo_tls_linux.h
#if SANITIZER_LINUX
... necessary declarations and bodies of inline functions go here ...
#endif

// scudo_tls_android.h
#if SANITIZER_ANDROID
... necessary declarations and bodies of inline functions go here ...
#endif

I understand their concerns, but some pieces of sanitizer runtimes are kinda performance-critical. We do have mixed per-platform code in interception.h library, in tsan_platform.h. And tsan_update_shadow_word_inl.h is just awful in the name of performance. These are intentional compromises.

cryptoad updated this revision to Diff 96933.Apr 27 2017, 9:55 AM

Addressing Dmitry's comments.

Introduce a scudo_tls_linux.h file, included within scudo_tls.h with fastpath
functions definition (initThreadMaybe & getThreadContext).

An additionaly change is aligning ScudoThreadContext on 64-bytes.

cryptoad marked 3 inline comments as done.Apr 27 2017, 9:56 AM
alekseyshl added inline comments.Apr 27 2017, 10:14 AM
lib/scudo/scudo_tls_linux.h
35

Why not ALWAYS_INLINE then?

dvyukov accepted this revision.Apr 27 2017, 10:17 AM

LGTM on my side, but wait for Aleksey if he has more comments

cryptoad updated this revision to Diff 96939.Apr 27 2017, 10:21 AM

Changing the fastpath functions to ALWAYS_INLINE as per Aleksey's suggestion.

cryptoad marked an inline comment as done.Apr 27 2017, 10:22 AM
alekseyshl accepted this revision.Apr 27 2017, 10:24 AM

Other than these minor comments, LGTM.

lib/scudo/scudo_allocator.cpp
209

What was the reason of removing it? This thread implements RSS checks and heap profiling, don't you want this functionality supported in Scusdo too?

cryptoad added inline comments.Apr 27 2017, 10:41 AM
lib/scudo/scudo_allocator.cpp
209

I removed it (for now) for a couple of reasons:

  • I currently do not have a __sanitizer_print_memory_profile function defined, so the heap profiling isn't of a much use;
  • the soft rss check uses a writeable function pointer as a callback, which is something I am generally trying to avoid in Scudo;

My plan is to have a second look once things stabilize a bit so that this function can be of use, but I figure it wasn't needed for now.

Let me know if that makes sense, or if you'd rather have it stay!

alekseyshl added inline comments.Apr 27 2017, 10:44 AM
lib/scudo/scudo_allocator.cpp
209

I was merely curious and you explained the reasoning, no need to bring it back. Thanks!

Thank you Dmitry & Aleksey.
I appreciate the time you guys are spending reviewing those changes, and the valuable feedback you are providing!

cryptoad closed this revision.Apr 27 2017, 1:34 PM