aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorWayne Zuo <wdvxdr@golangcn.org>2023-01-12 19:37:18 +0800
committerWayne Zuo <wdvxdr@golangcn.org>2023-01-19 23:30:35 +0000
commit5454834f011c019ca32d844d26d469cd47f407ad (patch)
treed00df83ad72e76c50d701181b6204c457c7cecef /src/cmd
parent45dc81d8565adb7d0a62502d039f4930e73d75e0 (diff)
downloadgo-5454834f011c019ca32d844d26d469cd47f407ad.tar.xz
cmd/internal/obj/riscv: add check for invalid shift amount input
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 <joel@sing.id.au> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Wayne Zuo <wdvxdr@golangcn.org>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/asm/internal/asm/testdata/riscv64error.s12
-rw-r--r--src/cmd/internal/obj/riscv/obj.go10
2 files changed, 22 insertions, 0 deletions
diff --git a/src/cmd/asm/internal/asm/testdata/riscv64error.s b/src/cmd/asm/internal/asm/testdata/riscv64error.s
index d3e43e721d..cdb8a028bd 100644
--- a/src/cmd/asm/internal/asm/testdata/riscv64error.s
+++ b/src/cmd/asm/internal/asm/testdata/riscv64error.s
@@ -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
diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go
index 95cd3659e8..cbf894817d 100644
--- a/src/cmd/internal/obj/riscv/obj.go
+++ b/src/cmd/internal/obj/riscv/obj.go
@@ -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
}