| 1 |
// run |
| 2 |
|
| 3 |
//go:build linux || darwin |
| 4 |
|
| 5 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 6 |
// Use of this source code is governed by a BSD-style |
| 7 |
// license that can be found in the LICENSE file. |
| 8 |
|
| 9 |
// This is an optimization check. We want to make sure that we compare |
| 10 |
// string lengths, and other scalar fields, before checking string |
| 11 |
// contents. There's no way to verify this in the language, and |
| 12 |
// codegen tests in test/codegen can't really detect ordering |
| 13 |
// optimizations like this. Instead, we generate invalid strings with |
| 14 |
// bad backing store pointers but nonzero length, so we can check that |
| 15 |
// the backing store never gets compared. |
| 16 |
// |
| 17 |
// We use two different bad strings so that pointer comparisons of |
| 18 |
// backing store pointers fail. |
| 19 |
|
| 20 |
package main |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"reflect" |
| 25 |
"syscall" |
| 26 |
"unsafe" |
| 27 |
) |
| 28 |
|
| 29 |
type SI struct { |
| 30 |
s string |
| 31 |
i int |
| 32 |
} |
| 33 |
|
| 34 |
type SS struct { |
| 35 |
s string |
| 36 |
t string |
| 37 |
} |
| 38 |
|
| 39 |
func main() { |
| 40 |
bad1 := "foo" |
| 41 |
bad2 := "foo" |
| 42 |
|
| 43 |
p := syscall.Getpagesize() |
| 44 |
b, err := syscall.Mmap(-1, 0, p, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE) |
| 45 |
if err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
err = syscall.Mprotect(b, syscall.PROT_NONE) |
| 49 |
if err != nil { |
| 50 |
panic(err) |
| 51 |
} |
| 52 |
// write inaccessible pointers as the data fields of bad1 and bad2. |
| 53 |
(*reflect.StringHeader)(unsafe.Pointer(&bad1)).Data = uintptr(unsafe.Pointer(&b[0])) |
| 54 |
(*reflect.StringHeader)(unsafe.Pointer(&bad2)).Data = uintptr(unsafe.Pointer(&b[1])) |
| 55 |
|
| 56 |
for _, test := range []struct { |
| 57 |
a, b interface{} |
| 58 |
}{ |
| 59 |
{SI{s: bad1, i: 1}, SI{s: bad2, i: 2}}, |
| 60 |
{SS{s: bad1, t: "a"}, SS{s: bad2, t: "aa"}}, |
| 61 |
{SS{s: "a", t: bad1}, SS{s: "b", t: bad2}}, |
| 62 |
// This one would panic because the length of both strings match, and we check |
| 63 |
// the body of the bad strings before the body of the good strings. |
| 64 |
//{SS{s: bad1, t: "a"}, SS{s: bad2, t: "b"}}, |
| 65 |
} { |
| 66 |
if test.a == test.b { |
| 67 |
panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b)) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
} |