]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/obj/riscv: prevent panics on bad branches
authorMark Ryan <markdryan@rivosinc.com>
Wed, 11 Dec 2024 13:03:17 +0000 (14:03 +0100)
committerJoel Sing <joel@sing.id.au>
Wed, 19 Mar 2025 14:00:36 +0000 (07:00 -0700)
Syntactically incorrect branches, such as

BEQ X5, X6, $1
BEQ X5, X6, 31(X10)

cause the assembler to panic, which they shouldn't really do.  It's
better for the user to see a normal error, as reported for other
syntax errors in riscv64 assembly.  The panics also prevent us
from writing negative tests for these sorts of errors.

Here we fix the issue by ensuring we generate a normal error instead
of panicking when the user provides an invalid branch target.  We
also add a couple of negative tests.

Change-Id: I1da568999a75097484b61a01d418f5d4be3e04fa
Reviewed-on: https://go-review.googlesource.com/c/go/+/637316
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com>
src/cmd/asm/internal/asm/testdata/riscv64error.s
src/cmd/internal/obj/riscv/obj.go

index e8855f6cd5f8aaa2c5bbe84568952b1c2dd5631b..005b7946122f2d4cd729d40a8a1f0459f90ad0cd 100644 (file)
@@ -30,6 +30,8 @@ TEXT errors(SB),$0
        SLLI    $64, X5, X6                     // ERROR "immediate out of range 0 to 63"
        SRLI    $64, X5, X6                     // ERROR "immediate out of range 0 to 63"
        SRAI    $64, X5, X6                     // ERROR "immediate out of range 0 to 63"
+       BEQ     X5, X6, $1                      // ERROR "instruction with branch-like opcode lacks destination"
+       BEQ     X5, X6, 31(X10)                 // ERROR "instruction with branch-like opcode lacks destination"
        RORI    $-1, X5, X6                     // ERROR "immediate out of range 0 to 63"
        SLLI    $-1, X5, X6                     // ERROR "immediate out of range 0 to 63"
        SRLI    $-1, X5, X6                     // ERROR "immediate out of range 0 to 63"
index de9851519acca975fb9cc13b8e58cc069918c468..f9ae5fe677f27a22e09e311131560fa692b80572 100644 (file)
@@ -769,7 +769,8 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
                        switch p.As {
                        case ABEQ, ABEQZ, ABGE, ABGEU, ABGEZ, ABGT, ABGTU, ABGTZ, ABLE, ABLEU, ABLEZ, ABLT, ABLTU, ABLTZ, ABNE, ABNEZ:
                                if p.To.Type != obj.TYPE_BRANCH {
-                                       panic("assemble: instruction with branch-like opcode lacks destination")
+                                       ctxt.Diag("%v: instruction with branch-like opcode lacks destination", p)
+                                       break
                                }
                                offset := p.To.Target().Pc - p.Pc
                                if offset < -4096 || 4096 <= offset {
@@ -853,7 +854,10 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
                        case obj.TYPE_BRANCH:
                                p.To.Type, p.To.Offset = obj.TYPE_CONST, p.To.Target().Pc-p.Pc
                        case obj.TYPE_MEM:
-                               panic("unhandled type")
+                               if ctxt.Errors == 0 {
+                                       // An error should have already been reported for this instruction
+                                       panic("unhandled type")
+                               }
                        }
 
                case AJAL: