Skip to content

Commit 4da2ad6

Browse files
authored
name function imports using name section (#1290)
1 parent 6cf7343 commit 4da2ad6

File tree

8 files changed

+43
-31
lines changed

8 files changed

+43
-31
lines changed

src/wasm-binary.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,6 @@ class WasmBinaryBuilder {
826826
void readMemory();
827827
void readSignatures();
828828

829-
std::vector<Name> functionImportIndexes; // index in function index space => name of function import
830-
831829
// gets a name in the combined function import+defined function space
832830
Name getFunctionIndexName(Index i);
833831
void getResizableLimits(Address& initial, Address& max, bool& shared, Address defaultIfNoMax);
@@ -844,7 +842,9 @@ class WasmBinaryBuilder {
844842

845843
// We read functions before we know their names, so we need to backpatch the names later
846844
std::vector<Function*> functions; // we store functions here before wasm.addFunction after we know their names
845+
std::vector<Import*> functionImports; // we store function imports here before wasm.addFunctionImport after we know their names
847846
std::map<Index, std::vector<Call*>> functionCalls; // at index i we have all calls to the defined function i
847+
std::map<Index, std::vector<CallImport*>> functionImportCalls; // at index i we have all callImports to the imported function i
848848
Function* currFunction = nullptr;
849849
Index endOfFunction = -1; // before we see a function (like global init expressions), there is no end of function to check
850850

src/wasm/wasm-binary.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,12 +1486,12 @@ void WasmBinaryBuilder::readSignatures() {
14861486
}
14871487

14881488
Name WasmBinaryBuilder::getFunctionIndexName(Index i) {
1489-
if (i < functionImportIndexes.size()) {
1490-
auto* import = wasm.getImport(functionImportIndexes[i]);
1489+
if (i < functionImports.size()) {
1490+
auto* import = functionImports[i];
14911491
assert(import->kind == ExternalKind::Function);
14921492
return import->name;
14931493
} else {
1494-
i -= functionImportIndexes.size();
1494+
i -= functionImports.size();
14951495
if (i >= wasm.functions.size()) {
14961496
throw ParseException("bad function index");
14971497
}
@@ -1529,7 +1529,8 @@ void WasmBinaryBuilder::readImports() {
15291529
}
15301530
curr->functionType = wasm.functionTypes[index]->name;
15311531
assert(curr->functionType.is());
1532-
functionImportIndexes.push_back(curr->name);
1532+
functionImports.push_back(curr);
1533+
continue; // don't add the import yet, we add them later after we know their names
15331534
break;
15341535
}
15351536
case ExternalKind::Table: {
@@ -1962,10 +1963,14 @@ Name WasmBinaryBuilder::getGlobalName(Index index) {
19621963
}
19631964

19641965
void WasmBinaryBuilder::processFunctions() {
1965-
for (auto& func : functions) {
1966+
for (auto* func : functions) {
19661967
wasm.addFunction(func);
19671968
}
19681969

1970+
for (auto* import : functionImports) {
1971+
wasm.addImport(import);
1972+
}
1973+
19691974
// we should have seen all the functions
19701975
// we assume this later down in fact, when we read wasm.functions[index],
19711976
// as index was validated vs functionTypes.size()
@@ -2002,6 +2007,14 @@ void WasmBinaryBuilder::processFunctions() {
20022007
}
20032008
}
20042009

2010+
for (auto& iter : functionImportCalls) {
2011+
size_t index = iter.first;
2012+
auto& calls = iter.second;
2013+
for (auto* call : calls) {
2014+
call->target = functionImports[index]->name;
2015+
}
2016+
}
2017+
20052018
for (auto& pair : functionTable) {
20062019
auto i = pair.first;
20072020
auto& indexes = pair.second;
@@ -2076,18 +2089,16 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
20762089
continue;
20772090
}
20782091
auto num = getU32LEB();
2079-
uint32_t importedFunctions = 0;
2080-
for (auto& import : wasm.imports) {
2081-
if (import->kind != ExternalKind::Function) continue;
2082-
importedFunctions++;
2083-
}
20842092
for (size_t i = 0; i < num; i++) {
20852093
auto index = getU32LEB();
2086-
if (index < importedFunctions) {
2087-
getInlineString(); // TODO: use this
2088-
} else if (index - importedFunctions < functions.size()) {
2089-
auto name = getInlineString();
2090-
functions[index - importedFunctions]->name = name;
2094+
auto name = getInlineString();
2095+
// note: we silently ignore errors here, as name section errors
2096+
// are not fatal. should we warn?
2097+
auto numFunctionImports = functionImports.size();
2098+
if (index < numFunctionImports) {
2099+
functionImports[index]->name = name;
2100+
} else if (index - numFunctionImports < functions.size()) {
2101+
functions[index - numFunctionImports]->name = name;
20912102
}
20922103
}
20932104
// disallow duplicate names
@@ -2359,19 +2370,20 @@ Expression* WasmBinaryBuilder::visitCall() {
23592370
auto index = getU32LEB();
23602371
FunctionType* type;
23612372
Expression* ret;
2362-
if (index < functionImportIndexes.size()) {
2373+
if (index < functionImports.size()) {
23632374
// this is a call of an imported function
23642375
auto* call = allocator.alloc<CallImport>();
2365-
auto* import = wasm.getImport(functionImportIndexes[index]);
2366-
call->target = import->name;
2376+
auto* import = functionImports[index];
23672377
type = wasm.getFunctionType(import->functionType);
2378+
functionImportCalls[index].push_back(call);
2379+
call->target = import->name; // name section may modify it
23682380
fillCall(call, type);
23692381
call->finalize();
23702382
ret = call;
23712383
} else {
23722384
// this is a call of a defined function
23732385
auto* call = allocator.alloc<Call>();
2374-
auto adjustedIndex = index - functionImportIndexes.size();
2386+
auto adjustedIndex = index - functionImports.size();
23752387
if (adjustedIndex >= functionTypes.size()) {
23762388
throw ParseException("bad call index");
23772389
}

test/dylib.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
(type $1 (func (result i32)))
44
(type $2 (func))
55
(import "env" "memoryBase" (global $import$0 i32))
6-
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
76
(import "env" "memory" (memory $0 256))
87
(import "env" "table" (table 0 anyfunc))
98
(import "env" "tableBase" (global $import$4 i32))
9+
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1010
(global $global$0 (mut i32) (i32.const 0))
1111
(global $global$1 (mut i32) (i32.const 0))
1212
(global $global$2 i32 (i32.const 0))

test/merge/dylib.wasm.combined

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
(type $1$0 (func (result i32)))
88
(type $2$0 (func))
99
(import "env" "memoryBase" (global $import$0 i32))
10-
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1110
(import "env" "memory" (memory $0 256))
1211
(import "env" "table" (table 0 anyfunc))
1312
(import "env" "tableBase" (global $import$4 i32))
13+
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1414
(import "env" "memoryBase" (global $import$0$0 i32))
1515
(import "env" "tableBase" (global $import$4$0 i32))
1616
(global $global$0 (mut i32) (i32.const 0))

test/merge/dylib.wasm.combined.finalized

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
(type $1$0 (func (result i32)))
88
(type $2$0 (func))
99
(import "env" "memoryBase" (global $import$0 i32))
10-
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1110
(import "env" "memory" (memory $0 256))
1211
(import "env" "table" (table 8 anyfunc))
1312
(import "env" "tableBase" (global $import$4 i32))
13+
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1414
(import "env" "memoryBase" (global $import$0$0 i32))
1515
(import "env" "tableBase" (global $import$4$0 i32))
1616
(global $global$0 (mut i32) (i32.const 0))

test/merge/dylib.wasm.combined.finalized.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
(type $1 (func (param i32) (result i32)))
33
(type $2 (func (result i32)))
44
(type $3 (func))
5-
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
65
(import "env" "memory" (memory $0 256))
76
(import "env" "table" (table 8 anyfunc))
7+
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
88
(global $global$0 (mut i32) (i32.const 0))
99
(global $global$1 (mut i32) (i32.const 0))
1010
(global $global$2 i32 (i32.const 0))

test/merge/dylib.wasm.combined.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
(type $2 (func (result i32)))
44
(type $3 (func))
55
(import "env" "memoryBase" (global $import$0 i32))
6-
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
76
(import "env" "memory" (memory $0 256))
87
(import "env" "table" (table 0 anyfunc))
98
(import "env" "tableBase" (global $import$4 i32))
9+
(import "env" "_puts" (func $import$1 (param i32) (result i32)))
1010
(import "env" "memoryBase" (global $import$0$0 i32))
1111
(global $global$0 (mut i32) (i32.const 0))
1212
(global $global$1 (mut i32) (i32.const 0))

test/unit.wast.fromBinary

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
(type $7 (func (param f64) (result f64)))
1010
(type $8 (func (result i64)))
1111
(type $9 (func (param i32 i64)))
12-
(import "env" "_emscripten_asm_const_vi" (func $import$0))
13-
(import "asm2wasm" "f64-to-int" (func $import$1 (param f64) (result i32)))
14-
(import "asm2wasm" "f64-rem" (func $import$2 (param f64 f64) (result f64)))
12+
(import "env" "_emscripten_asm_const_vi" (func $_emscripten_asm_const_vi))
13+
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
14+
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1515
(table 10 anyfunc)
1616
(elem (i32.const 0) $z $big_negative $z $z $w $w $importedDoubles $w $z $cneg)
1717
(memory $0 4096 4096)
@@ -153,7 +153,7 @@
153153
(local $var$0 i32)
154154
(local $var$1 f64)
155155
(set_local $var$0
156-
(call $import$1
156+
(call $f64-to-int
157157
(get_local $var$1)
158158
)
159159
)
@@ -274,7 +274,7 @@
274274
)
275275
)
276276
(func $frem (; 12 ;) (type $4) (result f64)
277-
(call $import$2
277+
(call $f64-rem
278278
(f64.const 5.5)
279279
(f64.const 1.2)
280280
)

0 commit comments

Comments
 (0)