forked from OSchip/llvm-project
tsan: remove internal allocator, switch to sanitizer_common one.
llvm-svn: 159141
This commit is contained in:
parent
aad697eb8a
commit
ec64f3e738
|
@ -1,47 +0,0 @@
|
|||
//===-- tsan_allocator-----------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#include "tsan_allocator.h"
|
||||
|
||||
// Provisional implementation.
|
||||
extern "C" void *__libc_malloc(__sanitizer::uptr size);
|
||||
extern "C" void __libc_free(void *ptr);
|
||||
|
||||
namespace __tsan {
|
||||
|
||||
u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
||||
|
||||
void AllocInit() {
|
||||
}
|
||||
|
||||
void *Alloc(uptr sz) {
|
||||
void *p = __libc_malloc(sz + sizeof(u64));
|
||||
((u64*)p)[0] = kBlockMagic;
|
||||
return (char*)p + sizeof(u64);
|
||||
}
|
||||
|
||||
void Free(void *p) {
|
||||
CHECK_NE(p, (char*)0);
|
||||
p = (char*)p - sizeof(u64);
|
||||
CHECK_EQ(((u64*)p)[0], kBlockMagic);
|
||||
((u64*)p)[0] = 0;
|
||||
__libc_free(p);
|
||||
}
|
||||
|
||||
void *AllocBlock(void *p) {
|
||||
CHECK_NE(p, (void*)0);
|
||||
u64 *pp = (u64*)((uptr)p & ~0x7);
|
||||
for (; pp[0] != kBlockMagic; pp--) {}
|
||||
return pp + 1;
|
||||
}
|
||||
|
||||
} // namespace __tsan
|
|
@ -1,29 +0,0 @@
|
|||
//===-- tsan_allocator.h ----------------------------------------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#ifndef TSAN_ALLOCATOR_H
|
||||
#define TSAN_ALLOCATOR_H
|
||||
|
||||
#include "tsan_defs.h"
|
||||
|
||||
namespace __tsan {
|
||||
|
||||
void AllocInit();
|
||||
void *Alloc(uptr sz);
|
||||
void Free(void *p); // Does not accept NULL.
|
||||
// Given the pointer p into a valid allocated block,
|
||||
// returns a pointer to the beginning of the block.
|
||||
void *AllocBlock(void *p);
|
||||
|
||||
} // namespace __tsan
|
||||
|
||||
#endif // TSAN_ALLOCATOR_H
|
|
@ -12,7 +12,6 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
#include "sanitizer_common/sanitizer_common.h"
|
||||
#include "tsan_mman.h"
|
||||
#include "tsan_allocator.h"
|
||||
#include "tsan_rtl.h"
|
||||
#include "tsan_report.h"
|
||||
#include "tsan_flags.h"
|
||||
|
@ -33,7 +32,7 @@ void *user_alloc(ThreadState *thr, uptr pc, uptr sz) {
|
|||
CHECK_GT(thr->in_rtl, 0);
|
||||
if (sz + sizeof(MBlock) < sz)
|
||||
return 0;
|
||||
MBlock *b = (MBlock*)Alloc(sz + sizeof(MBlock));
|
||||
MBlock *b = (MBlock*)InternalAlloc(sz + sizeof(MBlock));
|
||||
if (b == 0)
|
||||
return 0;
|
||||
b->size = sz;
|
||||
|
@ -55,7 +54,7 @@ void user_free(ThreadState *thr, uptr pc, void *p) {
|
|||
if (CTX() && CTX()->initialized && thr->in_rtl == 1) {
|
||||
MemoryRangeFreed(thr, pc, (uptr)p, b->size);
|
||||
}
|
||||
Free(b);
|
||||
InternalFree(b);
|
||||
SignalUnsafeCall(thr, pc);
|
||||
}
|
||||
|
||||
|
@ -90,7 +89,7 @@ void *user_alloc_aligned(ThreadState *thr, uptr pc, uptr sz, uptr align) {
|
|||
MBlock *user_mblock(ThreadState *thr, void *p) {
|
||||
CHECK_GT(thr->in_rtl, 0);
|
||||
CHECK_NE(p, (void*)0);
|
||||
MBlock *b = (MBlock*)AllocBlock(p);
|
||||
MBlock *b = (MBlock*)InternalAllocBlock(p);
|
||||
// FIXME: Output a warning, it's a user error.
|
||||
if (p < (char*)(b + 1) || p > (char*)(b + 1) + b->size) {
|
||||
TsanPrintf("user_mblock p=%p b=%p size=%zu beg=%p end=%p\n",
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
//===-- tsan_allocator_test.c----------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#include "tsan_allocator.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace __tsan {
|
||||
|
||||
TEST(Allocator, Basic) {
|
||||
char *p = (char*)Alloc(10);
|
||||
EXPECT_NE(p, (char*)0);
|
||||
char *p2 = (char*)Alloc(20);
|
||||
EXPECT_NE(p2, (char*)0);
|
||||
EXPECT_NE(p2, p);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
p[i] = 42;
|
||||
EXPECT_EQ(p, AllocBlock(p + i));
|
||||
}
|
||||
for (int i = 0; i < 20; i++) {
|
||||
((char*)p2)[i] = 42;
|
||||
EXPECT_EQ(p2, AllocBlock(p2 + i));
|
||||
}
|
||||
Free(p);
|
||||
Free(p2);
|
||||
}
|
||||
|
||||
TEST(Allocator, Stress) {
|
||||
const int kCount = 1000;
|
||||
char *ptrs[kCount];
|
||||
unsigned rnd = 42;
|
||||
for (int i = 0; i < kCount; i++) {
|
||||
uptr sz = rand_r(&rnd) % 1000;
|
||||
char *p = (char*)Alloc(sz);
|
||||
EXPECT_NE(p, (char*)0);
|
||||
for (uptr j = 0; j < sz; j++) {
|
||||
p[j] = 42;
|
||||
EXPECT_EQ(p, AllocBlock(p + j));
|
||||
}
|
||||
ptrs[i] = p;
|
||||
}
|
||||
for (int i = 0; i < kCount; i++) {
|
||||
Free(ptrs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace __tsan
|
Loading…
Reference in New Issue