Skip to content

Commit

Permalink
cmd/internal/obj/riscv: add check for invalid shift amount input
Browse files Browse the repository at this point in the history
Current RISCV64 assembler do not check the invalid shift amount. This CL
adds the check to avoid generating invalid instructions.

Fixes #57755

Change-Id: If33877605e161baefd98c50db1f71641ca057507
Reviewed-on: https://go-review.googlesource.com/c/go/+/461755
Reviewed-by: Joel Sing <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
Run-TryBot: Wayne Zuo <[email protected]>
  • Loading branch information
wdvxdr1123 committed Jan 19, 2023
1 parent 45dc81d commit 5454834
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cmd/asm/internal/asm/testdata/riscv64error.s
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,17 @@ TEXT errors(SB),$0
MOVD F0, F1, F2 // ERROR "illegal MOV instruction"
MOV X10, X11, X12 // ERROR "illegal MOV instruction"
MOVW X10, X11, X12 // ERROR "illegal MOV instruction"
SLLI $64, X5, X6 // ERROR "shift amount out of range 0 to 63"
SRLI $64, X5, X6 // ERROR "shift amount out of range 0 to 63"
SRAI $64, X5, X6 // ERROR "shift amount out of range 0 to 63"
SLLI $-1, X5, X6 // ERROR "shift amount out of range 0 to 63"
SRLI $-1, X5, X6 // ERROR "shift amount out of range 0 to 63"
SRAI $-1, X5, X6 // ERROR "shift amount out of range 0 to 63"
SLLIW $32, X5, X6 // ERROR "shift amount out of range 0 to 31"
SRLIW $32, X5, X6 // ERROR "shift amount out of range 0 to 31"
SRAIW $32, X5, X6 // ERROR "shift amount out of range 0 to 31"
SLLIW $-1, X5, X6 // ERROR "shift amount out of range 0 to 31"
SRLIW $-1, X5, X6 // ERROR "shift amount out of range 0 to 31"
SRAIW $-1, X5, X6 // ERROR "shift amount out of range 0 to 31"

RET
10 changes: 10 additions & 0 deletions src/cmd/internal/obj/riscv/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,16 @@ func instructionsForProg(p *obj.Prog) []*instruction {
// FNEGD rs, rd -> FSGNJND rs, rs, rd
ins.as = AFSGNJND
ins.rs1 = uint32(p.From.Reg)

case ASLLI, ASRLI, ASRAI:
if ins.imm < 0 || ins.imm > 63 {
p.Ctxt.Diag("%v: shift amount out of range 0 to 63", p)
}

case ASLLIW, ASRLIW, ASRAIW:
if ins.imm < 0 || ins.imm > 31 {
p.Ctxt.Diag("%v: shift amount out of range 0 to 31", p)
}
}
return inss
}
Expand Down

0 comments on commit 5454834

Please sign in to comment.