Skip to content

Commit 0265d84

Browse files
authored
Use debug version of malloc when ASSERTIONS=1. NFC (#23330)
Previously we were only using it with `ASSERTIONS=2`. The debug version dlmalloc has many very useful checks, including checking for double-free. This is something that is done by glibc malloc even in release builds. It seems reasonable that we should do this basic checking in our debug builds at least. Required fixing two genuine bugs: One double-free in test code and one invalid free in embind. Fixes: #23360
1 parent 5627123 commit 0265d84

File tree

8 files changed

+21
-11
lines changed

8 files changed

+21
-11
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ See docs/process.md for more on how version tagging works.
6666
specified with `-L`) for `libfoo.js`. (#23338)
6767
- The `mallinfo` struct members are now defined as `size_t` which makes them
6868
compatible with larger memories, and is also how linux defines them. (#23368)
69+
- Emscripten now uses the debug version of malloc (i.e. assertions enabled)
70+
when linking in debug mode (`-O0` and/or `-sASSERTIONS`). This means that
71+
things like double-free will be detected in these builds. Previously this was
72+
only true with `-sASSERTIONS=2`. (#23330)
6973

7074
3.1.74 - 12/14/24
7175
-----------------

embuilder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
'libdlmalloc-tracing',
5555
'libdlmalloc-debug',
5656
'libdlmalloc-ww',
57+
'libdlmalloc-ww-debug',
5758
'libembind',
5859
'libembind-rtti',
5960
'libemmalloc',
@@ -101,6 +102,7 @@
101102
'libc++-mt',
102103
'libc++-mt-noexcept',
103104
'libdlmalloc-mt',
105+
'libdlmalloc-mt-debug',
104106
'libGL-emu',
105107
'libGL-emu-webgl2-getprocaddr',
106108
'libGL-mt-getprocaddr',

src/embind/embind.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ var LibraryEmbind = {
567567
for (var i = 0; i < length; ++i) {
568568
var charCode = value.charCodeAt(i);
569569
if (charCode > 255) {
570-
_free(ptr);
570+
_free(base);
571571
throwBindingError('String has UTF-16 code units that do not fit in 8 bits');
572572
}
573573
HEAPU8[ptr + i] = charCode;

test/code_size/embind_hello_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"a.html": 552,
33
"a.html.gz": 380,
44
"a.js": 9879,
5-
"a.js.gz": 4288,
5+
"a.js.gz": 4287,
66
"a.wasm": 7348,
77
"a.wasm.gz": 3375,
88
"total": 17779,
9-
"total_gz": 8043
9+
"total_gz": 8042
1010
}

test/code_size/embind_val_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"a.html": 552,
33
"a.html.gz": 380,
44
"a.js": 7153,
5-
"a.js.gz": 3042,
5+
"a.js.gz": 3041,
66
"a.wasm": 9119,
77
"a.wasm.gz": 4701,
88
"total": 16824,
9-
"total_gz": 8123
9+
"total_gz": 8122
1010
}

test/other/test_tsearch.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ static void action(const void *nodep, VISIT which, int depth) {
3939
}
4040
}
4141

42+
static void free_node(void* nodep) {
43+
// no-op since we didn't allocate any per-node data
44+
}
45+
4246
int main(void) {
4347
int ptr[12];
4448
void *val = NULL;
@@ -51,6 +55,6 @@ int main(void) {
5155
assert(val);
5256
}
5357
twalk(root, action);
54-
tdestroy(root, free);
58+
tdestroy(root, free_node);
5559
return 0;
56-
}
60+
}

test/test_other.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8019,10 +8019,10 @@ def test_dlmalloc_modes(self):
80198019
printf("double-freed\n");
80208020
}
80218021
''')
8022-
self.run_process([EMCC, 'src.c'])
8022+
self.run_process([EMCC, 'src.c', '-O2'])
80238023
self.assertContained('double-freed', self.run_js('a.out.js'))
80248024
# in debug mode, the double-free is caught
8025-
self.run_process([EMCC, 'src.c', '-sASSERTIONS=2'])
8025+
self.run_process([EMCC, 'src.c', '-O0'])
80268026
out = self.run_js('a.out.js', assert_returncode=NON_ZERO)
80278027
self.assertContained('native code called abort()', out)
80288028

@@ -13405,7 +13405,7 @@ def test_standard_library_mapping(self):
1340513405

1340613406
# Check that the linker was run with `-mt` variants because `-pthread` was passed.
1340713407
self.assertContained(' -lc-mt-debug ', err)
13408-
self.assertContained(' -ldlmalloc-mt ', err)
13408+
self.assertContained(' -ldlmalloc-mt-debug ', err)
1340913409
self.assertContained(' -lcompiler_rt-mt ', err)
1341013410

1341113411
def test_explicit_gl_linking(self):

tools/system_libs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ def vary_on(cls):
17661766
def get_default_variation(cls, **kwargs):
17671767
return super().get_default_variation(
17681768
malloc=settings.MALLOC,
1769-
is_debug=settings.ASSERTIONS >= 2,
1769+
is_debug=settings.ASSERTIONS,
17701770
is_tracing=settings.EMSCRIPTEN_TRACING,
17711771
memvalidate='memvalidate' in settings.MALLOC,
17721772
verbose='verbose' in settings.MALLOC,

0 commit comments

Comments
 (0)