Skip to content

Commit 61d66fc

Browse files
committed
fix lsan issue
1 parent 20b9d61 commit 61d66fc

File tree

7 files changed

+14
-4
lines changed

7 files changed

+14
-4
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,20 +1575,24 @@ addToLibrary({
15751575
#if USE_ASAN || USE_LSAN || UBSAN_RUNTIME
15761576
// When lsan or asan is enabled withBuiltinMalloc temporarily replaces calls
15771577
// to malloc, free, and memalign.
1578-
$withBuiltinMalloc__deps: ['emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign'
1579-
],
1578+
$withBuiltinMalloc__deps: [
1579+
'emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign', 'emscripten_builtin_calloc'
1580+
],
15801581
$withBuiltinMalloc__docs: '/** @suppress{checkTypes} */',
15811582
$withBuiltinMalloc: (func) => {
15821583
var prev_malloc = typeof _malloc != 'undefined' ? _malloc : undefined;
1584+
var prev_calloc = typeof _calloc != 'undefined' ? _calloc : undefined;
15831585
var prev_memalign = typeof _memalign != 'undefined' ? _memalign : undefined;
15841586
var prev_free = typeof _free != 'undefined' ? _free : undefined;
15851587
_malloc = _emscripten_builtin_malloc;
1588+
_calloc = _emscripten_builtin_calloc;
15861589
_memalign = _emscripten_builtin_memalign;
15871590
_free = _emscripten_builtin_free;
15881591
try {
15891592
return func();
15901593
} finally {
15911594
_malloc = prev_malloc;
1595+
_calloc = prev_calloc;
15921596
_memalign = prev_memalign;
15931597
_free = prev_free;
15941598
}

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__)

tools/emscripten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ 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',
10261027
'calloc': 'ppp',
10271028
'webidl_malloc': 'pp',

0 commit comments

Comments
 (0)