2012-06-07 16:52:56 +08:00
|
|
|
//===-- sanitizer_allocator.cc --------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
// This allocator that is used inside run-times.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
|
|
|
|
// FIXME: We should probably use more low-level allocator that would
|
|
|
|
// mmap some pages and split them into chunks to fulfill requests.
|
2012-09-10 18:18:49 +08:00
|
|
|
#if defined(__linux__) && !defined(__ANDROID__)
|
2012-06-09 17:21:44 +08:00
|
|
|
extern "C" void *__libc_malloc(__sanitizer::uptr size);
|
|
|
|
extern "C" void __libc_free(void *ptr);
|
|
|
|
# define LIBC_MALLOC __libc_malloc
|
|
|
|
# define LIBC_FREE __libc_free
|
2012-09-10 18:18:49 +08:00
|
|
|
#else // __linux__ && !ANDROID
|
2012-06-09 17:21:44 +08:00
|
|
|
# include <stdlib.h>
|
|
|
|
# define LIBC_MALLOC malloc
|
|
|
|
# define LIBC_FREE free
|
2012-09-10 18:18:49 +08:00
|
|
|
#endif // __linux__ && !ANDROID
|
2012-06-07 16:52:56 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-06-25 23:09:24 +08:00
|
|
|
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
2012-06-07 16:52:56 +08:00
|
|
|
|
|
|
|
void *InternalAlloc(uptr size) {
|
2012-07-06 00:18:28 +08:00
|
|
|
if (size + sizeof(u64) < size)
|
|
|
|
return 0;
|
2012-06-09 17:21:44 +08:00
|
|
|
void *p = LIBC_MALLOC(size + sizeof(u64));
|
2012-07-06 00:18:28 +08:00
|
|
|
if (p == 0)
|
|
|
|
return 0;
|
2012-06-25 23:09:24 +08:00
|
|
|
((u64*)p)[0] = kBlockMagic;
|
2012-06-07 16:52:56 +08:00
|
|
|
return (char*)p + sizeof(u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalFree(void *addr) {
|
2012-07-06 00:18:28 +08:00
|
|
|
if (addr == 0)
|
|
|
|
return;
|
2012-06-07 16:52:56 +08:00
|
|
|
addr = (char*)addr - sizeof(u64);
|
2012-06-25 23:09:24 +08:00
|
|
|
CHECK_EQ(((u64*)addr)[0], kBlockMagic);
|
2012-06-07 16:52:56 +08:00
|
|
|
((u64*)addr)[0] = 0;
|
2012-06-09 17:21:44 +08:00
|
|
|
LIBC_FREE(addr);
|
2012-06-07 16:52:56 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 17:30:58 +08:00
|
|
|
// LowLevelAllocator
|
|
|
|
static LowLevelAllocateCallback low_level_alloc_callback;
|
|
|
|
|
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
2012-08-27 22:51:36 +08:00
|
|
|
// Align allocation size.
|
|
|
|
size = RoundUpTo(size, 8);
|
2012-08-27 17:30:58 +08:00
|
|
|
if (allocated_end_ - allocated_current_ < (sptr)size) {
|
2012-11-24 13:03:11 +08:00
|
|
|
uptr size_to_allocate = Max(size, GetPageSizeCached());
|
2012-08-27 17:30:58 +08:00
|
|
|
allocated_current_ =
|
|
|
|
(char*)MmapOrDie(size_to_allocate, __FUNCTION__);
|
|
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
|
|
|
if (low_level_alloc_callback) {
|
|
|
|
low_level_alloc_callback((uptr)allocated_current_,
|
|
|
|
size_to_allocate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
|
|
|
|
void *res = allocated_current_;
|
|
|
|
allocated_current_ += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
|
|
|
|
low_level_alloc_callback = callback;
|
|
|
|
}
|
|
|
|
|
2012-06-07 16:52:56 +08:00
|
|
|
} // namespace __sanitizer
|