From b60736ec1405bb0a8dd40989f67ef4c93da068ab Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Tue, 16 Feb 2021 21:13:02 +0100 Subject: Vendor import of llvm-project main 8e464dd76bef, the last commit before the upstream release/12.x branch was created. --- llvm/lib/Object/WasmObjectFile.cpp | 350 +++++++++++++++++++++++++------------ 1 file changed, 238 insertions(+), 112 deletions(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index bb2e81d64047..40f468881edd 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -187,19 +187,19 @@ static Error readInitExpr(wasm::WasmInitExpr &Expr, case wasm::WASM_OPCODE_REF_NULL: { wasm::ValType Ty = static_cast(readULEB128(Ctx)); if (Ty != wasm::ValType::EXTERNREF) { - return make_error("Invalid type for ref.null", + return make_error("invalid type for ref.null", object_error::parse_failed); } break; } default: - return make_error("Invalid opcode in init_expr", + return make_error("invalid opcode in init_expr", object_error::parse_failed); } uint8_t EndOpcode = readOpcode(Ctx); if (EndOpcode != wasm::WASM_OPCODE_END) { - return make_error("Invalid init_expr", + return make_error("invalid init_expr", object_error::parse_failed); } return Error::success(); @@ -214,11 +214,11 @@ static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { return Result; } -static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) { - wasm::WasmTable Table; - Table.ElemType = readUint8(Ctx); - Table.Limits = readLimits(Ctx); - return Table; +static wasm::WasmTableType readTableType(WasmObjectFile::ReadContext &Ctx) { + wasm::WasmTableType TableType; + TableType.ElemType = readUint8(Ctx); + TableType.Limits = readLimits(Ctx); + return TableType; } static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, @@ -228,10 +228,10 @@ static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n"); uint32_t Size = readVaruint32(Ctx); if (Size == 0) - return make_error("Zero length section", + return make_error("zero length section", object_error::parse_failed); if (Ctx.Ptr + Size > Ctx.End) - return make_error("Section too large", + return make_error("section too large", object_error::parse_failed); if (Section.Type == wasm::WASM_SEC_CUSTOM) { WasmObjectFile::ReadContext SectionCtx; @@ -247,7 +247,7 @@ static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, } if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) { - return make_error("Out of order section type: " + + return make_error("out of order section type: " + llvm::to_string(Section.Type), object_error::parse_failed); } @@ -262,8 +262,8 @@ WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) ErrorAsOutParameter ErrAsOutParam(&Err); Header.Magic = getData().substr(0, 4); if (Header.Magic != StringRef("\0asm", 4)) { - Err = - make_error("Bad magic number", object_error::parse_failed); + Err = make_error("invalid magic number", + object_error::parse_failed); return; } @@ -273,14 +273,15 @@ WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) Ctx.End = Ctx.Start + getData().size(); if (Ctx.Ptr + 4 > Ctx.End) { - Err = make_error("Missing version number", + Err = make_error("missing version number", object_error::parse_failed); return; } Header.Version = readUint32(Ctx); if (Header.Version != wasm::WasmVersion) { - Err = make_error("Bad version number", + Err = make_error("invalid version number: " + + Twine(Header.Version), object_error::parse_failed); return; } @@ -333,7 +334,7 @@ Error WasmObjectFile::parseSection(WasmSection &Sec) { return parseDataCountSection(Ctx); default: return make_error( - "Invalid section type: " + Twine(Sec.Type), object_error::parse_failed); + "invalid section type: " + Twine(Sec.Type), object_error::parse_failed); } } @@ -355,9 +356,11 @@ Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { } Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { - llvm::DenseSet Seen; + llvm::DenseSet SeenFunctions; + llvm::DenseSet SeenGlobals; + llvm::DenseSet SeenSegments; if (FunctionTypes.size() && !SeenCodeSection) { - return make_error("Names must come after code section", + return make_error("names must come after code section", object_error::parse_failed); } @@ -366,20 +369,42 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { uint32_t Size = readVaruint32(Ctx); const uint8_t *SubSectionEnd = Ctx.Ptr + Size; switch (Type) { - case wasm::WASM_NAMES_FUNCTION: { + case wasm::WASM_NAMES_FUNCTION: + case wasm::WASM_NAMES_GLOBAL: + case wasm::WASM_NAMES_DATA_SEGMENT: { uint32_t Count = readVaruint32(Ctx); while (Count--) { uint32_t Index = readVaruint32(Ctx); - if (!Seen.insert(Index).second) - return make_error("Function named more than once", - object_error::parse_failed); StringRef Name = readString(Ctx); - if (!isValidFunctionIndex(Index) || Name.empty()) - return make_error("Invalid name entry", - object_error::parse_failed); - DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); - if (isDefinedFunctionIndex(Index)) - getDefinedFunction(Index).DebugName = Name; + wasm::NameType nameType = wasm::NameType::FUNCTION; + if (Type == wasm::WASM_NAMES_FUNCTION) { + if (!SeenFunctions.insert(Index).second) + return make_error( + "function named more than once", object_error::parse_failed); + if (!isValidFunctionIndex(Index) || Name.empty()) + return make_error("invalid name entry", + object_error::parse_failed); + + if (isDefinedFunctionIndex(Index)) + getDefinedFunction(Index).DebugName = Name; + } else if (Type == wasm::WASM_NAMES_GLOBAL) { + nameType = wasm::NameType::GLOBAL; + if (!SeenGlobals.insert(Index).second) + return make_error("global named more than once", + object_error::parse_failed); + if (!isValidGlobalIndex(Index) || Name.empty()) + return make_error("invalid name entry", + object_error::parse_failed); + } else { + nameType = wasm::NameType::DATA_SEGMENT; + if (!SeenSegments.insert(Index).second) + return make_error( + "segment named more than once", object_error::parse_failed); + if (Index > DataSegments.size()) + return make_error("invalid named data segment", + object_error::parse_failed); + } + DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name}); } break; } @@ -391,11 +416,11 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { } if (Ctx.Ptr != SubSectionEnd) return make_error( - "Name sub-section ended prematurely", object_error::parse_failed); + "name sub-section ended prematurely", object_error::parse_failed); } if (Ctx.Ptr != Ctx.End) - return make_error("Name section ended prematurely", + return make_error("name section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -404,14 +429,14 @@ Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { HasLinkingSection = true; if (FunctionTypes.size() && !SeenCodeSection) { return make_error( - "Linking data must come after code section", + "linking data must come after code section", object_error::parse_failed); } LinkingData.Version = readVaruint32(Ctx); if (LinkingData.Version != wasm::WasmMetadataVersion) { return make_error( - "Unexpected metadata version: " + Twine(LinkingData.Version) + + "unexpected metadata version: " + Twine(LinkingData.Version) + " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", object_error::parse_failed); } @@ -432,7 +457,7 @@ Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { case wasm::WASM_SEGMENT_INFO: { uint32_t Count = readVaruint32(Ctx); if (Count > DataSegments.size()) - return make_error("Too many segment names", + return make_error("too many segment names", object_error::parse_failed); for (uint32_t I = 0; I < Count; I++) { DataSegments[I].Data.Name = readString(Ctx); @@ -449,7 +474,7 @@ Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { Init.Priority = readVaruint32(Ctx); Init.Symbol = readVaruint32(Ctx); if (!isValidFunctionSymbol(Init.Symbol)) - return make_error("Invalid function symbol: " + + return make_error("invalid function symbol: " + Twine(Init.Symbol), object_error::parse_failed); LinkingData.InitFunctions.emplace_back(Init); @@ -466,10 +491,10 @@ Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { } if (Ctx.Ptr != Ctx.End) return make_error( - "Linking sub-section ended prematurely", object_error::parse_failed); + "linking sub-section ended prematurely", object_error::parse_failed); } if (Ctx.Ptr != OrigEnd) - return make_error("Linking section ended prematurely", + return make_error("linking section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -483,9 +508,11 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { std::vector ImportedGlobals; std::vector ImportedFunctions; std::vector ImportedEvents; + std::vector ImportedTables; ImportedGlobals.reserve(Imports.size()); ImportedFunctions.reserve(Imports.size()); ImportedEvents.reserve(Imports.size()); + ImportedTables.reserve(Imports.size()); for (auto &I : Imports) { if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) ImportedFunctions.emplace_back(&I); @@ -493,12 +520,15 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { ImportedGlobals.emplace_back(&I); else if (I.Kind == wasm::WASM_EXTERNAL_EVENT) ImportedEvents.emplace_back(&I); + else if (I.Kind == wasm::WASM_EXTERNAL_TABLE) + ImportedTables.emplace_back(&I); } while (Count--) { wasm::WasmSymbolInfo Info; const wasm::WasmSignature *Signature = nullptr; const wasm::WasmGlobalType *GlobalType = nullptr; + const wasm::WasmTableType *TableType = nullptr; const wasm::WasmEventType *EventType = nullptr; Info.Kind = readUint8(Ctx); @@ -560,7 +590,38 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { Info.Name = Import.Field; } GlobalType = &Import.Global; - Info.ImportName = Import.Field; + if (!Import.Module.empty()) { + Info.ImportModule = Import.Module; + } + } + break; + + case wasm::WASM_SYMBOL_TYPE_TABLE: + Info.ElementIndex = readVaruint32(Ctx); + if (!isValidTableIndex(Info.ElementIndex) || + IsDefined != isDefinedTableIndex(Info.ElementIndex)) + return make_error("invalid table symbol index", + object_error::parse_failed); + if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == + wasm::WASM_SYMBOL_BINDING_WEAK) + return make_error("undefined weak table symbol", + object_error::parse_failed); + if (IsDefined) { + Info.Name = readString(Ctx); + unsigned TableIndex = Info.ElementIndex - NumImportedTables; + wasm::WasmTable &Table = Tables[TableIndex]; + TableType = &Table.Type; + if (Table.SymbolName.empty()) + Table.SymbolName = Info.Name; + } else { + wasm::WasmImport &Import = *ImportedTables[Info.ElementIndex]; + if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) { + Info.Name = readString(Ctx); + Info.ImportName = Import.Field; + } else { + Info.Name = Import.Field; + } + TableType = &Import.Table; if (!Import.Module.empty()) { Info.ImportModule = Import.Module; } @@ -587,7 +648,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != wasm::WASM_SYMBOL_BINDING_LOCAL) return make_error( - "Section symbols must have local binding", + "section symbols must have local binding", object_error::parse_failed); Info.ElementIndex = readVaruint32(Ctx); // Use somewhat unique section name as symbol name. @@ -633,19 +694,20 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { } default: - return make_error("Invalid symbol type", + return make_error("invalid symbol type: " + + Twine(unsigned(Info.Kind)), object_error::parse_failed); } if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != wasm::WASM_SYMBOL_BINDING_LOCAL && !SymbolNames.insert(Info.Name).second) - return make_error("Duplicate symbol name " + + return make_error("duplicate symbol name " + Twine(Info.Name), object_error::parse_failed); LinkingData.SymbolTable.emplace_back(Info); - Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType, - Signature); + Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, TableType, + EventType, Signature); LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); } @@ -658,13 +720,13 @@ Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { StringRef Name = readString(Ctx); if (Name.empty() || !ComdatSet.insert(Name).second) - return make_error("Bad/duplicate COMDAT name " + + return make_error("bad/duplicate COMDAT name " + Twine(Name), object_error::parse_failed); LinkingData.Comdats.emplace_back(Name); uint32_t Flags = readVaruint32(Ctx); if (Flags != 0) - return make_error("Unsupported COMDAT flags", + return make_error("unsupported COMDAT flags", object_error::parse_failed); uint32_t EntryCount = readVaruint32(Ctx); @@ -673,14 +735,14 @@ Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { unsigned Index = readVaruint32(Ctx); switch (Kind) { default: - return make_error("Invalid COMDAT entry type", + return make_error("invalid COMDAT entry type", object_error::parse_failed); case wasm::WASM_COMDAT_DATA: if (Index >= DataSegments.size()) return make_error( "COMDAT data index out of range", object_error::parse_failed); if (DataSegments[Index].Data.Comdat != UINT32_MAX) - return make_error("Data segment in two COMDATs", + return make_error("data segment in two COMDATs", object_error::parse_failed); DataSegments[Index].Data.Comdat = ComdatIndex; break; @@ -689,10 +751,19 @@ Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { return make_error( "COMDAT function index out of range", object_error::parse_failed); if (getDefinedFunction(Index).Comdat != UINT32_MAX) - return make_error("Function in two COMDATs", + return make_error("function in two COMDATs", object_error::parse_failed); getDefinedFunction(Index).Comdat = ComdatIndex; break; + case wasm::WASM_COMDAT_SECTION: + if (Index >= Sections.size()) + return make_error( + "COMDAT section index out of range", object_error::parse_failed); + if (Sections[Index].Type != wasm::WASM_SEC_CUSTOM) + return make_error( + "non-custom section in a COMDAT", object_error::parse_failed); + Sections[Index].Comdat = ComdatIndex; + break; } } } @@ -706,7 +777,7 @@ Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { StringRef FieldName = readString(Ctx); if (!FieldsSeen.insert(FieldName).second) return make_error( - "Producers section does not have unique fields", + "producers section does not have unique fields", object_error::parse_failed); std::vector> *ProducerVec = nullptr; if (FieldName == "language") { @@ -717,7 +788,7 @@ Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { ProducerVec = &ProducerInfo.SDKs; } else { return make_error( - "Producers section field is not named one of language, processed-by, " + "producers section field is not named one of language, processed-by, " "or sdk", object_error::parse_failed); } @@ -728,14 +799,14 @@ Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { StringRef Version = readString(Ctx); if (!ProducersSeen.insert(Name).second) { return make_error( - "Producers section contains repeated producer", + "producers section contains repeated producer", object_error::parse_failed); } ProducerVec->emplace_back(std::string(Name), std::string(Version)); } } if (Ctx.Ptr != Ctx.End) - return make_error("Producers section ended prematurely", + return make_error("producers section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -752,20 +823,20 @@ Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) { case wasm::WASM_FEATURE_PREFIX_DISALLOWED: break; default: - return make_error("Unknown feature policy prefix", + return make_error("unknown feature policy prefix", object_error::parse_failed); } Feature.Name = std::string(readString(Ctx)); if (!FeaturesSeen.insert(Feature.Name).second) return make_error( - "Target features section contains repeated feature \"" + + "target features section contains repeated feature \"" + Feature.Name + "\"", object_error::parse_failed); TargetFeatures.push_back(Feature); } if (Ctx.Ptr != Ctx.End) return make_error( - "Target features section ended prematurely", + "target features section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -773,7 +844,7 @@ Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) { Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { uint32_t SectionIndex = readVaruint32(Ctx); if (SectionIndex >= Sections.size()) - return make_error("Invalid section index", + return make_error("invalid section index", object_error::parse_failed); WasmSection &Section = Sections[SectionIndex]; uint32_t RelocCount = readVaruint32(Ctx); @@ -781,25 +852,33 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { uint32_t PreviousOffset = 0; while (RelocCount--) { wasm::WasmRelocation Reloc = {}; - Reloc.Type = readVaruint32(Ctx); + uint32_t type = readVaruint32(Ctx); + Reloc.Type = type; Reloc.Offset = readVaruint32(Ctx); if (Reloc.Offset < PreviousOffset) - return make_error("Relocations not in offset order", + return make_error("relocations not in offset order", object_error::parse_failed); PreviousOffset = Reloc.Offset; Reloc.Index = readVaruint32(Ctx); - switch (Reloc.Type) { + switch (type) { case wasm::R_WASM_FUNCTION_INDEX_LEB: case wasm::R_WASM_TABLE_INDEX_SLEB: + case wasm::R_WASM_TABLE_INDEX_SLEB64: case wasm::R_WASM_TABLE_INDEX_I32: + case wasm::R_WASM_TABLE_INDEX_I64: case wasm::R_WASM_TABLE_INDEX_REL_SLEB: if (!isValidFunctionSymbol(Reloc.Index)) - return make_error("Bad relocation function index", + return make_error( + "invalid relocation function index", object_error::parse_failed); + break; + case wasm::R_WASM_TABLE_NUMBER_LEB: + if (!isValidTableSymbol(Reloc.Index)) + return make_error("invalid relocation table index", object_error::parse_failed); break; case wasm::R_WASM_TYPE_INDEX_LEB: if (Reloc.Index >= Signatures.size()) - return make_error("Bad relocation type index", + return make_error("invalid relocation type index", object_error::parse_failed); break; case wasm::R_WASM_GLOBAL_INDEX_LEB: @@ -808,25 +887,26 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { if (!isValidGlobalSymbol(Reloc.Index) && !isValidDataSymbol(Reloc.Index) && !isValidFunctionSymbol(Reloc.Index)) - return make_error("Bad relocation global index", + return make_error("invalid relocation global index", object_error::parse_failed); break; case wasm::R_WASM_GLOBAL_INDEX_I32: if (!isValidGlobalSymbol(Reloc.Index)) - return make_error("Bad relocation global index", + return make_error("invalid relocation global index", object_error::parse_failed); break; case wasm::R_WASM_EVENT_INDEX_LEB: if (!isValidEventSymbol(Reloc.Index)) - return make_error("Bad relocation event index", + return make_error("invalid relocation event index", object_error::parse_failed); break; case wasm::R_WASM_MEMORY_ADDR_LEB: case wasm::R_WASM_MEMORY_ADDR_SLEB: case wasm::R_WASM_MEMORY_ADDR_I32: case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: + case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: if (!isValidDataSymbol(Reloc.Index)) - return make_error("Bad relocation data index", + return make_error("invalid relocation data index", object_error::parse_failed); Reloc.Addend = readVarint32(Ctx); break; @@ -835,25 +915,31 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { case wasm::R_WASM_MEMORY_ADDR_I64: case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: if (!isValidDataSymbol(Reloc.Index)) - return make_error("Bad relocation data index", + return make_error("invalid relocation data index", object_error::parse_failed); Reloc.Addend = readVarint64(Ctx); break; case wasm::R_WASM_FUNCTION_OFFSET_I32: if (!isValidFunctionSymbol(Reloc.Index)) - return make_error("Bad relocation function index", - object_error::parse_failed); + return make_error( + "invalid relocation function index", object_error::parse_failed); Reloc.Addend = readVarint32(Ctx); break; + case wasm::R_WASM_FUNCTION_OFFSET_I64: + if (!isValidFunctionSymbol(Reloc.Index)) + return make_error( + "invalid relocation function index", object_error::parse_failed); + Reloc.Addend = readVarint64(Ctx); + break; case wasm::R_WASM_SECTION_OFFSET_I32: if (!isValidSectionSymbol(Reloc.Index)) - return make_error("Bad relocation section index", - object_error::parse_failed); + return make_error( + "invalid relocation section index", object_error::parse_failed); Reloc.Addend = readVarint32(Ctx); break; default: - return make_error("Bad relocation type: " + - Twine(Reloc.Type), + return make_error("invalid relocation type: " + + Twine(type), object_error::parse_failed); } @@ -871,16 +957,18 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || Reloc.Type == wasm::R_WASM_GLOBAL_INDEX_I32) Size = 4; - if (Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64) + if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I64 || + Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I64 || + Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I64) Size = 8; if (Reloc.Offset + Size > EndOffset) - return make_error("Bad relocation offset", + return make_error("invalid relocation offset", object_error::parse_failed); Section.Relocations.push_back(Reloc); } if (Ctx.Ptr != Ctx.End) - return make_error("Reloc section ended prematurely", + return make_error("reloc section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -915,7 +1003,7 @@ Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { wasm::WasmSignature Sig; uint8_t Form = readUint8(Ctx); if (Form != wasm::WASM_TYPE_FUNC) { - return make_error("Invalid signature type", + return make_error("invalid signature type", object_error::parse_failed); } uint32_t ParamCount = readVaruint32(Ctx); @@ -932,7 +1020,7 @@ Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { Signatures.push_back(std::move(Sig)); } if (Ctx.Ptr != Ctx.End) - return make_error("Type section ended prematurely", + return make_error("type section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -957,26 +1045,32 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { break; case wasm::WASM_EXTERNAL_MEMORY: Im.Memory = readLimits(Ctx); + if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) + HasMemory64 = true; break; - case wasm::WASM_EXTERNAL_TABLE: - Im.Table = readTable(Ctx); - if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF) - return make_error("Invalid table element type", + case wasm::WASM_EXTERNAL_TABLE: { + Im.Table = readTableType(Ctx); + NumImportedTables++; + auto ElemType = Im.Table.ElemType; + if (ElemType != wasm::WASM_TYPE_FUNCREF && + ElemType != wasm::WASM_TYPE_EXTERNREF) + return make_error("invalid table element type", object_error::parse_failed); break; + } case wasm::WASM_EXTERNAL_EVENT: NumImportedEvents++; Im.Event.Attribute = readVarint32(Ctx); Im.Event.SigIndex = readVarint32(Ctx); break; default: - return make_error("Unexpected import kind", + return make_error("unexpected import kind", object_error::parse_failed); } Imports.push_back(Im); } if (Ctx.Ptr != Ctx.End) - return make_error("Import section ended prematurely", + return make_error("import section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -989,28 +1083,34 @@ Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { while (Count--) { uint32_t Type = readVaruint32(Ctx); if (Type >= NumTypes) - return make_error("Invalid function type", + return make_error("invalid function type", object_error::parse_failed); FunctionTypes.push_back(Type); } if (Ctx.Ptr != Ctx.End) - return make_error("Function section ended prematurely", + return make_error("function section ended prematurely", object_error::parse_failed); return Error::success(); } Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { + TableSection = Sections.size(); uint32_t Count = readVaruint32(Ctx); Tables.reserve(Count); while (Count--) { - Tables.push_back(readTable(Ctx)); - if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) { - return make_error("Invalid table element type", + wasm::WasmTable T; + T.Type = readTableType(Ctx); + T.Index = NumImportedTables + Tables.size(); + Tables.push_back(T); + auto ElemType = Tables.back().Type.ElemType; + if (ElemType != wasm::WASM_TYPE_FUNCREF && + ElemType != wasm::WASM_TYPE_EXTERNREF) { + return make_error("invalid table element type", object_error::parse_failed); } } if (Ctx.Ptr != Ctx.End) - return make_error("Table section ended prematurely", + return make_error("table section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1019,10 +1119,13 @@ Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); Memories.reserve(Count); while (Count--) { - Memories.push_back(readLimits(Ctx)); + auto Limits = readLimits(Ctx); + if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64) + HasMemory64 = true; + Memories.push_back(Limits); } if (Ctx.Ptr != Ctx.End) - return make_error("Memory section ended prematurely", + return make_error("memory section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1040,7 +1143,7 @@ Error WasmObjectFile::parseEventSection(ReadContext &Ctx) { } if (Ctx.Ptr != Ctx.End) - return make_error("Event section ended prematurely", + return make_error("event section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1059,7 +1162,7 @@ Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { Globals.push_back(Global); } if (Ctx.Ptr != Ctx.End) - return make_error("Global section ended prematurely", + return make_error("global section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1076,31 +1179,31 @@ Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { case wasm::WASM_EXTERNAL_FUNCTION: if (!isDefinedFunctionIndex(Ex.Index)) - return make_error("Invalid function export", + return make_error("invalid function export", object_error::parse_failed); getDefinedFunction(Ex.Index).ExportName = Ex.Name; break; case wasm::WASM_EXTERNAL_GLOBAL: if (!isValidGlobalIndex(Ex.Index)) - return make_error("Invalid global export", + return make_error("invalid global export", object_error::parse_failed); break; case wasm::WASM_EXTERNAL_EVENT: if (!isValidEventIndex(Ex.Index)) - return make_error("Invalid event export", + return make_error("invalid event export", object_error::parse_failed); break; case wasm::WASM_EXTERNAL_MEMORY: case wasm::WASM_EXTERNAL_TABLE: break; default: - return make_error("Unexpected export kind", + return make_error("unexpected export kind", object_error::parse_failed); } Exports.push_back(Ex); } if (Ctx.Ptr != Ctx.End) - return make_error("Export section ended prematurely", + return make_error("export section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1117,10 +1220,18 @@ bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { return Index < NumImportedGlobals + Globals.size(); } +bool WasmObjectFile::isValidTableIndex(uint32_t Index) const { + return Index < NumImportedTables + Tables.size(); +} + bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { return Index >= NumImportedGlobals && isValidGlobalIndex(Index); } +bool WasmObjectFile::isDefinedTableIndex(uint32_t Index) const { + return Index >= NumImportedTables && isValidTableIndex(Index); +} + bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { return Index < NumImportedEvents + Events.size(); } @@ -1133,6 +1244,10 @@ bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { return Index < Symbols.size() && Symbols[Index].isTypeFunction(); } +bool WasmObjectFile::isValidTableSymbol(uint32_t Index) const { + return Index < Symbols.size() && Symbols[Index].isTypeTable(); +} + bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); } @@ -1173,7 +1288,7 @@ wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) { Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { StartFunction = readVaruint32(Ctx); if (!isValidFunctionIndex(StartFunction)) - return make_error("Invalid start function", + return make_error("invalid start function", object_error::parse_failed); return Error::success(); } @@ -1183,7 +1298,7 @@ Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { CodeSection = Sections.size(); uint32_t FunctionCount = readVaruint32(Ctx); if (FunctionCount != FunctionTypes.size()) { - return make_error("Invalid function count", + return make_error("invalid function count", object_error::parse_failed); } @@ -1215,7 +1330,7 @@ Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { assert(Ctx.Ptr == FunctionEnd); } if (Ctx.Ptr != Ctx.End) - return make_error("Code section ended prematurely", + return make_error("code section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1227,7 +1342,7 @@ Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { wasm::WasmElemSegment Segment; Segment.TableIndex = readVaruint32(Ctx); if (Segment.TableIndex != 0) { - return make_error("Invalid TableIndex", + return make_error("invalid TableIndex", object_error::parse_failed); } if (Error Err = readInitExpr(Segment.Offset, Ctx)) @@ -1239,7 +1354,7 @@ Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { ElemSegments.push_back(Segment); } if (Ctx.Ptr != Ctx.End) - return make_error("Elem section ended prematurely", + return make_error("elem section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1249,14 +1364,16 @@ Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); if (DataCount && Count != DataCount.getValue()) return make_error( - "Number of data segments does not match DataCount section"); + "number of data segments does not match DataCount section"); DataSegments.reserve(Count); while (Count--) { WasmSegment Segment; Segment.Data.InitFlags = readVaruint32(Ctx); - Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) - ? readVaruint32(Ctx) : 0; - if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { + Segment.Data.MemoryIndex = + (Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) + ? readVaruint32(Ctx) + : 0; + if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) return Err; } else { @@ -1265,7 +1382,7 @@ Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { } uint32_t Size = readVaruint32(Ctx); if (Size > (size_t)(Ctx.End - Ctx.Ptr)) - return make_error("Invalid segment size", + return make_error("invalid segment size", object_error::parse_failed); Segment.Data.Content = ArrayRef(Ctx.Ptr, Size); // The rest of these Data fields are set later, when reading in the linking @@ -1278,7 +1395,7 @@ Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { DataSegments.push_back(Segment); } if (Ctx.Ptr != Ctx.End) - return make_error("Data section ended prematurely", + return make_error("data section ended prematurely", object_error::parse_failed); return Error::success(); } @@ -1352,6 +1469,7 @@ uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { case wasm::WASM_SYMBOL_TYPE_FUNCTION: case wasm::WASM_SYMBOL_TYPE_GLOBAL: case wasm::WASM_SYMBOL_TYPE_EVENT: + case wasm::WASM_SYMBOL_TYPE_TABLE: return Sym.Info.ElementIndex; case wasm::WASM_SYMBOL_TYPE_DATA: { // The value of a data symbol is the segment offset, plus the symbol @@ -1401,9 +1519,11 @@ WasmObjectFile::getSymbolType(DataRefImpl Symb) const { return SymbolRef::ST_Debug; case wasm::WASM_SYMBOL_TYPE_EVENT: return SymbolRef::ST_Other; + case wasm::WASM_SYMBOL_TYPE_TABLE: + return SymbolRef::ST_Other; } - llvm_unreachable("Unknown WasmSymbol::SymbolType"); + llvm_unreachable("unknown WasmSymbol::SymbolType"); return SymbolRef::ST_Other; } @@ -1435,8 +1555,10 @@ uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const { return Sym.Info.ElementIndex; case wasm::WASM_SYMBOL_TYPE_EVENT: return EventSection; + case wasm::WASM_SYMBOL_TYPE_TABLE: + return TableSection; default: - llvm_unreachable("Unknown WasmSymbol::SymbolType"); + llvm_unreachable("unknown WasmSymbol::SymbolType"); } } @@ -1576,11 +1698,15 @@ section_iterator WasmObjectFile::section_end() const { return section_iterator(SectionRef(Ref, this)); } -uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } +uint8_t WasmObjectFile::getBytesInAddress() const { + return HasMemory64 ? 8 : 4; +} StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } -Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; } +Triple::ArchType WasmObjectFile::getArch() const { + return HasMemory64 ? Triple::wasm64 : Triple::wasm32; +} SubtargetFeatures WasmObjectFile::getFeatures() const { return SubtargetFeatures(); -- cgit v1.2.3