Skip to content

Commit 2f21e23

Browse files
authored
Stop emitting the features section in some buggy cases, in optimized builds (#20409)
wasm-emscripten-finalize strips the features section (after emitting metadata using it), but we no longer always run that tool, so we need to strip it manually otherwise, at least in optimized builds (in unoptimized ones, it's just a few more bytes so we don't bother, as it would slow down the builds).
1 parent 1311f22 commit 2f21e23

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

emcc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,14 @@ def should_run_binaryen_optimizer():
627627
def get_binaryen_passes():
628628
passes = []
629629
optimizing = should_run_binaryen_optimizer()
630+
# wasm-emscripten-finalize will strip the features section for us
631+
# automatically, but if we did not modify the wasm then we didn't run it,
632+
# and in an optimized build we strip it manually here. (note that in an
633+
# unoptimized build we might end up with the features section, if we neither
634+
# optimize nor run wasm-emscripten-finalize, but a few extra bytes in the
635+
# binary don't matter in an unoptimized build)
636+
if optimizing:
637+
passes += ['--strip-target-features']
630638
# safe heap must run before post-emscripten, so post-emscripten can apply the sbrk ptr
631639
if settings.SAFE_HEAP:
632640
passes += ['--safe-heap']
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
319
1+
73
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19972
1+
19973
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4495
1+
4496

test/test_other.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9664,6 +9664,18 @@ def test_wasm_producers_section(self, args):
96649664
size_with_section = os.path.getsize('a.out.wasm')
96659665
self.assertLess(size, size_with_section)
96669666

9667+
@parameterized({
9668+
'': ([],),
9669+
# in some modes we skip wasm-emscripten-finalize, which normally strips the
9670+
# features section for us, so add testing for those
9671+
'bigint': (['-sWASM_BIGINT'],),
9672+
'wasm64': (['-sMEMORY64'],),
9673+
})
9674+
def test_wasm_features_section(self, args):
9675+
# The features section should never be in our output, when we optimize.
9676+
self.run_process([EMCC, test_file('hello_world.c'), '-O2'] + args)
9677+
self.verify_custom_sec_existence('a.out.wasm', 'target_features', False)
9678+
96679679
def test_js_preprocess(self):
96689680
# Use stderr rather than stdout here because stdout is redirected to the output JS file itself.
96699681
create_file('lib.js', '''

0 commit comments

Comments
 (0)