[compiler-rt] r227230 - tsan: properly instrument unaligned accesses
Dmitry Vyukov
dvyukov at google.com
Tue Jan 27 12:19:12 PST 2015
Author: dvyukov
Date: Tue Jan 27 14:19:12 2015
New Revision: 227230
URL: http://llvm.org/viewvc/llvm-project?rev=227230&view=rev
Log:
tsan: properly instrument unaligned accesses
If a memory access is unaligned, emit __tsan_unaligned_read/write
callbacks instead of __tsan_read/write.
Required to change semantics of __tsan_unaligned_read/write to not do the user memory.
But since they were unused (other than through __sanitizer_unaligned_load/store) this is fine.
Fixes long standing issue 17:
https://code.google.com/p/thread-sanitizer/issues/detail?id=17
Modified:
compiler-rt/trunk/lib/tsan/rtl/tsan_interface.cc
compiler-rt/trunk/lib/tsan/rtl/tsan_interface.h
compiler-rt/trunk/test/tsan/aligned_vs_unaligned_race.cc
compiler-rt/trunk/test/tsan/unaligned_norace.cc
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interface.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interface.cc?rev=227230&r1=227229&r2=227230&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interface.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interface.cc Tue Jan 27 14:19:12 2015
@@ -38,56 +38,79 @@ void __tsan_write16(void *addr) {
MemoryWrite(cur_thread(), CALLERPC, (uptr)addr + 8, kSizeLog8);
}
-u16 __tsan_unaligned_read2(const uu16 *addr) {
+// __tsan_unaligned_read/write calls are emitted by compiler.
+
+void __tsan_unaligned_read2(const void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, false, false);
- return *addr;
}
-u32 __tsan_unaligned_read4(const uu32 *addr) {
+void __tsan_unaligned_read4(const void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, false, false);
- return *addr;
}
-u64 __tsan_unaligned_read8(const uu64 *addr) {
+void __tsan_unaligned_read8(const void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, false, false);
- return *addr;
}
-void __tsan_unaligned_write2(uu16 *addr, u16 v) {
+void __tsan_unaligned_read16(const void *addr) {
+ UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, false, false);
+}
+
+void __tsan_unaligned_write2(void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 2, true, false);
- *addr = v;
}
-void __tsan_unaligned_write4(uu32 *addr, u32 v) {
+void __tsan_unaligned_write4(void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 4, true, false);
- *addr = v;
}
-void __tsan_unaligned_write8(uu64 *addr, u64 v) {
+void __tsan_unaligned_write8(void *addr) {
UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 8, true, false);
- *addr = v;
}
+void __tsan_unaligned_write16(void *addr) {
+ UnalignedMemoryAccess(cur_thread(), CALLERPC, (uptr)addr, 16, true, false);
+}
+
+// __sanitizer_unaligned_load/store are for user instrumentation.
+
extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE
-uint16_t __sanitizer_unaligned_load16(void *addr)
- ALIAS("__tsan_unaligned_read2");
+u16 __sanitizer_unaligned_load16(const uu16 *addr) {
+ __tsan_unaligned_read2(addr);
+ return *addr;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
-uint32_t __sanitizer_unaligned_load32(void *addr)
- ALIAS("__tsan_unaligned_read4");
+u32 __sanitizer_unaligned_load32(const uu32 *addr) {
+ __tsan_unaligned_read4(addr);
+ return *addr;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
-uint64_t __sanitizer_unaligned_load64(void *addr)
- ALIAS("__tsan_unaligned_read8");
+u64 __sanitizer_unaligned_load64(const uu64 *addr) {
+ __tsan_unaligned_read8(addr);
+ return *addr;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
-void __sanitizer_unaligned_store16(void *addr, uint16_t v)
- ALIAS("__tsan_unaligned_write2");
+void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
+ __tsan_unaligned_write2(addr);
+ *addr = v;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
-void __sanitizer_unaligned_store32(void *addr, uint32_t v)
- ALIAS("__tsan_unaligned_write4");
+void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
+ __tsan_unaligned_write4(addr);
+ *addr = v;
+}
+
SANITIZER_INTERFACE_ATTRIBUTE
-void __sanitizer_unaligned_store64(void *addr, uint64_t v)
- ALIAS("__tsan_unaligned_write8");
+void __sanitizer_unaligned_store64(uu64 *addr, u64 v) {
+ __tsan_unaligned_write8(addr);
+ *addr = v;
}
+} // extern "C"
void __tsan_acquire(void *addr) {
Acquire(cur_thread(), CALLERPC, (uptr)addr);
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interface.h?rev=227230&r1=227229&r2=227230&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interface.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interface.h Tue Jan 27 14:19:12 2015
@@ -41,12 +41,15 @@ SANITIZER_INTERFACE_ATTRIBUTE void __tsa
SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write8(void *addr);
SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write16(void *addr);
-SANITIZER_INTERFACE_ATTRIBUTE u16 __tsan_unaligned_read2(const uu16 *addr);
-SANITIZER_INTERFACE_ATTRIBUTE u32 __tsan_unaligned_read4(const uu32 *addr);
-SANITIZER_INTERFACE_ATTRIBUTE u64 __tsan_unaligned_read8(const uu64 *addr);
-SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write2(uu16 *addr, u16 v);
-SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write4(uu32 *addr, u32 v);
-SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write8(uu64 *addr, u64 v);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read2(const void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read4(const void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read8(const void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_read16(const void *addr);
+
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write2(void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write4(void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write8(void *addr);
+SANITIZER_INTERFACE_ATTRIBUTE void __tsan_unaligned_write16(void *addr);
SANITIZER_INTERFACE_ATTRIBUTE void __tsan_vptr_read(void **vptr_p);
SANITIZER_INTERFACE_ATTRIBUTE
Modified: compiler-rt/trunk/test/tsan/aligned_vs_unaligned_race.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/aligned_vs_unaligned_race.cc?rev=227230&r1=227229&r2=227230&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/aligned_vs_unaligned_race.cc (original)
+++ compiler-rt/trunk/test/tsan/aligned_vs_unaligned_race.cc Tue Jan 27 14:19:12 2015
@@ -1,34 +1,35 @@
-// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
// Race between an aligned access and an unaligned access, which
// touches the same memory region.
-// This is a real race which is not detected by tsan.
-// https://code.google.com/p/thread-sanitizer/issues/detail?id=17
-#include <pthread.h>
-#include <stdio.h>
+#include "test.h"
#include <stdint.h>
uint64_t Global[2];
void *Thread1(void *x) {
Global[1]++;
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
+ barrier_wait(&barrier);
char *p1 = reinterpret_cast<char *>(&Global[0]);
- uint64_t *p4 = reinterpret_cast<uint64_t *>(p1 + 1);
- (*p4)++;
+ struct __attribute__((packed, aligned(1))) u_uint64_t { uint64_t val; };
+ u_uint64_t *p4 = reinterpret_cast<u_uint64_t *>(p1 + 1);
+ (*p4).val++;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
printf("Pass\n");
- // CHECK-NOT: ThreadSanitizer: data race
+ // CHECK: ThreadSanitizer: data race
// CHECK: Pass
return 0;
}
Modified: compiler-rt/trunk/test/tsan/unaligned_norace.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/unaligned_norace.cc?rev=227230&r1=227229&r2=227230&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/unaligned_norace.cc (original)
+++ compiler-rt/trunk/test/tsan/unaligned_norace.cc Tue Jan 27 14:19:12 2015
@@ -7,20 +7,20 @@
uint64_t objs[8*3*3*2][3];
extern "C" {
-uint16_t __tsan_unaligned_read2(void *addr);
-uint32_t __tsan_unaligned_read4(void *addr);
-uint64_t __tsan_unaligned_read8(void *addr);
-void __tsan_unaligned_write2(void *addr, uint16_t v);
-void __tsan_unaligned_write4(void *addr, uint32_t v);
-void __tsan_unaligned_write8(void *addr, uint64_t v);
+void __tsan_unaligned_read2(void *addr);
+void __tsan_unaligned_read4(void *addr);
+void __tsan_unaligned_read8(void *addr);
+void __tsan_unaligned_write2(void *addr);
+void __tsan_unaligned_write4(void *addr);
+void __tsan_unaligned_write8(void *addr);
}
static void access(char *p, int sz, int rw) {
if (rw) {
switch (sz) {
- case 0: __tsan_unaligned_write2(p, 0); break;
- case 1: __tsan_unaligned_write4(p, 0); break;
- case 2: __tsan_unaligned_write8(p, 0); break;
+ case 0: __tsan_unaligned_write2(p); break;
+ case 1: __tsan_unaligned_write4(p); break;
+ case 2: __tsan_unaligned_write8(p); break;
default: exit(1);
}
} else {
More information about the llvm-commits
mailing list