Index: lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp =================================================================== --- lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp +++ lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp @@ -127,9 +127,13 @@ } if (!WasmInst) return MCDisassembler::Fail; - Opc = nextByte(Bytes, Size); - if (Opc < 0) + unsigned N = 0; + const char *Error = nullptr; + Opc = decodeULEB128(Bytes.data() + Size, &N, Bytes.data() + Bytes.size(), + &Error); + if (Error || Opc < 0 || Opc >= WebAssemblyInstructionTableSize) return MCDisassembler::Fail; + Size += N; WasmInst += Opc; } if (WasmInst->ET == ET_Unused) Index: test/MC/Disassembler/WebAssembly/wasm.txt =================================================================== --- test/MC/Disassembler/WebAssembly/wasm.txt +++ test/MC/Disassembler/WebAssembly/wasm.txt @@ -37,3 +37,12 @@ # CHECK: v8x16.shuffle 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 0xFD 0x03 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F + +# Check LEB128 encoding of SIMD instructions +# CHECK: i64x2.all_true +0xFD 0x8C 0x01 + +# Including non-canonical LEB128 encodings +# CHECK: i64x2.any_true +# CHECK-NOT: i64.div_u +0xFD 0x8B 0x81 0x80 0x80 0x80 0x80 0x00 Index: utils/TableGen/WebAssemblyDisassemblerEmitter.cpp =================================================================== --- utils/TableGen/WebAssemblyDisassemblerEmitter.cpp +++ utils/TableGen/WebAssemblyDisassemblerEmitter.cpp @@ -19,6 +19,8 @@ namespace llvm { +static constexpr int WebAssemblyInstructionTableSize = 256; + void emitWebAssemblyDisassemblerTables( raw_ostream &OS, const ArrayRef &NumberedInstructions) { @@ -59,6 +61,8 @@ OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n"; OS << "\n"; OS << "namespace llvm {\n\n"; + OS << "static constexpr int WebAssemblyInstructionTableSize = "; + OS << WebAssemblyInstructionTableSize << ";\n\n"; OS << "enum EntryType : uint8_t { "; OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n"; OS << "struct WebAssemblyInstruction {\n"; @@ -74,7 +78,7 @@ continue; OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first; OS << "[] = {\n"; - for (unsigned I = 0; I <= 0xFF; I++) { + for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) { auto InstIt = PrefixPair.second.find(I); if (InstIt != PrefixPair.second.end()) { // Regular instruction.