[llvm] r366176 - [RISCV] Avoid overflow when determining number of nops for code align

Alex Bradbury via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 21:40:25 PDT 2019


Author: asb
Date: Mon Jul 15 21:40:25 2019
New Revision: 366176

URL: http://llvm.org/viewvc/llvm-project?rev=366176&view=rev
Log:
[RISCV] Avoid overflow when determining number of nops for code align

RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign() assumed that the
align specified would be greater than or equal to the minimum nop length, but
that is not always the case - for example if a user specifies ".align 0" in
assembly.

Differential Revision: https://reviews.llvm.org/D63274
Patch by Edward Jones.


Modified:
    llvm/trunk/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
    llvm/trunk/test/MC/RISCV/align.s

Modified: llvm/trunk/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp?rev=366176&r1=366175&r2=366176&view=diff
==============================================================================
--- llvm/trunk/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp (original)
+++ llvm/trunk/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp Mon Jul 15 21:40:25 2019
@@ -313,8 +313,12 @@ bool RISCVAsmBackend::shouldInsertExtraN
   bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
   unsigned MinNopLen = HasStdExtC ? 2 : 4;
 
-  Size = AF.getAlignment() - MinNopLen;
-  return true;
+  if (AF.getAlignment() <= MinNopLen) {
+    return false;
+  } else {
+    Size = AF.getAlignment() - MinNopLen;
+    return true;
+  }
 }
 
 // We need to insert R_RISCV_ALIGN relocation type to indicate the

Modified: llvm/trunk/test/MC/RISCV/align.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/RISCV/align.s?rev=366176&r1=366175&r2=366176&view=diff
==============================================================================
--- llvm/trunk/test/MC/RISCV/align.s (original)
+++ llvm/trunk/test/MC/RISCV/align.s Mon Jul 15 21:40:25 2019
@@ -90,6 +90,13 @@ test:
 	ret
 # NORELAX-RELOC-NOT: R_RISCV
 # C-EXT-NORELAX-RELOC-NOT: R_RISCV
+# Code alignment of a byte size less than the size of a nop must be treated
+# as no alignment. This used to trigger a fatal error with relaxation enabled
+# as the calculation to emit the worst-case sequence of nops would overflow.
+	.p2align        1
+	add	a0, a0, a1
+	.p2align        0
+	add	a0, a0, a1
 # We only need to insert R_RISCV_ALIGN for code section
 # when the linker relaxation enabled.
         .data




More information about the llvm-commits mailing list