summaryrefslogtreecommitdiffstats
path: root/test/uintptrescapes.dir
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
commitf6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch)
tree7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /test/uintptrescapes.dir
parentInitial commit. (diff)
downloadgolang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz
golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/uintptrescapes.dir')
-rw-r--r--test/uintptrescapes.dir/a.go54
-rw-r--r--test/uintptrescapes.dir/main.go91
2 files changed, 145 insertions, 0 deletions
diff --git a/test/uintptrescapes.dir/a.go b/test/uintptrescapes.dir/a.go
new file mode 100644
index 0000000..29c8340
--- /dev/null
+++ b/test/uintptrescapes.dir/a.go
@@ -0,0 +1,54 @@
+// 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.
+
+package a
+
+import (
+ "unsafe"
+)
+
+func recurse(i int, s []byte) byte {
+ s[0] = byte(i)
+ if i == 0 {
+ return s[i]
+ } else {
+ var a [1024]byte
+ r := recurse(i-1, a[:])
+ return r + a[0]
+ }
+}
+
+//go:uintptrescapes
+func F1(a uintptr) {
+ var s [16]byte
+ recurse(4096, s[:])
+ *(*int)(unsafe.Pointer(a)) = 42
+}
+
+//go:uintptrescapes
+func F2(a ...uintptr) {
+ var s [16]byte
+ recurse(4096, s[:])
+ *(*int)(unsafe.Pointer(a[0])) = 42
+}
+
+type t struct{}
+
+func GetT() *t {
+ return &t{}
+}
+
+//go:uintptrescapes
+func (*t) M1(a uintptr) {
+ var s [16]byte
+ recurse(4096, s[:])
+ *(*int)(unsafe.Pointer(a)) = 42
+}
+
+//go:uintptrescapes
+func (*t) M2(a ...uintptr) {
+ var s [16]byte
+ recurse(4096, s[:])
+ *(*int)(unsafe.Pointer(a[0])) = 42
+}
diff --git a/test/uintptrescapes.dir/main.go b/test/uintptrescapes.dir/main.go
new file mode 100644
index 0000000..afda621
--- /dev/null
+++ b/test/uintptrescapes.dir/main.go
@@ -0,0 +1,91 @@
+// 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.
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "sync"
+ "unsafe"
+
+ "./a"
+)
+
+func F1() int {
+ var buf [1024]int
+ a.F1(uintptr(unsafe.Pointer(&buf[0])))
+ return buf[0]
+}
+
+func F2() int {
+ var buf [1024]int
+ a.F2(uintptr(unsafe.Pointer(&buf[0])))
+ return buf[0]
+}
+
+var t = a.GetT()
+
+func M1() int {
+ var buf [1024]int
+ t.M1(uintptr(unsafe.Pointer(&buf[0])))
+ return buf[0]
+}
+
+func M2() int {
+ var buf [1024]int
+ t.M2(uintptr(unsafe.Pointer(&buf[0])))
+ return buf[0]
+}
+
+func main() {
+ // Use different goroutines to force stack growth.
+ var wg sync.WaitGroup
+ wg.Add(4)
+ c := make(chan bool, 4)
+
+ go func() {
+ defer wg.Done()
+ b := F1()
+ if b != 42 {
+ fmt.Printf("F1: got %d, expected 42\n", b)
+ c <- false
+ }
+ }()
+
+ go func() {
+ defer wg.Done()
+ b := F2()
+ if b != 42 {
+ fmt.Printf("F2: got %d, expected 42\n", b)
+ c <- false
+ }
+ }()
+
+ go func() {
+ defer wg.Done()
+ b := M1()
+ if b != 42 {
+ fmt.Printf("M1: got %d, expected 42\n", b)
+ c <- false
+ }
+ }()
+
+ go func() {
+ defer wg.Done()
+ b := M2()
+ if b != 42 {
+ fmt.Printf("M2: got %d, expected 42\n", b)
+ c <- false
+ }
+ }()
+
+ wg.Wait()
+
+ select {
+ case <-c:
+ os.Exit(1)
+ default:
+ }
+}