1 //===- WasmObjectFile.cpp - Wasm object file implementation ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/ArrayRef.h" 10 #include "llvm/ADT/DenseSet.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/SmallSet.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/StringSet.h" 15 #include "llvm/ADT/Triple.h" 16 #include "llvm/BinaryFormat/Wasm.h" 17 #include "llvm/MC/SubtargetFeature.h" 18 #include "llvm/Object/Binary.h" 19 #include "llvm/Object/Error.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Object/SymbolicFile.h" 22 #include "llvm/Object/Wasm.h" 23 #include "llvm/Support/Endian.h" 24 #include "llvm/Support/Error.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/LEB128.h" 27 #include "llvm/Support/ScopedPrinter.h" 28 #include <algorithm> 29 #include <cassert> 30 #include <cstdint> 31 #include <cstring> 32 #include <system_error> 33 34 #define DEBUG_TYPE "wasm-object" 35 36 using namespace llvm; 37 using namespace object; 38 39 void WasmSymbol::print(raw_ostream &Out) const { 40 Out << "Name=" << Info.Name 41 << ", Kind=" << toString(wasm::WasmSymbolType(Info.Kind)) 42 << ", Flags=" << Info.Flags; 43 if (!isTypeData()) { 44 Out << ", ElemIndex=" << Info.ElementIndex; 45 } else if (isDefined()) { 46 Out << ", Segment=" << Info.DataRef.Segment; 47 Out << ", Offset=" << Info.DataRef.Offset; 48 Out << ", Size=" << Info.DataRef.Size; 49 } 50 } 51 52 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 53 LLVM_DUMP_METHOD void WasmSymbol::dump() const { print(dbgs()); } 54 #endif 55 56 Expected<std::unique_ptr<WasmObjectFile>> 57 ObjectFile::createWasmObjectFile(MemoryBufferRef Buffer) { 58 Error Err = Error::success(); 59 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err); 60 if (Err) 61 return std::move(Err); 62 63 return std::move(ObjectFile); 64 } 65 66 #define VARINT7_MAX ((1 << 7) - 1) 67 #define VARINT7_MIN (-(1 << 7)) 68 #define VARUINT7_MAX (1 << 7) 69 #define VARUINT1_MAX (1) 70 71 static uint8_t readUint8(WasmObjectFile::ReadContext &Ctx) { 72 if (Ctx.Ptr == Ctx.End) 73 report_fatal_error("EOF while reading uint8"); 74 return *Ctx.Ptr++; 75 } 76 77 static uint32_t readUint32(WasmObjectFile::ReadContext &Ctx) { 78 if (Ctx.Ptr + 4 > Ctx.End) 79 report_fatal_error("EOF while reading uint32"); 80 uint32_t Result = support::endian::read32le(Ctx.Ptr); 81 Ctx.Ptr += 4; 82 return Result; 83 } 84 85 static int32_t readFloat32(WasmObjectFile::ReadContext &Ctx) { 86 if (Ctx.Ptr + 4 > Ctx.End) 87 report_fatal_error("EOF while reading float64"); 88 int32_t Result = 0; 89 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 90 Ctx.Ptr += sizeof(Result); 91 return Result; 92 } 93 94 static int64_t readFloat64(WasmObjectFile::ReadContext &Ctx) { 95 if (Ctx.Ptr + 8 > Ctx.End) 96 report_fatal_error("EOF while reading float64"); 97 int64_t Result = 0; 98 memcpy(&Result, Ctx.Ptr, sizeof(Result)); 99 Ctx.Ptr += sizeof(Result); 100 return Result; 101 } 102 103 static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx) { 104 unsigned Count; 105 const char *Error = nullptr; 106 uint64_t Result = decodeULEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 107 if (Error) 108 report_fatal_error(Error); 109 Ctx.Ptr += Count; 110 return Result; 111 } 112 113 static StringRef readString(WasmObjectFile::ReadContext &Ctx) { 114 uint32_t StringLen = readULEB128(Ctx); 115 if (Ctx.Ptr + StringLen > Ctx.End) 116 report_fatal_error("EOF while reading string"); 117 StringRef Return = 118 StringRef(reinterpret_cast<const char *>(Ctx.Ptr), StringLen); 119 Ctx.Ptr += StringLen; 120 return Return; 121 } 122 123 static int64_t readLEB128(WasmObjectFile::ReadContext &Ctx) { 124 unsigned Count; 125 const char *Error = nullptr; 126 uint64_t Result = decodeSLEB128(Ctx.Ptr, &Count, Ctx.End, &Error); 127 if (Error) 128 report_fatal_error(Error); 129 Ctx.Ptr += Count; 130 return Result; 131 } 132 133 static uint8_t readVaruint1(WasmObjectFile::ReadContext &Ctx) { 134 int64_t Result = readLEB128(Ctx); 135 if (Result > VARUINT1_MAX || Result < 0) 136 report_fatal_error("LEB is outside Varuint1 range"); 137 return Result; 138 } 139 140 static int32_t readVarint32(WasmObjectFile::ReadContext &Ctx) { 141 int64_t Result = readLEB128(Ctx); 142 if (Result > INT32_MAX || Result < INT32_MIN) 143 report_fatal_error("LEB is outside Varint32 range"); 144 return Result; 145 } 146 147 static uint32_t readVaruint32(WasmObjectFile::ReadContext &Ctx) { 148 uint64_t Result = readULEB128(Ctx); 149 if (Result > UINT32_MAX) 150 report_fatal_error("LEB is outside Varuint32 range"); 151 return Result; 152 } 153 154 static int64_t readVarint64(WasmObjectFile::ReadContext &Ctx) { 155 return readLEB128(Ctx); 156 } 157 158 static uint8_t readOpcode(WasmObjectFile::ReadContext &Ctx) { 159 return readUint8(Ctx); 160 } 161 162 static Error readInitExpr(wasm::WasmInitExpr &Expr, 163 WasmObjectFile::ReadContext &Ctx) { 164 Expr.Opcode = readOpcode(Ctx); 165 166 switch (Expr.Opcode) { 167 case wasm::WASM_OPCODE_I32_CONST: 168 Expr.Value.Int32 = readVarint32(Ctx); 169 break; 170 case wasm::WASM_OPCODE_I64_CONST: 171 Expr.Value.Int64 = readVarint64(Ctx); 172 break; 173 case wasm::WASM_OPCODE_F32_CONST: 174 Expr.Value.Float32 = readFloat32(Ctx); 175 break; 176 case wasm::WASM_OPCODE_F64_CONST: 177 Expr.Value.Float64 = readFloat64(Ctx); 178 break; 179 case wasm::WASM_OPCODE_GLOBAL_GET: 180 Expr.Value.Global = readULEB128(Ctx); 181 break; 182 default: 183 return make_error<GenericBinaryError>("Invalid opcode in init_expr", 184 object_error::parse_failed); 185 } 186 187 uint8_t EndOpcode = readOpcode(Ctx); 188 if (EndOpcode != wasm::WASM_OPCODE_END) { 189 return make_error<GenericBinaryError>("Invalid init_expr", 190 object_error::parse_failed); 191 } 192 return Error::success(); 193 } 194 195 static wasm::WasmLimits readLimits(WasmObjectFile::ReadContext &Ctx) { 196 wasm::WasmLimits Result; 197 Result.Flags = readVaruint32(Ctx); 198 Result.Initial = readVaruint32(Ctx); 199 if (Result.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 200 Result.Maximum = readVaruint32(Ctx); 201 return Result; 202 } 203 204 static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) { 205 wasm::WasmTable Table; 206 Table.ElemType = readUint8(Ctx); 207 Table.Limits = readLimits(Ctx); 208 return Table; 209 } 210 211 static Error readSection(WasmSection &Section, WasmObjectFile::ReadContext &Ctx, 212 WasmSectionOrderChecker &Checker) { 213 Section.Offset = Ctx.Ptr - Ctx.Start; 214 Section.Type = readUint8(Ctx); 215 LLVM_DEBUG(dbgs() << "readSection type=" << Section.Type << "\n"); 216 uint32_t Size = readVaruint32(Ctx); 217 if (Size == 0) 218 return make_error<StringError>("Zero length section", 219 object_error::parse_failed); 220 if (Ctx.Ptr + Size > Ctx.End) 221 return make_error<StringError>("Section too large", 222 object_error::parse_failed); 223 if (Section.Type == wasm::WASM_SEC_CUSTOM) { 224 WasmObjectFile::ReadContext SectionCtx; 225 SectionCtx.Start = Ctx.Ptr; 226 SectionCtx.Ptr = Ctx.Ptr; 227 SectionCtx.End = Ctx.Ptr + Size; 228 229 Section.Name = readString(SectionCtx); 230 231 uint32_t SectionNameSize = SectionCtx.Ptr - SectionCtx.Start; 232 Ctx.Ptr += SectionNameSize; 233 Size -= SectionNameSize; 234 } 235 236 if (!Checker.isValidSectionOrder(Section.Type, Section.Name)) { 237 return make_error<StringError>("Out of order section type: " + 238 llvm::to_string(Section.Type), 239 object_error::parse_failed); 240 } 241 242 Section.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 243 Ctx.Ptr += Size; 244 return Error::success(); 245 } 246 247 WasmObjectFile::WasmObjectFile(MemoryBufferRef Buffer, Error &Err) 248 : ObjectFile(Binary::ID_Wasm, Buffer) { 249 ErrorAsOutParameter ErrAsOutParam(&Err); 250 Header.Magic = getData().substr(0, 4); 251 if (Header.Magic != StringRef("\0asm", 4)) { 252 Err = 253 make_error<StringError>("Bad magic number", object_error::parse_failed); 254 return; 255 } 256 257 ReadContext Ctx; 258 Ctx.Start = reinterpret_cast<const uint8_t *>(getData().data()); 259 Ctx.Ptr = Ctx.Start + 4; 260 Ctx.End = Ctx.Start + getData().size(); 261 262 if (Ctx.Ptr + 4 > Ctx.End) { 263 Err = make_error<StringError>("Missing version number", 264 object_error::parse_failed); 265 return; 266 } 267 268 Header.Version = readUint32(Ctx); 269 if (Header.Version != wasm::WasmVersion) { 270 Err = make_error<StringError>("Bad version number", 271 object_error::parse_failed); 272 return; 273 } 274 275 WasmSection Sec; 276 WasmSectionOrderChecker Checker; 277 while (Ctx.Ptr < Ctx.End) { 278 if ((Err = readSection(Sec, Ctx, Checker))) 279 return; 280 if ((Err = parseSection(Sec))) 281 return; 282 283 Sections.push_back(Sec); 284 } 285 } 286 287 Error WasmObjectFile::parseSection(WasmSection &Sec) { 288 ReadContext Ctx; 289 Ctx.Start = Sec.Content.data(); 290 Ctx.End = Ctx.Start + Sec.Content.size(); 291 Ctx.Ptr = Ctx.Start; 292 switch (Sec.Type) { 293 case wasm::WASM_SEC_CUSTOM: 294 return parseCustomSection(Sec, Ctx); 295 case wasm::WASM_SEC_TYPE: 296 return parseTypeSection(Ctx); 297 case wasm::WASM_SEC_IMPORT: 298 return parseImportSection(Ctx); 299 case wasm::WASM_SEC_FUNCTION: 300 return parseFunctionSection(Ctx); 301 case wasm::WASM_SEC_TABLE: 302 return parseTableSection(Ctx); 303 case wasm::WASM_SEC_MEMORY: 304 return parseMemorySection(Ctx); 305 case wasm::WASM_SEC_GLOBAL: 306 return parseGlobalSection(Ctx); 307 case wasm::WASM_SEC_EVENT: 308 return parseEventSection(Ctx); 309 case wasm::WASM_SEC_EXPORT: 310 return parseExportSection(Ctx); 311 case wasm::WASM_SEC_START: 312 return parseStartSection(Ctx); 313 case wasm::WASM_SEC_ELEM: 314 return parseElemSection(Ctx); 315 case wasm::WASM_SEC_CODE: 316 return parseCodeSection(Ctx); 317 case wasm::WASM_SEC_DATA: 318 return parseDataSection(Ctx); 319 default: 320 return make_error<GenericBinaryError>("Bad section type", 321 object_error::parse_failed); 322 } 323 } 324 325 Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { 326 // See https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md 327 HasDylinkSection = true; 328 DylinkInfo.MemorySize = readVaruint32(Ctx); 329 DylinkInfo.MemoryAlignment = readVaruint32(Ctx); 330 DylinkInfo.TableSize = readVaruint32(Ctx); 331 DylinkInfo.TableAlignment = readVaruint32(Ctx); 332 uint32_t Count = readVaruint32(Ctx); 333 while (Count--) { 334 DylinkInfo.Needed.push_back(readString(Ctx)); 335 } 336 if (Ctx.Ptr != Ctx.End) 337 return make_error<GenericBinaryError>("dylink section ended prematurely", 338 object_error::parse_failed); 339 return Error::success(); 340 } 341 342 Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { 343 llvm::DenseSet<uint64_t> Seen; 344 if (Functions.size() != FunctionTypes.size()) { 345 return make_error<GenericBinaryError>("Names must come after code section", 346 object_error::parse_failed); 347 } 348 349 while (Ctx.Ptr < Ctx.End) { 350 uint8_t Type = readUint8(Ctx); 351 uint32_t Size = readVaruint32(Ctx); 352 const uint8_t *SubSectionEnd = Ctx.Ptr + Size; 353 switch (Type) { 354 case wasm::WASM_NAMES_FUNCTION: { 355 uint32_t Count = readVaruint32(Ctx); 356 while (Count--) { 357 uint32_t Index = readVaruint32(Ctx); 358 if (!Seen.insert(Index).second) 359 return make_error<GenericBinaryError>("Function named more than once", 360 object_error::parse_failed); 361 StringRef Name = readString(Ctx); 362 if (!isValidFunctionIndex(Index) || Name.empty()) 363 return make_error<GenericBinaryError>("Invalid name entry", 364 object_error::parse_failed); 365 DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); 366 if (isDefinedFunctionIndex(Index)) 367 getDefinedFunction(Index).DebugName = Name; 368 } 369 break; 370 } 371 // Ignore local names for now 372 case wasm::WASM_NAMES_LOCAL: 373 default: 374 Ctx.Ptr += Size; 375 break; 376 } 377 if (Ctx.Ptr != SubSectionEnd) 378 return make_error<GenericBinaryError>( 379 "Name sub-section ended prematurely", object_error::parse_failed); 380 } 381 382 if (Ctx.Ptr != Ctx.End) 383 return make_error<GenericBinaryError>("Name section ended prematurely", 384 object_error::parse_failed); 385 return Error::success(); 386 } 387 388 Error WasmObjectFile::parseLinkingSection(ReadContext &Ctx) { 389 HasLinkingSection = true; 390 if (Functions.size() != FunctionTypes.size()) { 391 return make_error<GenericBinaryError>( 392 "Linking data must come after code section", 393 object_error::parse_failed); 394 } 395 396 LinkingData.Version = readVaruint32(Ctx); 397 if (LinkingData.Version != wasm::WasmMetadataVersion) { 398 return make_error<GenericBinaryError>( 399 "Unexpected metadata version: " + Twine(LinkingData.Version) + 400 " (Expected: " + Twine(wasm::WasmMetadataVersion) + ")", 401 object_error::parse_failed); 402 } 403 404 const uint8_t *OrigEnd = Ctx.End; 405 while (Ctx.Ptr < OrigEnd) { 406 Ctx.End = OrigEnd; 407 uint8_t Type = readUint8(Ctx); 408 uint32_t Size = readVaruint32(Ctx); 409 LLVM_DEBUG(dbgs() << "readSubsection type=" << int(Type) << " size=" << Size 410 << "\n"); 411 Ctx.End = Ctx.Ptr + Size; 412 switch (Type) { 413 case wasm::WASM_SYMBOL_TABLE: 414 if (Error Err = parseLinkingSectionSymtab(Ctx)) 415 return Err; 416 break; 417 case wasm::WASM_SEGMENT_INFO: { 418 uint32_t Count = readVaruint32(Ctx); 419 if (Count > DataSegments.size()) 420 return make_error<GenericBinaryError>("Too many segment names", 421 object_error::parse_failed); 422 for (uint32_t I = 0; I < Count; I++) { 423 DataSegments[I].Data.Name = readString(Ctx); 424 DataSegments[I].Data.Alignment = readVaruint32(Ctx); 425 DataSegments[I].Data.LinkerFlags = readVaruint32(Ctx); 426 } 427 break; 428 } 429 case wasm::WASM_INIT_FUNCS: { 430 uint32_t Count = readVaruint32(Ctx); 431 LinkingData.InitFunctions.reserve(Count); 432 for (uint32_t I = 0; I < Count; I++) { 433 wasm::WasmInitFunc Init; 434 Init.Priority = readVaruint32(Ctx); 435 Init.Symbol = readVaruint32(Ctx); 436 if (!isValidFunctionSymbol(Init.Symbol)) 437 return make_error<GenericBinaryError>("Invalid function symbol: " + 438 Twine(Init.Symbol), 439 object_error::parse_failed); 440 LinkingData.InitFunctions.emplace_back(Init); 441 } 442 break; 443 } 444 case wasm::WASM_COMDAT_INFO: 445 if (Error Err = parseLinkingSectionComdat(Ctx)) 446 return Err; 447 break; 448 default: 449 Ctx.Ptr += Size; 450 break; 451 } 452 if (Ctx.Ptr != Ctx.End) 453 return make_error<GenericBinaryError>( 454 "Linking sub-section ended prematurely", object_error::parse_failed); 455 } 456 if (Ctx.Ptr != OrigEnd) 457 return make_error<GenericBinaryError>("Linking section ended prematurely", 458 object_error::parse_failed); 459 return Error::success(); 460 } 461 462 Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { 463 uint32_t Count = readVaruint32(Ctx); 464 LinkingData.SymbolTable.reserve(Count); 465 Symbols.reserve(Count); 466 StringSet<> SymbolNames; 467 468 std::vector<wasm::WasmImport *> ImportedGlobals; 469 std::vector<wasm::WasmImport *> ImportedFunctions; 470 std::vector<wasm::WasmImport *> ImportedEvents; 471 ImportedGlobals.reserve(Imports.size()); 472 ImportedFunctions.reserve(Imports.size()); 473 ImportedEvents.reserve(Imports.size()); 474 for (auto &I : Imports) { 475 if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION) 476 ImportedFunctions.emplace_back(&I); 477 else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL) 478 ImportedGlobals.emplace_back(&I); 479 else if (I.Kind == wasm::WASM_EXTERNAL_EVENT) 480 ImportedEvents.emplace_back(&I); 481 } 482 483 while (Count--) { 484 wasm::WasmSymbolInfo Info; 485 const wasm::WasmSignature *Signature = nullptr; 486 const wasm::WasmGlobalType *GlobalType = nullptr; 487 const wasm::WasmEventType *EventType = nullptr; 488 489 Info.Kind = readUint8(Ctx); 490 Info.Flags = readVaruint32(Ctx); 491 bool IsDefined = (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0; 492 493 switch (Info.Kind) { 494 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 495 Info.ElementIndex = readVaruint32(Ctx); 496 if (!isValidFunctionIndex(Info.ElementIndex) || 497 IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) 498 return make_error<GenericBinaryError>("invalid function symbol index", 499 object_error::parse_failed); 500 if (IsDefined) { 501 Info.Name = readString(Ctx); 502 unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; 503 Signature = &Signatures[FunctionTypes[FuncIndex]]; 504 wasm::WasmFunction &Function = Functions[FuncIndex]; 505 if (Function.SymbolName.empty()) 506 Function.SymbolName = Info.Name; 507 } else { 508 wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; 509 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 510 Info.Name = readString(Ctx); 511 else 512 Info.Name = Import.Field; 513 Signature = &Signatures[Import.SigIndex]; 514 Info.ImportName = Import.Field; 515 Info.ImportModule = Import.Module; 516 } 517 break; 518 519 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 520 Info.ElementIndex = readVaruint32(Ctx); 521 if (!isValidGlobalIndex(Info.ElementIndex) || 522 IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) 523 return make_error<GenericBinaryError>("invalid global symbol index", 524 object_error::parse_failed); 525 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 526 wasm::WASM_SYMBOL_BINDING_WEAK) 527 return make_error<GenericBinaryError>("undefined weak global symbol", 528 object_error::parse_failed); 529 if (IsDefined) { 530 Info.Name = readString(Ctx); 531 unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; 532 wasm::WasmGlobal &Global = Globals[GlobalIndex]; 533 GlobalType = &Global.Type; 534 if (Global.SymbolName.empty()) 535 Global.SymbolName = Info.Name; 536 } else { 537 wasm::WasmImport &Import = *ImportedGlobals[Info.ElementIndex]; 538 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 539 Info.Name = readString(Ctx); 540 else 541 Info.Name = Import.Field; 542 GlobalType = &Import.Global; 543 Info.ImportName = Import.Field; 544 Info.ImportModule = Import.Module; 545 } 546 break; 547 548 case wasm::WASM_SYMBOL_TYPE_DATA: 549 Info.Name = readString(Ctx); 550 if (IsDefined) { 551 uint32_t Index = readVaruint32(Ctx); 552 if (Index >= DataSegments.size()) 553 return make_error<GenericBinaryError>("invalid data symbol index", 554 object_error::parse_failed); 555 uint32_t Offset = readVaruint32(Ctx); 556 uint32_t Size = readVaruint32(Ctx); 557 if (Offset + Size > DataSegments[Index].Data.Content.size()) 558 return make_error<GenericBinaryError>("invalid data symbol offset", 559 object_error::parse_failed); 560 Info.DataRef = wasm::WasmDataReference{Index, Offset, Size}; 561 } 562 break; 563 564 case wasm::WASM_SYMBOL_TYPE_SECTION: { 565 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 566 wasm::WASM_SYMBOL_BINDING_LOCAL) 567 return make_error<GenericBinaryError>( 568 "Section symbols must have local binding", 569 object_error::parse_failed); 570 Info.ElementIndex = readVaruint32(Ctx); 571 // Use somewhat unique section name as symbol name. 572 StringRef SectionName = Sections[Info.ElementIndex].Name; 573 Info.Name = SectionName; 574 break; 575 } 576 577 case wasm::WASM_SYMBOL_TYPE_EVENT: { 578 Info.ElementIndex = readVaruint32(Ctx); 579 if (!isValidEventIndex(Info.ElementIndex) || 580 IsDefined != isDefinedEventIndex(Info.ElementIndex)) 581 return make_error<GenericBinaryError>("invalid event symbol index", 582 object_error::parse_failed); 583 if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == 584 wasm::WASM_SYMBOL_BINDING_WEAK) 585 return make_error<GenericBinaryError>("undefined weak global symbol", 586 object_error::parse_failed); 587 if (IsDefined) { 588 Info.Name = readString(Ctx); 589 unsigned EventIndex = Info.ElementIndex - NumImportedEvents; 590 wasm::WasmEvent &Event = Events[EventIndex]; 591 Signature = &Signatures[Event.Type.SigIndex]; 592 EventType = &Event.Type; 593 if (Event.SymbolName.empty()) 594 Event.SymbolName = Info.Name; 595 596 } else { 597 wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex]; 598 if ((Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 599 Info.Name = readString(Ctx); 600 else 601 Info.Name = Import.Field; 602 EventType = &Import.Event; 603 Signature = &Signatures[EventType->SigIndex]; 604 Info.ImportName = Import.Field; 605 Info.ImportModule = Import.Module; 606 } 607 break; 608 } 609 610 default: 611 return make_error<GenericBinaryError>("Invalid symbol type", 612 object_error::parse_failed); 613 } 614 615 if ((Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) != 616 wasm::WASM_SYMBOL_BINDING_LOCAL && 617 !SymbolNames.insert(Info.Name).second) 618 return make_error<GenericBinaryError>("Duplicate symbol name " + 619 Twine(Info.Name), 620 object_error::parse_failed); 621 LinkingData.SymbolTable.emplace_back(Info); 622 Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType, 623 Signature); 624 LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); 625 } 626 627 return Error::success(); 628 } 629 630 Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { 631 uint32_t ComdatCount = readVaruint32(Ctx); 632 StringSet<> ComdatSet; 633 for (unsigned ComdatIndex = 0; ComdatIndex < ComdatCount; ++ComdatIndex) { 634 StringRef Name = readString(Ctx); 635 if (Name.empty() || !ComdatSet.insert(Name).second) 636 return make_error<GenericBinaryError>("Bad/duplicate COMDAT name " + 637 Twine(Name), 638 object_error::parse_failed); 639 LinkingData.Comdats.emplace_back(Name); 640 uint32_t Flags = readVaruint32(Ctx); 641 if (Flags != 0) 642 return make_error<GenericBinaryError>("Unsupported COMDAT flags", 643 object_error::parse_failed); 644 645 uint32_t EntryCount = readVaruint32(Ctx); 646 while (EntryCount--) { 647 unsigned Kind = readVaruint32(Ctx); 648 unsigned Index = readVaruint32(Ctx); 649 switch (Kind) { 650 default: 651 return make_error<GenericBinaryError>("Invalid COMDAT entry type", 652 object_error::parse_failed); 653 case wasm::WASM_COMDAT_DATA: 654 if (Index >= DataSegments.size()) 655 return make_error<GenericBinaryError>( 656 "COMDAT data index out of range", object_error::parse_failed); 657 if (DataSegments[Index].Data.Comdat != UINT32_MAX) 658 return make_error<GenericBinaryError>("Data segment in two COMDATs", 659 object_error::parse_failed); 660 DataSegments[Index].Data.Comdat = ComdatIndex; 661 break; 662 case wasm::WASM_COMDAT_FUNCTION: 663 if (!isDefinedFunctionIndex(Index)) 664 return make_error<GenericBinaryError>( 665 "COMDAT function index out of range", object_error::parse_failed); 666 if (getDefinedFunction(Index).Comdat != UINT32_MAX) 667 return make_error<GenericBinaryError>("Function in two COMDATs", 668 object_error::parse_failed); 669 getDefinedFunction(Index).Comdat = ComdatIndex; 670 break; 671 } 672 } 673 } 674 return Error::success(); 675 } 676 677 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { 678 llvm::SmallSet<StringRef, 3> FieldsSeen; 679 uint32_t Fields = readVaruint32(Ctx); 680 for (size_t I = 0; I < Fields; ++I) { 681 StringRef FieldName = readString(Ctx); 682 if (!FieldsSeen.insert(FieldName).second) 683 return make_error<GenericBinaryError>( 684 "Producers section does not have unique fields", 685 object_error::parse_failed); 686 std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr; 687 if (FieldName == "language") { 688 ProducerVec = &ProducerInfo.Languages; 689 } else if (FieldName == "processed-by") { 690 ProducerVec = &ProducerInfo.Tools; 691 } else if (FieldName == "sdk") { 692 ProducerVec = &ProducerInfo.SDKs; 693 } else { 694 return make_error<GenericBinaryError>( 695 "Producers section field is not named one of language, processed-by, " 696 "or sdk", 697 object_error::parse_failed); 698 } 699 uint32_t ValueCount = readVaruint32(Ctx); 700 llvm::SmallSet<StringRef, 8> ProducersSeen; 701 for (size_t J = 0; J < ValueCount; ++J) { 702 StringRef Name = readString(Ctx); 703 StringRef Version = readString(Ctx); 704 if (!ProducersSeen.insert(Name).second) { 705 return make_error<GenericBinaryError>( 706 "Producers section contains repeated producer", 707 object_error::parse_failed); 708 } 709 ProducerVec->emplace_back(Name, Version); 710 } 711 } 712 if (Ctx.Ptr != Ctx.End) 713 return make_error<GenericBinaryError>("Producers section ended prematurely", 714 object_error::parse_failed); 715 return Error::success(); 716 } 717 718 Error WasmObjectFile::parseTargetFeaturesSection(ReadContext &Ctx) { 719 llvm::SmallSet<std::string, 8> FeaturesSeen; 720 uint32_t FeatureCount = readVaruint32(Ctx); 721 for (size_t I = 0; I < FeatureCount; ++I) { 722 wasm::WasmFeatureEntry Feature; 723 Feature.Prefix = readUint8(Ctx); 724 switch (Feature.Prefix) { 725 case wasm::WASM_FEATURE_PREFIX_USED: 726 case wasm::WASM_FEATURE_PREFIX_REQUIRED: 727 case wasm::WASM_FEATURE_PREFIX_DISALLOWED: 728 break; 729 default: 730 return make_error<GenericBinaryError>("Unknown feature policy prefix", 731 object_error::parse_failed); 732 } 733 Feature.Name = readString(Ctx); 734 if (!FeaturesSeen.insert(Feature.Name).second) 735 return make_error<GenericBinaryError>( 736 "Target features section contains repeated feature \"" + 737 Feature.Name + "\"", 738 object_error::parse_failed); 739 TargetFeatures.push_back(Feature); 740 } 741 if (Ctx.Ptr != Ctx.End) 742 return make_error<GenericBinaryError>( 743 "Target features section ended prematurely", 744 object_error::parse_failed); 745 return Error::success(); 746 } 747 748 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { 749 uint32_t SectionIndex = readVaruint32(Ctx); 750 if (SectionIndex >= Sections.size()) 751 return make_error<GenericBinaryError>("Invalid section index", 752 object_error::parse_failed); 753 WasmSection &Section = Sections[SectionIndex]; 754 uint32_t RelocCount = readVaruint32(Ctx); 755 uint32_t EndOffset = Section.Content.size(); 756 uint32_t PreviousOffset = 0; 757 while (RelocCount--) { 758 wasm::WasmRelocation Reloc = {}; 759 Reloc.Type = readVaruint32(Ctx); 760 Reloc.Offset = readVaruint32(Ctx); 761 if (Reloc.Offset < PreviousOffset) 762 return make_error<GenericBinaryError>("Relocations not in offset order", 763 object_error::parse_failed); 764 PreviousOffset = Reloc.Offset; 765 Reloc.Index = readVaruint32(Ctx); 766 switch (Reloc.Type) { 767 case wasm::R_WASM_FUNCTION_INDEX_LEB: 768 case wasm::R_WASM_TABLE_INDEX_SLEB: 769 case wasm::R_WASM_TABLE_INDEX_I32: 770 if (!isValidFunctionSymbol(Reloc.Index)) 771 return make_error<GenericBinaryError>("Bad relocation function index", 772 object_error::parse_failed); 773 break; 774 case wasm::R_WASM_TYPE_INDEX_LEB: 775 if (Reloc.Index >= Signatures.size()) 776 return make_error<GenericBinaryError>("Bad relocation type index", 777 object_error::parse_failed); 778 break; 779 case wasm::R_WASM_GLOBAL_INDEX_LEB: 780 // R_WASM_GLOBAL_INDEX_LEB are can be used against function and data 781 // symbols to refer to thier GOT enties. 782 if (!isValidGlobalSymbol(Reloc.Index) && 783 !isValidDataSymbol(Reloc.Index) && 784 !isValidFunctionSymbol(Reloc.Index)) 785 return make_error<GenericBinaryError>("Bad relocation global index", 786 object_error::parse_failed); 787 break; 788 case wasm::R_WASM_EVENT_INDEX_LEB: 789 if (!isValidEventSymbol(Reloc.Index)) 790 return make_error<GenericBinaryError>("Bad relocation event index", 791 object_error::parse_failed); 792 break; 793 case wasm::R_WASM_MEMORY_ADDR_LEB: 794 case wasm::R_WASM_MEMORY_ADDR_SLEB: 795 case wasm::R_WASM_MEMORY_ADDR_I32: 796 if (!isValidDataSymbol(Reloc.Index)) 797 return make_error<GenericBinaryError>("Bad relocation data index", 798 object_error::parse_failed); 799 Reloc.Addend = readVarint32(Ctx); 800 break; 801 case wasm::R_WASM_FUNCTION_OFFSET_I32: 802 if (!isValidFunctionSymbol(Reloc.Index)) 803 return make_error<GenericBinaryError>("Bad relocation function index", 804 object_error::parse_failed); 805 Reloc.Addend = readVarint32(Ctx); 806 break; 807 case wasm::R_WASM_SECTION_OFFSET_I32: 808 if (!isValidSectionSymbol(Reloc.Index)) 809 return make_error<GenericBinaryError>("Bad relocation section index", 810 object_error::parse_failed); 811 Reloc.Addend = readVarint32(Ctx); 812 break; 813 default: 814 return make_error<GenericBinaryError>("Bad relocation type: " + 815 Twine(Reloc.Type), 816 object_error::parse_failed); 817 } 818 819 // Relocations must fit inside the section, and must appear in order. They 820 // also shouldn't overlap a function/element boundary, but we don't bother 821 // to check that. 822 uint64_t Size = 5; 823 if (Reloc.Type == wasm::R_WASM_TABLE_INDEX_I32 || 824 Reloc.Type == wasm::R_WASM_MEMORY_ADDR_I32 || 825 Reloc.Type == wasm::R_WASM_SECTION_OFFSET_I32 || 826 Reloc.Type == wasm::R_WASM_FUNCTION_OFFSET_I32) 827 Size = 4; 828 if (Reloc.Offset + Size > EndOffset) 829 return make_error<GenericBinaryError>("Bad relocation offset", 830 object_error::parse_failed); 831 832 Section.Relocations.push_back(Reloc); 833 } 834 if (Ctx.Ptr != Ctx.End) 835 return make_error<GenericBinaryError>("Reloc section ended prematurely", 836 object_error::parse_failed); 837 return Error::success(); 838 } 839 840 Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { 841 if (Sec.Name == "dylink") { 842 if (Error Err = parseDylinkSection(Ctx)) 843 return Err; 844 } else if (Sec.Name == "name") { 845 if (Error Err = parseNameSection(Ctx)) 846 return Err; 847 } else if (Sec.Name == "linking") { 848 if (Error Err = parseLinkingSection(Ctx)) 849 return Err; 850 } else if (Sec.Name == "producers") { 851 if (Error Err = parseProducersSection(Ctx)) 852 return Err; 853 } else if (Sec.Name == "target_features") { 854 if (Error Err = parseTargetFeaturesSection(Ctx)) 855 return Err; 856 } else if (Sec.Name.startswith("reloc.")) { 857 if (Error Err = parseRelocSection(Sec.Name, Ctx)) 858 return Err; 859 } 860 return Error::success(); 861 } 862 863 Error WasmObjectFile::parseTypeSection(ReadContext &Ctx) { 864 uint32_t Count = readVaruint32(Ctx); 865 Signatures.reserve(Count); 866 while (Count--) { 867 wasm::WasmSignature Sig; 868 uint8_t Form = readUint8(Ctx); 869 if (Form != wasm::WASM_TYPE_FUNC) { 870 return make_error<GenericBinaryError>("Invalid signature type", 871 object_error::parse_failed); 872 } 873 uint32_t ParamCount = readVaruint32(Ctx); 874 Sig.Params.reserve(ParamCount); 875 while (ParamCount--) { 876 uint32_t ParamType = readUint8(Ctx); 877 Sig.Params.push_back(wasm::ValType(ParamType)); 878 } 879 uint32_t ReturnCount = readVaruint32(Ctx); 880 if (ReturnCount) { 881 if (ReturnCount != 1) { 882 return make_error<GenericBinaryError>( 883 "Multiple return types not supported", object_error::parse_failed); 884 } 885 Sig.Returns.push_back(wasm::ValType(readUint8(Ctx))); 886 } 887 Signatures.push_back(std::move(Sig)); 888 } 889 if (Ctx.Ptr != Ctx.End) 890 return make_error<GenericBinaryError>("Type section ended prematurely", 891 object_error::parse_failed); 892 return Error::success(); 893 } 894 895 Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { 896 uint32_t Count = readVaruint32(Ctx); 897 Imports.reserve(Count); 898 for (uint32_t I = 0; I < Count; I++) { 899 wasm::WasmImport Im; 900 Im.Module = readString(Ctx); 901 Im.Field = readString(Ctx); 902 Im.Kind = readUint8(Ctx); 903 switch (Im.Kind) { 904 case wasm::WASM_EXTERNAL_FUNCTION: 905 NumImportedFunctions++; 906 Im.SigIndex = readVaruint32(Ctx); 907 break; 908 case wasm::WASM_EXTERNAL_GLOBAL: 909 NumImportedGlobals++; 910 Im.Global.Type = readUint8(Ctx); 911 Im.Global.Mutable = readVaruint1(Ctx); 912 break; 913 case wasm::WASM_EXTERNAL_MEMORY: 914 Im.Memory = readLimits(Ctx); 915 break; 916 case wasm::WASM_EXTERNAL_TABLE: 917 Im.Table = readTable(Ctx); 918 if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF) 919 return make_error<GenericBinaryError>("Invalid table element type", 920 object_error::parse_failed); 921 break; 922 case wasm::WASM_EXTERNAL_EVENT: 923 NumImportedEvents++; 924 Im.Event.Attribute = readVarint32(Ctx); 925 Im.Event.SigIndex = readVarint32(Ctx); 926 break; 927 default: 928 return make_error<GenericBinaryError>("Unexpected import kind", 929 object_error::parse_failed); 930 } 931 Imports.push_back(Im); 932 } 933 if (Ctx.Ptr != Ctx.End) 934 return make_error<GenericBinaryError>("Import section ended prematurely", 935 object_error::parse_failed); 936 return Error::success(); 937 } 938 939 Error WasmObjectFile::parseFunctionSection(ReadContext &Ctx) { 940 uint32_t Count = readVaruint32(Ctx); 941 FunctionTypes.reserve(Count); 942 uint32_t NumTypes = Signatures.size(); 943 while (Count--) { 944 uint32_t Type = readVaruint32(Ctx); 945 if (Type >= NumTypes) 946 return make_error<GenericBinaryError>("Invalid function type", 947 object_error::parse_failed); 948 FunctionTypes.push_back(Type); 949 } 950 if (Ctx.Ptr != Ctx.End) 951 return make_error<GenericBinaryError>("Function section ended prematurely", 952 object_error::parse_failed); 953 return Error::success(); 954 } 955 956 Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { 957 uint32_t Count = readVaruint32(Ctx); 958 Tables.reserve(Count); 959 while (Count--) { 960 Tables.push_back(readTable(Ctx)); 961 if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) { 962 return make_error<GenericBinaryError>("Invalid table element type", 963 object_error::parse_failed); 964 } 965 } 966 if (Ctx.Ptr != Ctx.End) 967 return make_error<GenericBinaryError>("Table section ended prematurely", 968 object_error::parse_failed); 969 return Error::success(); 970 } 971 972 Error WasmObjectFile::parseMemorySection(ReadContext &Ctx) { 973 uint32_t Count = readVaruint32(Ctx); 974 Memories.reserve(Count); 975 while (Count--) { 976 Memories.push_back(readLimits(Ctx)); 977 } 978 if (Ctx.Ptr != Ctx.End) 979 return make_error<GenericBinaryError>("Memory section ended prematurely", 980 object_error::parse_failed); 981 return Error::success(); 982 } 983 984 Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { 985 GlobalSection = Sections.size(); 986 uint32_t Count = readVaruint32(Ctx); 987 Globals.reserve(Count); 988 while (Count--) { 989 wasm::WasmGlobal Global; 990 Global.Index = NumImportedGlobals + Globals.size(); 991 Global.Type.Type = readUint8(Ctx); 992 Global.Type.Mutable = readVaruint1(Ctx); 993 if (Error Err = readInitExpr(Global.InitExpr, Ctx)) 994 return Err; 995 Globals.push_back(Global); 996 } 997 if (Ctx.Ptr != Ctx.End) 998 return make_error<GenericBinaryError>("Global section ended prematurely", 999 object_error::parse_failed); 1000 return Error::success(); 1001 } 1002 1003 Error WasmObjectFile::parseEventSection(ReadContext &Ctx) { 1004 EventSection = Sections.size(); 1005 uint32_t Count = readVarint32(Ctx); 1006 Events.reserve(Count); 1007 while (Count--) { 1008 wasm::WasmEvent Event; 1009 Event.Index = NumImportedEvents + Events.size(); 1010 Event.Type.Attribute = readVaruint32(Ctx); 1011 Event.Type.SigIndex = readVarint32(Ctx); 1012 Events.push_back(Event); 1013 } 1014 1015 if (Ctx.Ptr != Ctx.End) 1016 return make_error<GenericBinaryError>("Event section ended prematurely", 1017 object_error::parse_failed); 1018 return Error::success(); 1019 } 1020 1021 Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { 1022 uint32_t Count = readVaruint32(Ctx); 1023 Exports.reserve(Count); 1024 for (uint32_t I = 0; I < Count; I++) { 1025 wasm::WasmExport Ex; 1026 Ex.Name = readString(Ctx); 1027 Ex.Kind = readUint8(Ctx); 1028 Ex.Index = readVaruint32(Ctx); 1029 switch (Ex.Kind) { 1030 case wasm::WASM_EXTERNAL_FUNCTION: 1031 if (!isValidFunctionIndex(Ex.Index)) 1032 return make_error<GenericBinaryError>("Invalid function export", 1033 object_error::parse_failed); 1034 break; 1035 case wasm::WASM_EXTERNAL_GLOBAL: 1036 if (!isValidGlobalIndex(Ex.Index)) 1037 return make_error<GenericBinaryError>("Invalid global export", 1038 object_error::parse_failed); 1039 break; 1040 case wasm::WASM_EXTERNAL_EVENT: 1041 if (!isValidEventIndex(Ex.Index)) 1042 return make_error<GenericBinaryError>("Invalid event export", 1043 object_error::parse_failed); 1044 break; 1045 case wasm::WASM_EXTERNAL_MEMORY: 1046 case wasm::WASM_EXTERNAL_TABLE: 1047 break; 1048 default: 1049 return make_error<GenericBinaryError>("Unexpected export kind", 1050 object_error::parse_failed); 1051 } 1052 Exports.push_back(Ex); 1053 } 1054 if (Ctx.Ptr != Ctx.End) 1055 return make_error<GenericBinaryError>("Export section ended prematurely", 1056 object_error::parse_failed); 1057 return Error::success(); 1058 } 1059 1060 bool WasmObjectFile::isValidFunctionIndex(uint32_t Index) const { 1061 return Index < NumImportedFunctions + FunctionTypes.size(); 1062 } 1063 1064 bool WasmObjectFile::isDefinedFunctionIndex(uint32_t Index) const { 1065 return Index >= NumImportedFunctions && isValidFunctionIndex(Index); 1066 } 1067 1068 bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { 1069 return Index < NumImportedGlobals + Globals.size(); 1070 } 1071 1072 bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { 1073 return Index >= NumImportedGlobals && isValidGlobalIndex(Index); 1074 } 1075 1076 bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { 1077 return Index < NumImportedEvents + Events.size(); 1078 } 1079 1080 bool WasmObjectFile::isDefinedEventIndex(uint32_t Index) const { 1081 return Index >= NumImportedEvents && isValidEventIndex(Index); 1082 } 1083 1084 bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const { 1085 return Index < Symbols.size() && Symbols[Index].isTypeFunction(); 1086 } 1087 1088 bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const { 1089 return Index < Symbols.size() && Symbols[Index].isTypeGlobal(); 1090 } 1091 1092 bool WasmObjectFile::isValidEventSymbol(uint32_t Index) const { 1093 return Index < Symbols.size() && Symbols[Index].isTypeEvent(); 1094 } 1095 1096 bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const { 1097 return Index < Symbols.size() && Symbols[Index].isTypeData(); 1098 } 1099 1100 bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { 1101 return Index < Symbols.size() && Symbols[Index].isTypeSection(); 1102 } 1103 1104 wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { 1105 assert(isDefinedFunctionIndex(Index)); 1106 return Functions[Index - NumImportedFunctions]; 1107 } 1108 1109 const wasm::WasmFunction & 1110 WasmObjectFile::getDefinedFunction(uint32_t Index) const { 1111 assert(isDefinedFunctionIndex(Index)); 1112 return Functions[Index - NumImportedFunctions]; 1113 } 1114 1115 wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) { 1116 assert(isDefinedGlobalIndex(Index)); 1117 return Globals[Index - NumImportedGlobals]; 1118 } 1119 1120 wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) { 1121 assert(isDefinedEventIndex(Index)); 1122 return Events[Index - NumImportedEvents]; 1123 } 1124 1125 Error WasmObjectFile::parseStartSection(ReadContext &Ctx) { 1126 StartFunction = readVaruint32(Ctx); 1127 if (!isValidFunctionIndex(StartFunction)) 1128 return make_error<GenericBinaryError>("Invalid start function", 1129 object_error::parse_failed); 1130 return Error::success(); 1131 } 1132 1133 Error WasmObjectFile::parseCodeSection(ReadContext &Ctx) { 1134 CodeSection = Sections.size(); 1135 uint32_t FunctionCount = readVaruint32(Ctx); 1136 if (FunctionCount != FunctionTypes.size()) { 1137 return make_error<GenericBinaryError>("Invalid function count", 1138 object_error::parse_failed); 1139 } 1140 1141 while (FunctionCount--) { 1142 wasm::WasmFunction Function; 1143 const uint8_t *FunctionStart = Ctx.Ptr; 1144 uint32_t Size = readVaruint32(Ctx); 1145 const uint8_t *FunctionEnd = Ctx.Ptr + Size; 1146 1147 Function.CodeOffset = Ctx.Ptr - FunctionStart; 1148 Function.Index = NumImportedFunctions + Functions.size(); 1149 Function.CodeSectionOffset = FunctionStart - Ctx.Start; 1150 Function.Size = FunctionEnd - FunctionStart; 1151 1152 uint32_t NumLocalDecls = readVaruint32(Ctx); 1153 Function.Locals.reserve(NumLocalDecls); 1154 while (NumLocalDecls--) { 1155 wasm::WasmLocalDecl Decl; 1156 Decl.Count = readVaruint32(Ctx); 1157 Decl.Type = readUint8(Ctx); 1158 Function.Locals.push_back(Decl); 1159 } 1160 1161 uint32_t BodySize = FunctionEnd - Ctx.Ptr; 1162 Function.Body = ArrayRef<uint8_t>(Ctx.Ptr, BodySize); 1163 // This will be set later when reading in the linking metadata section. 1164 Function.Comdat = UINT32_MAX; 1165 Ctx.Ptr += BodySize; 1166 assert(Ctx.Ptr == FunctionEnd); 1167 Functions.push_back(Function); 1168 } 1169 if (Ctx.Ptr != Ctx.End) 1170 return make_error<GenericBinaryError>("Code section ended prematurely", 1171 object_error::parse_failed); 1172 return Error::success(); 1173 } 1174 1175 Error WasmObjectFile::parseElemSection(ReadContext &Ctx) { 1176 uint32_t Count = readVaruint32(Ctx); 1177 ElemSegments.reserve(Count); 1178 while (Count--) { 1179 wasm::WasmElemSegment Segment; 1180 Segment.TableIndex = readVaruint32(Ctx); 1181 if (Segment.TableIndex != 0) { 1182 return make_error<GenericBinaryError>("Invalid TableIndex", 1183 object_error::parse_failed); 1184 } 1185 if (Error Err = readInitExpr(Segment.Offset, Ctx)) 1186 return Err; 1187 uint32_t NumElems = readVaruint32(Ctx); 1188 while (NumElems--) { 1189 Segment.Functions.push_back(readVaruint32(Ctx)); 1190 } 1191 ElemSegments.push_back(Segment); 1192 } 1193 if (Ctx.Ptr != Ctx.End) 1194 return make_error<GenericBinaryError>("Elem section ended prematurely", 1195 object_error::parse_failed); 1196 return Error::success(); 1197 } 1198 1199 Error WasmObjectFile::parseDataSection(ReadContext &Ctx) { 1200 DataSection = Sections.size(); 1201 uint32_t Count = readVaruint32(Ctx); 1202 DataSegments.reserve(Count); 1203 while (Count--) { 1204 WasmSegment Segment; 1205 Segment.Data.InitFlags = readVaruint32(Ctx); 1206 Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) 1207 ? readVaruint32(Ctx) : 0; 1208 if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { 1209 if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) 1210 return Err; 1211 } else { 1212 Segment.Data.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST; 1213 Segment.Data.Offset.Value.Int32 = 0; 1214 } 1215 uint32_t Size = readVaruint32(Ctx); 1216 if (Size > (size_t)(Ctx.End - Ctx.Ptr)) 1217 return make_error<GenericBinaryError>("Invalid segment size", 1218 object_error::parse_failed); 1219 Segment.Data.Content = ArrayRef<uint8_t>(Ctx.Ptr, Size); 1220 // The rest of these Data fields are set later, when reading in the linking 1221 // metadata section. 1222 Segment.Data.Alignment = 0; 1223 Segment.Data.LinkerFlags = 0; 1224 Segment.Data.Comdat = UINT32_MAX; 1225 Segment.SectionOffset = Ctx.Ptr - Ctx.Start; 1226 Ctx.Ptr += Size; 1227 DataSegments.push_back(Segment); 1228 } 1229 if (Ctx.Ptr != Ctx.End) 1230 return make_error<GenericBinaryError>("Data section ended prematurely", 1231 object_error::parse_failed); 1232 return Error::success(); 1233 } 1234 1235 const wasm::WasmObjectHeader &WasmObjectFile::getHeader() const { 1236 return Header; 1237 } 1238 1239 void WasmObjectFile::moveSymbolNext(DataRefImpl &Symb) const { Symb.d.b++; } 1240 1241 uint32_t WasmObjectFile::getSymbolFlags(DataRefImpl Symb) const { 1242 uint32_t Result = SymbolRef::SF_None; 1243 const WasmSymbol &Sym = getWasmSymbol(Symb); 1244 1245 LLVM_DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); 1246 if (Sym.isBindingWeak()) 1247 Result |= SymbolRef::SF_Weak; 1248 if (!Sym.isBindingLocal()) 1249 Result |= SymbolRef::SF_Global; 1250 if (Sym.isHidden()) 1251 Result |= SymbolRef::SF_Hidden; 1252 if (!Sym.isDefined()) 1253 Result |= SymbolRef::SF_Undefined; 1254 if (Sym.isTypeFunction()) 1255 Result |= SymbolRef::SF_Executable; 1256 return Result; 1257 } 1258 1259 basic_symbol_iterator WasmObjectFile::symbol_begin() const { 1260 DataRefImpl Ref; 1261 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1262 Ref.d.b = 0; // Symbol index 1263 return BasicSymbolRef(Ref, this); 1264 } 1265 1266 basic_symbol_iterator WasmObjectFile::symbol_end() const { 1267 DataRefImpl Ref; 1268 Ref.d.a = 1; // Arbitrary non-zero value so that Ref.p is non-null 1269 Ref.d.b = Symbols.size(); // Symbol index 1270 return BasicSymbolRef(Ref, this); 1271 } 1272 1273 const WasmSymbol &WasmObjectFile::getWasmSymbol(const DataRefImpl &Symb) const { 1274 return Symbols[Symb.d.b]; 1275 } 1276 1277 const WasmSymbol &WasmObjectFile::getWasmSymbol(const SymbolRef &Symb) const { 1278 return getWasmSymbol(Symb.getRawDataRefImpl()); 1279 } 1280 1281 Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const { 1282 return getWasmSymbol(Symb).Info.Name; 1283 } 1284 1285 Expected<uint64_t> WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { 1286 auto &Sym = getWasmSymbol(Symb); 1287 if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && 1288 isDefinedFunctionIndex(Sym.Info.ElementIndex)) 1289 return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset; 1290 else 1291 return getSymbolValue(Symb); 1292 } 1293 1294 uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { 1295 switch (Sym.Info.Kind) { 1296 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1297 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1298 case wasm::WASM_SYMBOL_TYPE_EVENT: 1299 return Sym.Info.ElementIndex; 1300 case wasm::WASM_SYMBOL_TYPE_DATA: { 1301 // The value of a data symbol is the segment offset, plus the symbol 1302 // offset within the segment. 1303 uint32_t SegmentIndex = Sym.Info.DataRef.Segment; 1304 const wasm::WasmDataSegment &Segment = DataSegments[SegmentIndex].Data; 1305 assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST); 1306 return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset; 1307 } 1308 case wasm::WASM_SYMBOL_TYPE_SECTION: 1309 return 0; 1310 } 1311 llvm_unreachable("invalid symbol type"); 1312 } 1313 1314 uint64_t WasmObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1315 return getWasmSymbolValue(getWasmSymbol(Symb)); 1316 } 1317 1318 uint32_t WasmObjectFile::getSymbolAlignment(DataRefImpl Symb) const { 1319 llvm_unreachable("not yet implemented"); 1320 return 0; 1321 } 1322 1323 uint64_t WasmObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 1324 llvm_unreachable("not yet implemented"); 1325 return 0; 1326 } 1327 1328 Expected<SymbolRef::Type> 1329 WasmObjectFile::getSymbolType(DataRefImpl Symb) const { 1330 const WasmSymbol &Sym = getWasmSymbol(Symb); 1331 1332 switch (Sym.Info.Kind) { 1333 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1334 return SymbolRef::ST_Function; 1335 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1336 return SymbolRef::ST_Other; 1337 case wasm::WASM_SYMBOL_TYPE_DATA: 1338 return SymbolRef::ST_Data; 1339 case wasm::WASM_SYMBOL_TYPE_SECTION: 1340 return SymbolRef::ST_Debug; 1341 case wasm::WASM_SYMBOL_TYPE_EVENT: 1342 return SymbolRef::ST_Other; 1343 } 1344 1345 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1346 return SymbolRef::ST_Other; 1347 } 1348 1349 Expected<section_iterator> 1350 WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { 1351 const WasmSymbol &Sym = getWasmSymbol(Symb); 1352 if (Sym.isUndefined()) 1353 return section_end(); 1354 1355 DataRefImpl Ref; 1356 switch (Sym.Info.Kind) { 1357 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1358 Ref.d.a = CodeSection; 1359 break; 1360 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1361 Ref.d.a = GlobalSection; 1362 break; 1363 case wasm::WASM_SYMBOL_TYPE_DATA: 1364 Ref.d.a = DataSection; 1365 break; 1366 case wasm::WASM_SYMBOL_TYPE_SECTION: 1367 Ref.d.a = Sym.Info.ElementIndex; 1368 break; 1369 case wasm::WASM_SYMBOL_TYPE_EVENT: 1370 Ref.d.a = EventSection; 1371 break; 1372 default: 1373 llvm_unreachable("Unknown WasmSymbol::SymbolType"); 1374 } 1375 return section_iterator(SectionRef(Ref, this)); 1376 } 1377 1378 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } 1379 1380 std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, 1381 StringRef &Res) const { 1382 const WasmSection &S = Sections[Sec.d.a]; 1383 #define ECase(X) \ 1384 case wasm::WASM_SEC_##X: \ 1385 Res = #X; \ 1386 break 1387 switch (S.Type) { 1388 ECase(TYPE); 1389 ECase(IMPORT); 1390 ECase(FUNCTION); 1391 ECase(TABLE); 1392 ECase(MEMORY); 1393 ECase(GLOBAL); 1394 ECase(EVENT); 1395 ECase(EXPORT); 1396 ECase(START); 1397 ECase(ELEM); 1398 ECase(CODE); 1399 ECase(DATA); 1400 case wasm::WASM_SEC_CUSTOM: 1401 Res = S.Name; 1402 break; 1403 default: 1404 return object_error::invalid_section_index; 1405 } 1406 #undef ECase 1407 return std::error_code(); 1408 } 1409 1410 uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; } 1411 1412 uint64_t WasmObjectFile::getSectionIndex(DataRefImpl Sec) const { 1413 return Sec.d.a; 1414 } 1415 1416 uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { 1417 const WasmSection &S = Sections[Sec.d.a]; 1418 return S.Content.size(); 1419 } 1420 1421 std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, 1422 StringRef &Res) const { 1423 const WasmSection &S = Sections[Sec.d.a]; 1424 // This will never fail since wasm sections can never be empty (user-sections 1425 // must have a name and non-user sections each have a defined structure). 1426 Res = StringRef(reinterpret_cast<const char *>(S.Content.data()), 1427 S.Content.size()); 1428 return std::error_code(); 1429 } 1430 1431 uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { 1432 return 1; 1433 } 1434 1435 bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { 1436 return false; 1437 } 1438 1439 bool WasmObjectFile::isSectionText(DataRefImpl Sec) const { 1440 return getWasmSection(Sec).Type == wasm::WASM_SEC_CODE; 1441 } 1442 1443 bool WasmObjectFile::isSectionData(DataRefImpl Sec) const { 1444 return getWasmSection(Sec).Type == wasm::WASM_SEC_DATA; 1445 } 1446 1447 bool WasmObjectFile::isSectionBSS(DataRefImpl Sec) const { return false; } 1448 1449 bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } 1450 1451 bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } 1452 1453 relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { 1454 DataRefImpl RelocRef; 1455 RelocRef.d.a = Ref.d.a; 1456 RelocRef.d.b = 0; 1457 return relocation_iterator(RelocationRef(RelocRef, this)); 1458 } 1459 1460 relocation_iterator WasmObjectFile::section_rel_end(DataRefImpl Ref) const { 1461 const WasmSection &Sec = getWasmSection(Ref); 1462 DataRefImpl RelocRef; 1463 RelocRef.d.a = Ref.d.a; 1464 RelocRef.d.b = Sec.Relocations.size(); 1465 return relocation_iterator(RelocationRef(RelocRef, this)); 1466 } 1467 1468 void WasmObjectFile::moveRelocationNext(DataRefImpl &Rel) const { Rel.d.b++; } 1469 1470 uint64_t WasmObjectFile::getRelocationOffset(DataRefImpl Ref) const { 1471 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1472 return Rel.Offset; 1473 } 1474 1475 symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { 1476 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1477 if (Rel.Type == wasm::R_WASM_TYPE_INDEX_LEB) 1478 return symbol_end(); 1479 DataRefImpl Sym; 1480 Sym.d.a = 1; 1481 Sym.d.b = Rel.Index; 1482 return symbol_iterator(SymbolRef(Sym, this)); 1483 } 1484 1485 uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { 1486 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1487 return Rel.Type; 1488 } 1489 1490 void WasmObjectFile::getRelocationTypeName( 1491 DataRefImpl Ref, SmallVectorImpl<char> &Result) const { 1492 const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); 1493 StringRef Res = "Unknown"; 1494 1495 #define WASM_RELOC(name, value) \ 1496 case wasm::name: \ 1497 Res = #name; \ 1498 break; 1499 1500 switch (Rel.Type) { 1501 #include "llvm/BinaryFormat/WasmRelocs.def" 1502 } 1503 1504 #undef WASM_RELOC 1505 1506 Result.append(Res.begin(), Res.end()); 1507 } 1508 1509 section_iterator WasmObjectFile::section_begin() const { 1510 DataRefImpl Ref; 1511 Ref.d.a = 0; 1512 return section_iterator(SectionRef(Ref, this)); 1513 } 1514 1515 section_iterator WasmObjectFile::section_end() const { 1516 DataRefImpl Ref; 1517 Ref.d.a = Sections.size(); 1518 return section_iterator(SectionRef(Ref, this)); 1519 } 1520 1521 uint8_t WasmObjectFile::getBytesInAddress() const { return 4; } 1522 1523 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; } 1524 1525 Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; } 1526 1527 SubtargetFeatures WasmObjectFile::getFeatures() const { 1528 return SubtargetFeatures(); 1529 } 1530 1531 bool WasmObjectFile::isRelocatableObject() const { return HasLinkingSection; } 1532 1533 bool WasmObjectFile::isSharedObject() const { return HasDylinkSection; } 1534 1535 const WasmSection &WasmObjectFile::getWasmSection(DataRefImpl Ref) const { 1536 assert(Ref.d.a < Sections.size()); 1537 return Sections[Ref.d.a]; 1538 } 1539 1540 const WasmSection & 1541 WasmObjectFile::getWasmSection(const SectionRef &Section) const { 1542 return getWasmSection(Section.getRawDataRefImpl()); 1543 } 1544 1545 const wasm::WasmRelocation & 1546 WasmObjectFile::getWasmRelocation(const RelocationRef &Ref) const { 1547 return getWasmRelocation(Ref.getRawDataRefImpl()); 1548 } 1549 1550 const wasm::WasmRelocation & 1551 WasmObjectFile::getWasmRelocation(DataRefImpl Ref) const { 1552 assert(Ref.d.a < Sections.size()); 1553 const WasmSection &Sec = Sections[Ref.d.a]; 1554 assert(Ref.d.b < Sec.Relocations.size()); 1555 return Sec.Relocations[Ref.d.b]; 1556 } 1557 1558 int WasmSectionOrderChecker::getSectionOrder(unsigned ID, 1559 StringRef CustomSectionName) { 1560 switch (ID) { 1561 case wasm::WASM_SEC_CUSTOM: 1562 return StringSwitch<unsigned>(CustomSectionName) 1563 .Case("dylink", WASM_SEC_ORDER_DYLINK) 1564 .Case("linking", WASM_SEC_ORDER_LINKING) 1565 .StartsWith("reloc.", WASM_SEC_ORDER_RELOC) 1566 .Case("name", WASM_SEC_ORDER_NAME) 1567 .Case("producers", WASM_SEC_ORDER_PRODUCERS) 1568 .Case("target_features", WASM_SEC_ORDER_TARGET_FEATURES) 1569 .Default(WASM_SEC_ORDER_NONE); 1570 case wasm::WASM_SEC_TYPE: 1571 return WASM_SEC_ORDER_TYPE; 1572 case wasm::WASM_SEC_IMPORT: 1573 return WASM_SEC_ORDER_IMPORT; 1574 case wasm::WASM_SEC_FUNCTION: 1575 return WASM_SEC_ORDER_FUNCTION; 1576 case wasm::WASM_SEC_TABLE: 1577 return WASM_SEC_ORDER_TABLE; 1578 case wasm::WASM_SEC_MEMORY: 1579 return WASM_SEC_ORDER_MEMORY; 1580 case wasm::WASM_SEC_GLOBAL: 1581 return WASM_SEC_ORDER_GLOBAL; 1582 case wasm::WASM_SEC_EXPORT: 1583 return WASM_SEC_ORDER_EXPORT; 1584 case wasm::WASM_SEC_START: 1585 return WASM_SEC_ORDER_START; 1586 case wasm::WASM_SEC_ELEM: 1587 return WASM_SEC_ORDER_ELEM; 1588 case wasm::WASM_SEC_CODE: 1589 return WASM_SEC_ORDER_CODE; 1590 case wasm::WASM_SEC_DATA: 1591 return WASM_SEC_ORDER_DATA; 1592 case wasm::WASM_SEC_DATACOUNT: 1593 return WASM_SEC_ORDER_DATACOUNT; 1594 case wasm::WASM_SEC_EVENT: 1595 return WASM_SEC_ORDER_EVENT; 1596 default: 1597 llvm_unreachable("invalid section"); 1598 } 1599 } 1600 1601 // Represents the edges in a directed graph where any node B reachable from node 1602 // A is not allowed to appear before A in the section ordering, but may appear 1603 // afterward. 1604 int WasmSectionOrderChecker::DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = { 1605 {}, // WASM_SEC_ORDER_NONE 1606 {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, // WASM_SEC_ORDER_TYPE, 1607 {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, // WASM_SEC_ORDER_IMPORT, 1608 {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, // WASM_SEC_ORDER_FUNCTION, 1609 {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, // WASM_SEC_ORDER_TABLE, 1610 {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_GLOBAL}, // WASM_SEC_ORDER_MEMORY, 1611 {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EVENT}, // WASM_SEC_ORDER_GLOBAL, 1612 {WASM_SEC_ORDER_EVENT, WASM_SEC_ORDER_EXPORT}, // WASM_SEC_ORDER_EVENT, 1613 {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, // WASM_SEC_ORDER_EXPORT, 1614 {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, // WASM_SEC_ORDER_START, 1615 {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, // WASM_SEC_ORDER_ELEM, 1616 {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, // WASM_SEC_ORDER_DATACOUNT, 1617 {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, // WASM_SEC_ORDER_CODE, 1618 {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, // WASM_SEC_ORDER_DATA, 1619 1620 // Custom Sections 1621 {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, // WASM_SEC_ORDER_DYLINK, 1622 {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, // WASM_SEC_ORDER_LINKING, 1623 {}, // WASM_SEC_ORDER_RELOC (can be repeated), 1624 {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, // WASM_SEC_ORDER_NAME, 1625 {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES}, // WASM_SEC_ORDER_PRODUCERS, 1626 {WASM_SEC_ORDER_TARGET_FEATURES} // WASM_SEC_ORDER_TARGET_FEATURES 1627 }; 1628 1629 bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID, 1630 StringRef CustomSectionName) { 1631 int Order = getSectionOrder(ID, CustomSectionName); 1632 if (Order == WASM_SEC_ORDER_NONE) 1633 return true; 1634 1635 // Disallowed predecessors we need to check for 1636 SmallVector<int, WASM_NUM_SEC_ORDERS> WorkList; 1637 1638 // Keep track of completed checks to avoid repeating work 1639 bool Checked[WASM_NUM_SEC_ORDERS] = {}; 1640 1641 int Curr = Order; 1642 while (true) { 1643 // Add new disallowed predecessors to work list 1644 for (size_t I = 0;; ++I) { 1645 int Next = DisallowedPredecessors[Curr][I]; 1646 if (Next == WASM_SEC_ORDER_NONE) 1647 break; 1648 if (Checked[Next]) 1649 continue; 1650 WorkList.push_back(Next); 1651 Checked[Next] = true; 1652 } 1653 1654 if (WorkList.empty()) 1655 break; 1656 1657 // Consider next disallowed predecessor 1658 Curr = WorkList.pop_back_val(); 1659 if (Seen[Curr]) 1660 return false; 1661 } 1662 1663 // Have not seen any disallowed predecessors 1664 Seen[Order] = true; 1665 return true; 1666 } 1667