[PATCH] D63274: [RISCV] Avoid overflow when determining number of nops for code align

Edward Jones via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 13 08:00:40 PDT 2019


edward-jones created this revision.
edward-jones added reviewers: asb, lewis-revill.
Herald added subscribers: llvm-commits, Jim, benna, psnobl, jocewei, PkmX, rkruppe, the_o, brucehoult, MartinMosbeck, rogfer01, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, apazos, simoncook, johnrusso, rbar.
Herald added a project: LLVM.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D63274

Files:
  lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
  test/MC/RISCV/align.s


Index: test/MC/RISCV/align.s
===================================================================
--- test/MC/RISCV/align.s
+++ test/MC/RISCV/align.s
@@ -90,6 +90,13 @@
 	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
Index: lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
===================================================================
--- lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -317,7 +317,7 @@
   bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
   unsigned MinNopLen = HasStdExtC ? 2 : 4;
 
-  Size = AF.getAlignment() - MinNopLen;
+  Size = (AF.getAlignment() < MinNopLen) ? 0 : (AF.getAlignment() - MinNopLen);
   return true;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63274.204541.patch
Type: text/x-patch
Size: 1167 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190613/99070328/attachment.bin>


More information about the llvm-commits mailing list