Debian Codegraph @

tools/antithesis-go-toolexec/policy.go

raw
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "slices"
7 "strings"
8
9 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
10 )
11
12 // Package is what we learn about the package being compiled, from the compile
13 // invocation itself.
14 type Package struct {
15 // ImportPath comes from TOOLEXEC_IMPORTPATH, which the go command sets to the
16 // package being built.
17 ImportPath string
18
19 // GoFiles are the source files from the end of the argument list, carrying the
20 // index each was found at so rewritten copies can be substituted in place.
21 GoFiles []fileArg
22
23 // ImportCfg is the index of the value following -importcfg.
24 ImportCfg int
25 }
26
27 type fileArg struct {
28 Index int
29 Path string
30 }
31
32 // selectedPackage decides whether this compile invocation is one we act on, and
33 // extracts what we need from its arguments if so. The same selection governs both
34 // coverage instrumentation and cataloguing.
35 //
36 // Three categories are always skipped:
37 //
38 // - The standard library. The go command marks those compiles with -std. Some
39 // of them (runtime, internal/abi) cannot tolerate a Notify call at all, and
40 // instrumenting them would also mean instrumenting the code paths our own
41 // notifications travel through.
42 // - The SDK itself, for that same reentrancy reason.
43 // - Anything outside the configured scope; see inScope.
44 func selectedPackage(cfg *Config, args []string) (*Package, bool) {
45 importPath := os.Getenv("TOOLEXEC_IMPORTPATH")
46 if importPath == "" {
47 // No import path means the go command is doing something other than
48 // building a package we could instrument.
49 return nil, false
50 }
51 if slices.Contains(args, "-std") || isSDKPackage(importPath) {
52 return nil, false
53 }
54
55 pkg := &Package{ImportPath: importPath, ImportCfg: -1, GoFiles: sourceFiles(args)}
56 for i, arg := range args {
57 if arg == "-importcfg" && i+1 < len(args) {
58 pkg.ImportCfg = i + 1
59 }
60 }
61 if len(pkg.GoFiles) == 0 || pkg.ImportCfg < 0 {
62 return nil, false
63 }
64 if !inScope(cfg, importPath, pkg.GoFiles[0].Path) {
65 return nil, false
66 }
67
68 pkg.GoFiles = slices.DeleteFunc(pkg.GoFiles, func(f fileArg) bool {
69 if skip := skipFile(cfg, f.Path); skip {
70 common.Logger.Printf(common.Info, "Skipping %s", f.Path)
71 return true
72 }
73 return false
74 })
75 return pkg, len(pkg.GoFiles) > 0
76 }
77
78 // sourceFiles returns the source files a compile invocation was given.
79 //
80 // The go command always places the file list last and passes only ".go" files.
81 // No flag value can be mistaken for a source file: -importcfg, -embedcfg and
82 // -symabis all point at files named after the flag itself.
83 func sourceFiles(args []string) []fileArg {
84 first := len(args)
85 for first > 0 && strings.HasSuffix(args[first-1], ".go") {
86 first--
87 }
88 files := make([]fileArg, 0, len(args)-first)
89 for i := first; i < len(args); i++ {
90 files = append(files, fileArg{Index: i, Path: args[i]})
91 }
92 return files
93 }
94
95 // skipFile applies the same exclusions the whole-tree instrumentor honours, so
96 // that the two workflows agree about what is covered.
97 func skipFile(cfg *Config, path string) bool {
98 if cfg.SkipTestFiles && strings.HasSuffix(path, "_test.go") {
99 return true
100 }
101 // Generated code has no hand-written branches worth attributing, and protobuf
102 // output in particular is enormous.
103 if strings.HasSuffix(path, ".pb.go") {
104 return true
105 }
106 if cfg.Exclusions[path] {
107 return true
108 }
109 for excluded := range cfg.Exclusions {
110 excluded = strings.TrimSuffix(excluded, "/")
111 if path == excluded || strings.HasPrefix(path, excluded+string(filepath.Separator)) {
112 return true
113 }
114 }
115 return false
116 }
117
118 func isSDKPackage(importPath string) bool {
119 return importPath == common.ANTITHESIS_SDK_MODULE ||
120 strings.HasPrefix(importPath, common.ANTITHESIS_SDK_MODULE+"/")
121 }
122
123 // inScope reports whether this package is one we were asked to instrument.
124 //
125 // With ANTITHESIS_INSTRUMENT set we match import path prefixes. With it unset the
126 // default is "code in the working tree": anything whose sources live outside
127 // GOROOT and outside the module cache. That covers the main module and any
128 // locally replaced module, and excludes downloaded dependencies.
129 func inScope(cfg *Config, importPath, sourcePath string) bool {
130 if len(cfg.Instrument) > 0 {
131 for _, prefix := range cfg.Instrument {
132 prefix = strings.TrimSuffix(prefix, "/")
133 if importPath == prefix || strings.HasPrefix(importPath, prefix+"/") {
134 return true
135 }
136 }
137 return false
138 }
139 return isWorkingTreeSource(sourcePath)
140 }