Skip to content

[Hexagon][llvm-objdump] Fix crash at a truncated instruction #142082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025

Conversation

quic-akaryaki
Copy link
Contributor

Fixes #141740.

Fixes llvm#141740.

Co-authored-by: Sudharsan Veeravalli <quic_svs@quicinc.com>
@llvmbot
Copy link
Member

llvmbot commented May 30, 2025

@llvm/pr-subscribers-llvm-binary-utilities

@llvm/pr-subscribers-backend-hexagon

Author: Alexey Karyakin (quic-akaryaki)

Changes

Fixes #141740.


Full diff: https://github.com/llvm/llvm-project/pull/142082.diff

2 Files Affected:

  • (added) llvm/test/tools/llvm-objdump/ELF/Hexagon/truncated-inst.yaml (+26)
  • (modified) llvm/tools/llvm-objdump/llvm-objdump.cpp (+8-4)
diff --git a/llvm/test/tools/llvm-objdump/ELF/Hexagon/truncated-inst.yaml b/llvm/test/tools/llvm-objdump/ELF/Hexagon/truncated-inst.yaml
new file mode 100644
index 0000000000000..26481b9a13216
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/Hexagon/truncated-inst.yaml
@@ -0,0 +1,26 @@
+## Test disassembling of truncated instructions.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objdump --disassemble-all %t 2>&1 | FileCheck %s
+
+# CHECK:      0000000 <.data>:
+# CHECK-NEXT:       0: 55 <unknown>
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_HEXAGON
+Sections:
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x1
+    Content:         55
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .data
+      - Name:            .strtab
+      - Name:            .shstrtab
+...
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 85c455b2891a1..5ecb33375943f 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -700,14 +700,18 @@ class HexagonPrettyPrinter : public PrettyPrinter {
 public:
   void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
                  formatted_raw_ostream &OS) {
-    uint32_t opcode =
-      (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
     if (LeadingAddr)
       OS << format("%8" PRIx64 ":", Address);
     if (ShowRawInsn) {
       OS << "\t";
-      dumpBytes(Bytes.slice(0, 4), OS);
-      OS << format("\t%08" PRIx32, opcode);
+      if (Bytes.size() >= 4) {
+        dumpBytes(Bytes.slice(0, 4), OS);
+        uint32_t opcode =
+            (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
+        OS << format("\t%08" PRIx32, opcode);
+      } else {
+        dumpBytes(Bytes, OS);
+      }
     }
   }
   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,

Copy link
Contributor

@svs-quic svs-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@androm3da
Copy link
Member

LLVM :: tools/llvm-dwarfdump/X86/debug_info_crel.yaml -- this failure, is it on the baseline? Or could it be related to this change?

@svs-quic
Copy link
Contributor

LLVM :: tools/llvm-dwarfdump/X86/debug_info_crel.yaml -- this failure, is it on the baseline? Or could it be related to this change?

I think it is on the baseline. The changes in this patch are to a Hexagon specific part of the code and should not be affecting other targets.

@quic-akaryaki
Copy link
Contributor Author

tools/llvm-dwarfdump/X86/debug_info_crel.yaml

This test fails for me locally on the baseline commit too.

@quic-akaryaki quic-akaryaki merged commit c87edaf into llvm:main May 30, 2025
11 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Hexagon] llvm-objdump -D crashes in .debug_info on a Hexagon binary
4 participants