3 //go:build linux || darwin
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.
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.
17 // We use two different bad strings so that pointer comparisons of
18 // backing store pointers fail.
43 p := syscall.Getpagesize()
44 b, err := syscall.Mmap(-1, 0, p, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
48 err = syscall.Mprotect(b, syscall.PROT_NONE)
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]))
56 for _, test := range []struct {
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"}},
67 panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b))