Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Standalone View
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
Show First 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | if (!addr) | ||||
return; | return; | ||||
addr = (char*)addr - sizeof(u64); | addr = (char*)addr - sizeof(u64); | ||||
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]); | CHECK_EQ(kBlockMagic, ((u64*)addr)[0]); | ||||
((u64*)addr)[0] = 0; | ((u64*)addr)[0] = 0; | ||||
RawInternalFree(addr, cache); | RawInternalFree(addr, cache); | ||||
} | } | ||||
// LowLevelAllocator | // LowLevelAllocator | ||||
constexpr uptr kLowLevelAllocatorDefaultAlignment = 8; | |||||
static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment; | |||||
static LowLevelAllocateCallback low_level_alloc_callback; | static LowLevelAllocateCallback low_level_alloc_callback; | ||||
void *LowLevelAllocator::Allocate(uptr size) { | void *LowLevelAllocator::Allocate(uptr size) { | ||||
// Align allocation size. | // Align allocation size. | ||||
size = RoundUpTo(size, 8); | size = RoundUpTo(size, low_level_alloc_min_alignment); | ||||
if (allocated_end_ - allocated_current_ < (sptr)size) { | if (allocated_end_ - allocated_current_ < (sptr)size) { | ||||
vitalybuka: Why can't this be just a shadow granularity? | |||||
Not Done ReplyInline ActionsIt can, but I was not clear on how to get that value -- waltl: It can, but I was not clear on how to get that value --
SHADOW_GRANULARITY is defined in an… | |||||
uptr size_to_allocate = Max(size, GetPageSizeCached()); | uptr size_to_allocate = Max(size, GetPageSizeCached()); | ||||
allocated_current_ = | allocated_current_ = | ||||
(char*)MmapOrDie(size_to_allocate, __func__); | (char*)MmapOrDie(size_to_allocate, __func__); | ||||
allocated_end_ = allocated_current_ + size_to_allocate; | allocated_end_ = allocated_current_ + size_to_allocate; | ||||
if (low_level_alloc_callback) { | if (low_level_alloc_callback) { | ||||
low_level_alloc_callback((uptr)allocated_current_, | low_level_alloc_callback((uptr)allocated_current_, | ||||
size_to_allocate); | size_to_allocate); | ||||
} | } | ||||
} | } | ||||
CHECK(allocated_end_ - allocated_current_ >= (sptr)size); | CHECK(allocated_end_ - allocated_current_ >= (sptr)size); | ||||
void *res = allocated_current_; | void *res = allocated_current_; | ||||
allocated_current_ += size; | allocated_current_ += size; | ||||
return res; | return res; | ||||
} | } | ||||
void SetLowLevelAllocateMinAlignment(uptr alignment) { | |||||
CHECK(IsPowerOfTwo(alignment)); | |||||
low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment); | |||||
} | |||||
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) { | void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) { | ||||
low_level_alloc_callback = callback; | low_level_alloc_callback = callback; | ||||
} | } | ||||
static atomic_uint8_t allocator_out_of_memory = {0}; | static atomic_uint8_t allocator_out_of_memory = {0}; | ||||
static atomic_uint8_t allocator_may_return_null = {0}; | static atomic_uint8_t allocator_may_return_null = {0}; | ||||
bool IsAllocatorOutOfMemory() { | bool IsAllocatorOutOfMemory() { | ||||
Did you really mean that alignment can only go up? How about this: constexpr uptr kLowLevelAllocatorDefaultAlignment = 8; alekseyshl: Did you really mean that alignment can only go up? How about this:
constexpr uptr… | |||||
Not Done ReplyInline ActionsDone. Not sure if the final kLowLevelAllocatorDefaultAlignment is a typo, but I changed it to low_level_alloc_min_alignment so that if SetLowLevelAllocateMinAlignment is called multiple times, we get a max of all the input alignments. waltl: Done. Not sure if the final kLowLevelAllocatorDefaultAlignment is a typo, but I changed it to… | |||||
return atomic_load_relaxed(&allocator_out_of_memory); | return atomic_load_relaxed(&allocator_out_of_memory); | ||||
} | } | ||||
// Prints error message and kills the program. | // Prints error message and kills the program. | ||||
void NORETURN ReportAllocatorCannotReturnNull() { | void NORETURN ReportAllocatorCannotReturnNull() { | ||||
Report("%s's allocator is terminating the process instead of returning 0\n", | Report("%s's allocator is terminating the process instead of returning 0\n", | ||||
SanitizerToolName); | SanitizerToolName); | ||||
Report("If you don't like this behavior set allocator_may_return_null=1\n"); | Report("If you don't like this behavior set allocator_may_return_null=1\n"); | ||||
Show All 36 Lines |
Why can't this be just a shadow granularity?