| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"go/ast" |
| 7 |
"go/parser" |
| 8 |
"go/token" |
| 9 |
"math" |
| 10 |
"os" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/instrumentor" |
| 15 |
) |
| 16 |
|
| 17 |
// resolveCallbackName returns an identifier free of collisions in the package |
| 18 |
// whose files are paths. |
| 19 |
func resolveCallbackName(paths []string) (string, error) { |
| 20 |
taken, err := packageScopeNames(paths) |
| 21 |
if err != nil { |
| 22 |
return "", err |
| 23 |
} |
| 24 |
for attempt := 0; attempt <= math.MaxInt; attempt++ { |
| 25 |
if candidate := callbackCandidate(attempt); !taken[candidate] { |
| 26 |
return candidate, nil |
| 27 |
} |
| 28 |
} |
| 29 |
// a user package would need to declare math.MaxInt + 1 variables that conflict with candidates in order to reach this error |
| 30 |
// it's certainly possible for an adversarial package to do that (hence the handling), but it should never occur in practice |
| 31 |
return "", fmt.Errorf( |
| 32 |
"could not find a free name for the coverage callback: %s and %d suffixed variants are all declared in this package", |
| 33 |
instrumentor.InstrumentationPackageAlias, math.MaxInt) |
| 34 |
} |
| 35 |
|
| 36 |
// callbackCandidate names the nth choice. The zeroth is the default the |
| 37 |
// whole-tree instrumentor also uses, so an ordinary package is instrumented |
| 38 |
// identically by both front ends. |
| 39 |
func callbackCandidate(n int) string { |
| 40 |
base := instrumentor.InstrumentationPackageAlias |
| 41 |
if n == 0 { |
| 42 |
return base |
| 43 |
} |
| 44 |
// Keep the trailing underscores, so every variant still reads as generated. |
| 45 |
return strings.TrimSuffix(base, "__") + "_" + strconv.Itoa(n) + "__" |
| 46 |
} |
| 47 |
|
| 48 |
// packageScopeNames collects every identifier that would collide with a |
| 49 |
// package-level variable: top-level declarations from any file, and the names |
| 50 |
// files bind with their imports. |
| 51 |
func packageScopeNames(paths []string) (map[string]bool, error) { |
| 52 |
prefix := []byte(strings.TrimSuffix(instrumentor.InstrumentationPackageAlias, "__")) |
| 53 |
taken := map[string]bool{} |
| 54 |
for _, path := range paths { |
| 55 |
body, err := os.ReadFile(path) |
| 56 |
if err != nil { |
| 57 |
return nil, err |
| 58 |
} |
| 59 |
if !bytes.Contains(body, prefix) { |
| 60 |
continue |
| 61 |
} |
| 62 |
file, err := parser.ParseFile(token.NewFileSet(), path, body, parser.SkipObjectResolution) |
| 63 |
if err != nil { |
| 64 |
// The compiler is about to report this properly; nothing to add. |
| 65 |
return nil, fmt.Errorf("parsing %s: %w", path, err) |
| 66 |
} |
| 67 |
collectDeclaredNames(file, taken) |
| 68 |
} |
| 69 |
return taken, nil |
| 70 |
} |
| 71 |
|
| 72 |
func collectDeclaredNames(file *ast.File, taken map[string]bool) { |
| 73 |
for _, decl := range file.Decls { |
| 74 |
switch d := decl.(type) { |
| 75 |
case *ast.FuncDecl: |
| 76 |
// Methods are named in the type's scope, not the package's. |
| 77 |
if d.Recv == nil && d.Name != nil { |
| 78 |
taken[d.Name.Name] = true |
| 79 |
} |
| 80 |
case *ast.GenDecl: |
| 81 |
for _, spec := range d.Specs { |
| 82 |
switch s := spec.(type) { |
| 83 |
case *ast.ValueSpec: |
| 84 |
for _, name := range s.Names { |
| 85 |
taken[name.Name] = true |
| 86 |
} |
| 87 |
case *ast.TypeSpec: |
| 88 |
if s.Name != nil { |
| 89 |
taken[s.Name.Name] = true |
| 90 |
} |
| 91 |
case *ast.ImportSpec: |
| 92 |
if name := importBinding(s); name != "" { |
| 93 |
taken[name] = true |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// importBinding is the identifier an import introduces into its file's scope. |
| 102 |
// Without an explicit alias the binding is the imported package's name, which |
| 103 |
// cannot be known from the path alone -- but a package whose name matches a |
| 104 |
// candidate would have to be imported from a path containing it, so the last |
| 105 |
// path element is a sufficient approximation here. |
| 106 |
func importBinding(spec *ast.ImportSpec) string { |
| 107 |
if spec.Name != nil { |
| 108 |
if spec.Name.Name == "_" || spec.Name.Name == "." { |
| 109 |
return "" |
| 110 |
} |
| 111 |
return spec.Name.Name |
| 112 |
} |
| 113 |
path, err := strconv.Unquote(spec.Path.Value) |
| 114 |
if err != nil { |
| 115 |
return "" |
| 116 |
} |
| 117 |
if index := strings.LastIndexByte(path, '/'); index >= 0 { |
| 118 |
return path[index+1:] |
| 119 |
} |
| 120 |
return path |
| 121 |
} |