Runtime library for HWASan, initial commit.
Does not randomize tags yet, does not handle stack or globals.
Details
Diff Detail
- Build Status
Buildable 12924 Build 12924: arc lint + arc unit
Event Timeline
compiler-rt/lib/hwasan/hwasan_linux.cc | ||
---|---|---|
87 | I have a local draft MSan patch adjusting similar code for NetBSD. It's right no incomplete as I need to add proper definition of INTERCEPT_FUNCTION(_lwp_exit); instead of calling it for each thread. diff --git a/lib/msan/msan_linux.cc b/lib/msan/msan_linux.cc index 4e6321fcb..d68509672 100644 --- a/lib/msan/msan_linux.cc +++ b/lib/msan/msan_linux.cc @@ -30,6 +30,7 @@ #include <sys/time.h> #include <sys/resource.h> +#include "interception/interception.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_procmaps.h" @@ -174,13 +175,28 @@ void InstallAtExitHandler() { // ---------------------- TSD ---------------- {{{1 +#if !SANITIZER_NETBSD static pthread_key_t tsd_key; +#endif + static bool tsd_key_inited = false; +#if SANITIZER_NETBSD +INTERCEPTOR(void, _lwp_exit) { + CHECK(tsd_key_inited); + MsanTSDDtor(GetCurrentThread()); + REAL(_lwp_exit)(); +} +#endif + void MsanTSDInit(void (*destructor)(void *tsd)) { CHECK(!tsd_key_inited); tsd_key_inited = true; +#if SANITIZER_NETBSD + INTERCEPT_FUNCTION(_lwp_exit); +#else CHECK_EQ(0, pthread_key_create(&tsd_key, destructor)); +#endif } static THREADLOCAL MsanThread* msan_current_thread; @@ -195,16 +211,20 @@ void SetCurrentThread(MsanThread *t) { msan_current_thread = t; // Make sure that MsanTSDDtor gets called at the end. CHECK(tsd_key_inited); +#if !SANITIZER_NETBSD pthread_setspecific(tsd_key, (void *)t); +#endif } void MsanTSDDtor(void *tsd) { +#if !SANITIZER_NETBSD MsanThread *t = (MsanThread*)tsd; if (t->destructor_iterations_ > 1) { t->destructor_iterations_--; CHECK_EQ(0, pthread_setspecific(tsd_key, tsd)); return; } +#endif msan_current_thread = nullptr; // Make sure that signal handler can not see a stale current thread pointer. atomic_signal_fence(memory_order_seq_cst); |
Note that hwasan stores current thread pointer in the TSD slot, because it needs to run on Android where thread-local (i.e. __thread) variables don't really work.
We plan to do the same with MSan in the future, but I don't know when.
compiler-rt/lib/hwasan/hwasan_interceptors.cc | ||
---|---|---|
138 | Do we expect any of this code to (ever? any time soon?) work on FREEBSD? NETBSD? |
compiler-rt/lib/hwasan/hwasan_interceptors.cc | ||
---|---|---|
138 | At least FreeBSD does. |
compiler-rt/lib/hwasan/hwasan_interceptors.cc | ||
---|---|---|
138 | NetBSD works on evbarm64. |
My top level comment: can we delete all non-aarch64 code?
The arch owners can reinstate it if needed, but they will only need it if/when they have the TBI feature in HW.
compiler-rt/lib/hwasan/hwasan.cc | ||
---|---|---|
61 | seems to be unused (nothings sets it to non zero), please remove. | |
100 | can we not have any of these #ifdefs? | |
182 | Is this one relevant? | |
195 | do we need this? | |
compiler-rt/lib/hwasan/hwasan_allocator.cc | ||
73 | oy! Please remove all of this non-aarch64 stuff. | |
compiler-rt/lib/hwasan/hwasan_flags.inc | ||
25 | seems unused, remove | |
compiler-rt/lib/hwasan/hwasan_interceptors.cc | ||
479 | mips? | |
compiler-rt/lib/hwasan/hwasan_linux.cc | ||
39 | Note for future, feel free to ignore now: we should try to have the zero shadow base | |
107 | What's the use case for #else? |
Addressed review comments.
Implemented halt_on_error=1.
Fixed tests to pass on unmodified kernels (see disable_allocator_tagging flag).
seems to be unused (nothings sets it to non zero), please remove.