diff --git a/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/lib/Transforms/Instrumentation/ThreadSanitizer.cpp index 2c8a7c4118e..89fb746a5c4 100644 --- a/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ b/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -358,7 +358,7 @@ bool ThreadSanitizer::runOnFunction(Function &F) { // (e.g. variables that do not escape, etc). // Instrument memory accesses. - if (ClInstrumentMemoryAccesses) + if (ClInstrumentMemoryAccesses && F.hasFnAttribute(Attribute::SanitizeThread)) for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) { Res |= instrumentLoadOrStore(AllLoadsAndStores[i]); } diff --git a/test/Instrumentation/ThreadSanitizer/no_sanitize_thread.ll b/test/Instrumentation/ThreadSanitizer/no_sanitize_thread.ll new file mode 100644 index 00000000000..3949fd50a97 --- /dev/null +++ b/test/Instrumentation/ThreadSanitizer/no_sanitize_thread.ll @@ -0,0 +1,36 @@ +; RUN: opt < %s -tsan -S | FileCheck %s + +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" +target triple = "x86_64-unknown-linux-gnu" + +; no sanitize_thread attribute here +define i32 @read_4_bytes(i32* %a) { +entry: + %tmp1 = load i32* %a, align 4 + ret i32 %tmp1 +} + +; CHECK: define i32 @read_4_bytes(i32* %a) { +; CHECK-NEXT: entry: +; CHECK-NEXT: %tmp1 = load i32* %a, align 4 +; CHECK: ret i32 %tmp1 + +; no sanitize_thread attribute here +define i32 @read_4_bytes_and_call(i32* %a) { +entry: + call void @foo() + %tmp1 = load i32* %a, align 4 + ret i32 %tmp1 +} + +; CHECK: define i32 @read_4_bytes_and_call(i32* %a) { +; CHECK-NEXT: entry: +; CHECK-NEXT: %0 = call i8* @llvm.returnaddress(i32 0) +; CHECK-NEXT: call void @__tsan_func_entry(i8* %0) +; CHECK-NEXT: call void @foo() +; CHECK-NEXT: %tmp1 = load i32* %a, align 4 +; CHECK-NEXT: call void @__tsan_func_exit() +; CHECK-NEXT: ret i32 %tmp1 + +declare void @foo() + diff --git a/test/Instrumentation/ThreadSanitizer/read_before_write.ll b/test/Instrumentation/ThreadSanitizer/read_before_write.ll index 482362aa7dc..cb6603bc143 100644 --- a/test/Instrumentation/ThreadSanitizer/read_before_write.ll +++ b/test/Instrumentation/ThreadSanitizer/read_before_write.ll @@ -2,7 +2,7 @@ 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" -define void @IncrementMe(i32* nocapture %ptr) nounwind uwtable { +define void @IncrementMe(i32* nocapture %ptr) nounwind uwtable sanitize_thread { entry: %0 = load i32* %ptr, align 4 %inc = add nsw i32 %0, 1 @@ -14,7 +14,7 @@ entry: ; CHECK: __tsan_write ; CHECK: ret void -define void @IncrementMeWithCallInBetween(i32* nocapture %ptr) nounwind uwtable { +define void @IncrementMeWithCallInBetween(i32* nocapture %ptr) nounwind uwtable sanitize_thread { entry: %0 = load i32* %ptr, align 4 %inc = add nsw i32 %0, 1 diff --git a/test/Instrumentation/ThreadSanitizer/read_from_global.ll b/test/Instrumentation/ThreadSanitizer/read_from_global.ll index 92030a70fff..33614a32f9c 100644 --- a/test/Instrumentation/ThreadSanitizer/read_from_global.ll +++ b/test/Instrumentation/ThreadSanitizer/read_from_global.ll @@ -4,7 +4,7 @@ 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" @const_global = external constant i32 -define i32 @read_from_const_global() nounwind uwtable readnone { +define i32 @read_from_const_global() nounwind uwtable sanitize_thread readnone { entry: %0 = load i32* @const_global, align 4 ret i32 %0 @@ -14,7 +14,7 @@ entry: ; CHECK: ret i32 @non_const_global = global i32 0, align 4 -define i32 @read_from_non_const_global() nounwind uwtable readonly { +define i32 @read_from_non_const_global() nounwind uwtable sanitize_thread readonly { entry: %0 = load i32* @non_const_global, align 4 ret i32 %0 @@ -25,7 +25,7 @@ entry: ; CHECK: ret i32 @const_global_array = external constant [10 x i32] -define i32 @read_from_const_global_array(i32 %idx) nounwind uwtable readnone { +define i32 @read_from_const_global_array(i32 %idx) nounwind uwtable sanitize_thread readnone { entry: %idxprom = sext i32 %idx to i64 %arrayidx = getelementptr inbounds [10 x i32]* @const_global_array, i64 0, i64 %idxprom @@ -38,7 +38,7 @@ entry: ; CHECK: ret i32 %struct.Foo = type { i32 (...)** } -define void @call_virtual_func(%struct.Foo* %f) uwtable { +define void @call_virtual_func(%struct.Foo* %f) uwtable sanitize_thread { entry: %0 = bitcast %struct.Foo* %f to void (%struct.Foo*)*** %vtable = load void (%struct.Foo*)*** %0, align 8, !tbaa !2 diff --git a/test/Instrumentation/ThreadSanitizer/tsan_basic.ll b/test/Instrumentation/ThreadSanitizer/tsan_basic.ll index 19dd45bda23..d449a97a62a 100644 --- a/test/Instrumentation/ThreadSanitizer/tsan_basic.ll +++ b/test/Instrumentation/ThreadSanitizer/tsan_basic.ll @@ -3,7 +3,7 @@ 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" target triple = "x86_64-unknown-linux-gnu" -define i32 @read_4_bytes(i32* %a) { +define i32 @read_4_bytes(i32* %a) sanitize_thread { entry: %tmp1 = load i32* %a, align 4 ret i32 %tmp1 @@ -11,7 +11,7 @@ entry: ; CHECK: @llvm.global_ctors = {{.*}}@__tsan_init -; CHECK: define i32 @read_4_bytes(i32* %a) { +; CHECK: define i32 @read_4_bytes(i32* %a) ; CHECK: call void @__tsan_func_entry(i8* %0) ; CHECK-NEXT: %1 = bitcast i32* %a to i8* ; CHECK-NEXT: call void @__tsan_read4(i8* %1) diff --git a/test/Instrumentation/ThreadSanitizer/vptr_read.ll b/test/Instrumentation/ThreadSanitizer/vptr_read.ll index 5265dd6d71c..811ad8d1cf5 100644 --- a/test/Instrumentation/ThreadSanitizer/vptr_read.ll +++ b/test/Instrumentation/ThreadSanitizer/vptr_read.ll @@ -2,7 +2,7 @@ ; Check that vptr reads are treated in a special way. 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" -define i8 @Foo(i8* %a) nounwind uwtable { +define i8 @Foo(i8* %a) nounwind uwtable sanitize_thread { entry: ; CHECK: call void @__tsan_vptr_read %0 = load i8* %a, align 8, !tbaa !0 diff --git a/test/Instrumentation/ThreadSanitizer/vptr_update.ll b/test/Instrumentation/ThreadSanitizer/vptr_update.ll index 0f455f9c416..95c7bb0e591 100644 --- a/test/Instrumentation/ThreadSanitizer/vptr_update.ll +++ b/test/Instrumentation/ThreadSanitizer/vptr_update.ll @@ -2,7 +2,7 @@ ; Check that vtable pointer updates are treated in a special way. 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" -define void @Foo(i8** nocapture %a, i8* %b) nounwind uwtable { +define void @Foo(i8** nocapture %a, i8* %b) nounwind uwtable sanitize_thread { entry: ; CHECK: call void @__tsan_vptr_update store i8* %b, i8** %a, align 8, !tbaa !0