Commit 3583a44e authored by Shenghou Ma's avatar Shenghou Ma Committed by Minux Ma

runtime: check that masks and shifts are correct aligned

We need a runtime check because the original issue is encountered
when running cross compiled windows program from linux. It's better
to give a meaningful crash message earlier than to segfault later.

The added test should not impose any measurable overhead to Go
programs.

For #12415.

Change-Id: Ib4a24ef560c09c0585b351d62eefd157b6b7f04c
Reviewed-on: https://go-review.googlesource.com/14207Reviewed-by: 's avatarKeith Randall <khr@golang.org>
Run-TryBot: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 3cac3b5f
...@@ -1188,6 +1188,15 @@ DATA shifts<>+0xfc(SB)/4, $0xff0f0e0d ...@@ -1188,6 +1188,15 @@ DATA shifts<>+0xfc(SB)/4, $0xff0f0e0d
GLOBL shifts<>(SB),RODATA,$256 GLOBL shifts<>(SB),RODATA,$256
TEXT ·checkASM(SB),NOSPLIT,$0-1
// check that masks<>(SB) and shifts<>(SB) are aligned to 16-byte
MOVL $masks<>(SB), AX
MOVL $shifts<>(SB), BX
ORL BX, AX
TESTL $15, AX
SETEQ ret+0(FP)
RET
TEXT runtime·memeq(SB),NOSPLIT,$0-13 TEXT runtime·memeq(SB),NOSPLIT,$0-13
MOVL a+0(FP), SI MOVL a+0(FP), SI
MOVL b+4(FP), DI MOVL b+4(FP), DI
......
...@@ -1222,6 +1222,15 @@ DATA masks<>+0xf0(SB)/8, $0xffffffffffffffff ...@@ -1222,6 +1222,15 @@ DATA masks<>+0xf0(SB)/8, $0xffffffffffffffff
DATA masks<>+0xf8(SB)/8, $0x00ffffffffffffff DATA masks<>+0xf8(SB)/8, $0x00ffffffffffffff
GLOBL masks<>(SB),RODATA,$256 GLOBL masks<>(SB),RODATA,$256
TEXT ·checkASM(SB),NOSPLIT,$0-1
// check that masks<>(SB) and shifts<>(SB) are aligned to 16-byte
MOVQ $masks<>(SB), AX
MOVQ $shifts<>(SB), BX
ORQ BX, AX
TESTQ $15, AX
SETEQ ret+0(FP)
RET
// these are arguments to pshufb. They move data down from // these are arguments to pshufb. They move data down from
// the high bytes of the register to the low bytes of the register. // the high bytes of the register to the low bytes of the register.
// index is how many bytes to move. // index is how many bytes to move.
......
...@@ -1012,3 +1012,7 @@ TEXT runtime·prefetchnta(SB),NOSPLIT,$0-4 ...@@ -1012,3 +1012,7 @@ TEXT runtime·prefetchnta(SB),NOSPLIT,$0-4
MOVL addr+0(FP), AX MOVL addr+0(FP), AX
PREFETCHNTA (AX) PREFETCHNTA (AX)
RET RET
TEXT ·checkASM(SB),NOSPLIT,$0-1
MOVB $1, ret+0(FP)
RET
...@@ -1030,3 +1030,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT,$0-4 ...@@ -1030,3 +1030,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT,$0-4
MOVW saver9-4(SP), R9 MOVW saver9-4(SP), R9
RET RET
#endif #endif
TEXT ·checkASM(SB),NOSPLIT,$0-1
MOVW $1, R3
MOVB R3, ret+0(FP)
RET
...@@ -996,4 +996,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT,$0-0 ...@@ -996,4 +996,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT,$0-0
MOVD R0, runtime·lastmoduledatap(SB) MOVD R0, runtime·lastmoduledatap(SB)
MOVD 8(RSP), R27 MOVD 8(RSP), R27
ADD $0x10, RSP ADD $0x10, RSP
TEXT ·checkASM(SB),NOSPLIT,$0-1
MOVW $1, R3
MOVB R3, ret+0(FP)
RET RET
...@@ -818,3 +818,8 @@ TEXT runtime·prefetcht2(SB),NOSPLIT,$0-8 ...@@ -818,3 +818,8 @@ TEXT runtime·prefetcht2(SB),NOSPLIT,$0-8
TEXT runtime·prefetchnta(SB),NOSPLIT,$0-8 TEXT runtime·prefetchnta(SB),NOSPLIT,$0-8
RET RET
TEXT ·checkASM(SB),NOSPLIT,$0-1
MOVW $1, R1
MOVB R1, ret+0(FP)
RET
...@@ -1079,4 +1079,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT|NOFRAME,$0-0 ...@@ -1079,4 +1079,8 @@ TEXT runtime·addmoduledata(SB),NOSPLIT|NOFRAME,$0-0
MOVD R3, runtime·lastmoduledatap(SB) MOVD R3, runtime·lastmoduledatap(SB)
MOVD 0(R1), R31 MOVD 0(R1), R31
ADD $8, R1 ADD $8, R1
TEXT ·checkASM(SB),NOSPLIT,$0-1
MOVW $1, R3
MOVB R3, ret+0(FP)
RET RET
...@@ -296,6 +296,10 @@ func check() { ...@@ -296,6 +296,10 @@ func check() {
if _FixedStack != round2(_FixedStack) { if _FixedStack != round2(_FixedStack) {
throw("FixedStack is not power-of-2") throw("FixedStack is not power-of-2")
} }
if !checkASM() {
throw("assembly checks failed")
}
} }
type dbgVar struct { type dbgVar struct {
......
...@@ -270,3 +270,6 @@ func unixnanotime() int64 { ...@@ -270,3 +270,6 @@ func unixnanotime() int64 {
func round(n, a uintptr) uintptr { func round(n, a uintptr) uintptr {
return (n + a - 1) &^ (a - 1) return (n + a - 1) &^ (a - 1)
} }
// checkASM returns whether assembly runtime checks have passed.
func checkASM() bool
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment