[PATCH] D56038: hwasan: Implement lazy thread initialization for the interceptor ABI.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 21 16:52:51 PST 2018


pcc created this revision.
pcc added a reviewer: eugenis.
Herald added subscribers: jfb, hiraditya, kubamracek.

The problem is similar to D55986 <https://reviews.llvm.org/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.


Repository:
  rL LLVM

https://reviews.llvm.org/D56038

Files:
  clang/include/clang/Basic/CodeGenOptions.h
  clang/include/clang/Driver/CC1Options.td
  clang/include/clang/Driver/Options.td
  clang/include/clang/Driver/SanitizerArgs.h
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/SanitizerArgs.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGen/default-function-attr.c
  clang/test/Driver/fsanitize.c
  compiler-rt/lib/hwasan/hwasan_interceptors.cc
  compiler-rt/lib/hwasan/hwasan_linux.cc
  llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
  llvm/test/Instrumentation/HWAddressSanitizer/lazy-thread-init.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56038.179390.patch
Type: text/x-patch
Size: 11998 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181222/7e673165/attachment.bin>


More information about the llvm-commits mailing list