Skip to content

Closures of a second function init() or _() happens to merge with the closures of the first #75

Done

Description

@rahlk

Describe the bug
I found this in #55. When a file has two or more init() functions (or _() anon functions) in the same file that never binds to a name, they get signatures <pkg>.init.2 because of uniqueFunctionSignature method. But the symbol table add this suffix only after it calls buildCallable first. As a result, the closures of init.2 keep the the signatures of <pkg>.init.closure_N (i.e., the same as that of the first init init).

The closure's ids are different. They get …/init/closure_1 and …/init:2/closure_1, because the id comes from the parent id. Everything downstream that is keyed on the signature merges: the L3 unit, the L2 call-graph edge and the signature index.

To Reproduce

Take this file main.go for example:

   package main

   import "fmt"

   func init() {
   	f := func() { fmt.Println("first") }
   	f()
   }

   func init() {
   	g := func() { fmt.Println("second") }
   	g()
   }

   func main() {}
  1. Run cango -i . -a 3 -o out.
  2. Look at the two closure_1 callables in out/analysis.json.

Expected behavior

  • init:2/closure_1 has its own body and its own cfg, at line 11.
  • The call graph has two edges to fmt.Println, one from each closure.

Logs
From a build of PR #55:

init/closure_1    body {6:16, @entry, @exit}
                  cfg  [@entry→6:16, 6:16→@exit, 6:16→@exit]----------+
init:2/closure_1  body {11:16, 6:16, @entry, @exit}               (same cfg's)
                  cfg  [@entry→6:16, 6:16→@exit, 6:16→@exit]----------+
call_graph:       init:2/closure_1 → fmt/Println   (one edge only)
  • init:2/closure_1 has the cfg of the first closure. Its body has a node at line 6, which is outside its span (line 11).
  • Both closures have a duplicate 6:16→@exit edge.
  • The edge from init/closure_1 to fmt.Println is missing.
  • degraded is empty, so nothing tells the user.

Additional context

  • The location of the bug seems to be internal/syntactic_analysis/symbol_table.go:273. The suffix is set after buildCallable, which calls buildInnerCallables(pkg, sig, …) with the plain signature (line 640).
  • The fix is to compute the unique signature before buildCallable and pass it in, so the closures get <pkg>.init:2.closure_N.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions