Skip to content

Commit 16a0bf1

Browse files
authored
Reapply "Prefer calloc over of malloc+zeroMemory. NFC" (#22596)
This reverts commit 50e0b07. This change also adds calloc to the list of symbols that we provide `emscripten_builtin_` variants for.
1 parent 83a8e5d commit 16a0bf1

18 files changed

+32
-29
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ jobs:
515515
asan.test_externref_emjs_dynlink
516516
asan.test_asyncify_longjmp
517517
asan.test_pthread_run_on_main_thread
518+
lsan.test_dylink_dso_needed
518519
lsan.test_stdio_locking
519520
lsan.test_dlfcn_basic
520521
lsan.test_pthread_create

src/library.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,21 +1574,26 @@ addToLibrary({
15741574

15751575
#if USE_ASAN || USE_LSAN || UBSAN_RUNTIME
15761576
// When lsan or asan is enabled withBuiltinMalloc temporarily replaces calls
1577-
// to malloc, free, and memalign.
1578-
$withBuiltinMalloc__deps: ['emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign'
1579-
],
1577+
// to malloc, calloc, free, and memalign.
1578+
$withBuiltinMalloc__deps: [
1579+
'malloc', 'calloc', 'free', 'memalign',
1580+
'emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign', 'emscripten_builtin_calloc'
1581+
],
15801582
$withBuiltinMalloc__docs: '/** @suppress{checkTypes} */',
15811583
$withBuiltinMalloc: (func) => {
15821584
var prev_malloc = typeof _malloc != 'undefined' ? _malloc : undefined;
1585+
var prev_calloc = typeof _calloc != 'undefined' ? _calloc : undefined;
15831586
var prev_memalign = typeof _memalign != 'undefined' ? _memalign : undefined;
15841587
var prev_free = typeof _free != 'undefined' ? _free : undefined;
15851588
_malloc = _emscripten_builtin_malloc;
1589+
_calloc = _emscripten_builtin_calloc;
15861590
_memalign = _emscripten_builtin_memalign;
15871591
_free = _emscripten_builtin_free;
15881592
try {
15891593
return func();
15901594
} finally {
15911595
_malloc = prev_malloc;
1596+
_calloc = prev_calloc;
15921597
_memalign = prev_memalign;
15931598
_free = prev_free;
15941599
}

src/library_dylink.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ var LibraryDylink = {
363363
// Allocate memory even if malloc isn't ready yet. The allocated memory here
364364
// must be zero initialized since its used for all static data, including bss.
365365
$getMemory__noleakcheck: true,
366-
$getMemory__deps: ['$GOT', '__heap_base', '$zeroMemory', '$alignMemory', 'malloc'],
366+
$getMemory__deps: ['$GOT', '__heap_base', '$alignMemory', 'calloc'],
367367
$getMemory: (size) => {
368368
// After the runtime is initialized, we must only use sbrk() normally.
369369
#if DYLINK_DEBUG
@@ -373,7 +373,7 @@ var LibraryDylink = {
373373
// Currently we don't support freeing of static data when modules are
374374
// unloaded via dlclose. This function is tagged as `noleakcheck` to
375375
// avoid having this reported as leak.
376-
return zeroMemory(_malloc(size), size);
376+
return _calloc(size, 1);
377377
}
378378
var ret = ___heap_base;
379379
// Keep __heap_base stack aligned.
@@ -599,7 +599,7 @@ var LibraryDylink = {
599599
$loadWebAssemblyModule__deps: [
600600
'$loadDynamicLibrary', '$getMemory',
601601
'$relocateExports', '$resolveGlobalSymbol', '$GOTHandler',
602-
'$getDylinkMetadata', '$alignMemory', '$zeroMemory',
602+
'$getDylinkMetadata', '$alignMemory',
603603
'$currentModuleWeakSymbols',
604604
'$updateTableMap',
605605
'$wasmTable',

src/library_pthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var LibraryPThread = {
3535
$PThread__postset: 'PThread.init();',
3636
$PThread__deps: ['_emscripten_thread_init',
3737
'$terminateWorker',
38-
'$cleanupThread', '$zeroMemory',
38+
'$cleanupThread',
3939
#if MAIN_MODULE
4040
'$markAsFinished',
4141
#endif

src/library_sdl.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ var LibrarySDL = {
13571357
return SDL.version;
13581358
},
13591359

1360-
SDL_Init__deps: ['$zeroMemory', 'memcpy'],
1360+
SDL_Init__deps: ['calloc', 'memcpy'],
13611361
SDL_Init__proxy: 'sync',
13621362
SDL_Init__docs: '/** @param{number} initFlags */',
13631363
SDL_Init: (initFlags) => {
@@ -1376,8 +1376,7 @@ var LibrarySDL = {
13761376
}
13771377

13781378
window.addEventListener("unload", SDL.receiveEvent);
1379-
SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs
1380-
zeroMemory(SDL.keyboardState, 0x10000);
1379+
SDL.keyboardState = _calloc(0x10000, 1); // Our SDL needs 512, but 64K is safe for older SDLs
13811380
// Initialize this structure carefully for closure
13821381
SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */;
13831382
SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */;
@@ -1413,11 +1412,10 @@ var LibrarySDL = {
14131412
return 1;
14141413
},
14151414

1416-
SDL_GetVideoInfo__deps: ['$zeroMemory'],
1415+
SDL_GetVideoInfo__deps: ['calloc'],
14171416
SDL_GetVideoInfo__proxy: 'sync',
14181417
SDL_GetVideoInfo: () => {
1419-
var ret = _malloc({{{ C_STRUCTS.SDL_VideoInfo.__size__ }}});
1420-
zeroMemory(ret, {{{ C_STRUCTS.SDL_version.__size__ }}});
1418+
var ret = _calloc({{{ C_STRUCTS.SDL_VideoInfo.__size__ }}}, 1);
14211419
{{{ makeSetValue('ret', C_STRUCTS.SDL_VideoInfo.current_w, 'Module["canvas"].width', 'i32') }}};
14221420
{{{ makeSetValue('ret', C_STRUCTS.SDL_VideoInfo.current_h, 'Module["canvas"].height', 'i32') }}};
14231421
return ret;

system/include/emscripten/heap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ size_t emscripten_get_heap_max(void);
4242
// dlmalloc and emmalloc.
4343
void *emscripten_builtin_memalign(size_t alignment, size_t size);
4444
void *emscripten_builtin_malloc(size_t size);
45+
void *emscripten_builtin_calloc(size_t nmemb, size_t size);
4546
void emscripten_builtin_free(void *ptr);
4647

4748
#ifdef __cplusplus

system/lib/dlmalloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6083,6 +6083,7 @@ int mspace_mallopt(int param_number, int value) {
60836083
// This allows an easy mechanism for hooking into memory allocation.
60846084
#if defined(__EMSCRIPTEN__) && !ONLY_MSPACES
60856085
extern __typeof(malloc) emscripten_builtin_malloc __attribute__((alias("dlmalloc")));
6086+
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")));
60886089
#endif

system/lib/emmalloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,9 @@ void *emmalloc_calloc(size_t num, size_t size) {
11401140
}
11411141
return ptr;
11421142
}
1143-
EMMALLOC_ALIAS(__libc_calloc, emmalloc_calloc);
1144-
EMMALLOC_ALIAS(calloc, emmalloc_calloc);
1143+
EMMALLOC_ALIAS(emscripten_builtin_calloc, emmalloc_calloc);
1144+
EMMALLOC_ALIAS(__libc_calloc, emmalloc_calloc);
1145+
EMMALLOC_ALIAS(calloc, emmalloc_calloc);
11451146

11461147
static int count_linked_list_size(Region *list) {
11471148
int size = 1;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_allo
286286
void* emscripten_builtin_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
287287
void* emscripten_builtin_free(void* p) MI_FORWARD0(mi_free, p)
288288
void* emscripten_builtin_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
289+
void* emscripten_builtin_calloc(size_t nmemb, size_t size) MI_FORWARD2(mi_calloc, nmemb, size)
289290
#endif
290291

291292
#elif defined(__GLIBC__) && defined(__linux__)

test/other/codesize/test_codesize_hello_dylink.exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ __wasm_apply_data_relocs
22
__wasm_call_ctors
33
_emscripten_stack_alloc
44
_emscripten_stack_restore
5+
calloc
56
dynCall_jiji
67
emscripten_stack_get_current
78
main
8-
malloc
99
setThrew

test/other/codesize/test_codesize_hello_dylink.funcs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $__wasm_apply_global_relocs
88
$__wasm_call_ctors
99
$_emscripten_stack_alloc
1010
$_emscripten_stack_restore
11-
$dlmalloc
11+
$dlcalloc
1212
$emscripten_stack_get_current
1313
$legalstub$dynCall_jiji
1414
$main
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6292
1+
6285
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13833
1+
13820
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9350
1+
9763

test/test_core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,9 +1943,7 @@ def test_em_asm_side_module(self):
19431943
def test_em_js(self, args, force_c):
19441944
if '-sMAIN_MODULE=2' in args:
19451945
self.check_dylink()
1946-
else:
1947-
self.emcc_args += ['-sEXPORTED_FUNCTIONS=_main,_malloc']
1948-
self.emcc_args += args
1946+
self.emcc_args += ['-sEXPORTED_FUNCTIONS=_main,_malloc'] + args
19491947
if '-pthread' in args:
19501948
self.setup_node_pthreads()
19511949

test/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ def test_dylink_pthread_bigint_em_asm(self):
21892189
def test_dylink_pthread_bigint_em_js(self):
21902190
self.set_setting('MAIN_MODULE', 2)
21912191
self.set_setting('WASM_BIGINT')
2192+
self.set_setting('EXPORTED_FUNCTIONS', '_malloc,_main')
21922193
self.emcc_args += ['-Wno-experimental', '-pthread']
21932194
self.do_runf('core/test_em_js.cpp')
21942195

tools/emscripten.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,9 @@ def create_pointer_conversion_wrappers(metadata):
10221022
'sbrk': 'pP',
10231023
'_emscripten_stack_alloc': 'pp',
10241024
'emscripten_builtin_malloc': 'pp',
1025+
'emscripten_builtin_calloc': 'ppp',
10251026
'malloc': 'pp',
1027+
'calloc': 'ppp',
10261028
'webidl_malloc': 'pp',
10271029
'memalign': 'ppp',
10281030
'memcmp': '_ppp',

tools/link.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,12 +1518,6 @@ def phase_linker_setup(options, state, newargs):
15181518

15191519
if sanitize:
15201520
settings.USE_OFFSET_CONVERTER = 1
1521-
settings.REQUIRED_EXPORTS += [
1522-
'memalign',
1523-
'emscripten_builtin_memalign',
1524-
'emscripten_builtin_malloc',
1525-
'emscripten_builtin_free',
1526-
]
15271521

15281522
if ('leak' in sanitize or 'address' in sanitize) and not settings.ALLOW_MEMORY_GROWTH:
15291523
# Increase the minimum memory requirements to account for extra memory

0 commit comments

Comments
 (0)