Skip to content
Draft
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
10 changes: 5 additions & 5 deletions src/shell-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ struct ShellExternalInterface : ModuleRunner::ExternalInterface {

std::map<Name, Memory> memories;
std::unordered_map<Name, std::vector<Literal>> tables;
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;

ShellExternalInterface(
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances_ = {}) {
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances_ = {}) {
linkedInstances.swap(linkedInstances_);
}
virtual ~ShellExternalInterface() = default;

ModuleRunner* getImportInstanceOrNull(Importable* import) {
ModuleRunnerBase* getImportInstanceOrNull(Importable* import) {
auto it = linkedInstances.find(import->module);
if (it == linkedInstances.end()) {
return nullptr;
}
return it->second.get();
}

ModuleRunner* getImportInstance(Importable* import) {
ModuleRunnerBase* getImportInstance(Importable* import) {
auto* ret = getImportInstanceOrNull(import);
if (!ret) {
Fatal() << "getImportInstance: unknown import: " << import->module.str
Expand All @@ -119,7 +119,7 @@ struct ShellExternalInterface : ModuleRunner::ExternalInterface {
return ret;
}

void init(Module& wasm, ModuleRunner& instance) override {
void init(Module& wasm, ModuleRunnerBase& instance) override {
ModuleUtils::iterDefinedMemories(wasm, [&](wasm::Memory* memory) {
auto shellMemory = Memory();
shellMemory.resize(memory->initial * wasm::Memory::kPageSize);
Expand Down
20 changes: 10 additions & 10 deletions src/tools/execution-results.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ struct LoggingExternalInterface : public ShellExternalInterface {

// The ModuleRunner and this ExternalInterface end up needing links both ways,
// so we cannot init this in the constructor.
ModuleRunner* instance = nullptr;
ModuleRunnerBase* instance = nullptr;

public:
LoggingExternalInterface(
Loggings& loggings,
Module& wasm,
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances_ = {})
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances_ = {})
: ShellExternalInterface(linkedInstances_), loggings(loggings), wasm(wasm) {
for (auto& exp : wasm.exports) {
if (exp->kind == ExternalKind::Table && exp->name == "table") {
Expand Down Expand Up @@ -294,7 +294,7 @@ struct LoggingExternalInterface : public ShellExternalInterface {
return flow.values;
}

void setModuleRunner(ModuleRunner* instance_) { instance = instance_; }
void setModuleRunner(ModuleRunnerBase* instance_) { instance = instance_; }
};

// gets execution results from a wasm module. this is useful for fuzzing
Expand Down Expand Up @@ -322,7 +322,7 @@ struct ExecutionResults {

// Instantiate the second, if there is one (we instantiate both before
// running anything, so that we match the behavior of fuzz_shell.js).
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;
std::unique_ptr<LoggingExternalInterface> secondInterface;
std::shared_ptr<ModuleRunner> secondInstance;
if (second) {
Expand Down Expand Up @@ -351,16 +351,16 @@ struct ExecutionResults {
}
}

void instantiate(ModuleRunner& instance,
void instantiate(ModuleRunnerBase& instance,
LoggingExternalInterface& interface) {
// This is not an optimization: we want to execute anything, even relaxed
// SIMD instructions.
instance.setRelaxedBehavior(ModuleRunner::RelaxedBehavior::Execute);
instance.setRelaxedBehavior(ModuleRunnerBase::RelaxedBehavior::Execute);
instance.instantiate();
interface.setModuleRunner(&instance);
}

void callExports(Module& wasm, ModuleRunner& instance) {
void callExports(Module& wasm, ModuleRunnerBase& instance) {
// execute all exported methods (that are therefore preserved through
// opts)
for (auto& exp : wasm.exports) {
Expand Down Expand Up @@ -507,11 +507,11 @@ struct ExecutionResults {

bool operator!=(ExecutionResults& other) { return !((*this) == other); }

FunctionResult run(Function* func, Module& wasm, ModuleRunner& instance) {
FunctionResult run(Function* func, Module& wasm, ModuleRunnerBase& instance) {
// Clear the continuation state after each run of an export.
struct CleanUp {
ModuleRunner& instance;
CleanUp(ModuleRunner& instance) : instance(instance) {}
ModuleRunnerBase& instance;
CleanUp(ModuleRunnerBase& instance) : instance(instance) {}
~CleanUp() { instance.clearContinuationStore(); }
} cleanUp(instance);

Expand Down
19 changes: 9 additions & 10 deletions src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ bool isNullableAndMutable(Expression* ref, Index fieldIndex) {
// the output.
#define RECOMMENDATION "\n recommendation: "

class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
class EvallingModuleRunner : public ModuleRunnerBase {
public:
EvallingModuleRunner(
Module& wasm,
ExternalInterface* externalInterface,
std::map<Name, std::shared_ptr<EvallingModuleRunner>> linkedInstances_ = {})
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances_ = {})
: ModuleRunnerBase(wasm, externalInterface, linkedInstances_) {}

Flow visitGlobalGet(GlobalGet* curr) {
Expand All @@ -84,7 +84,7 @@ class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
global->base.toString());
}

return ModuleRunnerBase<EvallingModuleRunner>::visitGlobalGet(curr);
return ModuleRunnerBase::visitGlobalGet(curr);
}

Flow visitTableGet(TableGet* curr) {
Expand All @@ -99,7 +99,7 @@ class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
if (!allowContNew) {
throw FailToEvalException("cont.new disallowed");
}
return ModuleRunnerBase<EvallingModuleRunner>::visitContNew(curr);
return ModuleRunnerBase::visitContNew(curr);
}

// This needs to be duplicated from ModuleRunner, unfortunately.
Expand Down Expand Up @@ -182,7 +182,7 @@ static bool ignoreExternalInput = false;
struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
Module* wasm;
EvallingModuleRunner* instance;
std::map<Name, std::shared_ptr<EvallingModuleRunner>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;

// A representation of the contents of wasm memory as we execute.
std::unordered_map<Name, std::vector<char>> memories;
Expand All @@ -198,8 +198,7 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
bool instanceInitialized = false;

CtorEvalExternalInterface(
std::map<Name, std::shared_ptr<EvallingModuleRunner>> linkedInstances_ =
{}) {
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances_ = {}) {
linkedInstances.swap(linkedInstances_);
}

Expand All @@ -216,9 +215,9 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {
applyGlobalsToModule();
}

void init(Module& wasm_, EvallingModuleRunner& instance_) override {
void init(Module& wasm_, ModuleRunnerBase& instance_) override {
wasm = &wasm_;
instance = &instance_;
instance = static_cast<EvallingModuleRunner*>(&instance_);
for (auto& memory : wasm->memories) {
if (!memory->imported()) {
std::vector<char> data;
Expand Down Expand Up @@ -1354,7 +1353,7 @@ void evalCtors(Module& wasm,
std::unordered_set<std::string> keptExportsSet(keptExports.begin(),
keptExports.end());

std::map<Name, std::shared_ptr<EvallingModuleRunner>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;

// build and link the env module
auto envModule = buildEnvModule(wasm);
Expand Down
4 changes: 2 additions & 2 deletions src/tools/wasm-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ struct Shell {

// Keyed by instance name.
std::map<Name, std::shared_ptr<ShellExternalInterface>> interfaces;
std::map<Name, std::shared_ptr<ModuleRunner>> instances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> instances;
// Used for imports, keyed by instance name.
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;

Name lastInstance;
std::optional<Name> lastModuleDefinition;
Expand Down
37 changes: 20 additions & 17 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2956,8 +2956,7 @@ using GlobalValueSet = std::map<Name, Literals>;
// To call into the interpreter, use callExport.
//

template<typename SubType>
class ModuleRunnerBase : public ExpressionRunner<SubType> {
class ModuleRunnerBase : public ExpressionRunner<ModuleRunnerBase> {
public:
//
// You need to implement one of these to create a concrete interpreter. The
Expand All @@ -2966,9 +2965,9 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
//
struct ExternalInterface {
ExternalInterface(
std::map<Name, std::shared_ptr<SubType>> linkedInstances = {}) {}
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances = {}) {}
virtual ~ExternalInterface() = default;
virtual void init(Module& wasm, SubType& instance) {}
virtual void init(Module& wasm, ModuleRunnerBase& instance) {}
virtual void importGlobals(GlobalValueSet& globals, Module& wasm) = 0;
virtual Literal getImportedFunction(Function* import) = 0;
virtual bool growMemory(Name name, Address oldSize, Address newSize) = 0;
Expand Down Expand Up @@ -3169,7 +3168,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
}
};

SubType* self() { return static_cast<SubType*>(this); }
ModuleRunnerBase* self() { return this; }

// TODO: this duplicates module in ExpressionRunner, and can be removed
Module& wasm;
Expand All @@ -3183,8 +3182,8 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
ModuleRunnerBase(
Module& wasm,
ExternalInterface* externalInterface,
std::map<Name, std::shared_ptr<SubType>> linkedInstances_ = {})
: ExpressionRunner<SubType>(&wasm), wasm(wasm),
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances_ = {})
: ExpressionRunner<ModuleRunnerBase>(&wasm), wasm(wasm),
externalInterface(externalInterface), linkedInstances(linkedInstances_) {
// Set up a single shared CurrContinuations for all these linked instances,
// reusing one if it exists.
Expand Down Expand Up @@ -3302,7 +3301,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {

struct TableInstanceInfo {
// The ModuleRunner instance in which the memory is defined.
SubType* instance;
ModuleRunnerBase* instance;
// The external interface in which the table is defined
ExternalInterface* interface() { return instance->externalInterface; }
// The name the table has in that interface.
Expand Down Expand Up @@ -3358,7 +3357,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {

struct MemoryInstanceInfo {
// The ModuleRunner instance in which the memory is defined.
SubType* instance;
ModuleRunnerBase* instance;
// The external interface in which the memory is defined
ExternalInterface* interface() { return instance->externalInterface; }
// The name the memory has in that interface.
Expand Down Expand Up @@ -3445,13 +3444,13 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
public:
std::vector<Literals> locals;
Function* function;
SubType& parent;
ModuleRunnerBase& parent;

FunctionScope* oldScope;

FunctionScope(Function* function,
const Literals& arguments,
SubType& parent)
ModuleRunnerBase& parent)
: function(function), parent(parent) {
oldScope = parent.scope;
parent.scope = this;
Expand Down Expand Up @@ -3683,7 +3682,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
return ret;
}

Flow visitTableGet(TableGet* curr) {
virtual Flow visitTableGet(TableGet* curr) {
VISIT(index, curr->index)
auto info = getTableInstanceInfo(curr->table);
auto address = index.getSingleValue().getUnsigned();
Expand Down Expand Up @@ -3844,7 +3843,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
return curr->isTee() ? flow : Flow();
}

Flow visitGlobalGet(GlobalGet* curr) {
virtual Flow visitGlobalGet(GlobalGet* curr) {
auto name = curr->name;
return getGlobal(name);
}
Expand Down Expand Up @@ -4534,7 +4533,7 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
multiValues.pop_back();
return ret;
}
Flow visitContNew(ContNew* curr) {
virtual Flow visitContNew(ContNew* curr) {
VISIT(funcFlow, curr->func)
// Create a new continuation for the target function.
auto funcValue = funcFlow.getSingleValue();
Expand Down Expand Up @@ -5053,16 +5052,20 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
return externalInterface->store(&store, addr, toStore, memoryName);
}

virtual Literal makeFuncData(Name name, Type type) {
return ExpressionRunner<ModuleRunnerBase>::makeFuncData(name, type);
}

ExternalInterface* externalInterface;
std::map<Name, std::shared_ptr<SubType>> linkedInstances;
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances;
};

class ModuleRunner : public ModuleRunnerBase<ModuleRunner> {
class ModuleRunner : public ModuleRunnerBase {
public:
ModuleRunner(
Module& wasm,
ExternalInterface* externalInterface,
std::map<Name, std::shared_ptr<ModuleRunner>> linkedInstances = {})
std::map<Name, std::shared_ptr<ModuleRunnerBase>> linkedInstances = {})
: ModuleRunnerBase(wasm, externalInterface, linkedInstances) {}

Literal makeFuncData(Name name, Type type) {
Expand Down
Loading