diff --git a/compiler-rt/test/hwasan/TestCases/stack-uas.c b/compiler-rt/test/hwasan/TestCases/stack-uas.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/hwasan/TestCases/stack-uas.c @@ -0,0 +1,69 @@ +// Tests use-after-scope detection and reporting. +// RUN: %clang_hwasan -mllvm -hwasan-use-after-scope -g %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang_hwasan -mllvm -hwasan-use-after-scope -g %s -o %t && not %env_hwasan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOSYM + +// RUN: %clang_hwasan -mllvm -hwasan-use-after-scope=false -g %s -o %t && %run %t 2>&1 +// Use after scope is turned off by default. +// RUN: %clang_hwasan -g %s -o %t && %run %t 2>&1 + + +// RUN: %clang_hwasan -fexperimental-new-pass-manager -mllvm -hwasan-use-after-scope -g %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang_hwasan -fno-experimental-new-pass-manager -mllvm -hwasan-use-after-scope -g %s -o %t && not %run %t 2>&1 | FileCheck %s + +// REQUIRES: stable-runtime + +// Stack histories currently are not recorded on x86. +// XFAIL: x86_64 + +void USE(void *x) { // pretend_to_do_something(void *x) + __asm__ __volatile__("" + : + : "r"(x) + : "memory"); +} + +__attribute__((noinline)) void Unrelated1() { + int A[2]; + USE(&A[0]); +} +__attribute__((noinline)) void Unrelated2() { + int BB[3]; + USE(&BB[0]); +} +__attribute__((noinline)) void Unrelated3() { + int CCC[4]; + USE(&CCC[0]); +} + +__attribute__((noinline)) char buggy() { + char *volatile p; + { + char zzz[0x1000]; + p = zzz; + } + return *p; +} + +int main() { + Unrelated1(); + Unrelated2(); + Unrelated3(); + char p = buggy(); + return p; + // CHECK: READ of size 1 at + // CHECK: #0 {{.*}} in buggy{{.*}}stack-uas.c:[[@LINE-10]] + // CHECK: Cause: stack tag-mismatch + // CHECK: is located in stack of thread + // CHECK: Potentially referenced stack objects: + // CHECK-NEXT: zzz in buggy {{.*}}stack-uas.c:[[@LINE-17]] + // CHECK-NEXT: Memory tags around the buggy address + + // NOSYM: Previously allocated frames: + // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}} + // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}} + // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}} + // NOSYM-NEXT: record_addr:0x{{.*}} record:0x{{.*}} ({{.*}}/stack-uas.c.tmp+0x{{.*}}){{$}} + // NOSYM-NEXT: Memory tags around the buggy address + + // CHECK: SUMMARY: HWAddressSanitizer: tag-mismatch {{.*}} in buggy +} diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-capture.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-capture.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-capture.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-capture.cpp @@ -1,8 +1,8 @@ -// RUN: %clangxx_asan %stdcxx11 -O1 -fsanitize-address-use-after-scope %s -o %t && \ -// RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan -// XFAIL: * +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope --std=c++11 -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s + +// REQUIRES: aarch64-target-arch #include @@ -12,8 +12,9 @@ int x = 0; f = [&x]() __attribute__((noinline)) { return x; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in {{.*}}use-after-scope-capture.cpp:[[@LINE-2]] + // CHECK: Cause: stack tag-mismatch }; } return f(); // BOOM diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-dtor-order.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-dtor-order.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-dtor-order.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-dtor-order.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch #include @@ -10,7 +11,7 @@ explicit IntHolder(int *val = 0) : val_(val) {} __attribute__((noinline)) ~IntHolder() { printf("Value: %d\n", *val_); // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in IntHolder::~IntHolder{{.*}}.cpp:[[@LINE-2]] } void set(int *val) { val_ = val; } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-goto.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-goto.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-goto.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-goto.cpp @@ -1,10 +1,11 @@ -// RUN: %clangxx_asan -O0 -fsanitize-address-use-after-scope %s -o %t && %run %t +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O0 %s -o %t && %run %t // Function jumps over variable initialization making lifetime analysis // ambiguous. Asan should ignore such variable and program must not fail. -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch #include diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-if.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-if.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-if.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-if.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch int *p; bool b = true; @@ -13,6 +14,7 @@ p = x + 1; } return *p; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}.cpp:[[@LINE-2]] + // CHECK: Cause: stack tag-mismatch } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-inlined.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-inlined.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-inlined.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-inlined.cpp @@ -1,15 +1,15 @@ +// This is the ASAN test of the same name ported to HWAsan. + // Test with "-O2" only to make sure inlining (leading to use-after-scope) // happens. "always_inline" is not enough, as Clang doesn't emit // llvm.lifetime intrinsics at -O0. // -// RUN: %clangxx_asan -O2 -fsanitize-address-use-after-scope %s -o %t && \ +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O2 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * -int *arr; +// REQUIRES: aarch64-target-arch +int *arr; __attribute__((always_inline)) void inlined(int arg) { int x[5]; for (int i = 0; i < arg; i++) @@ -20,12 +20,6 @@ int main(int argc, char *argv[]) { inlined(argc); return arr[argc - 1]; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope - // CHECK: READ of size 4 at 0x{{.*}} thread T0 - // CHECK: #0 0x{{.*}} in main - // CHECK: {{.*}}use-after-scope-inlined.cpp:[[@LINE-4]] - // CHECK: Address 0x{{.*}} is located in stack of thread T0 at offset [[OFFSET:[^ ]*]] in frame - // CHECK: {{.*}} in main - // CHECK: This frame has - // CHECK: {{\[}}[[OFFSET]], {{.*}}) 'x.i' (line [[@LINE-15]]) + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch + // CHECK: Cause: stack tag-mismatch } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-bug.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-bug.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-bug.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-bug.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch volatile int *p; @@ -13,8 +14,7 @@ p = x + i; } return *p; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope-loop-bug.cpp:[[@LINE-2]] - // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame - // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x' + // CHECK: Cause: stack tag-mismatch } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-removed.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-removed.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-removed.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop-removed.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch #include @@ -14,8 +15,7 @@ p = &x; } return *p; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope-loop-removed.cpp:[[@LINE-2]] - // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame - // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x' + // CHECK: Cause: stack tag-mismatch } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-loop.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-loop.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch int *p[3]; @@ -12,6 +13,7 @@ p[i] = &x; } return **p; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}.cpp:[[@LINE-2]] + // CHECK: Cause: stack tag-mismatch } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-nobug.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-nobug.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-nobug.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-nobug.cpp @@ -1,7 +1,8 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && %run %t -// -// Not expected to work yet with HWAsan. -// XFAIL: * +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && %run %t + +// REQUIRES: aarch64-target-arch #include #include diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-temp.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-temp.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-temp.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-temp.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan %stdcxx11 -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -std=c++11 -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch struct IntHolder { int val; @@ -17,7 +18,8 @@ int main(int argc, char *argv[]) { save({argc}); int x = saved->val; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope-temp.cpp:[[@LINE-2]] + // CHECK: Cause: stack tag-mismatch return x; } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-temp2.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-temp2.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-temp2.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-temp2.cpp @@ -1,8 +1,9 @@ -// RUN: %clangxx_asan %stdcxx11 -O1 -fsanitize-address-use-after-scope %s -o %t && \ +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -std=c++11 -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch struct IntHolder { __attribute__((noinline)) const IntHolder &Self() const { @@ -16,7 +17,8 @@ int main(int argc, char *argv[]) { saved = &IntHolder().Self(); int x = saved->val; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope-temp2.cpp:[[@LINE-2]] + // CHECK: Cause: stack tag-mismatch return x; } diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope-types.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope-types.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope-types.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope-types.cpp @@ -1,18 +1,20 @@ -// RUN: %clangxx_asan %stdcxx11 -O0 -fsanitize-address-use-after-scope %s -o %t +// This is the ASAN test of the same name ported to HWAsan. + +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -std=c++11 -O0 %s -o %t // RUN: not %run %t 0 2>&1 | FileCheck %s // RUN: not %run %t 1 2>&1 | FileCheck %s // RUN: not %run %t 2 2>&1 | FileCheck %s // RUN: not %run %t 3 2>&1 | FileCheck %s // RUN: not %run %t 4 2>&1 | FileCheck %s // RUN: not %run %t 5 2>&1 | FileCheck %s -// RUN: not %run %t 6 2>&1 | FileCheck %s +// The std::vector case is broken because of limited lifetime tracking. +// TODO(fmayer): Fix and enable. // RUN: not %run %t 7 2>&1 | FileCheck %s // RUN: not %run %t 8 2>&1 | FileCheck %s // RUN: not %run %t 9 2>&1 | FileCheck %s // RUN: not %run %t 10 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch #include #include @@ -46,10 +48,9 @@ } ptr.Access(); - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #{{[0-9]+}} 0x{{.*}} in {{(void )?test.*\((void)?\) .*}}use-after-scope-types.cpp - // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame - // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x' + // CHECK: Cause: stack tag-mismatch } int main(int argc, char **argv) { diff --git a/compiler-rt/test/hwasan/TestCases/use-after-scope.cpp b/compiler-rt/test/hwasan/TestCases/use-after-scope.cpp --- a/compiler-rt/test/hwasan/TestCases/use-after-scope.cpp +++ b/compiler-rt/test/hwasan/TestCases/use-after-scope.cpp @@ -1,12 +1,9 @@ -// RUN: %clangxx_asan -O1 -fsanitize-address-use-after-scope %s -o %t && \ -// RUN: not %run %t 2>&1 | FileCheck %s +// This is the ASAN test of the same name ported to HWAsan. -// -fsanitize-address-use-after-scope is now on by default: -// RUN: %clangxx_asan -O1 %s -o %t && \ +// RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -O1 %s -o %t && \ // RUN: not %run %t 2>&1 | FileCheck %s -// -// Not expected to work yet with HWAsan. -// XFAIL: * + +// REQUIRES: aarch64-target-arch volatile int *p = 0; @@ -16,9 +13,8 @@ p = &x; } *p = 5; // BOOM - // CHECK: ERROR: AddressSanitizer: stack-use-after-scope + // CHECK: ERROR: HWAddressSanitizer: tag-mismatch // CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope.cpp:[[@LINE-2]] - // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame - // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x' + // CHECK: Cause: stack tag-mismatch return 0; } diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -17,7 +17,9 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/Analysis/PostDominators.h" #include "llvm/Analysis/StackSafetyAnalysis.h" +#include "llvm/Analysis/ValueTracking.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/BasicBlock.h" @@ -26,6 +28,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Dominators.h" #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InlineAsm.h" @@ -41,6 +44,7 @@ #include "llvm/IR/Value.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" +#include "llvm/PassRegistry.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -115,6 +119,11 @@ cl::Hidden, cl::desc("Use Stack Safety analysis results"), cl::Optional); +static cl::opt + ClUseAfterScope("hwasan-use-after-scope", + cl::desc("detect use after scope within function"), + cl::Hidden, cl::init(false)); + static cl::opt ClUARRetagToZero( "hwasan-uar-retag-to-zero", cl::desc("Clear alloca tags before returning from the function to allow " @@ -231,9 +240,22 @@ // No one should use the option directly. #pragma GCC poison ClUseStackSafety } + +bool shouldDetectUseAfterScope(const Triple &TargetTriple) { + return ClUseAfterScope && shouldInstrumentStack(TargetTriple); +#pragma GCC poison ClUseAfterScope +} + /// An instrumentation pass implementing detection of addressability bugs /// using tagged pointers. class HWAddressSanitizer { +private: + struct AllocaInfo { + AllocaInst *AI; + SmallVector LifetimeStart; + SmallVector LifetimeEnd; + }; + public: HWAddressSanitizer(Module &M, bool CompileKernel, bool Recover, const StackSafetyGlobalInfo *SSI) @@ -248,7 +270,7 @@ void setSSI(const StackSafetyGlobalInfo *S) { SSI = S; } - bool sanitizeFunction(Function &F); + bool sanitizeFunction(Function &F, DominatorTree *DT, PostDominatorTree *PDT); void initializeModule(); void createHwasanCtorComdat(); @@ -271,13 +293,15 @@ Instruction *I, SmallVectorImpl &Interesting); bool isInterestingAlloca(const AllocaInst &AI); - bool tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, size_t Size); + void tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, size_t Size); Value *tagPointer(IRBuilder<> &IRB, Type *Ty, Value *PtrLong, Value *Tag); Value *untagPointer(IRBuilder<> &IRB, Value *PtrLong); bool instrumentStack( - SmallVectorImpl &Allocas, + MapVector &AllocasToInstrument, + SmallVector &UnrecognizedLifetimes, DenseMap> &AllocaDbgMap, - SmallVectorImpl &RetVec, Value *StackTag); + SmallVectorImpl &RetVec, Value *StackTag, + DominatorTree *DT, PostDominatorTree *PDT); Value *readRegister(IRBuilder<> &IRB, StringRef Name); bool instrumentLandingPads(SmallVectorImpl &RetVec); Value *getNextTagWithCall(IRBuilder<> &IRB); @@ -326,6 +350,7 @@ void init(Triple &TargetTriple, bool InstrumentWithCalls); unsigned getObjectAlignment() const { return 1U << Scale; } }; + ShadowMapping Mapping; Type *VoidTy = Type::getVoidTy(M.getContext()); @@ -342,6 +367,7 @@ bool InstrumentLandingPads; bool InstrumentWithCalls; bool InstrumentStack; + bool DetectUseAfterScope; bool UsePageAliases; bool HasMatchAllTag = false; @@ -387,14 +413,22 @@ } bool runOnFunction(Function &F) override { - if (shouldUseStackSafetyAnalysis(Triple(F.getParent()->getTargetTriple()), + auto TargetTriple = Triple(F.getParent()->getTargetTriple()); + if (shouldUseStackSafetyAnalysis(TargetTriple, DisableOptimization)) { // We cannot call getAnalysis in doInitialization, that would cause a // crash as the required analyses are not initialized yet. HWASan->setSSI( &getAnalysis().getResult()); } - return HWASan->sanitizeFunction(F); + DominatorTree *DT = nullptr; + PostDominatorTree *PDT = nullptr; + if (auto *P = getAnalysisIfAvailable()) + DT = &P->getDomTree(); + + if (auto *P = getAnalysisIfAvailable()) + PDT = &P->getPostDomTree(); + return HWASan->sanitizeFunction(F, DT, PDT); } bool doFinalization(Module &M) override { @@ -443,13 +477,22 @@ PreservedAnalyses HWAddressSanitizerPass::run(Module &M, ModuleAnalysisManager &MAM) { const StackSafetyGlobalInfo *SSI = nullptr; - if (shouldUseStackSafetyAnalysis(llvm::Triple(M.getTargetTriple()), + auto TargetTriple = llvm::Triple(M.getTargetTriple()); + if (shouldUseStackSafetyAnalysis(TargetTriple, DisableOptimization)) SSI = &MAM.getResult(M); + HWAddressSanitizer HWASan(M, CompileKernel, Recover, SSI); bool Modified = false; - for (Function &F : M) - Modified |= HWASan.sanitizeFunction(F); + for (Function &F : M) { + auto &FAM = + MAM.getResult(M).getManager(); + // If the analysis is not cached, we only run it for the + // functions that have interesting allocas. + DominatorTree *DT = FAM.getCachedResult(F); + PostDominatorTree *PDT = FAM.getCachedResult(F); + Modified |= HWASan.sanitizeFunction(F, DT, PDT); + } if (Modified) return PreservedAnalyses::none(); return PreservedAnalyses::all(); @@ -571,6 +614,7 @@ UsePageAliases = shouldUsePageAliases(TargetTriple); InstrumentWithCalls = shouldInstrumentWithCalls(TargetTriple); InstrumentStack = shouldInstrumentStack(TargetTriple); + DetectUseAfterScope = shouldDetectUseAfterScope(TargetTriple); PointerTagShift = IsX86_64 ? 57 : 56; TagMaskByte = IsX86_64 ? 0x3F : 0xFF; @@ -973,7 +1017,7 @@ return SizeInBytes * ArraySize; } -bool HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, +void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, size_t Size) { size_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment()); if (!UseShortGranules) @@ -1004,7 +1048,6 @@ AlignedSize - 1)); } } - return true; } unsigned HWAddressSanitizer::retagMask(unsigned AllocaNo) { @@ -1237,20 +1280,25 @@ } bool HWAddressSanitizer::instrumentStack( - SmallVectorImpl &Allocas, + MapVector &AllocasToInstrument, + SmallVector &UnrecognizedLifetimes, DenseMap> &AllocaDbgMap, - SmallVectorImpl &RetVec, Value *StackTag) { + SmallVectorImpl &RetVec, Value *StackTag, DominatorTree *DT, + PostDominatorTree *PDT) { // Ideally, we want to calculate tagged stack base pointer, and rewrite all // alloca addresses using that. Unfortunately, offsets are not known yet // (unless we use ASan-style mega-alloca). Instead we keep the base tag in a // temp, shift-OR it into each alloca address and xor with the retag mask. // This generates one extra instruction per alloca use. - for (unsigned N = 0; N < Allocas.size(); ++N) { - auto *AI = Allocas[N]; + unsigned int N = 0; + + for (auto &KV : AllocasToInstrument) { + auto *AI = KV.first; + AllocaInfo &Info = KV.second; IRBuilder<> IRB(AI->getNextNode()); // Replace uses of the alloca with tagged address. - Value *Tag = getAllocaTag(IRB, StackTag, AI, N); + Value *Tag = getAllocaTag(IRB, StackTag, AI, ++N); Value *AILong = IRB.CreatePointerCast(AI, IntptrTy); Value *Replacement = tagPointer(IRB, AI->getType(), AILong, Tag); std::string Name = @@ -1273,17 +1321,40 @@ } size_t Size = getAllocaSizeInBytes(*AI); - tagAlloca(IRB, AI, Tag, Size); - - for (auto RI : RetVec) { - IRB.SetInsertPoint(RI); - - // Re-tag alloca memory with the special UAR tag. - Value *Tag = getUARTag(IRB, StackTag); - tagAlloca(IRB, AI, Tag, alignTo(Size, Mapping.getObjectAlignment())); + size_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment()); + bool StandardLifetime = UnrecognizedLifetimes.empty() && + Info.LifetimeStart.size() == 1 && + Info.LifetimeEnd.size() == 1; + if (DetectUseAfterScope && StandardLifetime) { + IntrinsicInst *Start = Info.LifetimeStart[0]; + IntrinsicInst *End = Info.LifetimeEnd[0]; + IRB.SetInsertPoint(Start->getNextNode()); + auto TagEnd = [&](Instruction *Node) { + IRB.SetInsertPoint(End); + Value *UARTag = getUARTag(IRB, StackTag); + tagAlloca(IRB, AI, UARTag, AlignedSize); + }; + tagAlloca(IRB, AI, Tag, Size); + if (!forAllReachableExits(DT, PDT, Start, End, RetVec, TagEnd)) { + End->eraseFromParent(); + }; + } else { + tagAlloca(IRB, AI, Tag, Size); + for (auto *RI : RetVec) { + IRB.SetInsertPoint(RI); + Value *UARTag = getUARTag(IRB, StackTag); + tagAlloca(IRB, AI, UARTag, AlignedSize); + } + if (!StandardLifetime) { + for (auto &II : Info.LifetimeStart) + II->eraseFromParent(); + for (auto &II : Info.LifetimeEnd) + II->eraseFromParent(); + } } } - + for (auto &I : UnrecognizedLifetimes) + I->eraseFromParent(); return true; } @@ -1305,7 +1376,8 @@ !(SSI && SSI->isSafe(AI)); } -bool HWAddressSanitizer::sanitizeFunction(Function &F) { +bool HWAddressSanitizer::sanitizeFunction(Function &F, DominatorTree *DT, + PostDominatorTree *PDT) { if (&F == HwasanCtorFunction) return false; @@ -1316,18 +1388,36 @@ SmallVector OperandsToInstrument; SmallVector IntrinToInstrument; - SmallVector AllocasToInstrument; + MapVector AllocasToInstrument; SmallVector RetVec; SmallVector LandingPadVec; + SmallVector UnrecognizedLifetimes; DenseMap> AllocaDbgMap; for (auto &BB : F) { for (auto &Inst : BB) { - if (InstrumentStack) + if (InstrumentStack) { if (AllocaInst *AI = dyn_cast(&Inst)) { if (isInterestingAlloca(*AI)) - AllocasToInstrument.push_back(AI); + AllocasToInstrument.insert({AI, {}}); + continue; + } + auto *II = dyn_cast(&Inst); + if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start || + II->getIntrinsicID() == Intrinsic::lifetime_end)) { + AllocaInst *AI = findAllocaForValue(II->getArgOperand(1)); + if (!AI) { + UnrecognizedLifetimes.push_back(&Inst); + continue; + } + if (!isInterestingAlloca(*AI)) + continue; + if (II->getIntrinsicID() == Intrinsic::lifetime_start) + AllocasToInstrument[AI].LifetimeStart.push_back(II); + else + AllocasToInstrument[AI].LifetimeEnd.push_back(II); continue; } + } if (isa(Inst) || isa(Inst) || isa(Inst)) @@ -1380,15 +1470,36 @@ Mapping.WithFrameRecord && !AllocasToInstrument.empty()); if (!AllocasToInstrument.empty()) { + std::unique_ptr DeleteDT; + std::unique_ptr DeletePDT; + + DominatorTree *LocalDT = nullptr; + PostDominatorTree *LocalPDT = nullptr; + if (DetectUseAfterScope) { + LocalDT = DT; + LocalPDT = PDT; + + if (LocalDT == nullptr) { + DeleteDT = std::make_unique(F); + LocalDT = DeleteDT.get(); + } + if (LocalPDT == nullptr) { + DeletePDT = std::make_unique(F); + LocalPDT = DeletePDT.get(); + } + } + Value *StackTag = ClGenerateTagsWithCalls ? nullptr : getStackBaseTag(EntryIRB); - instrumentStack(AllocasToInstrument, AllocaDbgMap, RetVec, StackTag); + instrumentStack(AllocasToInstrument, UnrecognizedLifetimes, AllocaDbgMap, + RetVec, StackTag, LocalDT, LocalPDT); } // Pad and align each of the allocas that we instrumented to stop small // uninteresting allocas from hiding in instrumented alloca's padding and so // that we have enough space to store real tags for short granules. DenseMap AllocaToPaddedAllocaMap; - for (AllocaInst *AI : AllocasToInstrument) { + for (auto &KV : AllocasToInstrument) { + AllocaInst *AI = KV.first; uint64_t Size = getAllocaSizeInBytes(*AI); uint64_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment()); AI->setAlignment(