From 5281426256427f944d1675b5b4649abb18eed241 Mon Sep 17 00:00:00 2001 From: mathetake Date: Wed, 28 Oct 2020 14:22:47 +0900 Subject: [PATCH 1/3] wavm: emit stack trace Signed-off-by: mathetake --- src/wavm/wavm.cc | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/wavm/wavm.cc b/src/wavm/wavm.cc index b7b6783bd..cefccb012 100644 --- a/src/wavm/wavm.cc +++ b/src/wavm/wavm.cc @@ -42,6 +42,7 @@ #include "WAVM/Runtime/Intrinsics.h" #include "WAVM/Runtime/Linker.h" #include "WAVM/Runtime/Runtime.h" +#include "WAVM/RuntimeABI/RuntimeABI.h" #include "WAVM/WASM/WASM.h" #include "WAVM/WASTParse/WASTParse.h" @@ -90,16 +91,35 @@ namespace { WAVM::Runtime::catchRuntimeExceptions( \ [&] { _x; }, \ [&](WAVM::Runtime::Exception *exception) { \ - auto description = describeException(exception); \ - _wavm->fail(FailState::RuntimeError, \ - "Function: " + std::string(function_name) + " failed: " + description); \ - destroyException(exception); \ + _wavm->fail(FailState::RuntimeError, getFailMessage(function_name, exception)); \ throw std::exception(); \ }); \ } catch (...) { \ } \ } while (0) +std::string getFailMessage(std::string_view function_name, WAVM::Runtime::Exception *exception) { + std::string message = "Function " + std::string(function_name) + " failed:\n" + + "WAVM message: " + WAVM::Runtime::describeExceptionType(exception->type) + + "\nwasm backrace:\n"; + std::vector callstack_descriptions = + WAVM::Runtime::describeCallStack(exception->callStack); + + // Since the first frame is on host and useless for developers, e.g.: `host!envoy+112901013` + // we start with index 1 here + for (size_t i = 1; i < callstack_descriptions.size(); i++) { + std::string description = callstack_descriptions[i]; + if (description.find("wasm!") == std::string::npos) { + // end of WASM's call stack + break; + } + message += " " + std::to_string(i) + ": " + description + "\n"; + } + + WAVM::Runtime::destroyException(exception); + return message; +} + struct WasmUntaggedValue : public WAVM::IR::UntaggedValue { WasmUntaggedValue() = default; WasmUntaggedValue(I32 inI32) { i32 = inI32; } From da4c2efa1e23a7c556acc46b87ccf177b9999c57 Mon Sep 17 00:00:00 2001 From: mathetake Date: Thu, 29 Oct 2020 18:28:51 +0900 Subject: [PATCH 2/3] review: tidy up stacktrace message Signed-off-by: mathetake --- src/wavm/wavm.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wavm/wavm.cc b/src/wavm/wavm.cc index cefccb012..7c743ea6a 100644 --- a/src/wavm/wavm.cc +++ b/src/wavm/wavm.cc @@ -99,9 +99,9 @@ namespace { } while (0) std::string getFailMessage(std::string_view function_name, WAVM::Runtime::Exception *exception) { - std::string message = "Function " + std::string(function_name) + " failed:\n" + - "WAVM message: " + WAVM::Runtime::describeExceptionType(exception->type) + - "\nwasm backrace:\n"; + std::string message = "Function " + std::string(function_name) + + " failed: " + WAVM::Runtime::describeExceptionType(exception->type) + + "\nProxy-Wasm plugin in-VM backtrace:\n"; std::vector callstack_descriptions = WAVM::Runtime::describeCallStack(exception->callStack); From 8100795ddfa1991593c970918a881fad27078f02 Mon Sep 17 00:00:00 2001 From: mathetake Date: Thu, 29 Oct 2020 19:47:51 +0900 Subject: [PATCH 3/3] align trailing spaces Signed-off-by: mathetake --- src/wavm/wavm.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wavm/wavm.cc b/src/wavm/wavm.cc index 7c743ea6a..f068ae31f 100644 --- a/src/wavm/wavm.cc +++ b/src/wavm/wavm.cc @@ -17,10 +17,12 @@ #include "include/proxy-wasm/wasm_vm.h" #include +#include #include #include #include #include +#include #include #include #include @@ -108,12 +110,14 @@ std::string getFailMessage(std::string_view function_name, WAVM::Runtime::Except // Since the first frame is on host and useless for developers, e.g.: `host!envoy+112901013` // we start with index 1 here for (size_t i = 1; i < callstack_descriptions.size(); i++) { + std::ostringstream oss; std::string description = callstack_descriptions[i]; if (description.find("wasm!") == std::string::npos) { // end of WASM's call stack break; } - message += " " + std::to_string(i) + ": " + description + "\n"; + oss << std::setw(3) << std::setfill(' ') << std::to_string(i); + message += oss.str() + ": " + description + "\n"; } WAVM::Runtime::destroyException(exception);