Skip to content

Commit

Permalink
Fix wavm run without an explicit ABI specified producing an error if …
Browse files Browse the repository at this point in the history
…the module doesn't have an emscripten_metadata section.
  • Loading branch information
AndrewScheidecker committed Oct 16, 2021
1 parent 89881af commit 9ffd3e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
29 changes: 12 additions & 17 deletions Programs/wavm/wavm-run.cpp
Expand Up @@ -510,40 +510,35 @@ struct State
}
}

// Otherwise, check whether it has any WASI or non-WASI imports.
// Otherwise, check whether it has any WASI or Emscripten ABI specific imports.
bool hasWASIImports = false;
bool hasNonWASIImports = false;
bool hasEmscriptenImports = false;
for(const auto& import : irModule.functions.imports)
{
if(stringStartsWith(import.moduleName.c_str(), "wasi_")) { hasWASIImports = true; }
else
else if(import.moduleName == "env" || import.moduleName == "asm2wasm"
|| import.moduleName == "global")
{
hasNonWASIImports = true;
hasEmscriptenImports = true;
}
}

if(hasNonWASIImports)
if(hasEmscriptenImports)
{
// If there are any non-WASI imports, it might be an Emscripten module. However, since
// it didn't have the 'emscripten_metadata' section, WAVM can't use it.
Log::printf(
Log::error,
"Module appears to be an Emscripten module, but does not have an"
" 'emscripten_metadata' section. WAVM only supports Emscripten modules compiled"
" with '-s EMIT_EMSCRIPTEN_METADATA=1'.\n"
"If this is not an Emscripten module, please use '--abi=<ABI>' on the WAVM"
" command line to specify the correct ABI.\n");
return false;
Log::printf(Log::debug, "Module has emscripten imports: using emscripten ABI.\n");
abi = ABI::emscripten;
return true;
}
else if(hasWASIImports)
{
Log::printf(Log::debug, "Module has only WASI imports: using WASI ABI.\n");
Log::printf(Log::debug,
"Module has WASI imports and no emscripten imports: using WASI ABI.\n");
abi = ABI::wasi;
return true;
}
else
{
Log::printf(Log::debug, "Module has no imports: using bare ABI.\n");
Log::printf(Log::debug, "Module has no recognized imports: using bare ABI.\n");
abi = ABI::bare;
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions Test/emscripten/CMakeLists.txt
Expand Up @@ -29,6 +29,12 @@ if(WAVM_ENABLE_RUNTIME)
NAME emscripten_stdout
COMMAND $<TARGET_FILE:wavm> run --abi=emscripten ${CMAKE_CURRENT_LIST_DIR}/stdout.wasm)
set_tests_properties(emscripten_stdout PROPERTIES PASS_REGULAR_EXPRESSION "Hello world!")

# Test emscripten_stdout without an explicit ABI specified on the command-line.
add_test(
NAME emscripten_stdout_detected_abi
COMMAND $<TARGET_FILE:wavm> run ${CMAKE_CURRENT_LIST_DIR}/stdout.wasm)
set_tests_properties(emscripten_stdout_detected_abi PROPERTIES PASS_REGULAR_EXPRESSION "Hello world!")
endif()

add_custom_target(EmscriptenTests SOURCES ${TestSources})
8 changes: 7 additions & 1 deletion Test/wasi/CMakeLists.txt
Expand Up @@ -31,7 +31,7 @@ foreach(TestName ${TestNames})
#endif()
endforeach()

# Run the stdout test as a basic test of "wavm run" and the WASI subsystem.
# Run some of the tests as a basic test of "wavm run" and the WASI subsystem.
if(WAVM_ENABLE_RUNTIME)
add_test(
NAME wasi_args
Expand All @@ -55,6 +55,12 @@ if(WAVM_ENABLE_RUNTIME)
NAME wasi_stdout
COMMAND $<TARGET_FILE:wavm> run --abi=wasi ${CMAKE_CURRENT_LIST_DIR}/stdout.wasm)
set_tests_properties(wasi_stdout PROPERTIES PASS_REGULAR_EXPRESSION "Hello world!")

# Test wasi_stdout without an explicit ABI specified on the command-line.
add_test(
NAME wasi_stdout_detected_abi
COMMAND $<TARGET_FILE:wavm> run ${CMAKE_CURRENT_LIST_DIR}/stdout.wasm)
set_tests_properties(wasi_stdout_detected_abi PROPERTIES PASS_REGULAR_EXPRESSION "Hello world!")
endif()

add_custom_target(WASITests SOURCES ${TestSources})

0 comments on commit 9ffd3e2

Please sign in to comment.