[llvm] [MachO] Detect overflow in section offset. (PR #98685)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 12:31:11 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mc

Author: Eli Friedman (efriedma-quic)

<details>
<summary>Changes</summary>

The section offset field is only 32 bits; if the computed section offset is larger, make sure we don't emit a corrupt object file.

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


2 Files Affected:

- (modified) llvm/lib/MC/MachObjectWriter.cpp (+2) 
- (added) llvm/test/MC/MachO/section-offset-overflow.s (+9) 


``````````diff
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 53eed0092a5b4..f890ecc1d20ee 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -277,6 +277,8 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm,
     W.write<uint32_t>(VMAddr);      // address
     W.write<uint32_t>(SectionSize); // size
   }
+  if (!isUInt<32>(FileOffset))
+    report_fatal_error("Cannot encode offset of section");
   W.write<uint32_t>(FileOffset);
 
   W.write<uint32_t>(Log2(Section.getAlign()));
diff --git a/llvm/test/MC/MachO/section-offset-overflow.s b/llvm/test/MC/MachO/section-offset-overflow.s
new file mode 100644
index 0000000000000..51fc90c2e3479
--- /dev/null
+++ b/llvm/test/MC/MachO/section-offset-overflow.s
@@ -0,0 +1,9 @@
+// RUN: not --crash llvm-mc -triple x86_64-apple-macosx -filetype=obj -o /dev/null %s 2>&1 | FileCheck  %s
+
+// CHECK: Cannot encode offset of section
+
+        .data
+        .long 1
+        .zero 0x100000000
+        .const
+        .long 1

``````````

</details>


https://github.com/llvm/llvm-project/pull/98685


More information about the llvm-commits mailing list