Skip to content

Commit c3c1a8f

Browse files
authored
Print trap messages in a consistent manner across Wasm engines. (#300)
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
1 parent d8a7847 commit c3c1a8f

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/wamr/wamr.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
578578
if (trap) {
579579
WasmByteVec error_message;
580580
wasm_trap_message(trap.get(), error_message.get());
581+
std::string message(error_message.get()->data); // NULL-terminated
581582
fail(FailState::RuntimeError,
582-
"Function: " + std::string(function_name) + " failed:\n" +
583-
std::string(error_message.get()->data, error_message.get()->size));
583+
"Function: " + std::string(function_name) + " failed: " + message);
584584
return;
585585
}
586586
if (log) {
@@ -628,9 +628,9 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
628628
if (trap) {
629629
WasmByteVec error_message;
630630
wasm_trap_message(trap.get(), error_message.get());
631+
std::string message(error_message.get()->data); // NULL-terminated
631632
fail(FailState::RuntimeError,
632-
"Function: " + std::string(function_name) + " failed:\n" +
633-
std::string(error_message.get()->data, error_message.get()->size));
633+
"Function: " + std::string(function_name) + " failed: " + message);
634634
return R{};
635635
}
636636
R ret = convertValueTypeToArg<R>(results.data[0]);

src/wasmedge/wasmedge.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
540540
WasmEdge_Result res =
541541
WasmEdge_ExecutorInvoke(executor_.get(), func_cxt, params, sizeof...(Args), nullptr, 0);
542542
if (!WasmEdge_ResultOK(res)) {
543-
fail(FailState::RuntimeError, "Function: " + std::string(function_name) + " failed:\n" +
544-
WasmEdge_ResultGetMessage(res));
543+
fail(FailState::RuntimeError, "Function: " + std::string(function_name) +
544+
" failed: " + WasmEdge_ResultGetMessage(res));
545545
return;
546546
}
547547
if (log) {
@@ -594,8 +594,8 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name,
594594
WasmEdge_Result res =
595595
WasmEdge_ExecutorInvoke(executor_.get(), func_cxt, params, sizeof...(Args), results, 1);
596596
if (!WasmEdge_ResultOK(res)) {
597-
fail(FailState::RuntimeError, "Function: " + std::string(function_name) + " failed:\n" +
598-
WasmEdge_ResultGetMessage(res));
597+
fail(FailState::RuntimeError, "Function: " + std::string(function_name) +
598+
" failed: " + WasmEdge_ResultGetMessage(res));
599599
return R{};
600600
}
601601
R ret = convValTypeToArg<R>(results[0]);

src/wasmtime/wasmtime.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
616616
if (trap) {
617617
WasmByteVec error_message;
618618
wasm_trap_message(trap.get(), error_message.get());
619+
std::string message(error_message.get()->data); // NULL-terminated
619620
fail(FailState::RuntimeError,
620-
"Function: " + std::string(function_name) + " failed:\n" +
621-
std::string(error_message.get()->data, error_message.get()->size));
621+
"Function: " + std::string(function_name) + " failed: " + message);
622622
return;
623623
}
624624
if (log) {
@@ -678,9 +678,9 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
678678
if (trap) {
679679
WasmByteVec error_message;
680680
wasm_trap_message(trap.get(), error_message.get());
681+
std::string message(error_message.get()->data); // NULL-terminated
681682
fail(FailState::RuntimeError,
682-
"Function: " + std::string(function_name) + " failed:\n" +
683-
std::string(error_message.get()->data, error_message.get()->size));
683+
"Function: " + std::string(function_name) + " failed: " + message);
684684
return R{};
685685
}
686686
R ret = convertValueTypeToArg<R>(results.data[0]);

test/runtime_test.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ TEST_P(TestVm, TerminateExecution) {
108108
// Check integration logs.
109109
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
110110
EXPECT_TRUE(host->isErrorLogged("Function: infinite_loop failed"));
111-
if (engine_ == "v8") {
112-
EXPECT_TRUE(host->isErrorLogged("Uncaught Error: termination_exception"));
113-
}
111+
EXPECT_TRUE(host->isErrorLogged("termination_exception"));
114112
}
115113

116114
TEST_P(TestVm, WasmMemoryLimit) {
@@ -135,8 +133,14 @@ TEST_P(TestVm, WasmMemoryLimit) {
135133
// Check integration logs.
136134
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
137135
EXPECT_TRUE(host->isErrorLogged("Function: infinite_memory failed"));
136+
// Trap message
137+
if (engine_ == "wavm") {
138+
EXPECT_TRUE(host->isErrorLogged("wavm.reachedUnreachable"));
139+
} else {
140+
EXPECT_TRUE(host->isErrorLogged("unreachable"));
141+
}
142+
// Backtrace
138143
if (engine_ == "v8") {
139-
EXPECT_TRUE(host->isErrorLogged("Uncaught RuntimeError: unreachable"));
140144
EXPECT_TRUE(host->isErrorLogged("Proxy-Wasm plugin in-VM backtrace:"));
141145
EXPECT_TRUE(host->isErrorLogged(" - rust_oom"));
142146
EXPECT_TRUE(host->isErrorLogged(" - alloc::alloc::handle_alloc_error"));
@@ -158,8 +162,14 @@ TEST_P(TestVm, Trap) {
158162
// Check integration logs.
159163
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
160164
EXPECT_TRUE(host->isErrorLogged("Function: trigger failed"));
165+
// Trap message
166+
if (engine_ == "wavm") {
167+
EXPECT_TRUE(host->isErrorLogged("wavm.reachedUnreachable"));
168+
} else {
169+
EXPECT_TRUE(host->isErrorLogged("unreachable"));
170+
}
171+
// Backtrace
161172
if (engine_ == "v8") {
162-
EXPECT_TRUE(host->isErrorLogged("Uncaught RuntimeError: unreachable"));
163173
EXPECT_TRUE(host->isErrorLogged("Proxy-Wasm plugin in-VM backtrace:"));
164174
EXPECT_TRUE(host->isErrorLogged(" - std::panicking::begin_panic"));
165175
EXPECT_TRUE(host->isErrorLogged(" - trigger"));
@@ -181,8 +191,14 @@ TEST_P(TestVm, Trap2) {
181191
// Check integration logs.
182192
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
183193
EXPECT_TRUE(host->isErrorLogged("Function: trigger2 failed"));
194+
// Trap message
195+
if (engine_ == "wavm") {
196+
EXPECT_TRUE(host->isErrorLogged("wavm.reachedUnreachable"));
197+
} else {
198+
EXPECT_TRUE(host->isErrorLogged("unreachable"));
199+
}
200+
// Backtrace
184201
if (engine_ == "v8") {
185-
EXPECT_TRUE(host->isErrorLogged("Uncaught RuntimeError: unreachable"));
186202
EXPECT_TRUE(host->isErrorLogged("Proxy-Wasm plugin in-VM backtrace:"));
187203
EXPECT_TRUE(host->isErrorLogged(" - std::panicking::begin_panic"));
188204
EXPECT_TRUE(host->isErrorLogged(" - trigger2"));

0 commit comments

Comments
 (0)