Skip to content

Commit 49e542a

Browse files
felixgecagedmantis
authored andcommitted
runtime: fix GoroutineProfile stacks not getting null terminated
Fix a regression introduced in CL 572396 causing goroutine stacks not getting null terminated. This bug impacts callers that reuse the []StackRecord slice for multiple calls to GoroutineProfile. See felixge/fgprof#33 for an example of the problem. Add a test case to prevent similar regressions in the future. Use null padding instead of null termination to be consistent with other profile types and because it's less code to implement. Also fix the ThreadCreateProfile code path. Fixes #69243 Change-Id: I0b9414f6c694c304bc03a5682586f619e9bf0588 Reviewed-on: https://go-review.googlesource.com/c/go/+/609815 Reviewed-by: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent c64ca8c commit 49e542a

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

src/runtime/mprof.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,8 @@ func pprof_mutexProfileInternal(p []profilerecord.BlockProfileRecord) (n int, ok
12771277
// of calling ThreadCreateProfile directly.
12781278
func ThreadCreateProfile(p []StackRecord) (n int, ok bool) {
12791279
return threadCreateProfileInternal(len(p), func(r profilerecord.StackRecord) {
1280-
copy(p[0].Stack0[:], r.Stack)
1280+
i := copy(p[0].Stack0[:], r.Stack)
1281+
clear(p[0].Stack0[i:])
12811282
p = p[1:]
12821283
})
12831284
}
@@ -1656,7 +1657,8 @@ func GoroutineProfile(p []StackRecord) (n int, ok bool) {
16561657
return
16571658
}
16581659
for i, mr := range records[0:n] {
1659-
copy(p[i].Stack0[:], mr.Stack)
1660+
l := copy(p[i].Stack0[:], mr.Stack)
1661+
clear(p[i].Stack0[l:])
16601662
}
16611663
return
16621664
}

src/runtime/pprof/pprof_test.go

+82-10
Original file line numberDiff line numberDiff line change
@@ -2471,16 +2471,7 @@ func TestTimeVDSO(t *testing.T) {
24712471
}
24722472

24732473
func TestProfilerStackDepth(t *testing.T) {
2474-
// Disable sampling, otherwise it's difficult to assert anything.
2475-
oldMemRate := runtime.MemProfileRate
2476-
runtime.MemProfileRate = 1
2477-
runtime.SetBlockProfileRate(1)
2478-
oldMutexRate := runtime.SetMutexProfileFraction(1)
2479-
t.Cleanup(func() {
2480-
runtime.MemProfileRate = oldMemRate
2481-
runtime.SetBlockProfileRate(0)
2482-
runtime.SetMutexProfileFraction(oldMutexRate)
2483-
})
2474+
t.Cleanup(disableSampling())
24842475

24852476
const depth = 128
24862477
go produceProfileEvents(t, depth)
@@ -2772,3 +2763,84 @@ runtime/pprof.inlineA`,
27722763
})
27732764
}
27742765
}
2766+
2767+
func TestProfileRecordNullPadding(t *testing.T) {
2768+
// Produce events for the different profile types.
2769+
t.Cleanup(disableSampling())
2770+
memSink = make([]byte, 1) // MemProfile
2771+
<-time.After(time.Millisecond) // BlockProfile
2772+
blockMutex(t) // MutexProfile
2773+
runtime.GC()
2774+
2775+
// Test that all profile records are null padded.
2776+
testProfileRecordNullPadding(t, "MutexProfile", runtime.MutexProfile)
2777+
testProfileRecordNullPadding(t, "GoroutineProfile", runtime.GoroutineProfile)
2778+
testProfileRecordNullPadding(t, "BlockProfile", runtime.BlockProfile)
2779+
testProfileRecordNullPadding(t, "MemProfile/inUseZero=true", func(p []runtime.MemProfileRecord) (int, bool) {
2780+
return runtime.MemProfile(p, true)
2781+
})
2782+
testProfileRecordNullPadding(t, "MemProfile/inUseZero=false", func(p []runtime.MemProfileRecord) (int, bool) {
2783+
return runtime.MemProfile(p, false)
2784+
})
2785+
// Not testing ThreadCreateProfile because it is broken, see issue 6104.
2786+
}
2787+
2788+
func testProfileRecordNullPadding[T runtime.StackRecord | runtime.MemProfileRecord | runtime.BlockProfileRecord](t *testing.T, name string, fn func([]T) (int, bool)) {
2789+
stack0 := func(sr *T) *[32]uintptr {
2790+
switch t := any(sr).(type) {
2791+
case *runtime.StackRecord:
2792+
return &t.Stack0
2793+
case *runtime.MemProfileRecord:
2794+
return &t.Stack0
2795+
case *runtime.BlockProfileRecord:
2796+
return &t.Stack0
2797+
default:
2798+
panic(fmt.Sprintf("unexpected type %T", sr))
2799+
}
2800+
}
2801+
2802+
t.Run(name, func(t *testing.T) {
2803+
var p []T
2804+
for {
2805+
n, ok := fn(p)
2806+
if ok {
2807+
p = p[:n]
2808+
break
2809+
}
2810+
p = make([]T, n*2)
2811+
for i := range p {
2812+
s0 := stack0(&p[i])
2813+
for j := range s0 {
2814+
// Poison the Stack0 array to identify lack of zero padding
2815+
s0[j] = ^uintptr(0)
2816+
}
2817+
}
2818+
}
2819+
2820+
if len(p) == 0 {
2821+
t.Fatal("no records found")
2822+
}
2823+
2824+
for _, sr := range p {
2825+
for i, v := range stack0(&sr) {
2826+
if v == ^uintptr(0) {
2827+
t.Fatalf("record p[%d].Stack0 is not null padded: %+v", i, sr)
2828+
}
2829+
}
2830+
}
2831+
})
2832+
}
2833+
2834+
// disableSampling configures the profilers to capture all events, otherwise
2835+
// it's difficult to assert anything.
2836+
func disableSampling() func() {
2837+
oldMemRate := runtime.MemProfileRate
2838+
runtime.MemProfileRate = 1
2839+
runtime.SetBlockProfileRate(1)
2840+
oldMutexRate := runtime.SetMutexProfileFraction(1)
2841+
return func() {
2842+
runtime.MemProfileRate = oldMemRate
2843+
runtime.SetBlockProfileRate(0)
2844+
runtime.SetMutexProfileFraction(oldMutexRate)
2845+
}
2846+
}

0 commit comments

Comments
 (0)