Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/snmalloc/backend_helpers/range_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ namespace snmalloc
auto end = pointer_offset(base, length);
base = pointer_align_up(base, bits::one_at_bit(MIN_BITS));
end = pointer_align_down(end, bits::one_at_bit(MIN_BITS));

/*
* The aligned range can be empty if the original range is smaller than a
* minimum-sized block, or if its ends round towards each other. In that
* case there is nothing to add, and computing the difference would
* underflow `length` and make the loop below walk outside the range.
*/
if (!(base < end))
return;

length = pointer_diff(base, end);

bool first = true;
Expand Down
93 changes: 93 additions & 0 deletions src/test/func/range_helpers/range_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Unit tests for range_to_pow_2_blocks.
*
* This includes ranges that are smaller than a minimum-sized block and ranges
* whose ends round towards each other. Such ranges must not cause the length
* to underflow (see https://github.com/microsoft/snmalloc/issues/567).
*/

#include <cstdlib>
#include <iostream>
#include <snmalloc/snmalloc_core.h>
#include <test/setup.h>

using namespace snmalloc;

namespace
{
static constexpr size_t MIN_BITS = 4;
static constexpr size_t MIN_BLOCK = size_t(1) << MIN_BITS;

[[noreturn]] void fail(size_t offset, size_t length, const char* message)
{
std::cout << "range_to_pow_2_blocks failed for offset=" << offset
<< " length=" << length << ": " << message << std::endl;
abort();
}

void test_range(size_t offset, size_t length)
{
// Large enough to hold any (offset, length) pair used by the caller.
alignas(MIN_BLOCK * 16) char buffer[1024];
auto* base = buffer + offset;

auto base_addr = static_cast<size_t>(address_cast(base));
size_t aligned_start = bits::align_up(base_addr, MIN_BLOCK);
size_t aligned_end = bits::align_down(base_addr + length, MIN_BLOCK);
size_t expected_total =
aligned_end > aligned_start ? aligned_end - aligned_start : 0;

size_t total = 0;
size_t blocks = 0;
size_t expected_next = 0;

range_to_pow_2_blocks<MIN_BITS>(
capptr::Chunk<void>::unsafe_from(base),
length,
[&](capptr::Chunk<void> p, size_t size, bool) {
auto addr = static_cast<size_t>(address_cast(p));

if ((addr < base_addr) || (addr + size > base_addr + length))
fail(offset, length, "block is outside the requested range");

if (!bits::is_pow2(size))
fail(offset, length, "block size is not a power of two");

if ((addr % size) != 0)
fail(offset, length, "block is not naturally aligned");

if ((blocks == 0) && (addr != aligned_start))
fail(offset, length, "first block does not start at the range start");

if ((blocks != 0) && (addr != expected_next))
fail(offset, length, "blocks are not contiguous");

blocks++;
total += size;
expected_next = addr + size;
});

if (total != expected_total)
fail(offset, length, "total block size does not match the range");

if ((blocks == 0) && (expected_total != 0))
fail(offset, length, "no blocks were produced for a non-empty range");
}
} // namespace

int main(int argc, char** argv)
{
UNUSED(argc, argv);

setup();

for (size_t offset = 0; offset < 64; offset++)
{
for (size_t length = 0; length <= 256; length++)
{
test_range(offset, length);
}
}

std::cout << "All range_to_pow_2_blocks tests passed." << std::endl;
}
Loading