[PATCH] D147931: [ARM] Fix null pointer dereferences in ARMMachObjectWriter::recordRelocation()

Alexey Vishnyakov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 10 04:28:35 PDT 2023


SweetVishnya created this revision.
SweetVishnya added reviewers: MaskRay, sbc100, chandlerc, SjoerdMeijer, samtebbs, ostannard, dmgreen.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
SweetVishnya requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Bugs were found by Svace static analysis tool. A can be a null pointer.
It is checked in some places. However, there are still some missing
checks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147931

Files:
  llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp


Index: llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
===================================================================
--- llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
+++ llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
@@ -434,7 +434,7 @@
                        "not yet implemented");
   } else {
     // Resolve constant variables.
-    if (A->isVariable()) {
+    if (A && A->isVariable()) {
       int64_t Res;
       if (A->getVariableValue()->evaluateAsAbsolute(
               Res, Layout, Writer->getSectionAddressMap())) {
@@ -444,20 +444,22 @@
     }
 
     // Check whether we need an external or internal relocation.
-    if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A,
-                                 FixedValue)) {
-      RelSymbol = A;
-
-      // For external relocations, make sure to offset the fixup value to
-      // compensate for the addend of the symbol address, if it was
-      // undefined. This occurs with weak definitions, for example.
-      if (!A->isUndefined())
-        FixedValue -= Layout.getSymbolOffset(*A);
-    } else {
-      // The index is the section ordinal (1-based).
-      const MCSection &Sec = A->getSection();
-      Index = Sec.getOrdinal() + 1;
-      FixedValue += Writer->getSectionAddress(&Sec);
+    if (A) {
+      if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A,
+                                   FixedValue)) {
+        RelSymbol = A;
+
+        // For external relocations, make sure to offset the fixup value to
+        // compensate for the addend of the symbol address, if it was
+        // undefined. This occurs with weak definitions, for example.
+        if (!A->isUndefined())
+          FixedValue -= Layout.getSymbolOffset(*A);
+      } else {
+        // The index is the section ordinal (1-based).
+        const MCSection &Sec = A->getSection();
+        Index = Sec.getOrdinal() + 1;
+        FixedValue += Writer->getSectionAddress(&Sec);
+      }
     }
     if (IsPCRel)
       FixedValue -= Writer->getSectionAddress(Fragment->getParent());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147931.512115.patch
Type: text/x-patch
Size: 2102 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230410/605ed783/attachment.bin>


More information about the llvm-commits mailing list