From 94ef6a9f91959077ff76caefce795043c1366f99 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 7 Jan 2025 10:52:14 -0800 Subject: [PATCH] Use debug version of malloc when ASSERTIONS=1. NFC Previously we were only using it with `ASSERTIONS=2`. The debug version dlmalloc has a lot very useful checks. Enabling this found a few real issues in our test code. --- ChangeLog.md | 4 ++++ embuilder.py | 2 ++ test/other/test_tsearch.c | 8 ++++++-- test/test_other.py | 6 +++--- tools/system_libs.py | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 67160174211a6..f16478d5484a9 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -61,6 +61,10 @@ See docs/process.md for more on how version tagging works. - JavaScript libraries can now be specified via `-lfoo.js`. This works like the existing `--js-library` flag but will search the library path (all paths specified with `-L`) for `libfoo.js`. (#23338) +- Emscripten now uses the debug version of malloc (i.e. assertions enabled) + when linking in debug mode (`-O0` and/or `-sASSERTIONS`). This means that + things like double-free will be detected in these builds. Previously this was + only true with `-sASSERTIONS=2`. (#23330) 3.1.74 - 12/14/24 ----------------- diff --git a/embuilder.py b/embuilder.py index bdee69849ee7b..b144c08872950 100755 --- a/embuilder.py +++ b/embuilder.py @@ -54,6 +54,7 @@ 'libdlmalloc-tracing', 'libdlmalloc-debug', 'libdlmalloc-ww', + 'libdlmalloc-ww-debug', 'libembind', 'libembind-rtti', 'libemmalloc', @@ -101,6 +102,7 @@ 'libc++-mt', 'libc++-mt-noexcept', 'libdlmalloc-mt', + 'libdlmalloc-mt-debug', 'libGL-emu', 'libGL-emu-webgl2-getprocaddr', 'libGL-mt-getprocaddr', diff --git a/test/other/test_tsearch.c b/test/other/test_tsearch.c index 85bae1dc603a1..f80d3bb4147c1 100644 --- a/test/other/test_tsearch.c +++ b/test/other/test_tsearch.c @@ -39,6 +39,10 @@ static void action(const void *nodep, VISIT which, int depth) { } } +static void free_node(void* nodep) { + // no-op since we didn't allocate any per-node data +} + int main(void) { int ptr[12]; void *val = NULL; @@ -51,6 +55,6 @@ int main(void) { assert(val); } twalk(root, action); - tdestroy(root, free); + tdestroy(root, free_node); return 0; -} +} diff --git a/test/test_other.py b/test/test_other.py index 214ec48c6d1fa..8151cdccab912 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -8019,10 +8019,10 @@ def test_dlmalloc_modes(self): printf("double-freed\n"); } ''') - self.run_process([EMCC, 'src.c']) + self.run_process([EMCC, 'src.c', '-O2']) self.assertContained('double-freed', self.run_js('a.out.js')) # in debug mode, the double-free is caught - self.run_process([EMCC, 'src.c', '-sASSERTIONS=2']) + self.run_process([EMCC, 'src.c', '-O0']) out = self.run_js('a.out.js', assert_returncode=NON_ZERO) self.assertContained('native code called abort()', out) @@ -13403,7 +13403,7 @@ def test_standard_library_mapping(self): # Check that the linker was run with `-mt` variants because `-pthread` was passed. self.assertContained(' -lc-mt-debug ', err) - self.assertContained(' -ldlmalloc-mt ', err) + self.assertContained(' -ldlmalloc-mt-debug ', err) self.assertContained(' -lcompiler_rt-mt ', err) def test_explicit_gl_linking(self): diff --git a/tools/system_libs.py b/tools/system_libs.py index f0d251cc36134..fefabe8195924 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1766,7 +1766,7 @@ def vary_on(cls): def get_default_variation(cls, **kwargs): return super().get_default_variation( malloc=settings.MALLOC, - is_debug=settings.ASSERTIONS >= 2, + is_debug=settings.ASSERTIONS, is_tracing=settings.EMSCRIPTEN_TRACING, memvalidate='memvalidate' in settings.MALLOC, verbose='verbose' in settings.MALLOC,