<div dir="ltr">This is now firing on code in brotli (a compression library): <a href="http://crbug.com/637864">http://crbug.com/637864</a><div><br></div><div>How can we avoid this error? Shouldn't LLVM be responsible for relaxing this into some other code sequence?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 15, 2016 at 12:57 AM, Prakhar Bahuguna via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: prakhar<br>
Date: Mon Aug 15 02:57:44 2016<br>
New Revision: 278659<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=278659&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=278659&view=rev</a><br>
Log:<br>
[Thumb] Validate branch target for CBZ/CBNZ instructions.<br>
<br>
Summary:<br>
The assembler currently does not check the branch target for CBZ/CBNZ<br>
instructions, which only permit branching forwards with a positive offset. This<br>
adds validation for the branch target to ensure negative PC-relative offsets are<br>
not encoded into the instruction, whether specified as a literal or as an<br>
assembler symbol.<br>
<br>
Reviewers: rengolin, t.p.northover<br>
<br>
Subscribers: llvm-commits, rengolin<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D23312" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D23312</a><br>
<br>
Added:<br>
    llvm/trunk/test/MC/ARM/thumb-<wbr>cb-negative-offsets.s<br>
Modified:<br>
    llvm/trunk/lib/Target/ARM/<wbr>AsmParser/ARMAsmParser.cpp<br>
    llvm/trunk/lib/Target/ARM/<wbr>MCTargetDesc/ARMAsmBackend.cpp<br>
    llvm/trunk/test/MC/ARM/thumb-<wbr>diagnostics.s<br>
<br>
Modified: llvm/trunk/lib/Target/ARM/<wbr>AsmParser/ARMAsmParser.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/AsmParser/ARMAsmParser.cpp?rev=278659&r1=278658&r2=278659&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/Target/<wbr>ARM/AsmParser/ARMAsmParser.<wbr>cpp?rev=278659&r1=278658&r2=<wbr>278659&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Target/ARM/<wbr>AsmParser/ARMAsmParser.cpp (original)<br>
+++ llvm/trunk/lib/Target/ARM/<wbr>AsmParser/ARMAsmParser.cpp Mon Aug 15 02:57:44 2016<br>
@@ -6684,6 +6684,12 @@ bool ARMAsmParser::<wbr>validateInstruction(M<br>
       return Error(Operands[Op]-><wbr>getStartLoc(), "branch target out of range");<br>
     break;<br>
   }<br>
+  case ARM::tCBZ:<br>
+  case ARM::tCBNZ: {<br>
+    if (!static_cast<ARMOperand &>(*Operands[2]).<wbr>isUnsignedOffset<6, 1>())<br>
+      return Error(Operands[2]-><wbr>getStartLoc(), "branch target out of range");<br>
+    break;<br>
+  }<br>
   case ARM::MOVi16:<br>
   case ARM::t2MOVi16:<br>
   case ARM::t2MOVTi16:<br>
<br>
Modified: llvm/trunk/lib/Target/ARM/<wbr>MCTargetDesc/ARMAsmBackend.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp?rev=278659&r1=278658&r2=278659&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/Target/<wbr>ARM/MCTargetDesc/<wbr>ARMAsmBackend.cpp?rev=278659&<wbr>r1=278658&r2=278659&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Target/ARM/<wbr>MCTargetDesc/ARMAsmBackend.cpp (original)<br>
+++ llvm/trunk/lib/Target/ARM/<wbr>MCTargetDesc/ARMAsmBackend.cpp Mon Aug 15 02:57:44 2016<br>
@@ -578,6 +578,11 @@ unsigned ARMAsmBackend::<wbr>adjustFixupValue<br>
     // Offset by 4, and don't encode the low two bits.<br>
     return ((Value - 4) >> 2) & 0xff;<br>
   case ARM::fixup_arm_thumb_cb: {<br>
+    // CB instructions can only branch to offsets in [0, 126] in multiples of 2<br>
+    if (Ctx && ((int64_t)Value < 0 || Value > 0x3e || Value & 1)) {<br>
+      Ctx->reportError(Fixup.getLoc(<wbr>), "out of range pc-relative fixup value");<br>
+      return 0;<br>
+    }<br>
     // Offset by 4 and don't encode the lower bit, which is always 0.<br>
     // FIXME: diagnose if no Thumb2<br>
     uint32_t Binary = (Value - 4) >> 1;<br>
<br>
Added: llvm/trunk/test/MC/ARM/thumb-<wbr>cb-negative-offsets.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ARM/thumb-cb-negative-offsets.s?rev=278659&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/MC/<wbr>ARM/thumb-cb-negative-offsets.<wbr>s?rev=278659&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/MC/ARM/thumb-<wbr>cb-negative-offsets.s (added)<br>
+++ llvm/trunk/test/MC/ARM/thumb-<wbr>cb-negative-offsets.s Mon Aug 15 02:57:44 2016<br>
@@ -0,0 +1,19 @@<br>
+@ RUN: not llvm-mc -triple thumbv7m-none-eabi      -filetype=obj -o /dev/null %s 2>&1 | FileCheck %s<br>
+@ RUN: not llvm-mc -triple thumbv8m.base-none-eabi -filetype=obj -o /dev/null %s 2>&1 | FileCheck %s<br>
+<br>
+label0:<br>
+  .word 4<br>
+<br>
+@ CHECK: out of range pc-relative fixup value<br>
+  cbz r0, label0<br>
+@ CHECK: out of range pc-relative fixup value<br>
+  cbnz r0, label0<br>
+<br>
+@ CHECK: out of range pc-relative fixup value<br>
+  cbz r0, label1<br>
+@ CHECK: out of range pc-relative fixup value<br>
+  cbnz r0, label1<br>
+<br>
+  .space 1000<br>
+label1:<br>
+  .word 4<br>
<br>
Modified: llvm/trunk/test/MC/ARM/thumb-<wbr>diagnostics.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ARM/thumb-diagnostics.s?rev=278659&r1=278658&r2=278659&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/MC/<wbr>ARM/thumb-diagnostics.s?rev=<wbr>278659&r1=278658&r2=278659&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/MC/ARM/thumb-<wbr>diagnostics.s (original)<br>
+++ llvm/trunk/test/MC/ARM/thumb-<wbr>diagnostics.s Mon Aug 15 02:57:44 2016<br>
@@ -235,6 +235,23 @@ error: invalid operand for instruction<br>
 @ CHECK-ERRORS: error: branch target out of range<br>
<br>
 @-----------------------------<wbr>------------------------------<wbr>-------------------<br>
+@ CBZ/CBNZ - out of range immediates for branches<br>
+@----------------------------<wbr>------------------------------<wbr>--------------------<br>
+<br>
+        cbz    r0, #-2<br>
+        cbz    r0, #0<br>
+        cbz    r0, #17<br>
+        cbnz   r0, #126<br>
+        cbnz   r0, #128<br>
+<br>
+@ CHECK-ERRORS-V7M: error: branch target out of range<br>
+@ CHECK-ERRORS-V7M: error: invalid operand for instruction<br>
+@ CHECK-ERRORS-V7M: error: branch target out of range<br>
+@ CHECK-ERRORS-V8: error: branch target out of range<br>
+@ CHECK-ERRORS-V8: error: invalid operand for instruction<br>
+@ CHECK-ERRORS-V8: error: branch target out of range<br>
+<br>
+@----------------------------<wbr>------------------------------<wbr>--------------------<br>
 @ SEV/WFE/WFI/YIELD - are not supported pre v6M or v6T2<br>
 @-----------------------------<wbr>------------------------------<wbr>-------------------<br>
         sev<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>