[TSan][Darwin] Handle NULL argument in interceptor

Handle NULL address argument in the `mach_vm_[de]allocate()`
interceptors and fix test: `Assignment 2` is not valid if we weren't
able to re-allocate memory.

rdar://67680613
This commit is contained in:
Julian Lettner 2020-08-24 11:30:58 -07:00
parent b9496efbb9
commit 2b7a2cbb15
2 changed files with 26 additions and 21 deletions

View File

@ -19,12 +19,11 @@
namespace __tsan { namespace __tsan {
static bool intersects_with_shadow(mach_vm_address_t *address, static bool intersects_with_shadow(mach_vm_address_t address,
mach_vm_size_t size, int flags) { mach_vm_size_t size, int flags) {
// VM_FLAGS_FIXED is 0x0, so we have to test for VM_FLAGS_ANYWHERE. // VM_FLAGS_FIXED is 0x0, so we have to test for VM_FLAGS_ANYWHERE.
if (flags & VM_FLAGS_ANYWHERE) return false; if (flags & VM_FLAGS_ANYWHERE) return false;
uptr ptr = *address; return !IsAppMem(address) || !IsAppMem(address + size - 1);
return !IsAppMem(ptr) || !IsAppMem(ptr + size - 1);
} }
TSAN_INTERCEPTOR(kern_return_t, mach_vm_allocate, vm_map_t target, TSAN_INTERCEPTOR(kern_return_t, mach_vm_allocate, vm_map_t target,
@ -32,12 +31,12 @@ TSAN_INTERCEPTOR(kern_return_t, mach_vm_allocate, vm_map_t target,
SCOPED_TSAN_INTERCEPTOR(mach_vm_allocate, target, address, size, flags); SCOPED_TSAN_INTERCEPTOR(mach_vm_allocate, target, address, size, flags);
if (target != mach_task_self()) if (target != mach_task_self())
return REAL(mach_vm_allocate)(target, address, size, flags); return REAL(mach_vm_allocate)(target, address, size, flags);
if (intersects_with_shadow(address, size, flags)) if (address && intersects_with_shadow(*address, size, flags))
return KERN_NO_SPACE; return KERN_NO_SPACE;
kern_return_t res = REAL(mach_vm_allocate)(target, address, size, flags); kern_return_t kr = REAL(mach_vm_allocate)(target, address, size, flags);
if (res == KERN_SUCCESS) if (kr == KERN_SUCCESS)
MemoryRangeImitateWriteOrResetRange(thr, pc, *address, size); MemoryRangeImitateWriteOrResetRange(thr, pc, *address, size);
return res; return kr;
} }
TSAN_INTERCEPTOR(kern_return_t, mach_vm_deallocate, vm_map_t target, TSAN_INTERCEPTOR(kern_return_t, mach_vm_deallocate, vm_map_t target,
@ -45,8 +44,10 @@ TSAN_INTERCEPTOR(kern_return_t, mach_vm_deallocate, vm_map_t target,
SCOPED_TSAN_INTERCEPTOR(mach_vm_deallocate, target, address, size); SCOPED_TSAN_INTERCEPTOR(mach_vm_deallocate, target, address, size);
if (target != mach_task_self()) if (target != mach_task_self())
return REAL(mach_vm_deallocate)(target, address, size); return REAL(mach_vm_deallocate)(target, address, size);
UnmapShadow(thr, address, size); kern_return_t kr = REAL(mach_vm_deallocate)(target, address, size);
return REAL(mach_vm_deallocate)(target, address, size); if (kr == KERN_SUCCESS && address)
UnmapShadow(thr, address, size);
return kr;
} }
} // namespace __tsan } // namespace __tsan

View File

@ -13,28 +13,28 @@
const mach_vm_size_t alloc_size = sizeof(int); const mach_vm_size_t alloc_size = sizeof(int);
static int *global_ptr; static int *global_ptr;
static bool realloc_success = false;
static int *alloc() { static int *alloc() {
mach_vm_address_t addr; mach_vm_address_t addr;
kern_return_t res = kern_return_t kr =
mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_ANYWHERE); mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_ANYWHERE);
assert(res == KERN_SUCCESS); assert(kr == KERN_SUCCESS);
return (int *)addr; return (int *)addr;
} }
static void alloc_fixed(int *ptr) { static void alloc_fixed(int *ptr) {
mach_vm_address_t addr = (mach_vm_address_t)ptr; mach_vm_address_t addr = (mach_vm_address_t)ptr;
// Re-allocation via VM_FLAGS_FIXED sporadically fails. // Re-allocation via VM_FLAGS_FIXED sporadically fails.
kern_return_t res = kern_return_t kr =
mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_FIXED); mach_vm_allocate(mach_task_self(), &addr, alloc_size, VM_FLAGS_FIXED);
realloc_success = res == KERN_SUCCESS; if (kr != KERN_SUCCESS)
global_ptr = NULL;
} }
static void dealloc(int *ptr) { static void dealloc(int *ptr) {
kern_return_t res = kern_return_t kr =
mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)ptr, alloc_size); mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)ptr, alloc_size);
assert(res == KERN_SUCCESS); assert(kr == KERN_SUCCESS);
} }
static void *Thread(void *arg) { static void *Thread(void *arg) {
@ -53,26 +53,30 @@ static void *Thread(void *arg) {
return NULL; return NULL;
} }
static void try_realloc_on_same_address() { static bool try_realloc_on_same_address() {
barrier_init(&barrier, 2); barrier_init(&barrier, 2);
global_ptr = alloc(); global_ptr = alloc();
pthread_t t; pthread_t t;
pthread_create(&t, NULL, Thread, NULL); pthread_create(&t, NULL, Thread, NULL);
barrier_wait(&barrier); barrier_wait(&barrier);
*global_ptr = 8; // Assignment 2 if (global_ptr)
*global_ptr = 8; // Assignment 2
pthread_join(t, NULL); pthread_join(t, NULL);
dealloc(global_ptr); dealloc(global_ptr);
return global_ptr != NULL;
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
bool success;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
try_realloc_on_same_address(); success = try_realloc_on_same_address();
if (realloc_success) break; if (success) break;
} }
if (!realloc_success) if (!success)
fprintf(stderr, "Unable to set up testing condition; silently pass test\n"); fprintf(stderr, "Unable to set up testing condition; silently pass test\n");
printf("Done.\n"); printf("Done.\n");