1// Copyright 2017 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
5package runtime
6
7import "unsafe"
8
9var labelSync uintptr
10
11// runtime_setProfLabel should be an internal detail,
12// but widely used packages access it using linkname.
13// Notable members of the hall of shame include:
14//   - github.com/cloudwego/localsession
15//   - github.com/DataDog/datadog-agent
16//
17// Do not remove or change the type signature.
18// See go.dev/issue/67401.
19//
20//go:linkname runtime_setProfLabel runtime/pprof.runtime_setProfLabel
21func runtime_setProfLabel(labels unsafe.Pointer) {
22	// Introduce race edge for read-back via profile.
23	// This would more properly use &getg().labels as the sync address,
24	// but we do the read in a signal handler and can't call the race runtime then.
25	//
26	// This uses racereleasemerge rather than just racerelease so
27	// the acquire in profBuf.read synchronizes with *all* prior
28	// setProfLabel operations, not just the most recent one. This
29	// is important because profBuf.read will observe different
30	// labels set by different setProfLabel operations on
31	// different goroutines, so it needs to synchronize with all
32	// of them (this wouldn't be an issue if we could synchronize
33	// on &getg().labels since we would synchronize with each
34	// most-recent labels write separately.)
35	//
36	// racereleasemerge is like a full read-modify-write on
37	// labelSync, rather than just a store-release, so it carries
38	// a dependency on the previous racereleasemerge, which
39	// ultimately carries forward to the acquire in profBuf.read.
40	if raceenabled {
41		racereleasemerge(unsafe.Pointer(&labelSync))
42	}
43	getg().labels = labels
44}
45
46// runtime_getProfLabel should be an internal detail,
47// but widely used packages access it using linkname.
48// Notable members of the hall of shame include:
49//   - github.com/cloudwego/localsession
50//   - github.com/DataDog/datadog-agent
51//
52// Do not remove or change the type signature.
53// See go.dev/issue/67401.
54//
55//go:linkname runtime_getProfLabel runtime/pprof.runtime_getProfLabel
56func runtime_getProfLabel() unsafe.Pointer {
57	return getg().labels
58}
59