diff options
Diffstat (limited to 'src/tools/wasm-ctor-eval.cpp')
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 1d08f5baf..aea0646a3 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -33,6 +33,8 @@ #include "wasm-builder.h" #include "ast/memory-utils.h" #include "ast/global-utils.h" +#include "ast/import-utils.h" +#include "ast/literal-utils.h" using namespace wasm; @@ -78,11 +80,6 @@ public: } throw FailToEvalException(std::string("tried to access a dangerous (import-initialized) global: ") + name.str + extra); } - if (sealed) { - if (globals.find(name) == globals.end()) { - throw FailToEvalException(std::string("tried to access missing global: ") + name.str); - } - } return globals[name]; } @@ -114,6 +111,13 @@ public: } }; +enum { + // put the stack in some ridiculously high location + STACK_START = 0x40000000, + // use a ridiculously large stack size + STACK_SIZE = 32 * 1024 * 1024 +}; + class EvallingModuleInstance : public ModuleInstanceBase<EvallingGlobalManager, EvallingModuleInstance> { public: EvallingModuleInstance(Module& wasm, ExternalInterface* externalInterface) : ModuleInstanceBase(wasm, externalInterface) { @@ -138,13 +142,6 @@ public: } } - enum { - // put the stack in some ridiculously high location - STACK_START = 0x40000000, - // use a ridiculously large stack size - STACK_SIZE = 32 * 1024 * 1024 - }; - std::vector<char> stack; // create C stack space for us to use. We do *NOT* care about their contents, @@ -153,13 +150,6 @@ public: void setupEnvironment() { // prepare scratch memory stack.resize(STACK_SIZE); - // fill usable values for stack imports - if (auto* stackTop = GlobalUtils::getGlobalInitializedToImport(wasm, "env", "STACKTOP")) { - globals[stackTop->name] = Literal(int32_t(STACK_START)); - } - if (auto* stackMax = GlobalUtils::getGlobalInitializedToImport(wasm, "env", "STACK_MAX")) { - globals[stackMax->name] = Literal(int32_t(STACK_START)); - } // tell the module to accept writes up to the stack end auto total = STACK_START + STACK_SIZE; memorySize = total / Memory::kPageSize; @@ -181,6 +171,32 @@ struct CtorEvalExternalInterface : EvallingModuleInstance::ExternalInterface { } void importGlobals(EvallingGlobalManager& globals, Module& wasm_) override { + // fill usable values for stack imports, and globals initialized to them + if (auto* stackTop = ImportUtils::getImport(wasm_, "env", "STACKTOP")) { + globals[stackTop->name] = Literal(int32_t(STACK_START)); + if (auto* stackTop = GlobalUtils::getGlobalInitializedToImport(wasm_, "env", "STACKTOP")) { + globals[stackTop->name] = Literal(int32_t(STACK_START)); + } + } + if (auto* stackMax = ImportUtils::getImport(wasm_, "env", "STACK_MAX")) { + globals[stackMax->name] = Literal(int32_t(STACK_START)); + if (auto* stackMax = GlobalUtils::getGlobalInitializedToImport(wasm_, "env", "STACK_MAX")) { + globals[stackMax->name] = Literal(int32_t(STACK_START)); + } + } + // fill in fake values for everything else, which is dangerous to use + for (auto& global : wasm_.globals) { + if (globals.find(global->name) == globals.end()) { + globals[global->name] = LiteralUtils::makeLiteralZero(global->type); + } + } + for (auto& import : wasm_.imports) { + if (import->kind == ExternalKind::Global) { + if (globals.find(import->name) == globals.end()) { + globals[import->name] = LiteralUtils::makeLiteralZero(import->globalType); + } + } + } } Literal callImport(Import *import, LiteralList& arguments) override { @@ -248,9 +264,9 @@ private: template <typename T> T* getMemory(Address address) { // if memory is on the stack, use the stack - if (address >= instance->STACK_START) { - Address relative = address - instance->STACK_START; - if (relative + sizeof(T) > instance->STACK_SIZE) { + if (address >= STACK_START) { + Address relative = address - STACK_START; + if (relative + sizeof(T) > STACK_SIZE) { throw FailToEvalException("stack usage too high"); } // in range, all is good, use the stack |