Skip to content

Commit c4db811

Browse files
committed
cmd/compile: don't ICE on unaligned offsets for pointer writes
User code is unlikely to be correct, but don't crash the compiler when the offset of a pointer in an object is not a multiple of the pointer size. Fixes #61187 Change-Id: Ie56bfcb38556c5dd6f702ae4ec1d4534c6acd420 Reviewed-on: https://go-review.googlesource.com/c/go/+/508555 Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 5c15498 commit c4db811

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/cmd/compile/internal/ssa/writebarrier.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ func mightContainHeapPointer(ptr *Value, size int64, mem *Value, zeroes map[ID]Z
5353
}
5454

5555
ptrSize := ptr.Block.Func.Config.PtrSize
56-
if off%ptrSize != 0 || size%ptrSize != 0 {
56+
if off%ptrSize != 0 {
57+
return true // see issue 61187
58+
}
59+
if size%ptrSize != 0 {
5760
ptr.Fatalf("unaligned pointer write")
5861
}
5962
if off < 0 || off+size > 64*ptrSize {
@@ -130,7 +133,7 @@ func needWBdst(ptr, mem *Value, zeroes map[ID]ZeroRegion) bool {
130133
}
131134
ptrSize := ptr.Block.Func.Config.PtrSize
132135
if off%ptrSize != 0 {
133-
ptr.Fatalf("unaligned pointer write")
136+
return true // see issue 61187
134137
}
135138
if off < 0 || off >= 64*ptrSize {
136139
// write goes off end of tracked offsets

test/fixedbugs/issue61187.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"reflect"
12+
"unsafe"
13+
)
14+
15+
var slice = []byte{'H', 'e', 'l', 'l', 'o', ','}
16+
17+
func main() {
18+
ptr := uintptr(unsafe.Pointer(&slice)) + 100
19+
header := (*reflect.SliceHeader)(unsafe.Pointer(ptr))
20+
header.Data += 1
21+
fmt.Printf("%d %d\n", cap(slice), header.Cap)
22+
}

0 commit comments

Comments
 (0)