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
33 changes: 23 additions & 10 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,6 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
Walrus::SegmentMode m_segmentMode;

Walrus::WASMParsingResult m_result;
std::string m_walrusParseError;

PreprocessData m_preprocessData;

Expand Down Expand Up @@ -892,11 +891,6 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
static void* operator new(size_t) = delete;
static void* operator new[](size_t) = delete;

std::string WalrusParseError()
{
return m_walrusParseError;
}

virtual void BeginModule(uint32_t version) override
{
m_result.m_version = version;
Expand All @@ -915,6 +909,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many type declarations.");
return;
}

m_result.m_compositeTypes.reserve(count);
Expand All @@ -935,6 +930,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (paramCount > PARSER_RESOURCE_LIMIT || resultCount > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many function params or results.");
return;
}

Walrus::FunctionType* functionType = new Walrus::FunctionType(paramCount, getRefCountOfTypes(paramTypes, paramCount),
Expand Down Expand Up @@ -1020,6 +1016,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many imports.");
return;
}

m_result.m_imports.reserve(count);
Expand Down Expand Up @@ -1088,6 +1085,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many exports.");
return;
}

m_result.m_exports.reserve(count);
Expand All @@ -1104,6 +1102,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many tables declarations.");
return;
}

m_result.m_tableTypes.reserve(count);
Expand Down Expand Up @@ -1133,6 +1132,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many elem segment declarations.");
return;
}

m_result.m_elements.reserve(count);
Expand Down Expand Up @@ -1196,6 +1196,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many memory declarations.");
return;
}

m_result.m_memoryTypes.reserve(count);
Expand All @@ -1211,6 +1212,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many elem segment declarations.");
return;
}

m_result.m_datas.reserve(count);
Expand Down Expand Up @@ -1251,6 +1253,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many function declarations.");
return;
}

m_result.m_functions.reserve(count);
Expand All @@ -1268,6 +1271,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many global declarations.");
return;
}

m_result.m_globalTypes.reserve(count);
Expand Down Expand Up @@ -1305,6 +1309,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many tags.");
return;
}

m_result.m_tagTypes.reserve(count);
Expand All @@ -1331,6 +1336,11 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {

virtual void OnLocalDeclCount(Index count) override
{
if (count > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many local declarations.");
return;
}

m_currentFunction->m_local.reserve(count);
m_localInfo.reserve(count + m_currentFunctionType->param().size());
}
Expand All @@ -1340,6 +1350,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
uint64_t totalLocalCount = static_cast<uint64_t>(m_localInfo.size()) + count;
if (totalLocalCount > PARSER_RESOURCE_LIMIT) {
m_walrusParseError = std::string("Engine limit reached: too many local declarations.");
return;
}

while (count) {
Expand Down Expand Up @@ -3409,6 +3420,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
// FIXME too many stack usage. we could not support this(yet)
if (m_initialFunctionStackSize > std::numeric_limits<Walrus::ByteCodeStackOffset>::max()) {
m_walrusParseError = std::string("Function stack usage is larger then supported maxium (65535 bytes).");
return;
}

m_lastI32EqzPos = s_noI32Eqz;
Expand Down Expand Up @@ -3901,18 +3913,19 @@ std::pair<Optional<Module*>, std::string> WASMParser::parseBinary(Store* store,
wabt::WASMBinaryReader delegate(store->getTypeStore());

std::string error = ReadWasmBinary(filename, data, len, &delegate, featureFlags);
if (error.length()) {

if (delegate.WalrusParseError().length()) {
if (delegate.parsingResult().m_typesAddedToStore) {
store->getTypeStore().releaseTypes(delegate.parsingResult().m_compositeTypes);
}
return std::make_pair(nullptr, error);
return std::make_pair(nullptr, delegate.WalrusParseError());
}

if (delegate.WalrusParseError().length()) {
if (error.length()) {
if (delegate.parsingResult().m_typesAddedToStore) {
store->getTypeStore().releaseTypes(delegate.parsingResult().m_compositeTypes);
}
return std::make_pair(nullptr, delegate.WalrusParseError());
return std::make_pair(nullptr, error);
}

Module* module = new Module(store, delegate.parsingResult());
Expand Down
6 changes: 6 additions & 0 deletions third_party/wabt/include/wabt/walrus/binary-reader-walrus.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ class WASMBinaryReaderDelegate {
return m_skipValidationUntil;
}

const std::string& WalrusParseError()
{
return m_walrusParseError;
}

protected:
std::string m_walrusParseError;
bool m_shouldContinueToGenerateByteCode;
size_t m_resumeGenerateByteCodeAfterNBlockEnd;
size_t m_skipValidationUntil;
Expand Down
32 changes: 18 additions & 14 deletions third_party/wabt/src/walrus/binary-reader-walrus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
m_labelStack.pop_back();
}

Result CheckParseError() {
return m_externalDelegate->WalrusParseError().empty() ? Result::Ok : Result::Error;
}

Result GetDropCount(Index keep_count, size_t type_stack_limit, Index *out_drop_count) {
assert(m_validator.type_stack_size() >= type_stack_limit);
Index type_stack_count = m_validator.type_stack_size() - type_stack_limit;
Expand Down Expand Up @@ -211,7 +215,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnTypeCount(Index count) override {
m_externalDelegate->OnTypeCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnRecursiveGroup(Index first_type_index, Index type_count) override
{
Expand All @@ -223,7 +227,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
CHECK_RESULT(m_validator.OnFuncType(GetLocation(), param_count, param_types, result_count, result_types, index, supertypes));
m_functionTypes.push_back(SimpleFuncType( { ToInterp(param_count, param_types), ToInterp(result_count, result_types) }));
m_externalDelegate->OnFuncType(index, param_count, param_types, result_count, result_types, supertypes);
return Result::Ok;
return CheckParseError();
}
Result OnStructType(Index index, Index field_count, TypeMut *fields, SupertypesInfo* supertypes) override {
CHECK_RESULT(m_validator.OnStructType(GetLocation(), field_count, fields, supertypes));
Expand All @@ -245,7 +249,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnImportCount(Index count) override {
m_externalDelegate->OnImportCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnImport(Index index, ExternalKind kind, nonstd::string_view module_name, nonstd::string_view field_name) override {
return Result::Ok;
Expand Down Expand Up @@ -287,7 +291,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnFunctionCount(Index count) override {
m_externalDelegate->OnFunctionCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnFunction(Index index, Index sig_index) override {
CHECK_RESULT(m_validator.OnFunction(GetLocation(), Var(sig_index, GetLocation())));
Expand All @@ -304,7 +308,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnTableCount(Index count) override {
m_externalDelegate->OnTableCount(count);
return Result::Ok;
return CheckParseError();
}
Result BeginTable(Index index, Type elem_type, const Limits* elem_limits, TableInitExprStatus init_provided) override {
CHECK_RESULT(m_validator.OnTable(GetLocation(), elem_type, *elem_limits, wabt::TableImportStatus::TableIsNotImported, init_provided));
Expand Down Expand Up @@ -344,7 +348,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnMemoryCount(Index count) override {
m_externalDelegate->OnMemoryCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnMemory(Index index, const Limits *limits, uint32_t page_size) override {
CHECK_RESULT(m_validator.OnMemory(GetLocation(), *limits, page_size));
Expand All @@ -362,7 +366,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnGlobalCount(Index count) override {
m_externalDelegate->OnGlobalCount(count);
return Result::Ok;
return CheckParseError();
}
Result BeginGlobal(Index index, Type type, bool mutable_) override {
CHECK_RESULT(m_validator.BeginGlobal(GetLocation(), type, mutable_));
Expand Down Expand Up @@ -401,7 +405,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnExportCount(Index count) override {
m_externalDelegate->OnExportCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnExport(Index index, ExternalKind kind, Index item_index, nonstd::string_view name) override {
CHECK_RESULT(m_validator.OnExport(GetLocation(), kind, Var(item_index, GetLocation()), name));
Expand Down Expand Up @@ -441,12 +445,12 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnLocalDeclCount(Index count) override {
m_externalDelegate->OnLocalDeclCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnLocalDecl(Index decl_index, Index count, Type type) override {
CHECK_RESULT(m_validator.OnLocalDecl(GetLocation(), count, type));
m_externalDelegate->OnLocalDecl(decl_index, count, type);
return Result::Ok;
return CheckParseError();
}

Result OnStartReadInstructions(Offset start, Offset end) override {
Expand Down Expand Up @@ -1025,7 +1029,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
CHECK_RESULT(m_validator.EndFunctionBody(GetLocation()));
EXECUTE_VALIDATOR(PopLabel());
m_externalDelegate->EndFunctionBody(index);
return Result::Ok;
return CheckParseError();
}
Result EndCodeSection() override {
return Result::Ok;
Expand Down Expand Up @@ -1076,7 +1080,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnElemSegmentCount(Index count) override {
m_externalDelegate->OnElemSegmentCount(count);
return Result::Ok;
return CheckParseError();
}
Result BeginElemSegment(Index index, Index table_index, uint8_t flags) override {
auto mode = ToSegmentMode(flags);
Expand Down Expand Up @@ -1138,7 +1142,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnDataSegmentCount(Index count) override {
m_externalDelegate->OnDataSegmentCount(count);
return Result::Ok;
return CheckParseError();
}
Result BeginDataSegment(Index index, Index memory_index, uint8_t flags) override {
auto mode = ToSegmentMode(flags);
Expand Down Expand Up @@ -1268,7 +1272,7 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnTagCount(Index count) override {
m_externalDelegate->OnTagCount(count);
return Result::Ok;
return CheckParseError();
}
Result OnTagType(Index index, Index sig_index) override {
CHECK_RESULT(m_validator.OnTag(GetLocation(), Var(sig_index, GetLocation())));
Expand Down
Loading