[libc-commits] [flang] [clang] [mlir] [clang-tools-extra] [libcxx] [libc] [compiler-rt] [llvm] [AsmWriter] Ensure getMnemonic doesn't return invalid pointers (PR #75783)
Lucas Duarte Prates via libc-commits
libc-commits at lists.llvm.org
Tue Dec 19 05:20:09 PST 2023
https://github.com/pratlucas updated https://github.com/llvm/llvm-project/pull/75783
>From 62f4b5daeca0be5b463c5c42271a4b997f084523 Mon Sep 17 00:00:00 2001
From: Lucas Prates <lucas.prates at arm.com>
Date: Mon, 18 Dec 2023 10:49:25 +0000
Subject: [PATCH 1/2] [AsmWriter] Ensure getMnemonic doesn't return invalid
pointers
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.
---
llvm/lib/MC/MCAsmStreamer.cpp | 5 ++++-
llvm/utils/TableGen/AsmWriterEmitter.cpp | 4 ++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 9e1d108ac14dc5..532ac89bf9ff76 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -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;
+ std::pair<const char *, uint64_t> M = InstPrinter->getMnemonic(&MI);
+ assert((M.second != 0 || M.first == nullptr) &&
+ "Invalid char pointer for instruction with no mnemonic");
+ return M.first;
}
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
index 0220927295cf78..e0cd5fad3254de 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";
>From 1326609f665f6f202301bc6f7945d49e3cd1b72c Mon Sep 17 00:00:00 2001
From: Lucas Prates <lucas.prates at arm.com>
Date: Mon, 18 Dec 2023 11:59:26 +0000
Subject: [PATCH 2/2] fixup! [AsmWriter] Ensure getMnemonic doesn't return
invalid pointers
---
llvm/lib/MC/MCAsmStreamer.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 532ac89bf9ff76..49668de27d67e7 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -154,10 +154,10 @@ class MCAsmStreamer final : public MCStreamer {
void emitGNUAttribute(unsigned Tag, unsigned Value) override;
StringRef getMnemonic(MCInst &MI) override {
- std::pair<const char *, uint64_t> M = InstPrinter->getMnemonic(&MI);
- assert((M.second != 0 || M.first == nullptr) &&
+ auto [Ptr, Bits] = InstPrinter->getMnemonic(&MI);
+ assert((Bits != 0 || Ptr == nullptr) &&
"Invalid char pointer for instruction with no mnemonic");
- return M.first;
+ return Ptr;
}
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
More information about the libc-commits
mailing list