Skip to content

Commit a38415a

Browse files
committed
fix tests
1 parent 1e93e00 commit a38415a

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/snmalloc/global/memcpy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ namespace snmalloc
494494
return report_fatal_bounds_error(
495495
dst, len, "memmove with destination out of bounds of heap allocation");
496496

497-
if (dst > src)
497+
if ((address_cast(dst) - address_cast(src)) < len)
498498
{
499499
// slow 'safe' reverse copy, we avoid optimised rollouts
500500
// to cope with typical memmove use cases, moving

src/snmalloc/override/memcpy.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern "C"
66
* Snmalloc checked memcpy.
77
*/
88
SNMALLOC_EXPORT void*
9-
SNMALLOC_NAME_MANGLE(memcpy)(void* dst, const void* src, size_t len)
9+
SNMALLOC_NAME_MANGLE(memcpy)(void* dst, const void* src, size_t len)
1010
{
1111
return snmalloc::memcpy<true>(dst, src, len);
1212
}
@@ -15,7 +15,7 @@ extern "C"
1515
* Snmalloc checked memmove.
1616
*/
1717
SNMALLOC_EXPORT void*
18-
SNMALLOC_NAME_MANGLE(memmove)(void* dst, const void* src, size_t len)
18+
SNMALLOC_NAME_MANGLE(memmove)(void* dst, const void* src, size_t len)
1919
{
2020
return snmalloc::memmove<true>(dst, src, len);
2121
}

src/test/func/memcpy/func-memcpy.cc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ void check_bounds(size_t size, size_t out_of_bounds)
163163
my_free(d);
164164
}
165165

166-
void check_overlaps(size_t size)
166+
void check_overlaps1()
167167
{
168-
START_TEST("memmove overlaps, size {}", size);
168+
size_t size = 16;
169+
START_TEST("memmove overlaps1");
169170
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
170-
auto sz = size / 2;
171171
for (size_t i = 0; i < size; ++i)
172172
{
173173
s[i] = static_cast<unsigned int>(i);
@@ -180,16 +180,29 @@ void check_overlaps(size_t size)
180180
my_memmove(ptr, s, size * sizeof(s[0]));
181181
EXPECT(ptr == s, "overlap error: {} {}", ptr, s);
182182
my_free(s);
183-
s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
183+
}
184+
185+
template<bool after>
186+
void check_overlaps2(size_t size)
187+
{
188+
START_TEST("memmove overlaps2, size {}", size);
189+
auto sz = size / 2;
190+
auto offset = size / 2;
191+
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int)));
184192
for (size_t i = 0; i < size; ++i)
185193
{
186194
s[i] = static_cast<unsigned int>(i);
187195
}
188-
ptr = s + sz;
189-
my_memmove(ptr, s, sz * sizeof(unsigned int));
190-
for (size_t i = 0; i < sz; ++i)
196+
auto dst = after ? s + offset : s;
197+
auto src = after ? s : s + offset;
198+
size_t i = after ? 0 : offset;
199+
size_t u = 0;
200+
my_memmove(dst, src, sz * sizeof(unsigned int));
201+
while (u < sz)
191202
{
192-
EXPECT(ptr[i] == s[i], "overlap error: {} {}", ptr[i], s[i]);
203+
EXPECT(dst[u] == i, "overlap error: {} {}", dst[u], i);
204+
u++;
205+
i++;
193206
}
194207
my_free(s);
195208
}
@@ -230,9 +243,12 @@ int main()
230243
check_size<true>(x);
231244
}
232245

233-
for (size_t x = 8; x < 256; x += 8)
246+
check_overlaps1();
247+
248+
for (size_t x = 8; x < 256; x += 64)
234249
{
235-
check_overlaps(x);
250+
check_overlaps2<false>(x);
251+
check_overlaps2<true>(x);
236252
}
237253
}
238254
#endif

0 commit comments

Comments
 (0)