| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2025 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 "runtime" |
| 10 |
|
| 11 |
const C = 16 |
| 12 |
|
| 13 |
type T [C * C]byte |
| 14 |
|
| 15 |
func main() { |
| 16 |
var ts []*T |
| 17 |
|
| 18 |
for i := 0; i < 100; i++ { |
| 19 |
t := new(T) |
| 20 |
// Save every even object. |
| 21 |
if i%2 == 0 { |
| 22 |
ts = append(ts, t) |
| 23 |
} |
| 24 |
} |
| 25 |
// Make sure the odd objects are collected. |
| 26 |
runtime.GC() |
| 27 |
|
| 28 |
for _, t := range ts { |
| 29 |
f(t, C, C) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
//go:noinline |
| 34 |
func f(t *T, i, j uint) { |
| 35 |
if i == 0 || i > C || j == 0 || j > C { |
| 36 |
return // gets rid of bounds check below (via prove pass) |
| 37 |
} |
| 38 |
p := &t[i*j-1] |
| 39 |
*p = 0 |
| 40 |
runtime.GC() |
| 41 |
*p = 0 |
| 42 |
|
| 43 |
// This goes badly if compiled to |
| 44 |
// q := &t[i*j] |
| 45 |
// *(q-1) = 0 |
| 46 |
// runtime.GC() |
| 47 |
// *(q-1) = 0 |
| 48 |
// as at the GC call, q is an invalid pointer |
| 49 |
// (it points past the end of t's allocation). |
| 50 |
} |