Skip to content

Commit b652674

Browse files
authored
[AsmWriter] Ensure getMnemonic doesn't return invalid pointers (llvm#75783)
For instructions that don't map to a mnemonic string, the implementation of MCInstPrinter::getMnemonic would return an invalid pointer due to the result of the calculation of the instruction's position in the `AsmStrs` table. This patch fixes the issue by ensuring those cases return a `nullptr` value instead. Fixes llvm#74177.
1 parent 9d60e95 commit b652674

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

llvm/lib/MC/MCAsmStreamer.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ class MCAsmStreamer final : public MCStreamer {
154154
void emitGNUAttribute(unsigned Tag, unsigned Value) override;
155155

156156
StringRef getMnemonic(MCInst &MI) override {
157-
return InstPrinter->getMnemonic(&MI).first;
157+
auto [Ptr, Bits] = InstPrinter->getMnemonic(&MI);
158+
assert((Bits != 0 || Ptr == nullptr) &&
159+
"Invalid char pointer for instruction with no mnemonic");
160+
return Ptr;
158161
}
159162

160163
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;

llvm/utils/TableGen/AsmWriterEmitter.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ void AsmWriterEmitter::EmitGetMnemonic(
438438
O << " // Emit the opcode for the instruction.\n";
439439
O << BitsString;
440440

441+
// Make sure we don't return an invalid pointer if bits is 0
442+
O << " if (Bits == 0)\n"
443+
" return {nullptr, Bits};\n";
444+
441445
// Return mnemonic string and bits.
442446
O << " return {AsmStrs+(Bits & " << (1 << AsmStrBits) - 1
443447
<< ")-1, Bits};\n\n";

0 commit comments

Comments
 (0)