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

llvm-svn: 227230
This commit is contained in:
Dmitry Vyukov 2015-01-27 20:19:12 +00:00
parent ca49c036ba
commit 312ad250ff
4 changed files with 79 additions and 52 deletions

View File

@ -38,57 +38,80 @@ 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");
SANITIZER_INTERFACE_ATTRIBUTE
uint32_t __sanitizer_unaligned_load32(void *addr)
ALIAS("__tsan_unaligned_read4");
SANITIZER_INTERFACE_ATTRIBUTE
uint64_t __sanitizer_unaligned_load64(void *addr)
ALIAS("__tsan_unaligned_read8");
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_unaligned_store16(void *addr, uint16_t v)
ALIAS("__tsan_unaligned_write2");
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_unaligned_store32(void *addr, uint32_t v)
ALIAS("__tsan_unaligned_write4");
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_unaligned_store64(void *addr, uint64_t v)
ALIAS("__tsan_unaligned_write8");
u16 __sanitizer_unaligned_load16(const uu16 *addr) {
__tsan_unaligned_read2(addr);
return *addr;
}
SANITIZER_INTERFACE_ATTRIBUTE
u32 __sanitizer_unaligned_load32(const uu32 *addr) {
__tsan_unaligned_read4(addr);
return *addr;
}
SANITIZER_INTERFACE_ATTRIBUTE
u64 __sanitizer_unaligned_load64(const uu64 *addr) {
__tsan_unaligned_read8(addr);
return *addr;
}
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_unaligned_store16(uu16 *addr, u16 v) {
__tsan_unaligned_write2(addr);
*addr = v;
}
SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_unaligned_store32(uu32 *addr, u32 v) {
__tsan_unaligned_write4(addr);
*addr = v;
}
SANITIZER_INTERFACE_ATTRIBUTE
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);
}

View File

@ -41,12 +41,15 @@ SANITIZER_INTERFACE_ATTRIBUTE void __tsan_write4(void *addr);
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

View File

@ -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;
}

View File

@ -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 {