diff --git a/src/parser/WASMParser.cpp b/src/parser/WASMParser.cpp index 28cafbe6b..71fcdbec9 100644 --- a/src/parser/WASMParser.cpp +++ b/src/parser/WASMParser.cpp @@ -641,7 +641,6 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { Walrus::SegmentMode m_segmentMode; Walrus::WASMParsingResult m_result; - std::string m_walrusParseError; PreprocessData m_preprocessData; @@ -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; @@ -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); @@ -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), @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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()); } @@ -1340,6 +1350,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { uint64_t totalLocalCount = static_cast(m_localInfo.size()) + count; if (totalLocalCount > PARSER_RESOURCE_LIMIT) { m_walrusParseError = std::string("Engine limit reached: too many local declarations."); + return; } while (count) { @@ -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::max()) { m_walrusParseError = std::string("Function stack usage is larger then supported maxium (65535 bytes)."); + return; } m_lastI32EqzPos = s_noI32Eqz; @@ -3901,18 +3913,19 @@ std::pair, 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()); diff --git a/third_party/wabt/include/wabt/walrus/binary-reader-walrus.h b/third_party/wabt/include/wabt/walrus/binary-reader-walrus.h index 716334129..17de02183 100644 --- a/third_party/wabt/include/wabt/walrus/binary-reader-walrus.h +++ b/third_party/wabt/include/wabt/walrus/binary-reader-walrus.h @@ -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; diff --git a/third_party/wabt/src/walrus/binary-reader-walrus.cc b/third_party/wabt/src/walrus/binary-reader-walrus.cc index 5f0a9651e..8d512f597 100644 --- a/third_party/wabt/src/walrus/binary-reader-walrus.cc +++ b/third_party/wabt/src/walrus/binary-reader-walrus.cc @@ -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; @@ -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 { @@ -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)); @@ -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; @@ -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()))); @@ -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)); @@ -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)); @@ -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_)); @@ -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)); @@ -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 { @@ -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; @@ -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); @@ -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); @@ -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())));