diff --git a/platform/dbops/binaries/go/go/test/blank.go b/platform/dbops/binaries/go/go/test/blank.go new file mode 100644 index 0000000000000000000000000000000000000000..0539debb1f25249788954bf0704db109ae1d635b --- /dev/null +++ b/platform/dbops/binaries/go/go/test/blank.go @@ -0,0 +1,189 @@ +// run + +// Copyright 2009 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. + +// Test behavior of the blank identifier (_). + +package main + +import ( + "os" + "unsafe" +) + +import _ "fmt" + +var call string + +type T struct { + _, _, _ int +} + +func (T) _() { +} + +func (T) _() { +} + +type U struct { + _ struct{ a, b, c int } +} + +const ( + c0 = iota + _ + _ + _ + c4 +) + +var ints = []string{ + "1", + "2", + "3", +} + +func f() (int, int) { + call += "f" + return 1, 2 +} + +func g() (float64, float64) { + call += "g" + return 3, 4 +} + +func h(_ int, _ float64) { +} + +func i() int { + call += "i" + return 23 +} + +var _ = i() + +func main() { + if call != "i" { + panic("init did not run") + } + call = "" + _, _ = f() + a, _ := f() + if a != 1 { + panic(a) + } + b, _ := g() + if b != 3 { + panic(b) + } + _, a = f() + if a != 2 { + panic(a) + } + _, b = g() + if b != 4 { + panic(b) + } + _ = i() + if call != "ffgfgi" { + panic(call) + } + if c4 != 4 { + panic(c4) + } + + out := "" + for _, s := range ints { + out += s + } + if out != "123" { + panic(out) + } + + sum := 0 + for s := range ints { + sum += s + } + if sum != 3 { + panic(sum) + } + + // go.tools/ssa/interp cannot support unsafe.Pointer. + if os.Getenv("GOSSAINTERP") == "" { + type T1 struct{ x, y, z int } + t1 := *(*T)(unsafe.Pointer(&T1{1, 2, 3})) + t2 := *(*T)(unsafe.Pointer(&T1{4, 5, 6})) + if t1 != t2 { + panic("T{} != T{}") + } + + var u1, u2 interface{} + u1 = *(*U)(unsafe.Pointer(&T1{1, 2, 3})) + u2 = *(*U)(unsafe.Pointer(&T1{4, 5, 6})) + if u1 != u2 { + panic("U{} != U{}") + } + } + + h(a, b) + + m() +} + +type I interface { + M(_ int, y int) +} + +type TI struct{} + +func (_ TI) M(x int, y int) { + if x != y { + println("invalid M call:", x, y) + panic("bad M") + } +} + +var fp = func(_ int, y int) {} + +func init() { + fp = fp1 +} + +func fp1(x, y int) { + if x != y { + println("invalid fp1 call:", x, y) + panic("bad fp1") + } +} + +func m() { + var i I + + i = TI{} + i.M(1, 1) + i.M(2, 2) + + fp(1, 1) + fp(2, 2) +} + +// useless but legal +var _ int = 1 +var _ = 2 +var _, _ = 3, 4 + +const _ = 3 +const _, _ = 4, 5 + +type _ int + +func _() { + panic("oops") +} + +func ff() { + var _ int = 1 +} diff --git a/platform/dbops/binaries/go/go/test/blank1.go b/platform/dbops/binaries/go/go/test/blank1.go new file mode 100644 index 0000000000000000000000000000000000000000..815a7e9b5a8b5eda5314acaaf676207583fea9af --- /dev/null +++ b/platform/dbops/binaries/go/go/test/blank1.go @@ -0,0 +1,32 @@ +// errorcheck + +// Copyright 2009 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. + +// Test that incorrect uses of the blank identifier are caught. +// Does not compile. + +package _ // ERROR "invalid package name" + +var t struct { + _ int +} + +func (x int) _() { // ERROR "methods on non-local type" + println(x) +} + +type T struct { + _ []int +} + +func main() { + _() // ERROR "cannot use .* as value" + x := _+1 // ERROR "cannot use .* as value" + _ = x + _ = t._ // ERROR "cannot refer to blank field|invalid use of|t._ undefined" + + var v1, v2 T + _ = v1 == v2 // ERROR "cannot be compared|non-comparable|cannot compare v1 == v2" +} diff --git a/platform/dbops/binaries/go/go/test/bom.go b/platform/dbops/binaries/go/go/test/bom.go new file mode 100644 index 0000000000000000000000000000000000000000..37f73bc5d2afa7567c00e3e670a353828ae168c1 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/bom.go @@ -0,0 +1,26 @@ +// runoutput + +// Copyright 2011 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. + +// Test source file beginning with a byte order mark. + +package main + +import ( + "fmt" + "strings" +) + +func main() { + prog = strings.Replace(prog, "BOM", "\uFEFF", -1) + fmt.Print(prog) +} + +var prog = `BOM +package main + +func main() { +} +` diff --git a/platform/dbops/binaries/go/go/test/bombad.go b/platform/dbops/binaries/go/go/test/bombad.go new file mode 100644 index 0000000000000000000000000000000000000000..6b79a98e48f8a5eca3312a27eda8e4dc4f5e8f50 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/bombad.go @@ -0,0 +1,18 @@ +// errorcheck + +// Copyright 2012 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. + +// Here for reference, but hard to test automatically +// because the BOM muddles the +// processing done by ../run. + +package main + +func main() { + // There's a bom here. // ERROR "BOM" + // And here. // ERROR "BOM" + /* And here.*/ // ERROR "BOM" + println("hi there") // and here // ERROR "BOM" +} diff --git a/platform/dbops/binaries/go/go/test/bounds.go b/platform/dbops/binaries/go/go/test/bounds.go new file mode 100644 index 0000000000000000000000000000000000000000..aa1d51b6f9cc6e5290bd677ebddac497fee69b71 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/bounds.go @@ -0,0 +1,284 @@ +// errorcheck -0 -m -l + +// Copyright 2012 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. + +// Test, using compiler diagnostic flags, that bounds check elimination +// is eliminating the correct checks. + +package foo + +var ( + s []int + + a1 [1]int + a1k [1000]int + a100k [100000]int + + p1 *[1]int + p1k *[1000]int + p100k *[100000]int + + i int + ui uint + i8 int8 + ui8 uint8 + i16 int16 + ui16 uint16 + i32 int32 + ui32 uint32 + i64 int64 + ui64 uint64 +) + +func main() { + // Most things need checks. + use(s[i]) + use(a1[i]) + use(a1k[i]) + use(a100k[i]) + use(p1[i]) + use(p1k[i]) + use(p100k[i]) + + use(s[ui]) + use(a1[ui]) + use(a1k[ui]) + use(a100k[ui]) + use(p1[ui]) + use(p1k[ui]) + use(p100k[ui]) + + use(s[i8]) + use(a1[i8]) + use(a1k[i8]) + use(a100k[i8]) + use(p1[i8]) + use(p1k[i8]) + use(p100k[i8]) + + // Unsigned 8-bit numbers don't need checks for len >= 2⁸. + use(s[ui8]) + use(a1[ui8]) + use(a1k[ui8]) // ERROR "index bounds check elided" + use(a100k[ui8]) // ERROR "index bounds check elided" + use(p1[ui8]) + use(p1k[ui8]) // ERROR "index bounds check elided" + use(p100k[ui8]) // ERROR "index bounds check elided" + + use(s[i16]) + use(a1[i16]) + use(a1k[i16]) + use(a100k[i16]) + use(p1[i16]) + use(p1k[i16]) + use(p100k[i16]) + + // Unsigned 16-bit numbers don't need checks for len >= 2¹⁶. + use(s[ui16]) + use(a1[ui16]) + use(a1k[ui16]) + use(a100k[ui16]) // ERROR "index bounds check elided" + use(p1[ui16]) + use(p1k[ui16]) + use(p100k[ui16]) // ERROR "index bounds check elided" + + use(s[i32]) + use(a1[i32]) + use(a1k[i32]) + use(a100k[i32]) + use(p1[i32]) + use(p1k[i32]) + use(p100k[i32]) + + use(s[ui32]) + use(a1[ui32]) + use(a1k[ui32]) + use(a100k[ui32]) + use(p1[ui32]) + use(p1k[ui32]) + use(p100k[ui32]) + + use(s[i64]) + use(a1[i64]) + use(a1k[i64]) + use(a100k[i64]) + use(p1[i64]) + use(p1k[i64]) + use(p100k[i64]) + + use(s[ui64]) + use(a1[ui64]) + use(a1k[ui64]) + use(a100k[ui64]) + use(p1[ui64]) + use(p1k[ui64]) + use(p100k[ui64]) + + // Mod truncates the maximum value to one less than the argument, + // but signed mod can be negative, so only unsigned mod counts. + use(s[i%999]) + use(a1[i%999]) + use(a1k[i%999]) + use(a100k[i%999]) + use(p1[i%999]) + use(p1k[i%999]) + use(p100k[i%999]) + + use(s[ui%999]) + use(a1[ui%999]) + use(a1k[ui%999]) // ERROR "index bounds check elided" + use(a100k[ui%999]) // ERROR "index bounds check elided" + use(p1[ui%999]) + use(p1k[ui%999]) // ERROR "index bounds check elided" + use(p100k[ui%999]) // ERROR "index bounds check elided" + + use(s[i%1000]) + use(a1[i%1000]) + use(a1k[i%1000]) + use(a100k[i%1000]) + use(p1[i%1000]) + use(p1k[i%1000]) + use(p100k[i%1000]) + + use(s[ui%1000]) + use(a1[ui%1000]) + use(a1k[ui%1000]) // ERROR "index bounds check elided" + use(a100k[ui%1000]) // ERROR "index bounds check elided" + use(p1[ui%1000]) + use(p1k[ui%1000]) // ERROR "index bounds check elided" + use(p100k[ui%1000]) // ERROR "index bounds check elided" + + use(s[i%1001]) + use(a1[i%1001]) + use(a1k[i%1001]) + use(a100k[i%1001]) + use(p1[i%1001]) + use(p1k[i%1001]) + use(p100k[i%1001]) + + use(s[ui%1001]) + use(a1[ui%1001]) + use(a1k[ui%1001]) + use(a100k[ui%1001]) // ERROR "index bounds check elided" + use(p1[ui%1001]) + use(p1k[ui%1001]) + use(p100k[ui%1001]) // ERROR "index bounds check elided" + + // Bitwise and truncates the maximum value to the mask value. + // The result (for a positive mask) cannot be negative, so elision + // applies to both signed and unsigned indexes. + use(s[i&999]) + use(a1[i&999]) + use(a1k[i&999]) // ERROR "index bounds check elided" + use(a100k[i&999]) // ERROR "index bounds check elided" + use(p1[i&999]) + use(p1k[i&999]) // ERROR "index bounds check elided" + use(p100k[i&999]) // ERROR "index bounds check elided" + + use(s[ui&999]) + use(a1[ui&999]) + use(a1k[ui&999]) // ERROR "index bounds check elided" + use(a100k[ui&999]) // ERROR "index bounds check elided" + use(p1[ui&999]) + use(p1k[ui&999]) // ERROR "index bounds check elided" + use(p100k[ui&999]) // ERROR "index bounds check elided" + + use(s[i&1000]) + use(a1[i&1000]) + use(a1k[i&1000]) + use(a100k[i&1000]) // ERROR "index bounds check elided" + use(p1[i&1000]) + use(p1k[i&1000]) + use(p100k[i&1000]) // ERROR "index bounds check elided" + + use(s[ui&1000]) + use(a1[ui&1000]) + use(a1k[ui&1000]) + use(a100k[ui&1000]) // ERROR "index bounds check elided" + use(p1[ui&1000]) + use(p1k[ui&1000]) + use(p100k[ui&1000]) // ERROR "index bounds check elided" + + use(a1[i&^-1]) // ERROR "index bounds check elided" + use(a1[i&^0]) + use(a1[i&^-2]) + use(a1[i&^1]) + use(a1k[i&^-1]) // ERROR "index bounds check elided" + use(a1k[i&^0]) + use(a1k[i&^-2]) // ERROR "index bounds check elided" + use(a1k[i&^1]) + use(a1k[i8&^0]) + use(a1k[i8&^-128]) // ERROR "index bounds check elided" + use(a1k[ui8&^1]) // ERROR "index bounds check elided" + use(a1k[ui16&^0xf000]) + use(a1k[ui16&^0xff00]) // ERROR "index bounds check elided" + + // Right shift cuts the effective number of bits in the index, + // but only for unsigned (signed stays negative). + use(s[i32>>22]) + use(a1[i32>>22]) + use(a1k[i32>>22]) + use(a100k[i32>>22]) + use(p1[i32>>22]) + use(p1k[i32>>22]) + use(p100k[i32>>22]) + + use(s[ui32>>22]) + use(a1[ui32>>22]) + use(a1k[ui32>>22]) + use(a100k[ui32>>22]) // ERROR "index bounds check elided" + use(p1[ui32>>22]) + use(p1k[ui32>>22]) + use(p100k[ui32>>22]) // ERROR "index bounds check elided" + + use(s[i32>>23]) + use(a1[i32>>23]) + use(a1k[i32>>23]) + use(a100k[i32>>23]) + use(p1[i32>>23]) + use(p1k[i32>>23]) + use(p100k[i32>>23]) + + use(s[ui32>>23]) + use(a1[ui32>>23]) + use(a1k[ui32>>23]) // ERROR "index bounds check elided" + use(a100k[ui32>>23]) // ERROR "index bounds check elided" + use(p1[ui32>>23]) + use(p1k[ui32>>23]) // ERROR "index bounds check elided" + use(p100k[ui32>>23]) // ERROR "index bounds check elided" + + // Division cuts the range like right shift does. + use(s[i/1e6]) + use(a1[i/1e6]) + use(a1k[i/1e6]) + use(a100k[i/1e6]) + use(p1[i/1e6]) + use(p1k[i/1e6]) + use(p100k[i/1e6]) + + use(s[ui/1e6]) + use(a1[ui/1e6]) + use(a1k[ui/1e6]) + use(p1[ui/1e6]) + use(p1k[ui/1e6]) + + use(s[i/1e7]) + use(a1[i/1e7]) + use(a1k[i/1e7]) + use(a100k[i/1e7]) + use(p1[i/1e7]) + use(p1k[i/1e7]) + use(p100k[i/1e7]) + + use(s[ui/1e7]) + use(a1[ui/1e7]) + use(p1[ui/1e7]) +} + +var sum int + +func use(x int) { + sum += x +} diff --git a/platform/dbops/binaries/go/go/test/cannotassign.go b/platform/dbops/binaries/go/go/test/cannotassign.go new file mode 100644 index 0000000000000000000000000000000000000000..27e62890c5249f6edd2d7e691af3d92f38daac40 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/cannotassign.go @@ -0,0 +1,33 @@ +// errorcheck + +// 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. + +// Test "cannot assign" errors + +package main + +func main() { + var s string = "hello" + s[1:2] = "a" // ERROR "cannot assign to .* (\(strings are immutable\))?" + s[3] = "b" // ERROR "cannot assign to .* (\(strings are immutable\))?" + + const n int = 1 + const cs string = "hello" + n = 2 // ERROR "cannot assign to .* (\(declared const\))?" + cs = "hi" // ERROR "cannot assign to .* (\(declared const\))?" + true = false // ERROR "cannot assign to .* (\(declared const\))?" + + var m map[int]struct{ n int } + m[0].n = 7 // ERROR "cannot assign to struct field .* in map$" + + 1 = 7 // ERROR "cannot assign to 1" + "hi" = 7 // ERROR `cannot assign to "hi"` + nil = 7 // ERROR "cannot assign to nil" + len("") = 7 // ERROR `cannot assign to len\(""\)` + []int{} = nil // ERROR "cannot assign to \[\]int\{\}" + + var x int = 7 + x + 1 = 7 // ERROR "cannot assign to x \+ 1" +} diff --git a/platform/dbops/binaries/go/go/test/chancap.go b/platform/dbops/binaries/go/go/test/chancap.go new file mode 100644 index 0000000000000000000000000000000000000000..8dce9247cd45ef710ef728c1d85d1e2d7c28e9e4 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/chancap.go @@ -0,0 +1,71 @@ +// run + +// Copyright 2009 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. + +// Test the cap predeclared function applied to channels. + +package main + +import ( + "strings" + "unsafe" +) + +type T chan int + +const ptrSize = unsafe.Sizeof((*byte)(nil)) + +func main() { + c := make(T, 10) + if len(c) != 0 || cap(c) != 10 { + println("chan len/cap ", len(c), cap(c), " want 0 10") + panic("fail") + } + + for i := 0; i < 3; i++ { + c <- i + } + if len(c) != 3 || cap(c) != 10 { + println("chan len/cap ", len(c), cap(c), " want 3 10") + panic("fail") + } + + c = make(T) + if len(c) != 0 || cap(c) != 0 { + println("chan len/cap ", len(c), cap(c), " want 0 0") + panic("fail") + } + + n := -1 + shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) + shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) }) + if ptrSize == 8 { + // Test mem > maxAlloc + var n2 int64 = 1 << 59 + shouldPanic("makechan: size out of range", func() { _ = make(T, int(n2)) }) + // Test elem.size*cap overflow + n2 = 1<<63 - 1 + shouldPanic("makechan: size out of range", func() { _ = make(T, int(n2)) }) + } else { + n = 1<<31 - 1 + shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) + shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) }) + } +} + +func shouldPanic(str string, f func()) { + defer func() { + err := recover() + if err == nil { + panic("did not panic") + } + s := err.(error).Error() + if !strings.Contains(s, str) { + panic("got panic " + s + ", want " + str) + } + }() + + f() +} diff --git a/platform/dbops/binaries/go/go/test/chanlinear.go b/platform/dbops/binaries/go/go/test/chanlinear.go new file mode 100644 index 0000000000000000000000000000000000000000..87faa84c93071925d51df04e5556f207c05479c2 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/chanlinear.go @@ -0,0 +1,95 @@ +// run + +//go:build darwin || linux + +// Copyright 2014 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. + +// Test that dequeuing from a pending channel doesn't +// take linear time. + +package main + +import ( + "fmt" + "runtime" + "time" +) + +// checkLinear asserts that the running time of f(n) is in O(n). +// tries is the initial number of iterations. +func checkLinear(typ string, tries int, f func(n int)) { + // Depending on the machine and OS, this test might be too fast + // to measure with accurate enough granularity. On failure, + // make it run longer, hoping that the timing granularity + // is eventually sufficient. + + timeF := func(n int) time.Duration { + t1 := time.Now() + f(n) + return time.Since(t1) + } + + t0 := time.Now() + + n := tries + fails := 0 + for { + runtime.GC() + t1 := timeF(n) + runtime.GC() + t2 := timeF(2 * n) + + // should be 2x (linear); allow up to 3x + if t2 < 3*t1 { + if false { + fmt.Println(typ, "\t", time.Since(t0)) + } + return + } + // If n ops run in under a second and the ratio + // doesn't work out, make n bigger, trying to reduce + // the effect that a constant amount of overhead has + // on the computed ratio. + if t1 < 1*time.Second { + n *= 2 + continue + } + // Once the test runs long enough for n ops, + // try to get the right ratio at least once. + // If five in a row all fail, give up. + if fails++; fails >= 5 { + panic(fmt.Sprintf("%s: too slow: %d channels: %v; %d channels: %v\n", + typ, n, t1, 2*n, t2)) + } + } +} + +func main() { + checkLinear("chanSelect", 1000, func(n int) { + const messages = 10 + c := make(chan bool) // global channel + var a []chan bool // local channels for each goroutine + for i := 0; i < n; i++ { + d := make(chan bool) + a = append(a, d) + go func() { + for j := 0; j < messages; j++ { + // queue ourselves on the global channel + select { + case <-c: + case <-d: + } + } + }() + } + for i := 0; i < messages; i++ { + // wake each goroutine up, forcing it to dequeue and then enqueue + // on the global channel. + for _, d := range a { + d <- true + } + } + }) +} diff --git a/platform/dbops/binaries/go/go/test/char_lit.go b/platform/dbops/binaries/go/go/test/char_lit.go new file mode 100644 index 0000000000000000000000000000000000000000..836c3c1a2d0b0de309abe4b8a6ffa0aabc85668a --- /dev/null +++ b/platform/dbops/binaries/go/go/test/char_lit.go @@ -0,0 +1,45 @@ +// run + +// Copyright 2009 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. + +// Test character literal syntax. + +package main + +import "os" + +func main() { + var i uint64 = + ' ' + + 'a' + + 'ä' + + '本' + + '\a' + + '\b' + + '\f' + + '\n' + + '\r' + + '\t' + + '\v' + + '\\' + + '\'' + + '\000' + + '\123' + + '\x00' + + '\xca' + + '\xFE' + + '\u0123' + + '\ubabe' + + '\U0010FFFF' + + '\U000ebabe' + if '\U000ebabe' != 0x000ebabe { + print("ebabe wrong\n") + os.Exit(1) + } + if i != 0x20e213 { + print("number is ", i, " should be ", 0x20e213, "\n") + os.Exit(1) + } +} diff --git a/platform/dbops/binaries/go/go/test/char_lit1.go b/platform/dbops/binaries/go/go/test/char_lit1.go new file mode 100644 index 0000000000000000000000000000000000000000..8899aff83aba34a42e887f0faa48e2630fbfd686 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/char_lit1.go @@ -0,0 +1,28 @@ +// errorcheck -d=panic + +// Copyright 2009 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. + +// Verify that illegal character literals are detected. +// Does not compile. + +package main + +const ( + // check that surrogate pair elements are invalid + // (d800-dbff, dc00-dfff). + _ = '\ud7ff' // ok + _ = '\ud800' // ERROR "Unicode|unicode" + _ = "\U0000D999" // ERROR "Unicode|unicode" + _ = '\udc01' // ERROR "Unicode|unicode" + _ = '\U0000dddd' // ERROR "Unicode|unicode" + _ = '\udfff' // ERROR "Unicode|unicode" + _ = '\ue000' // ok + _ = '\U0010ffff' // ok + _ = '\U00110000' // ERROR "Unicode|unicode" + _ = "abc\U0010ffffdef" // ok + _ = "abc\U00110000def" // ERROR "Unicode|unicode" + _ = '\Uffffffff' // ERROR "Unicode|unicode" +) + diff --git a/platform/dbops/binaries/go/go/test/checkbce.go b/platform/dbops/binaries/go/go/test/checkbce.go new file mode 100644 index 0000000000000000000000000000000000000000..71acfb71ac17d46ec08acde3f0d878f40c8cec65 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/checkbce.go @@ -0,0 +1,186 @@ +// errorcheck -0 -d=ssa/check_bce/debug=3 + +//go:build amd64 && !gcflags_noopt + +// Copyright 2016 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. + +// Test that the compiler does bounds check elimination as expected. +// This avoids accidental regressions. + +package main + +import "encoding/binary" + +func f0(a []int) { + a[0] = 1 // ERROR "Found IsInBounds$" + a[0] = 1 + a[6] = 1 // ERROR "Found IsInBounds$" + a[6] = 1 + a[5] = 1 + a[5] = 1 +} + +func f1(a [256]int, i int) { + var j int + useInt(a[i]) // ERROR "Found IsInBounds$" + j = i % 256 + useInt(a[j]) // ERROR "Found IsInBounds$" + j = i & 255 + useInt(a[j]) + j = i & 17 + useInt(a[j]) + + if 4 <= i && i < len(a) { + useInt(a[i]) + useInt(a[i-1]) + useInt(a[i-4]) + } +} + +func f2(a [256]int, i uint) { + useInt(a[i]) // ERROR "Found IsInBounds$" + j := i % 256 + useInt(a[j]) + j = i & 255 + useInt(a[j]) + j = i & 17 + useInt(a[j]) +} + +func f2a(a [35]int, i uint8) { + useInt(a[i]) // ERROR "Found IsInBounds$" + j := i & 34 + useInt(a[j]) + j = i & 17 + useInt(a[j]) +} + +func f2b(a [35]int, i uint16) { + useInt(a[i]) // ERROR "Found IsInBounds$" + j := i & 34 + useInt(a[j]) + j = i & 17 + useInt(a[j]) +} + +func f2c(a [35]int, i uint32) { + useInt(a[i]) // ERROR "Found IsInBounds$" + j := i & 34 + useInt(a[j]) + j = i & 17 + useInt(a[j]) +} + +func f3(a [256]int, i uint8) { + useInt(a[i]) + useInt(a[i+10]) + useInt(a[i+14]) +} + +func f4(a [27]int, i uint8) { + useInt(a[i%15]) + useInt(a[i%19]) + useInt(a[i%27]) +} + +func f5(a []int) { + if len(a) > 5 { + useInt(a[5]) + useSlice(a[6:]) + useSlice(a[:6]) + } +} + +func f6(a [32]int, b [64]int, i int) { + useInt(a[uint32(i*0x07C4ACDD)>>27]) + useInt(b[uint64(i*0x07C4ACDD)>>58]) + useInt(a[uint(i*0x07C4ACDD)>>59]) + + // The following bounds should not be removed because they can overflow. + useInt(a[uint32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$" + useInt(b[uint64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$" + useInt(a[int32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$" + useInt(b[int64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$" +} + +func g1(a []int) { + for i := range a { + a[i] = i + useSlice(a[:i+1]) + useSlice(a[:i]) + } +} + +func g2(a []int) { + useInt(a[3]) // ERROR "Found IsInBounds$" + useInt(a[2]) + useInt(a[1]) + useInt(a[0]) +} + +func g3(a []int) { + for i := range a[:256] { // ERROR "Found IsSliceInBounds$" + useInt(a[i]) // ERROR "Found IsInBounds$" + } + b := a[:256] + for i := range b { + useInt(b[i]) + } +} + +func g4(a [100]int) { + for i := 10; i < 50; i++ { + useInt(a[i-10]) + useInt(a[i]) + useInt(a[i+25]) + useInt(a[i+50]) + + // The following are out of bounds. + if a[0] == 0xdeadbeef { + // This is a trick to prohibit sccp to optimize out the following out of bound check + continue + } + useInt(a[i-11]) // ERROR "Found IsInBounds$" + useInt(a[i+51]) // ERROR "Found IsInBounds$" + } +} + +func decode1(data []byte) (x uint64) { + for len(data) >= 32 { + x += binary.BigEndian.Uint64(data[:8]) + x += binary.BigEndian.Uint64(data[8:16]) + x += binary.BigEndian.Uint64(data[16:24]) + x += binary.BigEndian.Uint64(data[24:32]) + data = data[32:] + } + return x +} + +func decode2(data []byte) (x uint64) { + // TODO(rasky): this should behave like decode1 and compile to no + // boundchecks. We're currently not able to remove all of them. + for len(data) >= 32 { + x += binary.BigEndian.Uint64(data) + data = data[8:] + x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$" + data = data[8:] + x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$" + data = data[8:] + x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$" + data = data[8:] + } + return x +} + +//go:noinline +func useInt(a int) { +} + +//go:noinline +func useSlice(a []int) { +} + +func main() { +} diff --git a/platform/dbops/binaries/go/go/test/clear.go b/platform/dbops/binaries/go/go/test/clear.go new file mode 100644 index 0000000000000000000000000000000000000000..60ee4ecf85edf8a02aace79143cae79fd577a913 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/clear.go @@ -0,0 +1,47 @@ +// run + +// Copyright 2022 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. + +package main + +import "math" + +func checkClearSlice() { + s := []int{1, 2, 3} + clear(s) + for i := range s { + if s[i] != 0 { + panic("clear not zeroing slice elem") + } + } + + clear([]int{}) +} + +func checkClearMap() { + m1 := make(map[int]int) + m1[0] = 0 + m1[1] = 1 + clear(m1) + if len(m1) != 0 { + panic("m1 is not cleared") + } + + // map contains NaN keys is also cleared. + m2 := make(map[float64]int) + m2[math.NaN()] = 1 + m2[math.NaN()] = 1 + clear(m2) + if len(m2) != 0 { + panic("m2 is not cleared") + } + + clear(map[int]int{}) +} + +func main() { + checkClearSlice() + checkClearMap() +} diff --git a/platform/dbops/binaries/go/go/test/clearfat.go b/platform/dbops/binaries/go/go/test/clearfat.go new file mode 100644 index 0000000000000000000000000000000000000000..45d539306e0a074d22088df342d20829a123d3f1 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/clearfat.go @@ -0,0 +1,68 @@ +// runoutput + +// Copyright 2014 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. + +// Check that {5,6,8,9}g/ggen.c:clearfat is zeroing the entire object. + +package main + +import ( + "bytes" + "fmt" + "strconv" + "strings" +) + +const ntest = 1100 + +func main() { + var decls, calls bytes.Buffer + + for i := 1; i <= ntest; i++ { + s := strconv.Itoa(i) + decls.WriteString(strings.Replace(decl, "$", s, -1)) + calls.WriteString(strings.Replace("poison$()\n\tclearfat$()\n\t", "$", s, -1)) + } + + program = strings.Replace(program, "$DECLS", decls.String(), 1) + program = strings.Replace(program, "$CALLS", calls.String(), 1) + fmt.Print(program) +} + +var program = `package main + +var count int + +$DECLS + +func main() { + $CALLS + if count != 0 { + println("failed", count, "case(s)") + } +} +` + +const decl = ` +func poison$() { + // Grow and poison the stack space that will be used by clearfat$ + var t [2*$]byte + for i := range t { + t[i] = 0xff + } +} + +func clearfat$() { + var t [$]byte + + for _, x := range t { + if x != 0 { +// println("clearfat$: index", i, "expected 0, got", x) + count++ + break + } + } +} +` diff --git a/platform/dbops/binaries/go/go/test/closedchan.go b/platform/dbops/binaries/go/go/test/closedchan.go new file mode 100644 index 0000000000000000000000000000000000000000..043a92d3880e83fb8aef11cc358bf1e49d02a7ab --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closedchan.go @@ -0,0 +1,359 @@ +// run + +// Copyright 2009 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. + +// Test close(c), receive of closed channel. +// +// TODO(rsc): Doesn't check behavior of close(c) when there +// are blocked senders/receivers. + +package main + +import "os" + +var failed bool + +type Chan interface { + Send(int) + Nbsend(int) bool + Recv() (int) + Nbrecv() (int, bool) + Recv2() (int, bool) + Nbrecv2() (int, bool, bool) + Close() + Impl() string +} + +// direct channel operations when possible +type XChan chan int + +func (c XChan) Send(x int) { + c <- x +} + +func (c XChan) Nbsend(x int) bool { + select { + case c <- x: + return true + default: + return false + } + panic("nbsend") +} + +func (c XChan) Recv() int { + return <-c +} + +func (c XChan) Nbrecv() (int, bool) { + select { + case x := <-c: + return x, true + default: + return 0, false + } + panic("nbrecv") +} + +func (c XChan) Recv2() (int, bool) { + x, ok := <-c + return x, ok +} + +func (c XChan) Nbrecv2() (int, bool, bool) { + select { + case x, ok := <-c: + return x, ok, true + default: + return 0, false, false + } + panic("nbrecv2") +} + +func (c XChan) Close() { + close(c) +} + +func (c XChan) Impl() string { + return "(<- operator)" +} + +// indirect operations via select +type SChan chan int + +func (c SChan) Send(x int) { + select { + case c <- x: + } +} + +func (c SChan) Nbsend(x int) bool { + select { + default: + return false + case c <- x: + return true + } + panic("nbsend") +} + +func (c SChan) Recv() int { + select { + case x := <-c: + return x + } + panic("recv") +} + +func (c SChan) Nbrecv() (int, bool) { + select { + default: + return 0, false + case x := <-c: + return x, true + } + panic("nbrecv") +} + +func (c SChan) Recv2() (int, bool) { + select { + case x, ok := <-c: + return x, ok + } + panic("recv") +} + +func (c SChan) Nbrecv2() (int, bool, bool) { + select { + default: + return 0, false, false + case x, ok := <-c: + return x, ok, true + } + panic("nbrecv") +} + +func (c SChan) Close() { + close(c) +} + +func (c SChan) Impl() string { + return "(select)" +} + +// indirect operations via larger selects +var dummy = make(chan bool) + +type SSChan chan int + +func (c SSChan) Send(x int) { + select { + case c <- x: + case <-dummy: + } +} + +func (c SSChan) Nbsend(x int) bool { + select { + default: + return false + case <-dummy: + case c <- x: + return true + } + panic("nbsend") +} + +func (c SSChan) Recv() int { + select { + case <-dummy: + case x := <-c: + return x + } + panic("recv") +} + +func (c SSChan) Nbrecv() (int, bool) { + select { + case <-dummy: + default: + return 0, false + case x := <-c: + return x, true + } + panic("nbrecv") +} + +func (c SSChan) Recv2() (int, bool) { + select { + case <-dummy: + case x, ok := <-c: + return x, ok + } + panic("recv") +} + +func (c SSChan) Nbrecv2() (int, bool, bool) { + select { + case <-dummy: + default: + return 0, false, false + case x, ok := <-c: + return x, ok, true + } + panic("nbrecv") +} + +func (c SSChan) Close() { + close(c) +} + +func (c SSChan) Impl() string { + return "(select)" +} + + +func shouldPanic(f func()) { + defer func() { + if recover() == nil { + panic("did not panic") + } + }() + f() +} + +func test1(c Chan) { + for i := 0; i < 3; i++ { + // recv a close signal (a zero value) + if x := c.Recv(); x != 0 { + println("test1: recv on closed:", x, c.Impl()) + failed = true + } + if x, ok := c.Recv2(); x != 0 || ok { + println("test1: recv2 on closed:", x, ok, c.Impl()) + failed = true + } + + // should work with select: received a value without blocking, so selected == true. + x, selected := c.Nbrecv() + if x != 0 || !selected { + println("test1: recv on closed nb:", x, selected, c.Impl()) + failed = true + } + x, ok, selected := c.Nbrecv2() + if x != 0 || ok || !selected { + println("test1: recv2 on closed nb:", x, ok, selected, c.Impl()) + failed = true + } + } + + // send should work with ,ok too: sent a value without blocking, so ok == true. + shouldPanic(func() { c.Nbsend(1) }) + + // the value should have been discarded. + if x := c.Recv(); x != 0 { + println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()) + failed = true + } + + // similarly Send. + shouldPanic(func() { c.Send(2) }) + if x := c.Recv(); x != 0 { + println("test1: recv on closed got non-zero after send on closed:", x, c.Impl()) + failed = true + } +} + +func testasync1(c Chan) { + // should be able to get the last value via Recv + if x := c.Recv(); x != 1 { + println("testasync1: Recv did not get 1:", x, c.Impl()) + failed = true + } + + test1(c) +} + +func testasync2(c Chan) { + // should be able to get the last value via Recv2 + if x, ok := c.Recv2(); x != 1 || !ok { + println("testasync1: Recv did not get 1, true:", x, ok, c.Impl()) + failed = true + } + + test1(c) +} + +func testasync3(c Chan) { + // should be able to get the last value via Nbrecv + if x, selected := c.Nbrecv(); x != 1 || !selected { + println("testasync2: Nbrecv did not get 1, true:", x, selected, c.Impl()) + failed = true + } + + test1(c) +} + +func testasync4(c Chan) { + // should be able to get the last value via Nbrecv2 + if x, ok, selected := c.Nbrecv2(); x != 1 || !ok || !selected { + println("testasync2: Nbrecv did not get 1, true, true:", x, ok, selected, c.Impl()) + failed = true + } + test1(c) +} + +func closedsync() chan int { + c := make(chan int) + close(c) + return c +} + +func closedasync() chan int { + c := make(chan int, 2) + c <- 1 + close(c) + return c +} + +var mks = []func(chan int) Chan { + func(c chan int) Chan { return XChan(c) }, + func(c chan int) Chan { return SChan(c) }, + func(c chan int) Chan { return SSChan(c) }, +} + +var testcloseds = []func(Chan) { + testasync1, + testasync2, + testasync3, + testasync4, +} + +func main() { + for _, mk := range mks { + test1(mk(closedsync())) + } + + for _, testclosed := range testcloseds { + for _, mk := range mks { + testclosed(mk(closedasync())) + } + } + + var ch chan int + shouldPanic(func() { + close(ch) + }) + + ch = make(chan int) + close(ch) + shouldPanic(func() { + close(ch) + }) + + if failed { + os.Exit(1) + } +} diff --git a/platform/dbops/binaries/go/go/test/closure.go b/platform/dbops/binaries/go/go/test/closure.go new file mode 100644 index 0000000000000000000000000000000000000000..c4a312464bcf4e130143941e67421b1a1615fa94 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure.go @@ -0,0 +1,128 @@ +// run + +// Copyright 2009 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. + +// Test the behavior of closures. + +package main + +import "runtime" + +var c = make(chan int) + +func check(a []int) { + for i := 0; i < len(a); i++ { + n := <-c + if n != a[i] { + println("want", a[i], "got", n, "at", i) + panic("fail") + } + } +} + +func f() { + var i, j int + + i = 1 + j = 2 + f := func() { + c <- i + i = 4 + g := func() { + c <- i + c <- j + } + g() + c <- i + } + j = 5 + f() +} + +// Accumulator generator +func accum(n int) func(int) int { + return func(i int) int { + n += i + return n + } +} + +func g(a, b func(int) int) { + c <- a(2) + c <- b(3) + c <- a(4) + c <- b(5) +} + +func h() { + var x8 byte = 100 + var x64 int64 = 200 + + c <- int(x8) + c <- int(x64) + f := func(z int) { + g := func() { + c <- int(x8) + c <- int(x64) + c <- z + } + g() + c <- int(x8) + c <- int(x64) + c <- int(z) + } + x8 = 101 + x64 = 201 + f(500) +} + +func newfunc() func(int) int { return func(x int) int { return x } } + +func main() { + runtime.GOMAXPROCS(1) + var fail bool + + go f() + check([]int{1, 4, 5, 4}) + + a := accum(0) + b := accum(1) + go g(a, b) + check([]int{2, 4, 6, 9}) + + go h() + check([]int{100, 200, 101, 201, 500, 101, 201, 500}) + + memstats := new(runtime.MemStats) + runtime.ReadMemStats(memstats) + n0 := memstats.Mallocs + + x, y := newfunc(), newfunc() + if x(1) != 1 || y(2) != 2 { + println("newfunc returned broken funcs") + fail = true + } + + runtime.ReadMemStats(memstats) + if n0 != memstats.Mallocs { + println("newfunc allocated unexpectedly") + fail = true + } + + ff(1) + + if fail { + panic("fail") + } +} + +func ff(x int) { + call(func() { + _ = x + }) +} + +func call(func()) { +} diff --git a/platform/dbops/binaries/go/go/test/closure1.go b/platform/dbops/binaries/go/go/test/closure1.go new file mode 100644 index 0000000000000000000000000000000000000000..586998203aa7791bb2d2c6d82806dff852ea6373 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure1.go @@ -0,0 +1,19 @@ +// run + +// Copyright 2015 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. + +package main + +func main() { + x := 0 + func() { + x = 1 + }() + func() { + if x != 1 { + panic("x != 1") + } + }() +} \ No newline at end of file diff --git a/platform/dbops/binaries/go/go/test/closure2.go b/platform/dbops/binaries/go/go/test/closure2.go new file mode 100644 index 0000000000000000000000000000000000000000..081d2e2d3d178595cc60028722a6aec07c11ef46 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure2.go @@ -0,0 +1,133 @@ +// run + +// Copyright 2015 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. + +// Check that these do not use "by value" capturing, +// because changes are made to the value during the closure. + +package main + +var never bool + +func main() { + { + type X struct { + v int + } + var x X + func() { + x.v++ + }() + if x.v != 1 { + panic("x.v != 1") + } + + type Y struct { + X + } + var y Y + func() { + y.v = 1 + }() + if y.v != 1 { + panic("y.v != 1") + } + } + + { + type Z struct { + a [3]byte + } + var z Z + func() { + i := 0 + for z.a[1] = 1; i < 10; i++ { + } + }() + if z.a[1] != 1 { + panic("z.a[1] != 1") + } + } + + { + w := 0 + tmp := 0 + f := func() { + if w != 1 { + panic("w != 1") + } + } + func() { + tmp = w // force capture of w, but do not write to it yet + _ = tmp + func() { + func() { + w++ // write in a nested closure + }() + }() + }() + f() + } + + { + var g func() int + var i int + for i = range [2]int{} { + if i == 0 { + g = func() int { + return i // test that we capture by ref here, i is mutated on every interaction + } + } + } + if g() != 1 { + panic("g() != 1") + } + } + + { + var g func() int + q := 0 + for range [2]int{} { + q++ + g = func() int { + return q // test that we capture by ref here + // q++ must on a different decldepth than q declaration + } + } + if g() != 2 { + panic("g() != 2") + } + } + + { + var g func() int + var a [2]int + q := 0 + for a[func() int { + q++ + return 0 + }()] = range [2]int{} { + g = func() int { + return q // test that we capture by ref here + // q++ must on a different decldepth than q declaration + } + } + if g() != 2 { + panic("g() != 2") + } + } + + { + var g func() int + q := 0 + q, g = 1, func() int { return q } + if never { + g = func() int { return 2 } + } + if g() != 1 { + panic("g() != 1") + } + } +} diff --git a/platform/dbops/binaries/go/go/test/closure3.go b/platform/dbops/binaries/go/go/test/closure3.go new file mode 100644 index 0000000000000000000000000000000000000000..24f8cefc30897489cdec9c0038c6c457436ab450 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure3.go @@ -0,0 +1,12 @@ +// errorcheckandrundir -0 -m -d=inlfuncswithclosures=1 + +//go:build !goexperiment.newinliner + +// Copyright 2017 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. + +// Check correctness of various closure corner cases +// that are expected to be inlined + +package ignored diff --git a/platform/dbops/binaries/go/go/test/closure4.go b/platform/dbops/binaries/go/go/test/closure4.go new file mode 100644 index 0000000000000000000000000000000000000000..ec4e0a18ebec8391787ed9d05224527d7c92d10e --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure4.go @@ -0,0 +1,21 @@ +// run + +// Copyright 2018 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. + +// Check that calling a nil func causes a proper panic. + +package main + +func main() { + defer func() { + err := recover() + if err == nil { + panic("panic expected") + } + }() + + var f func() + f() +} diff --git a/platform/dbops/binaries/go/go/test/closure5.go b/platform/dbops/binaries/go/go/test/closure5.go new file mode 100644 index 0000000000000000000000000000000000000000..a7022b27a68f707f6cbedc9ba94e91e554fd853f --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure5.go @@ -0,0 +1,10 @@ +// compiledir + +// Copyright 2021 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. + +// Check correctness of various closure corner cases +// that are expected to be inlined + +package ignored diff --git a/platform/dbops/binaries/go/go/test/closure6.go b/platform/dbops/binaries/go/go/test/closure6.go new file mode 100644 index 0000000000000000000000000000000000000000..b5592ad3d3e076b9f38ddb336bdabe86c0e6bcb6 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure6.go @@ -0,0 +1,18 @@ +// compile + +// 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. + +package p + +type Float64Slice []float64 + +func (a Float64Slice) Search1(x float64) int { + f := func(q int) bool { return a[q] >= x } + i := 0 + if !f(3) { + i = 5 + } + return i +} diff --git a/platform/dbops/binaries/go/go/test/closure7.go b/platform/dbops/binaries/go/go/test/closure7.go new file mode 100644 index 0000000000000000000000000000000000000000..823333f45fd25694714e0a509a1c8fa9c79a95bd --- /dev/null +++ b/platform/dbops/binaries/go/go/test/closure7.go @@ -0,0 +1,28 @@ +// run + +// 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. + +package main + +func g(f func()) { +} + +// Must have exportable name +func F() { + g(func() { + ch := make(chan int) + for { + select { + case <-ch: + return + default: + } + } + }) +} + +func main() { + F() +} diff --git a/platform/dbops/binaries/go/go/test/cmp.go b/platform/dbops/binaries/go/go/test/cmp.go new file mode 100644 index 0000000000000000000000000000000000000000..6db9ce5cc85b8b6223e88479301853bbe3cd009a --- /dev/null +++ b/platform/dbops/binaries/go/go/test/cmp.go @@ -0,0 +1,488 @@ +// run + +// Copyright 2009 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. + +// Test equality and inequality operations. + +package main + +import ( + "os" + "unsafe" +) + +var global bool + +func use(b bool) { global = b } + +func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) } + +func isfalse(b bool) { + if b { + // stack will explain where + panic("wanted false, got true") + } +} + +func istrue(b bool) { + if !b { + // stack will explain where + panic("wanted true, got false") + } +} + +type T *int + +type X int + +func (X) x() {} + +func main() { + var a []int + var b map[string]int + + var c string = "hello" + var d string = "hel" // try to get different pointer + d = d + "lo" + + // go.tools/ssa/interp can't handle unsafe.Pointer. + if os.Getenv("GOSSAINTERP") == "" { + if stringptr(c) == stringptr(d) { + panic("compiler too smart -- got same string") + } + } + + var e = make(chan int) + + var ia interface{} = a + var ib interface{} = b + var ic interface{} = c + var id interface{} = d + var ie interface{} = e + + // these comparisons are okay because + // string compare is okay and the others + // are comparisons where the types differ. + isfalse(ia == ib) + isfalse(ia == ic) + isfalse(ia == id) + isfalse(ib == ic) + isfalse(ib == id) + istrue(ic == id) + istrue(ie == ie) + + istrue(ia != ib) + istrue(ia != ic) + istrue(ia != id) + istrue(ib != ic) + istrue(ib != id) + isfalse(ic != id) + isfalse(ie != ie) + + // these are not okay, because there is no comparison on slices or maps. + //isfalse(a == ib) + //isfalse(a == ic) + //isfalse(a == id) + //isfalse(b == ic) + //isfalse(b == id) + + istrue(c == id) + istrue(e == ie) + + //isfalse(ia == b) + isfalse(ia == c) + isfalse(ia == d) + isfalse(ib == c) + isfalse(ib == d) + istrue(ic == d) + istrue(ie == e) + + //istrue(a != ib) + //istrue(a != ic) + //istrue(a != id) + //istrue(b != ic) + //istrue(b != id) + isfalse(c != id) + isfalse(e != ie) + + //istrue(ia != b) + istrue(ia != c) + istrue(ia != d) + istrue(ib != c) + istrue(ib != d) + isfalse(ic != d) + isfalse(ie != e) + + // gc used to let this go through as true. + var g uint64 = 123 + var h int64 = 123 + var ig interface{} = g + var ih interface{} = h + isfalse(ig == ih) + istrue(ig != ih) + + // map of interface should use == on interface values, + // not memory. + var m = make(map[interface{}]int) + m[ic] = 1 + m[id] = 2 + if m[c] != 2 { + println("m[c] = ", m[c]) + panic("bad m[c]") + } + + // interface comparisons (issue 7207) + { + type I1 interface { + x() + } + type I2 interface { + x() + } + a1 := I1(X(0)) + b1 := I1(X(1)) + a2 := I2(X(0)) + b2 := I2(X(1)) + a3 := I1(a2) + a4 := I2(a1) + var e interface{} = X(0) + a5 := e.(I1) + a6 := e.(I2) + isfalse(a1 == b1) + isfalse(a1 == b2) + isfalse(a2 == b1) + isfalse(a2 == b2) + istrue(a1 == a2) + istrue(a1 == a3) + istrue(a1 == a4) + istrue(a1 == a5) + istrue(a1 == a6) + istrue(a2 == a3) + istrue(a2 == a4) + istrue(a2 == a5) + istrue(a2 == a6) + istrue(a3 == a4) + istrue(a3 == a5) + istrue(a3 == a6) + istrue(a4 == a5) + istrue(a4 == a6) + istrue(a5 == a6) + } + + // non-interface comparisons + { + c := make(chan int) + c1 := (<-chan int)(c) + c2 := (chan<- int)(c) + istrue(c == c1) + istrue(c == c2) + istrue(c1 == c) + istrue(c2 == c) + + isfalse(c != c1) + isfalse(c != c2) + isfalse(c1 != c) + isfalse(c2 != c) + + d := make(chan int) + isfalse(c == d) + isfalse(d == c) + isfalse(d == c1) + isfalse(d == c2) + isfalse(c1 == d) + isfalse(c2 == d) + + istrue(c != d) + istrue(d != c) + istrue(d != c1) + istrue(d != c2) + istrue(c1 != d) + istrue(c2 != d) + } + + // named types vs not + { + var x = new(int) + var y T + var z T = x + + isfalse(x == y) + istrue(x == z) + isfalse(y == z) + + isfalse(y == x) + istrue(z == x) + isfalse(z == y) + + istrue(x != y) + isfalse(x != z) + istrue(y != z) + + istrue(y != x) + isfalse(z != x) + istrue(z != y) + } + + // structs + { + var x = struct { + x int + y string + }{1, "hi"} + var y = struct { + x int + y string + }{2, "bye"} + var z = struct { + x int + y string + }{1, "hi"} + + isfalse(x == y) + isfalse(y == x) + isfalse(y == z) + isfalse(z == y) + istrue(x == z) + istrue(z == x) + + istrue(x != y) + istrue(y != x) + istrue(y != z) + istrue(z != y) + isfalse(x != z) + isfalse(z != x) + + var m = make(map[struct { + x int + y string + }]int) + m[x] = 10 + m[y] = 20 + m[z] = 30 + istrue(m[x] == 30) + istrue(m[y] == 20) + istrue(m[z] == 30) + istrue(m[x] != 10) + isfalse(m[x] != 30) + isfalse(m[y] != 20) + isfalse(m[z] != 30) + isfalse(m[x] == 10) + + var m1 = make(map[struct { + x int + y string + }]struct { + x int + y string + }) + m1[x] = x + m1[y] = y + m1[z] = z + istrue(m1[x] == z) + istrue(m1[y] == y) + istrue(m1[z] == z) + istrue(m1[x] == x) + isfalse(m1[x] != z) + isfalse(m1[y] != y) + isfalse(m1[z] != z) + isfalse(m1[x] != x) + + var ix, iy, iz interface{} = x, y, z + + isfalse(ix == iy) + isfalse(iy == ix) + isfalse(iy == iz) + isfalse(iz == iy) + istrue(ix == iz) + istrue(iz == ix) + + isfalse(x == iy) + isfalse(y == ix) + isfalse(y == iz) + isfalse(z == iy) + istrue(x == iz) + istrue(z == ix) + + isfalse(ix == y) + isfalse(iy == x) + isfalse(iy == z) + isfalse(iz == y) + istrue(ix == z) + istrue(iz == x) + + istrue(ix != iy) + istrue(iy != ix) + istrue(iy != iz) + istrue(iz != iy) + isfalse(ix != iz) + isfalse(iz != ix) + + istrue(x != iy) + istrue(y != ix) + istrue(y != iz) + istrue(z != iy) + isfalse(x != iz) + isfalse(z != ix) + + istrue(ix != y) + istrue(iy != x) + istrue(iy != z) + istrue(iz != y) + isfalse(ix != z) + isfalse(iz != x) + } + + // structs with _ fields + { + var x = struct { + x int + _ string + y float64 + _ float64 + z int + }{ + x: 1, y: 2, z: 3, + } + var ix interface{} = x + + istrue(x == x) + istrue(x == ix) + istrue(ix == x) + istrue(ix == ix) + } + + // arrays + { + var x = [2]string{"1", "hi"} + var y = [2]string{"2", "bye"} + var z = [2]string{"1", "hi"} + + isfalse(x == y) + isfalse(y == x) + isfalse(y == z) + isfalse(z == y) + istrue(x == z) + istrue(z == x) + + istrue(x != y) + istrue(y != x) + istrue(y != z) + istrue(z != y) + isfalse(x != z) + isfalse(z != x) + + var m = make(map[[2]string]int) + m[x] = 10 + m[y] = 20 + m[z] = 30 + istrue(m[x] == 30) + istrue(m[y] == 20) + istrue(m[z] == 30) + isfalse(m[x] != 30) + isfalse(m[y] != 20) + isfalse(m[z] != 30) + + var ix, iy, iz interface{} = x, y, z + + isfalse(ix == iy) + isfalse(iy == ix) + isfalse(iy == iz) + isfalse(iz == iy) + istrue(ix == iz) + istrue(iz == ix) + + isfalse(x == iy) + isfalse(y == ix) + isfalse(y == iz) + isfalse(z == iy) + istrue(x == iz) + istrue(z == ix) + + isfalse(ix == y) + isfalse(iy == x) + isfalse(iy == z) + isfalse(iz == y) + istrue(ix == z) + istrue(iz == x) + + istrue(ix != iy) + istrue(iy != ix) + istrue(iy != iz) + istrue(iz != iy) + isfalse(ix != iz) + isfalse(iz != ix) + + istrue(x != iy) + istrue(y != ix) + istrue(y != iz) + istrue(z != iy) + isfalse(x != iz) + isfalse(z != ix) + + istrue(ix != y) + istrue(iy != x) + istrue(iy != z) + istrue(iz != y) + isfalse(ix != z) + isfalse(iz != x) + } + + // named booleans + { + type mybool bool + var b mybool + + type T struct{ data [20]byte } + var x, y T + b = x == y + istrue(x == y) + istrue(bool(b)) + + m := make(map[string][10]interface{}) + b = m["x"] == m["y"] + istrue(m["x"] == m["y"]) + istrue(bool(b)) + } + + shouldPanic(p1) + shouldPanic(p2) + shouldPanic(p3) + shouldPanic(p4) +} + +func p1() { + var a []int + var ia interface{} = a + use(ia == ia) +} + +func p2() { + var b []int + var ib interface{} = b + use(ib == ib) +} + +func p3() { + var a []int + var ia interface{} = a + var m = make(map[interface{}]int) + m[ia] = 1 +} + +func p4() { + var b []int + var ib interface{} = b + var m = make(map[interface{}]int) + m[ib] = 1 +} + +func shouldPanic(f func()) { + defer func() { + if recover() == nil { + panic("function should panic") + } + }() + f() +} diff --git a/platform/dbops/binaries/go/go/test/cmp6.go b/platform/dbops/binaries/go/go/test/cmp6.go new file mode 100644 index 0000000000000000000000000000000000000000..704ead2caa89ffb6d8160be69fbce6a2c95ef538 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/cmp6.go @@ -0,0 +1,86 @@ +// errorcheck + +// Copyright 2010 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. + +// Verify that incorrect comparisons are detected. +// Does not compile. + +package main + +func use(bool) {} + +type T1 *int +type T2 *int + +type T3 struct{ z []int } + +var t3 T3 + +type T4 struct { + _ []int + a float64 +} + +var t4 T4 + +func main() { + // Arguments to comparison must be + // assignable one to the other (or vice versa) + // so chan int can be compared against + // directional channels but channel of different + // direction cannot be compared against each other. + var c1 chan<- int + var c2 <-chan int + var c3 chan int + + use(c1 == c2) // ERROR "invalid operation|incompatible" + use(c2 == c1) // ERROR "invalid operation|incompatible" + use(c1 == c3) + use(c2 == c2) + use(c3 == c1) + use(c3 == c2) + + // Same applies to named types. + var p1 T1 + var p2 T2 + var p3 *int + + use(p1 == p2) // ERROR "invalid operation|incompatible" + use(p2 == p1) // ERROR "invalid operation|incompatible" + use(p1 == p3) + use(p2 == p2) + use(p3 == p1) + use(p3 == p2) + + // Arrays are comparable if and only if their element type is comparable. + var a1 [1]int + var a2 [1]func() + var a3 [0]func() + use(a1 == a1) + use(a2 == a2) // ERROR "invalid operation|invalid comparison" + use(a3 == a3) // ERROR "invalid operation|invalid comparison" + + // Comparison of structs should have a good message + use(t3 == t3) // ERROR "struct|expected|cannot compare" + use(t4 == t4) // ERROR "cannot be compared|non-comparable|cannot compare" + + // Slices, functions, and maps too. + var x []int + var f func() + var m map[int]int + use(x == x) // ERROR "slice can only be compared to nil|cannot compare" + use(f == f) // ERROR "func can only be compared to nil|cannot compare" + use(m == m) // ERROR "map can only be compared to nil|cannot compare" + + // Comparison with interface that cannot return true + // (would panic). + var i interface{} + use(i == x) // ERROR "invalid operation" + use(x == i) // ERROR "invalid operation" + use(i == f) // ERROR "invalid operation" + use(f == i) // ERROR "invalid operation" + use(i == m) // ERROR "invalid operation" + use(m == i) // ERROR "invalid operation" +} diff --git a/platform/dbops/binaries/go/go/test/cmplx.go b/platform/dbops/binaries/go/go/test/cmplx.go new file mode 100644 index 0000000000000000000000000000000000000000..d63c7ebc7e84c84b658d8880e1c28b717003dc4b --- /dev/null +++ b/platform/dbops/binaries/go/go/test/cmplx.go @@ -0,0 +1,68 @@ +// errorcheck + +// Copyright 2010 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. + +// Verify that incorrect invocations of the complex predeclared function are detected. +// Does not compile. + +package main + +type ( + Float32 float32 + Float64 float64 + Complex64 complex64 + Complex128 complex128 +) + +var ( + f32 float32 + f64 float64 + F32 Float32 + F64 Float64 + + c64 complex64 + c128 complex128 + C64 Complex64 + C128 Complex128 +) + +func F1() int { + return 1 +} + +func F3() (int, int, int) { + return 1, 2, 3 +} + +func main() { + // ok + c64 = complex(f32, f32) + c128 = complex(f64, f64) + + _ = complex128(0) // ok + _ = complex(f32, f64) // ERROR "complex" + _ = complex(f64, f32) // ERROR "complex" + _ = complex(f32, F32) // ERROR "complex" + _ = complex(F32, f32) // ERROR "complex" + _ = complex(f64, F64) // ERROR "complex" + _ = complex(F64, f64) // ERROR "complex" + + _ = complex(F1()) // ERROR "not enough arguments" + _ = complex(F3()) // ERROR "too many arguments" + + _ = complex() // ERROR "not enough arguments" + + c128 = complex(f32, f32) // ERROR "cannot use" + c64 = complex(f64, f64) // ERROR "cannot use" + + c64 = complex(1.0, 2.0) // ok, constant is untyped + c128 = complex(1.0, 2.0) + C64 = complex(1.0, 2.0) + C128 = complex(1.0, 2.0) + + C64 = complex(f32, f32) // ERROR "cannot use" + C128 = complex(f64, f64) // ERROR "cannot use" + +} diff --git a/platform/dbops/binaries/go/go/test/cmplxdivide.c b/platform/dbops/binaries/go/go/test/cmplxdivide.c new file mode 100644 index 0000000000000000000000000000000000000000..89a2868b75bdb249b905f4243cd5c1f8485569a8 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/cmplxdivide.c @@ -0,0 +1,98 @@ +// Copyright 2010 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 C program generates the file cmplxdivide1.go. It uses the +// output of the operations by C99 as the reference to check +// the implementation of complex numbers in Go. +// The generated file, cmplxdivide1.go, is compiled along +// with the driver cmplxdivide.go (the names are confusing +// and unimaginative) to run the actual test. This is done by +// the usual test runner. +// +// The file cmplxdivide1.go is checked in to the repository, but +// if it needs to be regenerated, compile and run this C program +// like this: +// gcc '-std=c99' cmplxdivide.c && a.out >cmplxdivide1.go + +#include +#include +#include +#include + +#define nelem(x) (sizeof(x)/sizeof((x)[0])) + +double f[] = { + 0.0, + -0.0, + 1.0, + -1.0, + 2.0, + NAN, + INFINITY, + -INFINITY, +}; + +char* fmt(double g) { + static char buf[10][30]; + static int n; + char *p; + + p = buf[n++]; + if(n == 10) { + n = 0; + } + + sprintf(p, "%g", g); + + if(strcmp(p, "0") == 0) { + strcpy(p, "zero"); + return p; + } + + if(strcmp(p, "-0") == 0) { + strcpy(p, "-zero"); + return p; + } + + return p; +} + +int main(void) { + int i, j, k, l; + double complex n, d, q; + + printf("// skip\n"); + printf("// # generated by cmplxdivide.c\n"); + printf("\n"); + printf("package main\n"); + printf("\n"); + printf("import \"math\"\n"); + printf("\n"); + printf("var (\n"); + printf("\tnan = math.NaN()\n"); + printf("\tinf = math.Inf(1)\n"); + printf("\tzero = 0.0\n"); + printf(")\n"); + printf("\n"); + printf("var tests = []struct {\n"); + printf("\tf, g complex128\n"); + printf("\tout complex128\n"); + printf("}{\n"); + + for(i=0; i> 100 + c3div2 = 3 / 2 + c1e3 = 1e3 + + rsh1 = 1e100 >> 1000 + rsh2 = 1e302 >> 1000 + + ctrue = true + cfalse = !ctrue + + // Issue #34563 + _ = string(int(123)) + _ = string(rune(456)) +) + +const ( + f0 = 0.0 + fm1 = -1. + fhuge float64 = 1 << 100 + fhuge_1 float64 = chuge - 1 + f1 float64 = chuge >> 100 + f3div2 = 3. / 2. + f1e3 float64 = 1e3 +) + +func assert(t bool, s string) { + if !t { + panic(s) + } +} + +func ints() { + assert(c0 == 0, "c0") + assert(c1 == 1, "c1") + assert(chuge > chuge_1, "chuge") + assert(chuge_1+1 == chuge, "chuge 1") + assert(chuge+cm1+1 == chuge, "cm1") + assert(c3div2 == 1, "3/2") + assert(c1e3 == 1000, "c1e3 int") + assert(c1e3 == 1e3, "c1e3 float") + assert(rsh1 == 0, "rsh1") + assert(rsh2 == 9, "rsh2") + + // verify that all (in range) are assignable as ints + var i int + i = c0 + assert(i == c0, "i == c0") + i = cm1 + assert(i == cm1, "i == cm1") + i = c1 + assert(i == c1, "i == c1") + i = c3div2 + assert(i == c3div2, "i == c3div2") + i = c1e3 + assert(i == c1e3, "i == c1e3") + + // verify that all are assignable as floats + var f float64 + f = c0 + assert(f == c0, "f == c0") + f = cm1 + assert(f == cm1, "f == cm1") + f = chuge + assert(f == chuge, "f == chuge") + f = chuge_1 + assert(f == chuge_1, "f == chuge_1") + f = c1 + assert(f == c1, "f == c1") + f = c3div2 + assert(f == c3div2, "f == c3div2") + f = c1e3 + assert(f == c1e3, "f == c1e3") +} + +func floats() { + assert(f0 == c0, "f0") + assert(f1 == c1, "f1") + // TODO(gri): exp/ssa/interp constant folding is incorrect. + if os.Getenv("GOSSAINTERP") == "" { + assert(fhuge == fhuge_1, "fhuge") // float64 can't distinguish fhuge, fhuge_1. + } + assert(fhuge_1+1 == fhuge, "fhuge 1") + assert(fhuge+fm1+1 == fhuge, "fm1") + assert(f3div2 == 1.5, "3./2.") + assert(f1e3 == 1000, "f1e3 int") + assert(f1e3 == 1.e3, "f1e3 float") + + // verify that all (in range) are assignable as ints + var i int + i = f0 + assert(i == f0, "i == f0") + i = fm1 + assert(i == fm1, "i == fm1") + + // verify that all are assignable as floats + var f float64 + f = f0 + assert(f == f0, "f == f0") + f = fm1 + assert(f == fm1, "f == fm1") + f = fhuge + assert(f == fhuge, "f == fhuge") + f = fhuge_1 + assert(f == fhuge_1, "f == fhuge_1") + f = f1 + assert(f == f1, "f == f1") + f = f3div2 + assert(f == f3div2, "f == f3div2") + f = f1e3 + assert(f == f1e3, "f == f1e3") +} + +func interfaces() { + var ( + nilN interface{} + nilI *int + five = 5 + + _ = nil == interface{}(nil) + _ = interface{}(nil) == nil + ) + ii := func(i1 interface{}, i2 interface{}) bool { return i1 == i2 } + ni := func(n interface{}, i int) bool { return n == i } + in := func(i int, n interface{}) bool { return i == n } + pi := func(p *int, i interface{}) bool { return p == i } + ip := func(i interface{}, p *int) bool { return i == p } + + assert((interface{}(nil) == interface{}(nil)) == ii(nilN, nilN), + "for interface{}==interface{} compiler == runtime") + + assert(((*int)(nil) == interface{}(nil)) == pi(nilI, nilN), + "for *int==interface{} compiler == runtime") + assert((interface{}(nil) == (*int)(nil)) == ip(nilN, nilI), + "for interface{}==*int compiler == runtime") + + assert((&five == interface{}(nil)) == pi(&five, nilN), + "for interface{}==*int compiler == runtime") + assert((interface{}(nil) == &five) == ip(nilN, &five), + "for interface{}==*int compiler == runtime") + + assert((5 == interface{}(5)) == ni(five, five), + "for int==interface{} compiler == runtime") + assert((interface{}(5) == 5) == in(five, five), + "for interface{}==int comipiler == runtime") +} + +// Test that typed floating-point and complex arithmetic +// is computed with correct precision. +func truncate() { + const ( + x30 = 1 << 30 + x60 = 1 << 60 + + staticF32 = float32(x30) + 1 - x30 + staticF64 = float64(x60) + 1 - x60 + staticC64 = complex64(x30) + 1 - x30 + staticC128 = complex128(x60) + 1 - x60 + ) + dynamicF32 := float32(x30) + dynamicF32 += 1 + dynamicF32 -= x30 + + dynamicF64 := float64(x60) + dynamicF64 += 1 + dynamicF64 -= x60 + + dynamicC64 := complex64(x30) + dynamicC64 += 1 + dynamicC64 -= x30 + + dynamicC128 := complex128(x60) + dynamicC128 += 1 + dynamicC128 -= x60 + + assert(staticF32 == 0, "staticF32 == 0") + assert(staticF64 == 0, "staticF64 == 0") + assert(dynamicF32 == 0, "dynamicF32 == 0") + assert(dynamicF64 == 0, "dynamicF64 == 0") + assert(staticC64 == 0, "staticC64 == 0") + assert(staticC128 == 0, "staticC128 == 0") + assert(dynamicC64 == 0, "dynamicC64 == 0") + assert(dynamicC128 == 0, "dynamicC128 == 0") +} + +func main() { + ints() + floats() + interfaces() + truncate() + + assert(ctrue == true, "ctrue == true") + assert(cfalse == false, "cfalse == false") +} diff --git a/platform/dbops/binaries/go/go/test/const1.go b/platform/dbops/binaries/go/go/test/const1.go new file mode 100644 index 0000000000000000000000000000000000000000..1efe688cb99ce2a4749ef083cca763b8e3ee09ad --- /dev/null +++ b/platform/dbops/binaries/go/go/test/const1.go @@ -0,0 +1,94 @@ +// errorcheck + +// Copyright 2009 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. + +// Verify overflow is detected when using numeric constants. +// Does not compile. + +package main + +import "unsafe" + +type I interface{} + +const ( + // assume all types behave similarly to int8/uint8 + Int8 int8 = 101 + Minus1 int8 = -1 + Uint8 uint8 = 102 + Const = 103 + + Float32 float32 = 104.5 + Float64 float64 = 105.5 + ConstFloat = 106.5 + Big float64 = 1e300 + + String = "abc" + Bool = true +) + +var ( + a1 = Int8 * 100 // ERROR "overflow|cannot convert" + a2 = Int8 * -1 // OK + a3 = Int8 * 1000 // ERROR "overflow|cannot convert" + a4 = Int8 * int8(1000) // ERROR "overflow|cannot convert" + a5 = int8(Int8 * 1000) // ERROR "overflow|cannot convert" + a6 = int8(Int8 * int8(1000)) // ERROR "overflow|cannot convert" + a7 = Int8 - 2*Int8 - 2*Int8 // ERROR "overflow|cannot convert" + a8 = Int8 * Const / 100 // ERROR "overflow|cannot convert" + a9 = Int8 * (Const / 100) // OK + + b1 = Uint8 * Uint8 // ERROR "overflow|cannot convert" + b2 = Uint8 * -1 // ERROR "overflow|cannot convert" + b3 = Uint8 - Uint8 // OK + b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow|cannot convert" + b5 = uint8(^0) // ERROR "overflow|cannot convert" + b5a = int64(^0) // OK + b6 = ^uint8(0) // OK + b6a = ^int64(0) // OK + b7 = uint8(Minus1) // ERROR "overflow|cannot convert" + b8 = uint8(int8(-1)) // ERROR "overflow|cannot convert" + b8a = uint8(-1) // ERROR "overflow|cannot convert" + b9 byte = (1 << 10) >> 8 // OK + b10 byte = (1 << 10) // ERROR "overflow|cannot convert" + b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow|cannot convert" + b12 byte = 1000 // ERROR "overflow|cannot convert" + b13 byte = byte(1000) // ERROR "overflow|cannot convert" + b14 byte = byte(100) * byte(100) // ERROR "overflow|cannot convert" + b15 byte = byte(100) * 100 // ERROR "overflow|cannot convert" + b16 byte = byte(0) * 1000 // ERROR "overflow|cannot convert" + b16a byte = 0 * 1000 // OK + b17 byte = byte(0) * byte(1000) // ERROR "overflow|cannot convert" + b18 byte = Uint8 / 0 // ERROR "division by zero" + + c1 float64 = Big + c2 float64 = Big * Big // ERROR "overflow|cannot convert" + c3 float64 = float64(Big) * Big // ERROR "overflow|cannot convert" + c4 = Big * Big // ERROR "overflow|cannot convert" + c5 = Big / 0 // ERROR "division by zero" + c6 = 1000 % 1e3 // ERROR "invalid operation|expected integer type" +) + +func f(int) + +func main() { + f(Int8) // ERROR "convert|wrong type|cannot" + f(Minus1) // ERROR "convert|wrong type|cannot" + f(Uint8) // ERROR "convert|wrong type|cannot" + f(Const) // OK + f(Float32) // ERROR "convert|wrong type|cannot" + f(Float64) // ERROR "convert|wrong type|cannot" + f(ConstFloat) // ERROR "truncate" + f(ConstFloat - 0.5) // OK + f(Big) // ERROR "convert|wrong type|cannot" + f(String) // ERROR "convert|wrong type|cannot|incompatible" + f(Bool) // ERROR "convert|wrong type|cannot|incompatible" +} + +const ptr = nil // ERROR "const.*nil|not constant" +const _ = string([]byte(nil)) // ERROR "is not a? ?constant" +const _ = uintptr(unsafe.Pointer((*int)(nil))) // ERROR "is not a? ?constant" +const _ = unsafe.Pointer((*int)(nil)) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant" +const _ = (*int)(nil) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant" diff --git a/platform/dbops/binaries/go/go/test/const2.go b/platform/dbops/binaries/go/go/test/const2.go new file mode 100644 index 0000000000000000000000000000000000000000..f0de37be15ecab9c472e034a774be8100391eb70 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/const2.go @@ -0,0 +1,32 @@ +// errorcheck + +// Copyright 2009 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. + +// Verify that large integer constant expressions cause overflow. +// Does not compile. + +package main + +const ( + A int = 1 + B byte; // ERROR "type without expr|expected .=.|missing init expr" +) + +const LargeA = 1000000000000000000 +const LargeB = LargeA * LargeA * LargeA +const LargeC = LargeB * LargeB * LargeB // GC_ERROR "constant multiplication overflow" + +const AlsoLargeA = LargeA << 400 << 400 >> 400 >> 400 // GC_ERROR "constant shift overflow" + +// Issue #42732. + +const a = 1e+500000000 +const b = a * a // ERROR "constant multiplication overflow|not representable" +const c = b * b + +const MaxInt512 = (1<<256 - 1) * (1<<256 + 1) +const _ = MaxInt512 + 1 // ERROR "constant addition overflow" +const _ = MaxInt512 ^ -1 // ERROR "constant bitwise XOR overflow" +const _ = ^MaxInt512 // ERROR "constant bitwise complement overflow" diff --git a/platform/dbops/binaries/go/go/test/const3.go b/platform/dbops/binaries/go/go/test/const3.go new file mode 100644 index 0000000000000000000000000000000000000000..3f4e3d1ae6fa9adf095afe91e176daa3708fe86b --- /dev/null +++ b/platform/dbops/binaries/go/go/test/const3.go @@ -0,0 +1,37 @@ +// run + +// Copyright 2009 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. + +// Test typed integer constants. + +package main + +import "fmt" + +type T int + +func (t T) String() string { return fmt.Sprintf("T%d", int(t)) } + +const ( + A T = 1 << (1 << iota) + B + C + D + E +) + +func main() { + s := fmt.Sprintf("%v %v %v %v %v", A, B, C, D, E) + if s != "T2 T4 T16 T256 T65536" { + println("type info didn't propagate in const: got", s) + panic("fail") + } + x := uint(5) + y := float64(uint64(1)< N { + return N + } + return n +} + +func ncopied(length, in, out int) int { + n := length + if in+n > N { + n = N - in + } + if out+n > N { + n = N - out + } + return n +} + +func doAllSlices(length, in, out int) { + reset() + n := copy(my8(output8[out:clamp(out+length)]), input8[in:clamp(in+length)]) + verify8(length, in, out, n) + n = copy(my8(outputS[out:clamp(out+length)]), myS(inputS[in:clamp(in+length)])) + verifyS(length, in, out, n) + n = copy(my16(output16[out:clamp(out+length)]), input16[in:clamp(in+length)]) + verify16(length, in, out, n) + n = copy(my32(output32[out:clamp(out+length)]), my32b(input32[in:clamp(in+length)])) + verify32(length, in, out, n) + n = copy(my64(output64[out:clamp(out+length)]), input64[in:clamp(in+length)]) + verify64(length, in, out, n) +} + +func bad8(state string, i, length, in, out int) { + fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n", + state, + length, in, out, + output8[i], + uint8(i+13), + input8, output8) + os.Exit(1) +} + +func verify8(length, in, out, m int) { + n := ncopied(length, in, out) + if m != n { + fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n) + os.Exit(1) + return + } + // before + var i int + for i = 0; i < out; i++ { + if output8[i] != u8(i+13) { + bad8("before8", i, length, in, out) + return + } + } + // copied part + for ; i < out+n; i++ { + if output8[i] != u8(i+in-out) { + bad8("copied8", i, length, in, out) + return + } + } + // after + for ; i < len(output8); i++ { + if output8[i] != u8(i+13) { + bad8("after8", i, length, in, out) + return + } + } +} + +func badS(state string, i, length, in, out int) { + fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n", + state, + length, in, out, + outputS[i], + uint8(i+13), + inputS, outputS) + os.Exit(1) +} + +func verifyS(length, in, out, m int) { + n := ncopied(length, in, out) + if m != n { + fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n) + os.Exit(1) + return + } + // before + var i int + for i = 0; i < out; i++ { + if outputS[i] != u8(i+13) { + badS("beforeS", i, length, in, out) + return + } + } + // copied part + for ; i < out+n; i++ { + if outputS[i] != u8(i+in-out) { + badS("copiedS", i, length, in, out) + return + } + } + // after + for ; i < len(outputS); i++ { + if outputS[i] != u8(i+13) { + badS("afterS", i, length, in, out) + return + } + } +} + +func bad16(state string, i, length, in, out int) { + fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n", + state, + length, in, out, + output16[i], + uint16(i+13), + input16, output16) + os.Exit(1) +} + +func verify16(length, in, out, m int) { + n := ncopied(length, in, out) + if m != n { + fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n) + os.Exit(1) + return + } + // before + var i int + for i = 0; i < out; i++ { + if output16[i] != u16(i+13) { + bad16("before16", i, length, in, out) + return + } + } + // copied part + for ; i < out+n; i++ { + if output16[i] != u16(i+in-out) { + bad16("copied16", i, length, in, out) + return + } + } + // after + for ; i < len(output16); i++ { + if output16[i] != u16(i+13) { + bad16("after16", i, length, in, out) + return + } + } +} + +func bad32(state string, i, length, in, out int) { + fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n", + state, + length, in, out, + output32[i], + uint32(i+13), + input32, output32) + os.Exit(1) +} + +func verify32(length, in, out, m int) { + n := ncopied(length, in, out) + if m != n { + fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n) + os.Exit(1) + return + } + // before + var i int + for i = 0; i < out; i++ { + if output32[i] != u32(i+13) { + bad32("before32", i, length, in, out) + return + } + } + // copied part + for ; i < out+n; i++ { + if output32[i] != u32(i+in-out) { + bad32("copied32", i, length, in, out) + return + } + } + // after + for ; i < len(output32); i++ { + if output32[i] != u32(i+13) { + bad32("after32", i, length, in, out) + return + } + } +} + +func bad64(state string, i, length, in, out int) { + fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n", + state, + length, in, out, + output64[i], + uint64(i+13), + input64, output64) + os.Exit(1) +} + +func verify64(length, in, out, m int) { + n := ncopied(length, in, out) + if m != n { + fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n) + os.Exit(1) + return + } + // before + var i int + for i = 0; i < out; i++ { + if output64[i] != u64(i+13) { + bad64("before64", i, length, in, out) + return + } + } + // copied part + for ; i < out+n; i++ { + if output64[i] != u64(i+in-out) { + bad64("copied64", i, length, in, out) + return + } + } + // after + for ; i < len(output64); i++ { + if output64[i] != u64(i+13) { + bad64("after64", i, length, in, out) + return + } + } +} + +func slice() { + for length := 0; length < N; length++ { + for in := 0; in <= 32; in++ { + for out := 0; out <= 32; out++ { + doAllSlices(length, in, out) + } + } + } +} + +// Array test. Can be much simpler. It's only checking for correct handling of [0:]. +func array() { + var array [N]uint8 + reset() + copy(array[0:], input8) + for i := 0; i < N; i++ { + output8[i] = 0 + } + copy(output8, array[0:]) + verify8(N, 0, 0, N) +} + +func main() { + slice() + array() +} diff --git a/platform/dbops/binaries/go/go/test/copy1.go b/platform/dbops/binaries/go/go/test/copy1.go new file mode 100644 index 0000000000000000000000000000000000000000..c0760f71904879206c81a7bbc6e7e45d27f58b85 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/copy1.go @@ -0,0 +1,27 @@ +// errorcheck + +// Copyright 2017 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. + +// Verify that copy arguments requirements are enforced by the +// compiler. + +package main + +func main() { + + si := make([]int, 8) + sf := make([]float64, 8) + + _ = copy() // ERROR "not enough arguments" + _ = copy(1, 2, 3) // ERROR "too many arguments" + + _ = copy(si, "hi") // ERROR "have different element types(.*int.*string| int and byte)" + _ = copy(si, sf) // ERROR "have different element types.*int.*float64" + + _ = copy(1, 2) // ERROR "must be slices; have int, int|expects slice arguments" + _ = copy(1, si) // ERROR "first argument to copy should be|expects slice arguments" + _ = copy(si, 2) // ERROR "second argument to copy should be|expects slice arguments" + +} diff --git a/platform/dbops/binaries/go/go/test/crlf.go b/platform/dbops/binaries/go/go/test/crlf.go new file mode 100644 index 0000000000000000000000000000000000000000..2d56889b039abe6927d20f824616414997c782eb --- /dev/null +++ b/platform/dbops/binaries/go/go/test/crlf.go @@ -0,0 +1,50 @@ +// runoutput + +// Copyright 2011 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. + +// Test source files and strings containing \r and \r\n. + +package main + +import ( + "fmt" + "strings" +) + +func main() { + prog = strings.Replace(prog, "BQ", "`", -1) + prog = strings.Replace(prog, "CR", "\r", -1) + fmt.Print(prog) +} + +var prog = ` +package main +CR + +import "fmt" + +var CR s = "hello\n" + CR + " world"CR + +var t = BQhelloCR + worldBQ + +var u = BQhCReCRlCRlCRoCR + worldBQ + +var golden = "hello\n world" + +func main() { + if s != golden { + fmt.Printf("s=%q, want %q", s, golden) + } + if t != golden { + fmt.Printf("t=%q, want %q", t, golden) + } + if u != golden { + fmt.Printf("u=%q, want %q", u, golden) + } +} +` diff --git a/platform/dbops/binaries/go/go/test/ddd.go b/platform/dbops/binaries/go/go/test/ddd.go new file mode 100644 index 0000000000000000000000000000000000000000..84503f7d5b182751a825ff42dc97e1cb01583e74 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/ddd.go @@ -0,0 +1,230 @@ +// run + +// Copyright 2010 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. + +// Test variadic functions and calls (dot-dot-dot). + +package main + +func sum(args ...int) int { + s := 0 + for _, v := range args { + s += v + } + return s +} + +func sumC(args ...int) int { return func() int { return sum(args...) }() } + +var sumD = func(args ...int) int { return sum(args...) } + +var sumE = func() func(...int) int { return func(args ...int) int { return sum(args...) } }() + +var sumF = func(args ...int) func() int { return func() int { return sum(args...) } } + +func sumA(args []int) int { + s := 0 + for _, v := range args { + s += v + } + return s +} + +func sumB(args []int) int { return sum(args...) } + +func sum2(args ...int) int { return 2 * sum(args...) } + +func sum3(args ...int) int { return 3 * sumA(args) } + +func sum4(args ...int) int { return 4 * sumB(args) } + +func intersum(args ...interface{}) int { + s := 0 + for _, v := range args { + s += v.(int) + } + return s +} + +type T []T + +func ln(args ...T) int { return len(args) } + +func ln2(args ...T) int { return 2 * ln(args...) } + +func (*T) Sum(args ...int) int { return sum(args...) } + +type U struct { + *T +} + +type I interface { + Sum(...int) int +} + +func main() { + if x := sum(1, 2, 3); x != 6 { + println("sum 6", x) + panic("fail") + } + if x := sum(); x != 0 { + println("sum 0", x) + panic("fail") + } + if x := sum(10); x != 10 { + println("sum 10", x) + panic("fail") + } + if x := sum(1, 8); x != 9 { + println("sum 9", x) + panic("fail") + } + if x := sumC(4, 5, 6); x != 15 { + println("sumC 15", x) + panic("fail") + } + if x := sumD(4, 5, 7); x != 16 { + println("sumD 16", x) + panic("fail") + } + if x := sumE(4, 5, 8); x != 17 { + println("sumE 17", x) + panic("fail") + } + if x := sumF(4, 5, 9)(); x != 18 { + println("sumF 18", x) + panic("fail") + } + if x := sum2(1, 2, 3); x != 2*6 { + println("sum 6", x) + panic("fail") + } + if x := sum2(); x != 2*0 { + println("sum 0", x) + panic("fail") + } + if x := sum2(10); x != 2*10 { + println("sum 10", x) + panic("fail") + } + if x := sum2(1, 8); x != 2*9 { + println("sum 9", x) + panic("fail") + } + if x := sum3(1, 2, 3); x != 3*6 { + println("sum 6", x) + panic("fail") + } + if x := sum3(); x != 3*0 { + println("sum 0", x) + panic("fail") + } + if x := sum3(10); x != 3*10 { + println("sum 10", x) + panic("fail") + } + if x := sum3(1, 8); x != 3*9 { + println("sum 9", x) + panic("fail") + } + if x := sum4(1, 2, 3); x != 4*6 { + println("sum 6", x) + panic("fail") + } + if x := sum4(); x != 4*0 { + println("sum 0", x) + panic("fail") + } + if x := sum4(10); x != 4*10 { + println("sum 10", x) + panic("fail") + } + if x := sum4(1, 8); x != 4*9 { + println("sum 9", x) + panic("fail") + } + if x := intersum(1, 2, 3); x != 6 { + println("intersum 6", x) + panic("fail") + } + if x := intersum(); x != 0 { + println("intersum 0", x) + panic("fail") + } + if x := intersum(10); x != 10 { + println("intersum 10", x) + panic("fail") + } + if x := intersum(1, 8); x != 9 { + println("intersum 9", x) + panic("fail") + } + + if x := ln(nil, nil, nil); x != 3 { + println("ln 3", x) + panic("fail") + } + if x := ln([]T{}); x != 1 { + println("ln 1", x) + panic("fail") + } + if x := ln2(nil, nil, nil); x != 2*3 { + println("ln2 3", x) + panic("fail") + } + if x := ln2([]T{}); x != 2*1 { + println("ln2 1", x) + panic("fail") + } + if x := ((*T)(nil)).Sum(1, 3, 5, 7); x != 16 { + println("(*T)(nil).Sum", x) + panic("fail") + } + if x := (*T).Sum(nil, 1, 3, 5, 6); x != 15 { + println("(*T).Sum", x) + panic("fail") + } + if x := (&U{}).Sum(1, 3, 5, 5); x != 14 { + println("(&U{}).Sum", x) + panic("fail") + } + var u U + if x := u.Sum(1, 3, 5, 4); x != 13 { + println("u.Sum", x) + panic("fail") + } + if x := (&u).Sum(1, 3, 5, 3); x != 12 { + println("(&u).Sum", x) + panic("fail") + } + var i interface { + Sum(...int) int + } = &u + if x := i.Sum(2, 3, 5, 7); x != 17 { + println("i(=&u).Sum", x) + panic("fail") + } + i = u + if x := i.Sum(2, 3, 5, 6); x != 16 { + println("i(=u).Sum", x) + panic("fail") + } + var s struct { + I + } + s.I = &u + if x := s.Sum(2, 3, 5, 8); x != 18 { + println("s{&u}.Sum", x) + panic("fail") + } + if x := (*U).Sum(&U{}, 1, 3, 5, 2); x != 11 { + println("(*U).Sum", x) + panic("fail") + } + if x := U.Sum(U{}, 1, 3, 5, 1); x != 10 { + println("U.Sum", x) + panic("fail") + } +} diff --git a/platform/dbops/binaries/go/go/test/ddd1.go b/platform/dbops/binaries/go/go/test/ddd1.go new file mode 100644 index 0000000000000000000000000000000000000000..e0035559867cf50ece064d0b8dc7ba9b2db90888 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/ddd1.go @@ -0,0 +1,64 @@ +// errorcheck + +// Copyright 2010 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. + +// Verify that illegal uses of ... are detected. +// Does not compile. + +package main + +import "unsafe" + +func sum(args ...int) int { return 0 } + +var ( + _ = sum(1, 2, 3) + _ = sum() + _ = sum(1.0, 2.0) + _ = sum(1.5) // ERROR "1\.5 .untyped float constant. as int|integer" + _ = sum("hello") // ERROR ".hello. (.untyped string constant. as int|.type untyped string. as type int)|incompatible" + _ = sum([]int{1}) // ERROR "\[\]int{.*}.*as int value" +) + +func sum3(int, int, int) int { return 0 } +func tuple() (int, int, int) { return 1, 2, 3 } + +var ( + _ = sum(tuple()) + _ = sum(tuple()...) // ERROR "\.{3} with 3-valued|multiple-value" + _ = sum3(tuple()) + _ = sum3(tuple()...) // ERROR "\.{3} in call to non-variadic|multiple-value|invalid use of .*[.][.][.]" +) + +type T []T + +func funny(args ...T) int { return 0 } + +var ( + _ = funny(nil) + _ = funny(nil, nil) + _ = funny([]T{}) // ok because []T{} is a T; passes []T{[]T{}} +) + +func Foo(n int) {} + +func bad(args ...int) { + print(1, 2, args...) // ERROR "[.][.][.]" + println(args...) // ERROR "[.][.][.]" + ch := make(chan int) + close(ch...) // ERROR "[.][.][.]" + _ = len(args...) // ERROR "[.][.][.]" + _ = new(int...) // ERROR "[.][.][.]" + n := 10 + _ = make([]byte, n...) // ERROR "[.][.][.]" + _ = make([]byte, 10 ...) // ERROR "[.][.][.]" + var x int + _ = unsafe.Pointer(&x...) // ERROR "[.][.][.]" + _ = unsafe.Sizeof(x...) // ERROR "[.][.][.]" + _ = [...]byte("foo") // ERROR "[.][.][.]" + _ = [...][...]int{{1,2,3},{4,5,6}} // ERROR "[.][.][.]" + + Foo(x...) // ERROR "\.{3} in call to non-variadic|invalid use of .*[.][.][.]" +} diff --git a/platform/dbops/binaries/go/go/test/ddd2.go b/platform/dbops/binaries/go/go/test/ddd2.go new file mode 100644 index 0000000000000000000000000000000000000000..612ba292c2eccb7f2ae3fa1a66c66a79e9712aa0 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/ddd2.go @@ -0,0 +1,9 @@ +// rundir + +// Copyright 2010 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. + +// Test that variadic functions work across package boundaries. + +package ignored diff --git a/platform/dbops/binaries/go/go/test/decl.go b/platform/dbops/binaries/go/go/test/decl.go new file mode 100644 index 0000000000000000000000000000000000000000..e2f126adb210a9f8cc867fbe3ded3c9eb49196f7 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/decl.go @@ -0,0 +1,41 @@ +// run + +// Copyright 2009 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. + +// Test correct short declarations and redeclarations. + +package main + +func f1() int { return 1 } +func f2() (float32, int) { return 1, 2 } +func f3() (float32, int, string) { return 1, 2, "3" } + +func x() (s string) { + a, b, s := f3() + _, _ = a, b + return // tests that result var is in scope for redeclaration +} + +func main() { + i, f, s := f3() + j, f := f2() // redeclare f + k := f1() + m, g, s := f3() + m, h, s := f3() + { + // new block should be ok. + i, f, s := f3() + j, f := f2() // redeclare f + k := f1() + m, g, s := f3() + m, h, s := f3() + _, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h + } + if y := x(); y != "3" { + println("x() failed", y) + panic("fail") + } + _, _, _, _, _, _, _, _, _ = i, f, s, j, k, m, g, s, h +} diff --git a/platform/dbops/binaries/go/go/test/declbad.go b/platform/dbops/binaries/go/go/test/declbad.go new file mode 100644 index 0000000000000000000000000000000000000000..b978652a2b158573a5ad4d3dd028fbaf1e92f414 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/declbad.go @@ -0,0 +1,67 @@ +// errorcheck + +// Copyright 2009 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. + +// Test that incorrect short declarations and redeclarations are detected. +// Does not compile. + +package main + +func f1() int { return 1 } +func f2() (float32, int) { return 1, 2 } +func f3() (float32, int, string) { return 1, 2, "3" } + +func main() { + { + // simple redeclaration + i := f1() + i := f1() // ERROR "redeclared|no new" + _ = i + } + { + // change of type for f + i, f, s := f3() + f, g, t := f3() // ERROR "redeclared|cannot assign|incompatible|cannot use" + _, _, _, _, _ = i, f, s, g, t + } + { + // change of type for i + i, f, s := f3() + j, i, t := f3() // ERROR "redeclared|cannot assign|incompatible|cannot use" + _, _, _, _, _ = i, f, s, j, t + } + { + // no new variables + i, f, s := f3() + i, f := f2() // ERROR "redeclared|no new" + _, _, _ = i, f, s + } + { + // multiline no new variables + i := f1 + i := func() int { // ERROR "redeclared|no new|incompatible" + return 0 + } + _ = i + } + { + // single redeclaration + i, f, s := f3() + i := 1 // ERROR "redeclared|no new|incompatible" + _, _, _ = i, f, s + } + // double redeclaration + { + i, f, s := f3() + i, f := f2() // ERROR "redeclared|no new" + _, _, _ = i, f, s + } + { + // triple redeclaration + i, f, s := f3() + i, f, s := f3() // ERROR "redeclared|no new" + _, _, _ = i, f, s + } +} diff --git a/platform/dbops/binaries/go/go/test/defer.go b/platform/dbops/binaries/go/go/test/defer.go new file mode 100644 index 0000000000000000000000000000000000000000..6731ab7ee8b0123a2a2fc64027cfebb115d49d45 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/defer.go @@ -0,0 +1,52 @@ +// run + +// Copyright 2009 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. + +// Test defer. + +package main + +import "fmt" + +var result string + +func addInt(i int) { result += fmt.Sprint(i) } + +func test1helper() { + for i := 0; i < 10; i++ { + defer addInt(i) + } +} + +func test1() { + result = "" + test1helper() + if result != "9876543210" { + fmt.Printf("test1: bad defer result (should be 9876543210): %q\n", result) + panic("defer") + } +} + +func addDotDotDot(v ...interface{}) { result += fmt.Sprint(v...) } + +func test2helper() { + for i := 0; i < 10; i++ { + defer addDotDotDot(i) + } +} + +func test2() { + result = "" + test2helper() + if result != "9876543210" { + fmt.Printf("test2: bad defer result (should be 9876543210): %q\n", result) + panic("defer") + } +} + +func main() { + test1() + test2() +} diff --git a/platform/dbops/binaries/go/go/test/defererrcheck.go b/platform/dbops/binaries/go/go/test/defererrcheck.go new file mode 100644 index 0000000000000000000000000000000000000000..95b91da54db987b5f0746669f4d5db6389022a87 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/defererrcheck.go @@ -0,0 +1,86 @@ +// errorcheck -0 -l -d=defer + +// Copyright 2019 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. + +// check that open-coded defers are used in expected situations + +package main + +import "fmt" + +var glob = 3 + +func f1() { + + for i := 0; i < 10; i++ { + fmt.Println("loop") + } + defer func() { // ERROR "open-coded defer" + fmt.Println("defer") + }() +} + +func f2() { + for { + defer func() { // ERROR "heap-allocated defer" + fmt.Println("defer1") + }() + if glob > 2 { + break + } + } + defer func() { // ERROR "stack-allocated defer" + fmt.Println("defer2") + }() +} + +func f3() { + defer func() { // ERROR "stack-allocated defer" + fmt.Println("defer2") + }() + for { + defer func() { // ERROR "heap-allocated defer" + fmt.Println("defer1") + }() + if glob > 2 { + break + } + } +} + +func f4() { + defer func() { // ERROR "open-coded defer" + fmt.Println("defer") + }() +label: + fmt.Println("goto loop") + if glob > 2 { + goto label + } +} + +func f5() { +label: + fmt.Println("goto loop") + defer func() { // ERROR "heap-allocated defer" + fmt.Println("defer") + }() + if glob > 2 { + goto label + } +} + +func f6() { +label: + fmt.Println("goto loop") + if glob > 2 { + goto label + } + // The current analysis doesn't end a backward goto loop, so this defer is + // considered to be inside a loop + defer func() { // ERROR "heap-allocated defer" + fmt.Println("defer") + }() +} diff --git a/platform/dbops/binaries/go/go/test/deferfin.go b/platform/dbops/binaries/go/go/test/deferfin.go new file mode 100644 index 0000000000000000000000000000000000000000..1312bbbe71b04ac195330dc9151c84cfcf87031e --- /dev/null +++ b/platform/dbops/binaries/go/go/test/deferfin.go @@ -0,0 +1,58 @@ +// run + +// Copyright 2013 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. + +// Test that defers do not prevent garbage collection. + +package main + +import ( + "runtime" + "sync" + "sync/atomic" + "time" +) + +var sink func() + +func main() { + // Does not work with gccgo, due to partially conservative GC. + // Try to enable when we have fully precise GC. + if runtime.Compiler == "gccgo" { + return + } + N := 10 + count := int32(N) + var wg sync.WaitGroup + wg.Add(N) + for i := 0; i < N; i++ { + go func() { + defer wg.Done() + v := new(string) + f := func() { + if *v != "" { + panic("oops") + } + } + if *v != "" { + // let the compiler think f escapes + sink = f + } + runtime.SetFinalizer(v, func(p *string) { + atomic.AddInt32(&count, -1) + }) + defer f() + }() + } + wg.Wait() + for i := 0; i < 3; i++ { + time.Sleep(10 * time.Millisecond) + runtime.GC() + } + if count != 0 { + println(count, "out of", N, "finalizer are not called") + panic("not all finalizers are called") + } +} diff --git a/platform/dbops/binaries/go/go/test/defernil.go b/platform/dbops/binaries/go/go/test/defernil.go new file mode 100644 index 0000000000000000000000000000000000000000..5be3abd2f508e64d2af4d159e73cb9982a4167c2 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/defernil.go @@ -0,0 +1,33 @@ +// run + +// Copyright 2019 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. + +// Check that deferring a nil function causes a proper +// panic when the deferred function is invoked (not +// when the function is deferred). +// See Issue #8047 and #34926. + +package main + +var x = 0 + +func main() { + defer func() { + err := recover() + if err == nil { + panic("did not panic") + } + if x != 1 { + panic("FAIL") + } + }() + f() +} + +func f() { + var nilf func() + defer nilf() + x = 1 +} diff --git a/platform/dbops/binaries/go/go/test/deferprint.go b/platform/dbops/binaries/go/go/test/deferprint.go new file mode 100644 index 0000000000000000000000000000000000000000..b74677ac59c9810743ce6ba667858bd4e835c440 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/deferprint.go @@ -0,0 +1,17 @@ +// run + +// Copyright 2010 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. + +// Test that we can defer the predeclared functions print and println. + +package main + +func main() { + defer println(42, true, false, true, 1.5, "world", (chan int)(nil), []int(nil), (map[string]int)(nil), (func())(nil), byte(255)) + defer println(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) + // Disabled so the test doesn't crash but left here for reference. + // defer panic("dead") + defer print("printing: ") +} diff --git a/platform/dbops/binaries/go/go/test/deferprint.out b/platform/dbops/binaries/go/go/test/deferprint.out new file mode 100644 index 0000000000000000000000000000000000000000..a71cfcebd7e723e5c6aaa133399b9c5dfa3a108f --- /dev/null +++ b/platform/dbops/binaries/go/go/test/deferprint.out @@ -0,0 +1,2 @@ +printing: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 +42 true false true +1.500000e+000 world 0x0 [0/0]0x0 0x0 0x0 255 diff --git a/platform/dbops/binaries/go/go/test/devirt.go b/platform/dbops/binaries/go/go/test/devirt.go new file mode 100644 index 0000000000000000000000000000000000000000..d5c815222e448cb01b9620256db9e55f9cc09d0d --- /dev/null +++ b/platform/dbops/binaries/go/go/test/devirt.go @@ -0,0 +1,38 @@ +// errorcheck -0 -d=ssa/opt/debug=1 + +package main + +// Trivial interface call devirtualization test. + +type real struct { + value int +} + +func (r *real) Value() int { return r.value } + +type Valuer interface { + Value() int +} + +type indirectiface struct { + a, b, c int +} + +func (i indirectiface) Value() int { + return i.a + i.b + i.c +} + +func main() { + var r Valuer + rptr := &real{value: 3} + r = rptr + + if r.Value() != 3 { // ERROR "de-virtualizing call$" + panic("not 3") + } + + r = indirectiface{3, 4, 5} + if r.Value() != 12 { // ERROR "de-virtualizing call$" + panic("not 12") + } +} diff --git a/platform/dbops/binaries/go/go/test/directive.go b/platform/dbops/binaries/go/go/test/directive.go new file mode 100644 index 0000000000000000000000000000000000000000..8da15e243798366ea829fab87aec61f24a4979d2 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/directive.go @@ -0,0 +1,57 @@ +// errorcheck + +// 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. + +// Verify that misplaced directives are diagnosed. + +//go:noinline // ERROR "misplaced compiler directive" + +//go:noinline // ERROR "misplaced compiler directive" +package main + +//go:nosplit +func f1() {} + +//go:nosplit +//go:noinline +func f2() {} + +//go:noinline // ERROR "misplaced compiler directive" + +//go:noinline // ERROR "misplaced compiler directive" +var x int + +//go:noinline // ERROR "misplaced compiler directive" +const c = 1 + +//go:noinline // ERROR "misplaced compiler directive" +type T int + +type ( + //go:noinline // ERROR "misplaced compiler directive" + T2 int + //go:noinline // ERROR "misplaced compiler directive" + T3 int +) + +//go:noinline +func f() { + x := 1 + + { + _ = x + } + //go:noinline // ERROR "misplaced compiler directive" + var y int + _ = y + + //go:noinline // ERROR "misplaced compiler directive" + const c = 1 + + _ = func() {} + + //go:noinline // ERROR "misplaced compiler directive" + type T int +} diff --git a/platform/dbops/binaries/go/go/test/directive2.go b/platform/dbops/binaries/go/go/test/directive2.go new file mode 100644 index 0000000000000000000000000000000000000000..2bb9ca9f0a44040e1b5d587c654d4391e609f74d --- /dev/null +++ b/platform/dbops/binaries/go/go/test/directive2.go @@ -0,0 +1,58 @@ +// errorcheck + +// 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. + +// Verify that misplaced directives are diagnosed. + +// ok +//go:build !ignore + +package main + +//go:build bad // ERROR "misplaced compiler directive" + +//go:noinline // ERROR "misplaced compiler directive" +type ( + T2 int //go:noinline // ERROR "misplaced compiler directive" + T2b int + T2c int + T3 int +) + +//go:noinline // ERROR "misplaced compiler directive" +type ( + T4 int +) + +//go:noinline // ERROR "misplaced compiler directive" +type () + +type T5 int + +func g() {} //go:noinline // ERROR "misplaced compiler directive" + +// ok: attached to f (duplicated yes, but ok) +//go:noinline + +//go:noinline +func f() { + //go:noinline // ERROR "misplaced compiler directive" + x := 1 + + //go:noinline // ERROR "misplaced compiler directive" + { + _ = x //go:noinline // ERROR "misplaced compiler directive" + } + var y int //go:noinline // ERROR "misplaced compiler directive" + //go:noinline // ERROR "misplaced compiler directive" + _ = y + + const c = 1 + + _ = func() {} +} + +// EOF +//go:noinline // ERROR "misplaced compiler directive" diff --git a/platform/dbops/binaries/go/go/test/divide.go b/platform/dbops/binaries/go/go/test/divide.go new file mode 100644 index 0000000000000000000000000000000000000000..b5570415c242bb1c0c47fddb87c527cc69adc6fd --- /dev/null +++ b/platform/dbops/binaries/go/go/test/divide.go @@ -0,0 +1,62 @@ +// run + +// Copyright 2011 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. + +// Test divide corner cases. + +package main + +import "fmt" + +func f8(x, y, q, r int8) { + if t := x / y; t != q { + fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) + panic("divide") + } + if t := x % y; t != r { + fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) + panic("divide") + } +} + +func f16(x, y, q, r int16) { + if t := x / y; t != q { + fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) + panic("divide") + } + if t := x % y; t != r { + fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) + panic("divide") + } +} + +func f32(x, y, q, r int32) { + if t := x / y; t != q { + fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) + panic("divide") + } + if t := x % y; t != r { + fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) + panic("divide") + } +} + +func f64(x, y, q, r int64) { + if t := x / y; t != q { + fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) + panic("divide") + } + if t := x % y; t != r { + fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) + panic("divide") + } +} + +func main() { + f8(-1<<7, -1, -1<<7, 0) + f16(-1<<15, -1, -1<<15, 0) + f32(-1<<31, -1, -1<<31, 0) + f64(-1<<63, -1, -1<<63, 0) +} diff --git a/platform/dbops/binaries/go/go/test/divmod.go b/platform/dbops/binaries/go/go/test/divmod.go new file mode 100644 index 0000000000000000000000000000000000000000..ab85b7f149237587ed978c2151e96a59f7332c44 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/divmod.go @@ -0,0 +1,460 @@ +// run + +// Copyright 2013 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. + +// Test division of variables. Generate many test cases, +// compute correct answer using shift and subtract, +// and then compare against results from division and +// modulus operators. +// +// Primarily useful for testing software div/mod. + +package main + +const long = false + +func main() { + if long { + // About 3e9 test cases (calls to checkdiv3). + // Too long for everyday testing. + gen2(3, 64, 2, 64, checkdiv1) + println(ntest) + } else { + // About 4e6 test cases (calls to checkdiv3). + // Runs for 8 seconds on ARM chromebook, much faster elsewhere. + gen2(2, 64, 1, 64, checkdiv1) + } +} + +// generate all uint64 values x where x has at most n bits set in the low w +// and call f(x) for each. +func gen1(n, w int, f func(uint64)) { + gen(0, 0, n, w-1, f) +} + +func gen(val uint64, nbits, maxbits, pos int, f func(uint64)) { + if pos < 0 { + f(val) + return + } + gen(val, nbits, maxbits, pos-1, f) + if nbits < maxbits { + gen(val|1< y && y+y <= x { + sh++ + y <<= 1 + } + for ; sh >= 0; sh-- { + q <<= 1 + if x >= y { + x -= y + q |= 1 + } + y >>= 1 + } + return q, x +} + +// signed divide and mod: do unsigned and adjust signs. +func idiv(x, y int64) (q, r int64) { + // special case for minint / -1 = minint + if x-1 > x && y == -1 { + return x, 0 + } + ux := uint64(x) + uy := uint64(y) + if x < 0 { + ux = -ux + } + if y < 0 { + uy = -uy + } + uq, ur := udiv(ux, uy) + q = int64(uq) + r = int64(ur) + if x < 0 { + r = -r + } + if (x < 0) != (y < 0) { + q = -q + } + return q, r +} diff --git a/platform/dbops/binaries/go/go/test/embedfunc.go b/platform/dbops/binaries/go/go/test/embedfunc.go new file mode 100644 index 0000000000000000000000000000000000000000..14e0f82975769449253152fb7d03b1b152e75374 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/embedfunc.go @@ -0,0 +1,15 @@ +// errorcheck + +// Copyright 2021 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. + +package p + +import _ "embed" + +func f() { + //go:embed x.txt // ERROR "go:embed cannot apply to var inside func" + var x string + _ = x +} diff --git a/platform/dbops/binaries/go/go/test/embedvers.go b/platform/dbops/binaries/go/go/test/embedvers.go new file mode 100644 index 0000000000000000000000000000000000000000..71f0f22f1d5053584e862e6269d7b1366a21fb0a --- /dev/null +++ b/platform/dbops/binaries/go/go/test/embedvers.go @@ -0,0 +1,12 @@ +// errorcheck -lang=go1.15 + +// Copyright 2021 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. + +package p + +import _ "embed" + +//go:embed x.txt // ERROR "go:embed requires go1.16 or later" +var x string diff --git a/platform/dbops/binaries/go/go/test/empty.go b/platform/dbops/binaries/go/go/test/empty.go new file mode 100644 index 0000000000000000000000000000000000000000..92a79a4e0d802d184f1b066d0a142a455e805e2f --- /dev/null +++ b/platform/dbops/binaries/go/go/test/empty.go @@ -0,0 +1,15 @@ +// compile + +// Copyright 2009 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. + +// Test that top-level parenthesized declarations can be empty. +// Compiles but does not run. + +package P + +import ( ) +const ( ) +var ( ) +type ( ) diff --git a/platform/dbops/binaries/go/go/test/env.go b/platform/dbops/binaries/go/go/test/env.go new file mode 100644 index 0000000000000000000000000000000000000000..1590f1e06bb1a83d0cfb7b5f137598d2d25db782 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/env.go @@ -0,0 +1,31 @@ +// run + +// Copyright 2009 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. + +// Test that environment variables are accessible through +// package os. + +package main + +import ( + "os" + "runtime" +) + +func main() { + ga := os.Getenv("PATH") + if runtime.GOOS == "plan9" { + ga = os.Getenv("path") + } + if ga == "" { + print("PATH is empty\n") + os.Exit(1) + } + xxx := os.Getenv("DOES_NOT_EXIST") + if xxx != "" { + print("$DOES_NOT_EXIST=", xxx, "\n") + os.Exit(1) + } +} diff --git a/platform/dbops/binaries/go/go/test/eof.go b/platform/dbops/binaries/go/go/test/eof.go new file mode 100644 index 0000000000000000000000000000000000000000..d051f33bf79db1941ddbb78179555f6064d86347 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/eof.go @@ -0,0 +1,12 @@ +// compile + +// Copyright 2010 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. + +// Test a source file does not need a final newline. +// Compiles but does not run. + +// No newline at the end of this file. + +package main \ No newline at end of file diff --git a/platform/dbops/binaries/go/go/test/eof1.go b/platform/dbops/binaries/go/go/test/eof1.go new file mode 100644 index 0000000000000000000000000000000000000000..90792ca76e10073aaef100015347b365a22fb4ce --- /dev/null +++ b/platform/dbops/binaries/go/go/test/eof1.go @@ -0,0 +1,12 @@ +// compile + +// Copyright 2010 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. + +// Test that a comment ending a source file does not need a final newline. +// Compiles but does not run. + +package eof1 + +// No newline at the end of this comment. \ No newline at end of file diff --git a/platform/dbops/binaries/go/go/test/escape.go b/platform/dbops/binaries/go/go/test/escape.go new file mode 100644 index 0000000000000000000000000000000000000000..e6103f72c89815a57443277e377c145d2de9c86c --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape.go @@ -0,0 +1,226 @@ +// run + +// Copyright 2009 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. + +package main + +// Test for correct heap-moving of escaped variables. +// It is hard to check for the allocations, but it is easy +// to check that if you call the function twice at the +// same stack level, the pointers returned should be +// different. + +var bad = false + +var allptr = make([]*int, 0, 100) + +func noalias(p, q *int, s string) { + n := len(allptr) + *p = -(n + 1) + *q = -(n + 2) + allptr = allptr[0 : n+2] + allptr[n] = p + allptr[n+1] = q + n += 2 + for i := 0; i < n; i++ { + if allptr[i] != nil && *allptr[i] != -(i+1) { + println("aliased pointers", -(i + 1), *allptr[i], "after", s) + allptr[i] = nil + bad = true + } + } +} + +func val(p, q *int, v int, s string) { + if *p != v { + println("wrong value want", v, "got", *p, "after", s) + bad = true + } + if *q != v+1 { + println("wrong value want", v+1, "got", *q, "after", s) + bad = true + } +} + +func chk(p, q *int, v int, s string) { + val(p, q, v, s) + noalias(p, q, s) +} + +func chkalias(p, q *int, v int, s string) { + if p != q { + println("want aliased pointers but got different after", s) + bad = true + } + if *q != v+1 { + println("wrong value want", v+1, "got", *q, "after", s) + bad = true + } +} + +func i_escapes(x int) *int { + var i int + i = x + return &i +} + +func j_escapes(x int) *int { + var j int = x + j = x + return &j +} + +func k_escapes(x int) *int { + k := x + return &k +} + +func in_escapes(x int) *int { + return &x +} + +func send(c chan int, x int) { + c <- x +} + +func select_escapes(x int) *int { + c := make(chan int) + go send(c, x) + select { + case req := <-c: + return &req + } + return nil +} + +func select_escapes1(x int, y int) (*int, *int) { + c := make(chan int) + var a [2]int + var p [2]*int + a[0] = x + a[1] = y + for i := 0; i < 2; i++ { + go send(c, a[i]) + select { + case req := <-c: + p[i] = &req + } + } + return p[0], p[1] +} + +func range_escapes(x int) *int { + var a [1]int + a[0] = x + for _, v := range a { + return &v + } + return nil +} + +// *is* aliased +func range_escapes2(x, y int) (*int, *int) { + var a [2]int + var p [2]*int + a[0] = x + a[1] = y + var k, v int + for k, v = range a { + p[k] = &v + } + return p[0], p[1] +} + +// *is* aliased +func for_escapes2(x int, y int) (*int, *int) { + var p [2]*int + n := 0 + i := x + for ; n < 2; i = y { + p[n] = &i + n++ + } + return p[0], p[1] +} + +func for_escapes3(x int, y int) (*int, *int) { + var f [2]func() *int + n := 0 + for i := x; n < 2; i = y { + p := new(int) + *p = i + f[n] = func() *int { return p } + n++ + } + return f[0](), f[1]() +} + +func out_escapes(i int) (x int, p *int) { + x = i + p = &x // ERROR "address of out parameter" + return +} + +func out_escapes_2(i int) (x int, p *int) { + x = i + return x, &x // ERROR "address of out parameter" +} + +func defer1(i int) (x int) { + c := make(chan int) + go func() { x = i; c <- 1 }() + <-c + return +} + +func main() { + p, q := i_escapes(1), i_escapes(2) + chk(p, q, 1, "i_escapes") + + p, q = j_escapes(3), j_escapes(4) + chk(p, q, 3, "j_escapes") + + p, q = k_escapes(5), k_escapes(6) + chk(p, q, 5, "k_escapes") + + p, q = in_escapes(7), in_escapes(8) + chk(p, q, 7, "in_escapes") + + p, q = select_escapes(9), select_escapes(10) + chk(p, q, 9, "select_escapes") + + p, q = select_escapes1(11, 12) + chk(p, q, 11, "select_escapes1") + + p, q = range_escapes(13), range_escapes(14) + chk(p, q, 13, "range_escapes") + + p, q = range_escapes2(101, 102) + chkalias(p, q, 101, "range_escapes2") + + p, q = for_escapes2(103, 104) + chkalias(p, q, 103, "for_escapes2") + + p, q = for_escapes3(105, 106) + chk(p, q, 105, "for_escapes3") + + _, p = out_escapes(15) + _, q = out_escapes(16) + chk(p, q, 15, "out_escapes") + + _, p = out_escapes_2(17) + _, q = out_escapes_2(18) + chk(p, q, 17, "out_escapes_2") + + x := defer1(20) + if x != 20 { + println("defer failed", x) + bad = true + } + + if bad { + panic("BUG: no escape") + } +} diff --git a/platform/dbops/binaries/go/go/test/escape2.go b/platform/dbops/binaries/go/go/test/escape2.go new file mode 100644 index 0000000000000000000000000000000000000000..3e5d11f88e1aba8fd8cba36a16dfb29b9efd426e --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape2.go @@ -0,0 +1,1845 @@ +// errorcheck -0 -m -l + +// Copyright 2010 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. + +// Test, using compiler diagnostic flags, that the escape analysis is working. +// Compiles but does not run. Inlining is disabled. + +// escape2n.go contains all the same tests but compiles with -N. + +package foo + +import ( + "fmt" + "unsafe" +) + +var gxx *int + +func foo1(x int) { // ERROR "moved to heap: x$" + gxx = &x +} + +func foo2(yy *int) { // ERROR "leaking param: yy$" + gxx = yy +} + +func foo3(x int) *int { // ERROR "moved to heap: x$" + return &x +} + +type T *T + +func foo3b(t T) { // ERROR "leaking param: t$" + *t = t +} + +// xx isn't going anywhere, so use of yy is ok +func foo4(xx, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + xx = yy +} + +// xx isn't going anywhere, so taking address of yy is ok +func foo5(xx **int, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + xx = &yy +} + +func foo6(xx **int, yy *int) { // ERROR "xx does not escape$" "leaking param: yy$" + *xx = yy +} + +func foo7(xx **int, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + **xx = *yy +} + +func foo8(xx, yy *int) int { // ERROR "xx does not escape$" "yy does not escape$" + xx = yy + return *xx +} + +func foo9(xx, yy *int) *int { // ERROR "leaking param: xx to result ~r0 level=0$" "leaking param: yy to result ~r0 level=0$" + xx = yy + return xx +} + +func foo10(xx, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + *xx = *yy +} + +func foo11() int { + x, y := 0, 42 + xx := &x + yy := &y + *xx = *yy + return x +} + +var xxx **int + +func foo12(yyy **int) { // ERROR "leaking param: yyy$" + xxx = yyy +} + +// Must treat yyy as leaking because *yyy leaks, and the escape analysis +// summaries in exported metadata do not distinguish these two cases. +func foo13(yyy **int) { // ERROR "leaking param content: yyy$" + *xxx = *yyy +} + +func foo14(yyy **int) { // ERROR "yyy does not escape$" + **xxx = **yyy +} + +func foo15(yy *int) { // ERROR "moved to heap: yy$" + xxx = &yy +} + +func foo16(yy *int) { // ERROR "leaking param: yy$" + *xxx = yy +} + +func foo17(yy *int) { // ERROR "yy does not escape$" + **xxx = *yy +} + +func foo18(y int) { // ERROR "moved to heap: y$" + *xxx = &y +} + +func foo19(y int) { + **xxx = y +} + +type Bar struct { + i int + ii *int +} + +func NewBar() *Bar { + return &Bar{42, nil} // ERROR "&Bar{...} escapes to heap$" +} + +func NewBarp(x *int) *Bar { // ERROR "leaking param: x$" + return &Bar{42, x} // ERROR "&Bar{...} escapes to heap$" +} + +func NewBarp2(x *int) *Bar { // ERROR "x does not escape$" + return &Bar{*x, nil} // ERROR "&Bar{...} escapes to heap$" +} + +func (b *Bar) NoLeak() int { // ERROR "b does not escape$" + return *(b.ii) +} + +func (b *Bar) Leak() *int { // ERROR "leaking param: b to result ~r0 level=0$" + return &b.i +} + +func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param: b to result ~r0 level=1$" + return b.ii +} + +func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b to result ~r0 level=0$" + return b.ii +} + +func (b Bar) LeaksToo() *int { // ERROR "leaking param: b to result ~r0 level=0$" + v := 0 // ERROR "moved to heap: v$" + b.ii = &v + return b.ii +} + +func (b *Bar) LeaksABit() *int { // ERROR "leaking param: b to result ~r0 level=1$" + v := 0 // ERROR "moved to heap: v$" + b.ii = &v + return b.ii +} + +func (b Bar) StillNoLeak() int { // ERROR "b does not escape$" + v := 0 + b.ii = &v + return b.i +} + +func goLeak(b *Bar) { // ERROR "leaking param: b$" + go b.NoLeak() +} + +type Bar2 struct { + i [12]int + ii []int +} + +func NewBar2() *Bar2 { + return &Bar2{[12]int{42}, nil} // ERROR "&Bar2{...} escapes to heap$" +} + +func (b *Bar2) NoLeak() int { // ERROR "b does not escape$" + return b.i[0] +} + +func (b *Bar2) Leak() []int { // ERROR "leaking param: b to result ~r0 level=0$" + return b.i[:] +} + +func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param: b to result ~r0 level=1$" + return b.ii[0:1] +} + +func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape$" + return b.i +} + +func (b *Bar2) LeakSelf() { // ERROR "leaking param: b$" + b.ii = b.i[0:4] +} + +func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b$" + var buf []int + buf = b.i[0:] + b.ii = buf +} + +func foo21() func() int { + x := 42 + return func() int { // ERROR "func literal escapes to heap$" + return x + } +} + +func foo21a() func() int { + x := 42 // ERROR "moved to heap: x$" + return func() int { // ERROR "func literal escapes to heap$" + x++ + return x + } +} + +func foo22() int { + x := 42 + return func() int { // ERROR "func literal does not escape$" + return x + }() +} + +func foo23(x int) func() int { + return func() int { // ERROR "func literal escapes to heap$" + return x + } +} + +func foo23a(x int) func() int { + f := func() int { // ERROR "func literal escapes to heap$" + return x + } + return f +} + +func foo23b(x int) *(func() int) { + f := func() int { return x } // ERROR "func literal escapes to heap$" "moved to heap: f$" + return &f +} + +func foo23c(x int) func() int { // ERROR "moved to heap: x$" + return func() int { // ERROR "func literal escapes to heap$" + x++ + return x + } +} + +func foo24(x int) int { + return func() int { // ERROR "func literal does not escape$" + return x + }() +} + +var x *int + +func fooleak(xx *int) int { // ERROR "leaking param: xx$" + x = xx + return *x +} + +func foonoleak(xx *int) int { // ERROR "xx does not escape$" + return *x + *xx +} + +func foo31(x int) int { // ERROR "moved to heap: x$" + return fooleak(&x) +} + +func foo32(x int) int { + return foonoleak(&x) +} + +type Foo struct { + xx *int + x int +} + +var F Foo +var pf *Foo + +func (f *Foo) fooleak() { // ERROR "leaking param: f$" + pf = f +} + +func (f *Foo) foonoleak() { // ERROR "f does not escape$" + F.x = f.x +} + +func (f *Foo) Leak() { // ERROR "leaking param: f$" + f.fooleak() +} + +func (f *Foo) NoLeak() { // ERROR "f does not escape$" + f.foonoleak() +} + +func foo41(x int) { // ERROR "moved to heap: x$" + F.xx = &x +} + +func (f *Foo) foo42(x int) { // ERROR "f does not escape$" "moved to heap: x$" + f.xx = &x +} + +func foo43(f *Foo, x int) { // ERROR "f does not escape$" "moved to heap: x$" + f.xx = &x +} + +func foo44(yy *int) { // ERROR "leaking param: yy$" + F.xx = yy +} + +func (f *Foo) foo45() { // ERROR "f does not escape$" + F.x = f.x +} + +// See foo13 above for explanation of why f leaks. +func (f *Foo) foo46() { // ERROR "leaking param content: f$" + F.xx = f.xx +} + +func (f *Foo) foo47() { // ERROR "leaking param: f$" + f.xx = &f.x +} + +var ptrSlice []*int + +func foo50(i *int) { // ERROR "leaking param: i$" + ptrSlice[0] = i +} + +var ptrMap map[*int]*int + +func foo51(i *int) { // ERROR "leaking param: i$" + ptrMap[i] = i +} + +func indaddr1(x int) *int { // ERROR "moved to heap: x$" + return &x +} + +func indaddr2(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return *&x +} + +func indaddr3(x *int32) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return *(**int)(unsafe.Pointer(&x)) +} + +// From package math: + +func Float32bits(f float32) uint32 { + return *(*uint32)(unsafe.Pointer(&f)) +} + +func Float32frombits(b uint32) float32 { + return *(*float32)(unsafe.Pointer(&b)) +} + +func Float64bits(f float64) uint64 { + return *(*uint64)(unsafe.Pointer(&f)) +} + +func Float64frombits(b uint64) float64 { + return *(*float64)(unsafe.Pointer(&b)) +} + +// contrast with +func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f$" + return (*uint64)(unsafe.Pointer(&f)) +} + +func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f to result ~r0 level=0$" + return (*uint64)(unsafe.Pointer(f)) +} + +func typesw(i interface{}) *int { // ERROR "leaking param: i to result ~r0 level=0$" + switch val := i.(type) { + case *int: + return val + case *int8: + v := int(*val) // ERROR "moved to heap: v$" + return &v + } + return nil +} + +func exprsw(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + switch j := i; *j + 110 { + case 12: + return j + case 42: + return nil + } + return nil +} + +// assigning to an array element is like assigning to the array +func foo60(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + var a [12]*int + a[0] = i + return a[1] +} + +func foo60a(i *int) *int { // ERROR "i does not escape$" + var a [12]*int + a[0] = i + return nil +} + +// assigning to a struct field is like assigning to the struct +func foo61(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + type S struct { + a, b *int + } + var s S + s.a = i + return s.b +} + +func foo61a(i *int) *int { // ERROR "i does not escape$" + type S struct { + a, b *int + } + var s S + s.a = i + return nil +} + +// assigning to a struct field is like assigning to the struct but +// here this subtlety is lost, since s.a counts as an assignment to a +// track-losing dereference. +func foo62(i *int) *int { // ERROR "leaking param: i$" + type S struct { + a, b *int + } + s := new(S) // ERROR "new\(S\) does not escape$" + s.a = i + return nil // s.b +} + +type M interface { + M() +} + +func foo63(m M) { // ERROR "m does not escape$" +} + +func foo64(m M) { // ERROR "leaking param: m$" + m.M() +} + +func foo64b(m M) { // ERROR "leaking param: m$" + defer m.M() +} + +type MV int + +func (MV) M() {} + +func foo65() { + var mv MV + foo63(&mv) +} + +func foo66() { + var mv MV // ERROR "moved to heap: mv$" + foo64(&mv) +} + +func foo67() { + var mv MV + foo63(mv) // ERROR "mv does not escape$" +} + +func foo68() { + var mv MV + // escapes but it's an int so irrelevant + foo64(mv) // ERROR "mv escapes to heap$" +} + +func foo69(m M) { // ERROR "leaking param: m$" + foo64(m) +} + +func foo70(mv1 *MV, m M) { // ERROR "leaking param: m$" "leaking param: mv1$" + m = mv1 + foo64(m) +} + +func foo71(x *int) []*int { // ERROR "leaking param: x$" + var y []*int + y = append(y, x) + return y +} + +func foo71a(x int) []*int { // ERROR "moved to heap: x$" + var y []*int + y = append(y, &x) + return y +} + +func foo72() { + var x int + var y [1]*int + y[0] = &x +} + +func foo72aa() [10]*int { + var x int // ERROR "moved to heap: x$" + var y [10]*int + y[0] = &x + return y +} + +func foo72a() { + var y [10]*int + for i := 0; i < 10; i++ { + // escapes its scope + x := i // ERROR "moved to heap: x$" + y[i] = &x + } + return +} + +func foo72b() [10]*int { + var y [10]*int + for i := 0; i < 10; i++ { + x := i // ERROR "moved to heap: x$" + y[i] = &x + } + return y +} + +// issue 2145 +func foo73() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v + // actually just escapes its scope + defer func() { // ERROR "func literal escapes to heap$" + println(vv) + }() + } +} + +func foo731() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + defer func() { // ERROR "func literal escapes to heap$" + vv = 42 + println(vv) + }() + } +} + +func foo74() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v + // actually just escapes its scope + fn := func() { // ERROR "func literal escapes to heap$" + println(vv) + } + defer fn() + } +} + +func foo74a() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + fn := func() { // ERROR "func literal escapes to heap$" + vv += 1 + println(vv) + } + defer fn() + } +} + +// issue 3975 +func foo74b() { + var array [3]func() + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for i, v := range s { + vv := v + // actually just escapes its scope + array[i] = func() { // ERROR "func literal escapes to heap$" + println(vv) + } + } +} + +func foo74c() { + var array [3]func() + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for i, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + array[i] = func() { // ERROR "func literal escapes to heap$" + println(&vv) + } + } +} + +func myprint(y *int, x ...interface{}) *int { // ERROR "leaking param: y to result ~r0 level=0$" "x does not escape$" + return y +} + +func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "leaking param: x to result ~r0 level=0$" "y does not escape$" + return &x[0] +} + +func foo75(z *int) { // ERROR "z does not escape$" + myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75a(z *int) { // ERROR "z does not escape$" + myprint1(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75esc(z *int) { // ERROR "leaking param: z$" + gxx = myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75aesc(z *int) { // ERROR "z does not escape$" + var ppi **interface{} // assignments to pointer dereferences lose track + *ppi = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" +} + +func foo75aesc1(z *int) { // ERROR "z does not escape$" + sink = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" +} + +func foo76(z *int) { // ERROR "z does not escape" + myprint(nil, z) // ERROR "... argument does not escape$" +} + +func foo76a(z *int) { // ERROR "z does not escape" + myprint1(nil, z) // ERROR "... argument does not escape$" +} + +func foo76b() { + myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76c() { + myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76d() { + defer myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76e() { + defer myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76f() { + for { + // TODO: This one really only escapes its scope, but we don't distinguish yet. + defer myprint(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" + } +} + +func foo76g() { + for { + defer myprint1(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" + } +} + +func foo77(z []interface{}) { // ERROR "z does not escape$" + myprint(nil, z...) // z does not escape +} + +func foo77a(z []interface{}) { // ERROR "z does not escape$" + myprint1(nil, z...) +} + +func foo77b(z []interface{}) { // ERROR "leaking param: z$" + var ppi **interface{} + *ppi = myprint1(nil, z...) +} + +func foo77c(z []interface{}) { // ERROR "leaking param: z$" + sink = myprint1(nil, z...) +} + +func dotdotdot() { + i := 0 + myprint(nil, &i) // ERROR "... argument does not escape$" + + j := 0 + myprint1(nil, &j) // ERROR "... argument does not escape$" +} + +func foo78(z int) *int { // ERROR "moved to heap: z$" + return &z +} + +func foo78a(z int) *int { // ERROR "moved to heap: z$" + y := &z + x := &y + return *x // really return y +} + +func foo79() *int { + return new(int) // ERROR "new\(int\) escapes to heap$" +} + +func foo80() *int { + var z *int + for { + // Really just escapes its scope but we don't distinguish + z = new(int) // ERROR "new\(int\) escapes to heap$" + } + _ = z + return nil +} + +func foo81() *int { + for { + z := new(int) // ERROR "new\(int\) does not escape$" + _ = z + } + return nil +} + +func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param: p to result x level=0$" "leaking param: p to result y level=0$" + +func noop(x, y *int) {} // ERROR "x does not escape$" "y does not escape$" + +func foo82() { + var x, y, z int // ERROR "moved to heap: x$" "moved to heap: y$" "moved to heap: z$" + go noop(tee(&z)) + go noop(&x, &y) + for { + var u, v, w int // ERROR "moved to heap: u$" "moved to heap: v$" "moved to heap: w$" + defer noop(tee(&u)) + defer noop(&v, &w) + } +} + +type Fooer interface { + Foo() +} + +type LimitedFooer struct { + Fooer + N int64 +} + +func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r$" + return &LimitedFooer{r, n} // ERROR "&LimitedFooer{...} escapes to heap$" +} + +func foo90(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo91(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo92(x *int) [2]*int { // ERROR "leaking param: x to result ~r0 level=0$" + return [2]*int{x, nil} +} + +// does not leak c +func foo93(c chan *int) *int { // ERROR "c does not escape$" + for v := range c { + return v + } + return nil +} + +// does not leak m +func foo94(m map[*int]*int, b bool) *int { // ERROR "leaking param: m to result ~r0 level=1" + for k, v := range m { + if b { + return k + } + return v + } + return nil +} + +// does leak x +func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape$" "leaking param: x$" + m[x] = x +} + +// does not leak m but does leak content +func foo96(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1" + return m[0] +} + +// does leak m +func foo97(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$" + return m[0] +} + +// does not leak m +func foo98(m map[int]*int) *int { // ERROR "m does not escape$" + return m[0] +} + +// does leak m +func foo99(m *[1]*int) []*int { // ERROR "leaking param: m to result ~r0 level=0$" + return m[:] +} + +// does not leak m +func foo100(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1" + for _, v := range m { + return v + } + return nil +} + +// does leak m +func foo101(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$" + for _, v := range m { + return v + } + return nil +} + +// does not leak m +func foo101a(m [1]*int) *int { // ERROR "m does not escape$" + for i := range m { // ERROR "moved to heap: i$" + return &i + } + return nil +} + +// does leak x +func foo102(m []*int, x *int) { // ERROR "m does not escape$" "leaking param: x$" + m[0] = x +} + +// does not leak x +func foo103(m [1]*int, x *int) { // ERROR "m does not escape$" "x does not escape$" + m[0] = x +} + +var y []*int + +// does not leak x but does leak content +func foo104(x []*int) { // ERROR "leaking param content: x" + copy(y, x) +} + +// does not leak x but does leak content +func foo105(x []*int) { // ERROR "leaking param content: x" + _ = append(y, x...) +} + +// does leak x +func foo106(x *int) { // ERROR "leaking param: x$" + _ = append(y, x) +} + +func foo107(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo108(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo109(x *int) *int { // ERROR "leaking param: x$" + m := map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} does not escape$" + for k, _ := range m { + return k + } + return nil +} + +func foo110(x *int) *int { // ERROR "leaking param: x$" + m := map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} does not escape$" + return m[nil] +} + +func foo111(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0" + m := []*int{x} // ERROR "\[\]\*int{...} does not escape$" + return m[0] +} + +func foo112(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := [1]*int{x} + return m[0] +} + +func foo113(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := Bar{ii: x} + return m.ii +} + +func foo114(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := &Bar{ii: x} // ERROR "&Bar{...} does not escape$" + return m.ii +} + +func foo115(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1)) +} + +func foo116(b bool) *int { + if b { + x := 1 // ERROR "moved to heap: x$" + return &x + } else { + y := 1 // ERROR "moved to heap: y$" + return &y + } + return nil +} + +func foo117(unknown func(interface{})) { // ERROR "unknown does not escape$" + x := 1 // ERROR "moved to heap: x$" + unknown(&x) +} + +func foo118(unknown func(*int)) { // ERROR "unknown does not escape$" + x := 1 // ERROR "moved to heap: x$" + unknown(&x) +} + +func external(*int) + +func foo119(x *int) { // ERROR "leaking param: x$" + external(x) +} + +func foo120() { + // formerly exponential time analysis +L1: +L2: +L3: +L4: +L5: +L6: +L7: +L8: +L9: +L10: +L11: +L12: +L13: +L14: +L15: +L16: +L17: +L18: +L19: +L20: +L21: +L22: +L23: +L24: +L25: +L26: +L27: +L28: +L29: +L30: +L31: +L32: +L33: +L34: +L35: +L36: +L37: +L38: +L39: +L40: +L41: +L42: +L43: +L44: +L45: +L46: +L47: +L48: +L49: +L50: +L51: +L52: +L53: +L54: +L55: +L56: +L57: +L58: +L59: +L60: +L61: +L62: +L63: +L64: +L65: +L66: +L67: +L68: +L69: +L70: +L71: +L72: +L73: +L74: +L75: +L76: +L77: +L78: +L79: +L80: +L81: +L82: +L83: +L84: +L85: +L86: +L87: +L88: +L89: +L90: +L91: +L92: +L93: +L94: +L95: +L96: +L97: +L98: +L99: +L100: + // use the labels to silence compiler errors + goto L1 + goto L2 + goto L3 + goto L4 + goto L5 + goto L6 + goto L7 + goto L8 + goto L9 + goto L10 + goto L11 + goto L12 + goto L13 + goto L14 + goto L15 + goto L16 + goto L17 + goto L18 + goto L19 + goto L20 + goto L21 + goto L22 + goto L23 + goto L24 + goto L25 + goto L26 + goto L27 + goto L28 + goto L29 + goto L30 + goto L31 + goto L32 + goto L33 + goto L34 + goto L35 + goto L36 + goto L37 + goto L38 + goto L39 + goto L40 + goto L41 + goto L42 + goto L43 + goto L44 + goto L45 + goto L46 + goto L47 + goto L48 + goto L49 + goto L50 + goto L51 + goto L52 + goto L53 + goto L54 + goto L55 + goto L56 + goto L57 + goto L58 + goto L59 + goto L60 + goto L61 + goto L62 + goto L63 + goto L64 + goto L65 + goto L66 + goto L67 + goto L68 + goto L69 + goto L70 + goto L71 + goto L72 + goto L73 + goto L74 + goto L75 + goto L76 + goto L77 + goto L78 + goto L79 + goto L80 + goto L81 + goto L82 + goto L83 + goto L84 + goto L85 + goto L86 + goto L87 + goto L88 + goto L89 + goto L90 + goto L91 + goto L92 + goto L93 + goto L94 + goto L95 + goto L96 + goto L97 + goto L98 + goto L99 + goto L100 +} + +func foo121() { + for i := 0; i < 10; i++ { + defer myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$" + go myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$" + } +} + +// same as foo121 but check across import +func foo121b() { + for i := 0; i < 10; i++ { + defer fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$" + go fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$" + } +} + +// a harmless forward jump +func foo122() { + var i *int + + goto L1 +L1: + i = new(int) // ERROR "new\(int\) does not escape$" + _ = i +} + +// a backward jump, increases loopdepth +func foo123() { + var i *int + +L1: + i = new(int) // ERROR "new\(int\) escapes to heap$" + + goto L1 + _ = i +} + +func foo124(x **int) { // ERROR "x does not escape$" + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + *x = p + }() +} + +func foo125(ch chan *int) { // ERROR "ch does not escape$" + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + ch <- p + }() +} + +func foo126() { + var px *int // loopdepth 0 + for { + // loopdepth 1 + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() + } + _ = px +} + +var px *int + +func foo127() { + var i int // ERROR "moved to heap: i$" + p := &i + q := p + px = q +} + +func foo128() { + var i int + p := &i + q := p + _ = q +} + +func foo129() { + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + px = r + }() + }() +} + +func foo130() { + for { + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() + } +} + +func foo131() { + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() +} + +func foo132() { + var i int // ERROR "moved to heap: i$" + go func() { // ERROR "func literal escapes to heap$" + px = &i + }() +} + +func foo133() { + var i int // ERROR "moved to heap: i$" + defer func() { // ERROR "func literal does not escape$" + px = &i + }() +} + +func foo134() { + var i int + p := &i + func() { // ERROR "func literal does not escape$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + _ = r + }() + }() +} + +func foo135() { + var i int // ERROR "moved to heap: i$" + p := &i + go func() { // ERROR "func literal escapes to heap$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + _ = r + }() + }() +} + +func foo136() { + var i int // ERROR "moved to heap: i$" + p := &i + go func() { // ERROR "func literal escapes to heap$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + px = r + }() + }() +} + +func foo137() { + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + q := p + go func() { // ERROR "func literal escapes to heap$" + r := q + _ = r + }() + }() +} + +func foo138() *byte { + type T struct { + x [1]byte + } + t := new(T) // ERROR "new\(T\) escapes to heap$" + return &t.x[0] +} + +func foo139() *byte { + type T struct { + x struct { + y byte + } + } + t := new(T) // ERROR "new\(T\) escapes to heap$" + return &t.x.y +} + +// issue 4751 +func foo140() interface{} { + type T struct { + X string + } + type U struct { + X string + T *T + } + t := &T{} // ERROR "&T{} escapes to heap$" + return U{ // ERROR "U{...} escapes to heap$" + X: t.X, + T: t, + } +} + +//go:noescape + +func F1([]byte) + +func F2([]byte) + +//go:noescape + +func F3(x []byte) // ERROR "x does not escape$" + +func F4(x []byte) // ERROR "leaking param: x$" + +func G() { + var buf1 [10]byte + F1(buf1[:]) + + var buf2 [10]byte // ERROR "moved to heap: buf2$" + F2(buf2[:]) + + var buf3 [10]byte + F3(buf3[:]) + + var buf4 [10]byte // ERROR "moved to heap: buf4$" + F4(buf4[:]) +} + +type Tm struct { + x int +} + +func (t *Tm) M() { // ERROR "t does not escape$" +} + +func foo141() { + var f func() + + t := new(Tm) // ERROR "new\(Tm\) does not escape$" + f = t.M // ERROR "t.M does not escape$" + _ = f +} + +var gf func() + +func foo142() { + t := new(Tm) // ERROR "new\(Tm\) escapes to heap$" + gf = t.M // ERROR "t.M escapes to heap$" +} + +// issue 3888. +func foo143() { + for i := 0; i < 1000; i++ { + func() { // ERROR "func literal does not escape$" + for i := 0; i < 1; i++ { + var t Tm + t.M() + } + }() + } +} + +// issue 5773 +// Check that annotations take effect regardless of whether they +// are before or after the use in the source code. + +//go:noescape + +func foo144a(*int) + +func foo144() { + var x int + foo144a(&x) + var y int + foo144b(&y) +} + +//go:noescape + +func foo144b(*int) + +// issue 7313: for loop init should not be treated as "in loop" + +type List struct { + Next *List +} + +func foo145(l List) { // ERROR "l does not escape$" + var p *List + for p = &l; p.Next != nil; p = p.Next { + } +} + +func foo146(l List) { // ERROR "l does not escape$" + var p *List + p = &l + for ; p.Next != nil; p = p.Next { + } +} + +func foo147(l List) { // ERROR "l does not escape$" + var p *List + p = &l + for p.Next != nil { + p = p.Next + } +} + +func foo148(l List) { // ERROR "l does not escape$" + for p := &l; p.Next != nil; p = p.Next { + } +} + +// related: address of variable should have depth of variable, not of loop + +func foo149(l List) { // ERROR "l does not escape$" + var p *List + for { + for p = &l; p.Next != nil; p = p.Next { + } + } +} + +// issue 7934: missed ... if element type had no pointers + +var save150 []byte + +func foo150(x ...byte) { // ERROR "leaking param: x$" + save150 = x +} + +func bar150() { + foo150(1, 2, 3) // ERROR "... argument escapes to heap$" +} + +// issue 7931: bad handling of slice of array + +var save151 *int + +func foo151(x *int) { // ERROR "leaking param: x$" + save151 = x +} + +func bar151() { + var a [64]int // ERROR "moved to heap: a$" + a[4] = 101 + foo151(&(&a)[4:8][0]) +} + +func bar151b() { + var a [10]int // ERROR "moved to heap: a$" + b := a[:] + foo151(&b[4:8][0]) +} + +func bar151c() { + var a [64]int // ERROR "moved to heap: a$" + a[4] = 101 + foo151(&(&a)[4:8:8][0]) +} + +func bar151d() { + var a [10]int // ERROR "moved to heap: a$" + b := a[:] + foo151(&b[4:8:8][0]) +} + +// issue 8120 + +type U struct { + s *string +} + +func (u *U) String() *string { // ERROR "leaking param: u to result ~r0 level=1$" + return u.s +} + +type V struct { + s *string +} + +func NewV(u U) *V { // ERROR "leaking param: u$" + return &V{u.String()} // ERROR "&V{...} escapes to heap$" +} + +func foo152() { + a := "a" // ERROR "moved to heap: a$" + u := U{&a} + v := NewV(u) + println(v) +} + +// issue 8176 - &x in type switch body not marked as escaping + +func foo153(v interface{}) *int { // ERROR "v does not escape" + switch x := v.(type) { + case int: // ERROR "moved to heap: x$" + return &x + } + panic(0) // ERROR "0 escapes to heap" +} + +// issue 8185 - &result escaping into result + +func f() (x int, y *int) { // ERROR "moved to heap: x$" + y = &x + return +} + +func g() (x interface{}) { // ERROR "moved to heap: x$" + x = &x + return +} + +var sink interface{} + +type Lit struct { + p *int +} + +func ptrlitNoescape() { + // Both literal and element do not escape. + i := 0 + x := &Lit{&i} // ERROR "&Lit{...} does not escape$" + _ = x +} + +func ptrlitNoEscape2() { + // Literal does not escape, but element does. + i := 0 // ERROR "moved to heap: i$" + x := &Lit{&i} // ERROR "&Lit{...} does not escape$" + sink = *x +} + +func ptrlitEscape() { + // Both literal and element escape. + i := 0 // ERROR "moved to heap: i$" + x := &Lit{&i} // ERROR "&Lit{...} escapes to heap$" + sink = x +} + +// self-assignments + +type Buffer struct { + arr [64]byte + arrPtr *[64]byte + buf1 []byte + buf2 []byte + str1 string + str2 string +} + +func (b *Buffer) foo() { // ERROR "b does not escape$" + b.buf1 = b.buf1[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2\]$" + b.buf1 = b.buf1[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2:3\]$" + b.buf1 = b.buf2[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2\]$" + b.buf1 = b.buf2[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2:3\]$" +} + +func (b *Buffer) bar() { // ERROR "leaking param: b$" + b.buf1 = b.arr[1:2] +} + +func (b *Buffer) arrayPtr() { // ERROR "b does not escape" + b.buf1 = b.arrPtr[1:2] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2\]$" + b.buf1 = b.arrPtr[1:2:3] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2:3\]$" +} + +func (b *Buffer) baz() { // ERROR "b does not escape$" + b.str1 = b.str1[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str1\[1:2\]$" + b.str1 = b.str2[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str2\[1:2\]$" +} + +func (b *Buffer) bat() { // ERROR "leaking param content: b$" + o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap$" + o.buf1 = b.buf1[1:2] + sink = o +} + +func quux(sp *string, bp *[]byte) { // ERROR "bp does not escape$" "sp does not escape$" + *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment in \*sp = \(\*sp\)\[1:2\]$" + *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment in \*bp = \(\*bp\)\[1:2\]$" +} + +type StructWithString struct { + p *int + s string +} + +// This is escape analysis false negative. +// We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows +// to just x, and thus &i looks escaping. +func fieldFlowTracking() { + var x StructWithString + i := 0 // ERROR "moved to heap: i$" + x.p = &i + sink = x.s // ERROR "x.s escapes to heap$" +} + +// String operations. + +func slicebytetostring0() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) does not escape$" + _ = s +} + +func slicebytetostring1() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) does not escape$" + s1 := s[0:1] + _ = s1 +} + +func slicebytetostring2() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) escapes to heap$" + s1 := s[0:1] // ERROR "moved to heap: s1$" + sink = &s1 +} + +func slicebytetostring3() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) escapes to heap$" + s1 := s[0:1] + sink = s1 // ERROR "s1 escapes to heap$" +} + +func addstr0() { + s0 := "a" + s1 := "b" + s := s0 + s1 // ERROR "s0 \+ s1 does not escape$" + _ = s +} + +func addstr1() { + s0 := "a" + s1 := "b" + s := "c" + s += s0 + s1 // ERROR "s0 \+ s1 does not escape$" + _ = s +} + +func addstr2() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s0 := "a" + s := string(b) + s0 // ERROR "string\(b\) \+ s0 does not escape$" "string\(b\) does not escape$" + _ = s +} + +func addstr3() { + s0 := "a" + s1 := "b" + s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap$" + s2 := s[0:1] + sink = s2 // ERROR "s2 escapes to heap$" +} + +func intstring0() bool { + // string does not escape + x := '0' + s := string(x) // ERROR "string\(x\) does not escape$" + return s == "0" +} + +func intstring1() string { + // string does not escape, but the buffer does + x := '0' + s := string(x) // ERROR "string\(x\) escapes to heap$" + return s +} + +func intstring2() { + // string escapes to heap + x := '0' + s := string(x) // ERROR "moved to heap: s$" "string\(x\) escapes to heap$" + sink = &s +} + +func stringtoslicebyte0() { + s := "foo" + x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape$" "zero-copy string->\[\]byte conversion" + _ = x +} + +func stringtoslicebyte1() []byte { + s := "foo" + return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$" +} + +func stringtoslicebyte2() { + s := "foo" + sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$" +} + +func stringtoslicerune0() { + s := "foo" + x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape$" + _ = x +} + +func stringtoslicerune1() []rune { + s := "foo" + return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$" +} + +func stringtoslicerune2() { + s := "foo" + sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$" +} + +func slicerunetostring0() { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + s := string(r) // ERROR "string\(r\) does not escape$" + _ = s +} + +func slicerunetostring1() string { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + return string(r) // ERROR "string\(r\) escapes to heap$" +} + +func slicerunetostring2() { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + sink = string(r) // ERROR "string\(r\) escapes to heap$" +} + +func makemap0() { + m := make(map[int]int) // ERROR "make\(map\[int\]int\) does not escape$" + m[0] = 0 + m[1]++ + delete(m, 1) + sink = m[0] // ERROR "m\[0\] escapes to heap$" +} + +func makemap1() map[int]int { + return make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$" +} + +func makemap2() { + m := make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$" + sink = m +} + +func nonescapingEface(m map[interface{}]bool) bool { // ERROR "m does not escape$" + return m["foo"] // ERROR ".foo. does not escape$" +} + +func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape$" + return m[MV(0)] // ERROR "MV\(0\) does not escape$" +} + +func issue10353() { + x := new(int) // ERROR "new\(int\) escapes to heap$" + issue10353a(x)() +} + +func issue10353a(x *int) func() { // ERROR "leaking param: x$" + return func() { // ERROR "func literal escapes to heap$" + println(*x) + } +} + +func issue10353b() { + var f func() + for { + x := new(int) // ERROR "new\(int\) escapes to heap$" + f = func() { // ERROR "func literal escapes to heap$" + println(*x) + } + } + _ = f +} + +func issue11387(x int) func() int { + f := func() int { return x } // ERROR "func literal escapes to heap" + slice1 := []func() int{f} // ERROR "\[\].* does not escape" + slice2 := make([]func() int, 1) // ERROR "make\(.*\) does not escape" + copy(slice2, slice1) + return slice2[0] +} + +func issue12397(x, y int) { // ERROR "moved to heap: y$" + // x does not escape below, because all relevant code is dead. + if false { + gxx = &x + } else { + gxx = &y + } + + if true { + gxx = &y + } else { + gxx = &x + } +} diff --git a/platform/dbops/binaries/go/go/test/escape2n.go b/platform/dbops/binaries/go/go/test/escape2n.go new file mode 100644 index 0000000000000000000000000000000000000000..26131521504cd45a0f99007b9560a867cb538fc6 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape2n.go @@ -0,0 +1,1845 @@ +// errorcheck -0 -N -m -l + +// Copyright 2010 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. + +// Test, using compiler diagnostic flags, that the escape analysis is working. +// Compiles but does not run. Inlining is disabled. +// Registerization is disabled too (-N), which should +// have no effect on escape analysis. + +package foo + +import ( + "fmt" + "unsafe" +) + +var gxx *int + +func foo1(x int) { // ERROR "moved to heap: x$" + gxx = &x +} + +func foo2(yy *int) { // ERROR "leaking param: yy$" + gxx = yy +} + +func foo3(x int) *int { // ERROR "moved to heap: x$" + return &x +} + +type T *T + +func foo3b(t T) { // ERROR "leaking param: t$" + *t = t +} + +// xx isn't going anywhere, so use of yy is ok +func foo4(xx, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + xx = yy +} + +// xx isn't going anywhere, so taking address of yy is ok +func foo5(xx **int, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + xx = &yy +} + +func foo6(xx **int, yy *int) { // ERROR "xx does not escape$" "leaking param: yy$" + *xx = yy +} + +func foo7(xx **int, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + **xx = *yy +} + +func foo8(xx, yy *int) int { // ERROR "xx does not escape$" "yy does not escape$" + xx = yy + return *xx +} + +func foo9(xx, yy *int) *int { // ERROR "leaking param: xx to result ~r0 level=0$" "leaking param: yy to result ~r0 level=0$" + xx = yy + return xx +} + +func foo10(xx, yy *int) { // ERROR "xx does not escape$" "yy does not escape$" + *xx = *yy +} + +func foo11() int { + x, y := 0, 42 + xx := &x + yy := &y + *xx = *yy + return x +} + +var xxx **int + +func foo12(yyy **int) { // ERROR "leaking param: yyy$" + xxx = yyy +} + +// Must treat yyy as leaking because *yyy leaks, and the escape analysis +// summaries in exported metadata do not distinguish these two cases. +func foo13(yyy **int) { // ERROR "leaking param content: yyy$" + *xxx = *yyy +} + +func foo14(yyy **int) { // ERROR "yyy does not escape$" + **xxx = **yyy +} + +func foo15(yy *int) { // ERROR "moved to heap: yy$" + xxx = &yy +} + +func foo16(yy *int) { // ERROR "leaking param: yy$" + *xxx = yy +} + +func foo17(yy *int) { // ERROR "yy does not escape$" + **xxx = *yy +} + +func foo18(y int) { // ERROR "moved to heap: y$" + *xxx = &y +} + +func foo19(y int) { + **xxx = y +} + +type Bar struct { + i int + ii *int +} + +func NewBar() *Bar { + return &Bar{42, nil} // ERROR "&Bar{...} escapes to heap$" +} + +func NewBarp(x *int) *Bar { // ERROR "leaking param: x$" + return &Bar{42, x} // ERROR "&Bar{...} escapes to heap$" +} + +func NewBarp2(x *int) *Bar { // ERROR "x does not escape$" + return &Bar{*x, nil} // ERROR "&Bar{...} escapes to heap$" +} + +func (b *Bar) NoLeak() int { // ERROR "b does not escape$" + return *(b.ii) +} + +func (b *Bar) Leak() *int { // ERROR "leaking param: b to result ~r0 level=0$" + return &b.i +} + +func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param: b to result ~r0 level=1$" + return b.ii +} + +func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b to result ~r0 level=0$" + return b.ii +} + +func (b Bar) LeaksToo() *int { // ERROR "leaking param: b to result ~r0 level=0$" + v := 0 // ERROR "moved to heap: v$" + b.ii = &v + return b.ii +} + +func (b *Bar) LeaksABit() *int { // ERROR "leaking param: b to result ~r0 level=1$" + v := 0 // ERROR "moved to heap: v$" + b.ii = &v + return b.ii +} + +func (b Bar) StillNoLeak() int { // ERROR "b does not escape$" + v := 0 + b.ii = &v + return b.i +} + +func goLeak(b *Bar) { // ERROR "leaking param: b$" + go b.NoLeak() +} + +type Bar2 struct { + i [12]int + ii []int +} + +func NewBar2() *Bar2 { + return &Bar2{[12]int{42}, nil} // ERROR "&Bar2{...} escapes to heap$" +} + +func (b *Bar2) NoLeak() int { // ERROR "b does not escape$" + return b.i[0] +} + +func (b *Bar2) Leak() []int { // ERROR "leaking param: b to result ~r0 level=0$" + return b.i[:] +} + +func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param: b to result ~r0 level=1$" + return b.ii[0:1] +} + +func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape$" + return b.i +} + +func (b *Bar2) LeakSelf() { // ERROR "leaking param: b$" + b.ii = b.i[0:4] +} + +func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b$" + var buf []int + buf = b.i[0:] + b.ii = buf +} + +func foo21() func() int { + x := 42 + return func() int { // ERROR "func literal escapes to heap$" + return x + } +} + +func foo21a() func() int { + x := 42 // ERROR "moved to heap: x$" + return func() int { // ERROR "func literal escapes to heap$" + x++ + return x + } +} + +func foo22() int { + x := 42 + return func() int { // ERROR "func literal does not escape$" + return x + }() +} + +func foo23(x int) func() int { + return func() int { // ERROR "func literal escapes to heap$" + return x + } +} + +func foo23a(x int) func() int { + f := func() int { // ERROR "func literal escapes to heap$" + return x + } + return f +} + +func foo23b(x int) *(func() int) { + f := func() int { return x } // ERROR "func literal escapes to heap$" "moved to heap: f$" + return &f +} + +func foo23c(x int) func() int { // ERROR "moved to heap: x$" + return func() int { // ERROR "func literal escapes to heap$" + x++ + return x + } +} + +func foo24(x int) int { + return func() int { // ERROR "func literal does not escape$" + return x + }() +} + +var x *int + +func fooleak(xx *int) int { // ERROR "leaking param: xx$" + x = xx + return *x +} + +func foonoleak(xx *int) int { // ERROR "xx does not escape$" + return *x + *xx +} + +func foo31(x int) int { // ERROR "moved to heap: x$" + return fooleak(&x) +} + +func foo32(x int) int { + return foonoleak(&x) +} + +type Foo struct { + xx *int + x int +} + +var F Foo +var pf *Foo + +func (f *Foo) fooleak() { // ERROR "leaking param: f$" + pf = f +} + +func (f *Foo) foonoleak() { // ERROR "f does not escape$" + F.x = f.x +} + +func (f *Foo) Leak() { // ERROR "leaking param: f$" + f.fooleak() +} + +func (f *Foo) NoLeak() { // ERROR "f does not escape$" + f.foonoleak() +} + +func foo41(x int) { // ERROR "moved to heap: x$" + F.xx = &x +} + +func (f *Foo) foo42(x int) { // ERROR "f does not escape$" "moved to heap: x$" + f.xx = &x +} + +func foo43(f *Foo, x int) { // ERROR "f does not escape$" "moved to heap: x$" + f.xx = &x +} + +func foo44(yy *int) { // ERROR "leaking param: yy$" + F.xx = yy +} + +func (f *Foo) foo45() { // ERROR "f does not escape$" + F.x = f.x +} + +// See foo13 above for explanation of why f leaks. +func (f *Foo) foo46() { // ERROR "leaking param content: f$" + F.xx = f.xx +} + +func (f *Foo) foo47() { // ERROR "leaking param: f$" + f.xx = &f.x +} + +var ptrSlice []*int + +func foo50(i *int) { // ERROR "leaking param: i$" + ptrSlice[0] = i +} + +var ptrMap map[*int]*int + +func foo51(i *int) { // ERROR "leaking param: i$" + ptrMap[i] = i +} + +func indaddr1(x int) *int { // ERROR "moved to heap: x$" + return &x +} + +func indaddr2(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return *&x +} + +func indaddr3(x *int32) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return *(**int)(unsafe.Pointer(&x)) +} + +// From package math: + +func Float32bits(f float32) uint32 { + return *(*uint32)(unsafe.Pointer(&f)) +} + +func Float32frombits(b uint32) float32 { + return *(*float32)(unsafe.Pointer(&b)) +} + +func Float64bits(f float64) uint64 { + return *(*uint64)(unsafe.Pointer(&f)) +} + +func Float64frombits(b uint64) float64 { + return *(*float64)(unsafe.Pointer(&b)) +} + +// contrast with +func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f$" + return (*uint64)(unsafe.Pointer(&f)) +} + +func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f to result ~r0 level=0$" + return (*uint64)(unsafe.Pointer(f)) +} + +func typesw(i interface{}) *int { // ERROR "leaking param: i to result ~r0 level=0$" + switch val := i.(type) { + case *int: + return val + case *int8: + v := int(*val) // ERROR "moved to heap: v$" + return &v + } + return nil +} + +func exprsw(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + switch j := i; *j + 110 { + case 12: + return j + case 42: + return nil + } + return nil +} + +// assigning to an array element is like assigning to the array +func foo60(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + var a [12]*int + a[0] = i + return a[1] +} + +func foo60a(i *int) *int { // ERROR "i does not escape$" + var a [12]*int + a[0] = i + return nil +} + +// assigning to a struct field is like assigning to the struct +func foo61(i *int) *int { // ERROR "leaking param: i to result ~r0 level=0$" + type S struct { + a, b *int + } + var s S + s.a = i + return s.b +} + +func foo61a(i *int) *int { // ERROR "i does not escape$" + type S struct { + a, b *int + } + var s S + s.a = i + return nil +} + +// assigning to a struct field is like assigning to the struct but +// here this subtlety is lost, since s.a counts as an assignment to a +// track-losing dereference. +func foo62(i *int) *int { // ERROR "leaking param: i$" + type S struct { + a, b *int + } + s := new(S) // ERROR "new\(S\) does not escape$" + s.a = i + return nil // s.b +} + +type M interface { + M() +} + +func foo63(m M) { // ERROR "m does not escape$" +} + +func foo64(m M) { // ERROR "leaking param: m$" + m.M() +} + +func foo64b(m M) { // ERROR "leaking param: m$" + defer m.M() +} + +type MV int + +func (MV) M() {} + +func foo65() { + var mv MV + foo63(&mv) +} + +func foo66() { + var mv MV // ERROR "moved to heap: mv$" + foo64(&mv) +} + +func foo67() { + var mv MV + foo63(mv) // ERROR "mv does not escape$" +} + +func foo68() { + var mv MV + // escapes but it's an int so irrelevant + foo64(mv) // ERROR "mv escapes to heap$" +} + +func foo69(m M) { // ERROR "leaking param: m$" + foo64(m) +} + +func foo70(mv1 *MV, m M) { // ERROR "leaking param: m$" "leaking param: mv1$" + m = mv1 + foo64(m) +} + +func foo71(x *int) []*int { // ERROR "leaking param: x$" + var y []*int + y = append(y, x) + return y +} + +func foo71a(x int) []*int { // ERROR "moved to heap: x$" + var y []*int + y = append(y, &x) + return y +} + +func foo72() { + var x int + var y [1]*int + y[0] = &x +} + +func foo72aa() [10]*int { + var x int // ERROR "moved to heap: x$" + var y [10]*int + y[0] = &x + return y +} + +func foo72a() { + var y [10]*int + for i := 0; i < 10; i++ { + // escapes its scope + x := i // ERROR "moved to heap: x$" + y[i] = &x + } + return +} + +func foo72b() [10]*int { + var y [10]*int + for i := 0; i < 10; i++ { + x := i // ERROR "moved to heap: x$" + y[i] = &x + } + return y +} + +// issue 2145 +func foo73() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v + // actually just escapes its scope + defer func() { // ERROR "func literal escapes to heap$" + println(vv) + }() + } +} + +func foo731() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + defer func() { // ERROR "func literal escapes to heap$" + vv = 42 + println(vv) + }() + } +} + +func foo74() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v + // actually just escapes its scope + fn := func() { // ERROR "func literal escapes to heap$" + println(vv) + } + defer fn() + } +} + +func foo74a() { + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for _, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + fn := func() { // ERROR "func literal escapes to heap$" + vv += 1 + println(vv) + } + defer fn() + } +} + +// issue 3975 +func foo74b() { + var array [3]func() + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for i, v := range s { + vv := v + // actually just escapes its scope + array[i] = func() { // ERROR "func literal escapes to heap$" + println(vv) + } + } +} + +func foo74c() { + var array [3]func() + s := []int{3, 2, 1} // ERROR "\[\]int{...} does not escape$" + for i, v := range s { + vv := v // ERROR "moved to heap: vv$" + // actually just escapes its scope + array[i] = func() { // ERROR "func literal escapes to heap$" + println(&vv) + } + } +} + +func myprint(y *int, x ...interface{}) *int { // ERROR "leaking param: y to result ~r0 level=0$" "x does not escape$" + return y +} + +func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "leaking param: x to result ~r0 level=0$" "y does not escape$" + return &x[0] +} + +func foo75(z *int) { // ERROR "z does not escape$" + myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75a(z *int) { // ERROR "z does not escape$" + myprint1(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75esc(z *int) { // ERROR "leaking param: z$" + gxx = myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo75aesc(z *int) { // ERROR "z does not escape$" + var ppi **interface{} // assignments to pointer dereferences lose track + *ppi = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" +} + +func foo75aesc1(z *int) { // ERROR "z does not escape$" + sink = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" +} + +func foo76(z *int) { // ERROR "z does not escape" + myprint(nil, z) // ERROR "... argument does not escape$" +} + +func foo76a(z *int) { // ERROR "z does not escape" + myprint1(nil, z) // ERROR "... argument does not escape$" +} + +func foo76b() { + myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76c() { + myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76d() { + defer myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76e() { + defer myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$" +} + +func foo76f() { + for { + // TODO: This one really only escapes its scope, but we don't distinguish yet. + defer myprint(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" + } +} + +func foo76g() { + for { + defer myprint1(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" + } +} + +func foo77(z []interface{}) { // ERROR "z does not escape$" + myprint(nil, z...) // z does not escape +} + +func foo77a(z []interface{}) { // ERROR "z does not escape$" + myprint1(nil, z...) +} + +func foo77b(z []interface{}) { // ERROR "leaking param: z$" + var ppi **interface{} + *ppi = myprint1(nil, z...) +} + +func foo77c(z []interface{}) { // ERROR "leaking param: z$" + sink = myprint1(nil, z...) +} + +func dotdotdot() { + i := 0 + myprint(nil, &i) // ERROR "... argument does not escape$" + + j := 0 + myprint1(nil, &j) // ERROR "... argument does not escape$" +} + +func foo78(z int) *int { // ERROR "moved to heap: z$" + return &z +} + +func foo78a(z int) *int { // ERROR "moved to heap: z$" + y := &z + x := &y + return *x // really return y +} + +func foo79() *int { + return new(int) // ERROR "new\(int\) escapes to heap$" +} + +func foo80() *int { + var z *int + for { + // Really just escapes its scope but we don't distinguish + z = new(int) // ERROR "new\(int\) escapes to heap$" + } + _ = z + return nil +} + +func foo81() *int { + for { + z := new(int) // ERROR "new\(int\) does not escape$" + _ = z + } + return nil +} + +func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param: p to result x level=0$" "leaking param: p to result y level=0$" + +func noop(x, y *int) {} // ERROR "x does not escape$" "y does not escape$" + +func foo82() { + var x, y, z int // ERROR "moved to heap: x$" "moved to heap: y$" "moved to heap: z$" + go noop(tee(&z)) + go noop(&x, &y) + for { + var u, v, w int // ERROR "moved to heap: u$" "moved to heap: v$" "moved to heap: w$" + defer noop(tee(&u)) + defer noop(&v, &w) + } +} + +type Fooer interface { + Foo() +} + +type LimitedFooer struct { + Fooer + N int64 +} + +func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r$" + return &LimitedFooer{r, n} // ERROR "&LimitedFooer{...} escapes to heap$" +} + +func foo90(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo91(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo92(x *int) [2]*int { // ERROR "leaking param: x to result ~r0 level=0$" + return [2]*int{x, nil} +} + +// does not leak c +func foo93(c chan *int) *int { // ERROR "c does not escape$" + for v := range c { + return v + } + return nil +} + +// does not leak m +func foo94(m map[*int]*int, b bool) *int { // ERROR "leaking param: m to result ~r0 level=1" + for k, v := range m { + if b { + return k + } + return v + } + return nil +} + +// does leak x +func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape$" "leaking param: x$" + m[x] = x +} + +// does not leak m but does leak content +func foo96(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1" + return m[0] +} + +// does leak m +func foo97(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$" + return m[0] +} + +// does not leak m +func foo98(m map[int]*int) *int { // ERROR "m does not escape$" + return m[0] +} + +// does leak m +func foo99(m *[1]*int) []*int { // ERROR "leaking param: m to result ~r0 level=0$" + return m[:] +} + +// does not leak m +func foo100(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1" + for _, v := range m { + return v + } + return nil +} + +// does leak m +func foo101(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$" + for _, v := range m { + return v + } + return nil +} + +// does not leak m +func foo101a(m [1]*int) *int { // ERROR "m does not escape$" + for i := range m { // ERROR "moved to heap: i$" + return &i + } + return nil +} + +// does leak x +func foo102(m []*int, x *int) { // ERROR "m does not escape$" "leaking param: x$" + m[0] = x +} + +// does not leak x +func foo103(m [1]*int, x *int) { // ERROR "m does not escape$" "x does not escape$" + m[0] = x +} + +var y []*int + +// does not leak x but does leak content +func foo104(x []*int) { // ERROR "leaking param content: x" + copy(y, x) +} + +// does not leak x but does leak content +func foo105(x []*int) { // ERROR "leaking param content: x" + _ = append(y, x...) +} + +// does leak x +func foo106(x *int) { // ERROR "leaking param: x$" + _ = append(y, x) +} + +func foo107(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo108(x *int) map[*int]*int { // ERROR "leaking param: x$" + return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$" +} + +func foo109(x *int) *int { // ERROR "leaking param: x$" + m := map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} does not escape$" + for k, _ := range m { + return k + } + return nil +} + +func foo110(x *int) *int { // ERROR "leaking param: x$" + m := map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} does not escape$" + return m[nil] +} + +func foo111(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0" + m := []*int{x} // ERROR "\[\]\*int{...} does not escape$" + return m[0] +} + +func foo112(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := [1]*int{x} + return m[0] +} + +func foo113(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := Bar{ii: x} + return m.ii +} + +func foo114(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + m := &Bar{ii: x} // ERROR "&Bar{...} does not escape$" + return m.ii +} + +func foo115(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$" + return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1)) +} + +func foo116(b bool) *int { + if b { + x := 1 // ERROR "moved to heap: x$" + return &x + } else { + y := 1 // ERROR "moved to heap: y$" + return &y + } + return nil +} + +func foo117(unknown func(interface{})) { // ERROR "unknown does not escape$" + x := 1 // ERROR "moved to heap: x$" + unknown(&x) +} + +func foo118(unknown func(*int)) { // ERROR "unknown does not escape$" + x := 1 // ERROR "moved to heap: x$" + unknown(&x) +} + +func external(*int) + +func foo119(x *int) { // ERROR "leaking param: x$" + external(x) +} + +func foo120() { + // formerly exponential time analysis +L1: +L2: +L3: +L4: +L5: +L6: +L7: +L8: +L9: +L10: +L11: +L12: +L13: +L14: +L15: +L16: +L17: +L18: +L19: +L20: +L21: +L22: +L23: +L24: +L25: +L26: +L27: +L28: +L29: +L30: +L31: +L32: +L33: +L34: +L35: +L36: +L37: +L38: +L39: +L40: +L41: +L42: +L43: +L44: +L45: +L46: +L47: +L48: +L49: +L50: +L51: +L52: +L53: +L54: +L55: +L56: +L57: +L58: +L59: +L60: +L61: +L62: +L63: +L64: +L65: +L66: +L67: +L68: +L69: +L70: +L71: +L72: +L73: +L74: +L75: +L76: +L77: +L78: +L79: +L80: +L81: +L82: +L83: +L84: +L85: +L86: +L87: +L88: +L89: +L90: +L91: +L92: +L93: +L94: +L95: +L96: +L97: +L98: +L99: +L100: + // use the labels to silence compiler errors + goto L1 + goto L2 + goto L3 + goto L4 + goto L5 + goto L6 + goto L7 + goto L8 + goto L9 + goto L10 + goto L11 + goto L12 + goto L13 + goto L14 + goto L15 + goto L16 + goto L17 + goto L18 + goto L19 + goto L20 + goto L21 + goto L22 + goto L23 + goto L24 + goto L25 + goto L26 + goto L27 + goto L28 + goto L29 + goto L30 + goto L31 + goto L32 + goto L33 + goto L34 + goto L35 + goto L36 + goto L37 + goto L38 + goto L39 + goto L40 + goto L41 + goto L42 + goto L43 + goto L44 + goto L45 + goto L46 + goto L47 + goto L48 + goto L49 + goto L50 + goto L51 + goto L52 + goto L53 + goto L54 + goto L55 + goto L56 + goto L57 + goto L58 + goto L59 + goto L60 + goto L61 + goto L62 + goto L63 + goto L64 + goto L65 + goto L66 + goto L67 + goto L68 + goto L69 + goto L70 + goto L71 + goto L72 + goto L73 + goto L74 + goto L75 + goto L76 + goto L77 + goto L78 + goto L79 + goto L80 + goto L81 + goto L82 + goto L83 + goto L84 + goto L85 + goto L86 + goto L87 + goto L88 + goto L89 + goto L90 + goto L91 + goto L92 + goto L93 + goto L94 + goto L95 + goto L96 + goto L97 + goto L98 + goto L99 + goto L100 +} + +func foo121() { + for i := 0; i < 10; i++ { + defer myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$" + go myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$" + } +} + +// same as foo121 but check across import +func foo121b() { + for i := 0; i < 10; i++ { + defer fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$" + go fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$" + } +} + +// a harmless forward jump +func foo122() { + var i *int + + goto L1 +L1: + i = new(int) // ERROR "new\(int\) does not escape$" + _ = i +} + +// a backward jump, increases loopdepth +func foo123() { + var i *int + +L1: + i = new(int) // ERROR "new\(int\) escapes to heap$" + + goto L1 + _ = i +} + +func foo124(x **int) { // ERROR "x does not escape$" + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + *x = p + }() +} + +func foo125(ch chan *int) { // ERROR "ch does not escape$" + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + ch <- p + }() +} + +func foo126() { + var px *int // loopdepth 0 + for { + // loopdepth 1 + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() + } + _ = px +} + +var px *int + +func foo127() { + var i int // ERROR "moved to heap: i$" + p := &i + q := p + px = q +} + +func foo128() { + var i int + p := &i + q := p + _ = q +} + +func foo129() { + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + px = r + }() + }() +} + +func foo130() { + for { + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() + } +} + +func foo131() { + var i int // ERROR "moved to heap: i$" + func() { // ERROR "func literal does not escape$" + px = &i + }() +} + +func foo132() { + var i int // ERROR "moved to heap: i$" + go func() { // ERROR "func literal escapes to heap$" + px = &i + }() +} + +func foo133() { + var i int // ERROR "moved to heap: i$" + defer func() { // ERROR "func literal does not escape$" + px = &i + }() +} + +func foo134() { + var i int + p := &i + func() { // ERROR "func literal does not escape$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + _ = r + }() + }() +} + +func foo135() { + var i int // ERROR "moved to heap: i$" + p := &i + go func() { // ERROR "func literal escapes to heap$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + _ = r + }() + }() +} + +func foo136() { + var i int // ERROR "moved to heap: i$" + p := &i + go func() { // ERROR "func literal escapes to heap$" + q := p + func() { // ERROR "func literal does not escape$" + r := q + px = r + }() + }() +} + +func foo137() { + var i int // ERROR "moved to heap: i$" + p := &i + func() { // ERROR "func literal does not escape$" + q := p + go func() { // ERROR "func literal escapes to heap$" + r := q + _ = r + }() + }() +} + +func foo138() *byte { + type T struct { + x [1]byte + } + t := new(T) // ERROR "new\(T\) escapes to heap$" + return &t.x[0] +} + +func foo139() *byte { + type T struct { + x struct { + y byte + } + } + t := new(T) // ERROR "new\(T\) escapes to heap$" + return &t.x.y +} + +// issue 4751 +func foo140() interface{} { + type T struct { + X string + } + type U struct { + X string + T *T + } + t := &T{} // ERROR "&T{} escapes to heap$" + return U{ // ERROR "U{...} escapes to heap$" + X: t.X, + T: t, + } +} + +//go:noescape + +func F1([]byte) + +func F2([]byte) + +//go:noescape + +func F3(x []byte) // ERROR "x does not escape$" + +func F4(x []byte) // ERROR "leaking param: x$" + +func G() { + var buf1 [10]byte + F1(buf1[:]) + + var buf2 [10]byte // ERROR "moved to heap: buf2$" + F2(buf2[:]) + + var buf3 [10]byte + F3(buf3[:]) + + var buf4 [10]byte // ERROR "moved to heap: buf4$" + F4(buf4[:]) +} + +type Tm struct { + x int +} + +func (t *Tm) M() { // ERROR "t does not escape$" +} + +func foo141() { + var f func() + + t := new(Tm) // ERROR "new\(Tm\) does not escape$" + f = t.M // ERROR "t.M does not escape$" + _ = f +} + +var gf func() + +func foo142() { + t := new(Tm) // ERROR "new\(Tm\) escapes to heap$" + gf = t.M // ERROR "t.M escapes to heap$" +} + +// issue 3888. +func foo143() { + for i := 0; i < 1000; i++ { + func() { // ERROR "func literal does not escape$" + for i := 0; i < 1; i++ { + var t Tm + t.M() + } + }() + } +} + +// issue 5773 +// Check that annotations take effect regardless of whether they +// are before or after the use in the source code. + +//go:noescape + +func foo144a(*int) + +func foo144() { + var x int + foo144a(&x) + var y int + foo144b(&y) +} + +//go:noescape + +func foo144b(*int) + +// issue 7313: for loop init should not be treated as "in loop" + +type List struct { + Next *List +} + +func foo145(l List) { // ERROR "l does not escape$" + var p *List + for p = &l; p.Next != nil; p = p.Next { + } +} + +func foo146(l List) { // ERROR "l does not escape$" + var p *List + p = &l + for ; p.Next != nil; p = p.Next { + } +} + +func foo147(l List) { // ERROR "l does not escape$" + var p *List + p = &l + for p.Next != nil { + p = p.Next + } +} + +func foo148(l List) { // ERROR "l does not escape$" + for p := &l; p.Next != nil; p = p.Next { + } +} + +// related: address of variable should have depth of variable, not of loop + +func foo149(l List) { // ERROR "l does not escape$" + var p *List + for { + for p = &l; p.Next != nil; p = p.Next { + } + } +} + +// issue 7934: missed ... if element type had no pointers + +var save150 []byte + +func foo150(x ...byte) { // ERROR "leaking param: x$" + save150 = x +} + +func bar150() { + foo150(1, 2, 3) // ERROR "... argument escapes to heap$" +} + +// issue 7931: bad handling of slice of array + +var save151 *int + +func foo151(x *int) { // ERROR "leaking param: x$" + save151 = x +} + +func bar151() { + var a [64]int // ERROR "moved to heap: a$" + a[4] = 101 + foo151(&(&a)[4:8][0]) +} + +func bar151b() { + var a [10]int // ERROR "moved to heap: a$" + b := a[:] + foo151(&b[4:8][0]) +} + +func bar151c() { + var a [64]int // ERROR "moved to heap: a$" + a[4] = 101 + foo151(&(&a)[4:8:8][0]) +} + +func bar151d() { + var a [10]int // ERROR "moved to heap: a$" + b := a[:] + foo151(&b[4:8:8][0]) +} + +// issue 8120 + +type U struct { + s *string +} + +func (u *U) String() *string { // ERROR "leaking param: u to result ~r0 level=1$" + return u.s +} + +type V struct { + s *string +} + +func NewV(u U) *V { // ERROR "leaking param: u$" + return &V{u.String()} // ERROR "&V{...} escapes to heap$" +} + +func foo152() { + a := "a" // ERROR "moved to heap: a$" + u := U{&a} + v := NewV(u) + println(v) +} + +// issue 8176 - &x in type switch body not marked as escaping + +func foo153(v interface{}) *int { // ERROR "v does not escape" + switch x := v.(type) { + case int: // ERROR "moved to heap: x$" + return &x + } + panic(0) // ERROR "0 escapes to heap" +} + +// issue 8185 - &result escaping into result + +func f() (x int, y *int) { // ERROR "moved to heap: x$" + y = &x + return +} + +func g() (x interface{}) { // ERROR "moved to heap: x$" + x = &x + return +} + +var sink interface{} + +type Lit struct { + p *int +} + +func ptrlitNoescape() { + // Both literal and element do not escape. + i := 0 + x := &Lit{&i} // ERROR "&Lit{...} does not escape$" + _ = x +} + +func ptrlitNoEscape2() { + // Literal does not escape, but element does. + i := 0 // ERROR "moved to heap: i$" + x := &Lit{&i} // ERROR "&Lit{...} does not escape$" + sink = *x +} + +func ptrlitEscape() { + // Both literal and element escape. + i := 0 // ERROR "moved to heap: i$" + x := &Lit{&i} // ERROR "&Lit{...} escapes to heap$" + sink = x +} + +// self-assignments + +type Buffer struct { + arr [64]byte + arrPtr *[64]byte + buf1 []byte + buf2 []byte + str1 string + str2 string +} + +func (b *Buffer) foo() { // ERROR "b does not escape$" + b.buf1 = b.buf1[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2\]$" + b.buf1 = b.buf1[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2:3\]$" + b.buf1 = b.buf2[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2\]$" + b.buf1 = b.buf2[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2:3\]$" +} + +func (b *Buffer) bar() { // ERROR "leaking param: b$" + b.buf1 = b.arr[1:2] +} + +func (b *Buffer) arrayPtr() { // ERROR "b does not escape" + b.buf1 = b.arrPtr[1:2] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2\]$" + b.buf1 = b.arrPtr[1:2:3] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2:3\]$" +} + +func (b *Buffer) baz() { // ERROR "b does not escape$" + b.str1 = b.str1[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str1\[1:2\]$" + b.str1 = b.str2[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str2\[1:2\]$" +} + +func (b *Buffer) bat() { // ERROR "leaking param content: b$" + o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap$" + o.buf1 = b.buf1[1:2] + sink = o +} + +func quux(sp *string, bp *[]byte) { // ERROR "bp does not escape$" "sp does not escape$" + *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment in \*sp = \(\*sp\)\[1:2\]$" + *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment in \*bp = \(\*bp\)\[1:2\]$" +} + +type StructWithString struct { + p *int + s string +} + +// This is escape analysis false negative. +// We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows +// to just x, and thus &i looks escaping. +func fieldFlowTracking() { + var x StructWithString + i := 0 // ERROR "moved to heap: i$" + x.p = &i + sink = x.s // ERROR "x.s escapes to heap$" +} + +// String operations. + +func slicebytetostring0() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) does not escape$" + _ = s +} + +func slicebytetostring1() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) does not escape$" + s1 := s[0:1] + _ = s1 +} + +func slicebytetostring2() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) escapes to heap$" + s1 := s[0:1] // ERROR "moved to heap: s1$" + sink = &s1 +} + +func slicebytetostring3() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s := string(b) // ERROR "string\(b\) escapes to heap$" + s1 := s[0:1] + sink = s1 // ERROR "s1 escapes to heap$" +} + +func addstr0() { + s0 := "a" + s1 := "b" + s := s0 + s1 // ERROR "s0 \+ s1 does not escape$" + _ = s +} + +func addstr1() { + s0 := "a" + s1 := "b" + s := "c" + s += s0 + s1 // ERROR "s0 \+ s1 does not escape$" + _ = s +} + +func addstr2() { + b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$" + s0 := "a" + s := string(b) + s0 // ERROR "string\(b\) \+ s0 does not escape$" "string\(b\) does not escape$" + _ = s +} + +func addstr3() { + s0 := "a" + s1 := "b" + s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap$" + s2 := s[0:1] + sink = s2 // ERROR "s2 escapes to heap$" +} + +func intstring0() bool { + // string does not escape + x := '0' + s := string(x) // ERROR "string\(x\) does not escape$" + return s == "0" +} + +func intstring1() string { + // string does not escape, but the buffer does + x := '0' + s := string(x) // ERROR "string\(x\) escapes to heap$" + return s +} + +func intstring2() { + // string escapes to heap + x := '0' + s := string(x) // ERROR "moved to heap: s$" "string\(x\) escapes to heap$" + sink = &s +} + +func stringtoslicebyte0() { + s := "foo" + x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape$" "zero-copy string->\[\]byte conversion" + _ = x +} + +func stringtoslicebyte1() []byte { + s := "foo" + return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$" +} + +func stringtoslicebyte2() { + s := "foo" + sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$" +} + +func stringtoslicerune0() { + s := "foo" + x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape$" + _ = x +} + +func stringtoslicerune1() []rune { + s := "foo" + return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$" +} + +func stringtoslicerune2() { + s := "foo" + sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$" +} + +func slicerunetostring0() { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + s := string(r) // ERROR "string\(r\) does not escape$" + _ = s +} + +func slicerunetostring1() string { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + return string(r) // ERROR "string\(r\) escapes to heap$" +} + +func slicerunetostring2() { + r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$" + sink = string(r) // ERROR "string\(r\) escapes to heap$" +} + +func makemap0() { + m := make(map[int]int) // ERROR "make\(map\[int\]int\) does not escape$" + m[0] = 0 + m[1]++ + delete(m, 1) + sink = m[0] // ERROR "m\[0\] escapes to heap$" +} + +func makemap1() map[int]int { + return make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$" +} + +func makemap2() { + m := make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$" + sink = m +} + +func nonescapingEface(m map[interface{}]bool) bool { // ERROR "m does not escape$" + return m["foo"] // ERROR ".foo. does not escape$" +} + +func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape$" + return m[MV(0)] // ERROR "MV\(0\) does not escape$" +} + +func issue10353() { + x := new(int) // ERROR "new\(int\) escapes to heap$" + issue10353a(x)() +} + +func issue10353a(x *int) func() { // ERROR "leaking param: x$" + return func() { // ERROR "func literal escapes to heap$" + println(*x) + } +} + +func issue10353b() { + var f func() + for { + x := new(int) // ERROR "new\(int\) escapes to heap$" + f = func() { // ERROR "func literal escapes to heap$" + println(*x) + } + } + _ = f +} + +func issue11387(x int) func() int { + f := func() int { return x } // ERROR "func literal escapes to heap" + slice1 := []func() int{f} // ERROR "\[\].* does not escape" + slice2 := make([]func() int, 1) // ERROR "make\(.*\) does not escape" + copy(slice2, slice1) + return slice2[0] +} + +func issue12397(x, y int) { // ERROR "moved to heap: y$" + // x does not escape below, because all relevant code is dead. + if false { + gxx = &x + } else { + gxx = &y + } + + if true { + gxx = &y + } else { + gxx = &x + } +} diff --git a/platform/dbops/binaries/go/go/test/escape3.go b/platform/dbops/binaries/go/go/test/escape3.go new file mode 100644 index 0000000000000000000000000000000000000000..f1131a268845582d37dab792a10c7fbf90b37d83 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape3.go @@ -0,0 +1,36 @@ +// run + +// Copyright 2011 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. + +// Test the run-time behavior of escape analysis-related optimizations. + +package main + +func main() { + test1() +} + +func test1() { + check1(0) + check1(1) + check1(2) +} + +type T1 struct { + X, Y, Z int +} + +func f() int { + return 1 +} + +func check1(pass int) T1 { + v := []T1{{X: f(), Z: f()}} + if v[0].Y != 0 { + panic("nonzero init") + } + v[0].Y = pass + return v[0] +} diff --git a/platform/dbops/binaries/go/go/test/escape4.go b/platform/dbops/binaries/go/go/test/escape4.go new file mode 100644 index 0000000000000000000000000000000000000000..c4a2fc15e7ec297f1c45841e2cdf9b300463388a --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape4.go @@ -0,0 +1,59 @@ +// errorcheck -0 -m + +//go:build !goexperiment.newinliner + +// Copyright 2010 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. + +// Test, using compiler diagnostic flags, that the escape analysis is working. +// Compiles but does not run. Inlining is enabled. + +package foo + +var p *int + +func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x" + return &x +} + +var f func() + +func f1() { + p = alloc(2) // ERROR "inlining call to alloc" "moved to heap: x" + + // Escape analysis used to miss inlined code in closures. + + func() { // ERROR "can inline f1.func1" + p = alloc(3) // ERROR "inlining call to alloc" + }() // ERROR "inlining call to f1.func1" "inlining call to alloc" "moved to heap: x" + + f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2" + p = alloc(3) // ERROR "inlining call to alloc" "moved to heap: x" + } + f() +} + +func f2() {} // ERROR "can inline f2" + +// No inline for recover; panic now allowed to inline. +func f3() { panic(1) } // ERROR "can inline f3" "1 escapes to heap" +func f4() { recover() } + +func f5() *byte { // ERROR "can inline f5" + type T struct { + x [1]byte + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x[0] +} + +func f6() *byte { // ERROR "can inline f6" + type T struct { + x struct { + y byte + } + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x.y +} diff --git a/platform/dbops/binaries/go/go/test/escape5.go b/platform/dbops/binaries/go/go/test/escape5.go new file mode 100644 index 0000000000000000000000000000000000000000..133d973ba5d5f8290d7f2746f6a2c28918350a79 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape5.go @@ -0,0 +1,281 @@ +// errorcheck -0 -m -l + +// Copyright 2012 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. + +// Test, using compiler diagnostic flags, that the escape analysis is working. +// Compiles but does not run. Inlining is disabled. + +package foo + +import ( + "runtime" + "unsafe" +) + +func noleak(p *int) int { // ERROR "p does not escape" + return *p +} + +func leaktoret(p *int) *int { // ERROR "leaking param: p to result" + return p +} + +func leaktoret2(p *int) (*int, *int) { // ERROR "leaking param: p to result ~r0" "leaking param: p to result ~r1" + return p, p +} + +func leaktoret22(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r0" "leaking param: q to result ~r1" + return p, q +} + +func leaktoret22b(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: q to result ~r0" + return leaktoret22(q, p) +} + +func leaktoret22c(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: q to result ~r0" + r, s := leaktoret22(q, p) + return r, s +} + +func leaktoret22d(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r" + r, s = leaktoret22(q, p) + return +} + +func leaktoret22e(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r" + r, s = leaktoret22(q, p) + return r, s +} + +func leaktoret22f(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r" + rr, ss := leaktoret22(q, p) + return rr, ss +} + +var gp *int + +func leaktosink(p *int) *int { // ERROR "leaking param: p" + gp = p + return p +} + +func f1() { + var x int + p := noleak(&x) + _ = p +} + +func f2() { + var x int + p := leaktoret(&x) + _ = p +} + +func f3() { + var x int // ERROR "moved to heap: x" + p := leaktoret(&x) + gp = p +} + +func f4() { + var x int // ERROR "moved to heap: x" + p, q := leaktoret2(&x) + gp = p + gp = q +} + +func f5() { + var x int + leaktoret22(leaktoret2(&x)) +} + +func f6() { + var x int // ERROR "moved to heap: x" + px1, px2 := leaktoret22(leaktoret2(&x)) + gp = px1 + _ = px2 +} + +type T struct{ x int } + +func (t *T) Foo(u int) (*T, bool) { // ERROR "leaking param: t to result" + t.x += u + return t, true +} + +func f7() *T { + r, _ := new(T).Foo(42) // ERROR "new.T. escapes to heap" + return r +} + +func leakrecursive1(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q" + return leakrecursive2(q, p) +} + +func leakrecursive2(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q" + if *p > *q { + return leakrecursive1(q, p) + } + // without this, leakrecursive? are safe for p and q, b/c in fact their graph does not have leaking edges. + return p, q +} + +var global interface{} + +type T1 struct { + X *int +} + +type T2 struct { + Y *T1 +} + +func f8(p *T1) (k T2) { // ERROR "leaking param: p$" + if p == nil { + k = T2{} + return + } + + // should make p leak always + global = p + return T2{p} +} + +func f9() { + var j T1 // ERROR "moved to heap: j" + f8(&j) +} + +func f10() { + // These don't escape but are too big for the stack + var x [1 << 30]byte // ERROR "moved to heap: x" + var y = make([]byte, 1<<30) // ERROR "make\(\[\]byte, 1073741824\) escapes to heap" + _ = x[0] + y[0] +} + +// Test for issue 19687 (passing to unnamed parameters does not escape). +func f11(**int) { +} +func f12(_ **int) { +} +func f13() { + var x *int + f11(&x) + f12(&x) + runtime.KeepAlive(&x) +} + +// Test for issue 24305 (passing to unnamed receivers does not escape). +type U int + +func (*U) M() {} +func (_ *U) N() {} + +func fbad24305a() { + var u U + u.M() + u.N() +} + +func fbad24305b() { + var u U + (*U).M(&u) + (*U).N(&u) +} + +// Issue 24730: taking address in a loop causes unnecessary escape +type T24730 struct { + x [64]byte +} + +func (t *T24730) g() { // ERROR "t does not escape" + y := t.x[:] + for i := range t.x[:] { + y = t.x[:] + y[i] = 1 + } + + var z *byte + for i := range t.x[:] { + z = &t.x[i] + *z = 2 + } +} + +// Issue 15730: copy causes unnecessary escape + +var sink []byte +var sink2 []int +var sink3 []*int + +func f15730a(args ...interface{}) { // ERROR "args does not escape" + for _, arg := range args { + switch a := arg.(type) { + case string: + copy(sink, a) + } + } +} + +func f15730b(args ...interface{}) { // ERROR "args does not escape" + for _, arg := range args { + switch a := arg.(type) { + case []int: + copy(sink2, a) + } + } +} + +func f15730c(args ...interface{}) { // ERROR "leaking param content: args" + for _, arg := range args { + switch a := arg.(type) { + case []*int: + // copy pointerful data should cause escape + copy(sink3, a) + } + } +} + +// Issue 29000: unnamed parameter is not handled correctly + +var sink4 interface{} +var alwaysFalse = false + +func f29000(_ int, x interface{}) { // ERROR "leaking param: x" + sink4 = x + if alwaysFalse { + g29000() + } +} + +func g29000() { + x := 1 + f29000(2, x) // ERROR "x escapes to heap" +} + +// Issue 28369: taking an address of a parameter and converting it into a uintptr causes an +// unnecessary escape. + +var sink28369 uintptr + +func f28369(n int) int { + if n == 0 { + sink28369 = uintptr(unsafe.Pointer(&n)) + return n + } + + return 1 + f28369(n-1) +} + +// Issue 44614: parameters that flow to a heap-allocated result +// parameter must be recorded as a heap-flow rather than a +// result-flow. + +// N.B., must match "leaking param: p", +// but *not* "leaking param: p to result r level=0". +func f(p *int) (r *int) { // ERROR "leaking param: p$" "moved to heap: r" + sink4 = &r + return p +} diff --git a/platform/dbops/binaries/go/go/test/escape_array.go b/platform/dbops/binaries/go/go/test/escape_array.go new file mode 100644 index 0000000000000000000000000000000000000000..83062c9436e71d2a1ed9462633b2e8d829a63197 --- /dev/null +++ b/platform/dbops/binaries/go/go/test/escape_array.go @@ -0,0 +1,129 @@ +// errorcheck -0 -m -l + +// Copyright 2015 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. + +// Test escape analysis for arrays and some large things + +package foo + +var Ssink *string + +type U [2]*string + +func bar(a, b *string) U { // ERROR "leaking param: a to result ~r0 level=0$" "leaking param: b to result ~r0 level=0$" + return U{a, b} +} + +func foo(x U) U { // ERROR "leaking param: x to result ~r0 level=0$" + return U{x[1], x[0]} +} + +func bff(a, b *string) U { // ERROR "leaking param: a to result ~r0 level=0$" "leaking param: b to result ~r0 level=0$" + return foo(foo(bar(a, b))) +} + +func tbff1() *string { + a := "cat" + b := "dog" // ERROR "moved to heap: b$" + u := bff(&a, &b) + _ = u[0] + return &b +} + +// BAD: need fine-grained analysis to track u[0] and u[1] differently. +func tbff2() *string { + a := "cat" // ERROR "moved to heap: a$" + b := "dog" // ERROR "moved to heap: b$" + u := bff(&a, &b) + _ = u[0] + return u[1] +} + +func car(x U) *string { // ERROR "leaking param: x to result ~r0 level=0$" + return x[0] +} + +// BAD: need fine-grained analysis to track x[0] and x[1] differently. +func fun(x U, y *string) *string { // ERROR "leaking param: x to result ~r0 level=0$" "leaking param: y to result ~r0 level=0$" + x[0] = y + return x[1] +} + +func fup(x *U, y *string) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param: y$" + x[0] = y // leaking y to heap is intended + return x[1] +} + +func fum(x *U, y **string) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param content: y$" + x[0] = *y + return x[1] +} + +func fuo(x *U, y *U) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param content: y$" + x[0] = y[0] + return x[1] +} + +// These two tests verify that: +// small array literals are stack allocated; +// pointers stored in small array literals do not escape; +// large array literals are heap allocated; +// pointers stored in large array literals escape. +func hugeLeaks1(x **string, y **string) { // ERROR "leaking param content: x" "y does not escape" + a := [10]*string{*y} + _ = a + // 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger. + b := [4000000]*string{*x} // ERROR "moved to heap: b" + _ = b +} + +func hugeLeaks2(x *string, y *string) { // ERROR "leaking param: x" "y does not escape" + a := [10]*string{y} + _ = a + // 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger. + b := [4000000]*string{x} // ERROR "moved to heap: b" + _ = b +} + +// BAD: x need not leak. +func doesNew1(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" + a := new([10]*string) // ERROR "new\(\[10\]\*string\) does not escape" + a[0] = x + b := new([65537]*string) // ERROR "new\(\[65537\]\*string\) escapes to heap" + b[0] = y +} + +type a10 struct { + s *string + i [10]int32 +} + +type a65537 struct { + s *string + i [65537]int32 +} + +// BAD: x need not leak. +func doesNew2(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" + a := new(a10) // ERROR "new\(a10\) does not escape" + a.s = x + b := new(a65537) // ERROR "new\(a65537\) escapes to heap" + b.s = y +} + +// BAD: x need not leak. +func doesMakeSlice(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" + a := make([]*string, 10) // ERROR "make\(\[\]\*string, 10\) does not escape" + a[0] = x + b := make([]*string, 65537) // ERROR "make\(\[\]\*string, 65537\) escapes to heap" + b[0] = y +} + +func nonconstArray() { + n := 32 + s1 := make([]int, n) // ERROR "make\(\[\]int, n\) escapes to heap" + s2 := make([]int, 0, n) // ERROR "make\(\[\]int, 0, n\) escapes to heap" + _, _ = s1, s2 +} diff --git a/platform/dbops/projects/dto/.env.example b/platform/dbops/projects/dto/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..fd11aed7def442c148e87b9b27c3dc73fdc60e5b --- /dev/null +++ b/platform/dbops/projects/dto/.env.example @@ -0,0 +1,45 @@ +# DTO Framework Environment Example + +# Core +DTO_ENV=production + +# Hugging Face / Xet +HF_TOKEN=replace_with_hf_token +HF_ORG=your-org +HF_REPO_ARTIFACTS=your-org/dto-artifacts +HF_REPO_DATASETS=your-org/dto-datasets +HF_REPO_MODELS=your-org/dto-models +HF_MAX_UPLOAD_THREADS=8 +HF_CHUNK_SIZE_MB=64 +HF_TIMEOUT=300 + +# NATS (events) +NATS_PRODUCER_PASSWORD=replace +NATS_CONSUMER_PASSWORD=replace +NATS_ADMIN_PASSWORD=replace + +# Dragonfly (cache) +DRAGONFLY_PASSWORD=replace +DRAGONFLY_PRODUCER_PASSWORD=replace +DRAGONFLY_CONSUMER_PASSWORD=replace +DRAGONFLY_ADMIN_PASSWORD=replace + +# Slack +SLACK_BOT_TOKEN=xoxb-... +SLACK_APP_TOKEN=xapp-... + +# Jira +JIRA_SERVER_URL=https://jira.example.com +JIRA_USERNAME=your-user +JIRA_API_TOKEN=your-token + +# Confluence +CONFLUENCE_SERVER_URL=https://confluence.example.com +CONFLUENCE_USERNAME=your-user +CONFLUENCE_API_TOKEN=your-token + +# Cassandra / Redis (optional) +CASSANDRA_USERNAME= +CASSANDRA_PASSWORD= +REDIS_CLUSTER_PASSWORD= +