Skip to content

Commit

Permalink
[AsmWriter] Ensure getMnemonic doesn't return invalid pointers (llvm#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
pratlucas committed Dec 20, 2023
1 parent 9d60e95 commit b652674
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion llvm/lib/MC/MCAsmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ class MCAsmStreamer final : public MCStreamer {
void emitGNUAttribute(unsigned Tag, unsigned Value) override;

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

void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
Expand Down
4 changes: 4 additions & 0 deletions llvm/utils/TableGen/AsmWriterEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ void AsmWriterEmitter::EmitGetMnemonic(
O << " // Emit the opcode for the instruction.\n";
O << BitsString;

// Make sure we don't return an invalid pointer if bits is 0
O << " if (Bits == 0)\n"
" return {nullptr, Bits};\n";

// Return mnemonic string and bits.
O << " return {AsmStrs+(Bits & " << (1 << AsmStrBits) - 1
<< ")-1, Bits};\n\n";
Expand Down

0 comments on commit b652674

Please sign in to comment.