OSDN Git Service

hwasan: Implement lazy thread initialization for the interceptor ABI.
authorPeter Collingbourne <peter@pcc.me.uk>
Fri, 4 Jan 2019 19:27:04 +0000 (19:27 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Fri, 4 Jan 2019 19:27:04 +0000 (19:27 +0000)
commit1011e428fd0908dbabe4c6387ef65ba1f92c39a2
tree616066dcf0198621d1a98e8e00b78e094fccbe9a
parent1f63024b270c0eeadc9ca129eebb5c62603f425e
hwasan: Implement lazy thread initialization for the interceptor ABI.

The problem is similar to D55986 but for threads: a process with the
interceptor hwasan library loaded might have some threads started by
instrumented libraries and some by uninstrumented libraries, and we
need to be able to run instrumented code on the latter.

The solution is to perform per-thread initialization lazily. If a
function needs to access shadow memory or add itself to the per-thread
ring buffer its prologue checks to see whether the value in the
sanitizer TLS slot is null, and if so it calls __hwasan_thread_enter
and reloads from the TLS slot. The runtime does the same thing if it
needs to access this data structure.

This change means that the code generator needs to know whether we
are targeting the interceptor runtime, since we don't want to pay
the cost of lazy initialization when targeting a platform with native
hwasan support. A flag -fsanitize-hwaddress-abi={interceptor,platform}
has been introduced for selecting the runtime ABI to target. The
default ABI is set to interceptor since it's assumed that it will
be more common that users will be compiling application code than
platform code.

Because we can no longer assume that the TLS slot is initialized,
the pthread_create interceptor is no longer necessary, so it has
been removed.

Ideally, lazy initialization should only cost one instruction in the
hot path, but at present the call may cause us to spill arguments
to the stack, which means more instructions in the hot path (or
theoretically in the cold path if the spills are moved with shrink
wrapping). With an appropriately chosen calling convention for
the per-thread initialization function (TODO) the hot path should
always need just one instruction and the cold path should need two
instructions with no spilling required.

Differential Revision: https://reviews.llvm.org/D56038

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350429 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
test/Instrumentation/HWAddressSanitizer/lazy-thread-init.ll [new file with mode: 0644]