Skip to content

Commit fede531

Browse files
pratlucasAlgunenano
authored andcommitted
[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 2568a7c commit fede531

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ class MCAsmStreamer final : public MCStreamer {
153153
void emitGNUAttribute(unsigned Tag, unsigned Value) override;
154154

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

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

llvm/utils/TableGen/AsmWriterEmitter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ void AsmWriterEmitter::EmitGetMnemonic(
437437
O << " // Emit the opcode for the instruction.\n";
438438
O << BitsString;
439439

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

0 commit comments

Comments
 (0)