Skip to content

cmd/compile: fix duplicated dwarf parameters for some functions #61529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cmd/internal/dwarf/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,11 +1204,21 @@ func putPrunedScopes(ctxt Context, s *FnState, fnabbrev int) error {
}
scopes := make([]Scope, len(s.Scopes), len(s.Scopes))
pvars := inlinedVarTable(&s.InlCalls)
dedupvars := make(map[string]bool)
for k, s := range s.Scopes {
var pruned Scope = Scope{Parent: s.Parent, Ranges: s.Ranges}
for i := 0; i < len(s.Vars); i++ {
// FIXME: This map was added to deduplicate the list of variables.
// When generating the formal parameters from the symbols some variables
// were added to the scope more than once.
_, dup := dedupvars[s.Vars[i].Name]
if dup {
continue
}

_, found := pvars[s.Vars[i]]
if !found {
dedupvars[s.Vars[i].Name] = true
pruned.Vars = append(pruned.Vars, s.Vars[i])
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/cmd/link/internal/ld/dwarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,7 @@ func processParams(die *dwarf.Entry, ex *dwtest.Examiner) string {
// Walk the subprogram DIE's children looking for params.
pIdx := ex.IdxFromOffset(die.Offset)
childDies := ex.Children(pIdx)
entryName := die.Val(dwarf.AttrName)
idx := 0
for _, child := range childDies {
if child.Tag == dwarf.TagFormalParameter {
Expand All @@ -1626,6 +1627,9 @@ func processParams(die *dwarf.Entry, ex *dwtest.Examiner) string {
}
}
if name, ok := child.Val(dwarf.AttrName).(string); ok {
if _, ok := foundParams[name]; ok {
panic(fmt.Sprintf("Found duplicated child parameter %s while parsing dwarf entry %s\n", name, entryName))
}
foundParams[name] = fmt.Sprintf("%d:%d", idx, st)
idx++
}
Expand Down Expand Up @@ -2024,3 +2028,33 @@ func main() {
})
}
}

func TestIssue61357(t *testing.T) {
// This test asserts that parameters are not duplicated
// in the generated dwarf symbols.
testenv.MustHaveGoBuild(t)

mustHaveDWARF(t)
t.Parallel()

const prog = `
package main

import "fmt"

func main() {
fmt.Println("Hello")
}
`
_, ex := gobuildAndExamine(t, prog, NoOpt)

die := findSubprogramDIE(t, ex, "internal/poll.(*FD).Write")

found := processParams(die, ex)

expected := "[fd:0:1 p:1:1 ~r0:2:2 ~r1:3:2]"
if found != expected {
t.Errorf("param check failed, wanted:\n%s\ngot:\n%s\n",
expected, found)
}
}