Skip to content

Commit 84eb631

Browse files
authored
Fix global destructor ordering problem (#3592)
On Windows/VS the maps in this code caused a double-delete of a Literal. Given that order of destruction is unspecified, I decided to make them locals, which fixed it. Not sure if there is still a latent ordering bug, but not having these as globals seems an improvement regardless.
1 parent 611e6de commit 84eb631

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/tools/wasm-shell.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ Name ASSERT_UNLINKABLE("assert_unlinkable");
4242
Name INVOKE("invoke");
4343
Name GET("get");
4444

45-
// Modules named in the file
46-
47-
std::map<Name, std::unique_ptr<Module>> modules;
48-
std::map<Name, std::unique_ptr<SExpressionWasmBuilder>> builders;
49-
std::map<Name, std::unique_ptr<ShellExternalInterface>> interfaces;
50-
std::map<Name, std::unique_ptr<ModuleInstance>> instances;
51-
5245
//
5346
// An operation on a module
5447
//
@@ -61,7 +54,8 @@ struct Operation {
6154

6255
Operation(Element& element,
6356
ModuleInstance* instanceInit,
64-
SExpressionWasmBuilder& builder)
57+
SExpressionWasmBuilder& builder,
58+
std::map<Name, std::unique_ptr<ModuleInstance>>& instances)
6559
: instance(instanceInit) {
6660
operation = element[0]->str();
6761
Index i = 1;
@@ -88,13 +82,16 @@ struct Operation {
8882
}
8983
};
9084

91-
static void run_asserts(Name moduleName,
92-
size_t* i,
93-
bool* checked,
94-
Module* wasm,
95-
Element* root,
96-
SExpressionWasmBuilder* builder,
97-
Name entry) {
85+
static void
86+
run_asserts(Name moduleName,
87+
size_t* i,
88+
bool* checked,
89+
Module* wasm,
90+
Element* root,
91+
SExpressionWasmBuilder* builder,
92+
Name entry,
93+
std::map<Name, std::unique_ptr<ShellExternalInterface>>& interfaces,
94+
std::map<Name, std::unique_ptr<ModuleInstance>>& instances) {
9895
ModuleInstance* instance = nullptr;
9996
if (wasm) {
10097
// prefix make_unique to work around visual studio bugs
@@ -195,15 +192,15 @@ static void run_asserts(Name moduleName,
195192
}
196193
} else if (id == INVOKE) {
197194
assert(wasm);
198-
Operation operation(curr, instance, *builder);
195+
Operation operation(curr, instance, *builder, instances);
199196
operation.operate();
200197
} else if (wasm) { // if no wasm, we skipped the module
201198
// an invoke test
202199
bool trapped = false;
203200
WASM_UNUSED(trapped);
204201
Literals result;
205202
try {
206-
Operation operation(*curr[1], instance, *builder);
203+
Operation operation(*curr[1], instance, *builder, instances);
207204
result = operation.operate();
208205
} catch (const TrapException&) {
209206
trapped = true;
@@ -274,6 +271,13 @@ int main(int argc, const char* argv[]) {
274271

275272
bool checked = false;
276273

274+
// Modules named in the file
275+
276+
std::map<Name, std::unique_ptr<Module>> modules;
277+
std::map<Name, std::unique_ptr<SExpressionWasmBuilder>> builders;
278+
std::map<Name, std::unique_ptr<ShellExternalInterface>> interfaces;
279+
std::map<Name, std::unique_ptr<ModuleInstance>> instances;
280+
277281
try {
278282
if (options.debug) {
279283
std::cerr << "parsing text to s-expressions...\n";
@@ -319,9 +323,19 @@ int main(int argc, const char* argv[]) {
319323
modules[moduleName].get(),
320324
&root,
321325
builders[moduleName].get(),
322-
entry);
326+
entry,
327+
interfaces,
328+
instances);
323329
} else {
324-
run_asserts(Name(), &i, &checked, nullptr, &root, nullptr, entry);
330+
run_asserts(Name(),
331+
&i,
332+
&checked,
333+
nullptr,
334+
&root,
335+
nullptr,
336+
entry,
337+
interfaces,
338+
instances);
325339
}
326340
}
327341
} catch (ParseException& p) {

0 commit comments

Comments
 (0)