From: Alex Bradbury Date: Tue, 16 Jul 2019 04:40:25 +0000 (+0000) Subject: [RISCV] Avoid overflow when determining number of nops for code align X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c052b6489a258faf29a8654fa207501e3304a71c;p=llvm [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366176 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp index 821ac2033c9..ee5f760ebcb 100644 --- a/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp +++ b/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp @@ -313,8 +313,12 @@ bool RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign( 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 diff --git a/test/MC/RISCV/align.s b/test/MC/RISCV/align.s index e62af931555..b4b3e6aa778 100644 --- a/test/MC/RISCV/align.s +++ b/test/MC/RISCV/align.s @@ -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