From b523d2b232ab8b7011ae803990e93d54a12817f6 Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Wed, 22 Jun 2016 00:15:52 +0000 Subject: [PATCH] [asan] Do not instrument pointers with address space attributes Do not instrument pointers with address space attributes since we cannot track them anyway. Instrumenting them results in false positives in ASan and a compiler crash in TSan. (The compiler should not crash in any case, but that's a different problem.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273339 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/AddressSanitizer.cpp | 8 +++++ .../Instrumentation/ThreadSanitizer.cpp | 9 +++++ .../asan_address_space_attr.ll | 32 ++++++++++++++++++ .../tsan_address_space_attr.ll | 33 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 test/Instrumentation/AddressSanitizer/asan_address_space_attr.ll create mode 100644 test/Instrumentation/ThreadSanitizer/tsan_address_space_attr.ll diff --git a/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 79a36b313b4..396e23b88ce 100644 --- a/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -952,6 +952,14 @@ Value *AddressSanitizer::isInterestingMemoryAccess(Instruction *I, PtrOperand = XCHG->getPointerOperand(); } + // Do not instrument acesses from different address spaces; we cannot deal + // with them. + if (PtrOperand) { + Type *PtrTy = cast(PtrOperand->getType()->getScalarType()); + if (PtrTy->getPointerAddressSpace() != 0) + return nullptr; + } + // Treat memory accesses to promotable allocas as non-interesting since they // will not cause memory violations. This greatly speeds up the instrumented // executable at -O0. diff --git a/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/lib/Transforms/Instrumentation/ThreadSanitizer.cpp index 22a26d49374..287d5bfa260 100644 --- a/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ b/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -276,6 +276,15 @@ static bool shouldInstrumentReadWriteFromAddress(Value *Addr) { if (GV->getName() == "__llvm_gcov_ctr") return false; } + + // Do not instrument acesses from different address spaces; we cannot deal + // with them. + if (Addr) { + Type *PtrTy = cast(Addr->getType()->getScalarType()); + if (PtrTy->getPointerAddressSpace() != 0) + return false; + } + return true; } diff --git a/test/Instrumentation/AddressSanitizer/asan_address_space_attr.ll b/test/Instrumentation/AddressSanitizer/asan_address_space_attr.ll new file mode 100644 index 00000000000..87d72bbe142 --- /dev/null +++ b/test/Instrumentation/AddressSanitizer/asan_address_space_attr.ll @@ -0,0 +1,32 @@ +; RUN: opt < %s -asan -S | FileCheck %s +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + +; Checks that we do not instrument loads and stores comming from custom address space. +; These result in invalid (false positive) reports. +; int foo(int argc, const char * argv[]) { +; void *__attribute__((address_space(256))) *gs_base = (((void * __attribute__((address_space(256))) *)0)); +; void *somevalue = gs_base[-1]; +; return somevalue; +; } + +define i32 @foo(i32 %argc, i8** %argv) sanitize_address { +entry: + %retval = alloca i32, align 4 + %argc.addr = alloca i32, align 4 + %argv.addr = alloca i8**, align 8 + %gs_base = alloca i8* addrspace(256)*, align 8 + %somevalue = alloca i8*, align 8 + store i32 0, i32* %retval, align 4 + store i32 %argc, i32* %argc.addr, align 4 + store i8** %argv, i8*** %argv.addr, align 8 + store i8* addrspace(256)* null, i8* addrspace(256)** %gs_base, align 8 + %0 = load i8* addrspace(256)*, i8* addrspace(256)** %gs_base, align 8 + %arrayidx = getelementptr inbounds i8*, i8* addrspace(256)* %0, i64 -1 + %1 = load i8*, i8* addrspace(256)* %arrayidx, align 8 + store i8* %1, i8** %somevalue, align 8 + %2 = load i8*, i8** %somevalue, align 8 + %3 = ptrtoint i8* %2 to i32 + ret i32 %3 +} +; CHECK-NOT: call void @__asan_report_load8 diff --git a/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr.ll b/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr.ll new file mode 100644 index 00000000000..1495a989bc5 --- /dev/null +++ b/test/Instrumentation/ThreadSanitizer/tsan_address_space_attr.ll @@ -0,0 +1,33 @@ +; RUN: opt < %s -tsan -S | FileCheck %s +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + +; Checks that we do not instrument loads and stores comming from custom address space. +; These result in crashing the compiler. +; int foo(int argc, const char * argv[]) { +; void *__attribute__((address_space(256))) *gs_base = (((void * __attribute__((address_space(256))) *)0)); +; void *somevalue = gs_base[-1]; +; return somevalue; +; } + +define i32 @foo(i32 %argc, i8** %argv) sanitize_thread { +entry: + %retval = alloca i32, align 4 + %argc.addr = alloca i32, align 4 + %argv.addr = alloca i8**, align 8 + %gs_base = alloca i8* addrspace(256)*, align 8 + %somevalue = alloca i8*, align 8 + store i32 0, i32* %retval, align 4 + store i32 %argc, i32* %argc.addr, align 4 + store i8** %argv, i8*** %argv.addr, align 8 + store i8* addrspace(256)* null, i8* addrspace(256)** %gs_base, align 8 + %0 = load i8* addrspace(256)*, i8* addrspace(256)** %gs_base, align 8 + %arrayidx = getelementptr inbounds i8*, i8* addrspace(256)* %0, i64 -1 + %1 = load i8*, i8* addrspace(256)* %arrayidx, align 8 + store i8* %1, i8** %somevalue, align 8 + %2 = load i8*, i8** %somevalue, align 8 + %3 = ptrtoint i8* %2 to i32 + ret i32 %3 +} +; CHECK-NOT: call void @__tsan_read +; CHECK-NOT: addrspacecast -- 2.50.1