diff options
author | Lucas Duarte Prates <lucas.prates@arm.com> | 2023-12-20 10:09:29 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-20 10:09:29 +0000 |
commit | b652674dd0252b09a3101d8f7a2a4fc73675ac8d (patch) | |
tree | 2fb444a9027e1142e920d0accd4ee25ec77be4bf | |
parent | 9d60e95bcdce44fcf592bfcc9f847640b1bf6e5d (diff) | |
download | llvm-b652674dd0252b09a3101d8f7a2a4fc73675ac8d.zip llvm-b652674dd0252b09a3101d8f7a2a4fc73675ac8d.tar.gz llvm-b652674dd0252b09a3101d8f7a2a4fc73675ac8d.tar.bz2 |
[AsmWriter] Ensure getMnemonic doesn't return invalid pointers (#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 #74177.
-rw-r--r-- | llvm/lib/MC/MCAsmStreamer.cpp | 5 | ||||
-rw-r--r-- | llvm/utils/TableGen/AsmWriterEmitter.cpp | 4 |
2 files changed, 8 insertions, 1 deletions
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index 9e1d108..49668de 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -154,7 +154,10 @@ public: 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; diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp index 0220927..e0cd5fa 100644 --- a/llvm/utils/TableGen/AsmWriterEmitter.cpp +++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp @@ -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"; |