Skip to content

Commit

Permalink
fix lsan issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Sep 19, 2024
1 parent 20b9d61 commit e2eb92f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ jobs:
asan.test_externref_emjs_dynlink
asan.test_asyncify_longjmp
asan.test_pthread_run_on_main_thread
lsan.test_dylink_dso_needed
lsan.test_stdio_locking
lsan.test_dlfcn_basic
lsan.test_pthread_create
Expand Down
8 changes: 6 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,20 +1575,24 @@ addToLibrary({
#if USE_ASAN || USE_LSAN || UBSAN_RUNTIME
// When lsan or asan is enabled withBuiltinMalloc temporarily replaces calls
// to malloc, free, and memalign.
$withBuiltinMalloc__deps: ['emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign'
],
$withBuiltinMalloc__deps: [
'emscripten_builtin_malloc', 'emscripten_builtin_free', 'emscripten_builtin_memalign', 'emscripten_builtin_calloc'
],
$withBuiltinMalloc__docs: '/** @suppress{checkTypes} */',
$withBuiltinMalloc: (func) => {
var prev_malloc = typeof _malloc != 'undefined' ? _malloc : undefined;
var prev_calloc = typeof _calloc != 'undefined' ? _calloc : undefined;
var prev_memalign = typeof _memalign != 'undefined' ? _memalign : undefined;
var prev_free = typeof _free != 'undefined' ? _free : undefined;
_malloc = _emscripten_builtin_malloc;
_calloc = _emscripten_builtin_calloc;
_memalign = _emscripten_builtin_memalign;
_free = _emscripten_builtin_free;
try {
return func();
} finally {
_malloc = prev_malloc;
_calloc = prev_calloc;
_memalign = prev_memalign;
_free = prev_free;
}
Expand Down
1 change: 1 addition & 0 deletions system/include/emscripten/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ size_t emscripten_get_heap_max(void);
// dlmalloc and emmalloc.
void *emscripten_builtin_memalign(size_t alignment, size_t size);
void *emscripten_builtin_malloc(size_t size);
void *emscripten_builtin_calloc(size_t nmemb, size_t size);
void emscripten_builtin_free(void *ptr);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions system/lib/dlmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6083,6 +6083,7 @@ int mspace_mallopt(int param_number, int value) {
// This allows an easy mechanism for hooking into memory allocation.
#if defined(__EMSCRIPTEN__) && !ONLY_MSPACES
extern __typeof(malloc) emscripten_builtin_malloc __attribute__((alias("dlmalloc")));
extern __typeof(calloc) emscripten_builtin_calloc __attribute__((alias("dlcalloc")));
extern __typeof(free) emscripten_builtin_free __attribute__((alias("dlfree")));
extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlmemalign")));
#endif
Expand Down
5 changes: 3 additions & 2 deletions system/lib/emmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,9 @@ void *emmalloc_calloc(size_t num, size_t size) {
}
return ptr;
}
EMMALLOC_ALIAS(__libc_calloc, emmalloc_calloc);
EMMALLOC_ALIAS(calloc, emmalloc_calloc);
EMMALLOC_ALIAS(emscripten_builtin_calloc, emmalloc_calloc);
EMMALLOC_ALIAS(__libc_calloc, emmalloc_calloc);
EMMALLOC_ALIAS(calloc, emmalloc_calloc);

static int count_linked_list_size(Region *list) {
int size = 1;
Expand Down
1 change: 1 addition & 0 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ def create_pointer_conversion_wrappers(metadata):
'sbrk': 'pP',
'_emscripten_stack_alloc': 'pp',
'emscripten_builtin_malloc': 'pp',
'emscripten_builtin_calloc': 'ppp',
'malloc': 'pp',
'calloc': 'ppp',
'webidl_malloc': 'pp',
Expand Down

0 comments on commit e2eb92f

Please sign in to comment.