Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version-file: go.mod

- name: Build
run: go build -v ./...
Expand Down
103 changes: 95 additions & 8 deletions callgraphutil/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package callgraphutil
import (
"bytes"
"fmt"
"go/token"
"go/types"

"golang.org/x/tools/go/callgraph"
Expand Down Expand Up @@ -48,19 +49,34 @@ func NewGraph(root *ssa.Function, srcFns ...*ssa.Function) (*callgraph.Graph, er

allFns := ssautil.AllFunctions(root.Prog)

for _, srcFn := range srcFns {
// debug("adding src function %d/%d: %v\n", i+1, len(srcFns), srcFn)
visited := make(map[*ssa.Function]bool)

err := AddFunction(g, srcFn, allFns)
if err != nil {
return g, fmt.Errorf("failed to add src function %v: %w", srcFn, err)
var walkFn func(fn *ssa.Function) error
walkFn = func(fn *ssa.Function) error {
if visited[fn] {
return nil
}
visited[fn] = true

if err := AddFunction(g, fn, allFns); err != nil {
return fmt.Errorf("failed to add function %v: %w", fn, err)
}

for _, block := range srcFn.DomPreorder() {
for _, block := range fn.DomPreorder() {
for _, instr := range block.Instrs {
checkBlockInstruction(root, allFns, g, srcFn, instr)
if err := checkBlockInstruction(root, allFns, g, fn, instr, walkFn); err != nil {
return err
}
}
}

return nil
}

for _, srcFn := range srcFns {
if err := walkFn(srcFn); err != nil {
return g, err
}
}

return g, nil
Expand All @@ -69,7 +85,7 @@ func NewGraph(root *ssa.Function, srcFns ...*ssa.Function) (*callgraph.Graph, er
// checkBlockInstruction checks the given instruction for any function calls, adding
// edges to the call graph as needed and recursively adding any new functions to the graph
// that are discovered during the process (typically via interface methods).
func checkBlockInstruction(root *ssa.Function, allFns map[*ssa.Function]bool, g *callgraph.Graph, fn *ssa.Function, instr ssa.Instruction) error {
func checkBlockInstruction(root *ssa.Function, allFns map[*ssa.Function]bool, g *callgraph.Graph, fn *ssa.Function, instr ssa.Instruction, walkFn func(*ssa.Function) error) error {
// debug("\tcheckBlockInstruction: %v\n", instr)
switch instrt := instr.(type) {
case *ssa.Call:
Expand Down Expand Up @@ -159,6 +175,15 @@ func checkBlockInstruction(root *ssa.Function, allFns map[*ssa.Function]bool, g
case *ssa.Function:
instrCall = calltFn
}
case *ssa.UnOp:
if callt.Op == token.MUL {
switch fa := callt.X.(type) {
case *ssa.FieldAddr:
instrCall = findFunctionInField(fa, allFns)
case *ssa.Field:
instrCall = findFunctionInFieldValue(fa, allFns)
}
}
case *ssa.Parameter:
// This is likely a method call, so we need to
// get the function from the method receiver which
Expand Down Expand Up @@ -204,6 +229,10 @@ func checkBlockInstruction(root *ssa.Function, allFns map[*ssa.Function]bool, g
return fmt.Errorf("failed to add function %v from block instr: %w", instrCall, err)
}

if err := walkFn(instrCall); err != nil {
return err
}

// attempt to link function arguments that are functions
for a := 0; a < len(instrt.Call.Args); a++ {
arg := instrt.Call.Args[a]
Expand Down Expand Up @@ -306,3 +335,61 @@ func AddFunction(cg *callgraph.Graph, target *ssa.Function, allFns map[*ssa.Func

return nil
}

// findFunctionInField scans all functions for assignments to the provided
// struct field address and returns the first discovered function value.
func findFunctionInField(fieldAddr *ssa.FieldAddr, allFns map[*ssa.Function]bool) *ssa.Function {
idx := fieldAddr.Field
structType := fieldAddr.X.Type()

for fn := range allFns {
for _, blk := range fn.Blocks {
for _, ins := range blk.Instrs {
if store, ok := ins.(*ssa.Store); ok {
if fa, ok := store.Addr.(*ssa.FieldAddr); ok {
if fa.Field == idx && types.Identical(fa.X.Type(), structType) {
switch v := store.Val.(type) {
case *ssa.Function:
return v
case *ssa.MakeClosure:
if f, ok := v.Fn.(*ssa.Function); ok {
return f
}
}
}
}
}
}
}
}
return nil
}

// findFunctionInFieldValue searches for function assignments made to the struct
// field represented by the given Field value.
func findFunctionInFieldValue(field *ssa.Field, allFns map[*ssa.Function]bool) *ssa.Function {
idx := field.Field
structType := field.X.Type()

for fn := range allFns {
for _, blk := range fn.Blocks {
for _, ins := range blk.Instrs {
if store, ok := ins.(*ssa.Store); ok {
if fa, ok := store.Addr.(*ssa.FieldAddr); ok {
if fa.Field == idx && types.Identical(fa.X.Type(), structType) {
switch v := store.Val.(type) {
case *ssa.Function:
return v
case *ssa.MakeClosure:
if f, ok := v.Fn.(*ssa.Function); ok {
return f
}
}
}
}
}
}
}
}
return nil
}
36 changes: 36 additions & 0 deletions callgraphutil/struct_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package callgraphutil_test

import (
"context"
"path/filepath"
"testing"

"github.com/picatz/taint/callgraphutil"
)

func TestStructFieldCallGraph(t *testing.T) {
dir, err := filepath.Abs(filepath.Join("testdata"))
if err != nil {
t.Fatal(err)
}

pkgs, err := loadPackages(context.Background(), dir, "./...")
if err != nil {
t.Fatal(err)
}

mainFn, srcFns, err := loadSSA(context.Background(), pkgs)
if err != nil {
t.Fatal(err)
}

cg, err := loadCallGraph(context.Background(), mainFn, srcFns)
if err != nil {
t.Fatal(err)
}

target := "github.com/picatz/taint/callgraphutil/testdata.doSomething"
if paths := callgraphutil.PathsSearchCallTo(cg.Root, target); len(paths) == 0 {
t.Fatalf("expected path to %s", target)
}
}
53 changes: 53 additions & 0 deletions callgraphutil/testdata/struct_field_simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"os"
)

type command struct {
name string
run func(args []string) error
}

type commands []*command

func (c commands) run(args []string) error {
for i := 0; i < len(c); i++ {
cmd := c[i]
if cmd.name == args[0] {
return cmd.run(args[1:])
}
}
return fmt.Errorf("unknown command: %s", args[0])
}

type cli struct {
commands commands
}

func (c *cli) run(args []string) error {
return c.commands.run(args)
}

func doSomething() error {
fmt.Println("doing something")
return nil
}

func main() {
c := &cli{
commands{
{
name: "do-something",
run: func(args []string) error {
return doSomething()
},
},
},
}

if err := c.run(os.Args[1:]); err != nil {
panic(err)
}
}
51 changes: 28 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
module github.com/picatz/taint

go 1.21
go 1.24.4

require (
github.com/charmbracelet/lipgloss v0.9.1
github.com/go-git/go-git/v5 v5.11.0
golang.org/x/term v0.18.0
golang.org/x/tools v0.16.1
github.com/charmbracelet/lipgloss v1.1.0
github.com/go-git/go-git/v5 v5.16.2
golang.org/x/term v0.32.0
golang.org/x/tools v0.34.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
dario.cat/mergo v1.0.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.3.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/charmbracelet/colorprofile v0.3.1 // indirect
github.com/charmbracelet/x/ansi v0.9.3 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/cloudflare/circl v1.6.1 // indirect
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/go-git/go-billy/v5 v5.6.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/pjbgf/sha1cd v0.3.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sergi/go-diff v1.4.0 // indirect
github.com/skeema/knownhosts v1.3.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.39.0 // indirect
golang.org/x/mod v0.25.0 // indirect
golang.org/x/net v0.41.0 // indirect
golang.org/x/sync v0.15.0 // indirect
golang.org/x/sys v0.33.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading