tsan: return 0 on malloc() failure instead of crashing

llvm-svn: 177741
This commit is contained in:
Dmitry Vyukov 2013-03-22 17:06:22 +00:00
parent dd9276e464
commit 9af68719ed
3 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,22 @@
// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
#include <stdio.h>
#include <stdlib.h>
int main() {
void *p = malloc((size_t)-1);
if (p != 0)
printf("FAIL malloc(-1) = %p\n", p);
p = malloc((size_t)-1 / 2);
if (p != 0)
printf("FAIL malloc(-1/2) = %p\n", p);
p = calloc((size_t)-1, (size_t)-1);
if (p != 0)
printf("FAIL calloc(-1, -1) = %p\n", p);
p = calloc((size_t)-1 / 2, (size_t)-1 / 2);
if (p != 0)
printf("FAIL calloc(-1/2, -1/2) = %p\n", p);
printf("OK\n");
}
// CHECK-NOT: FAIL
// CHECK-NOT: failed to allocate

View File

@ -356,7 +356,8 @@ TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
{
SCOPED_INTERCEPTOR_RAW(calloc, size, n);
p = user_alloc(thr, pc, n * size);
if (p) internal_memset(p, 0, n * size);
if (p)
internal_memset(p, 0, n * size);
}
invoke_malloc_hook(p, n * size);
return p;

View File

@ -101,6 +101,8 @@ static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align) {
CHECK_GT(thr->in_rtl, 0);
if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
return 0;
void *p = allocator()->Allocate(&thr->alloc_cache, sz, align);
if (p == 0)
return 0;