Skip to content

Commit 63b499e

Browse files
In AsmConstWalker, don't assume a segment exists (WebAssembly#697)
It's possible to generate an EM_ASM call with empty contents (due to ifdefs, for example), and this gets converted to an empty string. AsmConstWalker assumes that by this point any addresses it is pointing to have a corresponding data section, which is reasonable. However in the case of an empty string, we don't create a data section, but just leave that address uninitialized, i.e. set to 0. In the case of AsmConstWalker, a correct thing to do is to emit the empty string as metadata, which becomes an empty emscripten_asm_v call.
1 parent ac078dc commit 63b499e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/wasm-emscripten.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,16 @@ struct AsmConstWalker : public PostWalker<AsmConstWalker, Visitor<AsmConstWalker
105105
void AsmConstWalker::visitCallImport(CallImport* curr) {
106106
if (curr->target == EMSCRIPTEN_ASM_CONST) {
107107
auto arg = curr->operands[0]->cast<Const>();
108-
Address segmentIndex = segmentsByAddress[arg->value.geti32()];
109-
std::string code = escape(&wasm.memory.segments[segmentIndex].data[0]);
108+
auto address = arg->value.geti32();
109+
auto segmentIterator = segmentsByAddress.find(address);
110+
std::string code;
111+
if (segmentIterator != segmentsByAddress.end()) {
112+
Address segmentIndex = segmentsByAddress[address];
113+
code = escape(&wasm.memory.segments[segmentIndex].data[0]);
114+
} else {
115+
// If we can't find the segment corresponding with the address, then we omitted the segment and the address points to an empty string.
116+
code = escape("");
117+
}
110118
int32_t id;
111119
if (ids.count(code) == 0) {
112120
id = ids.size();

0 commit comments

Comments
 (0)