Skip to content

Commit

Permalink
wasm: add a bunch of Emscripten stubs. (envoyproxy#57)
Browse files Browse the repository at this point in the history
While there, fix a few invalid signatures.

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora authored Apr 29, 2019
1 parent 7d6bb53 commit b1fb04c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 19 deletions.
103 changes: 87 additions & 16 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,47 +470,100 @@ uint64_t getMetricHandler(void* raw_context, uint32_t metric_id) {
return context->getMetric(metric_id);
}

uint32_t getTotalMemoryHandler(void*) { return 0x7FFFFFFF; }
uint32_t _emscripten_memcpy_bigHandler(void*, uint32_t, uint32_t, uint32_t) {
throw WasmException("emscripten emscripten_memcpy_big");
}

uint32_t _emscripten_get_heap_sizeHandler(void*) { return 0x7FFFFFFF; }

uint32_t _emscripten_resize_heapHandler(void*, uint32_t) {
throw WasmException("emscripten emscripten_resize_heap");
}

uint32_t abortOnCannotGrowMemoryHandler(void*) {
throw WasmException("emscripten abortOnCannotGrowMemory");
}

void abortHandler(void*, uint32_t) { throw WasmException("emscripten abort"); }

void _abortHandler(void*) { throw WasmException("emscripten abort"); }

void _llvm_trapHandler(void*) { throw WasmException("emscripten llvm_trap"); }

void ___assert_failHandler(void*, uint32_t, uint32_t, uint32_t, uint32_t) {
throw WasmException("emscripten assert_fail");
}

void ___cxa_throwHandler(void*, uint32_t, uint32_t, uint32_t) {
throw WasmException("emscripten cxa_throw");
}

void ___cxa_pure_virtualHandler(void*) { throw WasmException("emscripten cxa_pure_virtual"); }

uint32_t ___cxa_allocate_exceptionHandler(void*, uint32_t) {
throw WasmException("emscripten cxa_allocate_exception");
}

uint32_t ___call_mainHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten call_main");
return 0;
}

uint32_t ___clock_gettimeHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten clock_gettime");
return 0;
}

void ___lockHandler(void*, uint32_t) { throw WasmException("emscripten lock"); }

void ___unlockHandler(void*, uint32_t) { throw WasmException("emscripten unlock"); }

uint32_t ___syscall6Handler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten syscall6");
}

uint32_t ___syscall54Handler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten syscall54");
}

uint32_t ___syscall140Handler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten syscall140");
}

uint32_t ___syscall146Handler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten syscall146");
}

void ___setErrNoHandler(void*, uint32_t) { throw WasmException("emscripten setErrNo"); }

// pthread_equal is required to return 0 by the protobuf libarary.
uint32_t _pthread_equalHandler(void*, uint32_t,
uint32_t) { /* throw WasmException("emscripten pthread_equal"); */
uint32_t _pthread_equalHandler(void*, uint32_t, uint32_t) {
/* throw WasmException("emscripten pthread_equal"); */
return 0;
}

uint32_t _pthread_mutex_destroyHandler(void*, uint32_t) {
throw WasmException("emscripten pthread_mutex_destroy");
return 0;
}
uint32_t _pthread_cond_waitHandler(void*, uint32_t) {

uint32_t _pthread_cond_waitHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten pthread_cond_wait");
return 0;
}

uint32_t _pthread_getspecificHandler(void*, uint32_t) {
throw WasmException("emscripten pthread_getspecific");
return 0;
}
uint32_t _pthread_key_createHandler(void*, uint32_t) {

uint32_t _pthread_key_createHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten pthread_key_create");
return 0;
}
uint32_t _pthread_onceHandler(void*, uint32_t) {

uint32_t _pthread_onceHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten pthread_once");
return 0;
}
uint32_t _pthread_setspecificHandler(void*, uint32_t) {

uint32_t _pthread_setspecificHandler(void*, uint32_t, uint32_t) {
throw WasmException("emscripten pthread_setspecific");
return 0;
}

void setTempRet0Handler(void*, uint32_t) { throw WasmException("emscripten setTempRet0"); }

void setTickPeriodMillisecondsHandler(void* raw_context, uint32_t tick_period_milliseconds) {
Expand Down Expand Up @@ -1179,12 +1232,26 @@ Wasm::Wasm(absl::string_view vm, absl::string_view id, absl::string_view initial
void Wasm::registerCallbacks() {
#define _REGISTER(_fn) wasm_vm_->registerCallback("envoy", #_fn, &_fn##Handler);
if (is_emscripten_) {
_REGISTER(getTotalMemory);
_REGISTER(_emscripten_memcpy_big);
_REGISTER(_emscripten_get_heap_size);
_REGISTER(_emscripten_resize_heap);
_REGISTER(abortOnCannotGrowMemory);
_REGISTER(abort);
_REGISTER(_abort);
_REGISTER(_llvm_trap);
_REGISTER(___assert_fail);
_REGISTER(___cxa_throw);
_REGISTER(___cxa_pure_virtual);
_REGISTER(___cxa_allocate_exception);
_REGISTER(___call_main);
_REGISTER(___clock_gettime);
_REGISTER(___lock);
_REGISTER(___unlock);
_REGISTER(___syscall6);
_REGISTER(___syscall54);
_REGISTER(___syscall140);
_REGISTER(___syscall146);
_REGISTER(___setErrNo);
_REGISTER(_pthread_equal);
_REGISTER(_pthread_mutex_destroy);
_REGISTER(_pthread_cond_wait);
Expand Down Expand Up @@ -1256,6 +1323,10 @@ void Wasm::registerCallbacks() {

void Wasm::establishEnvironment() {
if (is_emscripten_) {
emscripten_table_base_ = wasm_vm_->makeGlobal("env", "__table_base", static_cast<uint32_t>(0));
emscripten_dynamictop_ =
wasm_vm_->makeGlobal("env", "DYNAMICTOP_PTR", static_cast<uint32_t>(128 * 64 * 1024));

wasm_vm_->makeModule("global");
emscripten_NaN_ = wasm_vm_->makeGlobal("global", "NaN", std::nan("0"));
emscripten_Infinity_ =
Expand Down
4 changes: 4 additions & 0 deletions source/extensions/common/wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ class Wasm : public Envoy::Server::Wasm,
uint32_t emscripten_memory_size_ = 0;
uint32_t emscripten_table_size_ = 0;

std::unique_ptr<Global<uint32_t>> emscripten_table_base_;
std::unique_ptr<Global<uint32_t>> emscripten_dynamictop_;
std::unique_ptr<Global<double>> emscripten_NaN_;
std::unique_ptr<Global<double>> emscripten_Infinity_;

Expand Down Expand Up @@ -571,6 +573,8 @@ class WasmVm : public Logger::Loggable<Logger::Id::wasm> {
WasmCallback_mj f) PURE;

// Register typed value exported by the host environment.
virtual std::unique_ptr<Global<uint32_t>>
makeGlobal(absl::string_view moduleName, absl::string_view name, uint32_t initialValue) PURE;
virtual std::unique_ptr<Global<double>>
makeGlobal(absl::string_view moduleName, absl::string_view name, double initialValue) PURE;
};
Expand Down
10 changes: 7 additions & 3 deletions source/extensions/common/wasm/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,14 @@ struct Wavm : public WasmVm {
_REGISTER_CALLBACK(WasmCallback_mj);
#undef _REGISTER_CALLBACK

std::unique_ptr<Global<double>> makeGlobal(absl::string_view moduleName, absl::string_view name,
double initialValue) override {
return makeGlobalWavm(this, moduleName, name, initialValue);
#define _REGISTER_GLOBAL(_type) \
std::unique_ptr<Global<_type>> makeGlobal(absl::string_view moduleName, absl::string_view name, \
_type initialValue) override { \
return makeGlobalWavm(this, moduleName, name, initialValue); \
};
_REGISTER_GLOBAL(uint32_t);
_REGISTER_GLOBAL(double);
#undef _REGISTER_GLOBAL

bool hasInstantiatedModule_ = false;
IR::Module irModule_;
Expand Down

0 comments on commit b1fb04c

Please sign in to comment.