Skip to content

Commit

Permalink
Merge pull request #24 from Dasaav-dsv/allocation-integrity-fix
Browse files Browse the repository at this point in the history
Fix allocator heuristic for integrity checking
  • Loading branch information
Dasaav-dsv authored Apr 26, 2024
2 parents bc5af4e + 7c08e41 commit edf82da
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions source/memory/from_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ static class alignas(1 << CHAR_BIT) liber_internal_allocator
size_t lg2align = static_cast<unsigned char>(data);
// Undo the encoding and confirm integrity
size_t nsize = (_block_size(p) ^ LIBER_ALLOC_MAGIC) - lg2align;
size_t nalign = ~0ull << lg2align;
if (lg2align >= 64 || nalign < nsize)
size_t size = lg2align - (_block_size(p) ^ LIBER_ALLOC_MAGIC);
size_t alignment = 1ull << lg2align;
if (lg2align >= 64 || alignment > size)
std::terminate();
return nalign - static_cast<ptrdiff_t>(nsize);
return size - alignment;
}
else if (!allocator) {
// Try WINAPI HeapSize
Expand Down Expand Up @@ -176,8 +177,8 @@ static class alignas(1 << CHAR_BIT) liber_internal_allocator
size_t lg2align = static_cast<unsigned char>(data);
size_t alignment = 1ull << lg2align;
// Confirm block integrity
size_t nsize = (_block_size(p) ^ LIBER_ALLOC_MAGIC) - lg2align;
if (~alignment < nsize)
size_t size = lg2align - (_block_size(p) ^ LIBER_ALLOC_MAGIC);
if (alignment > size)
std::terminate();
void* base = _block_base(p, alignment);
_zero_block_data(p);
Expand Down Expand Up @@ -258,8 +259,8 @@ static class alignas(1 << CHAR_BIT) liber_internal_allocator
// Could still have been allocated by a replaced system allocator
std::shared_lock lock{ this->all_allocators };
auto& all_allocators = this->all_allocators.get();
auto iter = std::find(all_allocators.begin(), all_allocators.end(),
allocator);
auto iter =
std::find(all_allocators.begin(), all_allocators.end(), allocator);
if (iter == all_allocators.end())
return allocator;
return nullptr;
Expand Down

0 comments on commit edf82da

Please sign in to comment.