Skip to content
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
1 change: 0 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
# and splitting for module instances
'instance.wast',
'table64.wast', # Requires validations for table size
'table_grow.wast', # Incorrect table linking semantics in interpreter
'tag.wast', # Non-empty tag results allowed by stack switching
'try_table.wast', # Requires try_table interpretation
'local_init.wast', # Requires local validation to respect unnamed blocks
Expand Down
14 changes: 12 additions & 2 deletions src/ir/runtime-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

namespace wasm {

// Traps on out of bounds access
// Runtime representation of a table for interpreting use cases e.g.
// wasm-interpreter.h. Effectively a vector of Literal. Throws TrapException on
// out-of-bounds access.
class RuntimeTable {
public:
RuntimeTable(Table table) : tableMeta_(table) {}
Expand All @@ -41,7 +43,15 @@ class RuntimeTable {

virtual std::size_t size() const = 0;

virtual const Table* tableMeta() const { return &tableMeta_; }
// True iff this is a subtype of the definition `other`. i.e. This table can
// be imported with the definition of `other`
virtual bool isSubType(const Table& other) {
return tableMeta_.addressType == other.addressType &&
Type::isSubType(tableMeta_.type, other.type) &&
size() >= other.initial && tableMeta_.max <= other.max;
}

const Table* tableMeta() const { return &tableMeta_; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "meta" mean here? Could this be getTable() perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it like 'metadata', I didn't want to name this table since this class is also a table. I think the Table struct represents something more like a table definition than a table itself, maybe I can rename this to tableDefinition?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah, maybe getDefinition() then? This is indeed the definition of the runtime table.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I put it in the next PR if that sounds good #8230


protected:
const Table tableMeta_;
Expand Down
5 changes: 0 additions & 5 deletions src/ir/table-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

namespace wasm::TableUtils {

bool isSubType(const Table& a, const Table& b) {
return a.addressType == b.addressType && Type::isSubType(a.type, b.type) &&
a.initial >= b.initial && a.max <= b.max;
}

std::set<Name> getFunctionsNeedingElemDeclare(Module& wasm) {
// Without reference types there are no ref.funcs or elem declare.
if (!wasm.features.hasReferenceTypes()) {
Expand Down
2 changes: 0 additions & 2 deletions src/ir/table-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

namespace wasm::TableUtils {

bool isSubType(const Table& a, const Table& b);

struct FlatTable {
std::vector<Name> names;
bool valid;
Expand Down
7 changes: 7 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3970,4 +3970,11 @@ std::ostream& operator<<(std::ostream& os, wasm::MemoryOrder mo) {
return os;
}

std::ostream& operator<<(std::ostream& o, const Table& table) {
wasm::PrintSExpression printer(o);
// TODO: printTableHeader should take a const Table*
printer.printTableHeader(const_cast<Table*>(&table));
return o;
}

} // namespace wasm
26 changes: 20 additions & 6 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3364,6 +3364,9 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
// Trap if types don't match between all imports and their corresponding
// exports. Imported memories and tables must also be a subtype of their
// export.
// TODO: we should also *resolve* the imports here e.g. by writing to
// allGlobals / allTables etc. First finish migrating all imports here, then
// enable this code to run in all cases e.g. ctor-eval.
void validateImports() {
ModuleUtils::iterImportable(
wasm,
Expand All @@ -3374,6 +3377,8 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {

// These two modules are injected implicitly to tests. We won't find any
// import information for them.
// TODO: remove this workaround once we have a better way of handling
// intrinsic / spec function imports.
if (importable->module == "binaryen-intrinsics" ||
(importable->module == "spectest" &&
importable->base.startsWith("print")) ||
Expand All @@ -3399,12 +3404,21 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
}
}

// TODO: Use the table's runtime information when checking this.
if (auto** table = std::get_if<Table*>(&import)) {
Table* exportedTable =
importedInstance->wasm.getTable(*export_->getInternalName());
if (!TableUtils::isSubType(*exportedTable, **table)) {
trap("Imported table isn't compatible");
if (auto** tableDecl = std::get_if<Table*>(&import)) {
auto* importedTable = importResolver->getTableOrNull(
importable->importNames(), **tableDecl);
if (!importedTable) {
trap((std::stringstream() << "No imported table found for export "
<< importable->importNames())
.str());
}
if (!importedTable->isSubType(**tableDecl)) {
trap(
(std::stringstream()
<< "Imported table " << importedTable->tableMeta()
<< " with size " << importedTable->size()
<< " isn't compatible with import declaration: " << **tableDecl)
.str());
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,7 @@ std::ostream& operator<<(std::ostream& o, wasm::ModuleType pair);
std::ostream& operator<<(std::ostream& o, wasm::ModuleHeapType pair);
std::ostream& operator<<(std::ostream& os, wasm::MemoryOrder mo);
std::ostream& operator<<(std::ostream& o, const wasm::ImportNames& importNames);
std::ostream& operator<<(std::ostream& o, const Table& table);

} // namespace wasm

Expand Down
Loading