diff --git a/src/snmalloc/backend_helpers/range_helpers.h b/src/snmalloc/backend_helpers/range_helpers.h index 77455ff49..6a9200268 100644 --- a/src/snmalloc/backend_helpers/range_helpers.h +++ b/src/snmalloc/backend_helpers/range_helpers.h @@ -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; diff --git a/src/test/func/range_helpers/range_helpers.cc b/src/test/func/range_helpers/range_helpers.cc new file mode 100644 index 000000000..fc59ff5f0 --- /dev/null +++ b/src/test/func/range_helpers/range_helpers.cc @@ -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 +#include +#include +#include + +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(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( + capptr::Chunk::unsafe_from(base), + length, + [&](capptr::Chunk p, size_t size, bool) { + auto addr = static_cast(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; +}