Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Exception handling and test for divide by zero exception. #29

Merged
merged 7 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ci/build_container/build_recipes/wavm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ cp -pR include/ "$THIRDPARTY_BUILD"/lib/llvm-6.0/include/

# WAVM.

COMMIT=69d647ecf6d355921b79a58aa6b1d674a76b4c2b # 2019-01-24
SHA256=b0789161eb96cc0695cda6e0ea0b2db0bd1919795cebf5d139d66a173234d301
COMMIT=275e15fff5928d5cac3b8c5410c5a3b3fa7168ac # Wed Feb 20 16:13:27 2019 -0500
SHA256=1e8306322c7de31ecb6cceb198ff1a3f37e31702402235f242a4daf68b54242f

curl https://github.com/WAVM/WAVM/archive/"$COMMIT".tar.gz -sLo WAVM-"$COMMIT".tar.gz \
&& echo "$SHA256" WAVM-"$COMMIT".tar.gz | sha256sum --check
Expand Down
2 changes: 2 additions & 0 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ uint32_t httpCallHandler(void* raw_context, uint32_t uri_ptr, uint32_t uri_size,

uint32_t getTotalMemoryHandler(void*) { return 0x7FFFFFFF; }
uint32_t _emscripten_get_heap_sizeHandler(void*) { return 0x7FFFFFFF; }
void _llvm_trapHandler(void*) { throw WasmException("emscripten llvm_trap"); }

void setTickPeriodMillisecondsHandler(void* raw_context, uint32_t tick_period_milliseconds) {
WASM_CONTEXT(raw_context)->setTickPeriod(std::chrono::milliseconds(tick_period_milliseconds));
Expand Down Expand Up @@ -938,6 +939,7 @@ Wasm::Wasm(absl::string_view vm, absl::string_view id, absl::string_view initial
#define _REGISTER(_fn) registerCallback(wasm_vm_.get(), #_fn, &_fn##Handler);
_REGISTER(getTotalMemory);
_REGISTER(_emscripten_get_heap_size);
_REGISTER(_llvm_trap);
#undef _REGISTER

// Calls with the "_proxy_" prefix.
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/common/wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class Wasm : public Envoy::Server::Wasm,
uint32_t next_context_id_ = 0;
std::unique_ptr<WasmVm> wasm_vm_;
std::shared_ptr<Context> general_context_; // Context unrelated to any specific stream.
std::function<void(Context*)> tick_;
std::function<void(Common::Wasm::Context*)> tick_;
std::chrono::milliseconds tick_period_;
Event::TimerPtr timer_;

Expand Down
17 changes: 15 additions & 2 deletions source/extensions/common/wasm/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,26 @@ struct SaveRestoreContext {
#define CALL_WITH_CONTEXT(_x, _context) \
do { \
SaveRestoreContext _saved_context(static_cast<Context*>(_context)); \
_x; \
WAVM::Runtime::catchRuntimeExceptions([&] { _x; }, \
[&](WAVM::Runtime::Exception* exception) { \
auto description = describeException(exception); \
destroyException(exception); \
throw WasmException(description); \
}); \
} while (0)

#define CALL_WITH_CONTEXT_RETURN(_x, _context, _type, _member) \
do { \
SaveRestoreContext _saved_context(static_cast<Context*>(_context)); \
return static_cast<_type>(_x[0]._member); \
_type _return_value; \
WAVM::Runtime::catchRuntimeExceptions( \
[&] { _return_value = static_cast<_type>(_x[0]._member); }, \
[&](WAVM::Runtime::Exception* exception) { \
auto description = describeException(exception); \
destroyException(exception); \
throw WasmException(description); \
}); \
return _return_value; \
} while (0)

class RootResolver : public WAVM::Runtime::Resolver, public Logger::Loggable<wasmId> {
Expand Down
2 changes: 1 addition & 1 deletion test/extensions/wasm/test_data/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NO_CONTEXT = true

all: logging.wasm bad_signature.wasm
all: logging.wasm bad_signature.wasm segv.wasm

include ../../../../api/wasm/cpp/Makefile.base
18 changes: 18 additions & 0 deletions test/extensions/wasm/test_data/segv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// NOLINT(namespace-envoy)
#include <string>

#include "proxy_wasm_intrinsics.h"

static int* badptr = nullptr;

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onStart() {
logError("before badptr");
*badptr = 1;
logError("after badptr");
}

extern "C" EMSCRIPTEN_KEEPALIVE void proxy_onLog(uint32_t context_id) {
logError("before div by zero");
logError("divide by zero: " + std::to_string(100 / context_id));
logError("after div by zero");
}
Binary file added test/extensions/wasm/test_data/segv.wasm
Binary file not shown.
Loading