diff options
| author | Josh Bleecher Snyder <josharian@gmail.com> | 2021-03-14 14:24:47 -0700 |
|---|---|---|
| committer | Josh Bleecher Snyder <josharian@gmail.com> | 2021-04-21 00:53:48 +0000 |
| commit | faa4fa1a6e94fce4f6fa22524a2bece5125213b6 (patch) | |
| tree | ecd5c4b860564b5b1926725f6ffafcebfac3b3d9 /src/runtime | |
| parent | 1c268431f49ee2fc843eac52a0854aea3d02a6e0 (diff) | |
| download | go-faa4fa1a6e94fce4f6fa22524a2bece5125213b6.tar.xz | |
cmd/compile: allow conversion from slice to array ptr
Panic if the slice is too short.
Updates #395
Change-Id: I90f4bff2da5d8f3148ba06d2482084f32b25c29a
Reviewed-on: https://go-review.googlesource.com/c/go/+/301650
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/asm_386.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_amd64.s | 8 | ||||
| -rw-r--r-- | src/runtime/asm_arm.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_arm64.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_mips64x.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_mipsx.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_ppc64x.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_riscv64.s | 4 | ||||
| -rw-r--r-- | src/runtime/asm_s390x.s | 4 | ||||
| -rw-r--r-- | src/runtime/error.go | 2 | ||||
| -rw-r--r-- | src/runtime/panic.go | 7 |
11 files changed, 49 insertions, 0 deletions
diff --git a/src/runtime/asm_386.s b/src/runtime/asm_386.s index 5cf6827c21..45f8bf1003 100644 --- a/src/runtime/asm_386.s +++ b/src/runtime/asm_386.s @@ -1473,6 +1473,10 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-8 MOVL AX, x+0(FP) MOVL CX, y+4(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-8 + MOVL DX, x+0(FP) + MOVL BX, y+4(FP) + JMP runtime·goPanicSliceConvert(SB) // Extended versions for 64-bit indexes. TEXT runtime·panicExtendIndex(SB),NOSPLIT,$0-12 diff --git a/src/runtime/asm_amd64.s b/src/runtime/asm_amd64.s index 1e6d8189c9..d2848e5a45 100644 --- a/src/runtime/asm_amd64.s +++ b/src/runtime/asm_amd64.s @@ -2091,6 +2091,14 @@ TEXT runtime·panicSlice3CU<ABIInternal>(SB),NOSPLIT,$0-16 MOVQ CX, y+8(FP) #endif JMP runtime·goPanicSlice3CU<ABIInternal>(SB) +TEXT runtime·panicSliceConvert<ABIInternal>(SB),NOSPLIT,$0-16 +#ifdef GOEXPERIMENT_regabiargs + MOVQ DX, AX +#else + MOVQ DX, x+0(FP) + MOVQ BX, y+8(FP) +#endif + JMP runtime·goPanicSliceConvert<ABIInternal>(SB) #ifdef GOOS_android // Use the free TLS_SLOT_APP slot #2 on Android Q. diff --git a/src/runtime/asm_arm.s b/src/runtime/asm_arm.s index 9896ab4383..f468b77ee3 100644 --- a/src/runtime/asm_arm.s +++ b/src/runtime/asm_arm.s @@ -992,6 +992,10 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-8 MOVW R0, x+0(FP) MOVW R1, y+4(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-8 + MOVW R2, x+0(FP) + MOVW R3, y+4(FP) + JMP runtime·goPanicSliceConvert(SB) // Extended versions for 64-bit indexes. TEXT runtime·panicExtendIndex(SB),NOSPLIT,$0-12 diff --git a/src/runtime/asm_arm64.s b/src/runtime/asm_arm64.s index 2e120dcf1e..2d495397a8 100644 --- a/src/runtime/asm_arm64.s +++ b/src/runtime/asm_arm64.s @@ -1314,3 +1314,7 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16 MOVD R0, x+0(FP) MOVD R1, y+8(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16 + MOVD R2, x+0(FP) + MOVD R3, y+8(FP) + JMP runtime·goPanicSliceConvert(SB) diff --git a/src/runtime/asm_mips64x.s b/src/runtime/asm_mips64x.s index cee4b528bb..c3b57e472a 100644 --- a/src/runtime/asm_mips64x.s +++ b/src/runtime/asm_mips64x.s @@ -805,3 +805,7 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16 MOVV R1, x+0(FP) MOVV R2, y+8(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16 + MOVV R3, x+0(FP) + MOVV R4, y+8(FP) + JMP runtime·goPanicSliceConvert(SB) diff --git a/src/runtime/asm_mipsx.s b/src/runtime/asm_mipsx.s index 17fbc902c2..1d828b03cf 100644 --- a/src/runtime/asm_mipsx.s +++ b/src/runtime/asm_mipsx.s @@ -801,6 +801,10 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-8 MOVW R1, x+0(FP) MOVW R2, y+4(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-8 + MOVW R3, x+0(FP) + MOVW R4, y+4(FP) + JMP runtime·goPanicSliceConvert(SB) // Extended versions for 64-bit indexes. TEXT runtime·panicExtendIndex(SB),NOSPLIT,$0-12 diff --git a/src/runtime/asm_ppc64x.s b/src/runtime/asm_ppc64x.s index 6544048497..2c39b38912 100644 --- a/src/runtime/asm_ppc64x.s +++ b/src/runtime/asm_ppc64x.s @@ -1022,3 +1022,7 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16 MOVD R3, x+0(FP) MOVD R4, y+8(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16 + MOVD R5, x+0(FP) + MOVD R6, y+8(FP) + JMP runtime·goPanicSliceConvert(SB) diff --git a/src/runtime/asm_riscv64.s b/src/runtime/asm_riscv64.s index d8d5252ed5..ef7af4e10d 100644 --- a/src/runtime/asm_riscv64.s +++ b/src/runtime/asm_riscv64.s @@ -806,6 +806,10 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16 MOV T0, x+0(FP) MOV T1, y+8(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16 + MOV T2, x+0(FP) + MOV T3, y+8(FP) + JMP runtime·goPanicSliceConvert(SB) DATA runtime·mainPC+0(SB)/8,$runtime·main(SB) GLOBL runtime·mainPC(SB),RODATA,$8 diff --git a/src/runtime/asm_s390x.s b/src/runtime/asm_s390x.s index 4748e00aa8..fb38271630 100644 --- a/src/runtime/asm_s390x.s +++ b/src/runtime/asm_s390x.s @@ -906,3 +906,7 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16 MOVD R0, x+0(FP) MOVD R1, y+8(FP) JMP runtime·goPanicSlice3CU(SB) +TEXT runtime·panicSliceConvert(SB),NOSPLIT,$0-16 + MOVD R2, x+0(FP) + MOVD R3, y+8(FP) + JMP runtime·goPanicSliceConvert(SB) diff --git a/src/runtime/error.go b/src/runtime/error.go index 9e6cdf35dd..91f83ae126 100644 --- a/src/runtime/error.go +++ b/src/runtime/error.go @@ -134,6 +134,7 @@ const ( boundsSlice3B // s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen) boundsSlice3C // s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen) + boundsConvert // (*[x]T)(s), 0 <= x <= len(s) failed // Note: in the above, len(s) and cap(s) are stored in y ) @@ -149,6 +150,7 @@ var boundsErrorFmts = [...]string{ boundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y", boundsSlice3B: "slice bounds out of range [:%x:%y]", boundsSlice3C: "slice bounds out of range [%x:%y:]", + boundsConvert: "cannot convert slice with length %y to pointer to array with length %x", } // boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y. diff --git a/src/runtime/panic.go b/src/runtime/panic.go index f8f2f39dbb..f6c38aafcc 100644 --- a/src/runtime/panic.go +++ b/src/runtime/panic.go @@ -160,6 +160,12 @@ func goPanicSlice3CU(x uint, y int) { panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3C}) } +// failures in the conversion (*[x]T)s, 0 <= x <= y, x == cap(s) +func goPanicSliceConvert(x int, y int) { + panicCheck1(getcallerpc(), "slice length too short to convert to pointer to array") + panic(boundsError{x: int64(x), signed: true, y: y, code: boundsConvert}) +} + // Implemented in assembly, as they take arguments in registers. // Declared here to mark them as ABIInternal. func panicIndex(x int, y int) @@ -178,6 +184,7 @@ func panicSlice3B(x int, y int) func panicSlice3BU(x uint, y int) func panicSlice3C(x int, y int) func panicSlice3CU(x uint, y int) +func panicSliceConvert(x int, y int) var shiftError = error(errorString("negative shift amount")) |
