Skip to content

extract_metadata: Improve detection of mainReadsParams #17702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27498
25683
1 change: 0 additions & 1 deletion test/other/metadce/test_metadce_mem_O3.exports
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ b
c
d
e
f
1 change: 0 additions & 1 deletion test/other/metadce/test_metadce_mem_O3.funcs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
$__wasm_call_ctors
$main
$sbrk
$stackAlloc
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6153
5284
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5957
5935
1 change: 0 additions & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.exports
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ b
c
d
e
f
1 change: 0 additions & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.funcs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
$__wasm_call_ctors
$main
$sbrk
$stackAlloc
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6484
5606
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5958
5936
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
83875
83653
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56428
56206
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
65940
65556
2 changes: 1 addition & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10055,7 +10055,7 @@ def test_main_reads_params(self):
no = os.path.getsize('no.js')
create_file('yes.c', '''
int main(int argc, char **argv) {
return argc;
return (long)argv[argc-1];
}
''')
self.run_process([EMCC, 'yes.c', '-O3', '-o', 'yes.js'])
Expand Down
27 changes: 17 additions & 10 deletions tools/extract_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def skip_function_header(module):
num_local_decls -= 1


def is_wrapper_function(module, function):
def is_orig_main_wrapper(module, function):
module.seek(function.offset)
skip_function_header(module)
end = function.offset + function.size
Expand All @@ -29,11 +29,15 @@ def is_wrapper_function(module, function):
print(e)
return False
if opcode == OpCode.CALL:
callee = module.read_uleb() # noqa
elif opcode == OpCode.END:
break
module.read_uleb() # callee
elif opcode in (OpCode.LOCAL_GET, OpCode.LOCAL_SET):
module.read_uleb() # local index
elif opcode in (OpCode.END, OpCode.RETURN):
pass
else:
# Any other opcodes and we assume this not a simple wrapper
return False

assert opcode == OpCode.END
return True

Expand Down Expand Up @@ -199,17 +203,20 @@ def get_asm_strings(module, export_map):

def get_main_reads_params(module, export_map):
if settings.STANDALONE_WASM:
return 1
return True

main = export_map.get('main') or export_map.get('__main_argc_argv')
if not main or main.kind != webassembly.ExternType.FUNC:
return 0
return False

main_func = module.get_function(main.index)
if is_wrapper_function(module, main_func):
return 0
else:
return 1
if is_orig_main_wrapper(module, main_func):
# If main is simple wrapper function then we know that __orginial_main
# doesn't read arguments.
return False

# By default assume params are read
return True


def get_named_globals(module, exports):
Expand Down