fix(asm): read the riscv64 raw-data immediates at full width
WORD and BYTE read their immediate through the truncating helper, so the int32 wrap turned WORD $0xffffffff into -1 and rejected it, while WORD $0x100000000 and BYTE $0x100000001 arrived pre-truncated and slipped past the range check as small values. Both statements now read the immediate the source wrote and bound it at the toolchain's own limits: [0, 0xffffffff] for WORD, [0, 0xff] for BYTE, with the bounds pinned in a test. Assisted-by: GLM 5.3 Flash
This commit is contained in:
1 parent
bffe408afa
commit
c3540f0549
2 files changed
+75
-3
No files matched your search
@@ -1340,3 +1340,52 @@ TEXT ·v(SB), NOSPLIT, $0
|
||||
0xEA056207, // vlsseg8e32.v v4, (x10), x0
|
||||
)
|
||||
}
|
||||
|
||||
// TestRISCV_rawDataRange pins the WORD and BYTE immediate ranges at the
|
||||
// toolchain's own boundaries: WORD takes [0, 0xffffffff] and BYTE [0, 0xff],
|
||||
// and a value the source spells wider must reach the check whole. The read
|
||||
// once truncated through int32, which rejected WORD $0xffffffff as -1 while
|
||||
// accepting WORD $0x100000000 as 0.
|
||||
func TestRISCV_rawDataRange(t *testing.T) {
|
||||
asmOne := func(t *testing.T, stmt string) ([]byte, error) {
|
||||
t.Helper()
|
||||
fn := firstTextRISCV(t, "#include \"textflag.h\"\nTEXT ·w(SB), NOSPLIT, $0\n\t"+stmt+"\n\tRET\n")
|
||||
code, _, _, _, _, _, err := assembleRISCV(fn)
|
||||
return code, err
|
||||
}
|
||||
t.Run("word bounds", func(t *testing.T) {
|
||||
code, err := asmOne(t, "WORD $0")
|
||||
if err != nil {
|
||||
t.Fatalf("WORD $0: %v", err)
|
||||
}
|
||||
if string(code[:4]) != "\x00\x00\x00\x00" {
|
||||
t.Errorf("WORD $0 = % x", code[:4])
|
||||
}
|
||||
code, err = asmOne(t, "WORD $0xffffffff")
|
||||
if err != nil {
|
||||
t.Fatalf("WORD $0xffffffff must assemble, as go tool asm accepts it: %v", err)
|
||||
}
|
||||
if string(code[:4]) != "\xff\xff\xff\xff" {
|
||||
t.Errorf("WORD $0xffffffff = % x", code[:4])
|
||||
}
|
||||
for _, w := range []string{"$-1", "$0x100000000", "$-4294967296"} {
|
||||
if _, err := asmOne(t, "WORD "+w); err == nil {
|
||||
t.Errorf("WORD %s must be rejected, as go tool asm rejects it", w)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("byte bounds", func(t *testing.T) {
|
||||
code, err := asmOne(t, "BYTE $255")
|
||||
if err != nil {
|
||||
t.Fatalf("BYTE $255: %v", err)
|
||||
}
|
||||
if code[0] != 0xff {
|
||||
t.Errorf("BYTE $255 = % x", code[:1])
|
||||
}
|
||||
for _, b := range []string{"$256", "$0x100000001", "$-1"} {
|
||||
if _, err := asmOne(t, "BYTE "+b); err == nil {
|
||||
t.Errorf("BYTE %s must be rejected: out of the byte range", b)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user