Skip to content

Commit

Permalink
Add Exception handling and test for divide by zero exception. (envoyp…
Browse files Browse the repository at this point in the history
…roxy#29)

* Add Exception handling and test for divide by zero exception.
  • Loading branch information
jplevyak authored Feb 22, 2019
1 parent 1c34c71 commit 233e29e
Show file tree
Hide file tree
Showing 8 changed files with 12,039 additions and 4 deletions.
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

0 comments on commit 233e29e

Please sign in to comment.