Skip to content

Commit 1e88f8c

Browse files
committed
Add libc::memcpy with Checked/ReadsChecked template parameters
Expose snmalloc's checked memcpy through the libc API surface: libc::memcpy<Checked, ReadsChecked>(dst, src, len) Checked controls destination bounds checking, ReadsChecked controls source bounds checking (defaults to same as Checked). Explicit instantiations provided in testlib for the common variants: <true,true>, <true,false>, <false,false>.
1 parent f26d146 commit 1e88f8c

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/snmalloc/global/libc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "globalalloc.h"
4+
#include "memcpy.h"
45

56
#include <errno.h>
67
#include <string.h>
@@ -225,4 +226,16 @@ namespace snmalloc::libc
225226
*memptr = p;
226227
return 0;
227228
}
229+
230+
/**
231+
* Checked memcpy through the libc API surface.
232+
* @tparam Checked check the destination is within a snmalloc allocation
233+
* @tparam ReadsChecked also check the source
234+
*/
235+
template<bool Checked, bool ReadsChecked = Checked>
236+
SNMALLOC_API void*
237+
memcpy(void* dst, const void* src, size_t len)
238+
{
239+
return snmalloc::memcpy<Checked, ReadsChecked>(dst, src, len);
240+
}
228241
} // namespace snmalloc::libc

src/test/snmalloc_testlib.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ namespace snmalloc
160160
return MAX_SMALL_SIZECLASS_BITS;
161161
}
162162

163+
// -- Explicit instantiations of libc::memcpy variants --------------------
164+
namespace libc
165+
{
166+
template void* memcpy<true, true>(void*, const void*, size_t);
167+
template void* memcpy<true, false>(void*, const void*, size_t);
168+
template void* memcpy<false, false>(void*, const void*, size_t);
169+
} // namespace libc
170+
163171
// -- PAL/AAL wrappers ----------------------------------------------------
164172

165173
size_t pal_address_bits()

src/test/snmalloc_testlib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ namespace snmalloc
125125
void* __malloc_start_pointer(void* ptr);
126126
void* __malloc_last_byte_pointer(void* ptr);
127127
void* __malloc_end_pointer(void* ptr);
128+
129+
template<bool Checked, bool ReadsChecked = Checked>
130+
void* memcpy(void* dst, const void* src, size_t len);
128131
} // namespace libc
129132

130133
/**

0 commit comments

Comments
 (0)