Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Mar 8, 2023
1 parent 45dd8ff commit 9065f38
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/snmalloc/global/memcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ namespace snmalloc
return report_fatal_bounds_error(
dst, len, "memmove with destination out of bounds of heap allocation");

if (dst > src)
if ((address_cast(dst) - address_cast(src)) < len)
{
// slow 'safe' reverse copy, we avoid optimised rollouts
// to cope with typical memmove use cases, moving
Expand Down
4 changes: 2 additions & 2 deletions src/snmalloc/override/memcpy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern "C"
* Snmalloc checked memcpy.
*/
SNMALLOC_EXPORT void*
SNMALLOC_NAME_MANGLE(memcpy)(void* dst, const void* src, size_t len)
SNMALLOC_NAME_MANGLE(memcpy)(void* dst, const void* src, size_t len)
{
return snmalloc::memcpy<true>(dst, src, len);
}
Expand All @@ -15,7 +15,7 @@ extern "C"
* Snmalloc checked memmove.
*/
SNMALLOC_EXPORT void*
SNMALLOC_NAME_MANGLE(memmove)(void* dst, const void* src, size_t len)
SNMALLOC_NAME_MANGLE(memmove)(void* dst, const void* src, size_t len)
{
return snmalloc::memmove<true>(dst, src, len);
}
Expand Down
40 changes: 30 additions & 10 deletions src/test/func/memcpy/func-memcpy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ void check_bounds(size_t size, size_t out_of_bounds)
my_free(d);
}

void check_overlaps(size_t size)
void check_overlaps1()
{
START_TEST("memmove overlaps, size {}", size);
size_t size = 16;
START_TEST("memmove overlaps1");
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
auto sz = size / 2;
for (size_t i = 0; i < size; ++i)
{
s[i] = static_cast<unsigned int>(i);
Expand All @@ -177,16 +177,33 @@ void check_overlaps(size_t size)
my_memmove(ptr, s, size * sizeof(s[0]));
EXPECT(ptr == s, "overlap error: {} {}", ptr, s);
my_free(s);
s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
}

template<bool after>
void check_overlaps2(size_t size)
{
START_TEST("memmove overlaps2, size {}", size);
auto sz = size / 2;
auto offset = size / 2;
if constexpr (sizeof(void *) == 8)
{
offset = size / 3;
}
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
for (size_t i = 0; i < size; ++i)
{
s[i] = static_cast<unsigned int>(i);
}
ptr = s + sz;
my_memmove(ptr, s, sz * sizeof(unsigned int));
for (size_t i = 0; i < sz; ++i)
auto dst = after ? s + offset : s;
auto src = after ? s : s + offset;
size_t i = after ? 0 : offset;
size_t u = 0;
my_memmove(dst, src, sz * sizeof(unsigned int));
while (u < sz)
{
EXPECT(ptr[i] == s[i], "overlap error: {} {}", ptr[i], s[i]);
EXPECT(dst[u] == i, "overlap error: {} {}", dst[u], i);
u++;
i++;
}
my_free(s);
}
Expand Down Expand Up @@ -223,9 +240,12 @@ int main()
check_size<true>(x);
}

for (size_t x = 8; x < 256; x += 8)
check_overlaps1();

for (size_t x = 8; x < 256; x += 64)
{
check_overlaps(x);
check_overlaps2<false>(x);
check_overlaps2<true>(x);
}
}
#endif

0 comments on commit 9065f38

Please sign in to comment.