[compiler-rt] 77654a6 - [tsan] Replace mem intrinsics with calls to interceptors
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 6 08:25:46 PDT 2022
Author: Vitaly Buka
Date: 2022-09-06T08:25:32-07:00
New Revision: 77654a65a373da9c4829de821e7b393ea811ee40
URL: https://github.com/llvm/llvm-project/commit/77654a65a373da9c4829de821e7b393ea811ee40
DIFF: https://github.com/llvm/llvm-project/commit/77654a65a373da9c4829de821e7b393ea811ee40.diff
LOG: [tsan] Replace mem intrinsics with calls to interceptors
After https://reviews.llvm.org/rG463aa814182a23 tsan replaces llvm
intrinsics with calls to glibc functions. However this approach is
fragile, as slight changes in pipeline can return llvm intrinsics back.
In particular InstCombine can do that.
Msan/Asan already declare own version of these memory
functions for the similar purpose.
KCSAN, or anything that uses something else than compiler-rt, needs to
implement this callbacks.
Reviewed By: melver
Differential Revision: https://reviews.llvm.org/D133268
Added:
Modified:
compiler-rt/lib/tsan/rtl/tsan.syms.extra
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
compiler-rt/lib/tsan/rtl/tsan_interface.h
llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll
Removed:
################################################################################
diff --git a/compiler-rt/lib/tsan/rtl/tsan.syms.extra b/compiler-rt/lib/tsan/rtl/tsan.syms.extra
index 4838bb0a72792..a5bd17176b12b 100644
--- a/compiler-rt/lib/tsan/rtl/tsan.syms.extra
+++ b/compiler-rt/lib/tsan/rtl/tsan.syms.extra
@@ -9,6 +9,9 @@ __tsan_java*
__tsan_unaligned*
__tsan_release
__tsan_acquire
+__tsan_memcpy
+__tsan_memmove
+__tsan_memset
__tsan_mutex_create
__tsan_mutex_destroy
__tsan_mutex_pre_lock
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 17f6b1f472d8f..1f90679d5b2f6 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -3059,3 +3059,17 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __tsan_testonly_barrier_wait(
FutexWait(barrier, cur);
}
}
+
+extern "C" {
+__attribute__((alias(SANITIZER_STRINGIFY(WRAP(memcpy))),
+ visibility("default"))) void *
+__tsan_memcpy(void *dst, const void *src, uptr size);
+
+__attribute__((alias(SANITIZER_STRINGIFY(WRAP(memset))),
+ visibility("default"))) void *
+__tsan_memset(void *dst, int c, uptr size);
+
+__attribute__((alias(SANITIZER_STRINGIFY(WRAP(memmove))),
+ visibility("default"))) void *
+__tsan_memmove(void *dst, const void *src, uptr size);
+}
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interface.h b/compiler-rt/lib/tsan/rtl/tsan_interface.h
index 711f064174c2c..5b9d664e503fc 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interface.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_interface.h
@@ -72,6 +72,13 @@ SANITIZER_INTERFACE_ATTRIBUTE void __tsan_vptr_read(void **vptr_p);
SANITIZER_INTERFACE_ATTRIBUTE
void __tsan_vptr_update(void **vptr_p, void *new_val);
+SANITIZER_INTERFACE_ATTRIBUTE
+void *__tsan_memcpy(void *dest, const void *src, uptr count);
+SANITIZER_INTERFACE_ATTRIBUTE
+void *__tsan_memset(void *dest, int ch, uptr count);
+SANITIZER_INTERFACE_ATTRIBUTE
+void *__tsan_memmove(void *dest, const void *src, uptr count);
+
SANITIZER_INTERFACE_ATTRIBUTE void __tsan_func_entry(void *call_pc);
SANITIZER_INTERFACE_ATTRIBUTE void __tsan_func_exit();
diff --git a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
index 863b085fc15b8..0cda1964e42f8 100644
--- a/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
@@ -341,13 +341,13 @@ void ThreadSanitizer::initialize(Module &M) {
}
MemmoveFn =
- M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
+ M.getOrInsertFunction("__tsan_memmove", Attr, IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
MemcpyFn =
- M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
+ M.getOrInsertFunction("__tsan_memcpy", Attr, IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
MemsetFn =
- M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
+ M.getOrInsertFunction("__tsan_memset", Attr, IRB.getInt8PtrTy(),
IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
}
diff --git a/llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll b/llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll
index 9a1ef9e3215aa..d59efd976a7ad 100644
--- a/llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll
+++ b/llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll
@@ -35,7 +35,7 @@ entry:
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %x, i8* align 4 %y, i64 16, i1 false)
ret void
; CHECK: define void @MemCpyTest
-; CHECK: call i8* @memcpy
+; CHECK: call i8* @__tsan_memcpy
; CHECK: ret void
}
@@ -44,7 +44,7 @@ entry:
tail call void @llvm.memcpy.inline.p0i8.p0i8.i64(i8* align 4 %x, i8* align 4 %y, i64 16, i1 false)
ret void
; CHECK: define void @MemCpyInlineTest
-; CHECK: call i8* @memcpy
+; CHECK: call i8* @__tsan_memcpy
; CHECK: ret void
}
@@ -53,7 +53,7 @@ entry:
tail call void @llvm.memmove.p0i8.p0i8.i64(i8* align 4 %x, i8* align 4 %y, i64 16, i1 false)
ret void
; CHECK: define void @MemMoveTest
-; CHECK: call i8* @memmove
+; CHECK: call i8* @__tsan_memmove
; CHECK: ret void
}
@@ -62,7 +62,7 @@ entry:
tail call void @llvm.memset.p0i8.i64(i8* align 4 %x, i8 77, i64 16, i1 false)
ret void
; CHECK: define void @MemSetTest
-; CHECK: call i8* @memset
+; CHECK: call i8* @__tsan_memset
; CHECK: ret void
}
@@ -71,7 +71,7 @@ entry:
tail call void @llvm.memset.inline.p0i8.i64(i8* align 4 %x, i8 77, i64 16, i1 false)
ret void
; CHECK: define void @MemSetInlineTest
-; CHECK: call i8* @memset
+; CHECK: call i8* @__tsan_memset
; CHECK: ret void
}
More information about the llvm-commits
mailing list