Author: pcc Date: Fri Jan 4 11:27:04 2019 New Revision: 350429 URL: http://llvm.org/viewvc/llvm-project?rev=350429&view=rev Log: 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 Added: cfe/trunk/test/CodeGen/default-function-attr.c Modified: cfe/trunk/include/clang/Basic/CodeGenOptions.h cfe/trunk/include/clang/Driver/CC1Options.td cfe/trunk/include/clang/Driver/Options.td cfe/trunk/include/clang/Driver/SanitizerArgs.h cfe/trunk/lib/CodeGen/CGCall.cpp cfe/trunk/lib/Driver/SanitizerArgs.cpp cfe/trunk/lib/Frontend/CompilerInvocation.cpp cfe/trunk/test/Driver/fsanitize.c Modified: cfe/trunk/include/clang/Basic/CodeGenOptions.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/CodeGenOptions.h?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/CodeGenOptions.h (original) +++ cfe/trunk/include/clang/Basic/CodeGenOptions.h Fri Jan 4 11:27:04 2019 @@ -286,6 +286,8 @@ public: /// Set of XRay instrumentation kinds to emit. XRayInstrSet XRayInstrumentationBundle; + std::vector<std::string> DefaultFunctionAttrs; + public: // Define accessors/mutators for code generation options of enumeration type. #define CODEGENOPT(Name, Bits, Default) Modified: cfe/trunk/include/clang/Driver/CC1Options.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/include/clang/Driver/CC1Options.td (original) +++ cfe/trunk/include/clang/Driver/CC1Options.td Fri Jan 4 11:27:04 2019 @@ -163,6 +163,8 @@ let Flags = [CC1Option, CC1AsOption, NoD def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">; def debug_info_macro : Flag<["-"], "debug-info-macro">, HelpText<"Emit macro debug information">; +def default_function_attr : Separate<["-"], "default-function-attr">, + HelpText<"Apply given attribute to all functions">; def dwarf_version_EQ : Joined<["-"], "dwarf-version=">; def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">; def fdebug_compilation_dir : Separate<["-"], "fdebug-compilation-dir">, Modified: cfe/trunk/include/clang/Driver/Options.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/include/clang/Driver/Options.td (original) +++ cfe/trunk/include/clang/Driver/Options.td Fri Jan 4 11:27:04 2019 @@ -998,6 +998,10 @@ def fno_sanitize_address_use_odr_indicat : Flag<["-"], "fno-sanitize-address-use-odr-indicator">, Group<f_clang_Group>, HelpText<"Disable ODR indicator globals">; +def fsanitize_hwaddress_abi_EQ + : Joined<["-"], "fsanitize-hwaddress-abi=">, + Group<f_clang_Group>, + HelpText<"Select the HWAddressSanitizer ABI to target (interceptor or platform, default interceptor)">; def fsanitize_recover : Flag<["-"], "fsanitize-recover">, Group<f_clang_Group>; def fno_sanitize_recover : Flag<["-"], "fno-sanitize-recover">, Flags<[CoreOption, DriverOption]>, Modified: cfe/trunk/include/clang/Driver/SanitizerArgs.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/SanitizerArgs.h?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/include/clang/Driver/SanitizerArgs.h (original) +++ cfe/trunk/include/clang/Driver/SanitizerArgs.h Fri Jan 4 11:27:04 2019 @@ -39,6 +39,7 @@ class SanitizerArgs { bool AsanPoisonCustomArrayCookie = false; bool AsanGlobalsDeadStripping = false; bool AsanUseOdrIndicator = false; + std::string HwasanAbi; bool LinkCXXRuntimes = false; bool NeedPIE = false; bool SafeStackRuntime = false; Modified: cfe/trunk/lib/CodeGen/CGCall.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGCall.cpp (original) +++ cfe/trunk/lib/CodeGen/CGCall.cpp Fri Jan 4 11:27:04 2019 @@ -1816,6 +1816,12 @@ void CodeGenModule::ConstructDefaultFnAt if (CodeGenOpts.FlushDenorm) FuncAttrs.addAttribute("nvptx-f32ftz", "true"); } + + for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) { + StringRef Var, Value; + std::tie(Var, Value) = Attr.split('='); + FuncAttrs.addAttribute(Var, Value); + } } void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) { Modified: cfe/trunk/lib/Driver/SanitizerArgs.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/SanitizerArgs.cpp?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/lib/Driver/SanitizerArgs.cpp (original) +++ cfe/trunk/lib/Driver/SanitizerArgs.cpp Fri Jan 4 11:27:04 2019 @@ -741,6 +741,18 @@ SanitizerArgs::SanitizerArgs(const ToolC AsanUseAfterScope = false; } + if (AllAddedKinds & HWAddress) { + if (Arg *HwasanAbiArg = + Args.getLastArg(options::OPT_fsanitize_hwaddress_abi_EQ)) { + HwasanAbi = HwasanAbiArg->getValue(); + if (HwasanAbi != "platform" && HwasanAbi != "interceptor") + D.Diag(clang::diag::err_drv_invalid_value) + << HwasanAbiArg->getAsString(Args) << HwasanAbi; + } else { + HwasanAbi = "interceptor"; + } + } + if (AllAddedKinds & SafeStack) { // SafeStack runtime is built into the system on Fuchsia. SafeStackRuntime = !TC.getTriple().isOSFuchsia(); @@ -913,6 +925,11 @@ void SanitizerArgs::addArgs(const ToolCh if (AsanUseOdrIndicator) CmdArgs.push_back("-fsanitize-address-use-odr-indicator"); + if (!HwasanAbi.empty()) { + CmdArgs.push_back("-default-function-attr"); + CmdArgs.push_back(Args.MakeArgString("hwasan-abi=" + HwasanAbi)); + } + // MSan: Workaround for PR16386. // ASan: This is mainly to help LSan with cases such as // https://github.com/google/sanitizers/issues/373 Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original) +++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Fri Jan 4 11:27:04 2019 @@ -1319,6 +1319,8 @@ static bool ParseCodeGenArgs(CodeGenOpti Opts.SpeculativeLoadHardening = Args.hasArg(OPT_mspeculative_load_hardening); + Opts.DefaultFunctionAttrs = Args.getAllArgValues(OPT_default_function_attr); + return Success; } Added: cfe/trunk/test/CodeGen/default-function-attr.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/default-function-attr.c?rev=350429&view=auto ============================================================================== --- cfe/trunk/test/CodeGen/default-function-attr.c (added) +++ cfe/trunk/test/CodeGen/default-function-attr.c Fri Jan 4 11:27:04 2019 @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -default-function-attr foo=bar -emit-llvm %s -o - | FileCheck %s + +// CHECK: define void @foo() #[[X:[0-9]+]] +void foo() {} + +// CHECK: attributes #[[X]] = {{.*}} "foo"="bar" Modified: cfe/trunk/test/Driver/fsanitize.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/fsanitize.c?rev=350429&r1=350428&r2=350429&view=diff ============================================================================== --- cfe/trunk/test/Driver/fsanitize.c (original) +++ cfe/trunk/test/Driver/fsanitize.c Fri Jan 4 11:27:04 2019 @@ -837,3 +837,11 @@ // // RUN: %clang -target x86_64-linux-gnu -fsanitize=scudo,kernel-memory %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SCUDO-KMSAN // CHECK-SCUDO-KMSAN: error: invalid argument '-fsanitize=kernel-memory' not allowed with '-fsanitize=scudo' + +// RUN: %clang -target x86_64-linux-gnu -fsanitize=hwaddress %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-HWASAN-INTERCEPTOR-ABI +// RUN: %clang -target x86_64-linux-gnu -fsanitize=hwaddress -fsanitize-hwaddress-abi=interceptor %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-HWASAN-INTERCEPTOR-ABI +// RUN: %clang -target x86_64-linux-gnu -fsanitize=hwaddress -fsanitize-hwaddress-abi=platform %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-HWASAN-PLATFORM-ABI +// RUN: %clang -target x86_64-linux-gnu -fsanitize=hwaddress -fsanitize-hwaddress-abi=foo %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-HWASAN-FOO-ABI +// CHECK-HWASAN-INTERCEPTOR-ABI: "-default-function-attr" "hwasan-abi=interceptor" +// CHECK-HWASAN-PLATFORM-ABI: "-default-function-attr" "hwasan-abi=platform" +// CHECK-HWASAN-FOO-ABI: error: invalid value 'foo' in '-fsanitize-hwaddress-abi=foo' _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits