1
0
mirror of https://github.com/golang/go synced 2024-11-16 17:54:39 -07:00
go/test/fixedbugs/issue8606b.go
Dmitri Shuralyov b2fd76ab8d test: migrate remaining files to go:build syntax
Most of the test cases in the test directory use the new go:build syntax
already. Convert the rest. In general, try to place the build constraint
line below the test directive comment in more places.

For #41184.
For #60268.

Change-Id: I11c41a0642a8a26dc2eda1406da908645bbc005b
Cq-Include-Trybots: luci.golang.try:gotip-linux-386-longtest,gotip-linux-amd64-longtest,gotip-windows-amd64-longtest
Reviewed-on: https://go-review.googlesource.com/c/go/+/536236
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-10-19 23:33:25 +00:00

72 lines
1.9 KiB
Go

// run
//go:build linux || darwin
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is an optimization check. We want to make sure that we compare
// string lengths, and other scalar fields, before checking string
// contents. There's no way to verify this in the language, and
// codegen tests in test/codegen can't really detect ordering
// optimizations like this. Instead, we generate invalid strings with
// bad backing store pointers but nonzero length, so we can check that
// the backing store never gets compared.
//
// We use two different bad strings so that pointer comparisons of
// backing store pointers fail.
package main
import (
"fmt"
"reflect"
"syscall"
"unsafe"
)
type SI struct {
s string
i int
}
type SS struct {
s string
t string
}
func main() {
bad1 := "foo"
bad2 := "foo"
p := syscall.Getpagesize()
b, err := syscall.Mmap(-1, 0, p, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
if err != nil {
panic(err)
}
err = syscall.Mprotect(b, syscall.PROT_NONE)
if err != nil {
panic(err)
}
// write inaccessible pointers as the data fields of bad1 and bad2.
(*reflect.StringHeader)(unsafe.Pointer(&bad1)).Data = uintptr(unsafe.Pointer(&b[0]))
(*reflect.StringHeader)(unsafe.Pointer(&bad2)).Data = uintptr(unsafe.Pointer(&b[1]))
for _, test := range []struct {
a, b interface{}
}{
{SI{s: bad1, i: 1}, SI{s: bad2, i: 2}},
{SS{s: bad1, t: "a"}, SS{s: bad2, t: "aa"}},
{SS{s: "a", t: bad1}, SS{s: "b", t: bad2}},
// This one would panic because the length of both strings match, and we check
// the body of the bad strings before the body of the good strings.
//{SS{s: bad1, t: "a"}, SS{s: bad2, t: "b"}},
} {
if test.a == test.b {
panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b))
}
}
}