From ef238ae40b1cba74c7899b429055fb53d93ef3fe Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Tue, 18 Oct 2022 14:27:08 -0700 Subject: [PATCH] runtime/pprof: make TestConvertCPUProfile more resilient Updates the test to be more forgiving when it comes to how a processes memory maps are laid out. --- src/runtime/pprof/proto_test.go | 34 +++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/runtime/pprof/proto_test.go b/src/runtime/pprof/proto_test.go index 780b481de83b7c..3b9019ffb57a5d 100644 --- a/src/runtime/pprof/proto_test.go +++ b/src/runtime/pprof/proto_test.go @@ -15,6 +15,7 @@ import ( "os/exec" "reflect" "runtime" + "strconv" "strings" "testing" "unsafe" @@ -95,11 +96,17 @@ func testPCs(t *testing.T) (addr1, addr2 uint64, map1, map2 *profile.Mapping) { // region of memory. t.Skipf("need 2 or more mappings, got %v", len(mprof.Mapping)) } - addr1 = mprof.Mapping[0].Start + + addr1 = findAddrInExecutableSection(t, mmap, mprof.Mapping[0]) map1 = mprof.Mapping[0] + map1.Offset = (addr1 - map1.Start) + map1.Offset + map1.Start = addr1 map1.BuildID, _ = elfBuildID(map1.File) - addr2 = mprof.Mapping[1].Start + + addr2 = findAddrInExecutableSection(t, mmap, mprof.Mapping[1]) map2 = mprof.Mapping[1] + map2.Offset = (addr2 - map2.Start) + map2.Offset + map2.Start = addr2 map2.BuildID, _ = elfBuildID(map2.File) case "windows": addr1 = uint64(abi.FuncPCABIInternal(f1)) @@ -145,6 +152,29 @@ func testPCs(t *testing.T) (addr1, addr2 uint64, map1, map2 *profile.Mapping) { return } +func findAddrInExecutableSection(t *testing.T, mmap []byte, m *profile.Mapping) uint64 { + mappings := strings.Split(string(mmap), "\n") + for _, mapping := range mappings { + parts := strings.Fields(mapping) + if len(parts) < 6 { + continue + } + if !strings.Contains(parts[1], "x") { + continue + } + addr, err := strconv.ParseUint(strings.Split(parts[0], "-")[0], 16, 64) + if err != nil { + t.Fatal(err) + } + if addr >= m.Start && addr < m.Limit { + return addr + } + } + + t.Error("could not find executable section in /proc/self/maps") + return 0 +} + func TestConvertCPUProfile(t *testing.T) { addr1, addr2, map1, map2 := testPCs(t)