From 3ec1760d91a38e30d9535c313e4231e332910dd3 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 1 Oct 2021 19:07:41 -0700 Subject: [WebAssembly] Remove WasmTagType This removes `WasmTagType`. `WasmTagType` contained an attribute and a signature index: ``` struct WasmTagType { uint8_t Attribute; uint32_t SigIndex; }; ``` Currently the attribute field is not used and reserved for future use, and always 0. And that this class contains `SigIndex` as its property is a little weird in the place, because the tag type's signature index is not an inherent property of a tag but rather a reference to another section that changes after linking. This makes tag handling in the linker also weird that tag-related methods are taking both `WasmTagType` and `WasmSignature` even though `WasmTagType` contains a signature index. This is because the signature index changes in linking so it doesn't have any info at this point. This instead moves `SigIndex` to `struct WasmTag` itself, as we did for `struct WasmFunction` in D111104. In this CL, in lib/MC and lib/Object, this now treats tag types in the same way as function types. Also in YAML, this removes `struct Tag`, because now it only contains the tag index. Also tags set `SigIndex` in `WasmImport` union, as functions do. I think this makes things simpler and makes tag handling more in line with function handling. These two shares similar properties in that both of them have signatures, but they are kind of nominal so having the same signature doesn't mean they are the same element. Also a drive-by fix: the reserved 'attirubute' part's encoding changed from uleb32 to uint8 a while ago. This was fixed in lib/MC and lib/Object but not in YAML. This doesn't change object files because the field's value is always 0 and its encoding is the same for the both encoding. This is effectively NFC; I didn't mark it as such just because it changed YAML test results. Reviewed By: sbc100, tlively Differential Revision: https://reviews.llvm.org/D111086 --- llvm/lib/Object/WasmObjectFile.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 8dcd9af..a6a4748 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -582,7 +582,6 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { const wasm::WasmSignature *Signature = nullptr; const wasm::WasmGlobalType *GlobalType = nullptr; const wasm::WasmTableType *TableType = nullptr; - const wasm::WasmTagType *TagType = nullptr; Info.Kind = readUint8(Ctx); Info.Flags = readVaruint32(Ctx); @@ -727,8 +726,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { Info.Name = readString(Ctx); unsigned TagIndex = Info.ElementIndex - NumImportedTags; wasm::WasmTag &Tag = Tags[TagIndex]; - Signature = &Signatures[Tag.Type.SigIndex]; - TagType = &Tag.Type; + Signature = &Signatures[Tag.SigIndex]; if (Tag.SymbolName.empty()) Tag.SymbolName = Info.Name; @@ -740,8 +738,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { } else { Info.Name = Import.Field; } - TagType = &Import.Tag; - Signature = &Signatures[TagType->SigIndex]; + Signature = &Signatures[Import.SigIndex]; if (!Import.Module.empty()) { Info.ImportModule = Import.Module; } @@ -763,7 +760,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { object_error::parse_failed); LinkingData.SymbolTable.emplace_back(Info); Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, TableType, - TagType, Signature); + Signature); LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); } @@ -1090,6 +1087,7 @@ Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); + uint32_t NumTypes = Signatures.size(); Imports.reserve(Count); for (uint32_t I = 0; I < Count; I++) { wasm::WasmImport Im; @@ -1100,6 +1098,9 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { case wasm::WASM_EXTERNAL_FUNCTION: NumImportedFunctions++; Im.SigIndex = readVaruint32(Ctx); + if (Im.SigIndex >= NumTypes) + return make_error("invalid function type", + object_error::parse_failed); break; case wasm::WASM_EXTERNAL_GLOBAL: NumImportedGlobals++; @@ -1123,8 +1124,10 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { } case wasm::WASM_EXTERNAL_TAG: NumImportedTags++; - Im.Tag.Attribute = readUint8(Ctx); - Im.Tag.SigIndex = readVarint32(Ctx); + Im.SigIndex = readVaruint32(Ctx); + if (Im.SigIndex >= NumTypes) + return make_error("invalid tag type", + object_error::parse_failed); break; default: return make_error("unexpected import kind", @@ -1198,11 +1201,19 @@ Error WasmObjectFile::parseTagSection(ReadContext &Ctx) { TagSection = Sections.size(); uint32_t Count = readVaruint32(Ctx); Tags.reserve(Count); + uint32_t NumTypes = Signatures.size(); while (Count--) { + char Attr = readUint8(Ctx); // Reserved 'attribute' field + if (Attr != 0) + return make_error("invalid attribute", + object_error::parse_failed); + uint32_t Type = readVaruint32(Ctx); + if (Type >= NumTypes) + return make_error("invalid tag type", + object_error::parse_failed); wasm::WasmTag Tag; Tag.Index = NumImportedTags + Tags.size(); - Tag.Type.Attribute = readUint8(Ctx); - Tag.Type.SigIndex = readVaruint32(Ctx); + Tag.SigIndex = Type; Tags.push_back(Tag); } -- cgit v1.1