diff --git a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h --- a/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h +++ b/llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h @@ -20,5 +20,16 @@ Invalid, ///< Not a valid destructor Kind. // TODO(dliew): Add more more kinds. }; + +/// Mode of ASan detect stack use after return +enum class AsanDetectStackUseAfterReturnMode { + Never, ///< Never detect stack use after return. + Runtime, ///< Detect stack use after return if runtime flag is enabled + ///< (ASAN_OPTIONS=detect_stack_use_after_return=1) + Always, ///< Always detect stack use after return. + Invalid, ///< Not a valid detect mode. +}; + } // namespace llvm + #endif diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -72,6 +72,7 @@ #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Instrumentation/AddressSanitizerCommon.h" +#include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h" #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Local.h" @@ -261,6 +262,23 @@ cl::desc("Check stack-use-after-return"), cl::Hidden, cl::init(true)); +static cl::opt + ClDetectStackUseAfterReturnMode( + "asan-detect-stack-use-after-return-mode", + cl::desc( + "Sets the ASan destructor kind. The default is to use the value " + "provided to the pass constructor"), + cl::values(clEnumValN(AsanDetectStackUseAfterReturnMode::Never, "never", + "Never detect stack use after return."), + clEnumValN(AsanDetectStackUseAfterReturnMode::Runtime, + "runtime", + "Detect stack use after return if " + "'asan-use-after-return' is true."), + clEnumValN(AsanDetectStackUseAfterReturnMode::Always, + "always", + "Always detect stack use after return.")), + cl::Hidden, cl::init(AsanDetectStackUseAfterReturnMode::Runtime)); + static cl::opt ClRedzoneByvalArgs("asan-redzone-byval-args", cl::desc("Create redzones for byval " "arguments (extra copy " @@ -3282,6 +3300,8 @@ uint64_t LocalStackSize = L.FrameSize; bool DoStackMalloc = ClUseAfterReturn && !ASan.CompileKernel && LocalStackSize <= kMaxStackMallocSize; + DoStackMalloc &= ClDetectStackUseAfterReturnMode != + AsanDetectStackUseAfterReturnMode::Never; bool DoDynamicAlloca = ClDynamicAllocaStack; // Don't do dynamic alloca or stack malloc if: // 1) There is inline asm: too often it makes assumptions on which registers @@ -3307,34 +3327,50 @@ // ? __asan_stack_malloc_N(LocalStackSize) // : nullptr; // void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize); - Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal( - kAsanOptionDetectUseAfterReturn, IRB.getInt32Ty()); - Value *UseAfterReturnIsEnabled = IRB.CreateICmpNE( - IRB.CreateLoad(IRB.getInt32Ty(), OptionDetectUseAfterReturn), - Constant::getNullValue(IRB.getInt32Ty())); - Instruction *Term = - SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false); - IRBuilder<> IRBIf(Term); - StackMallocIdx = StackMallocSizeClass(LocalStackSize); - assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass); - Value *FakeStackValue = - IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx], - ConstantInt::get(IntptrTy, LocalStackSize)); - IRB.SetInsertPoint(InsBefore); - FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term, - ConstantInt::get(IntptrTy, 0)); - - Value *NoFakeStack = - IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy)); - Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false); - IRBIf.SetInsertPoint(Term); - Value *AllocaValue = - DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca; - - IRB.SetInsertPoint(InsBefore); - LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack); - IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca); - DIExprFlags |= DIExpression::DerefBefore; + if (ClDetectStackUseAfterReturnMode == + AsanDetectStackUseAfterReturnMode::Runtime) { + Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal( + kAsanOptionDetectUseAfterReturn, IRB.getInt32Ty()); + Value *UseAfterReturnIsEnabled = IRB.CreateICmpNE( + IRB.CreateLoad(IRB.getInt32Ty(), OptionDetectUseAfterReturn), + Constant::getNullValue(IRB.getInt32Ty())); + Instruction *Term = + SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false); + IRBuilder<> IRBIf(Term); + StackMallocIdx = StackMallocSizeClass(LocalStackSize); + assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass); + Value *FakeStackValue = + IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx], + ConstantInt::get(IntptrTy, LocalStackSize)); + IRB.SetInsertPoint(InsBefore); + FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term, + ConstantInt::get(IntptrTy, 0)); + + Value *NoFakeStack = + IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy)); + Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false); + IRBIf.SetInsertPoint(Term); + Value *AllocaValue = DoDynamicAlloca + ? createAllocaForLayout(IRBIf, L, true) + : StaticAlloca; + + IRB.SetInsertPoint(InsBefore); + LocalStackBase = + createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack); + IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca); + DIExprFlags |= DIExpression::DerefBefore; + } else { + // assert(ClDetectStackUseAfterReturnMode == + // AsanDetectStackUseAfterReturnMode:Always) + StackMallocIdx = StackMallocSizeClass(LocalStackSize); + FakeStack = IRB.CreateCall(AsanStackMallocFunc[StackMallocIdx], + ConstantInt::get(IntptrTy, LocalStackSize)); + IRB.SetInsertPoint(InsBefore); + + Value *AllocValue = + DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca; + LocalStackBase = (FakeStack) ? FakeStack : AllocValue; + } } else { // void *FakeStack = nullptr; // void *LocalStackBase = alloca(LocalStackSize); diff --git a/llvm/test/Instrumentation/AddressSanitizer/lifetime-uar-uas.ll b/llvm/test/Instrumentation/AddressSanitizer/lifetime-uar-uas.ll --- a/llvm/test/Instrumentation/AddressSanitizer/lifetime-uar-uas.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/lifetime-uar-uas.ll @@ -7,6 +7,14 @@ ; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return=0 -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS ; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-use-after-return=1 -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS ; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return=1 -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=never -asan-use-after-scope=0 -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=never -asan-use-after-scope=0 -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=never -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=never -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=always -asan-use-after-scope=0 -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=always -asan-use-after-scope=0 -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=always -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=always -asan-use-after-scope=1 -S | FileCheck %s --check-prefix=CHECK-UAS target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" diff --git a/llvm/test/Instrumentation/AddressSanitizer/localescape.ll b/llvm/test/Instrumentation/AddressSanitizer/localescape.ll --- a/llvm/test/Instrumentation/AddressSanitizer/localescape.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/localescape.ll @@ -2,6 +2,14 @@ ; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return -asan-stack-dynamic-alloca -S | FileCheck %s ; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-use-after-return=0 -asan-stack-dynamic-alloca=0 -S | FileCheck %s ; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return=0 -asan-stack-dynamic-alloca=0 -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=always -asan-stack-dynamic-alloca -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=always -asan-stack-dynamic-alloca -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=always -asan-stack-dynamic-alloca=0 -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=always -asan-stack-dynamic-alloca=0 -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=never -asan-stack-dynamic-alloca -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=never -asan-stack-dynamic-alloca -S | FileCheck %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=never -asan-stack-dynamic-alloca=0 -S | FileCheck %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=never -asan-stack-dynamic-alloca=0 -S | FileCheck %s target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32" target triple = "i686-pc-windows-msvc18.0.0" diff --git a/llvm/test/Instrumentation/AddressSanitizer/stack-poisoning.ll b/llvm/test/Instrumentation/AddressSanitizer/stack-poisoning.ll --- a/llvm/test/Instrumentation/AddressSanitizer/stack-poisoning.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/stack-poisoning.ll @@ -1,7 +1,11 @@ -; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-use-after-return -S | FileCheck --check-prefix=CHECK-UAR %s -; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return -S | FileCheck --check-prefix=CHECK-UAR %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-use-after-return -S | FileCheck --check-prefixes=CHECK-UAR,CHECK-UAR-RUNTIME %s +; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return -S | FileCheck --check-prefixes=CHECK-UAR,CHECK-UAR-RUNTIME %s ; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-use-after-return=0 -S | FileCheck --check-prefix=CHECK-PLAIN %s ; RUN: opt < %s -passes='asan-pipeline' -asan-use-after-return=0 -S | FileCheck --check-prefix=CHECK-PLAIN %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=always -S | FileCheck --check-prefixes=CHECK-UAR,CHECK-UAR-NORUNTIME %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=always -S | FileCheck --check-prefix=CHECK-UAR %s +; RUN: opt < %s -asan -asan-module -enable-new-pm=0 -asan-detect-stack-use-after-return-mode=never -S | FileCheck --check-prefix=CHECK-PLAIN %s +; RUN: opt < %s -passes='asan-pipeline' -asan-detect-stack-use-after-return-mode=never -S | FileCheck --check-prefix=CHECK-PLAIN %s target datalayout = "e-i64:64-f80:128-s:64-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -14,10 +18,12 @@ ; CHECK-PLAIN: ret void ; CHECK-UAR-LABEL: Bar -; CHECK-UAR: load i32, i32* @__asan_option_detect_stack_use_after_return -; CHECK-UAR: label +; CHECK-UAR-RUNTIME: load i32, i32* @__asan_option_detect_stack_use_after_return +; CHECK-UAR-RUNTIME: label +; CHECK-UAR-NORUNTIME-NOT: load i32, i32* @__asan_option_detect_stack_use_after_return +; CHECK-UAR-NORUNTIME-NOT: label ; CHECK-UAR: call i64 @__asan_stack_malloc_4 -; CHECK-UAR: label +; CHECK-UAR-RUNTIME: label ; Poison red zones. ; CHECK-UAR: store i64 -1007680412564983311 ; CHECK-UAR: store i64 72057598113936114 diff --git a/llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll b/llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll --- a/llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/stack_dynamic_alloca.ll @@ -1,11 +1,19 @@ ; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca \ -; RUN: -asan-use-after-return -S -enable-new-pm=0 | FileCheck %s +; RUN: -asan-use-after-return -S -enable-new-pm=0 | FileCheck %s --check-prefixes=CHECK-RUNTIME ; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca \ -; RUN: -asan-use-after-return -S | FileCheck %s +; RUN: -asan-use-after-return -S | FileCheck %s --check-prefixes=CHECK-RUNTIME ; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca -asan-mapping-scale=5 \ -; RUN: -asan-use-after-return -S -enable-new-pm=0 | FileCheck %s +; RUN: -asan-use-after-return -S -enable-new-pm=0 | FileCheck %s --check-prefixes=CHECK-RUNTIME ; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca -asan-mapping-scale=5 \ -; RUN: -asan-use-after-return -S | FileCheck %s +; RUN: -asan-use-after-return -S | FileCheck %s --check-prefixes=CHECK-RUNTIME +; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca \ +; RUN: -asan-detect-stack-use-after-return-mode=always -S -enable-new-pm=0 | FileCheck %s --check-prefixes=CHECK-NORUNTIME +; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca \ +; RUN: -asan-detect-stack-use-after-return-mode=always -S | FileCheck %s --check-prefixes=CHECK-NORUNTIME +; RUN: opt < %s -asan -asan-module -asan-stack-dynamic-alloca -asan-mapping-scale=5 \ +; RUN: -asan-detect-stack-use-after-return-mode=always -S -enable-new-pm=0 | FileCheck %s --check-prefixes=CHECK-NORUNTIME +; RUN: opt < %s -passes='asan-pipeline' -asan-stack-dynamic-alloca -asan-mapping-scale=5 \ +; RUN: -asan-detect-stack-use-after-return-mode=always -S | FileCheck %s --check-prefixes=CHECK-NORUNTIME target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -14,7 +22,8 @@ ; CHECK-LABEL: Func1 ; CHECK: entry: -; CHECK: load i32, i32* @__asan_option_detect_stack_use_after_return +; CHECK-RUNTIME: load i32, i32* @__asan_option_detect_stack_use_after_return +; CHECK-NORUNTIME-NOT: load i32, i32* @__asan_option_detect_stack_use_after_return ; CHECK: [[UAR_ENABLED_BB:^[0-9]+]]: ; CHECK: [[FAKE_STACK_RT:%[0-9]+]] = call i64 @__asan_stack_malloc_