Skip to content

Commit cefc9de

Browse files
authored
Add emscripten_builtin_realloc (#23091)
Also improve testing for allocator wrapping. Fixes: #23080
1 parent b48aedd commit cefc9de

File tree

7 files changed

+27
-4
lines changed

7 files changed

+27
-4
lines changed

system/include/emscripten/heap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ size_t emscripten_get_heap_max(void);
4646
// dlmalloc and emmalloc.
4747
void *emscripten_builtin_memalign(size_t alignment, size_t size);
4848
void *emscripten_builtin_malloc(size_t size);
49+
void *emscripten_builtin_realloc(void *ptr, size_t size);
4950
void *emscripten_builtin_calloc(size_t nmemb, size_t size);
5051
void emscripten_builtin_free(void *ptr);
5152

system/lib/dlmalloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6082,6 +6082,7 @@ int mspace_mallopt(int param_number, int value) {
60826082
// This allows an easy mechanism for hooking into memory allocation.
60836083
#if defined(__EMSCRIPTEN__) && !ONLY_MSPACES
60846084
extern __typeof(malloc) emscripten_builtin_malloc __attribute__((alias("dlmalloc")));
6085+
extern __typeof(realloc) emscripten_builtin_realloc __attribute__((alias("dlrealloc")));
60856086
extern __typeof(calloc) emscripten_builtin_calloc __attribute__((alias("dlcalloc")));
60866087
extern __typeof(free) emscripten_builtin_free __attribute__((alias("dlfree")));
60876088
extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlmemalign")));

system/lib/emmalloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ void *emmalloc_aligned_realloc_uninitialized(void *ptr, size_t alignment, size_t
11131113
void *emmalloc_realloc(void *ptr, size_t size) {
11141114
return emmalloc_aligned_realloc(ptr, MALLOC_ALIGNMENT, size);
11151115
}
1116+
EMMALLOC_ALIAS(emscripten_builtin_realloc, emmalloc_realloc);
11161117
EMMALLOC_ALIAS(__libc_realloc, emmalloc_realloc);
11171118
EMMALLOC_ALIAS(realloc, emmalloc_realloc);
11181119

system/lib/mimalloc/src/alloc-override.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ mi_decl_weak int reallocarr(void* p, size_t count, size_t size) { return mi_r
298298

299299
#ifdef __EMSCRIPTEN__ // emscripten adds some more on top of WASI
300300
void* emscripten_builtin_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
301+
void* emscripten_builtin_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
301302
void* emscripten_builtin_free(void* p) MI_FORWARD0(mi_free, p)
302303
void* emscripten_builtin_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
303304
void* emscripten_builtin_calloc(size_t nmemb, size_t size) MI_FORWARD2(mi_calloc, nmemb, size)

test/core/test_wrap_malloc.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
static int totalAllocs;
1313
static int totalFrees;
14+
static int totalReallocs;
1415

1516
void *malloc(size_t size) {
1617
++totalAllocs;
@@ -19,6 +20,13 @@ void *malloc(size_t size) {
1920
return ptr;
2021
}
2122

23+
void *realloc(void* ptr, size_t size) {
24+
++totalReallocs;
25+
ptr = emscripten_builtin_realloc(ptr, size);
26+
emscripten_console_logf("Reallocated %zu bytes, got %p. %d pointers re-allocated total.", size, ptr, totalReallocs);
27+
return ptr;
28+
}
29+
2230
void free(void *ptr) {
2331
++totalFrees;
2432
emscripten_builtin_free(ptr);
@@ -39,9 +47,14 @@ int main() {
3947
free(ptr);
4048
}
4149

50+
void* ptr = malloc(50);
51+
ptr = realloc(ptr, 50);
52+
4253
emscripten_console_logf("totalAllocs: %d", totalAllocs);
4354
emscripten_console_logf("totalFrees: %d", totalFrees);
44-
assert(totalAllocs == 20);
55+
emscripten_console_logf("totalReallocs: %d", totalReallocs);
56+
assert(totalAllocs == 21);
57+
assert(totalReallocs == 1);
4558
assert(totalFrees == 20);
4659
emscripten_console_logf("OK.");
4760
return 0;

test/test_core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8610,8 +8610,14 @@ def test_mallinfo(self):
86108610

86118611
@no_asan('cannot replace malloc/free with ASan')
86128612
@no_lsan('cannot replace malloc/free with LSan')
8613-
def test_wrap_malloc(self):
8614-
self.do_runf('core/test_wrap_malloc.c', 'OK.')
8613+
@parameterized({
8614+
'': ([],),
8615+
'emmalloc': (['-sMALLOC=emmalloc'],),
8616+
# FIXME(https://github.com/emscripten-core/emscripten/issues/23090)
8617+
# 'mimalloc': (['-sMALLOC=mimalloc'],),
8618+
})
8619+
def test_wrap_malloc(self, args):
8620+
self.do_runf('core/test_wrap_malloc.c', 'OK.', emcc_args=args)
86158621

86168622
def test_environment(self):
86178623
self.set_setting('ASSERTIONS')

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ class libmalloc(MTLibrary):
17211721
def __init__(self, **kwargs):
17221722
self.malloc = kwargs.pop('malloc')
17231723
if self.malloc not in ('dlmalloc', 'emmalloc', 'emmalloc-debug', 'emmalloc-memvalidate', 'emmalloc-verbose', 'emmalloc-memvalidate-verbose', 'mimalloc', 'none'):
1724-
raise Exception('malloc must be one of "emmalloc[-debug|-memvalidate][-verbose]", "dlmalloc" or "none", see settings.js')
1724+
raise Exception('malloc must be one of "emmalloc[-debug|-memvalidate][-verbose]", "mimalloc", "dlmalloc" or "none", see settings.js')
17251725

17261726
self.is_tracing = kwargs.pop('is_tracing')
17271727
self.memvalidate = kwargs.pop('memvalidate')

0 commit comments

Comments
 (0)