ADAPT-Chase commited on
Commit
a860e08
verified
1 Parent(s): 96c56c2

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes. 聽 See raw diff
Files changed (50) hide show
  1. .gitattributes +11 -0
  2. platform/dbops/binaries/go/go/src/runtime/cgo/gcc_util.c +69 -0
  3. platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_386.c +53 -0
  4. platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_amd64.c +52 -0
  5. platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_arm64.c +40 -0
  6. platform/dbops/binaries/go/go/src/runtime/cgo/handle.go +144 -0
  7. platform/dbops/binaries/go/go/src/runtime/cgo/handle_test.go +103 -0
  8. platform/dbops/binaries/go/go/src/runtime/cgo/iscgo.go +17 -0
  9. platform/dbops/binaries/go/go/src/runtime/cgo/libcgo.h +151 -0
  10. platform/dbops/binaries/go/go/src/runtime/cgo/libcgo_unix.h +20 -0
  11. platform/dbops/binaries/go/go/src/runtime/cgo/libcgo_windows.h +6 -0
  12. platform/dbops/binaries/go/go/src/runtime/cgo/linux.go +74 -0
  13. platform/dbops/binaries/go/go/src/runtime/cgo/linux_syscall.c +85 -0
  14. platform/dbops/binaries/go/go/src/runtime/cgo/mmap.go +31 -0
  15. platform/dbops/binaries/go/go/src/runtime/cgo/netbsd.go +21 -0
  16. platform/dbops/binaries/go/go/src/runtime/cgo/openbsd.go +21 -0
  17. platform/dbops/binaries/go/go/src/runtime/cgo/setenv.go +21 -0
  18. platform/dbops/binaries/go/go/src/runtime/cgo/sigaction.go +22 -0
  19. platform/dbops/binaries/go/go/src/runtime/cgo/signal_ios_arm64.go +10 -0
  20. platform/dbops/binaries/go/go/src/runtime/cgo/signal_ios_arm64.s +56 -0
  21. platform/dbops/binaries/go/go/src/runtime/coverage/apis.go +178 -0
  22. platform/dbops/binaries/go/go/src/runtime/coverage/dummy.s +8 -0
  23. platform/dbops/binaries/go/go/src/runtime/coverage/emit.go +609 -0
  24. platform/dbops/binaries/go/go/src/runtime/coverage/emitdata_test.go +550 -0
  25. platform/dbops/binaries/go/go/src/runtime/coverage/hooks.go +42 -0
  26. platform/dbops/binaries/go/go/src/runtime/coverage/testdata/harness.go +259 -0
  27. platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue56006/repro.go +26 -0
  28. platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue56006/repro_test.go +8 -0
  29. platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue59563/repro.go +823 -0
  30. platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue59563/repro_test.go +14 -0
  31. platform/dbops/binaries/go/go/src/runtime/coverage/testsupport.go +323 -0
  32. platform/dbops/binaries/go/go/src/runtime/coverage/ts_test.go +207 -0
  33. platform/dbops/binaries/go/go/src/runtime/debug/debug.s +9 -0
  34. platform/dbops/binaries/go/go/src/runtime/debug/garbage.go +238 -0
  35. platform/dbops/binaries/go/go/src/runtime/debug/garbage_test.go +238 -0
  36. platform/dbops/binaries/go/go/src/runtime/debug/heapdump_test.go +95 -0
  37. platform/dbops/binaries/go/go/src/runtime/debug/mod.go +287 -0
  38. platform/dbops/binaries/go/go/src/runtime/debug/mod_test.go +75 -0
  39. platform/dbops/binaries/go/go/src/runtime/debug/panic_test.go +56 -0
  40. platform/dbops/binaries/go/go/src/runtime/debug/stack.go +30 -0
  41. platform/dbops/binaries/go/go/src/runtime/debug/stack_test.go +121 -0
  42. platform/dbops/binaries/go/go/src/runtime/debug/stubs.go +18 -0
  43. platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/5501685e611fa764 +2 -0
  44. platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/71634114e78567cf +2 -0
  45. platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/c73dce23c1f2494c +2 -0
  46. platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_386.go +121 -0
  47. platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_386.s +366 -0
  48. platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_amd64.go +135 -0
  49. platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_amd64.s +289 -0
  50. platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_andor_generic.go +67 -0
.gitattributes CHANGED
@@ -3162,3 +3162,14 @@ platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_19_good filte
3162
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_21_good filter=lfs diff=lfs merge=lfs -text
3163
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_7_good filter=lfs diff=lfs merge=lfs -text
3164
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_9_good filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
3162
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_21_good filter=lfs diff=lfs merge=lfs -text
3163
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_7_good filter=lfs diff=lfs merge=lfs -text
3164
  platform/dbops/binaries/go/go/src/internal/trace/testdata/stress_1_9_good filter=lfs diff=lfs merge=lfs -text
3165
+ platform/dbops/binaries/go/go/src/runtime/race/race_darwin_arm64.syso filter=lfs diff=lfs merge=lfs -text
3166
+ platform/dbops/binaries/go/go/src/runtime/race/race_linux_arm64.syso filter=lfs diff=lfs merge=lfs -text
3167
+ platform/dbops/binaries/go/go/src/runtime/race/race_linux_ppc64le.syso filter=lfs diff=lfs merge=lfs -text
3168
+ platform/dbops/binaries/go/go/src/runtime/race/race_linux_s390x.syso filter=lfs diff=lfs merge=lfs -text
3169
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_darwin.syso filter=lfs diff=lfs merge=lfs -text
3170
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_freebsd.syso filter=lfs diff=lfs merge=lfs -text
3171
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_linux.syso filter=lfs diff=lfs merge=lfs -text
3172
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_netbsd.syso filter=lfs diff=lfs merge=lfs -text
3173
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_openbsd.syso filter=lfs diff=lfs merge=lfs -text
3174
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v1/race_windows.syso filter=lfs diff=lfs merge=lfs -text
3175
+ platform/dbops/binaries/go/go/src/runtime/race/internal/amd64v3/race_linux.syso filter=lfs diff=lfs merge=lfs -text
platform/dbops/binaries/go/go/src/runtime/cgo/gcc_util.c ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #include "libcgo.h"
6
+
7
+ /* Stub for creating a new thread */
8
+ void
9
+ x_cgo_thread_start(ThreadStart *arg)
10
+ {
11
+ ThreadStart *ts;
12
+
13
+ /* Make our own copy that can persist after we return. */
14
+ _cgo_tsan_acquire();
15
+ ts = malloc(sizeof *ts);
16
+ _cgo_tsan_release();
17
+ if(ts == nil) {
18
+ fprintf(stderr, "runtime/cgo: out of memory in thread_start\n");
19
+ abort();
20
+ }
21
+ *ts = *arg;
22
+
23
+ _cgo_sys_thread_start(ts); /* OS-dependent half */
24
+ }
25
+
26
+ #ifndef CGO_TSAN
27
+ void(* const _cgo_yield)() = NULL;
28
+ #else
29
+
30
+ #include <string.h>
31
+
32
+ char x_cgo_yield_strncpy_src = 0;
33
+ char x_cgo_yield_strncpy_dst = 0;
34
+ size_t x_cgo_yield_strncpy_n = 0;
35
+
36
+ /*
37
+ Stub for allowing libc interceptors to execute.
38
+
39
+ _cgo_yield is set to NULL if we do not expect libc interceptors to exist.
40
+ */
41
+ static void
42
+ x_cgo_yield()
43
+ {
44
+ /*
45
+ The libc function(s) we call here must form a no-op and include at least one
46
+ call that triggers TSAN to process pending asynchronous signals.
47
+
48
+ sleep(0) would be fine, but it's not portable C (so it would need more header
49
+ guards).
50
+ free(NULL) has a fast-path special case in TSAN, so it doesn't
51
+ trigger signal delivery.
52
+ free(malloc(0)) would work (triggering the interceptors in malloc), but
53
+ it also runs a bunch of user-supplied malloc hooks.
54
+
55
+ So we choose strncpy(_, _, 0): it requires an extra header,
56
+ but it's standard and should be very efficient.
57
+
58
+ GCC 7 has an unfortunate habit of optimizing out strncpy calls (see
59
+ https://golang.org/issue/21196), so the arguments here need to be global
60
+ variables with external linkage in order to ensure that the call traps all the
61
+ way down into libc.
62
+ */
63
+ strncpy(&x_cgo_yield_strncpy_dst, &x_cgo_yield_strncpy_src,
64
+ x_cgo_yield_strncpy_n);
65
+ }
66
+
67
+ void(* const _cgo_yield)() = &x_cgo_yield;
68
+
69
+ #endif /* GO_TSAN */
platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_386.c ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #define WIN32_LEAN_AND_MEAN
6
+ #include <windows.h>
7
+ #include <process.h>
8
+ #include <stdlib.h>
9
+ #include <stdio.h>
10
+ #include <errno.h>
11
+ #include "libcgo.h"
12
+ #include "libcgo_windows.h"
13
+
14
+ static void threadentry(void*);
15
+ static void (*setg_gcc)(void*);
16
+ static DWORD *tls_g;
17
+
18
+ void
19
+ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
20
+ {
21
+ setg_gcc = setg;
22
+ tls_g = (DWORD *)tlsg;
23
+ }
24
+
25
+ void
26
+ _cgo_sys_thread_start(ThreadStart *ts)
27
+ {
28
+ _cgo_beginthread(threadentry, ts);
29
+ }
30
+
31
+ extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
32
+ static void
33
+ threadentry(void *v)
34
+ {
35
+ ThreadStart ts;
36
+
37
+ ts = *(ThreadStart*)v;
38
+ free(v);
39
+
40
+ // minit queries stack bounds from the OS.
41
+
42
+ /*
43
+ * Set specific keys in thread local storage.
44
+ */
45
+ asm volatile (
46
+ "movl %0, %%fs:0(%1)\n" // MOVL tls0, 0(tls_g)(FS)
47
+ "movl %%fs:0(%1), %%eax\n" // MOVL 0(tls_g)(FS), tmp
48
+ "movl %2, 0(%%eax)\n" // MOVL g, 0(AX)
49
+ :: "r"(ts.tls), "r"(*tls_g), "r"(ts.g) : "%eax"
50
+ );
51
+
52
+ crosscall1(ts.fn, setg_gcc, ts.g);
53
+ }
platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_amd64.c ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #define WIN32_LEAN_AND_MEAN
6
+ #include <windows.h>
7
+ #include <process.h>
8
+ #include <stdlib.h>
9
+ #include <stdio.h>
10
+ #include <errno.h>
11
+ #include "libcgo.h"
12
+ #include "libcgo_windows.h"
13
+
14
+ static void threadentry(void*);
15
+ static void (*setg_gcc)(void*);
16
+ static DWORD *tls_g;
17
+
18
+ void
19
+ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
20
+ {
21
+ setg_gcc = setg;
22
+ tls_g = (DWORD *)tlsg;
23
+ }
24
+
25
+
26
+ void
27
+ _cgo_sys_thread_start(ThreadStart *ts)
28
+ {
29
+ _cgo_beginthread(threadentry, ts);
30
+ }
31
+
32
+ extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
33
+ static void
34
+ threadentry(void *v)
35
+ {
36
+ ThreadStart ts;
37
+
38
+ ts = *(ThreadStart*)v;
39
+ free(v);
40
+
41
+ // minit queries stack bounds from the OS.
42
+
43
+ /*
44
+ * Set specific keys in thread local storage.
45
+ */
46
+ asm volatile (
47
+ "movq %0, %%gs:0(%1)\n" // MOVL tls0, 0(tls_g)(GS)
48
+ :: "r"(ts.tls), "r"(*tls_g)
49
+ );
50
+
51
+ crosscall1(ts.fn, setg_gcc, (void*)ts.g);
52
+ }
platform/dbops/binaries/go/go/src/runtime/cgo/gcc_windows_arm64.c ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #define WIN32_LEAN_AND_MEAN
6
+ #include <windows.h>
7
+ #include <process.h>
8
+ #include <stdlib.h>
9
+ #include <stdio.h>
10
+ #include <errno.h>
11
+ #include "libcgo.h"
12
+ #include "libcgo_windows.h"
13
+
14
+ static void threadentry(void*);
15
+ static void (*setg_gcc)(void*);
16
+
17
+ void
18
+ x_cgo_init(G *g, void (*setg)(void*))
19
+ {
20
+ setg_gcc = setg;
21
+ }
22
+
23
+ void
24
+ _cgo_sys_thread_start(ThreadStart *ts)
25
+ {
26
+ _cgo_beginthread(threadentry, ts);
27
+ }
28
+
29
+ extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
30
+
31
+ static void
32
+ threadentry(void *v)
33
+ {
34
+ ThreadStart ts;
35
+
36
+ ts = *(ThreadStart*)v;
37
+ free(v);
38
+
39
+ crosscall1(ts.fn, setg_gcc, (void *)ts.g);
40
+ }
platform/dbops/binaries/go/go/src/runtime/cgo/handle.go ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2021 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package cgo
6
+
7
+ import (
8
+ "sync"
9
+ "sync/atomic"
10
+ )
11
+
12
+ // Handle provides a way to pass values that contain Go pointers
13
+ // (pointers to memory allocated by Go) between Go and C without
14
+ // breaking the cgo pointer passing rules. A Handle is an integer
15
+ // value that can represent any Go value. A Handle can be passed
16
+ // through C and back to Go, and Go code can use the Handle to
17
+ // retrieve the original Go value.
18
+ //
19
+ // The underlying type of Handle is guaranteed to fit in an integer type
20
+ // that is large enough to hold the bit pattern of any pointer. The zero
21
+ // value of a Handle is not valid, and thus is safe to use as a sentinel
22
+ // in C APIs.
23
+ //
24
+ // For instance, on the Go side:
25
+ //
26
+ // package main
27
+ //
28
+ // /*
29
+ // #include <stdint.h> // for uintptr_t
30
+ //
31
+ // extern void MyGoPrint(uintptr_t handle);
32
+ // void myprint(uintptr_t handle);
33
+ // */
34
+ // import "C"
35
+ // import "runtime/cgo"
36
+ //
37
+ // //export MyGoPrint
38
+ // func MyGoPrint(handle C.uintptr_t) {
39
+ // h := cgo.Handle(handle)
40
+ // val := h.Value().(string)
41
+ // println(val)
42
+ // h.Delete()
43
+ // }
44
+ //
45
+ // func main() {
46
+ // val := "hello Go"
47
+ // C.myprint(C.uintptr_t(cgo.NewHandle(val)))
48
+ // // Output: hello Go
49
+ // }
50
+ //
51
+ // and on the C side:
52
+ //
53
+ // #include <stdint.h> // for uintptr_t
54
+ //
55
+ // // A Go function
56
+ // extern void MyGoPrint(uintptr_t handle);
57
+ //
58
+ // // A C function
59
+ // void myprint(uintptr_t handle) {
60
+ // MyGoPrint(handle);
61
+ // }
62
+ //
63
+ // Some C functions accept a void* argument that points to an arbitrary
64
+ // data value supplied by the caller. It is not safe to coerce a [cgo.Handle]
65
+ // (an integer) to a Go [unsafe.Pointer], but instead we can pass the address
66
+ // of the cgo.Handle to the void* parameter, as in this variant of the
67
+ // previous example:
68
+ //
69
+ // package main
70
+ //
71
+ // /*
72
+ // extern void MyGoPrint(void *context);
73
+ // static inline void myprint(void *context) {
74
+ // MyGoPrint(context);
75
+ // }
76
+ // */
77
+ // import "C"
78
+ // import (
79
+ // "runtime/cgo"
80
+ // "unsafe"
81
+ // )
82
+ //
83
+ // //export MyGoPrint
84
+ // func MyGoPrint(context unsafe.Pointer) {
85
+ // h := *(*cgo.Handle)(context)
86
+ // val := h.Value().(string)
87
+ // println(val)
88
+ // h.Delete()
89
+ // }
90
+ //
91
+ // func main() {
92
+ // val := "hello Go"
93
+ // h := cgo.NewHandle(val)
94
+ // C.myprint(unsafe.Pointer(&h))
95
+ // // Output: hello Go
96
+ // }
97
+ type Handle uintptr
98
+
99
+ // NewHandle returns a handle for a given value.
100
+ //
101
+ // The handle is valid until the program calls Delete on it. The handle
102
+ // uses resources, and this package assumes that C code may hold on to
103
+ // the handle, so a program must explicitly call Delete when the handle
104
+ // is no longer needed.
105
+ //
106
+ // The intended use is to pass the returned handle to C code, which
107
+ // passes it back to Go, which calls Value.
108
+ func NewHandle(v any) Handle {
109
+ h := handleIdx.Add(1)
110
+ if h == 0 {
111
+ panic("runtime/cgo: ran out of handle space")
112
+ }
113
+
114
+ handles.Store(h, v)
115
+ return Handle(h)
116
+ }
117
+
118
+ // Value returns the associated Go value for a valid handle.
119
+ //
120
+ // The method panics if the handle is invalid.
121
+ func (h Handle) Value() any {
122
+ v, ok := handles.Load(uintptr(h))
123
+ if !ok {
124
+ panic("runtime/cgo: misuse of an invalid Handle")
125
+ }
126
+ return v
127
+ }
128
+
129
+ // Delete invalidates a handle. This method should only be called once
130
+ // the program no longer needs to pass the handle to C and the C code
131
+ // no longer has a copy of the handle value.
132
+ //
133
+ // The method panics if the handle is invalid.
134
+ func (h Handle) Delete() {
135
+ _, ok := handles.LoadAndDelete(uintptr(h))
136
+ if !ok {
137
+ panic("runtime/cgo: misuse of an invalid Handle")
138
+ }
139
+ }
140
+
141
+ var (
142
+ handles = sync.Map{} // map[Handle]interface{}
143
+ handleIdx atomic.Uintptr
144
+ )
platform/dbops/binaries/go/go/src/runtime/cgo/handle_test.go ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2021 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package cgo
6
+
7
+ import (
8
+ "reflect"
9
+ "testing"
10
+ )
11
+
12
+ func TestHandle(t *testing.T) {
13
+ v := 42
14
+
15
+ tests := []struct {
16
+ v1 any
17
+ v2 any
18
+ }{
19
+ {v1: v, v2: v},
20
+ {v1: &v, v2: &v},
21
+ {v1: nil, v2: nil},
22
+ }
23
+
24
+ for _, tt := range tests {
25
+ h1 := NewHandle(tt.v1)
26
+ h2 := NewHandle(tt.v2)
27
+
28
+ if uintptr(h1) == 0 || uintptr(h2) == 0 {
29
+ t.Fatalf("NewHandle returns zero")
30
+ }
31
+
32
+ if uintptr(h1) == uintptr(h2) {
33
+ t.Fatalf("Duplicated Go values should have different handles, but got equal")
34
+ }
35
+
36
+ h1v := h1.Value()
37
+ h2v := h2.Value()
38
+ if !reflect.DeepEqual(h1v, h2v) || !reflect.DeepEqual(h1v, tt.v1) {
39
+ t.Fatalf("Value of a Handle got wrong, got %+v %+v, want %+v", h1v, h2v, tt.v1)
40
+ }
41
+
42
+ h1.Delete()
43
+ h2.Delete()
44
+ }
45
+
46
+ siz := 0
47
+ handles.Range(func(k, v any) bool {
48
+ siz++
49
+ return true
50
+ })
51
+ if siz != 0 {
52
+ t.Fatalf("handles are not cleared, got %d, want %d", siz, 0)
53
+ }
54
+ }
55
+
56
+ func TestInvalidHandle(t *testing.T) {
57
+ t.Run("zero", func(t *testing.T) {
58
+ h := Handle(0)
59
+
60
+ defer func() {
61
+ if r := recover(); r != nil {
62
+ return
63
+ }
64
+ t.Fatalf("Delete of zero handle did not trigger a panic")
65
+ }()
66
+
67
+ h.Delete()
68
+ })
69
+
70
+ t.Run("invalid", func(t *testing.T) {
71
+ h := NewHandle(42)
72
+
73
+ defer func() {
74
+ if r := recover(); r != nil {
75
+ h.Delete()
76
+ return
77
+ }
78
+ t.Fatalf("Invalid handle did not trigger a panic")
79
+ }()
80
+
81
+ Handle(h + 1).Delete()
82
+ })
83
+ }
84
+
85
+ func BenchmarkHandle(b *testing.B) {
86
+ b.Run("non-concurrent", func(b *testing.B) {
87
+ for i := 0; i < b.N; i++ {
88
+ h := NewHandle(i)
89
+ _ = h.Value()
90
+ h.Delete()
91
+ }
92
+ })
93
+ b.Run("concurrent", func(b *testing.B) {
94
+ b.RunParallel(func(pb *testing.PB) {
95
+ var v int
96
+ for pb.Next() {
97
+ h := NewHandle(v)
98
+ _ = h.Value()
99
+ h.Delete()
100
+ }
101
+ })
102
+ })
103
+ }
platform/dbops/binaries/go/go/src/runtime/cgo/iscgo.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2010 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // The runtime package contains an uninitialized definition
6
+ // for runtime路iscgo. Override it to tell the runtime we're here.
7
+ // There are various function pointers that should be set too,
8
+ // but those depend on dynamic linker magic to get initialized
9
+ // correctly, and sometimes they break. This variable is a
10
+ // backup: it depends only on old C style static linking rules.
11
+
12
+ package cgo
13
+
14
+ import _ "unsafe" // for go:linkname
15
+
16
+ //go:linkname _iscgo runtime.iscgo
17
+ var _iscgo bool = true
platform/dbops/binaries/go/go/src/runtime/cgo/libcgo.h ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #include <stdint.h>
6
+ #include <stdlib.h>
7
+ #include <stdio.h>
8
+
9
+ #undef nil
10
+ #define nil ((void*)0)
11
+ #define nelem(x) (sizeof(x)/sizeof((x)[0]))
12
+
13
+ typedef uint32_t uint32;
14
+ typedef uint64_t uint64;
15
+ typedef uintptr_t uintptr;
16
+
17
+ /*
18
+ * The beginning of the per-goroutine structure,
19
+ * as defined in ../pkg/runtime/runtime.h.
20
+ * Just enough to edit these two fields.
21
+ */
22
+ typedef struct G G;
23
+ struct G
24
+ {
25
+ uintptr stacklo;
26
+ uintptr stackhi;
27
+ };
28
+
29
+ /*
30
+ * Arguments to the _cgo_thread_start call.
31
+ * Also known to ../pkg/runtime/runtime.h.
32
+ */
33
+ typedef struct ThreadStart ThreadStart;
34
+ struct ThreadStart
35
+ {
36
+ G *g;
37
+ uintptr *tls;
38
+ void (*fn)(void);
39
+ };
40
+
41
+ /*
42
+ * Called by 5c/6c/8c world.
43
+ * Makes a local copy of the ThreadStart and
44
+ * calls _cgo_sys_thread_start(ts).
45
+ */
46
+ extern void (*_cgo_thread_start)(ThreadStart *ts);
47
+
48
+ /*
49
+ * Creates a new operating system thread without updating any Go state
50
+ * (OS dependent).
51
+ */
52
+ extern void (*_cgo_sys_thread_create)(void* (*func)(void*), void* arg);
53
+
54
+ /*
55
+ * Indicates whether a dummy pthread per-thread variable is allocated.
56
+ */
57
+ extern uintptr_t *_cgo_pthread_key_created;
58
+
59
+ /*
60
+ * Creates the new operating system thread (OS, arch dependent).
61
+ */
62
+ void _cgo_sys_thread_start(ThreadStart *ts);
63
+
64
+ /*
65
+ * Waits for the Go runtime to be initialized (OS dependent).
66
+ * If runtime.SetCgoTraceback is used to set a context function,
67
+ * calls the context function and returns the context value.
68
+ */
69
+ uintptr_t _cgo_wait_runtime_init_done(void);
70
+
71
+ /*
72
+ * Get the low and high boundaries of the stack.
73
+ */
74
+ void x_cgo_getstackbound(uintptr bounds[2]);
75
+
76
+ /*
77
+ * Prints error then calls abort. For linux and android.
78
+ */
79
+ void fatalf(const char* format, ...);
80
+
81
+ /*
82
+ * Registers the current mach thread port for EXC_BAD_ACCESS processing.
83
+ */
84
+ void darwin_arm_init_thread_exception_port(void);
85
+
86
+ /*
87
+ * Starts a mach message server processing EXC_BAD_ACCESS.
88
+ */
89
+ void darwin_arm_init_mach_exception_handler(void);
90
+
91
+ /*
92
+ * The cgo context function. See runtime.SetCgoTraceback.
93
+ */
94
+ struct context_arg {
95
+ uintptr_t Context;
96
+ };
97
+ extern void (*(_cgo_get_context_function(void)))(struct context_arg*);
98
+
99
+ /*
100
+ * The argument for the cgo traceback callback. See runtime.SetCgoTraceback.
101
+ */
102
+ struct cgoTracebackArg {
103
+ uintptr_t Context;
104
+ uintptr_t SigContext;
105
+ uintptr_t* Buf;
106
+ uintptr_t Max;
107
+ };
108
+
109
+ /*
110
+ * TSAN support. This is only useful when building with
111
+ * CGO_CFLAGS="-fsanitize=thread" CGO_LDFLAGS="-fsanitize=thread" go install
112
+ */
113
+ #undef CGO_TSAN
114
+ #if defined(__has_feature)
115
+ # if __has_feature(thread_sanitizer)
116
+ # define CGO_TSAN
117
+ # endif
118
+ #elif defined(__SANITIZE_THREAD__)
119
+ # define CGO_TSAN
120
+ #endif
121
+
122
+ #ifdef CGO_TSAN
123
+
124
+ // These must match the definitions in yesTsanProlog in cmd/cgo/out.go.
125
+ // In general we should call _cgo_tsan_acquire when we enter C code,
126
+ // and call _cgo_tsan_release when we return to Go code.
127
+ // This is only necessary when calling code that might be instrumented
128
+ // by TSAN, which mostly means system library calls that TSAN intercepts.
129
+ // See the comment in cmd/cgo/out.go for more details.
130
+
131
+ long long _cgo_sync __attribute__ ((common));
132
+
133
+ extern void __tsan_acquire(void*);
134
+ extern void __tsan_release(void*);
135
+
136
+ __attribute__ ((unused))
137
+ static void _cgo_tsan_acquire() {
138
+ __tsan_acquire(&_cgo_sync);
139
+ }
140
+
141
+ __attribute__ ((unused))
142
+ static void _cgo_tsan_release() {
143
+ __tsan_release(&_cgo_sync);
144
+ }
145
+
146
+ #else // !defined(CGO_TSAN)
147
+
148
+ #define _cgo_tsan_acquire()
149
+ #define _cgo_tsan_release()
150
+
151
+ #endif // !defined(CGO_TSAN)
platform/dbops/binaries/go/go/src/runtime/cgo/libcgo_unix.h ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ /*
6
+ * Initialize g->stacklo.
7
+ */
8
+ extern void _cgo_set_stacklo(G *, uintptr *);
9
+
10
+ /*
11
+ * Call pthread_create, retrying on EAGAIN.
12
+ */
13
+ extern int _cgo_try_pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*);
14
+
15
+ /*
16
+ * Same as _cgo_try_pthread_create, but passing on the pthread_create function.
17
+ * Only defined on OpenBSD.
18
+ */
19
+ extern int _cgo_openbsd_try_pthread_create(int (*)(pthread_t*, const pthread_attr_t*, void *(*pfn)(void*), void*),
20
+ pthread_t*, const pthread_attr_t*, void* (*)(void*), void* arg);
platform/dbops/binaries/go/go/src/runtime/cgo/libcgo_windows.h ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ // Copyright 2020 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Call _beginthread, aborting on failure.
6
+ void _cgo_beginthread(void (*func)(void*), void* arg);
platform/dbops/binaries/go/go/src/runtime/cgo/linux.go ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Linux system call wrappers that provide POSIX semantics through the
6
+ // corresponding cgo->libc (nptl) wrappers for various system calls.
7
+
8
+ //go:build linux
9
+
10
+ package cgo
11
+
12
+ import "unsafe"
13
+
14
+ // Each of the following entries is needed to ensure that the
15
+ // syscall.syscall_linux code can conditionally call these
16
+ // function pointers:
17
+ //
18
+ // 1. find the C-defined function start
19
+ // 2. force the local byte alias to be mapped to that location
20
+ // 3. map the Go pointer to the function to the syscall package
21
+
22
+ //go:cgo_import_static _cgo_libc_setegid
23
+ //go:linkname _cgo_libc_setegid _cgo_libc_setegid
24
+ //go:linkname cgo_libc_setegid syscall.cgo_libc_setegid
25
+ var _cgo_libc_setegid byte
26
+ var cgo_libc_setegid = unsafe.Pointer(&_cgo_libc_setegid)
27
+
28
+ //go:cgo_import_static _cgo_libc_seteuid
29
+ //go:linkname _cgo_libc_seteuid _cgo_libc_seteuid
30
+ //go:linkname cgo_libc_seteuid syscall.cgo_libc_seteuid
31
+ var _cgo_libc_seteuid byte
32
+ var cgo_libc_seteuid = unsafe.Pointer(&_cgo_libc_seteuid)
33
+
34
+ //go:cgo_import_static _cgo_libc_setregid
35
+ //go:linkname _cgo_libc_setregid _cgo_libc_setregid
36
+ //go:linkname cgo_libc_setregid syscall.cgo_libc_setregid
37
+ var _cgo_libc_setregid byte
38
+ var cgo_libc_setregid = unsafe.Pointer(&_cgo_libc_setregid)
39
+
40
+ //go:cgo_import_static _cgo_libc_setresgid
41
+ //go:linkname _cgo_libc_setresgid _cgo_libc_setresgid
42
+ //go:linkname cgo_libc_setresgid syscall.cgo_libc_setresgid
43
+ var _cgo_libc_setresgid byte
44
+ var cgo_libc_setresgid = unsafe.Pointer(&_cgo_libc_setresgid)
45
+
46
+ //go:cgo_import_static _cgo_libc_setresuid
47
+ //go:linkname _cgo_libc_setresuid _cgo_libc_setresuid
48
+ //go:linkname cgo_libc_setresuid syscall.cgo_libc_setresuid
49
+ var _cgo_libc_setresuid byte
50
+ var cgo_libc_setresuid = unsafe.Pointer(&_cgo_libc_setresuid)
51
+
52
+ //go:cgo_import_static _cgo_libc_setreuid
53
+ //go:linkname _cgo_libc_setreuid _cgo_libc_setreuid
54
+ //go:linkname cgo_libc_setreuid syscall.cgo_libc_setreuid
55
+ var _cgo_libc_setreuid byte
56
+ var cgo_libc_setreuid = unsafe.Pointer(&_cgo_libc_setreuid)
57
+
58
+ //go:cgo_import_static _cgo_libc_setgroups
59
+ //go:linkname _cgo_libc_setgroups _cgo_libc_setgroups
60
+ //go:linkname cgo_libc_setgroups syscall.cgo_libc_setgroups
61
+ var _cgo_libc_setgroups byte
62
+ var cgo_libc_setgroups = unsafe.Pointer(&_cgo_libc_setgroups)
63
+
64
+ //go:cgo_import_static _cgo_libc_setgid
65
+ //go:linkname _cgo_libc_setgid _cgo_libc_setgid
66
+ //go:linkname cgo_libc_setgid syscall.cgo_libc_setgid
67
+ var _cgo_libc_setgid byte
68
+ var cgo_libc_setgid = unsafe.Pointer(&_cgo_libc_setgid)
69
+
70
+ //go:cgo_import_static _cgo_libc_setuid
71
+ //go:linkname _cgo_libc_setuid _cgo_libc_setuid
72
+ //go:linkname cgo_libc_setuid syscall.cgo_libc_setuid
73
+ var _cgo_libc_setuid byte
74
+ var cgo_libc_setuid = unsafe.Pointer(&_cgo_libc_setuid)
platform/dbops/binaries/go/go/src/runtime/cgo/linux_syscall.c ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build linux
6
+
7
+ #ifndef _GNU_SOURCE // setres[ug]id() API.
8
+ #define _GNU_SOURCE
9
+ #endif
10
+
11
+ #include <grp.h>
12
+ #include <sys/types.h>
13
+ #include <unistd.h>
14
+ #include <errno.h>
15
+ #include "libcgo.h"
16
+
17
+ /*
18
+ * Assumed POSIX compliant libc system call wrappers. For linux, the
19
+ * glibc/nptl/setxid mechanism ensures that POSIX semantics are
20
+ * honored for all pthreads (by default), and this in turn with cgo
21
+ * ensures that all Go threads launched with cgo are kept in sync for
22
+ * these function calls.
23
+ */
24
+
25
+ // argset_t matches runtime/cgocall.go:argset.
26
+ typedef struct {
27
+ uintptr_t* args;
28
+ uintptr_t retval;
29
+ } argset_t;
30
+
31
+ // libc backed posix-compliant syscalls.
32
+
33
+ #define SET_RETVAL(fn) \
34
+ uintptr_t ret = (uintptr_t) fn ; \
35
+ if (ret == (uintptr_t) -1) { \
36
+ x->retval = (uintptr_t) errno; \
37
+ } else \
38
+ x->retval = ret
39
+
40
+ void
41
+ _cgo_libc_setegid(argset_t* x) {
42
+ SET_RETVAL(setegid((gid_t) x->args[0]));
43
+ }
44
+
45
+ void
46
+ _cgo_libc_seteuid(argset_t* x) {
47
+ SET_RETVAL(seteuid((uid_t) x->args[0]));
48
+ }
49
+
50
+ void
51
+ _cgo_libc_setgid(argset_t* x) {
52
+ SET_RETVAL(setgid((gid_t) x->args[0]));
53
+ }
54
+
55
+ void
56
+ _cgo_libc_setgroups(argset_t* x) {
57
+ SET_RETVAL(setgroups((size_t) x->args[0], (const gid_t *) x->args[1]));
58
+ }
59
+
60
+ void
61
+ _cgo_libc_setregid(argset_t* x) {
62
+ SET_RETVAL(setregid((gid_t) x->args[0], (gid_t) x->args[1]));
63
+ }
64
+
65
+ void
66
+ _cgo_libc_setresgid(argset_t* x) {
67
+ SET_RETVAL(setresgid((gid_t) x->args[0], (gid_t) x->args[1],
68
+ (gid_t) x->args[2]));
69
+ }
70
+
71
+ void
72
+ _cgo_libc_setresuid(argset_t* x) {
73
+ SET_RETVAL(setresuid((uid_t) x->args[0], (uid_t) x->args[1],
74
+ (uid_t) x->args[2]));
75
+ }
76
+
77
+ void
78
+ _cgo_libc_setreuid(argset_t* x) {
79
+ SET_RETVAL(setreuid((uid_t) x->args[0], (uid_t) x->args[1]));
80
+ }
81
+
82
+ void
83
+ _cgo_libc_setuid(argset_t* x) {
84
+ SET_RETVAL(setuid((uid_t) x->args[0]));
85
+ }
platform/dbops/binaries/go/go/src/runtime/cgo/mmap.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build (linux && (amd64 || arm64 || loong64)) || (freebsd && amd64)
6
+
7
+ package cgo
8
+
9
+ // Import "unsafe" because we use go:linkname.
10
+ import _ "unsafe"
11
+
12
+ // When using cgo, call the C library for mmap, so that we call into
13
+ // any sanitizer interceptors. This supports using the memory
14
+ // sanitizer with Go programs. The memory sanitizer only applies to
15
+ // C/C++ code; this permits that code to see the Go code as normal
16
+ // program addresses that have been initialized.
17
+
18
+ // To support interceptors that look for both mmap and munmap,
19
+ // also call the C library for munmap.
20
+
21
+ //go:cgo_import_static x_cgo_mmap
22
+ //go:linkname x_cgo_mmap x_cgo_mmap
23
+ //go:linkname _cgo_mmap _cgo_mmap
24
+ var x_cgo_mmap byte
25
+ var _cgo_mmap = &x_cgo_mmap
26
+
27
+ //go:cgo_import_static x_cgo_munmap
28
+ //go:linkname x_cgo_munmap x_cgo_munmap
29
+ //go:linkname _cgo_munmap _cgo_munmap
30
+ var x_cgo_munmap byte
31
+ var _cgo_munmap = &x_cgo_munmap
platform/dbops/binaries/go/go/src/runtime/cgo/netbsd.go ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2010 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build netbsd
6
+
7
+ package cgo
8
+
9
+ import _ "unsafe" // for go:linkname
10
+
11
+ // Supply environ and __progname, because we don't
12
+ // link against the standard NetBSD crt0.o and the
13
+ // libc dynamic library needs them.
14
+
15
+ //go:linkname _environ environ
16
+ //go:linkname _progname __progname
17
+ //go:linkname ___ps_strings __ps_strings
18
+
19
+ var _environ uintptr
20
+ var _progname uintptr
21
+ var ___ps_strings uintptr
platform/dbops/binaries/go/go/src/runtime/cgo/openbsd.go ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2010 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build openbsd
6
+
7
+ package cgo
8
+
9
+ import _ "unsafe" // for go:linkname
10
+
11
+ // Supply __guard_local because we don't link against the standard
12
+ // OpenBSD crt0.o and the libc dynamic library needs it.
13
+
14
+ //go:linkname _guard_local __guard_local
15
+
16
+ var _guard_local uintptr
17
+
18
+ // This is normally marked as hidden and placed in the
19
+ // .openbsd.randomdata section.
20
+ //
21
+ //go:cgo_export_dynamic __guard_local __guard_local
platform/dbops/binaries/go/go/src/runtime/cgo/setenv.go ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2011 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build unix
6
+
7
+ package cgo
8
+
9
+ import _ "unsafe" // for go:linkname
10
+
11
+ //go:cgo_import_static x_cgo_setenv
12
+ //go:linkname x_cgo_setenv x_cgo_setenv
13
+ //go:linkname _cgo_setenv runtime._cgo_setenv
14
+ var x_cgo_setenv byte
15
+ var _cgo_setenv = &x_cgo_setenv
16
+
17
+ //go:cgo_import_static x_cgo_unsetenv
18
+ //go:linkname x_cgo_unsetenv x_cgo_unsetenv
19
+ //go:linkname _cgo_unsetenv runtime._cgo_unsetenv
20
+ var x_cgo_unsetenv byte
21
+ var _cgo_unsetenv = &x_cgo_unsetenv
platform/dbops/binaries/go/go/src/runtime/cgo/sigaction.go ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build (linux && amd64) || (freebsd && amd64) || (linux && arm64) || (linux && ppc64le)
6
+
7
+ package cgo
8
+
9
+ // Import "unsafe" because we use go:linkname.
10
+ import _ "unsafe"
11
+
12
+ // When using cgo, call the C library for sigaction, so that we call into
13
+ // any sanitizer interceptors. This supports using the sanitizers
14
+ // with Go programs. The thread and memory sanitizers only apply to
15
+ // C/C++ code; this permits that code to see the Go runtime's existing signal
16
+ // handlers when registering new signal handlers for the process.
17
+
18
+ //go:cgo_import_static x_cgo_sigaction
19
+ //go:linkname x_cgo_sigaction x_cgo_sigaction
20
+ //go:linkname _cgo_sigaction _cgo_sigaction
21
+ var x_cgo_sigaction byte
22
+ var _cgo_sigaction = &x_cgo_sigaction
platform/dbops/binaries/go/go/src/runtime/cgo/signal_ios_arm64.go ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package cgo
6
+
7
+ import _ "unsafe"
8
+
9
+ //go:cgo_export_static xx_cgo_panicmem xx_cgo_panicmem
10
+ func xx_cgo_panicmem()
platform/dbops/binaries/go/go/src/runtime/cgo/signal_ios_arm64.s ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #include "textflag.h"
6
+
7
+ // xx_cgo_panicmem is the entrypoint for SIGSEGV as intercepted via a
8
+ // mach thread port as EXC_BAD_ACCESS. As the segfault may have happened
9
+ // in C code, we first need to load_g then call xx_cgo_panicmem.
10
+ //
11
+ // R1 - LR at moment of fault
12
+ // R2 - PC at moment of fault
13
+ TEXT xx_cgo_panicmem(SB),NOSPLIT|NOFRAME,$0
14
+ // If in external C code, we need to load the g register.
15
+ BL runtime路load_g(SB)
16
+ CMP $0, g
17
+ BNE ongothread
18
+
19
+ // On a foreign thread.
20
+ // TODO(crawshaw): call badsignal
21
+ MOVD.W $0, -16(RSP)
22
+ MOVW $139, R1
23
+ MOVW R1, 8(RSP)
24
+ B runtime路exit(SB)
25
+
26
+ ongothread:
27
+ // Trigger a SIGSEGV panic.
28
+ //
29
+ // The goal is to arrange the stack so it looks like the runtime
30
+ // function sigpanic was called from the PC that faulted. It has
31
+ // to be sigpanic, as the stack unwinding code in traceback.go
32
+ // looks explicitly for it.
33
+ //
34
+ // To do this we call into runtime路setsigsegv, which sets the
35
+ // appropriate state inside the g object. We give it the faulting
36
+ // PC on the stack, then put it in the LR before calling sigpanic.
37
+
38
+ // Build a 32-byte stack frame for us for this call.
39
+ // Saved LR (none available) is at the bottom,
40
+ // then the PC argument for setsigsegv,
41
+ // then a copy of the LR for us to restore.
42
+ MOVD.W $0, -32(RSP)
43
+ MOVD R1, 8(RSP)
44
+ MOVD R2, 16(RSP)
45
+ BL runtime路setsigsegv(SB)
46
+ MOVD 8(RSP), R1
47
+ MOVD 16(RSP), R2
48
+
49
+ // Build a 16-byte stack frame for the simulated
50
+ // call to sigpanic, by taking 16 bytes away from the
51
+ // 32-byte stack frame above.
52
+ // The saved LR in this frame is the LR at time of fault,
53
+ // and the LR on entry to sigpanic is the PC at time of fault.
54
+ MOVD.W R1, 16(RSP)
55
+ MOVD R2, R30
56
+ B runtime路sigpanic(SB)
platform/dbops/binaries/go/go/src/runtime/coverage/apis.go ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import (
8
+ "fmt"
9
+ "internal/coverage"
10
+ "io"
11
+ "sync/atomic"
12
+ "unsafe"
13
+ )
14
+
15
+ // WriteMetaDir writes a coverage meta-data file for the currently
16
+ // running program to the directory specified in 'dir'. An error will
17
+ // be returned if the operation can't be completed successfully (for
18
+ // example, if the currently running program was not built with
19
+ // "-cover", or if the directory does not exist).
20
+ func WriteMetaDir(dir string) error {
21
+ if !finalHashComputed {
22
+ return fmt.Errorf("error: no meta-data available (binary not built with -cover?)")
23
+ }
24
+ return emitMetaDataToDirectory(dir, getCovMetaList())
25
+ }
26
+
27
+ // WriteMeta writes the meta-data content (the payload that would
28
+ // normally be emitted to a meta-data file) for the currently running
29
+ // program to the writer 'w'. An error will be returned if the
30
+ // operation can't be completed successfully (for example, if the
31
+ // currently running program was not built with "-cover", or if a
32
+ // write fails).
33
+ func WriteMeta(w io.Writer) error {
34
+ if w == nil {
35
+ return fmt.Errorf("error: nil writer in WriteMeta")
36
+ }
37
+ if !finalHashComputed {
38
+ return fmt.Errorf("error: no meta-data available (binary not built with -cover?)")
39
+ }
40
+ ml := getCovMetaList()
41
+ return writeMetaData(w, ml, cmode, cgran, finalHash)
42
+ }
43
+
44
+ // WriteCountersDir writes a coverage counter-data file for the
45
+ // currently running program to the directory specified in 'dir'. An
46
+ // error will be returned if the operation can't be completed
47
+ // successfully (for example, if the currently running program was not
48
+ // built with "-cover", or if the directory does not exist). The
49
+ // counter data written will be a snapshot taken at the point of the
50
+ // call.
51
+ func WriteCountersDir(dir string) error {
52
+ if cmode != coverage.CtrModeAtomic {
53
+ return fmt.Errorf("WriteCountersDir invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String())
54
+ }
55
+ return emitCounterDataToDirectory(dir)
56
+ }
57
+
58
+ // WriteCounters writes coverage counter-data content for the
59
+ // currently running program to the writer 'w'. An error will be
60
+ // returned if the operation can't be completed successfully (for
61
+ // example, if the currently running program was not built with
62
+ // "-cover", or if a write fails). The counter data written will be a
63
+ // snapshot taken at the point of the invocation.
64
+ func WriteCounters(w io.Writer) error {
65
+ if w == nil {
66
+ return fmt.Errorf("error: nil writer in WriteCounters")
67
+ }
68
+ if cmode != coverage.CtrModeAtomic {
69
+ return fmt.Errorf("WriteCounters invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String())
70
+ }
71
+ // Ask the runtime for the list of coverage counter symbols.
72
+ cl := getCovCounterList()
73
+ if len(cl) == 0 {
74
+ return fmt.Errorf("program not built with -cover")
75
+ }
76
+ if !finalHashComputed {
77
+ return fmt.Errorf("meta-data not written yet, unable to write counter data")
78
+ }
79
+
80
+ pm := getCovPkgMap()
81
+ s := &emitState{
82
+ counterlist: cl,
83
+ pkgmap: pm,
84
+ }
85
+ return s.emitCounterDataToWriter(w)
86
+ }
87
+
88
+ // ClearCounters clears/resets all coverage counter variables in the
89
+ // currently running program. It returns an error if the program in
90
+ // question was not built with the "-cover" flag. Clearing of coverage
91
+ // counters is also not supported for programs not using atomic
92
+ // counter mode (see more detailed comments below for the rationale
93
+ // here).
94
+ func ClearCounters() error {
95
+ cl := getCovCounterList()
96
+ if len(cl) == 0 {
97
+ return fmt.Errorf("program not built with -cover")
98
+ }
99
+ if cmode != coverage.CtrModeAtomic {
100
+ return fmt.Errorf("ClearCounters invoked for program built with -covermode=%s (please use -covermode=atomic)", cmode.String())
101
+ }
102
+
103
+ // Implementation note: this function would be faster and simpler
104
+ // if we could just zero out the entire counter array, but for the
105
+ // moment we go through and zero out just the slots in the array
106
+ // corresponding to the counter values. We do this to avoid the
107
+ // following bad scenario: suppose that a user builds their Go
108
+ // program with "-cover", and that program has a function (call it
109
+ // main.XYZ) that invokes ClearCounters:
110
+ //
111
+ // func XYZ() {
112
+ // ... do some stuff ...
113
+ // coverage.ClearCounters()
114
+ // if someCondition { <<--- HERE
115
+ // ...
116
+ // }
117
+ // }
118
+ //
119
+ // At the point where ClearCounters executes, main.XYZ has not yet
120
+ // finished running, thus as soon as the call returns the line
121
+ // marked "HERE" above will trigger the writing of a non-zero
122
+ // value into main.XYZ's counter slab. However since we've just
123
+ // finished clearing the entire counter segment, we will have lost
124
+ // the values in the prolog portion of main.XYZ's counter slab
125
+ // (nctrs, pkgid, funcid). This means that later on at the end of
126
+ // program execution as we walk through the entire counter array
127
+ // for the program looking for executed functions, we'll zoom past
128
+ // main.XYZ's prolog (which was zero'd) and hit the non-zero
129
+ // counter value corresponding to the "HERE" block, which will
130
+ // then be interpreted as the start of another live function.
131
+ // Things will go downhill from there.
132
+ //
133
+ // This same scenario is also a potential risk if the program is
134
+ // running on an architecture that permits reordering of
135
+ // writes/stores, since the inconsistency described above could
136
+ // arise here. Example scenario:
137
+ //
138
+ // func ABC() {
139
+ // ... // prolog
140
+ // if alwaysTrue() {
141
+ // XYZ() // counter update here
142
+ // }
143
+ // }
144
+ //
145
+ // In the instrumented version of ABC, the prolog of the function
146
+ // will contain a series of stores to the initial portion of the
147
+ // counter array to write number-of-counters, pkgid, funcid. Later
148
+ // in the function there is also a store to increment a counter
149
+ // for the block containing the call to XYZ(). If the CPU is
150
+ // allowed to reorder stores and decides to issue the XYZ store
151
+ // before the prolog stores, this could be observable as an
152
+ // inconsistency similar to the one above. Hence the requirement
153
+ // for atomic counter mode: according to package atomic docs,
154
+ // "...operations that happen in a specific order on one thread,
155
+ // will always be observed to happen in exactly that order by
156
+ // another thread". Thus we can be sure that there will be no
157
+ // inconsistency when reading the counter array from the thread
158
+ // running ClearCounters.
159
+
160
+ for _, c := range cl {
161
+ sd := unsafe.Slice((*atomic.Uint32)(unsafe.Pointer(c.Counters)), int(c.Len))
162
+ for i := 0; i < len(sd); i++ {
163
+ // Skip ahead until the next non-zero value.
164
+ sdi := sd[i].Load()
165
+ if sdi == 0 {
166
+ continue
167
+ }
168
+ // We found a function that was executed; clear its counters.
169
+ nCtrs := sdi
170
+ for j := 0; j < int(nCtrs); j++ {
171
+ sd[i+coverage.FirstCtrOffset+j].Store(0)
172
+ }
173
+ // Move to next function.
174
+ i += coverage.FirstCtrOffset + int(nCtrs) - 1
175
+ }
176
+ }
177
+ return nil
178
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/dummy.s ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // The runtime package uses //go:linkname to push a few functions into this
6
+ // package but we still need a .s file so the Go tool does not pass -complete
7
+ // to 'go tool compile' so the latter does not complain about Go functions
8
+ // with no bodies.
platform/dbops/binaries/go/go/src/runtime/coverage/emit.go ADDED
@@ -0,0 +1,609 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import (
8
+ "crypto/md5"
9
+ "fmt"
10
+ "internal/coverage"
11
+ "internal/coverage/encodecounter"
12
+ "internal/coverage/encodemeta"
13
+ "internal/coverage/rtcov"
14
+ "io"
15
+ "os"
16
+ "path/filepath"
17
+ "runtime"
18
+ "strconv"
19
+ "sync/atomic"
20
+ "time"
21
+ "unsafe"
22
+ )
23
+
24
+ // This file contains functions that support the writing of data files
25
+ // emitted at the end of code coverage testing runs, from instrumented
26
+ // executables.
27
+
28
+ // getCovMetaList returns a list of meta-data blobs registered
29
+ // for the currently executing instrumented program. It is defined in the
30
+ // runtime.
31
+ func getCovMetaList() []rtcov.CovMetaBlob
32
+
33
+ // getCovCounterList returns a list of counter-data blobs registered
34
+ // for the currently executing instrumented program. It is defined in the
35
+ // runtime.
36
+ func getCovCounterList() []rtcov.CovCounterBlob
37
+
38
+ // getCovPkgMap returns a map storing the remapped package IDs for
39
+ // hard-coded runtime packages (see internal/coverage/pkgid.go for
40
+ // more on why hard-coded package IDs are needed). This function
41
+ // is defined in the runtime.
42
+ func getCovPkgMap() map[int]int
43
+
44
+ // emitState holds useful state information during the emit process.
45
+ //
46
+ // When an instrumented program finishes execution and starts the
47
+ // process of writing out coverage data, it's possible that an
48
+ // existing meta-data file already exists in the output directory. In
49
+ // this case openOutputFiles() below will leave the 'mf' field below
50
+ // as nil. If a new meta-data file is needed, field 'mfname' will be
51
+ // the final desired path of the meta file, 'mftmp' will be a
52
+ // temporary file, and 'mf' will be an open os.File pointer for
53
+ // 'mftmp'. The meta-data file payload will be written to 'mf', the
54
+ // temp file will be then closed and renamed (from 'mftmp' to
55
+ // 'mfname'), so as to insure that the meta-data file is created
56
+ // atomically; we want this so that things work smoothly in cases
57
+ // where there are several instances of a given instrumented program
58
+ // all terminating at the same time and trying to create meta-data
59
+ // files simultaneously.
60
+ //
61
+ // For counter data files there is less chance of a collision, hence
62
+ // the openOutputFiles() stores the counter data file in 'cfname' and
63
+ // then places the *io.File into 'cf'.
64
+ type emitState struct {
65
+ mfname string // path of final meta-data output file
66
+ mftmp string // path to meta-data temp file (if needed)
67
+ mf *os.File // open os.File for meta-data temp file
68
+ cfname string // path of final counter data file
69
+ cftmp string // path to counter data temp file
70
+ cf *os.File // open os.File for counter data file
71
+ outdir string // output directory
72
+
73
+ // List of meta-data symbols obtained from the runtime
74
+ metalist []rtcov.CovMetaBlob
75
+
76
+ // List of counter-data symbols obtained from the runtime
77
+ counterlist []rtcov.CovCounterBlob
78
+
79
+ // Table to use for remapping hard-coded pkg ids.
80
+ pkgmap map[int]int
81
+
82
+ // emit debug trace output
83
+ debug bool
84
+ }
85
+
86
+ var (
87
+ // finalHash is computed at init time from the list of meta-data
88
+ // symbols registered during init. It is used both for writing the
89
+ // meta-data file and counter-data files.
90
+ finalHash [16]byte
91
+ // Set to true when we've computed finalHash + finalMetaLen.
92
+ finalHashComputed bool
93
+ // Total meta-data length.
94
+ finalMetaLen uint64
95
+ // Records whether we've already attempted to write meta-data.
96
+ metaDataEmitAttempted bool
97
+ // Counter mode for this instrumented program run.
98
+ cmode coverage.CounterMode
99
+ // Counter granularity for this instrumented program run.
100
+ cgran coverage.CounterGranularity
101
+ // Cached value of GOCOVERDIR environment variable.
102
+ goCoverDir string
103
+ // Copy of os.Args made at init time, converted into map format.
104
+ capturedOsArgs map[string]string
105
+ // Flag used in tests to signal that coverage data already written.
106
+ covProfileAlreadyEmitted bool
107
+ )
108
+
109
+ // fileType is used to select between counter-data files and
110
+ // meta-data files.
111
+ type fileType int
112
+
113
+ const (
114
+ noFile = 1 << iota
115
+ metaDataFile
116
+ counterDataFile
117
+ )
118
+
119
+ // emitMetaData emits the meta-data output file for this coverage run.
120
+ // This entry point is intended to be invoked by the compiler from
121
+ // an instrumented program's main package init func.
122
+ func emitMetaData() {
123
+ if covProfileAlreadyEmitted {
124
+ return
125
+ }
126
+ ml, err := prepareForMetaEmit()
127
+ if err != nil {
128
+ fmt.Fprintf(os.Stderr, "error: coverage meta-data prep failed: %v\n", err)
129
+ if os.Getenv("GOCOVERDEBUG") != "" {
130
+ panic("meta-data write failure")
131
+ }
132
+ }
133
+ if len(ml) == 0 {
134
+ fmt.Fprintf(os.Stderr, "program not built with -cover\n")
135
+ return
136
+ }
137
+
138
+ goCoverDir = os.Getenv("GOCOVERDIR")
139
+ if goCoverDir == "" {
140
+ fmt.Fprintf(os.Stderr, "warning: GOCOVERDIR not set, no coverage data emitted\n")
141
+ return
142
+ }
143
+
144
+ if err := emitMetaDataToDirectory(goCoverDir, ml); err != nil {
145
+ fmt.Fprintf(os.Stderr, "error: coverage meta-data emit failed: %v\n", err)
146
+ if os.Getenv("GOCOVERDEBUG") != "" {
147
+ panic("meta-data write failure")
148
+ }
149
+ }
150
+ }
151
+
152
+ func modeClash(m coverage.CounterMode) bool {
153
+ if m == coverage.CtrModeRegOnly || m == coverage.CtrModeTestMain {
154
+ return false
155
+ }
156
+ if cmode == coverage.CtrModeInvalid {
157
+ cmode = m
158
+ return false
159
+ }
160
+ return cmode != m
161
+ }
162
+
163
+ func granClash(g coverage.CounterGranularity) bool {
164
+ if cgran == coverage.CtrGranularityInvalid {
165
+ cgran = g
166
+ return false
167
+ }
168
+ return cgran != g
169
+ }
170
+
171
+ // prepareForMetaEmit performs preparatory steps needed prior to
172
+ // emitting a meta-data file, notably computing a final hash of
173
+ // all meta-data blobs and capturing os args.
174
+ func prepareForMetaEmit() ([]rtcov.CovMetaBlob, error) {
175
+ // Ask the runtime for the list of coverage meta-data symbols.
176
+ ml := getCovMetaList()
177
+
178
+ // In the normal case (go build -o prog.exe ... ; ./prog.exe)
179
+ // len(ml) will always be non-zero, but we check here since at
180
+ // some point this function will be reachable via user-callable
181
+ // APIs (for example, to write out coverage data from a server
182
+ // program that doesn't ever call os.Exit).
183
+ if len(ml) == 0 {
184
+ return nil, nil
185
+ }
186
+
187
+ s := &emitState{
188
+ metalist: ml,
189
+ debug: os.Getenv("GOCOVERDEBUG") != "",
190
+ }
191
+
192
+ // Capture os.Args() now so as to avoid issues if args
193
+ // are rewritten during program execution.
194
+ capturedOsArgs = captureOsArgs()
195
+
196
+ if s.debug {
197
+ fmt.Fprintf(os.Stderr, "=+= GOCOVERDIR is %s\n", os.Getenv("GOCOVERDIR"))
198
+ fmt.Fprintf(os.Stderr, "=+= contents of covmetalist:\n")
199
+ for k, b := range ml {
200
+ fmt.Fprintf(os.Stderr, "=+= slot: %d path: %s ", k, b.PkgPath)
201
+ if b.PkgID != -1 {
202
+ fmt.Fprintf(os.Stderr, " hcid: %d", b.PkgID)
203
+ }
204
+ fmt.Fprintf(os.Stderr, "\n")
205
+ }
206
+ pm := getCovPkgMap()
207
+ fmt.Fprintf(os.Stderr, "=+= remap table:\n")
208
+ for from, to := range pm {
209
+ fmt.Fprintf(os.Stderr, "=+= from %d to %d\n",
210
+ uint32(from), uint32(to))
211
+ }
212
+ }
213
+
214
+ h := md5.New()
215
+ tlen := uint64(unsafe.Sizeof(coverage.MetaFileHeader{}))
216
+ for _, entry := range ml {
217
+ if _, err := h.Write(entry.Hash[:]); err != nil {
218
+ return nil, err
219
+ }
220
+ tlen += uint64(entry.Len)
221
+ ecm := coverage.CounterMode(entry.CounterMode)
222
+ if modeClash(ecm) {
223
+ return nil, fmt.Errorf("coverage counter mode clash: package %s uses mode=%d, but package %s uses mode=%s\n", ml[0].PkgPath, cmode, entry.PkgPath, ecm)
224
+ }
225
+ ecg := coverage.CounterGranularity(entry.CounterGranularity)
226
+ if granClash(ecg) {
227
+ return nil, fmt.Errorf("coverage counter granularity clash: package %s uses gran=%d, but package %s uses gran=%s\n", ml[0].PkgPath, cgran, entry.PkgPath, ecg)
228
+ }
229
+ }
230
+
231
+ // Hash mode and granularity as well.
232
+ h.Write([]byte(cmode.String()))
233
+ h.Write([]byte(cgran.String()))
234
+
235
+ // Compute final digest.
236
+ fh := h.Sum(nil)
237
+ copy(finalHash[:], fh)
238
+ finalHashComputed = true
239
+ finalMetaLen = tlen
240
+
241
+ return ml, nil
242
+ }
243
+
244
+ // emitMetaDataToDirectory emits the meta-data output file to the specified
245
+ // directory, returning an error if something went wrong.
246
+ func emitMetaDataToDirectory(outdir string, ml []rtcov.CovMetaBlob) error {
247
+ ml, err := prepareForMetaEmit()
248
+ if err != nil {
249
+ return err
250
+ }
251
+ if len(ml) == 0 {
252
+ return nil
253
+ }
254
+
255
+ metaDataEmitAttempted = true
256
+
257
+ s := &emitState{
258
+ metalist: ml,
259
+ debug: os.Getenv("GOCOVERDEBUG") != "",
260
+ outdir: outdir,
261
+ }
262
+
263
+ // Open output files.
264
+ if err := s.openOutputFiles(finalHash, finalMetaLen, metaDataFile); err != nil {
265
+ return err
266
+ }
267
+
268
+ // Emit meta-data file only if needed (may already be present).
269
+ if s.needMetaDataFile() {
270
+ if err := s.emitMetaDataFile(finalHash, finalMetaLen); err != nil {
271
+ return err
272
+ }
273
+ }
274
+ return nil
275
+ }
276
+
277
+ // emitCounterData emits the counter data output file for this coverage run.
278
+ // This entry point is intended to be invoked by the runtime when an
279
+ // instrumented program is terminating or calling os.Exit().
280
+ func emitCounterData() {
281
+ if goCoverDir == "" || !finalHashComputed || covProfileAlreadyEmitted {
282
+ return
283
+ }
284
+ if err := emitCounterDataToDirectory(goCoverDir); err != nil {
285
+ fmt.Fprintf(os.Stderr, "error: coverage counter data emit failed: %v\n", err)
286
+ if os.Getenv("GOCOVERDEBUG") != "" {
287
+ panic("counter-data write failure")
288
+ }
289
+ }
290
+ }
291
+
292
+ // emitCounterDataToDirectory emits the counter-data output file for this coverage run.
293
+ func emitCounterDataToDirectory(outdir string) error {
294
+ // Ask the runtime for the list of coverage counter symbols.
295
+ cl := getCovCounterList()
296
+ if len(cl) == 0 {
297
+ // no work to do here.
298
+ return nil
299
+ }
300
+
301
+ if !finalHashComputed {
302
+ return fmt.Errorf("error: meta-data not available (binary not built with -cover?)")
303
+ }
304
+
305
+ // Ask the runtime for the list of coverage counter symbols.
306
+ pm := getCovPkgMap()
307
+ s := &emitState{
308
+ counterlist: cl,
309
+ pkgmap: pm,
310
+ outdir: outdir,
311
+ debug: os.Getenv("GOCOVERDEBUG") != "",
312
+ }
313
+
314
+ // Open output file.
315
+ if err := s.openOutputFiles(finalHash, finalMetaLen, counterDataFile); err != nil {
316
+ return err
317
+ }
318
+ if s.cf == nil {
319
+ return fmt.Errorf("counter data output file open failed (no additional info")
320
+ }
321
+
322
+ // Emit counter data file.
323
+ if err := s.emitCounterDataFile(finalHash, s.cf); err != nil {
324
+ return err
325
+ }
326
+ if err := s.cf.Close(); err != nil {
327
+ return fmt.Errorf("closing counter data file: %v", err)
328
+ }
329
+
330
+ // Counter file has now been closed. Rename the temp to the
331
+ // final desired path.
332
+ if err := os.Rename(s.cftmp, s.cfname); err != nil {
333
+ return fmt.Errorf("writing %s: rename from %s failed: %v\n", s.cfname, s.cftmp, err)
334
+ }
335
+
336
+ return nil
337
+ }
338
+
339
+ // emitCounterDataToWriter emits counter data for this coverage run to an io.Writer.
340
+ func (s *emitState) emitCounterDataToWriter(w io.Writer) error {
341
+ if err := s.emitCounterDataFile(finalHash, w); err != nil {
342
+ return err
343
+ }
344
+ return nil
345
+ }
346
+
347
+ // openMetaFile determines whether we need to emit a meta-data output
348
+ // file, or whether we can reuse the existing file in the coverage out
349
+ // dir. It updates mfname/mftmp/mf fields in 's', returning an error
350
+ // if something went wrong. See the comment on the emitState type
351
+ // definition above for more on how file opening is managed.
352
+ func (s *emitState) openMetaFile(metaHash [16]byte, metaLen uint64) error {
353
+
354
+ // Open meta-outfile for reading to see if it exists.
355
+ fn := fmt.Sprintf("%s.%x", coverage.MetaFilePref, metaHash)
356
+ s.mfname = filepath.Join(s.outdir, fn)
357
+ fi, err := os.Stat(s.mfname)
358
+ if err != nil || fi.Size() != int64(metaLen) {
359
+ // We need a new meta-file.
360
+ tname := "tmp." + fn + strconv.FormatInt(time.Now().UnixNano(), 10)
361
+ s.mftmp = filepath.Join(s.outdir, tname)
362
+ s.mf, err = os.Create(s.mftmp)
363
+ if err != nil {
364
+ return fmt.Errorf("creating meta-data file %s: %v", s.mftmp, err)
365
+ }
366
+ }
367
+ return nil
368
+ }
369
+
370
+ // openCounterFile opens an output file for the counter data portion
371
+ // of a test coverage run. If updates the 'cfname' and 'cf' fields in
372
+ // 's', returning an error if something went wrong.
373
+ func (s *emitState) openCounterFile(metaHash [16]byte) error {
374
+ processID := os.Getpid()
375
+ fn := fmt.Sprintf(coverage.CounterFileTempl, coverage.CounterFilePref, metaHash, processID, time.Now().UnixNano())
376
+ s.cfname = filepath.Join(s.outdir, fn)
377
+ s.cftmp = filepath.Join(s.outdir, "tmp."+fn)
378
+ var err error
379
+ s.cf, err = os.Create(s.cftmp)
380
+ if err != nil {
381
+ return fmt.Errorf("creating counter data file %s: %v", s.cftmp, err)
382
+ }
383
+ return nil
384
+ }
385
+
386
+ // openOutputFiles opens output files in preparation for emitting
387
+ // coverage data. In the case of the meta-data file, openOutputFiles
388
+ // may determine that we can reuse an existing meta-data file in the
389
+ // outdir, in which case it will leave the 'mf' field in the state
390
+ // struct as nil. If a new meta-file is needed, the field 'mfname'
391
+ // will be the final desired path of the meta file, 'mftmp' will be a
392
+ // temporary file, and 'mf' will be an open os.File pointer for
393
+ // 'mftmp'. The idea is that the client/caller will write content into
394
+ // 'mf', close it, and then rename 'mftmp' to 'mfname'. This function
395
+ // also opens the counter data output file, setting 'cf' and 'cfname'
396
+ // in the state struct.
397
+ func (s *emitState) openOutputFiles(metaHash [16]byte, metaLen uint64, which fileType) error {
398
+ fi, err := os.Stat(s.outdir)
399
+ if err != nil {
400
+ return fmt.Errorf("output directory %q inaccessible (err: %v); no coverage data written", s.outdir, err)
401
+ }
402
+ if !fi.IsDir() {
403
+ return fmt.Errorf("output directory %q not a directory; no coverage data written", s.outdir)
404
+ }
405
+
406
+ if (which & metaDataFile) != 0 {
407
+ if err := s.openMetaFile(metaHash, metaLen); err != nil {
408
+ return err
409
+ }
410
+ }
411
+ if (which & counterDataFile) != 0 {
412
+ if err := s.openCounterFile(metaHash); err != nil {
413
+ return err
414
+ }
415
+ }
416
+ return nil
417
+ }
418
+
419
+ // emitMetaDataFile emits coverage meta-data to a previously opened
420
+ // temporary file (s.mftmp), then renames the generated file to the
421
+ // final path (s.mfname).
422
+ func (s *emitState) emitMetaDataFile(finalHash [16]byte, tlen uint64) error {
423
+ if err := writeMetaData(s.mf, s.metalist, cmode, cgran, finalHash); err != nil {
424
+ return fmt.Errorf("writing %s: %v\n", s.mftmp, err)
425
+ }
426
+ if err := s.mf.Close(); err != nil {
427
+ return fmt.Errorf("closing meta data temp file: %v", err)
428
+ }
429
+
430
+ // Temp file has now been flushed and closed. Rename the temp to the
431
+ // final desired path.
432
+ if err := os.Rename(s.mftmp, s.mfname); err != nil {
433
+ return fmt.Errorf("writing %s: rename from %s failed: %v\n", s.mfname, s.mftmp, err)
434
+ }
435
+
436
+ return nil
437
+ }
438
+
439
+ // needMetaDataFile returns TRUE if we need to emit a meta-data file
440
+ // for this program run. It should be used only after
441
+ // openOutputFiles() has been invoked.
442
+ func (s *emitState) needMetaDataFile() bool {
443
+ return s.mf != nil
444
+ }
445
+
446
+ func writeMetaData(w io.Writer, metalist []rtcov.CovMetaBlob, cmode coverage.CounterMode, gran coverage.CounterGranularity, finalHash [16]byte) error {
447
+ mfw := encodemeta.NewCoverageMetaFileWriter("<io.Writer>", w)
448
+
449
+ var blobs [][]byte
450
+ for _, e := range metalist {
451
+ sd := unsafe.Slice(e.P, int(e.Len))
452
+ blobs = append(blobs, sd)
453
+ }
454
+ return mfw.Write(finalHash, blobs, cmode, gran)
455
+ }
456
+
457
+ func (s *emitState) VisitFuncs(f encodecounter.CounterVisitorFn) error {
458
+ var tcounters []uint32
459
+
460
+ rdCounters := func(actrs []atomic.Uint32, ctrs []uint32) []uint32 {
461
+ ctrs = ctrs[:0]
462
+ for i := range actrs {
463
+ ctrs = append(ctrs, actrs[i].Load())
464
+ }
465
+ return ctrs
466
+ }
467
+
468
+ dpkg := uint32(0)
469
+ for _, c := range s.counterlist {
470
+ sd := unsafe.Slice((*atomic.Uint32)(unsafe.Pointer(c.Counters)), int(c.Len))
471
+ for i := 0; i < len(sd); i++ {
472
+ // Skip ahead until the next non-zero value.
473
+ sdi := sd[i].Load()
474
+ if sdi == 0 {
475
+ continue
476
+ }
477
+
478
+ // We found a function that was executed.
479
+ nCtrs := sd[i+coverage.NumCtrsOffset].Load()
480
+ pkgId := sd[i+coverage.PkgIdOffset].Load()
481
+ funcId := sd[i+coverage.FuncIdOffset].Load()
482
+ cst := i + coverage.FirstCtrOffset
483
+ counters := sd[cst : cst+int(nCtrs)]
484
+
485
+ // Check to make sure that we have at least one live
486
+ // counter. See the implementation note in ClearCoverageCounters
487
+ // for a description of why this is needed.
488
+ isLive := false
489
+ for i := 0; i < len(counters); i++ {
490
+ if counters[i].Load() != 0 {
491
+ isLive = true
492
+ break
493
+ }
494
+ }
495
+ if !isLive {
496
+ // Skip this function.
497
+ i += coverage.FirstCtrOffset + int(nCtrs) - 1
498
+ continue
499
+ }
500
+
501
+ if s.debug {
502
+ if pkgId != dpkg {
503
+ dpkg = pkgId
504
+ fmt.Fprintf(os.Stderr, "\n=+= %d: pk=%d visit live fcn",
505
+ i, pkgId)
506
+ }
507
+ fmt.Fprintf(os.Stderr, " {i=%d F%d NC%d}", i, funcId, nCtrs)
508
+ }
509
+
510
+ // Vet and/or fix up package ID. A package ID of zero
511
+ // indicates that there is some new package X that is a
512
+ // runtime dependency, and this package has code that
513
+ // executes before its corresponding init package runs.
514
+ // This is a fatal error that we should only see during
515
+ // Go development (e.g. tip).
516
+ ipk := int32(pkgId)
517
+ if ipk == 0 {
518
+ fmt.Fprintf(os.Stderr, "\n")
519
+ reportErrorInHardcodedList(int32(i), ipk, funcId, nCtrs)
520
+ } else if ipk < 0 {
521
+ if newId, ok := s.pkgmap[int(ipk)]; ok {
522
+ pkgId = uint32(newId)
523
+ } else {
524
+ fmt.Fprintf(os.Stderr, "\n")
525
+ reportErrorInHardcodedList(int32(i), ipk, funcId, nCtrs)
526
+ }
527
+ } else {
528
+ // The package ID value stored in the counter array
529
+ // has 1 added to it (so as to preclude the
530
+ // possibility of a zero value ; see
531
+ // runtime.addCovMeta), so subtract off 1 here to form
532
+ // the real package ID.
533
+ pkgId--
534
+ }
535
+
536
+ tcounters = rdCounters(counters, tcounters)
537
+ if err := f(pkgId, funcId, tcounters); err != nil {
538
+ return err
539
+ }
540
+
541
+ // Skip over this function.
542
+ i += coverage.FirstCtrOffset + int(nCtrs) - 1
543
+ }
544
+ if s.debug {
545
+ fmt.Fprintf(os.Stderr, "\n")
546
+ }
547
+ }
548
+ return nil
549
+ }
550
+
551
+ // captureOsArgs converts os.Args() into the format we use to store
552
+ // this info in the counter data file (counter data file "args"
553
+ // section is a generic key-value collection). See the 'args' section
554
+ // in internal/coverage/defs.go for more info. The args map
555
+ // is also used to capture GOOS + GOARCH values as well.
556
+ func captureOsArgs() map[string]string {
557
+ m := make(map[string]string)
558
+ m["argc"] = strconv.Itoa(len(os.Args))
559
+ for k, a := range os.Args {
560
+ m[fmt.Sprintf("argv%d", k)] = a
561
+ }
562
+ m["GOOS"] = runtime.GOOS
563
+ m["GOARCH"] = runtime.GOARCH
564
+ return m
565
+ }
566
+
567
+ // emitCounterDataFile emits the counter data portion of a
568
+ // coverage output file (to the file 's.cf').
569
+ func (s *emitState) emitCounterDataFile(finalHash [16]byte, w io.Writer) error {
570
+ cfw := encodecounter.NewCoverageDataWriter(w, coverage.CtrULeb128)
571
+ if err := cfw.Write(finalHash, capturedOsArgs, s); err != nil {
572
+ return err
573
+ }
574
+ return nil
575
+ }
576
+
577
+ // markProfileEmitted signals the runtime/coverage machinery that
578
+ // coverage data output files have already been written out, and there
579
+ // is no need to take any additional action at exit time. This
580
+ // function is called (via linknamed reference) from the
581
+ // coverage-related boilerplate code in _testmain.go emitted for go
582
+ // unit tests.
583
+ func markProfileEmitted(val bool) {
584
+ covProfileAlreadyEmitted = val
585
+ }
586
+
587
+ func reportErrorInHardcodedList(slot, pkgID int32, fnID, nCtrs uint32) {
588
+ metaList := getCovMetaList()
589
+ pkgMap := getCovPkgMap()
590
+
591
+ println("internal error in coverage meta-data tracking:")
592
+ println("encountered bad pkgID:", pkgID, " at slot:", slot,
593
+ " fnID:", fnID, " numCtrs:", nCtrs)
594
+ println("list of hard-coded runtime package IDs needs revising.")
595
+ println("[see the comment on the 'rtPkgs' var in ")
596
+ println(" <goroot>/src/internal/coverage/pkid.go]")
597
+ println("registered list:")
598
+ for k, b := range metaList {
599
+ print("slot: ", k, " path='", b.PkgPath, "' ")
600
+ if b.PkgID != -1 {
601
+ print(" hard-coded id: ", b.PkgID)
602
+ }
603
+ println("")
604
+ }
605
+ println("remap table:")
606
+ for from, to := range pkgMap {
607
+ println("from ", from, " to ", to)
608
+ }
609
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/emitdata_test.go ADDED
@@ -0,0 +1,550 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import (
8
+ "fmt"
9
+ "internal/coverage"
10
+ "internal/goexperiment"
11
+ "internal/platform"
12
+ "internal/testenv"
13
+ "os"
14
+ "os/exec"
15
+ "path/filepath"
16
+ "runtime"
17
+ "strings"
18
+ "testing"
19
+ )
20
+
21
+ // Set to true for debugging (linux only).
22
+ const fixedTestDir = false
23
+
24
+ func TestCoverageApis(t *testing.T) {
25
+ if testing.Short() {
26
+ t.Skipf("skipping test: too long for short mode")
27
+ }
28
+ if !goexperiment.CoverageRedesign {
29
+ t.Skipf("skipping new coverage tests (experiment not enabled)")
30
+ }
31
+ testenv.MustHaveGoBuild(t)
32
+ dir := t.TempDir()
33
+ if fixedTestDir {
34
+ dir = "/tmp/qqqzzz"
35
+ os.RemoveAll(dir)
36
+ mkdir(t, dir)
37
+ }
38
+
39
+ // Build harness. We need two copies of the harness, one built
40
+ // with -covermode=atomic and one built non-atomic.
41
+ bdir1 := mkdir(t, filepath.Join(dir, "build1"))
42
+ hargs1 := []string{"-covermode=atomic", "-coverpkg=all"}
43
+ atomicHarnessPath := buildHarness(t, bdir1, hargs1)
44
+ nonAtomicMode := testing.CoverMode()
45
+ if testing.CoverMode() == "atomic" {
46
+ nonAtomicMode = "set"
47
+ }
48
+ bdir2 := mkdir(t, filepath.Join(dir, "build2"))
49
+ hargs2 := []string{"-coverpkg=all", "-covermode=" + nonAtomicMode}
50
+ nonAtomicHarnessPath := buildHarness(t, bdir2, hargs2)
51
+
52
+ t.Logf("atomic harness path is %s", atomicHarnessPath)
53
+ t.Logf("non-atomic harness path is %s", nonAtomicHarnessPath)
54
+
55
+ // Sub-tests for each API we want to inspect, plus
56
+ // extras for error testing.
57
+ t.Run("emitToDir", func(t *testing.T) {
58
+ t.Parallel()
59
+ testEmitToDir(t, atomicHarnessPath, dir)
60
+ })
61
+ t.Run("emitToWriter", func(t *testing.T) {
62
+ t.Parallel()
63
+ testEmitToWriter(t, atomicHarnessPath, dir)
64
+ })
65
+ t.Run("emitToNonexistentDir", func(t *testing.T) {
66
+ t.Parallel()
67
+ testEmitToNonexistentDir(t, atomicHarnessPath, dir)
68
+ })
69
+ t.Run("emitToNilWriter", func(t *testing.T) {
70
+ t.Parallel()
71
+ testEmitToNilWriter(t, atomicHarnessPath, dir)
72
+ })
73
+ t.Run("emitToFailingWriter", func(t *testing.T) {
74
+ t.Parallel()
75
+ testEmitToFailingWriter(t, atomicHarnessPath, dir)
76
+ })
77
+ t.Run("emitWithCounterClear", func(t *testing.T) {
78
+ t.Parallel()
79
+ testEmitWithCounterClear(t, atomicHarnessPath, dir)
80
+ })
81
+ t.Run("emitToDirNonAtomic", func(t *testing.T) {
82
+ t.Parallel()
83
+ testEmitToDirNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, dir)
84
+ })
85
+ t.Run("emitToWriterNonAtomic", func(t *testing.T) {
86
+ t.Parallel()
87
+ testEmitToWriterNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, dir)
88
+ })
89
+ t.Run("emitWithCounterClearNonAtomic", func(t *testing.T) {
90
+ t.Parallel()
91
+ testEmitWithCounterClearNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, dir)
92
+ })
93
+ }
94
+
95
+ // upmergeCoverData helps improve coverage data for this package
96
+ // itself. If this test itself is being invoked with "-cover", then
97
+ // what we'd like is for package coverage data (that is, coverage for
98
+ // routines in "runtime/coverage") to be incorporated into the test
99
+ // run from the "harness.exe" runs we've just done. We can accomplish
100
+ // this by doing a merge from the harness gocoverdir's to the test
101
+ // gocoverdir.
102
+ func upmergeCoverData(t *testing.T, gocoverdir string, mode string) {
103
+ if testing.CoverMode() != mode {
104
+ return
105
+ }
106
+ testGoCoverDir := os.Getenv("GOCOVERDIR")
107
+ if testGoCoverDir == "" {
108
+ return
109
+ }
110
+ args := []string{"tool", "covdata", "merge", "-pkg=runtime/coverage",
111
+ "-o", testGoCoverDir, "-i", gocoverdir}
112
+ t.Logf("up-merge of covdata from %s to %s", gocoverdir, testGoCoverDir)
113
+ t.Logf("executing: go %+v", args)
114
+ cmd := exec.Command(testenv.GoToolPath(t), args...)
115
+ if b, err := cmd.CombinedOutput(); err != nil {
116
+ t.Fatalf("covdata merge failed (%v): %s", err, b)
117
+ }
118
+ }
119
+
120
+ // buildHarness builds the helper program "harness.exe".
121
+ func buildHarness(t *testing.T, dir string, opts []string) string {
122
+ harnessPath := filepath.Join(dir, "harness.exe")
123
+ harnessSrc := filepath.Join("testdata", "harness.go")
124
+ args := []string{"build", "-o", harnessPath}
125
+ args = append(args, opts...)
126
+ args = append(args, harnessSrc)
127
+ //t.Logf("harness build: go %+v\n", args)
128
+ cmd := exec.Command(testenv.GoToolPath(t), args...)
129
+ if b, err := cmd.CombinedOutput(); err != nil {
130
+ t.Fatalf("build failed (%v): %s", err, b)
131
+ }
132
+ return harnessPath
133
+ }
134
+
135
+ func mkdir(t *testing.T, d string) string {
136
+ t.Helper()
137
+ if err := os.Mkdir(d, 0777); err != nil {
138
+ t.Fatalf("mkdir failed: %v", err)
139
+ }
140
+ return d
141
+ }
142
+
143
+ // updateGoCoverDir updates the specified environment 'env' to set
144
+ // GOCOVERDIR to 'gcd' (if setGoCoverDir is TRUE) or removes
145
+ // GOCOVERDIR from the environment (if setGoCoverDir is false).
146
+ func updateGoCoverDir(env []string, gcd string, setGoCoverDir bool) []string {
147
+ rv := []string{}
148
+ found := false
149
+ for _, v := range env {
150
+ if strings.HasPrefix(v, "GOCOVERDIR=") {
151
+ if !setGoCoverDir {
152
+ continue
153
+ }
154
+ v = "GOCOVERDIR=" + gcd
155
+ found = true
156
+ }
157
+ rv = append(rv, v)
158
+ }
159
+ if !found && setGoCoverDir {
160
+ rv = append(rv, "GOCOVERDIR="+gcd)
161
+ }
162
+ return rv
163
+ }
164
+
165
+ func runHarness(t *testing.T, harnessPath string, tp string, setGoCoverDir bool, rdir, edir string) (string, error) {
166
+ t.Logf("running: %s -tp %s -o %s with rdir=%s and GOCOVERDIR=%v", harnessPath, tp, edir, rdir, setGoCoverDir)
167
+ cmd := exec.Command(harnessPath, "-tp", tp, "-o", edir)
168
+ cmd.Dir = rdir
169
+ cmd.Env = updateGoCoverDir(os.Environ(), rdir, setGoCoverDir)
170
+ b, err := cmd.CombinedOutput()
171
+ //t.Logf("harness run output: %s\n", string(b))
172
+ return string(b), err
173
+ }
174
+
175
+ func testForSpecificFunctions(t *testing.T, dir string, want []string, avoid []string) string {
176
+ args := []string{"tool", "covdata", "debugdump",
177
+ "-live", "-pkg=command-line-arguments", "-i=" + dir}
178
+ t.Logf("running: go %v\n", args)
179
+ cmd := exec.Command(testenv.GoToolPath(t), args...)
180
+ b, err := cmd.CombinedOutput()
181
+ if err != nil {
182
+ t.Fatalf("'go tool covdata failed (%v): %s", err, b)
183
+ }
184
+ output := string(b)
185
+ rval := ""
186
+ for _, f := range want {
187
+ wf := "Func: " + f + "\n"
188
+ if strings.Contains(output, wf) {
189
+ continue
190
+ }
191
+ rval += fmt.Sprintf("error: output should contain %q but does not\n", wf)
192
+ }
193
+ for _, f := range avoid {
194
+ wf := "Func: " + f + "\n"
195
+ if strings.Contains(output, wf) {
196
+ rval += fmt.Sprintf("error: output should not contain %q but does\n", wf)
197
+ }
198
+ }
199
+ if rval != "" {
200
+ t.Logf("=-= begin output:\n" + output + "\n=-= end output\n")
201
+ }
202
+ return rval
203
+ }
204
+
205
+ func withAndWithoutRunner(f func(setit bool, tag string)) {
206
+ // Run 'f' with and without GOCOVERDIR set.
207
+ for i := 0; i < 2; i++ {
208
+ tag := "x"
209
+ setGoCoverDir := true
210
+ if i == 0 {
211
+ setGoCoverDir = false
212
+ tag = "y"
213
+ }
214
+ f(setGoCoverDir, tag)
215
+ }
216
+ }
217
+
218
+ func mktestdirs(t *testing.T, tag, tp, dir string) (string, string) {
219
+ t.Helper()
220
+ rdir := mkdir(t, filepath.Join(dir, tp+"-rdir-"+tag))
221
+ edir := mkdir(t, filepath.Join(dir, tp+"-edir-"+tag))
222
+ return rdir, edir
223
+ }
224
+
225
+ func testEmitToDir(t *testing.T, harnessPath string, dir string) {
226
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
227
+ tp := "emitToDir"
228
+ rdir, edir := mktestdirs(t, tag, tp, dir)
229
+ output, err := runHarness(t, harnessPath, tp,
230
+ setGoCoverDir, rdir, edir)
231
+ if err != nil {
232
+ t.Logf("%s", output)
233
+ t.Fatalf("running 'harness -tp emitDir': %v", err)
234
+ }
235
+
236
+ // Just check to make sure meta-data file and counter data file were
237
+ // written. Another alternative would be to run "go tool covdata"
238
+ // or equivalent, but for now, this is what we've got.
239
+ dents, err := os.ReadDir(edir)
240
+ if err != nil {
241
+ t.Fatalf("os.ReadDir(%s) failed: %v", edir, err)
242
+ }
243
+ mfc := 0
244
+ cdc := 0
245
+ for _, e := range dents {
246
+ if e.IsDir() {
247
+ continue
248
+ }
249
+ if strings.HasPrefix(e.Name(), coverage.MetaFilePref) {
250
+ mfc++
251
+ } else if strings.HasPrefix(e.Name(), coverage.CounterFilePref) {
252
+ cdc++
253
+ }
254
+ }
255
+ wantmf := 1
256
+ wantcf := 1
257
+ if mfc != wantmf {
258
+ t.Errorf("EmitToDir: want %d meta-data files, got %d\n", wantmf, mfc)
259
+ }
260
+ if cdc != wantcf {
261
+ t.Errorf("EmitToDir: want %d counter-data files, got %d\n", wantcf, cdc)
262
+ }
263
+ upmergeCoverData(t, edir, "atomic")
264
+ upmergeCoverData(t, rdir, "atomic")
265
+ })
266
+ }
267
+
268
+ func testEmitToWriter(t *testing.T, harnessPath string, dir string) {
269
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
270
+ tp := "emitToWriter"
271
+ rdir, edir := mktestdirs(t, tag, tp, dir)
272
+ output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
273
+ if err != nil {
274
+ t.Logf("%s", output)
275
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
276
+ }
277
+ want := []string{"main", tp}
278
+ avoid := []string{"final"}
279
+ if msg := testForSpecificFunctions(t, edir, want, avoid); msg != "" {
280
+ t.Errorf("coverage data from %q output match failed: %s", tp, msg)
281
+ }
282
+ upmergeCoverData(t, edir, "atomic")
283
+ upmergeCoverData(t, rdir, "atomic")
284
+ })
285
+ }
286
+
287
+ func testEmitToNonexistentDir(t *testing.T, harnessPath string, dir string) {
288
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
289
+ tp := "emitToNonexistentDir"
290
+ rdir, edir := mktestdirs(t, tag, tp, dir)
291
+ output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
292
+ if err != nil {
293
+ t.Logf("%s", output)
294
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
295
+ }
296
+ upmergeCoverData(t, edir, "atomic")
297
+ upmergeCoverData(t, rdir, "atomic")
298
+ })
299
+ }
300
+
301
+ func testEmitToUnwritableDir(t *testing.T, harnessPath string, dir string) {
302
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
303
+
304
+ tp := "emitToUnwritableDir"
305
+ rdir, edir := mktestdirs(t, tag, tp, dir)
306
+
307
+ // Make edir unwritable.
308
+ if err := os.Chmod(edir, 0555); err != nil {
309
+ t.Fatalf("chmod failed: %v", err)
310
+ }
311
+ defer os.Chmod(edir, 0777)
312
+
313
+ output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
314
+ if err != nil {
315
+ t.Logf("%s", output)
316
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
317
+ }
318
+ upmergeCoverData(t, edir, "atomic")
319
+ upmergeCoverData(t, rdir, "atomic")
320
+ })
321
+ }
322
+
323
+ func testEmitToNilWriter(t *testing.T, harnessPath string, dir string) {
324
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
325
+ tp := "emitToNilWriter"
326
+ rdir, edir := mktestdirs(t, tag, tp, dir)
327
+ output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
328
+ if err != nil {
329
+ t.Logf("%s", output)
330
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
331
+ }
332
+ upmergeCoverData(t, edir, "atomic")
333
+ upmergeCoverData(t, rdir, "atomic")
334
+ })
335
+ }
336
+
337
+ func testEmitToFailingWriter(t *testing.T, harnessPath string, dir string) {
338
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
339
+ tp := "emitToFailingWriter"
340
+ rdir, edir := mktestdirs(t, tag, tp, dir)
341
+ output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
342
+ if err != nil {
343
+ t.Logf("%s", output)
344
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
345
+ }
346
+ upmergeCoverData(t, edir, "atomic")
347
+ upmergeCoverData(t, rdir, "atomic")
348
+ })
349
+ }
350
+
351
+ func testEmitWithCounterClear(t *testing.T, harnessPath string, dir string) {
352
+ withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
353
+ tp := "emitWithCounterClear"
354
+ rdir, edir := mktestdirs(t, tag, tp, dir)
355
+ output, err := runHarness(t, harnessPath, tp,
356
+ setGoCoverDir, rdir, edir)
357
+ if err != nil {
358
+ t.Logf("%s", output)
359
+ t.Fatalf("running 'harness -tp %s': %v", tp, err)
360
+ }
361
+ want := []string{tp, "postClear"}
362
+ avoid := []string{"preClear", "main", "final"}
363
+ if msg := testForSpecificFunctions(t, edir, want, avoid); msg != "" {
364
+ t.Logf("%s", output)
365
+ t.Errorf("coverage data from %q output match failed: %s", tp, msg)
366
+ }
367
+ upmergeCoverData(t, edir, "atomic")
368
+ upmergeCoverData(t, rdir, "atomic")
369
+ })
370
+ }
371
+
372
+ func testEmitToDirNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
373
+ tp := "emitToDir"
374
+ tag := "nonatomdir"
375
+ rdir, edir := mktestdirs(t, tag, tp, dir)
376
+ output, err := runHarness(t, harnessPath, tp,
377
+ true, rdir, edir)
378
+
379
+ // We expect an error here.
380
+ if err == nil {
381
+ t.Logf("%s", output)
382
+ t.Fatalf("running 'harness -tp %s': did not get expected error", tp)
383
+ }
384
+
385
+ got := strings.TrimSpace(string(output))
386
+ want := "WriteCountersDir invoked for program built"
387
+ if !strings.Contains(got, want) {
388
+ t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
389
+ tp, got, want)
390
+ }
391
+ upmergeCoverData(t, edir, naMode)
392
+ upmergeCoverData(t, rdir, naMode)
393
+ }
394
+
395
+ func testEmitToWriterNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
396
+ tp := "emitToWriter"
397
+ tag := "nonatomw"
398
+ rdir, edir := mktestdirs(t, tag, tp, dir)
399
+ output, err := runHarness(t, harnessPath, tp,
400
+ true, rdir, edir)
401
+
402
+ // We expect an error here.
403
+ if err == nil {
404
+ t.Logf("%s", output)
405
+ t.Fatalf("running 'harness -tp %s': did not get expected error", tp)
406
+ }
407
+
408
+ got := strings.TrimSpace(string(output))
409
+ want := "WriteCounters invoked for program built"
410
+ if !strings.Contains(got, want) {
411
+ t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
412
+ tp, got, want)
413
+ }
414
+
415
+ upmergeCoverData(t, edir, naMode)
416
+ upmergeCoverData(t, rdir, naMode)
417
+ }
418
+
419
+ func testEmitWithCounterClearNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
420
+ tp := "emitWithCounterClear"
421
+ tag := "cclear"
422
+ rdir, edir := mktestdirs(t, tag, tp, dir)
423
+ output, err := runHarness(t, harnessPath, tp,
424
+ true, rdir, edir)
425
+
426
+ // We expect an error here.
427
+ if err == nil {
428
+ t.Logf("%s", output)
429
+ t.Fatalf("running 'harness -tp %s' nonatomic: did not get expected error", tp)
430
+ }
431
+
432
+ got := strings.TrimSpace(string(output))
433
+ want := "ClearCounters invoked for program built"
434
+ if !strings.Contains(got, want) {
435
+ t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
436
+ tp, got, want)
437
+ }
438
+
439
+ upmergeCoverData(t, edir, naMode)
440
+ upmergeCoverData(t, rdir, naMode)
441
+ }
442
+
443
+ func TestApisOnNocoverBinary(t *testing.T) {
444
+ if testing.Short() {
445
+ t.Skipf("skipping test: too long for short mode")
446
+ }
447
+ testenv.MustHaveGoBuild(t)
448
+ dir := t.TempDir()
449
+
450
+ // Build harness with no -cover.
451
+ bdir := mkdir(t, filepath.Join(dir, "nocover"))
452
+ edir := mkdir(t, filepath.Join(dir, "emitDirNo"))
453
+ harnessPath := buildHarness(t, bdir, nil)
454
+ output, err := runHarness(t, harnessPath, "emitToDir", false, edir, edir)
455
+ if err == nil {
456
+ t.Fatalf("expected error on TestApisOnNocoverBinary harness run")
457
+ }
458
+ const want = "not built with -cover"
459
+ if !strings.Contains(output, want) {
460
+ t.Errorf("error output does not contain %q: %s", want, output)
461
+ }
462
+ }
463
+
464
+ func TestIssue56006EmitDataRaceCoverRunningGoroutine(t *testing.T) {
465
+ if testing.Short() {
466
+ t.Skipf("skipping test: too long for short mode")
467
+ }
468
+ if !goexperiment.CoverageRedesign {
469
+ t.Skipf("skipping new coverage tests (experiment not enabled)")
470
+ }
471
+
472
+ // This test requires "go test -race -cover", meaning that we need
473
+ // go build, go run, and "-race" support.
474
+ testenv.MustHaveGoRun(t)
475
+ if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) ||
476
+ !testenv.HasCGO() {
477
+ t.Skip("skipped due to lack of race detector support / CGO")
478
+ }
479
+
480
+ // This will run a program with -cover and -race where we have a
481
+ // goroutine still running (and updating counters) at the point where
482
+ // the test runtime is trying to write out counter data.
483
+ cmd := exec.Command(testenv.GoToolPath(t), "test", "-cover", "-race")
484
+ cmd.Dir = filepath.Join("testdata", "issue56006")
485
+ b, err := cmd.CombinedOutput()
486
+ if err != nil {
487
+ t.Fatalf("go test -cover -race failed: %v", err)
488
+ }
489
+
490
+ // Don't want to see any data races in output.
491
+ avoid := []string{"DATA RACE"}
492
+ for _, no := range avoid {
493
+ if strings.Contains(string(b), no) {
494
+ t.Logf("%s\n", string(b))
495
+ t.Fatalf("found %s in test output, not permitted", no)
496
+ }
497
+ }
498
+ }
499
+
500
+ func TestIssue59563TruncatedCoverPkgAll(t *testing.T) {
501
+ if testing.Short() {
502
+ t.Skipf("skipping test: too long for short mode")
503
+ }
504
+ testenv.MustHaveGoRun(t)
505
+
506
+ tmpdir := t.TempDir()
507
+ ppath := filepath.Join(tmpdir, "foo.cov")
508
+
509
+ cmd := exec.Command(testenv.GoToolPath(t), "test", "-coverpkg=all", "-coverprofile="+ppath)
510
+ cmd.Dir = filepath.Join("testdata", "issue59563")
511
+ b, err := cmd.CombinedOutput()
512
+ if err != nil {
513
+ t.Fatalf("go test -cover failed: %v", err)
514
+ }
515
+
516
+ cmd = exec.Command(testenv.GoToolPath(t), "tool", "cover", "-func="+ppath)
517
+ b, err = cmd.CombinedOutput()
518
+ if err != nil {
519
+ t.Fatalf("go tool cover -func failed: %v", err)
520
+ }
521
+
522
+ lines := strings.Split(string(b), "\n")
523
+ nfound := 0
524
+ bad := false
525
+ for _, line := range lines {
526
+ f := strings.Fields(line)
527
+ if len(f) == 0 {
528
+ continue
529
+ }
530
+ // We're only interested in the specific function "large" for
531
+ // the testcase being built. See the #59563 for details on why
532
+ // size matters.
533
+ if !(strings.HasPrefix(f[0], "runtime/coverage/testdata/issue59563/repro.go") && strings.Contains(line, "large")) {
534
+ continue
535
+ }
536
+ nfound++
537
+ want := "100.0%"
538
+ if f[len(f)-1] != want {
539
+ t.Errorf("wanted %s got: %q\n", want, line)
540
+ bad = true
541
+ }
542
+ }
543
+ if nfound != 1 {
544
+ t.Errorf("wanted 1 found, got %d\n", nfound)
545
+ bad = true
546
+ }
547
+ if bad {
548
+ t.Logf("func output:\n%s\n", string(b))
549
+ }
550
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/hooks.go ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import _ "unsafe"
8
+
9
+ // initHook is invoked from the main package "init" routine in
10
+ // programs built with "-cover". This function is intended to be
11
+ // called only by the compiler.
12
+ //
13
+ // If 'istest' is false, it indicates we're building a regular program
14
+ // ("go build -cover ..."), in which case we immediately try to write
15
+ // out the meta-data file, and register emitCounterData as an exit
16
+ // hook.
17
+ //
18
+ // If 'istest' is true (indicating that the program in question is a
19
+ // Go test binary), then we tentatively queue up both emitMetaData and
20
+ // emitCounterData as exit hooks. In the normal case (e.g. regular "go
21
+ // test -cover" run) the testmain.go boilerplate will run at the end
22
+ // of the test, write out the coverage percentage, and then invoke
23
+ // markProfileEmitted() to indicate that no more work needs to be
24
+ // done. If however that call is never made, this is a sign that the
25
+ // test binary is being used as a replacement binary for the tool
26
+ // being tested, hence we do want to run exit hooks when the program
27
+ // terminates.
28
+ func initHook(istest bool) {
29
+ // Note: hooks are run in reverse registration order, so
30
+ // register the counter data hook before the meta-data hook
31
+ // (in the case where two hooks are needed).
32
+ runOnNonZeroExit := true
33
+ runtime_addExitHook(emitCounterData, runOnNonZeroExit)
34
+ if istest {
35
+ runtime_addExitHook(emitMetaData, runOnNonZeroExit)
36
+ } else {
37
+ emitMetaData()
38
+ }
39
+ }
40
+
41
+ //go:linkname runtime_addExitHook runtime.addExitHook
42
+ func runtime_addExitHook(f func(), runOnNonZeroExit bool)
platform/dbops/binaries/go/go/src/runtime/coverage/testdata/harness.go ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package main
6
+
7
+ import (
8
+ "flag"
9
+ "fmt"
10
+ "internal/coverage/slicewriter"
11
+ "io"
12
+ "io/ioutil"
13
+ "log"
14
+ "path/filepath"
15
+ "runtime/coverage"
16
+ "strings"
17
+ )
18
+
19
+ var verbflag = flag.Int("v", 0, "Verbose trace output level")
20
+ var testpointflag = flag.String("tp", "", "Testpoint to run")
21
+ var outdirflag = flag.String("o", "", "Output dir into which to emit")
22
+
23
+ func emitToWriter() {
24
+ log.SetPrefix("emitToWriter: ")
25
+ var slwm slicewriter.WriteSeeker
26
+ if err := coverage.WriteMeta(&slwm); err != nil {
27
+ log.Fatalf("error: WriteMeta returns %v", err)
28
+ }
29
+ mf := filepath.Join(*outdirflag, "covmeta.0abcdef")
30
+ if err := ioutil.WriteFile(mf, slwm.BytesWritten(), 0666); err != nil {
31
+ log.Fatalf("error: writing %s: %v", mf, err)
32
+ }
33
+ var slwc slicewriter.WriteSeeker
34
+ if err := coverage.WriteCounters(&slwc); err != nil {
35
+ log.Fatalf("error: WriteCounters returns %v", err)
36
+ }
37
+ cf := filepath.Join(*outdirflag, "covcounters.0abcdef.99.77")
38
+ if err := ioutil.WriteFile(cf, slwc.BytesWritten(), 0666); err != nil {
39
+ log.Fatalf("error: writing %s: %v", cf, err)
40
+ }
41
+ }
42
+
43
+ func emitToDir() {
44
+ log.SetPrefix("emitToDir: ")
45
+ if err := coverage.WriteMetaDir(*outdirflag); err != nil {
46
+ log.Fatalf("error: WriteMetaDir returns %v", err)
47
+ }
48
+ if err := coverage.WriteCountersDir(*outdirflag); err != nil {
49
+ log.Fatalf("error: WriteCountersDir returns %v", err)
50
+ }
51
+ }
52
+
53
+ func emitToNonexistentDir() {
54
+ log.SetPrefix("emitToNonexistentDir: ")
55
+
56
+ want := []string{
57
+ "no such file or directory", // linux-ish
58
+ "system cannot find the file specified", // windows
59
+ "does not exist", // plan9
60
+ }
61
+
62
+ checkWant := func(which string, got string) {
63
+ found := false
64
+ for _, w := range want {
65
+ if strings.Contains(got, w) {
66
+ found = true
67
+ break
68
+ }
69
+ }
70
+ if !found {
71
+ log.Fatalf("%s emit to bad dir: got error:\n %v\nwanted error with one of:\n %+v", which, got, want)
72
+ }
73
+ }
74
+
75
+ // Mangle the output directory to produce something nonexistent.
76
+ mangled := *outdirflag + "_MANGLED"
77
+ if err := coverage.WriteMetaDir(mangled); err == nil {
78
+ log.Fatal("expected error from WriteMetaDir to nonexistent dir")
79
+ } else {
80
+ got := fmt.Sprintf("%v", err)
81
+ checkWant("meta data", got)
82
+ }
83
+
84
+ // Now try to emit counter data file to a bad dir.
85
+ if err := coverage.WriteCountersDir(mangled); err == nil {
86
+ log.Fatal("expected error emitting counter data to bad dir")
87
+ } else {
88
+ got := fmt.Sprintf("%v", err)
89
+ checkWant("counter data", got)
90
+ }
91
+ }
92
+
93
+ func emitToUnwritableDir() {
94
+ log.SetPrefix("emitToUnwritableDir: ")
95
+
96
+ want := "permission denied"
97
+
98
+ if err := coverage.WriteMetaDir(*outdirflag); err == nil {
99
+ log.Fatal("expected error from WriteMetaDir to unwritable dir")
100
+ } else {
101
+ got := fmt.Sprintf("%v", err)
102
+ if !strings.Contains(got, want) {
103
+ log.Fatalf("meta-data emit to unwritable dir: wanted error containing %q got %q", want, got)
104
+ }
105
+ }
106
+
107
+ // Similarly with writing counter data.
108
+ if err := coverage.WriteCountersDir(*outdirflag); err == nil {
109
+ log.Fatal("expected error emitting counter data to unwritable dir")
110
+ } else {
111
+ got := fmt.Sprintf("%v", err)
112
+ if !strings.Contains(got, want) {
113
+ log.Fatalf("emitting counter data to unwritable dir: wanted error containing %q got %q", want, got)
114
+ }
115
+ }
116
+ }
117
+
118
+ func emitToNilWriter() {
119
+ log.SetPrefix("emitToWriter: ")
120
+ want := "nil writer"
121
+ var bad io.WriteSeeker
122
+ if err := coverage.WriteMeta(bad); err == nil {
123
+ log.Fatal("expected error passing nil writer for meta emit")
124
+ } else {
125
+ got := fmt.Sprintf("%v", err)
126
+ if !strings.Contains(got, want) {
127
+ log.Fatalf("emitting meta-data passing nil writer: wanted error containing %q got %q", want, got)
128
+ }
129
+ }
130
+
131
+ if err := coverage.WriteCounters(bad); err == nil {
132
+ log.Fatal("expected error passing nil writer for counter emit")
133
+ } else {
134
+ got := fmt.Sprintf("%v", err)
135
+ if !strings.Contains(got, want) {
136
+ log.Fatalf("emitting counter data passing nil writer: wanted error containing %q got %q", want, got)
137
+ }
138
+ }
139
+ }
140
+
141
+ type failingWriter struct {
142
+ writeCount int
143
+ writeLimit int
144
+ slws slicewriter.WriteSeeker
145
+ }
146
+
147
+ func (f *failingWriter) Write(p []byte) (n int, err error) {
148
+ c := f.writeCount
149
+ f.writeCount++
150
+ if f.writeLimit < 0 || c < f.writeLimit {
151
+ return f.slws.Write(p)
152
+ }
153
+ return 0, fmt.Errorf("manufactured write error")
154
+ }
155
+
156
+ func (f *failingWriter) Seek(offset int64, whence int) (int64, error) {
157
+ return f.slws.Seek(offset, whence)
158
+ }
159
+
160
+ func (f *failingWriter) reset(lim int) {
161
+ f.writeCount = 0
162
+ f.writeLimit = lim
163
+ f.slws = slicewriter.WriteSeeker{}
164
+ }
165
+
166
+ func writeStressTest(tag string, testf func(testf *failingWriter) error) {
167
+ // Invoke the function initially without the write limit
168
+ // set, to capture the number of writes performed.
169
+ fw := &failingWriter{writeLimit: -1}
170
+ testf(fw)
171
+
172
+ // Now that we know how many writes are going to happen, run the
173
+ // function repeatedly, each time with a Write operation set to
174
+ // fail at a new spot. The goal here is to make sure that:
175
+ // A) an error is reported, and B) nothing crashes.
176
+ tot := fw.writeCount
177
+ for i := 0; i < tot; i++ {
178
+ fw.reset(i)
179
+ err := testf(fw)
180
+ if err == nil {
181
+ log.Fatalf("no error from write %d tag %s", i, tag)
182
+ }
183
+ }
184
+ }
185
+
186
+ func postClear() int {
187
+ return 42
188
+ }
189
+
190
+ func preClear() int {
191
+ return 42
192
+ }
193
+
194
+ // This test is designed to ensure that write errors are properly
195
+ // handled by the code that writes out coverage data. It repeatedly
196
+ // invokes the 'emit to writer' apis using a specially crafted writer
197
+ // that captures the total number of expected writes, then replays the
198
+ // execution N times with a manufactured write error at the
199
+ // appropriate spot.
200
+ func emitToFailingWriter() {
201
+ log.SetPrefix("emitToFailingWriter: ")
202
+
203
+ writeStressTest("emit-meta", func(f *failingWriter) error {
204
+ return coverage.WriteMeta(f)
205
+ })
206
+ writeStressTest("emit-counter", func(f *failingWriter) error {
207
+ return coverage.WriteCounters(f)
208
+ })
209
+ }
210
+
211
+ func emitWithCounterClear() {
212
+ log.SetPrefix("emitWitCounterClear: ")
213
+ preClear()
214
+ if err := coverage.ClearCounters(); err != nil {
215
+ log.Fatalf("clear failed: %v", err)
216
+ }
217
+ postClear()
218
+ if err := coverage.WriteMetaDir(*outdirflag); err != nil {
219
+ log.Fatalf("error: WriteMetaDir returns %v", err)
220
+ }
221
+ if err := coverage.WriteCountersDir(*outdirflag); err != nil {
222
+ log.Fatalf("error: WriteCountersDir returns %v", err)
223
+ }
224
+ }
225
+
226
+ func final() int {
227
+ println("I run last.")
228
+ return 43
229
+ }
230
+
231
+ func main() {
232
+ log.SetFlags(0)
233
+ flag.Parse()
234
+ if *testpointflag == "" {
235
+ log.Fatalf("error: no testpoint (use -tp flag)")
236
+ }
237
+ if *outdirflag == "" {
238
+ log.Fatalf("error: no output dir specified (use -o flag)")
239
+ }
240
+ switch *testpointflag {
241
+ case "emitToDir":
242
+ emitToDir()
243
+ case "emitToWriter":
244
+ emitToWriter()
245
+ case "emitToNonexistentDir":
246
+ emitToNonexistentDir()
247
+ case "emitToUnwritableDir":
248
+ emitToUnwritableDir()
249
+ case "emitToNilWriter":
250
+ emitToNilWriter()
251
+ case "emitToFailingWriter":
252
+ emitToFailingWriter()
253
+ case "emitWithCounterClear":
254
+ emitWithCounterClear()
255
+ default:
256
+ log.Fatalf("error: unknown testpoint %q", *testpointflag)
257
+ }
258
+ final()
259
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue56006/repro.go ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ //go:noinline
4
+ func blah(x int) int {
5
+ if x != 0 {
6
+ return x + 42
7
+ }
8
+ return x - 42
9
+ }
10
+
11
+ func main() {
12
+ go infloop()
13
+ println(blah(1) + blah(0))
14
+ }
15
+
16
+ var G int
17
+
18
+ func infloop() {
19
+ for {
20
+ G += blah(1)
21
+ G += blah(0)
22
+ if G > 10000 {
23
+ G = 0
24
+ }
25
+ }
26
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue56006/repro_test.go ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import "testing"
4
+
5
+ func TestSomething(t *testing.T) {
6
+ go infloop()
7
+ println(blah(1) + blah(0))
8
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue59563/repro.go ADDED
@@ -0,0 +1,823 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package repro
6
+
7
+ import (
8
+ "fmt"
9
+ "net/http"
10
+ )
11
+
12
+ func small() {
13
+ go func() {
14
+ fmt.Println(http.ListenAndServe("localhost:7070", nil))
15
+ }()
16
+ }
17
+
18
+ func large(x int) int {
19
+ if x == 0 {
20
+ x += 0
21
+ } else if x == 1 {
22
+ x += 1
23
+ } else if x == 2 {
24
+ x += 2
25
+ } else if x == 3 {
26
+ x += 3
27
+ } else if x == 4 {
28
+ x += 4
29
+ } else if x == 5 {
30
+ x += 5
31
+ } else if x == 6 {
32
+ x += 6
33
+ } else if x == 7 {
34
+ x += 7
35
+ } else if x == 8 {
36
+ x += 8
37
+ } else if x == 9 {
38
+ x += 9
39
+ } else if x == 10 {
40
+ x += 10
41
+ } else if x == 11 {
42
+ x += 11
43
+ } else if x == 12 {
44
+ x += 12
45
+ } else if x == 13 {
46
+ x += 13
47
+ } else if x == 14 {
48
+ x += 14
49
+ } else if x == 15 {
50
+ x += 15
51
+ } else if x == 16 {
52
+ x += 16
53
+ } else if x == 17 {
54
+ x += 17
55
+ } else if x == 18 {
56
+ x += 18
57
+ } else if x == 19 {
58
+ x += 19
59
+ } else if x == 20 {
60
+ x += 20
61
+ } else if x == 21 {
62
+ x += 21
63
+ } else if x == 22 {
64
+ x += 22
65
+ } else if x == 23 {
66
+ x += 23
67
+ } else if x == 24 {
68
+ x += 24
69
+ } else if x == 25 {
70
+ x += 25
71
+ } else if x == 26 {
72
+ x += 26
73
+ } else if x == 27 {
74
+ x += 27
75
+ } else if x == 28 {
76
+ x += 28
77
+ } else if x == 29 {
78
+ x += 29
79
+ } else if x == 30 {
80
+ x += 30
81
+ } else if x == 31 {
82
+ x += 31
83
+ } else if x == 32 {
84
+ x += 32
85
+ } else if x == 33 {
86
+ x += 33
87
+ } else if x == 34 {
88
+ x += 34
89
+ } else if x == 35 {
90
+ x += 35
91
+ } else if x == 36 {
92
+ x += 36
93
+ } else if x == 37 {
94
+ x += 37
95
+ } else if x == 38 {
96
+ x += 38
97
+ } else if x == 39 {
98
+ x += 39
99
+ } else if x == 40 {
100
+ x += 40
101
+ } else if x == 41 {
102
+ x += 41
103
+ } else if x == 42 {
104
+ x += 42
105
+ } else if x == 43 {
106
+ x += 43
107
+ } else if x == 44 {
108
+ x += 44
109
+ } else if x == 45 {
110
+ x += 45
111
+ } else if x == 46 {
112
+ x += 46
113
+ } else if x == 47 {
114
+ x += 47
115
+ } else if x == 48 {
116
+ x += 48
117
+ } else if x == 49 {
118
+ x += 49
119
+ } else if x == 50 {
120
+ x += 50
121
+ } else if x == 51 {
122
+ x += 51
123
+ } else if x == 52 {
124
+ x += 52
125
+ } else if x == 53 {
126
+ x += 53
127
+ } else if x == 54 {
128
+ x += 54
129
+ } else if x == 55 {
130
+ x += 55
131
+ } else if x == 56 {
132
+ x += 56
133
+ } else if x == 57 {
134
+ x += 57
135
+ } else if x == 58 {
136
+ x += 58
137
+ } else if x == 59 {
138
+ x += 59
139
+ } else if x == 60 {
140
+ x += 60
141
+ } else if x == 61 {
142
+ x += 61
143
+ } else if x == 62 {
144
+ x += 62
145
+ } else if x == 63 {
146
+ x += 63
147
+ } else if x == 64 {
148
+ x += 64
149
+ } else if x == 65 {
150
+ x += 65
151
+ } else if x == 66 {
152
+ x += 66
153
+ } else if x == 67 {
154
+ x += 67
155
+ } else if x == 68 {
156
+ x += 68
157
+ } else if x == 69 {
158
+ x += 69
159
+ } else if x == 70 {
160
+ x += 70
161
+ } else if x == 71 {
162
+ x += 71
163
+ } else if x == 72 {
164
+ x += 72
165
+ } else if x == 73 {
166
+ x += 73
167
+ } else if x == 74 {
168
+ x += 74
169
+ } else if x == 75 {
170
+ x += 75
171
+ } else if x == 76 {
172
+ x += 76
173
+ } else if x == 77 {
174
+ x += 77
175
+ } else if x == 78 {
176
+ x += 78
177
+ } else if x == 79 {
178
+ x += 79
179
+ } else if x == 80 {
180
+ x += 80
181
+ } else if x == 81 {
182
+ x += 81
183
+ } else if x == 82 {
184
+ x += 82
185
+ } else if x == 83 {
186
+ x += 83
187
+ } else if x == 84 {
188
+ x += 84
189
+ } else if x == 85 {
190
+ x += 85
191
+ } else if x == 86 {
192
+ x += 86
193
+ } else if x == 87 {
194
+ x += 87
195
+ } else if x == 88 {
196
+ x += 88
197
+ } else if x == 89 {
198
+ x += 89
199
+ } else if x == 90 {
200
+ x += 90
201
+ } else if x == 91 {
202
+ x += 91
203
+ } else if x == 92 {
204
+ x += 92
205
+ } else if x == 93 {
206
+ x += 93
207
+ } else if x == 94 {
208
+ x += 94
209
+ } else if x == 95 {
210
+ x += 95
211
+ } else if x == 96 {
212
+ x += 96
213
+ } else if x == 97 {
214
+ x += 97
215
+ } else if x == 98 {
216
+ x += 98
217
+ } else if x == 99 {
218
+ x += 99
219
+ } else if x == 100 {
220
+ x += 100
221
+ } else if x == 101 {
222
+ x += 101
223
+ } else if x == 102 {
224
+ x += 102
225
+ } else if x == 103 {
226
+ x += 103
227
+ } else if x == 104 {
228
+ x += 104
229
+ } else if x == 105 {
230
+ x += 105
231
+ } else if x == 106 {
232
+ x += 106
233
+ } else if x == 107 {
234
+ x += 107
235
+ } else if x == 108 {
236
+ x += 108
237
+ } else if x == 109 {
238
+ x += 109
239
+ } else if x == 110 {
240
+ x += 110
241
+ } else if x == 111 {
242
+ x += 111
243
+ } else if x == 112 {
244
+ x += 112
245
+ } else if x == 113 {
246
+ x += 113
247
+ } else if x == 114 {
248
+ x += 114
249
+ } else if x == 115 {
250
+ x += 115
251
+ } else if x == 116 {
252
+ x += 116
253
+ } else if x == 117 {
254
+ x += 117
255
+ } else if x == 118 {
256
+ x += 118
257
+ } else if x == 119 {
258
+ x += 119
259
+ } else if x == 120 {
260
+ x += 120
261
+ } else if x == 121 {
262
+ x += 121
263
+ } else if x == 122 {
264
+ x += 122
265
+ } else if x == 123 {
266
+ x += 123
267
+ } else if x == 124 {
268
+ x += 124
269
+ } else if x == 125 {
270
+ x += 125
271
+ } else if x == 126 {
272
+ x += 126
273
+ } else if x == 127 {
274
+ x += 127
275
+ } else if x == 128 {
276
+ x += 128
277
+ } else if x == 129 {
278
+ x += 129
279
+ } else if x == 130 {
280
+ x += 130
281
+ } else if x == 131 {
282
+ x += 131
283
+ } else if x == 132 {
284
+ x += 132
285
+ } else if x == 133 {
286
+ x += 133
287
+ } else if x == 134 {
288
+ x += 134
289
+ } else if x == 135 {
290
+ x += 135
291
+ } else if x == 136 {
292
+ x += 136
293
+ } else if x == 137 {
294
+ x += 137
295
+ } else if x == 138 {
296
+ x += 138
297
+ } else if x == 139 {
298
+ x += 139
299
+ } else if x == 140 {
300
+ x += 140
301
+ } else if x == 141 {
302
+ x += 141
303
+ } else if x == 142 {
304
+ x += 142
305
+ } else if x == 143 {
306
+ x += 143
307
+ } else if x == 144 {
308
+ x += 144
309
+ } else if x == 145 {
310
+ x += 145
311
+ } else if x == 146 {
312
+ x += 146
313
+ } else if x == 147 {
314
+ x += 147
315
+ } else if x == 148 {
316
+ x += 148
317
+ } else if x == 149 {
318
+ x += 149
319
+ } else if x == 150 {
320
+ x += 150
321
+ } else if x == 151 {
322
+ x += 151
323
+ } else if x == 152 {
324
+ x += 152
325
+ } else if x == 153 {
326
+ x += 153
327
+ } else if x == 154 {
328
+ x += 154
329
+ } else if x == 155 {
330
+ x += 155
331
+ } else if x == 156 {
332
+ x += 156
333
+ } else if x == 157 {
334
+ x += 157
335
+ } else if x == 158 {
336
+ x += 158
337
+ } else if x == 159 {
338
+ x += 159
339
+ } else if x == 160 {
340
+ x += 160
341
+ } else if x == 161 {
342
+ x += 161
343
+ } else if x == 162 {
344
+ x += 162
345
+ } else if x == 163 {
346
+ x += 163
347
+ } else if x == 164 {
348
+ x += 164
349
+ } else if x == 165 {
350
+ x += 165
351
+ } else if x == 166 {
352
+ x += 166
353
+ } else if x == 167 {
354
+ x += 167
355
+ } else if x == 168 {
356
+ x += 168
357
+ } else if x == 169 {
358
+ x += 169
359
+ } else if x == 170 {
360
+ x += 170
361
+ } else if x == 171 {
362
+ x += 171
363
+ } else if x == 172 {
364
+ x += 172
365
+ } else if x == 173 {
366
+ x += 173
367
+ } else if x == 174 {
368
+ x += 174
369
+ } else if x == 175 {
370
+ x += 175
371
+ } else if x == 176 {
372
+ x += 176
373
+ } else if x == 177 {
374
+ x += 177
375
+ } else if x == 178 {
376
+ x += 178
377
+ } else if x == 179 {
378
+ x += 179
379
+ } else if x == 180 {
380
+ x += 180
381
+ } else if x == 181 {
382
+ x += 181
383
+ } else if x == 182 {
384
+ x += 182
385
+ } else if x == 183 {
386
+ x += 183
387
+ } else if x == 184 {
388
+ x += 184
389
+ } else if x == 185 {
390
+ x += 185
391
+ } else if x == 186 {
392
+ x += 186
393
+ } else if x == 187 {
394
+ x += 187
395
+ } else if x == 188 {
396
+ x += 188
397
+ } else if x == 189 {
398
+ x += 189
399
+ } else if x == 190 {
400
+ x += 190
401
+ } else if x == 191 {
402
+ x += 191
403
+ } else if x == 192 {
404
+ x += 192
405
+ } else if x == 193 {
406
+ x += 193
407
+ } else if x == 194 {
408
+ x += 194
409
+ } else if x == 195 {
410
+ x += 195
411
+ } else if x == 196 {
412
+ x += 196
413
+ } else if x == 197 {
414
+ x += 197
415
+ } else if x == 198 {
416
+ x += 198
417
+ } else if x == 199 {
418
+ x += 199
419
+ } else if x == 200 {
420
+ x += 200
421
+ } else if x == 201 {
422
+ x += 201
423
+ } else if x == 202 {
424
+ x += 202
425
+ } else if x == 203 {
426
+ x += 203
427
+ } else if x == 204 {
428
+ x += 204
429
+ } else if x == 205 {
430
+ x += 205
431
+ } else if x == 206 {
432
+ x += 206
433
+ } else if x == 207 {
434
+ x += 207
435
+ } else if x == 208 {
436
+ x += 208
437
+ } else if x == 209 {
438
+ x += 209
439
+ } else if x == 210 {
440
+ x += 210
441
+ } else if x == 211 {
442
+ x += 211
443
+ } else if x == 212 {
444
+ x += 212
445
+ } else if x == 213 {
446
+ x += 213
447
+ } else if x == 214 {
448
+ x += 214
449
+ } else if x == 215 {
450
+ x += 215
451
+ } else if x == 216 {
452
+ x += 216
453
+ } else if x == 217 {
454
+ x += 217
455
+ } else if x == 218 {
456
+ x += 218
457
+ } else if x == 219 {
458
+ x += 219
459
+ } else if x == 220 {
460
+ x += 220
461
+ } else if x == 221 {
462
+ x += 221
463
+ } else if x == 222 {
464
+ x += 222
465
+ } else if x == 223 {
466
+ x += 223
467
+ } else if x == 224 {
468
+ x += 224
469
+ } else if x == 225 {
470
+ x += 225
471
+ } else if x == 226 {
472
+ x += 226
473
+ } else if x == 227 {
474
+ x += 227
475
+ } else if x == 228 {
476
+ x += 228
477
+ } else if x == 229 {
478
+ x += 229
479
+ } else if x == 230 {
480
+ x += 230
481
+ } else if x == 231 {
482
+ x += 231
483
+ } else if x == 232 {
484
+ x += 232
485
+ } else if x == 233 {
486
+ x += 233
487
+ } else if x == 234 {
488
+ x += 234
489
+ } else if x == 235 {
490
+ x += 235
491
+ } else if x == 236 {
492
+ x += 236
493
+ } else if x == 237 {
494
+ x += 237
495
+ } else if x == 238 {
496
+ x += 238
497
+ } else if x == 239 {
498
+ x += 239
499
+ } else if x == 240 {
500
+ x += 240
501
+ } else if x == 241 {
502
+ x += 241
503
+ } else if x == 242 {
504
+ x += 242
505
+ } else if x == 243 {
506
+ x += 243
507
+ } else if x == 244 {
508
+ x += 244
509
+ } else if x == 245 {
510
+ x += 245
511
+ } else if x == 246 {
512
+ x += 246
513
+ } else if x == 247 {
514
+ x += 247
515
+ } else if x == 248 {
516
+ x += 248
517
+ } else if x == 249 {
518
+ x += 249
519
+ } else if x == 250 {
520
+ x += 250
521
+ } else if x == 251 {
522
+ x += 251
523
+ } else if x == 252 {
524
+ x += 252
525
+ } else if x == 253 {
526
+ x += 253
527
+ } else if x == 254 {
528
+ x += 254
529
+ } else if x == 255 {
530
+ x += 255
531
+ } else if x == 256 {
532
+ x += 256
533
+ } else if x == 257 {
534
+ x += 257
535
+ } else if x == 258 {
536
+ x += 258
537
+ } else if x == 259 {
538
+ x += 259
539
+ } else if x == 260 {
540
+ x += 260
541
+ } else if x == 261 {
542
+ x += 261
543
+ } else if x == 262 {
544
+ x += 262
545
+ } else if x == 263 {
546
+ x += 263
547
+ } else if x == 264 {
548
+ x += 264
549
+ } else if x == 265 {
550
+ x += 265
551
+ } else if x == 266 {
552
+ x += 266
553
+ } else if x == 267 {
554
+ x += 267
555
+ } else if x == 268 {
556
+ x += 268
557
+ } else if x == 269 {
558
+ x += 269
559
+ } else if x == 270 {
560
+ x += 270
561
+ } else if x == 271 {
562
+ x += 271
563
+ } else if x == 272 {
564
+ x += 272
565
+ } else if x == 273 {
566
+ x += 273
567
+ } else if x == 274 {
568
+ x += 274
569
+ } else if x == 275 {
570
+ x += 275
571
+ } else if x == 276 {
572
+ x += 276
573
+ } else if x == 277 {
574
+ x += 277
575
+ } else if x == 278 {
576
+ x += 278
577
+ } else if x == 279 {
578
+ x += 279
579
+ } else if x == 280 {
580
+ x += 280
581
+ } else if x == 281 {
582
+ x += 281
583
+ } else if x == 282 {
584
+ x += 282
585
+ } else if x == 283 {
586
+ x += 283
587
+ } else if x == 284 {
588
+ x += 284
589
+ } else if x == 285 {
590
+ x += 285
591
+ } else if x == 286 {
592
+ x += 286
593
+ } else if x == 287 {
594
+ x += 287
595
+ } else if x == 288 {
596
+ x += 288
597
+ } else if x == 289 {
598
+ x += 289
599
+ } else if x == 290 {
600
+ x += 290
601
+ } else if x == 291 {
602
+ x += 291
603
+ } else if x == 292 {
604
+ x += 292
605
+ } else if x == 293 {
606
+ x += 293
607
+ } else if x == 294 {
608
+ x += 294
609
+ } else if x == 295 {
610
+ x += 295
611
+ } else if x == 296 {
612
+ x += 296
613
+ } else if x == 297 {
614
+ x += 297
615
+ } else if x == 298 {
616
+ x += 298
617
+ } else if x == 299 {
618
+ x += 299
619
+ } else if x == 300 {
620
+ x += 300
621
+ } else if x == 301 {
622
+ x += 301
623
+ } else if x == 302 {
624
+ x += 302
625
+ } else if x == 303 {
626
+ x += 303
627
+ } else if x == 304 {
628
+ x += 304
629
+ } else if x == 305 {
630
+ x += 305
631
+ } else if x == 306 {
632
+ x += 306
633
+ } else if x == 307 {
634
+ x += 307
635
+ } else if x == 308 {
636
+ x += 308
637
+ } else if x == 309 {
638
+ x += 309
639
+ } else if x == 310 {
640
+ x += 310
641
+ } else if x == 311 {
642
+ x += 311
643
+ } else if x == 312 {
644
+ x += 312
645
+ } else if x == 313 {
646
+ x += 313
647
+ } else if x == 314 {
648
+ x += 314
649
+ } else if x == 315 {
650
+ x += 315
651
+ } else if x == 316 {
652
+ x += 316
653
+ } else if x == 317 {
654
+ x += 317
655
+ } else if x == 318 {
656
+ x += 318
657
+ } else if x == 319 {
658
+ x += 319
659
+ } else if x == 320 {
660
+ x += 320
661
+ } else if x == 321 {
662
+ x += 321
663
+ } else if x == 322 {
664
+ x += 322
665
+ } else if x == 323 {
666
+ x += 323
667
+ } else if x == 324 {
668
+ x += 324
669
+ } else if x == 325 {
670
+ x += 325
671
+ } else if x == 326 {
672
+ x += 326
673
+ } else if x == 327 {
674
+ x += 327
675
+ } else if x == 328 {
676
+ x += 328
677
+ } else if x == 329 {
678
+ x += 329
679
+ } else if x == 330 {
680
+ x += 330
681
+ } else if x == 331 {
682
+ x += 331
683
+ } else if x == 332 {
684
+ x += 332
685
+ } else if x == 333 {
686
+ x += 333
687
+ } else if x == 334 {
688
+ x += 334
689
+ } else if x == 335 {
690
+ x += 335
691
+ } else if x == 336 {
692
+ x += 336
693
+ } else if x == 337 {
694
+ x += 337
695
+ } else if x == 338 {
696
+ x += 338
697
+ } else if x == 339 {
698
+ x += 339
699
+ } else if x == 340 {
700
+ x += 340
701
+ } else if x == 341 {
702
+ x += 341
703
+ } else if x == 342 {
704
+ x += 342
705
+ } else if x == 343 {
706
+ x += 343
707
+ } else if x == 344 {
708
+ x += 344
709
+ } else if x == 345 {
710
+ x += 345
711
+ } else if x == 346 {
712
+ x += 346
713
+ } else if x == 347 {
714
+ x += 347
715
+ } else if x == 348 {
716
+ x += 348
717
+ } else if x == 349 {
718
+ x += 349
719
+ } else if x == 350 {
720
+ x += 350
721
+ } else if x == 351 {
722
+ x += 351
723
+ } else if x == 352 {
724
+ x += 352
725
+ } else if x == 353 {
726
+ x += 353
727
+ } else if x == 354 {
728
+ x += 354
729
+ } else if x == 355 {
730
+ x += 355
731
+ } else if x == 356 {
732
+ x += 356
733
+ } else if x == 357 {
734
+ x += 357
735
+ } else if x == 358 {
736
+ x += 358
737
+ } else if x == 359 {
738
+ x += 359
739
+ } else if x == 360 {
740
+ x += 360
741
+ } else if x == 361 {
742
+ x += 361
743
+ } else if x == 362 {
744
+ x += 362
745
+ } else if x == 363 {
746
+ x += 363
747
+ } else if x == 364 {
748
+ x += 364
749
+ } else if x == 365 {
750
+ x += 365
751
+ } else if x == 366 {
752
+ x += 366
753
+ } else if x == 367 {
754
+ x += 367
755
+ } else if x == 368 {
756
+ x += 368
757
+ } else if x == 369 {
758
+ x += 369
759
+ } else if x == 370 {
760
+ x += 370
761
+ } else if x == 371 {
762
+ x += 371
763
+ } else if x == 372 {
764
+ x += 372
765
+ } else if x == 373 {
766
+ x += 373
767
+ } else if x == 374 {
768
+ x += 374
769
+ } else if x == 375 {
770
+ x += 375
771
+ } else if x == 376 {
772
+ x += 376
773
+ } else if x == 377 {
774
+ x += 377
775
+ } else if x == 378 {
776
+ x += 378
777
+ } else if x == 379 {
778
+ x += 379
779
+ } else if x == 380 {
780
+ x += 380
781
+ } else if x == 381 {
782
+ x += 381
783
+ } else if x == 382 {
784
+ x += 382
785
+ } else if x == 383 {
786
+ x += 383
787
+ } else if x == 384 {
788
+ x += 384
789
+ } else if x == 385 {
790
+ x += 385
791
+ } else if x == 386 {
792
+ x += 386
793
+ } else if x == 387 {
794
+ x += 387
795
+ } else if x == 388 {
796
+ x += 388
797
+ } else if x == 389 {
798
+ x += 389
799
+ } else if x == 390 {
800
+ x += 390
801
+ } else if x == 391 {
802
+ x += 391
803
+ } else if x == 392 {
804
+ x += 392
805
+ } else if x == 393 {
806
+ x += 393
807
+ } else if x == 394 {
808
+ x += 394
809
+ } else if x == 395 {
810
+ x += 395
811
+ } else if x == 396 {
812
+ x += 396
813
+ } else if x == 397 {
814
+ x += 397
815
+ } else if x == 398 {
816
+ x += 398
817
+ } else if x == 399 {
818
+ x += 399
819
+ } else if x == 400 {
820
+ x += 400
821
+ }
822
+ return x * x
823
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/testdata/issue59563/repro_test.go ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package repro
6
+
7
+ import "testing"
8
+
9
+ func TestSomething(t *testing.T) {
10
+ small()
11
+ for i := 0; i < 1001; i++ {
12
+ large(i)
13
+ }
14
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/testsupport.go ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import (
8
+ "encoding/json"
9
+ "fmt"
10
+ "internal/coverage"
11
+ "internal/coverage/calloc"
12
+ "internal/coverage/cformat"
13
+ "internal/coverage/cmerge"
14
+ "internal/coverage/decodecounter"
15
+ "internal/coverage/decodemeta"
16
+ "internal/coverage/pods"
17
+ "io"
18
+ "os"
19
+ "path/filepath"
20
+ "runtime/internal/atomic"
21
+ "strings"
22
+ "unsafe"
23
+ )
24
+
25
+ // processCoverTestDir is called (via a linknamed reference) from
26
+ // testmain code when "go test -cover" is in effect. It is not
27
+ // intended to be used other than internally by the Go command's
28
+ // generated code.
29
+ func processCoverTestDir(dir string, cfile string, cm string, cpkg string) error {
30
+ return processCoverTestDirInternal(dir, cfile, cm, cpkg, os.Stdout)
31
+ }
32
+
33
+ // processCoverTestDirInternal is an io.Writer version of processCoverTestDir,
34
+ // exposed for unit testing.
35
+ func processCoverTestDirInternal(dir string, cfile string, cm string, cpkg string, w io.Writer) error {
36
+ cmode := coverage.ParseCounterMode(cm)
37
+ if cmode == coverage.CtrModeInvalid {
38
+ return fmt.Errorf("invalid counter mode %q", cm)
39
+ }
40
+
41
+ // Emit meta-data and counter data.
42
+ ml := getCovMetaList()
43
+ if len(ml) == 0 {
44
+ // This corresponds to the case where we have a package that
45
+ // contains test code but no functions (which is fine). In this
46
+ // case there is no need to emit anything.
47
+ } else {
48
+ if err := emitMetaDataToDirectory(dir, ml); err != nil {
49
+ return err
50
+ }
51
+ if err := emitCounterDataToDirectory(dir); err != nil {
52
+ return err
53
+ }
54
+ }
55
+
56
+ // Collect pods from test run. For the majority of cases we would
57
+ // expect to see a single pod here, but allow for multiple pods in
58
+ // case the test harness is doing extra work to collect data files
59
+ // from builds that it kicks off as part of the testing.
60
+ podlist, err := pods.CollectPods([]string{dir}, false)
61
+ if err != nil {
62
+ return fmt.Errorf("reading from %s: %v", dir, err)
63
+ }
64
+
65
+ // Open text output file if appropriate.
66
+ var tf *os.File
67
+ var tfClosed bool
68
+ if cfile != "" {
69
+ var err error
70
+ tf, err = os.Create(cfile)
71
+ if err != nil {
72
+ return fmt.Errorf("internal error: opening coverage data output file %q: %v", cfile, err)
73
+ }
74
+ defer func() {
75
+ if !tfClosed {
76
+ tfClosed = true
77
+ tf.Close()
78
+ }
79
+ }()
80
+ }
81
+
82
+ // Read/process the pods.
83
+ ts := &tstate{
84
+ cm: &cmerge.Merger{},
85
+ cf: cformat.NewFormatter(cmode),
86
+ cmode: cmode,
87
+ }
88
+ // Generate the expected hash string based on the final meta-data
89
+ // hash for this test, then look only for pods that refer to that
90
+ // hash (just in case there are multiple instrumented executables
91
+ // in play). See issue #57924 for more on this.
92
+ hashstring := fmt.Sprintf("%x", finalHash)
93
+ importpaths := make(map[string]struct{})
94
+ for _, p := range podlist {
95
+ if !strings.Contains(p.MetaFile, hashstring) {
96
+ continue
97
+ }
98
+ if err := ts.processPod(p, importpaths); err != nil {
99
+ return err
100
+ }
101
+ }
102
+
103
+ metafilespath := filepath.Join(dir, coverage.MetaFilesFileName)
104
+ if _, err := os.Stat(metafilespath); err == nil {
105
+ if err := ts.readAuxMetaFiles(metafilespath, importpaths); err != nil {
106
+ return err
107
+ }
108
+ }
109
+
110
+ // Emit percent.
111
+ if err := ts.cf.EmitPercent(w, cpkg, true, true); err != nil {
112
+ return err
113
+ }
114
+
115
+ // Emit text output.
116
+ if tf != nil {
117
+ if err := ts.cf.EmitTextual(tf); err != nil {
118
+ return err
119
+ }
120
+ tfClosed = true
121
+ if err := tf.Close(); err != nil {
122
+ return fmt.Errorf("closing %s: %v", cfile, err)
123
+ }
124
+ }
125
+
126
+ return nil
127
+ }
128
+
129
+ type tstate struct {
130
+ calloc.BatchCounterAlloc
131
+ cm *cmerge.Merger
132
+ cf *cformat.Formatter
133
+ cmode coverage.CounterMode
134
+ }
135
+
136
+ // processPod reads coverage counter data for a specific pod.
137
+ func (ts *tstate) processPod(p pods.Pod, importpaths map[string]struct{}) error {
138
+ // Open meta-data file
139
+ f, err := os.Open(p.MetaFile)
140
+ if err != nil {
141
+ return fmt.Errorf("unable to open meta-data file %s: %v", p.MetaFile, err)
142
+ }
143
+ defer func() {
144
+ f.Close()
145
+ }()
146
+ var mfr *decodemeta.CoverageMetaFileReader
147
+ mfr, err = decodemeta.NewCoverageMetaFileReader(f, nil)
148
+ if err != nil {
149
+ return fmt.Errorf("error reading meta-data file %s: %v", p.MetaFile, err)
150
+ }
151
+ newmode := mfr.CounterMode()
152
+ if newmode != ts.cmode {
153
+ return fmt.Errorf("internal error: counter mode clash: %q from test harness, %q from data file %s", ts.cmode.String(), newmode.String(), p.MetaFile)
154
+ }
155
+ newgran := mfr.CounterGranularity()
156
+ if err := ts.cm.SetModeAndGranularity(p.MetaFile, cmode, newgran); err != nil {
157
+ return err
158
+ }
159
+
160
+ // A map to store counter data, indexed by pkgid/fnid tuple.
161
+ pmm := make(map[pkfunc][]uint32)
162
+
163
+ // Helper to read a single counter data file.
164
+ readcdf := func(cdf string) error {
165
+ cf, err := os.Open(cdf)
166
+ if err != nil {
167
+ return fmt.Errorf("opening counter data file %s: %s", cdf, err)
168
+ }
169
+ defer cf.Close()
170
+ var cdr *decodecounter.CounterDataReader
171
+ cdr, err = decodecounter.NewCounterDataReader(cdf, cf)
172
+ if err != nil {
173
+ return fmt.Errorf("reading counter data file %s: %s", cdf, err)
174
+ }
175
+ var data decodecounter.FuncPayload
176
+ for {
177
+ ok, err := cdr.NextFunc(&data)
178
+ if err != nil {
179
+ return fmt.Errorf("reading counter data file %s: %v", cdf, err)
180
+ }
181
+ if !ok {
182
+ break
183
+ }
184
+
185
+ // NB: sanity check on pkg and func IDs?
186
+ key := pkfunc{pk: data.PkgIdx, fcn: data.FuncIdx}
187
+ if prev, found := pmm[key]; found {
188
+ // Note: no overflow reporting here.
189
+ if err, _ := ts.cm.MergeCounters(data.Counters, prev); err != nil {
190
+ return fmt.Errorf("processing counter data file %s: %v", cdf, err)
191
+ }
192
+ }
193
+ c := ts.AllocateCounters(len(data.Counters))
194
+ copy(c, data.Counters)
195
+ pmm[key] = c
196
+ }
197
+ return nil
198
+ }
199
+
200
+ // Read counter data files.
201
+ for _, cdf := range p.CounterDataFiles {
202
+ if err := readcdf(cdf); err != nil {
203
+ return err
204
+ }
205
+ }
206
+
207
+ // Visit meta-data file.
208
+ np := uint32(mfr.NumPackages())
209
+ payload := []byte{}
210
+ for pkIdx := uint32(0); pkIdx < np; pkIdx++ {
211
+ var pd *decodemeta.CoverageMetaDataDecoder
212
+ pd, payload, err = mfr.GetPackageDecoder(pkIdx, payload)
213
+ if err != nil {
214
+ return fmt.Errorf("reading pkg %d from meta-file %s: %s", pkIdx, p.MetaFile, err)
215
+ }
216
+ ts.cf.SetPackage(pd.PackagePath())
217
+ importpaths[pd.PackagePath()] = struct{}{}
218
+ var fd coverage.FuncDesc
219
+ nf := pd.NumFuncs()
220
+ for fnIdx := uint32(0); fnIdx < nf; fnIdx++ {
221
+ if err := pd.ReadFunc(fnIdx, &fd); err != nil {
222
+ return fmt.Errorf("reading meta-data file %s: %v",
223
+ p.MetaFile, err)
224
+ }
225
+ key := pkfunc{pk: pkIdx, fcn: fnIdx}
226
+ counters, haveCounters := pmm[key]
227
+ for i := 0; i < len(fd.Units); i++ {
228
+ u := fd.Units[i]
229
+ // Skip units with non-zero parent (no way to represent
230
+ // these in the existing format).
231
+ if u.Parent != 0 {
232
+ continue
233
+ }
234
+ count := uint32(0)
235
+ if haveCounters {
236
+ count = counters[i]
237
+ }
238
+ ts.cf.AddUnit(fd.Srcfile, fd.Funcname, fd.Lit, u, count)
239
+ }
240
+ }
241
+ }
242
+ return nil
243
+ }
244
+
245
+ type pkfunc struct {
246
+ pk, fcn uint32
247
+ }
248
+
249
+ func (ts *tstate) readAuxMetaFiles(metafiles string, importpaths map[string]struct{}) error {
250
+ // Unmarshall the information on available aux metafiles into
251
+ // a MetaFileCollection struct.
252
+ var mfc coverage.MetaFileCollection
253
+ data, err := os.ReadFile(metafiles)
254
+ if err != nil {
255
+ return fmt.Errorf("error reading auxmetafiles file %q: %v", metafiles, err)
256
+ }
257
+ if err := json.Unmarshal(data, &mfc); err != nil {
258
+ return fmt.Errorf("error reading auxmetafiles file %q: %v", metafiles, err)
259
+ }
260
+
261
+ // Walk through each available aux meta-file. If we've already
262
+ // seen the package path in question during the walk of the
263
+ // "regular" meta-data file, then we can skip the package,
264
+ // otherwise construct a dummy pod with the single meta-data file
265
+ // (no counters) and invoke processPod on it.
266
+ for i := range mfc.ImportPaths {
267
+ p := mfc.ImportPaths[i]
268
+ if _, ok := importpaths[p]; ok {
269
+ continue
270
+ }
271
+ var pod pods.Pod
272
+ pod.MetaFile = mfc.MetaFileFragments[i]
273
+ if err := ts.processPod(pod, importpaths); err != nil {
274
+ return err
275
+ }
276
+ }
277
+ return nil
278
+ }
279
+
280
+ // snapshot returns a snapshot of coverage percentage at a moment of
281
+ // time within a running test, so as to support the testing.Coverage()
282
+ // function. This version doesn't examine coverage meta-data, so the
283
+ // result it returns will be less accurate (more "slop") due to the
284
+ // fact that we don't look at the meta data to see how many statements
285
+ // are associated with each counter.
286
+ func snapshot() float64 {
287
+ cl := getCovCounterList()
288
+ if len(cl) == 0 {
289
+ // no work to do here.
290
+ return 0.0
291
+ }
292
+
293
+ tot := uint64(0)
294
+ totExec := uint64(0)
295
+ for _, c := range cl {
296
+ sd := unsafe.Slice((*atomic.Uint32)(unsafe.Pointer(c.Counters)), c.Len)
297
+ tot += uint64(len(sd))
298
+ for i := 0; i < len(sd); i++ {
299
+ // Skip ahead until the next non-zero value.
300
+ if sd[i].Load() == 0 {
301
+ continue
302
+ }
303
+ // We found a function that was executed.
304
+ nCtrs := sd[i+coverage.NumCtrsOffset].Load()
305
+ cst := i + coverage.FirstCtrOffset
306
+
307
+ if cst+int(nCtrs) > len(sd) {
308
+ break
309
+ }
310
+ counters := sd[cst : cst+int(nCtrs)]
311
+ for i := range counters {
312
+ if counters[i].Load() != 0 {
313
+ totExec++
314
+ }
315
+ }
316
+ i += coverage.FirstCtrOffset + int(nCtrs) - 1
317
+ }
318
+ }
319
+ if tot == 0 {
320
+ return 0.0
321
+ }
322
+ return float64(totExec) / float64(tot)
323
+ }
platform/dbops/binaries/go/go/src/runtime/coverage/ts_test.go ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package coverage
6
+
7
+ import (
8
+ "encoding/json"
9
+ "internal/coverage"
10
+ "internal/goexperiment"
11
+ "internal/testenv"
12
+ "os"
13
+ "os/exec"
14
+ "path/filepath"
15
+ "strings"
16
+ "testing"
17
+ _ "unsafe"
18
+ )
19
+
20
+ //go:linkname testing_testGoCoverDir testing.testGoCoverDir
21
+ func testing_testGoCoverDir() string
22
+
23
+ func testGoCoverDir(t *testing.T) string {
24
+ tgcd := testing_testGoCoverDir()
25
+ if tgcd != "" {
26
+ return tgcd
27
+ }
28
+ return t.TempDir()
29
+ }
30
+
31
+ // TestTestSupport does a basic verification of the functionality in
32
+ // runtime/coverage.processCoverTestDir (doing this here as opposed to
33
+ // relying on other test paths will provide a better signal when
34
+ // running "go test -cover" for this package).
35
+ func TestTestSupport(t *testing.T) {
36
+ if !goexperiment.CoverageRedesign {
37
+ return
38
+ }
39
+ if testing.CoverMode() == "" {
40
+ return
41
+ }
42
+ tgcd := testGoCoverDir(t)
43
+ t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
44
+ tgcd, testing.CoverMode())
45
+
46
+ textfile := filepath.Join(t.TempDir(), "file.txt")
47
+ var sb strings.Builder
48
+ err := processCoverTestDirInternal(tgcd, textfile,
49
+ testing.CoverMode(), "", &sb)
50
+ if err != nil {
51
+ t.Fatalf("bad: %v", err)
52
+ }
53
+
54
+ // Check for existence of text file.
55
+ if inf, err := os.Open(textfile); err != nil {
56
+ t.Fatalf("problems opening text file %s: %v", textfile, err)
57
+ } else {
58
+ inf.Close()
59
+ }
60
+
61
+ // Check for percent output with expected tokens.
62
+ strout := sb.String()
63
+ want := "of statements"
64
+ if !strings.Contains(strout, want) {
65
+ t.Logf("output from run: %s\n", strout)
66
+ t.Fatalf("percent output missing token: %q", want)
67
+ }
68
+ }
69
+
70
+ var funcInvoked bool
71
+
72
+ //go:noinline
73
+ func thisFunctionOnlyCalledFromSnapshotTest(n int) int {
74
+ if funcInvoked {
75
+ panic("bad")
76
+ }
77
+ funcInvoked = true
78
+
79
+ // Contents here not especially important, just so long as we
80
+ // have some statements.
81
+ t := 0
82
+ for i := 0; i < n; i++ {
83
+ for j := 0; j < i; j++ {
84
+ t += i ^ j
85
+ }
86
+ }
87
+ return t
88
+ }
89
+
90
+ // Tests runtime/coverage.snapshot() directly. Note that if
91
+ // coverage is not enabled, the hook is designed to just return
92
+ // zero.
93
+ func TestCoverageSnapshot(t *testing.T) {
94
+ C1 := snapshot()
95
+ thisFunctionOnlyCalledFromSnapshotTest(15)
96
+ C2 := snapshot()
97
+ cond := "C1 > C2"
98
+ val := C1 > C2
99
+ if testing.CoverMode() != "" {
100
+ cond = "C1 >= C2"
101
+ val = C1 >= C2
102
+ }
103
+ t.Logf("%f %f\n", C1, C2)
104
+ if val {
105
+ t.Errorf("erroneous snapshots, %s = true C1=%f C2=%f",
106
+ cond, C1, C2)
107
+ }
108
+ }
109
+
110
+ const hellogo = `
111
+ package main
112
+
113
+ func main() {
114
+ println("hello")
115
+ }
116
+ `
117
+
118
+ // Returns a pair F,T where F is a meta-data file generated from
119
+ // "hello.go" above, and T is a token to look for that should be
120
+ // present in the coverage report from F.
121
+ func genAuxMeta(t *testing.T, dstdir string) (string, string) {
122
+ // Do a GOCOVERDIR=<tmp> go run hello.go
123
+ src := filepath.Join(dstdir, "hello.go")
124
+ if err := os.WriteFile(src, []byte(hellogo), 0777); err != nil {
125
+ t.Fatalf("write failed: %v", err)
126
+ }
127
+ args := []string{"run", "-covermode=" + testing.CoverMode(), src}
128
+ cmd := exec.Command(testenv.GoToolPath(t), args...)
129
+ cmd.Env = updateGoCoverDir(os.Environ(), dstdir, true)
130
+ if b, err := cmd.CombinedOutput(); err != nil {
131
+ t.Fatalf("go run failed (%v): %s", err, b)
132
+ }
133
+
134
+ // Pick out the generated meta-data file.
135
+ files, err := os.ReadDir(dstdir)
136
+ if err != nil {
137
+ t.Fatalf("reading %s: %v", dstdir, err)
138
+ }
139
+ for _, f := range files {
140
+ if strings.HasPrefix(f.Name(), "covmeta") {
141
+ return filepath.Join(dstdir, f.Name()), "hello.go:"
142
+ }
143
+ }
144
+ t.Fatalf("could not locate generated meta-data file")
145
+ return "", ""
146
+ }
147
+
148
+ func TestAuxMetaDataFiles(t *testing.T) {
149
+ if !goexperiment.CoverageRedesign {
150
+ return
151
+ }
152
+ if testing.CoverMode() == "" {
153
+ return
154
+ }
155
+ testenv.MustHaveGoRun(t)
156
+ tgcd := testGoCoverDir(t)
157
+ t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
158
+ tgcd, testing.CoverMode())
159
+
160
+ td := t.TempDir()
161
+
162
+ // Manufacture a new, separate meta-data file not related to this
163
+ // test. Contents are not important, just so long as the
164
+ // packages/paths are different.
165
+ othermetadir := filepath.Join(td, "othermeta")
166
+ if err := os.Mkdir(othermetadir, 0777); err != nil {
167
+ t.Fatalf("mkdir failed: %v", err)
168
+ }
169
+ mfile, token := genAuxMeta(t, othermetadir)
170
+
171
+ // Write a metafiles file.
172
+ metafiles := filepath.Join(tgcd, coverage.MetaFilesFileName)
173
+ mfc := coverage.MetaFileCollection{
174
+ ImportPaths: []string{"command-line-arguments"},
175
+ MetaFileFragments: []string{mfile},
176
+ }
177
+ jdata, err := json.Marshal(mfc)
178
+ if err != nil {
179
+ t.Fatalf("marshal MetaFileCollection: %v", err)
180
+ }
181
+ if err := os.WriteFile(metafiles, jdata, 0666); err != nil {
182
+ t.Fatalf("write failed: %v", err)
183
+ }
184
+
185
+ // Kick off guts of test.
186
+ var sb strings.Builder
187
+ textfile := filepath.Join(td, "file2.txt")
188
+ err = processCoverTestDirInternal(tgcd, textfile,
189
+ testing.CoverMode(), "", &sb)
190
+ if err != nil {
191
+ t.Fatalf("bad: %v", err)
192
+ }
193
+ if err = os.Remove(metafiles); err != nil {
194
+ t.Fatalf("removing metafiles file: %v", err)
195
+ }
196
+
197
+ // Look for the expected things in the coverage profile.
198
+ contents, err := os.ReadFile(textfile)
199
+ strc := string(contents)
200
+ if err != nil {
201
+ t.Fatalf("problems reading text file %s: %v", textfile, err)
202
+ }
203
+ if !strings.Contains(strc, token) {
204
+ t.Logf("content: %s\n", string(contents))
205
+ t.Fatalf("cov profile does not contain aux meta content %q", token)
206
+ }
207
+ }
platform/dbops/binaries/go/go/src/runtime/debug/debug.s ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2013 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Nothing to see here.
6
+ // This file exists so that the go command knows that parts of the
7
+ // package are implemented in C, so that it does not instruct the
8
+ // Go compiler to complain about extern declarations.
9
+ // The actual implementations are in package runtime.
platform/dbops/binaries/go/go/src/runtime/debug/garbage.go ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2013 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug
6
+
7
+ import (
8
+ "runtime"
9
+ "sort"
10
+ "time"
11
+ )
12
+
13
+ // GCStats collect information about recent garbage collections.
14
+ type GCStats struct {
15
+ LastGC time.Time // time of last collection
16
+ NumGC int64 // number of garbage collections
17
+ PauseTotal time.Duration // total pause for all collections
18
+ Pause []time.Duration // pause history, most recent first
19
+ PauseEnd []time.Time // pause end times history, most recent first
20
+ PauseQuantiles []time.Duration
21
+ }
22
+
23
+ // ReadGCStats reads statistics about garbage collection into stats.
24
+ // The number of entries in the pause history is system-dependent;
25
+ // stats.Pause slice will be reused if large enough, reallocated otherwise.
26
+ // ReadGCStats may use the full capacity of the stats.Pause slice.
27
+ // If stats.PauseQuantiles is non-empty, ReadGCStats fills it with quantiles
28
+ // summarizing the distribution of pause time. For example, if
29
+ // len(stats.PauseQuantiles) is 5, it will be filled with the minimum,
30
+ // 25%, 50%, 75%, and maximum pause times.
31
+ func ReadGCStats(stats *GCStats) {
32
+ // Create a buffer with space for at least two copies of the
33
+ // pause history tracked by the runtime. One will be returned
34
+ // to the caller and the other will be used as transfer buffer
35
+ // for end times history and as a temporary buffer for
36
+ // computing quantiles.
37
+ const maxPause = len(((*runtime.MemStats)(nil)).PauseNs)
38
+ if cap(stats.Pause) < 2*maxPause+3 {
39
+ stats.Pause = make([]time.Duration, 2*maxPause+3)
40
+ }
41
+
42
+ // readGCStats fills in the pause and end times histories (up to
43
+ // maxPause entries) and then three more: Unix ns time of last GC,
44
+ // number of GC, and total pause time in nanoseconds. Here we
45
+ // depend on the fact that time.Duration's native unit is
46
+ // nanoseconds, so the pauses and the total pause time do not need
47
+ // any conversion.
48
+ readGCStats(&stats.Pause)
49
+ n := len(stats.Pause) - 3
50
+ stats.LastGC = time.Unix(0, int64(stats.Pause[n]))
51
+ stats.NumGC = int64(stats.Pause[n+1])
52
+ stats.PauseTotal = stats.Pause[n+2]
53
+ n /= 2 // buffer holds pauses and end times
54
+ stats.Pause = stats.Pause[:n]
55
+
56
+ if cap(stats.PauseEnd) < maxPause {
57
+ stats.PauseEnd = make([]time.Time, 0, maxPause)
58
+ }
59
+ stats.PauseEnd = stats.PauseEnd[:0]
60
+ for _, ns := range stats.Pause[n : n+n] {
61
+ stats.PauseEnd = append(stats.PauseEnd, time.Unix(0, int64(ns)))
62
+ }
63
+
64
+ if len(stats.PauseQuantiles) > 0 {
65
+ if n == 0 {
66
+ for i := range stats.PauseQuantiles {
67
+ stats.PauseQuantiles[i] = 0
68
+ }
69
+ } else {
70
+ // There's room for a second copy of the data in stats.Pause.
71
+ // See the allocation at the top of the function.
72
+ sorted := stats.Pause[n : n+n]
73
+ copy(sorted, stats.Pause)
74
+ sort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] })
75
+ nq := len(stats.PauseQuantiles) - 1
76
+ for i := 0; i < nq; i++ {
77
+ stats.PauseQuantiles[i] = sorted[len(sorted)*i/nq]
78
+ }
79
+ stats.PauseQuantiles[nq] = sorted[len(sorted)-1]
80
+ }
81
+ }
82
+ }
83
+
84
+ // SetGCPercent sets the garbage collection target percentage:
85
+ // a collection is triggered when the ratio of freshly allocated data
86
+ // to live data remaining after the previous collection reaches this percentage.
87
+ // SetGCPercent returns the previous setting.
88
+ // The initial setting is the value of the GOGC environment variable
89
+ // at startup, or 100 if the variable is not set.
90
+ // This setting may be effectively reduced in order to maintain a memory
91
+ // limit.
92
+ // A negative percentage effectively disables garbage collection, unless
93
+ // the memory limit is reached.
94
+ // See SetMemoryLimit for more details.
95
+ func SetGCPercent(percent int) int {
96
+ return int(setGCPercent(int32(percent)))
97
+ }
98
+
99
+ // FreeOSMemory forces a garbage collection followed by an
100
+ // attempt to return as much memory to the operating system
101
+ // as possible. (Even if this is not called, the runtime gradually
102
+ // returns memory to the operating system in a background task.)
103
+ func FreeOSMemory() {
104
+ freeOSMemory()
105
+ }
106
+
107
+ // SetMaxStack sets the maximum amount of memory that
108
+ // can be used by a single goroutine stack.
109
+ // If any goroutine exceeds this limit while growing its stack,
110
+ // the program crashes.
111
+ // SetMaxStack returns the previous setting.
112
+ // The initial setting is 1 GB on 64-bit systems, 250 MB on 32-bit systems.
113
+ // There may be a system-imposed maximum stack limit regardless
114
+ // of the value provided to SetMaxStack.
115
+ //
116
+ // SetMaxStack is useful mainly for limiting the damage done by
117
+ // goroutines that enter an infinite recursion. It only limits future
118
+ // stack growth.
119
+ func SetMaxStack(bytes int) int {
120
+ return setMaxStack(bytes)
121
+ }
122
+
123
+ // SetMaxThreads sets the maximum number of operating system
124
+ // threads that the Go program can use. If it attempts to use more than
125
+ // this many, the program crashes.
126
+ // SetMaxThreads returns the previous setting.
127
+ // The initial setting is 10,000 threads.
128
+ //
129
+ // The limit controls the number of operating system threads, not the number
130
+ // of goroutines. A Go program creates a new thread only when a goroutine
131
+ // is ready to run but all the existing threads are blocked in system calls, cgo calls,
132
+ // or are locked to other goroutines due to use of runtime.LockOSThread.
133
+ //
134
+ // SetMaxThreads is useful mainly for limiting the damage done by
135
+ // programs that create an unbounded number of threads. The idea is
136
+ // to take down the program before it takes down the operating system.
137
+ func SetMaxThreads(threads int) int {
138
+ return setMaxThreads(threads)
139
+ }
140
+
141
+ // SetPanicOnFault controls the runtime's behavior when a program faults
142
+ // at an unexpected (non-nil) address. Such faults are typically caused by
143
+ // bugs such as runtime memory corruption, so the default response is to crash
144
+ // the program. Programs working with memory-mapped files or unsafe
145
+ // manipulation of memory may cause faults at non-nil addresses in less
146
+ // dramatic situations; SetPanicOnFault allows such programs to request
147
+ // that the runtime trigger only a panic, not a crash.
148
+ // The runtime.Error that the runtime panics with may have an additional method:
149
+ //
150
+ // Addr() uintptr
151
+ //
152
+ // If that method exists, it returns the memory address which triggered the fault.
153
+ // The results of Addr are best-effort and the veracity of the result
154
+ // may depend on the platform.
155
+ // SetPanicOnFault applies only to the current goroutine.
156
+ // It returns the previous setting.
157
+ func SetPanicOnFault(enabled bool) bool {
158
+ return setPanicOnFault(enabled)
159
+ }
160
+
161
+ // WriteHeapDump writes a description of the heap and the objects in
162
+ // it to the given file descriptor.
163
+ //
164
+ // WriteHeapDump suspends the execution of all goroutines until the heap
165
+ // dump is completely written. Thus, the file descriptor must not be
166
+ // connected to a pipe or socket whose other end is in the same Go
167
+ // process; instead, use a temporary file or network socket.
168
+ //
169
+ // The heap dump format is defined at https://golang.org/s/go15heapdump.
170
+ func WriteHeapDump(fd uintptr)
171
+
172
+ // SetTraceback sets the amount of detail printed by the runtime in
173
+ // the traceback it prints before exiting due to an unrecovered panic
174
+ // or an internal runtime error.
175
+ // The level argument takes the same values as the GOTRACEBACK
176
+ // environment variable. For example, SetTraceback("all") ensure
177
+ // that the program prints all goroutines when it crashes.
178
+ // See the package runtime documentation for details.
179
+ // If SetTraceback is called with a level lower than that of the
180
+ // environment variable, the call is ignored.
181
+ func SetTraceback(level string)
182
+
183
+ // SetMemoryLimit provides the runtime with a soft memory limit.
184
+ //
185
+ // The runtime undertakes several processes to try to respect this
186
+ // memory limit, including adjustments to the frequency of garbage
187
+ // collections and returning memory to the underlying system more
188
+ // aggressively. This limit will be respected even if GOGC=off (or,
189
+ // if SetGCPercent(-1) is executed).
190
+ //
191
+ // The input limit is provided as bytes, and includes all memory
192
+ // mapped, managed, and not released by the Go runtime. Notably, it
193
+ // does not account for space used by the Go binary and memory
194
+ // external to Go, such as memory managed by the underlying system
195
+ // on behalf of the process, or memory managed by non-Go code inside
196
+ // the same process. Examples of excluded memory sources include: OS
197
+ // kernel memory held on behalf of the process, memory allocated by
198
+ // C code, and memory mapped by syscall.Mmap (because it is not
199
+ // managed by the Go runtime).
200
+ //
201
+ // More specifically, the following expression accurately reflects
202
+ // the value the runtime attempts to maintain as the limit:
203
+ //
204
+ // runtime.MemStats.Sys - runtime.MemStats.HeapReleased
205
+ //
206
+ // or in terms of the runtime/metrics package:
207
+ //
208
+ // /memory/classes/total:bytes - /memory/classes/heap/released:bytes
209
+ //
210
+ // A zero limit or a limit that's lower than the amount of memory
211
+ // used by the Go runtime may cause the garbage collector to run
212
+ // nearly continuously. However, the application may still make
213
+ // progress.
214
+ //
215
+ // The memory limit is always respected by the Go runtime, so to
216
+ // effectively disable this behavior, set the limit very high.
217
+ // [math.MaxInt64] is the canonical value for disabling the limit,
218
+ // but values much greater than the available memory on the underlying
219
+ // system work just as well.
220
+ //
221
+ // See https://go.dev/doc/gc-guide for a detailed guide explaining
222
+ // the soft memory limit in more detail, as well as a variety of common
223
+ // use-cases and scenarios.
224
+ //
225
+ // The initial setting is math.MaxInt64 unless the GOMEMLIMIT
226
+ // environment variable is set, in which case it provides the initial
227
+ // setting. GOMEMLIMIT is a numeric value in bytes with an optional
228
+ // unit suffix. The supported suffixes include B, KiB, MiB, GiB, and
229
+ // TiB. These suffixes represent quantities of bytes as defined by
230
+ // the IEC 80000-13 standard. That is, they are based on powers of
231
+ // two: KiB means 2^10 bytes, MiB means 2^20 bytes, and so on.
232
+ //
233
+ // SetMemoryLimit returns the previously set memory limit.
234
+ // A negative input does not adjust the limit, and allows for
235
+ // retrieval of the currently set memory limit.
236
+ func SetMemoryLimit(limit int64) int64 {
237
+ return setMemoryLimit(limit)
238
+ }
platform/dbops/binaries/go/go/src/runtime/debug/garbage_test.go ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2013 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug_test
6
+
7
+ import (
8
+ "internal/testenv"
9
+ "os"
10
+ "runtime"
11
+ . "runtime/debug"
12
+ "testing"
13
+ "time"
14
+ )
15
+
16
+ func TestReadGCStats(t *testing.T) {
17
+ defer SetGCPercent(SetGCPercent(-1))
18
+
19
+ var stats GCStats
20
+ var mstats runtime.MemStats
21
+ var min, max time.Duration
22
+
23
+ // First ReadGCStats will allocate, second should not,
24
+ // especially if we follow up with an explicit garbage collection.
25
+ stats.PauseQuantiles = make([]time.Duration, 10)
26
+ ReadGCStats(&stats)
27
+ runtime.GC()
28
+
29
+ // Assume these will return same data: no GC during ReadGCStats.
30
+ ReadGCStats(&stats)
31
+ runtime.ReadMemStats(&mstats)
32
+
33
+ if stats.NumGC != int64(mstats.NumGC) {
34
+ t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC)
35
+ }
36
+ if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) {
37
+ t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs)
38
+ }
39
+ if stats.LastGC.UnixNano() != int64(mstats.LastGC) {
40
+ t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC)
41
+ }
42
+ n := int(mstats.NumGC)
43
+ if n > len(mstats.PauseNs) {
44
+ n = len(mstats.PauseNs)
45
+ }
46
+ if len(stats.Pause) != n {
47
+ t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n)
48
+ } else {
49
+ off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
50
+ for i := 0; i < n; i++ {
51
+ dt := stats.Pause[i]
52
+ if dt != time.Duration(mstats.PauseNs[off]) {
53
+ t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off])
54
+ }
55
+ if max < dt {
56
+ max = dt
57
+ }
58
+ if min > dt || i == 0 {
59
+ min = dt
60
+ }
61
+ off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
62
+ }
63
+ }
64
+
65
+ q := stats.PauseQuantiles
66
+ nq := len(q)
67
+ if q[0] != min || q[nq-1] != max {
68
+ t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max)
69
+ }
70
+
71
+ for i := 0; i < nq-1; i++ {
72
+ if q[i] > q[i+1] {
73
+ t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1])
74
+ }
75
+ }
76
+
77
+ // compare memory stats with gc stats:
78
+ if len(stats.PauseEnd) != n {
79
+ t.Fatalf("len(stats.PauseEnd) = %d, want %d", len(stats.PauseEnd), n)
80
+ }
81
+ off := (int(mstats.NumGC) + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd)
82
+ for i := 0; i < n; i++ {
83
+ dt := stats.PauseEnd[i]
84
+ if dt.UnixNano() != int64(mstats.PauseEnd[off]) {
85
+ t.Errorf("stats.PauseEnd[%d] = %d, want %d", i, dt.UnixNano(), mstats.PauseEnd[off])
86
+ }
87
+ off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd)
88
+ }
89
+ }
90
+
91
+ var big []byte
92
+
93
+ func TestFreeOSMemory(t *testing.T) {
94
+ // Tests FreeOSMemory by making big susceptible to collection
95
+ // and checking that at least that much memory is returned to
96
+ // the OS after.
97
+
98
+ const bigBytes = 32 << 20
99
+ big = make([]byte, bigBytes)
100
+
101
+ // Make sure any in-progress GCs are complete.
102
+ runtime.GC()
103
+
104
+ var before runtime.MemStats
105
+ runtime.ReadMemStats(&before)
106
+
107
+ // Clear the last reference to the big allocation, making it
108
+ // susceptible to collection.
109
+ big = nil
110
+
111
+ // FreeOSMemory runs a GC cycle before releasing memory,
112
+ // so it's fine to skip a GC here.
113
+ //
114
+ // It's possible the background scavenger runs concurrently
115
+ // with this function and does most of the work for it.
116
+ // If that happens, it's OK. What we want is a test that fails
117
+ // often if FreeOSMemory does not work correctly, and a test
118
+ // that passes every time if it does.
119
+ FreeOSMemory()
120
+
121
+ var after runtime.MemStats
122
+ runtime.ReadMemStats(&after)
123
+
124
+ // Check to make sure that the big allocation (now freed)
125
+ // had its memory shift into HeapReleased as a result of that
126
+ // FreeOSMemory.
127
+ if after.HeapReleased <= before.HeapReleased {
128
+ t.Fatalf("no memory released: %d -> %d", before.HeapReleased, after.HeapReleased)
129
+ }
130
+
131
+ // Check to make sure bigBytes was released, plus some slack. Pages may get
132
+ // allocated in between the two measurements above for a variety for reasons,
133
+ // most commonly for GC work bufs. Since this can get fairly high, depending
134
+ // on scheduling and what GOMAXPROCS is, give a lot of slack up-front.
135
+ //
136
+ // Add a little more slack too if the page size is bigger than the runtime page size.
137
+ // "big" could end up unaligned on its ends, forcing the scavenger to skip at worst
138
+ // 2x pages.
139
+ slack := uint64(bigBytes / 2)
140
+ pageSize := uint64(os.Getpagesize())
141
+ if pageSize > 8<<10 {
142
+ slack += pageSize * 2
143
+ }
144
+ if slack > bigBytes {
145
+ // We basically already checked this.
146
+ return
147
+ }
148
+ if after.HeapReleased-before.HeapReleased < bigBytes-slack {
149
+ t.Fatalf("less than %d released: %d -> %d", bigBytes-slack, before.HeapReleased, after.HeapReleased)
150
+ }
151
+ }
152
+
153
+ var (
154
+ setGCPercentBallast any
155
+ setGCPercentSink any
156
+ )
157
+
158
+ func TestSetGCPercent(t *testing.T) {
159
+ testenv.SkipFlaky(t, 20076)
160
+
161
+ // Test that the variable is being set and returned correctly.
162
+ old := SetGCPercent(123)
163
+ new := SetGCPercent(old)
164
+ if new != 123 {
165
+ t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)
166
+ }
167
+
168
+ // Test that the percentage is implemented correctly.
169
+ defer func() {
170
+ SetGCPercent(old)
171
+ setGCPercentBallast, setGCPercentSink = nil, nil
172
+ }()
173
+ SetGCPercent(100)
174
+ runtime.GC()
175
+ // Create 100 MB of live heap as a baseline.
176
+ const baseline = 100 << 20
177
+ var ms runtime.MemStats
178
+ runtime.ReadMemStats(&ms)
179
+ setGCPercentBallast = make([]byte, baseline-ms.Alloc)
180
+ runtime.GC()
181
+ runtime.ReadMemStats(&ms)
182
+ if abs64(baseline-int64(ms.Alloc)) > 10<<20 {
183
+ t.Fatalf("failed to set up baseline live heap; got %d MB, want %d MB", ms.Alloc>>20, baseline>>20)
184
+ }
185
+ // NextGC should be ~200 MB.
186
+ const thresh = 20 << 20 // TODO: Figure out why this is so noisy on some builders
187
+ if want := int64(2 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
188
+ t.Errorf("NextGC = %d MB, want %d卤%d MB", ms.NextGC>>20, want>>20, thresh>>20)
189
+ }
190
+ // Create some garbage, but not enough to trigger another GC.
191
+ for i := 0; i < int(1.2*baseline); i += 1 << 10 {
192
+ setGCPercentSink = make([]byte, 1<<10)
193
+ }
194
+ setGCPercentSink = nil
195
+ // Adjust GOGC to 50. NextGC should be ~150 MB.
196
+ SetGCPercent(50)
197
+ runtime.ReadMemStats(&ms)
198
+ if want := int64(1.5 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
199
+ t.Errorf("NextGC = %d MB, want %d卤%d MB", ms.NextGC>>20, want>>20, thresh>>20)
200
+ }
201
+
202
+ // Trigger a GC and get back to 100 MB live with GOGC=100.
203
+ SetGCPercent(100)
204
+ runtime.GC()
205
+ // Raise live to 120 MB.
206
+ setGCPercentSink = make([]byte, int(0.2*baseline))
207
+ // Lower GOGC to 10. This must force a GC.
208
+ runtime.ReadMemStats(&ms)
209
+ ngc1 := ms.NumGC
210
+ SetGCPercent(10)
211
+ // It may require an allocation to actually force the GC.
212
+ setGCPercentSink = make([]byte, 1<<20)
213
+ runtime.ReadMemStats(&ms)
214
+ ngc2 := ms.NumGC
215
+ if ngc1 == ngc2 {
216
+ t.Errorf("expected GC to run but it did not")
217
+ }
218
+ }
219
+
220
+ func abs64(a int64) int64 {
221
+ if a < 0 {
222
+ return -a
223
+ }
224
+ return a
225
+ }
226
+
227
+ func TestSetMaxThreadsOvf(t *testing.T) {
228
+ // Verify that a big threads count will not overflow the int32
229
+ // maxmcount variable, causing a panic (see Issue 16076).
230
+ //
231
+ // This can only happen when ints are 64 bits, since on platforms
232
+ // with 32 bit ints SetMaxThreads (which takes an int parameter)
233
+ // cannot be given anything that will overflow an int32.
234
+ //
235
+ // Call SetMaxThreads with 1<<31, but only on 64 bit systems.
236
+ nt := SetMaxThreads(1 << (30 + ^uint(0)>>63))
237
+ SetMaxThreads(nt) // restore previous value
238
+ }
platform/dbops/binaries/go/go/src/runtime/debug/heapdump_test.go ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2014 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug_test
6
+
7
+ import (
8
+ "os"
9
+ "runtime"
10
+ . "runtime/debug"
11
+ "testing"
12
+ )
13
+
14
+ func TestWriteHeapDumpNonempty(t *testing.T) {
15
+ if runtime.GOOS == "js" {
16
+ t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
17
+ }
18
+ f, err := os.CreateTemp("", "heapdumptest")
19
+ if err != nil {
20
+ t.Fatalf("TempFile failed: %v", err)
21
+ }
22
+ defer os.Remove(f.Name())
23
+ defer f.Close()
24
+ WriteHeapDump(f.Fd())
25
+ fi, err := f.Stat()
26
+ if err != nil {
27
+ t.Fatalf("Stat failed: %v", err)
28
+ }
29
+ const minSize = 1
30
+ if size := fi.Size(); size < minSize {
31
+ t.Fatalf("Heap dump size %d bytes, expected at least %d bytes", size, minSize)
32
+ }
33
+ }
34
+
35
+ type Obj struct {
36
+ x, y int
37
+ }
38
+
39
+ func objfin(x *Obj) {
40
+ //println("finalized", x)
41
+ }
42
+
43
+ func TestWriteHeapDumpFinalizers(t *testing.T) {
44
+ if runtime.GOOS == "js" {
45
+ t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
46
+ }
47
+ f, err := os.CreateTemp("", "heapdumptest")
48
+ if err != nil {
49
+ t.Fatalf("TempFile failed: %v", err)
50
+ }
51
+ defer os.Remove(f.Name())
52
+ defer f.Close()
53
+
54
+ // bug 9172: WriteHeapDump couldn't handle more than one finalizer
55
+ println("allocating objects")
56
+ x := &Obj{}
57
+ runtime.SetFinalizer(x, objfin)
58
+ y := &Obj{}
59
+ runtime.SetFinalizer(y, objfin)
60
+
61
+ // Trigger collection of x and y, queueing of their finalizers.
62
+ println("starting gc")
63
+ runtime.GC()
64
+
65
+ // Make sure WriteHeapDump doesn't fail with multiple queued finalizers.
66
+ println("starting dump")
67
+ WriteHeapDump(f.Fd())
68
+ println("done dump")
69
+ }
70
+
71
+ type G[T any] struct{}
72
+ type I interface {
73
+ M()
74
+ }
75
+
76
+ //go:noinline
77
+ func (g G[T]) M() {}
78
+
79
+ var dummy I = G[int]{}
80
+ var dummy2 I = G[G[int]]{}
81
+
82
+ func TestWriteHeapDumpTypeName(t *testing.T) {
83
+ if runtime.GOOS == "js" {
84
+ t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
85
+ }
86
+ f, err := os.CreateTemp("", "heapdumptest")
87
+ if err != nil {
88
+ t.Fatalf("TempFile failed: %v", err)
89
+ }
90
+ defer os.Remove(f.Name())
91
+ defer f.Close()
92
+ WriteHeapDump(f.Fd())
93
+ dummy.M()
94
+ dummy2.M()
95
+ }
platform/dbops/binaries/go/go/src/runtime/debug/mod.go ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug
6
+
7
+ import (
8
+ "fmt"
9
+ "runtime"
10
+ "strconv"
11
+ "strings"
12
+ )
13
+
14
+ // exported from runtime.
15
+ func modinfo() string
16
+
17
+ // ReadBuildInfo returns the build information embedded
18
+ // in the running binary. The information is available only
19
+ // in binaries built with module support.
20
+ func ReadBuildInfo() (info *BuildInfo, ok bool) {
21
+ data := modinfo()
22
+ if len(data) < 32 {
23
+ return nil, false
24
+ }
25
+ data = data[16 : len(data)-16]
26
+ bi, err := ParseBuildInfo(data)
27
+ if err != nil {
28
+ return nil, false
29
+ }
30
+
31
+ // The go version is stored separately from other build info, mostly for
32
+ // historical reasons. It is not part of the modinfo() string, and
33
+ // ParseBuildInfo does not recognize it. We inject it here to hide this
34
+ // awkwardness from the user.
35
+ bi.GoVersion = runtime.Version()
36
+
37
+ return bi, true
38
+ }
39
+
40
+ // BuildInfo represents the build information read from a Go binary.
41
+ type BuildInfo struct {
42
+ // GoVersion is the version of the Go toolchain that built the binary
43
+ // (for example, "go1.19.2").
44
+ GoVersion string
45
+
46
+ // Path is the package path of the main package for the binary
47
+ // (for example, "golang.org/x/tools/cmd/stringer").
48
+ Path string
49
+
50
+ // Main describes the module that contains the main package for the binary.
51
+ Main Module
52
+
53
+ // Deps describes all the dependency modules, both direct and indirect,
54
+ // that contributed packages to the build of this binary.
55
+ Deps []*Module
56
+
57
+ // Settings describes the build settings used to build the binary.
58
+ Settings []BuildSetting
59
+ }
60
+
61
+ // A Module describes a single module included in a build.
62
+ type Module struct {
63
+ Path string // module path
64
+ Version string // module version
65
+ Sum string // checksum
66
+ Replace *Module // replaced by this module
67
+ }
68
+
69
+ // A BuildSetting is a key-value pair describing one setting that influenced a build.
70
+ //
71
+ // Defined keys include:
72
+ //
73
+ // - -buildmode: the buildmode flag used (typically "exe")
74
+ // - -compiler: the compiler toolchain flag used (typically "gc")
75
+ // - CGO_ENABLED: the effective CGO_ENABLED environment variable
76
+ // - CGO_CFLAGS: the effective CGO_CFLAGS environment variable
77
+ // - CGO_CPPFLAGS: the effective CGO_CPPFLAGS environment variable
78
+ // - CGO_CXXFLAGS: the effective CGO_CXXFLAGS environment variable
79
+ // - CGO_LDFLAGS: the effective CGO_LDFLAGS environment variable
80
+ // - GOARCH: the architecture target
81
+ // - GOAMD64/GOARM/GO386/etc: the architecture feature level for GOARCH
82
+ // - GOOS: the operating system target
83
+ // - vcs: the version control system for the source tree where the build ran
84
+ // - vcs.revision: the revision identifier for the current commit or checkout
85
+ // - vcs.time: the modification time associated with vcs.revision, in RFC3339 format
86
+ // - vcs.modified: true or false indicating whether the source tree had local modifications
87
+ type BuildSetting struct {
88
+ // Key and Value describe the build setting.
89
+ // Key must not contain an equals sign, space, tab, or newline.
90
+ // Value must not contain newlines ('\n').
91
+ Key, Value string
92
+ }
93
+
94
+ // quoteKey reports whether key is required to be quoted.
95
+ func quoteKey(key string) bool {
96
+ return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`")
97
+ }
98
+
99
+ // quoteValue reports whether value is required to be quoted.
100
+ func quoteValue(value string) bool {
101
+ return strings.ContainsAny(value, " \t\r\n\"`")
102
+ }
103
+
104
+ func (bi *BuildInfo) String() string {
105
+ buf := new(strings.Builder)
106
+ if bi.GoVersion != "" {
107
+ fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion)
108
+ }
109
+ if bi.Path != "" {
110
+ fmt.Fprintf(buf, "path\t%s\n", bi.Path)
111
+ }
112
+ var formatMod func(string, Module)
113
+ formatMod = func(word string, m Module) {
114
+ buf.WriteString(word)
115
+ buf.WriteByte('\t')
116
+ buf.WriteString(m.Path)
117
+ buf.WriteByte('\t')
118
+ buf.WriteString(m.Version)
119
+ if m.Replace == nil {
120
+ buf.WriteByte('\t')
121
+ buf.WriteString(m.Sum)
122
+ } else {
123
+ buf.WriteByte('\n')
124
+ formatMod("=>", *m.Replace)
125
+ }
126
+ buf.WriteByte('\n')
127
+ }
128
+ if bi.Main != (Module{}) {
129
+ formatMod("mod", bi.Main)
130
+ }
131
+ for _, dep := range bi.Deps {
132
+ formatMod("dep", *dep)
133
+ }
134
+ for _, s := range bi.Settings {
135
+ key := s.Key
136
+ if quoteKey(key) {
137
+ key = strconv.Quote(key)
138
+ }
139
+ value := s.Value
140
+ if quoteValue(value) {
141
+ value = strconv.Quote(value)
142
+ }
143
+ fmt.Fprintf(buf, "build\t%s=%s\n", key, value)
144
+ }
145
+
146
+ return buf.String()
147
+ }
148
+
149
+ func ParseBuildInfo(data string) (bi *BuildInfo, err error) {
150
+ lineNum := 1
151
+ defer func() {
152
+ if err != nil {
153
+ err = fmt.Errorf("could not parse Go build info: line %d: %w", lineNum, err)
154
+ }
155
+ }()
156
+
157
+ var (
158
+ pathLine = "path\t"
159
+ modLine = "mod\t"
160
+ depLine = "dep\t"
161
+ repLine = "=>\t"
162
+ buildLine = "build\t"
163
+ newline = "\n"
164
+ tab = "\t"
165
+ )
166
+
167
+ readModuleLine := func(elem []string) (Module, error) {
168
+ if len(elem) != 2 && len(elem) != 3 {
169
+ return Module{}, fmt.Errorf("expected 2 or 3 columns; got %d", len(elem))
170
+ }
171
+ version := elem[1]
172
+ sum := ""
173
+ if len(elem) == 3 {
174
+ sum = elem[2]
175
+ }
176
+ return Module{
177
+ Path: elem[0],
178
+ Version: version,
179
+ Sum: sum,
180
+ }, nil
181
+ }
182
+
183
+ bi = new(BuildInfo)
184
+ var (
185
+ last *Module
186
+ line string
187
+ ok bool
188
+ )
189
+ // Reverse of BuildInfo.String(), except for go version.
190
+ for len(data) > 0 {
191
+ line, data, ok = strings.Cut(data, newline)
192
+ if !ok {
193
+ break
194
+ }
195
+ switch {
196
+ case strings.HasPrefix(line, pathLine):
197
+ elem := line[len(pathLine):]
198
+ bi.Path = string(elem)
199
+ case strings.HasPrefix(line, modLine):
200
+ elem := strings.Split(line[len(modLine):], tab)
201
+ last = &bi.Main
202
+ *last, err = readModuleLine(elem)
203
+ if err != nil {
204
+ return nil, err
205
+ }
206
+ case strings.HasPrefix(line, depLine):
207
+ elem := strings.Split(line[len(depLine):], tab)
208
+ last = new(Module)
209
+ bi.Deps = append(bi.Deps, last)
210
+ *last, err = readModuleLine(elem)
211
+ if err != nil {
212
+ return nil, err
213
+ }
214
+ case strings.HasPrefix(line, repLine):
215
+ elem := strings.Split(line[len(repLine):], tab)
216
+ if len(elem) != 3 {
217
+ return nil, fmt.Errorf("expected 3 columns for replacement; got %d", len(elem))
218
+ }
219
+ if last == nil {
220
+ return nil, fmt.Errorf("replacement with no module on previous line")
221
+ }
222
+ last.Replace = &Module{
223
+ Path: string(elem[0]),
224
+ Version: string(elem[1]),
225
+ Sum: string(elem[2]),
226
+ }
227
+ last = nil
228
+ case strings.HasPrefix(line, buildLine):
229
+ kv := line[len(buildLine):]
230
+ if len(kv) < 1 {
231
+ return nil, fmt.Errorf("build line missing '='")
232
+ }
233
+
234
+ var key, rawValue string
235
+ switch kv[0] {
236
+ case '=':
237
+ return nil, fmt.Errorf("build line with missing key")
238
+
239
+ case '`', '"':
240
+ rawKey, err := strconv.QuotedPrefix(kv)
241
+ if err != nil {
242
+ return nil, fmt.Errorf("invalid quoted key in build line")
243
+ }
244
+ if len(kv) == len(rawKey) {
245
+ return nil, fmt.Errorf("build line missing '=' after quoted key")
246
+ }
247
+ if c := kv[len(rawKey)]; c != '=' {
248
+ return nil, fmt.Errorf("unexpected character after quoted key: %q", c)
249
+ }
250
+ key, _ = strconv.Unquote(rawKey)
251
+ rawValue = kv[len(rawKey)+1:]
252
+
253
+ default:
254
+ var ok bool
255
+ key, rawValue, ok = strings.Cut(kv, "=")
256
+ if !ok {
257
+ return nil, fmt.Errorf("build line missing '=' after key")
258
+ }
259
+ if quoteKey(key) {
260
+ return nil, fmt.Errorf("unquoted key %q must be quoted", key)
261
+ }
262
+ }
263
+
264
+ var value string
265
+ if len(rawValue) > 0 {
266
+ switch rawValue[0] {
267
+ case '`', '"':
268
+ var err error
269
+ value, err = strconv.Unquote(rawValue)
270
+ if err != nil {
271
+ return nil, fmt.Errorf("invalid quoted value in build line")
272
+ }
273
+
274
+ default:
275
+ value = rawValue
276
+ if quoteValue(value) {
277
+ return nil, fmt.Errorf("unquoted value %q must be quoted", value)
278
+ }
279
+ }
280
+ }
281
+
282
+ bi.Settings = append(bi.Settings, BuildSetting{Key: key, Value: value})
283
+ }
284
+ lineNum++
285
+ }
286
+ return bi, nil
287
+ }
platform/dbops/binaries/go/go/src/runtime/debug/mod_test.go ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2022 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug_test
6
+
7
+ import (
8
+ "reflect"
9
+ "runtime/debug"
10
+ "strings"
11
+ "testing"
12
+ )
13
+
14
+ // strip removes two leading tabs after each newline of s.
15
+ func strip(s string) string {
16
+ replaced := strings.ReplaceAll(s, "\n\t\t", "\n")
17
+ if len(replaced) > 0 && replaced[0] == '\n' {
18
+ replaced = replaced[1:]
19
+ }
20
+ return replaced
21
+ }
22
+
23
+ func FuzzParseBuildInfoRoundTrip(f *testing.F) {
24
+ // Package built from outside a module, missing some fields..
25
+ f.Add(strip(`
26
+ path rsc.io/fortune
27
+ mod rsc.io/fortune v1.0.0
28
+ `))
29
+
30
+ // Package built from the standard library, missing some fields..
31
+ f.Add(`path cmd/test2json`)
32
+
33
+ // Package built from inside a module.
34
+ f.Add(strip(`
35
+ go 1.18
36
+ path example.com/m
37
+ mod example.com/m (devel)
38
+ build -compiler=gc
39
+ `))
40
+
41
+ // Package built in GOPATH mode.
42
+ f.Add(strip(`
43
+ go 1.18
44
+ path example.com/m
45
+ build -compiler=gc
46
+ `))
47
+
48
+ // Escaped build info.
49
+ f.Add(strip(`
50
+ go 1.18
51
+ path example.com/m
52
+ build CRAZY_ENV="requires\nescaping"
53
+ `))
54
+
55
+ f.Fuzz(func(t *testing.T, s string) {
56
+ bi, err := debug.ParseBuildInfo(s)
57
+ if err != nil {
58
+ // Not a round-trippable BuildInfo string.
59
+ t.Log(err)
60
+ return
61
+ }
62
+
63
+ // s2 could have different escaping from s.
64
+ // However, it should parse to exactly the same contents.
65
+ s2 := bi.String()
66
+ bi2, err := debug.ParseBuildInfo(s2)
67
+ if err != nil {
68
+ t.Fatalf("%v:\n%s", err, s2)
69
+ }
70
+
71
+ if !reflect.DeepEqual(bi2, bi) {
72
+ t.Fatalf("Parsed representation differs.\ninput:\n%s\noutput:\n%s", s, s2)
73
+ }
74
+ })
75
+ }
platform/dbops/binaries/go/go/src/runtime/debug/panic_test.go ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2020 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd
6
+
7
+ // TODO: test on Windows?
8
+
9
+ package debug_test
10
+
11
+ import (
12
+ "runtime"
13
+ "runtime/debug"
14
+ "syscall"
15
+ "testing"
16
+ "unsafe"
17
+ )
18
+
19
+ func TestPanicOnFault(t *testing.T) {
20
+ if runtime.GOARCH == "s390x" {
21
+ t.Skip("s390x fault addresses are missing the low order bits")
22
+ }
23
+ if runtime.GOOS == "ios" {
24
+ t.Skip("iOS doesn't provide fault addresses")
25
+ }
26
+ if runtime.GOOS == "netbsd" && runtime.GOARCH == "arm" {
27
+ t.Skip("netbsd-arm doesn't provide fault address (golang.org/issue/45026)")
28
+ }
29
+ m, err := syscall.Mmap(-1, 0, 0x1000, syscall.PROT_READ /* Note: no PROT_WRITE */, syscall.MAP_SHARED|syscall.MAP_ANON)
30
+ if err != nil {
31
+ t.Fatalf("can't map anonymous memory: %s", err)
32
+ }
33
+ defer syscall.Munmap(m)
34
+ old := debug.SetPanicOnFault(true)
35
+ defer debug.SetPanicOnFault(old)
36
+ const lowBits = 0x3e7
37
+ defer func() {
38
+ r := recover()
39
+ if r == nil {
40
+ t.Fatalf("write did not fault")
41
+ }
42
+ type addressable interface {
43
+ Addr() uintptr
44
+ }
45
+ a, ok := r.(addressable)
46
+ if !ok {
47
+ t.Fatalf("fault does not contain address")
48
+ }
49
+ want := uintptr(unsafe.Pointer(&m[lowBits]))
50
+ got := a.Addr()
51
+ if got != want {
52
+ t.Fatalf("fault address %x, want %x", got, want)
53
+ }
54
+ }()
55
+ m[lowBits] = 1 // will fault
56
+ }
platform/dbops/binaries/go/go/src/runtime/debug/stack.go ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2011 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Package debug contains facilities for programs to debug themselves while
6
+ // they are running.
7
+ package debug
8
+
9
+ import (
10
+ "os"
11
+ "runtime"
12
+ )
13
+
14
+ // PrintStack prints to standard error the stack trace returned by runtime.Stack.
15
+ func PrintStack() {
16
+ os.Stderr.Write(Stack())
17
+ }
18
+
19
+ // Stack returns a formatted stack trace of the goroutine that calls it.
20
+ // It calls [runtime.Stack] with a large enough buffer to capture the entire trace.
21
+ func Stack() []byte {
22
+ buf := make([]byte, 1024)
23
+ for {
24
+ n := runtime.Stack(buf, false)
25
+ if n < len(buf) {
26
+ return buf[:n]
27
+ }
28
+ buf = make([]byte, 2*len(buf))
29
+ }
30
+ }
platform/dbops/binaries/go/go/src/runtime/debug/stack_test.go ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2011 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug_test
6
+
7
+ import (
8
+ "bytes"
9
+ "fmt"
10
+ "internal/testenv"
11
+ "os"
12
+ "os/exec"
13
+ "path/filepath"
14
+ "runtime"
15
+ . "runtime/debug"
16
+ "strings"
17
+ "testing"
18
+ )
19
+
20
+ func TestMain(m *testing.M) {
21
+ if os.Getenv("GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT") != "" {
22
+ fmt.Println(runtime.GOROOT())
23
+ os.Exit(0)
24
+ }
25
+ os.Exit(m.Run())
26
+ }
27
+
28
+ type T int
29
+
30
+ func (t *T) ptrmethod() []byte {
31
+ return Stack()
32
+ }
33
+ func (t T) method() []byte {
34
+ return t.ptrmethod()
35
+ }
36
+
37
+ /*
38
+ The traceback should look something like this, modulo line numbers and hex constants.
39
+ Don't worry much about the base levels, but check the ones in our own package.
40
+
41
+ goroutine 10 [running]:
42
+ runtime/debug.Stack(0x0, 0x0, 0x0)
43
+ /Users/r/go/src/runtime/debug/stack.go:28 +0x80
44
+ runtime/debug.(*T).ptrmethod(0xc82005ee70, 0x0, 0x0, 0x0)
45
+ /Users/r/go/src/runtime/debug/stack_test.go:15 +0x29
46
+ runtime/debug.T.method(0x0, 0x0, 0x0, 0x0)
47
+ /Users/r/go/src/runtime/debug/stack_test.go:18 +0x32
48
+ runtime/debug.TestStack(0xc8201ce000)
49
+ /Users/r/go/src/runtime/debug/stack_test.go:37 +0x38
50
+ testing.tRunner(0xc8201ce000, 0x664b58)
51
+ /Users/r/go/src/testing/testing.go:456 +0x98
52
+ created by testing.RunTests
53
+ /Users/r/go/src/testing/testing.go:561 +0x86d
54
+ */
55
+ func TestStack(t *testing.T) {
56
+ b := T(0).method()
57
+ lines := strings.Split(string(b), "\n")
58
+ if len(lines) < 6 {
59
+ t.Fatal("too few lines")
60
+ }
61
+
62
+ // If built with -trimpath, file locations should start with package paths.
63
+ // Otherwise, file locations should start with a GOROOT/src prefix
64
+ // (for whatever value of GOROOT is baked into the binary, not the one
65
+ // that may be set in the environment).
66
+ fileGoroot := ""
67
+ if envGoroot := os.Getenv("GOROOT"); envGoroot != "" {
68
+ // Since GOROOT is set explicitly in the environment, we can't be certain
69
+ // that it is the same GOROOT value baked into the binary, and we can't
70
+ // change the value in-process because runtime.GOROOT uses the value from
71
+ // initial (not current) environment. Spawn a subprocess to determine the
72
+ // real baked-in GOROOT.
73
+ t.Logf("found GOROOT %q from environment; checking embedded GOROOT value", envGoroot)
74
+ testenv.MustHaveExec(t)
75
+ exe, err := os.Executable()
76
+ if err != nil {
77
+ t.Fatal(err)
78
+ }
79
+ cmd := exec.Command(exe)
80
+ cmd.Env = append(os.Environ(), "GOROOT=", "GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT=1")
81
+ out, err := cmd.Output()
82
+ if err != nil {
83
+ t.Fatal(err)
84
+ }
85
+ fileGoroot = string(bytes.TrimSpace(out))
86
+ } else {
87
+ // Since GOROOT is not set in the environment, its value (if any) must come
88
+ // from the path embedded in the binary.
89
+ fileGoroot = runtime.GOROOT()
90
+ }
91
+ filePrefix := ""
92
+ if fileGoroot != "" {
93
+ filePrefix = filepath.ToSlash(fileGoroot) + "/src/"
94
+ }
95
+
96
+ n := 0
97
+ frame := func(file, code string) {
98
+ t.Helper()
99
+
100
+ line := lines[n]
101
+ if !strings.Contains(line, code) {
102
+ t.Errorf("expected %q in %q", code, line)
103
+ }
104
+ n++
105
+
106
+ line = lines[n]
107
+
108
+ wantPrefix := "\t" + filePrefix + file
109
+ if !strings.HasPrefix(line, wantPrefix) {
110
+ t.Errorf("in line %q, expected prefix %q", line, wantPrefix)
111
+ }
112
+ n++
113
+ }
114
+ n++
115
+
116
+ frame("runtime/debug/stack.go", "runtime/debug.Stack")
117
+ frame("runtime/debug/stack_test.go", "runtime/debug_test.(*T).ptrmethod")
118
+ frame("runtime/debug/stack_test.go", "runtime/debug_test.T.method")
119
+ frame("runtime/debug/stack_test.go", "runtime/debug_test.TestStack")
120
+ frame("testing/testing.go", "")
121
+ }
platform/dbops/binaries/go/go/src/runtime/debug/stubs.go ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2014 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package debug
6
+
7
+ import (
8
+ "time"
9
+ )
10
+
11
+ // Implemented in package runtime.
12
+ func readGCStats(*[]time.Duration)
13
+ func freeOSMemory()
14
+ func setMaxStack(int) int
15
+ func setGCPercent(int32) int32
16
+ func setPanicOnFault(bool) bool
17
+ func setMaxThreads(int) int
18
+ func setMemoryLimit(int64) int64
platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/5501685e611fa764 ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ go test fuzz v1
2
+ string("mod\t\t0\n")
platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/71634114e78567cf ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ go test fuzz v1
2
+ string("mod\t0\t\n")
platform/dbops/binaries/go/go/src/runtime/debug/testdata/fuzz/FuzzParseBuildInfoRoundTrip/c73dce23c1f2494c ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ go test fuzz v1
2
+ string("build\t0=\" 0\"\n")
platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_386.go ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build 386
6
+
7
+ package atomic
8
+
9
+ import "unsafe"
10
+
11
+ // Export some functions via linkname to assembly in sync/atomic.
12
+ //
13
+ //go:linkname Load
14
+ //go:linkname Loadp
15
+
16
+ //go:nosplit
17
+ //go:noinline
18
+ func Load(ptr *uint32) uint32 {
19
+ return *ptr
20
+ }
21
+
22
+ //go:nosplit
23
+ //go:noinline
24
+ func Loadp(ptr unsafe.Pointer) unsafe.Pointer {
25
+ return *(*unsafe.Pointer)(ptr)
26
+ }
27
+
28
+ //go:nosplit
29
+ //go:noinline
30
+ func LoadAcq(ptr *uint32) uint32 {
31
+ return *ptr
32
+ }
33
+
34
+ //go:nosplit
35
+ //go:noinline
36
+ func LoadAcquintptr(ptr *uintptr) uintptr {
37
+ return *ptr
38
+ }
39
+
40
+ //go:noescape
41
+ func Xadd64(ptr *uint64, delta int64) uint64
42
+
43
+ //go:noescape
44
+ func Xadduintptr(ptr *uintptr, delta uintptr) uintptr
45
+
46
+ //go:noescape
47
+ func Xadd(ptr *uint32, delta int32) uint32
48
+
49
+ //go:noescape
50
+ func Xchg64(ptr *uint64, new uint64) uint64
51
+
52
+ //go:noescape
53
+ func Xchg(ptr *uint32, new uint32) uint32
54
+
55
+ //go:noescape
56
+ func Xchguintptr(ptr *uintptr, new uintptr) uintptr
57
+
58
+ //go:noescape
59
+ func Load64(ptr *uint64) uint64
60
+
61
+ //go:nosplit
62
+ //go:noinline
63
+ func Load8(ptr *uint8) uint8 {
64
+ return *ptr
65
+ }
66
+
67
+ //go:noescape
68
+ func And8(ptr *uint8, val uint8)
69
+
70
+ //go:noescape
71
+ func Or8(ptr *uint8, val uint8)
72
+
73
+ //go:noescape
74
+ func And(ptr *uint32, val uint32)
75
+
76
+ //go:noescape
77
+ func Or(ptr *uint32, val uint32)
78
+
79
+ //go:noescape
80
+ func And32(ptr *uint32, val uint32) uint32
81
+
82
+ //go:noescape
83
+ func Or32(ptr *uint32, val uint32) uint32
84
+
85
+ //go:noescape
86
+ func And64(ptr *uint64, val uint64) uint64
87
+
88
+ //go:noescape
89
+ func Or64(ptr *uint64, val uint64) uint64
90
+
91
+ //go:noescape
92
+ func Anduintptr(ptr *uintptr, val uintptr) uintptr
93
+
94
+ //go:noescape
95
+ func Oruintptr(ptr *uintptr, val uintptr) uintptr
96
+
97
+ // NOTE: Do not add atomicxor8 (XOR is not idempotent).
98
+
99
+ //go:noescape
100
+ func Cas64(ptr *uint64, old, new uint64) bool
101
+
102
+ //go:noescape
103
+ func CasRel(ptr *uint32, old, new uint32) bool
104
+
105
+ //go:noescape
106
+ func Store(ptr *uint32, val uint32)
107
+
108
+ //go:noescape
109
+ func Store8(ptr *uint8, val uint8)
110
+
111
+ //go:noescape
112
+ func Store64(ptr *uint64, val uint64)
113
+
114
+ //go:noescape
115
+ func StoreRel(ptr *uint32, val uint32)
116
+
117
+ //go:noescape
118
+ func StoreReluintptr(ptr *uintptr, val uintptr)
119
+
120
+ // NO go:noescape annotation; see atomic_pointer.go.
121
+ func StorepNoWB(ptr unsafe.Pointer, val unsafe.Pointer)
platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_386.s ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ #include "textflag.h"
6
+ #include "funcdata.h"
7
+
8
+ // bool Cas(int32 *val, int32 old, int32 new)
9
+ // Atomically:
10
+ // if(*val == old){
11
+ // *val = new;
12
+ // return 1;
13
+ // }else
14
+ // return 0;
15
+ TEXT 路Cas(SB), NOSPLIT, $0-13
16
+ MOVL ptr+0(FP), BX
17
+ MOVL old+4(FP), AX
18
+ MOVL new+8(FP), CX
19
+ LOCK
20
+ CMPXCHGL CX, 0(BX)
21
+ SETEQ ret+12(FP)
22
+ RET
23
+
24
+ TEXT 路Casint32(SB), NOSPLIT, $0-13
25
+ JMP 路Cas(SB)
26
+
27
+ TEXT 路Casint64(SB), NOSPLIT, $0-21
28
+ JMP 路Cas64(SB)
29
+
30
+ TEXT 路Casuintptr(SB), NOSPLIT, $0-13
31
+ JMP 路Cas(SB)
32
+
33
+ TEXT 路CasRel(SB), NOSPLIT, $0-13
34
+ JMP 路Cas(SB)
35
+
36
+ TEXT 路Loaduintptr(SB), NOSPLIT, $0-8
37
+ JMP 路Load(SB)
38
+
39
+ TEXT 路Loaduint(SB), NOSPLIT, $0-8
40
+ JMP 路Load(SB)
41
+
42
+ TEXT 路Storeint32(SB), NOSPLIT, $0-8
43
+ JMP 路Store(SB)
44
+
45
+ TEXT 路Storeint64(SB), NOSPLIT, $0-12
46
+ JMP 路Store64(SB)
47
+
48
+ TEXT 路Storeuintptr(SB), NOSPLIT, $0-8
49
+ JMP 路Store(SB)
50
+
51
+ TEXT 路Xadduintptr(SB), NOSPLIT, $0-12
52
+ JMP 路Xadd(SB)
53
+
54
+ TEXT 路Loadint32(SB), NOSPLIT, $0-8
55
+ JMP 路Load(SB)
56
+
57
+ TEXT 路Loadint64(SB), NOSPLIT, $0-12
58
+ JMP 路Load64(SB)
59
+
60
+ TEXT 路Xaddint32(SB), NOSPLIT, $0-12
61
+ JMP 路Xadd(SB)
62
+
63
+ TEXT 路Xaddint64(SB), NOSPLIT, $0-20
64
+ JMP 路Xadd64(SB)
65
+
66
+ // bool 路Cas64(uint64 *val, uint64 old, uint64 new)
67
+ // Atomically:
68
+ // if(*val == old){
69
+ // *val = new;
70
+ // return 1;
71
+ // } else {
72
+ // return 0;
73
+ // }
74
+ TEXT 路Cas64(SB), NOSPLIT, $0-21
75
+ NO_LOCAL_POINTERS
76
+ MOVL ptr+0(FP), BP
77
+ TESTL $7, BP
78
+ JZ 2(PC)
79
+ CALL 路panicUnaligned(SB)
80
+ MOVL old_lo+4(FP), AX
81
+ MOVL old_hi+8(FP), DX
82
+ MOVL new_lo+12(FP), BX
83
+ MOVL new_hi+16(FP), CX
84
+ LOCK
85
+ CMPXCHG8B 0(BP)
86
+ SETEQ ret+20(FP)
87
+ RET
88
+
89
+ // bool Casp1(void **p, void *old, void *new)
90
+ // Atomically:
91
+ // if(*p == old){
92
+ // *p = new;
93
+ // return 1;
94
+ // }else
95
+ // return 0;
96
+ TEXT 路Casp1(SB), NOSPLIT, $0-13
97
+ MOVL ptr+0(FP), BX
98
+ MOVL old+4(FP), AX
99
+ MOVL new+8(FP), CX
100
+ LOCK
101
+ CMPXCHGL CX, 0(BX)
102
+ SETEQ ret+12(FP)
103
+ RET
104
+
105
+ // uint32 Xadd(uint32 volatile *val, int32 delta)
106
+ // Atomically:
107
+ // *val += delta;
108
+ // return *val;
109
+ TEXT 路Xadd(SB), NOSPLIT, $0-12
110
+ MOVL ptr+0(FP), BX
111
+ MOVL delta+4(FP), AX
112
+ MOVL AX, CX
113
+ LOCK
114
+ XADDL AX, 0(BX)
115
+ ADDL CX, AX
116
+ MOVL AX, ret+8(FP)
117
+ RET
118
+
119
+ TEXT 路Xadd64(SB), NOSPLIT, $0-20
120
+ NO_LOCAL_POINTERS
121
+ // no XADDQ so use CMPXCHG8B loop
122
+ MOVL ptr+0(FP), BP
123
+ TESTL $7, BP
124
+ JZ 2(PC)
125
+ CALL 路panicUnaligned(SB)
126
+ // DI:SI = delta
127
+ MOVL delta_lo+4(FP), SI
128
+ MOVL delta_hi+8(FP), DI
129
+ // DX:AX = *addr
130
+ MOVL 0(BP), AX
131
+ MOVL 4(BP), DX
132
+ addloop:
133
+ // CX:BX = DX:AX (*addr) + DI:SI (delta)
134
+ MOVL AX, BX
135
+ MOVL DX, CX
136
+ ADDL SI, BX
137
+ ADCL DI, CX
138
+
139
+ // if *addr == DX:AX {
140
+ // *addr = CX:BX
141
+ // } else {
142
+ // DX:AX = *addr
143
+ // }
144
+ // all in one instruction
145
+ LOCK
146
+ CMPXCHG8B 0(BP)
147
+
148
+ JNZ addloop
149
+
150
+ // success
151
+ // return CX:BX
152
+ MOVL BX, ret_lo+12(FP)
153
+ MOVL CX, ret_hi+16(FP)
154
+ RET
155
+
156
+ TEXT 路Xchg(SB), NOSPLIT, $0-12
157
+ MOVL ptr+0(FP), BX
158
+ MOVL new+4(FP), AX
159
+ XCHGL AX, 0(BX)
160
+ MOVL AX, ret+8(FP)
161
+ RET
162
+
163
+ TEXT 路Xchgint32(SB), NOSPLIT, $0-12
164
+ JMP 路Xchg(SB)
165
+
166
+ TEXT 路Xchgint64(SB), NOSPLIT, $0-20
167
+ JMP 路Xchg64(SB)
168
+
169
+ TEXT 路Xchguintptr(SB), NOSPLIT, $0-12
170
+ JMP 路Xchg(SB)
171
+
172
+ TEXT 路Xchg64(SB),NOSPLIT,$0-20
173
+ NO_LOCAL_POINTERS
174
+ // no XCHGQ so use CMPXCHG8B loop
175
+ MOVL ptr+0(FP), BP
176
+ TESTL $7, BP
177
+ JZ 2(PC)
178
+ CALL 路panicUnaligned(SB)
179
+ // CX:BX = new
180
+ MOVL new_lo+4(FP), BX
181
+ MOVL new_hi+8(FP), CX
182
+ // DX:AX = *addr
183
+ MOVL 0(BP), AX
184
+ MOVL 4(BP), DX
185
+ swaploop:
186
+ // if *addr == DX:AX
187
+ // *addr = CX:BX
188
+ // else
189
+ // DX:AX = *addr
190
+ // all in one instruction
191
+ LOCK
192
+ CMPXCHG8B 0(BP)
193
+ JNZ swaploop
194
+
195
+ // success
196
+ // return DX:AX
197
+ MOVL AX, ret_lo+12(FP)
198
+ MOVL DX, ret_hi+16(FP)
199
+ RET
200
+
201
+ TEXT 路StorepNoWB(SB), NOSPLIT, $0-8
202
+ MOVL ptr+0(FP), BX
203
+ MOVL val+4(FP), AX
204
+ XCHGL AX, 0(BX)
205
+ RET
206
+
207
+ TEXT 路Store(SB), NOSPLIT, $0-8
208
+ MOVL ptr+0(FP), BX
209
+ MOVL val+4(FP), AX
210
+ XCHGL AX, 0(BX)
211
+ RET
212
+
213
+ TEXT 路StoreRel(SB), NOSPLIT, $0-8
214
+ JMP 路Store(SB)
215
+
216
+ TEXT 路StoreReluintptr(SB), NOSPLIT, $0-8
217
+ JMP 路Store(SB)
218
+
219
+ // uint64 atomicload64(uint64 volatile* addr);
220
+ TEXT 路Load64(SB), NOSPLIT, $0-12
221
+ NO_LOCAL_POINTERS
222
+ MOVL ptr+0(FP), AX
223
+ TESTL $7, AX
224
+ JZ 2(PC)
225
+ CALL 路panicUnaligned(SB)
226
+ MOVQ (AX), M0
227
+ MOVQ M0, ret+4(FP)
228
+ EMMS
229
+ RET
230
+
231
+ // void 路Store64(uint64 volatile* addr, uint64 v);
232
+ TEXT 路Store64(SB), NOSPLIT, $0-12
233
+ NO_LOCAL_POINTERS
234
+ MOVL ptr+0(FP), AX
235
+ TESTL $7, AX
236
+ JZ 2(PC)
237
+ CALL 路panicUnaligned(SB)
238
+ // MOVQ and EMMS were introduced on the Pentium MMX.
239
+ MOVQ val+4(FP), M0
240
+ MOVQ M0, (AX)
241
+ EMMS
242
+ // This is essentially a no-op, but it provides required memory fencing.
243
+ // It can be replaced with MFENCE, but MFENCE was introduced only on the Pentium4 (SSE2).
244
+ XORL AX, AX
245
+ LOCK
246
+ XADDL AX, (SP)
247
+ RET
248
+
249
+ // void 路Or8(byte volatile*, byte);
250
+ TEXT 路Or8(SB), NOSPLIT, $0-5
251
+ MOVL ptr+0(FP), AX
252
+ MOVB val+4(FP), BX
253
+ LOCK
254
+ ORB BX, (AX)
255
+ RET
256
+
257
+ // void 路And8(byte volatile*, byte);
258
+ TEXT 路And8(SB), NOSPLIT, $0-5
259
+ MOVL ptr+0(FP), AX
260
+ MOVB val+4(FP), BX
261
+ LOCK
262
+ ANDB BX, (AX)
263
+ RET
264
+
265
+ TEXT 路Store8(SB), NOSPLIT, $0-5
266
+ MOVL ptr+0(FP), BX
267
+ MOVB val+4(FP), AX
268
+ XCHGB AX, 0(BX)
269
+ RET
270
+
271
+ // func Or(addr *uint32, v uint32)
272
+ TEXT 路Or(SB), NOSPLIT, $0-8
273
+ MOVL ptr+0(FP), AX
274
+ MOVL val+4(FP), BX
275
+ LOCK
276
+ ORL BX, (AX)
277
+ RET
278
+
279
+ // func And(addr *uint32, v uint32)
280
+ TEXT 路And(SB), NOSPLIT, $0-8
281
+ MOVL ptr+0(FP), AX
282
+ MOVL val+4(FP), BX
283
+ LOCK
284
+ ANDL BX, (AX)
285
+ RET
286
+
287
+ // func And32(addr *uint32, v uint32) old uint32
288
+ TEXT 路And32(SB), NOSPLIT, $0-12
289
+ MOVL ptr+0(FP), BX
290
+ MOVL val+4(FP), CX
291
+ casloop:
292
+ MOVL CX, DX
293
+ MOVL (BX), AX
294
+ ANDL AX, DX
295
+ LOCK
296
+ CMPXCHGL DX, (BX)
297
+ JNZ casloop
298
+ MOVL AX, ret+8(FP)
299
+ RET
300
+
301
+ // func Or32(addr *uint32, v uint32) old uint32
302
+ TEXT 路Or32(SB), NOSPLIT, $0-12
303
+ MOVL ptr+0(FP), BX
304
+ MOVL val+4(FP), CX
305
+ casloop:
306
+ MOVL CX, DX
307
+ MOVL (BX), AX
308
+ ORL AX, DX
309
+ LOCK
310
+ CMPXCHGL DX, (BX)
311
+ JNZ casloop
312
+ MOVL AX, ret+8(FP)
313
+ RET
314
+
315
+ // func And64(addr *uint64, v uint64) old uint64
316
+ TEXT 路And64(SB), NOSPLIT, $0-20
317
+ MOVL ptr+0(FP), BP
318
+ // DI:SI = v
319
+ MOVL val_lo+4(FP), SI
320
+ MOVL val_hi+8(FP), DI
321
+ // DX:AX = *addr
322
+ MOVL 0(BP), AX
323
+ MOVL 4(BP), DX
324
+ casloop:
325
+ // CX:BX = DX:AX (*addr) & DI:SI (mask)
326
+ MOVL AX, BX
327
+ MOVL DX, CX
328
+ ANDL SI, BX
329
+ ANDL DI, CX
330
+ LOCK
331
+ CMPXCHG8B 0(BP)
332
+ JNZ casloop
333
+ MOVL AX, ret_lo+12(FP)
334
+ MOVL DX, ret_hi+16(FP)
335
+ RET
336
+
337
+
338
+ // func Or64(addr *uint64, v uint64) old uint64
339
+ TEXT 路Or64(SB), NOSPLIT, $0-20
340
+ MOVL ptr+0(FP), BP
341
+ // DI:SI = v
342
+ MOVL val_lo+4(FP), SI
343
+ MOVL val_hi+8(FP), DI
344
+ // DX:AX = *addr
345
+ MOVL 0(BP), AX
346
+ MOVL 4(BP), DX
347
+ casloop:
348
+ // CX:BX = DX:AX (*addr) | DI:SI (mask)
349
+ MOVL AX, BX
350
+ MOVL DX, CX
351
+ ORL SI, BX
352
+ ORL DI, CX
353
+ LOCK
354
+ CMPXCHG8B 0(BP)
355
+ JNZ casloop
356
+ MOVL AX, ret_lo+12(FP)
357
+ MOVL DX, ret_hi+16(FP)
358
+ RET
359
+
360
+ // func Anduintptr(addr *uintptr, v uintptr) old uintptr
361
+ TEXT 路Anduintptr(SB), NOSPLIT, $0-12
362
+ JMP 路And32(SB)
363
+
364
+ // func Oruintptr(addr *uintptr, v uintptr) old uintptr
365
+ TEXT 路Oruintptr(SB), NOSPLIT, $0-12
366
+ JMP 路Or32(SB)
platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_amd64.go ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2009 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package atomic
6
+
7
+ import "unsafe"
8
+
9
+ // Export some functions via linkname to assembly in sync/atomic.
10
+ //
11
+ //go:linkname Load
12
+ //go:linkname Loadp
13
+ //go:linkname Load64
14
+
15
+ //go:nosplit
16
+ //go:noinline
17
+ func Load(ptr *uint32) uint32 {
18
+ return *ptr
19
+ }
20
+
21
+ //go:nosplit
22
+ //go:noinline
23
+ func Loadp(ptr unsafe.Pointer) unsafe.Pointer {
24
+ return *(*unsafe.Pointer)(ptr)
25
+ }
26
+
27
+ //go:nosplit
28
+ //go:noinline
29
+ func Load64(ptr *uint64) uint64 {
30
+ return *ptr
31
+ }
32
+
33
+ //go:nosplit
34
+ //go:noinline
35
+ func LoadAcq(ptr *uint32) uint32 {
36
+ return *ptr
37
+ }
38
+
39
+ //go:nosplit
40
+ //go:noinline
41
+ func LoadAcq64(ptr *uint64) uint64 {
42
+ return *ptr
43
+ }
44
+
45
+ //go:nosplit
46
+ //go:noinline
47
+ func LoadAcquintptr(ptr *uintptr) uintptr {
48
+ return *ptr
49
+ }
50
+
51
+ //go:noescape
52
+ func Xadd(ptr *uint32, delta int32) uint32
53
+
54
+ //go:noescape
55
+ func Xadd64(ptr *uint64, delta int64) uint64
56
+
57
+ //go:noescape
58
+ func Xadduintptr(ptr *uintptr, delta uintptr) uintptr
59
+
60
+ //go:noescape
61
+ func Xchg(ptr *uint32, new uint32) uint32
62
+
63
+ //go:noescape
64
+ func Xchg64(ptr *uint64, new uint64) uint64
65
+
66
+ //go:noescape
67
+ func Xchguintptr(ptr *uintptr, new uintptr) uintptr
68
+
69
+ //go:nosplit
70
+ //go:noinline
71
+ func Load8(ptr *uint8) uint8 {
72
+ return *ptr
73
+ }
74
+
75
+ //go:noescape
76
+ func And8(ptr *uint8, val uint8)
77
+
78
+ //go:noescape
79
+ func Or8(ptr *uint8, val uint8)
80
+
81
+ //go:noescape
82
+ func And(ptr *uint32, val uint32)
83
+
84
+ //go:noescape
85
+ func Or(ptr *uint32, val uint32)
86
+
87
+ //go:noescape
88
+ func And32(ptr *uint32, val uint32) uint32
89
+
90
+ //go:noescape
91
+ func Or32(ptr *uint32, val uint32) uint32
92
+
93
+ //go:noescape
94
+ func And64(ptr *uint64, val uint64) uint64
95
+
96
+ //go:noescape
97
+ func Or64(ptr *uint64, val uint64) uint64
98
+
99
+ //go:noescape
100
+ func Anduintptr(ptr *uintptr, val uintptr) uintptr
101
+
102
+ //go:noescape
103
+ func Oruintptr(ptr *uintptr, val uintptr) uintptr
104
+
105
+ // NOTE: Do not add atomicxor8 (XOR is not idempotent).
106
+
107
+ //go:noescape
108
+ func Cas64(ptr *uint64, old, new uint64) bool
109
+
110
+ //go:noescape
111
+ func CasRel(ptr *uint32, old, new uint32) bool
112
+
113
+ //go:noescape
114
+ func Store(ptr *uint32, val uint32)
115
+
116
+ //go:noescape
117
+ func Store8(ptr *uint8, val uint8)
118
+
119
+ //go:noescape
120
+ func Store64(ptr *uint64, val uint64)
121
+
122
+ //go:noescape
123
+ func StoreRel(ptr *uint32, val uint32)
124
+
125
+ //go:noescape
126
+ func StoreRel64(ptr *uint64, val uint64)
127
+
128
+ //go:noescape
129
+ func StoreReluintptr(ptr *uintptr, val uintptr)
130
+
131
+ // StorepNoWB performs *ptr = val atomically and without a write
132
+ // barrier.
133
+ //
134
+ // NO go:noescape annotation; see atomic_pointer.go.
135
+ func StorepNoWB(ptr unsafe.Pointer, val unsafe.Pointer)
platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_amd64.s ADDED
@@ -0,0 +1,289 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Note: some of these functions are semantically inlined
6
+ // by the compiler (in src/cmd/compile/internal/gc/ssa.go).
7
+
8
+ #include "textflag.h"
9
+
10
+ TEXT 路Loaduintptr(SB), NOSPLIT, $0-16
11
+ JMP 路Load64(SB)
12
+
13
+ TEXT 路Loaduint(SB), NOSPLIT, $0-16
14
+ JMP 路Load64(SB)
15
+
16
+ TEXT 路Loadint32(SB), NOSPLIT, $0-12
17
+ JMP 路Load(SB)
18
+
19
+ TEXT 路Loadint64(SB), NOSPLIT, $0-16
20
+ JMP 路Load64(SB)
21
+
22
+ // bool Cas(int32 *val, int32 old, int32 new)
23
+ // Atomically:
24
+ // if(*val == old){
25
+ // *val = new;
26
+ // return 1;
27
+ // } else
28
+ // return 0;
29
+ TEXT 路Cas(SB),NOSPLIT,$0-17
30
+ MOVQ ptr+0(FP), BX
31
+ MOVL old+8(FP), AX
32
+ MOVL new+12(FP), CX
33
+ LOCK
34
+ CMPXCHGL CX, 0(BX)
35
+ SETEQ ret+16(FP)
36
+ RET
37
+
38
+ // bool 路Cas64(uint64 *val, uint64 old, uint64 new)
39
+ // Atomically:
40
+ // if(*val == old){
41
+ // *val = new;
42
+ // return 1;
43
+ // } else {
44
+ // return 0;
45
+ // }
46
+ TEXT 路Cas64(SB), NOSPLIT, $0-25
47
+ MOVQ ptr+0(FP), BX
48
+ MOVQ old+8(FP), AX
49
+ MOVQ new+16(FP), CX
50
+ LOCK
51
+ CMPXCHGQ CX, 0(BX)
52
+ SETEQ ret+24(FP)
53
+ RET
54
+
55
+ // bool Casp1(void **val, void *old, void *new)
56
+ // Atomically:
57
+ // if(*val == old){
58
+ // *val = new;
59
+ // return 1;
60
+ // } else
61
+ // return 0;
62
+ TEXT 路Casp1(SB), NOSPLIT, $0-25
63
+ MOVQ ptr+0(FP), BX
64
+ MOVQ old+8(FP), AX
65
+ MOVQ new+16(FP), CX
66
+ LOCK
67
+ CMPXCHGQ CX, 0(BX)
68
+ SETEQ ret+24(FP)
69
+ RET
70
+
71
+ TEXT 路Casint32(SB), NOSPLIT, $0-17
72
+ JMP 路Cas(SB)
73
+
74
+ TEXT 路Casint64(SB), NOSPLIT, $0-25
75
+ JMP 路Cas64(SB)
76
+
77
+ TEXT 路Casuintptr(SB), NOSPLIT, $0-25
78
+ JMP 路Cas64(SB)
79
+
80
+ TEXT 路CasRel(SB), NOSPLIT, $0-17
81
+ JMP 路Cas(SB)
82
+
83
+ // uint32 Xadd(uint32 volatile *val, int32 delta)
84
+ // Atomically:
85
+ // *val += delta;
86
+ // return *val;
87
+ TEXT 路Xadd(SB), NOSPLIT, $0-20
88
+ MOVQ ptr+0(FP), BX
89
+ MOVL delta+8(FP), AX
90
+ MOVL AX, CX
91
+ LOCK
92
+ XADDL AX, 0(BX)
93
+ ADDL CX, AX
94
+ MOVL AX, ret+16(FP)
95
+ RET
96
+
97
+ // uint64 Xadd64(uint64 volatile *val, int64 delta)
98
+ // Atomically:
99
+ // *val += delta;
100
+ // return *val;
101
+ TEXT 路Xadd64(SB), NOSPLIT, $0-24
102
+ MOVQ ptr+0(FP), BX
103
+ MOVQ delta+8(FP), AX
104
+ MOVQ AX, CX
105
+ LOCK
106
+ XADDQ AX, 0(BX)
107
+ ADDQ CX, AX
108
+ MOVQ AX, ret+16(FP)
109
+ RET
110
+
111
+ TEXT 路Xaddint32(SB), NOSPLIT, $0-20
112
+ JMP 路Xadd(SB)
113
+
114
+ TEXT 路Xaddint64(SB), NOSPLIT, $0-24
115
+ JMP 路Xadd64(SB)
116
+
117
+ TEXT 路Xadduintptr(SB), NOSPLIT, $0-24
118
+ JMP 路Xadd64(SB)
119
+
120
+ // uint32 Xchg(ptr *uint32, new uint32)
121
+ // Atomically:
122
+ // old := *ptr;
123
+ // *ptr = new;
124
+ // return old;
125
+ TEXT 路Xchg(SB), NOSPLIT, $0-20
126
+ MOVQ ptr+0(FP), BX
127
+ MOVL new+8(FP), AX
128
+ XCHGL AX, 0(BX)
129
+ MOVL AX, ret+16(FP)
130
+ RET
131
+
132
+ // uint64 Xchg64(ptr *uint64, new uint64)
133
+ // Atomically:
134
+ // old := *ptr;
135
+ // *ptr = new;
136
+ // return old;
137
+ TEXT 路Xchg64(SB), NOSPLIT, $0-24
138
+ MOVQ ptr+0(FP), BX
139
+ MOVQ new+8(FP), AX
140
+ XCHGQ AX, 0(BX)
141
+ MOVQ AX, ret+16(FP)
142
+ RET
143
+
144
+ TEXT 路Xchgint32(SB), NOSPLIT, $0-20
145
+ JMP 路Xchg(SB)
146
+
147
+ TEXT 路Xchgint64(SB), NOSPLIT, $0-24
148
+ JMP 路Xchg64(SB)
149
+
150
+ TEXT 路Xchguintptr(SB), NOSPLIT, $0-24
151
+ JMP 路Xchg64(SB)
152
+
153
+ TEXT 路StorepNoWB(SB), NOSPLIT, $0-16
154
+ MOVQ ptr+0(FP), BX
155
+ MOVQ val+8(FP), AX
156
+ XCHGQ AX, 0(BX)
157
+ RET
158
+
159
+ TEXT 路Store(SB), NOSPLIT, $0-12
160
+ MOVQ ptr+0(FP), BX
161
+ MOVL val+8(FP), AX
162
+ XCHGL AX, 0(BX)
163
+ RET
164
+
165
+ TEXT 路Store8(SB), NOSPLIT, $0-9
166
+ MOVQ ptr+0(FP), BX
167
+ MOVB val+8(FP), AX
168
+ XCHGB AX, 0(BX)
169
+ RET
170
+
171
+ TEXT 路Store64(SB), NOSPLIT, $0-16
172
+ MOVQ ptr+0(FP), BX
173
+ MOVQ val+8(FP), AX
174
+ XCHGQ AX, 0(BX)
175
+ RET
176
+
177
+ TEXT 路Storeint32(SB), NOSPLIT, $0-12
178
+ JMP 路Store(SB)
179
+
180
+ TEXT 路Storeint64(SB), NOSPLIT, $0-16
181
+ JMP 路Store64(SB)
182
+
183
+ TEXT 路Storeuintptr(SB), NOSPLIT, $0-16
184
+ JMP 路Store64(SB)
185
+
186
+ TEXT 路StoreRel(SB), NOSPLIT, $0-12
187
+ JMP 路Store(SB)
188
+
189
+ TEXT 路StoreRel64(SB), NOSPLIT, $0-16
190
+ JMP 路Store64(SB)
191
+
192
+ TEXT 路StoreReluintptr(SB), NOSPLIT, $0-16
193
+ JMP 路Store64(SB)
194
+
195
+ // void 路Or8(byte volatile*, byte);
196
+ TEXT 路Or8(SB), NOSPLIT, $0-9
197
+ MOVQ ptr+0(FP), AX
198
+ MOVB val+8(FP), BX
199
+ LOCK
200
+ ORB BX, (AX)
201
+ RET
202
+
203
+ // void 路And8(byte volatile*, byte);
204
+ TEXT 路And8(SB), NOSPLIT, $0-9
205
+ MOVQ ptr+0(FP), AX
206
+ MOVB val+8(FP), BX
207
+ LOCK
208
+ ANDB BX, (AX)
209
+ RET
210
+
211
+ // func Or(addr *uint32, v uint32)
212
+ TEXT 路Or(SB), NOSPLIT, $0-12
213
+ MOVQ ptr+0(FP), AX
214
+ MOVL val+8(FP), BX
215
+ LOCK
216
+ ORL BX, (AX)
217
+ RET
218
+
219
+ // func And(addr *uint32, v uint32)
220
+ TEXT 路And(SB), NOSPLIT, $0-12
221
+ MOVQ ptr+0(FP), AX
222
+ MOVL val+8(FP), BX
223
+ LOCK
224
+ ANDL BX, (AX)
225
+ RET
226
+
227
+ // func Or32(addr *uint32, v uint32) old uint32
228
+ TEXT 路Or32(SB), NOSPLIT, $0-20
229
+ MOVQ ptr+0(FP), BX
230
+ MOVL val+8(FP), CX
231
+ casloop:
232
+ MOVL CX, DX
233
+ MOVL (BX), AX
234
+ ORL AX, DX
235
+ LOCK
236
+ CMPXCHGL DX, (BX)
237
+ JNZ casloop
238
+ MOVL AX, ret+16(FP)
239
+ RET
240
+
241
+ // func And32(addr *uint32, v uint32) old uint32
242
+ TEXT 路And32(SB), NOSPLIT, $0-20
243
+ MOVQ ptr+0(FP), BX
244
+ MOVL val+8(FP), CX
245
+ casloop:
246
+ MOVL CX, DX
247
+ MOVL (BX), AX
248
+ ANDL AX, DX
249
+ LOCK
250
+ CMPXCHGL DX, (BX)
251
+ JNZ casloop
252
+ MOVL AX, ret+16(FP)
253
+ RET
254
+
255
+ // func Or64(addr *uint64, v uint64) old uint64
256
+ TEXT 路Or64(SB), NOSPLIT, $0-24
257
+ MOVQ ptr+0(FP), BX
258
+ MOVQ val+8(FP), CX
259
+ casloop:
260
+ MOVQ CX, DX
261
+ MOVQ (BX), AX
262
+ ORQ AX, DX
263
+ LOCK
264
+ CMPXCHGQ DX, (BX)
265
+ JNZ casloop
266
+ MOVQ AX, ret+16(FP)
267
+ RET
268
+
269
+ // func And64(addr *uint64, v uint64) old uint64
270
+ TEXT 路And64(SB), NOSPLIT, $0-24
271
+ MOVQ ptr+0(FP), BX
272
+ MOVQ val+8(FP), CX
273
+ casloop:
274
+ MOVQ CX, DX
275
+ MOVQ (BX), AX
276
+ ANDQ AX, DX
277
+ LOCK
278
+ CMPXCHGQ DX, (BX)
279
+ JNZ casloop
280
+ MOVQ AX, ret+16(FP)
281
+ RET
282
+
283
+ // func Anduintptr(addr *uintptr, v uintptr) old uintptr
284
+ TEXT 路Anduintptr(SB), NOSPLIT, $0-24
285
+ JMP 路And64(SB)
286
+
287
+ // func Oruintptr(addr *uintptr, v uintptr) old uintptr
288
+ TEXT 路Oruintptr(SB), NOSPLIT, $0-24
289
+ JMP 路Or64(SB)
platform/dbops/binaries/go/go/src/runtime/internal/atomic/atomic_andor_generic.go ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ //go:build arm || s390x || loong64 || mips || mipsle || mips64 || mips64le || wasm
6
+
7
+ package atomic
8
+
9
+ //go:nosplit
10
+ func And32(ptr *uint32, val uint32) uint32 {
11
+ for {
12
+ old := *ptr
13
+ if Cas(ptr, old, old&val) {
14
+ return old
15
+ }
16
+ }
17
+ }
18
+
19
+ //go:nosplit
20
+ func Or32(ptr *uint32, val uint32) uint32 {
21
+ for {
22
+ old := *ptr
23
+ if Cas(ptr, old, old|val) {
24
+ return old
25
+ }
26
+ }
27
+ }
28
+
29
+ //go:nosplit
30
+ func And64(ptr *uint64, val uint64) uint64 {
31
+ for {
32
+ old := *ptr
33
+ if Cas64(ptr, old, old&val) {
34
+ return old
35
+ }
36
+ }
37
+ }
38
+
39
+ //go:nosplit
40
+ func Or64(ptr *uint64, val uint64) uint64 {
41
+ for {
42
+ old := *ptr
43
+ if Cas64(ptr, old, old|val) {
44
+ return old
45
+ }
46
+ }
47
+ }
48
+
49
+ //go:nosplit
50
+ func Anduintptr(ptr *uintptr, val uintptr) uintptr {
51
+ for {
52
+ old := *ptr
53
+ if Casuintptr(ptr, old, old&val) {
54
+ return old
55
+ }
56
+ }
57
+ }
58
+
59
+ //go:nosplit
60
+ func Oruintptr(ptr *uintptr, val uintptr) uintptr {
61
+ for {
62
+ old := *ptr
63
+ if Casuintptr(ptr, old, old|val) {
64
+ return old
65
+ }
66
+ }
67
+ }