| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
"go/importer" |
| 7 |
"go/parser" |
| 8 |
"go/token" |
| 9 |
"go/types" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 18 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/assertions" |
| 19 |
) |
| 20 |
|
| 21 |
const catalogFile = common.GENERATED_CATALOG_FILE |
| 22 |
|
| 23 |
// catalogPackage writes an assertion catalog for the package being compiled and |
| 24 |
// returns its path, or "" if the package has no assertions. |
| 25 |
// |
| 26 |
// sourcePaths are the package's original files, not the instrumented rewrites: |
| 27 |
// cataloguing records source positions, and there is no reason to read them |
| 28 |
// through a transformation. |
| 29 |
func catalogPackage(cfg *Config, pkg *Package, args []string, sourcePaths []string, work string) (string, error) { |
| 30 |
fset := token.NewFileSet() |
| 31 |
files := make([]*ast.File, 0, len(sourcePaths)) |
| 32 |
importsAssert := false |
| 33 |
for _, path := range sourcePaths { |
| 34 |
parsed, err := parser.ParseFile(fset, path, nil, parser.ParseComments) |
| 35 |
if err != nil { |
| 36 |
return "", fmt.Errorf("parsing %s: %w", path, err) |
| 37 |
} |
| 38 |
for _, spec := range parsed.Imports { |
| 39 |
if strings.Trim(spec.Path.Value, `"`) == common.AssertPackageName() { |
| 40 |
importsAssert = true |
| 41 |
} |
| 42 |
} |
| 43 |
files = append(files, parsed) |
| 44 |
} |
| 45 |
|
| 46 |
// A package that does not import the assert package cannot contain an |
| 47 |
// assertion, so there is nothing to find and no reason to pay for a |
| 48 |
// type-check. This is the common case by a wide margin. |
| 49 |
if !importsAssert { |
| 50 |
return "", nil |
| 51 |
} |
| 52 |
|
| 53 |
info, err := typeCheck(pkg.ImportPath, fset, files, args) |
| 54 |
if err != nil { |
| 55 |
return "", err |
| 56 |
} |
| 57 |
|
| 58 |
// Record positions relative to the module root, as the whole-tree |
| 59 |
// instrumentor does, so catalogs do not carry the build machine's paths. |
| 60 |
scanner := assertions.NewAssertionScanner(moduleRoot(filepath.Dir(sourcePaths[0])), "") |
| 61 |
result := scanner.ScanPackage(assertions.PackageSyntax{ |
| 62 |
PkgPath: pkg.ImportPath, |
| 63 |
Fset: fset, |
| 64 |
TypesInfo: info, |
| 65 |
Files: files, |
| 66 |
}) |
| 67 |
if !result.HasAssertions() { |
| 68 |
return "", nil |
| 69 |
} |
| 70 |
|
| 71 |
packageName, err := packageClause(sourcePaths[0]) |
| 72 |
if err != nil { |
| 73 |
return "", err |
| 74 |
} |
| 75 |
if err := assertions.GenerateAssertionsCatalog(work, catalogInfo(packageName, result)); err != nil { |
| 76 |
return "", err |
| 77 |
} |
| 78 |
path := filepath.Join(work, catalogFile) |
| 79 |
common.Logger.Printf(common.Info, "Cataloged %s: %d %s, %d guidance %s", |
| 80 |
pkg.ImportPath, |
| 81 |
len(result.Expects), common.Pluralize(len(result.Expects), "assertion"), |
| 82 |
len(result.Guidance), common.Pluralize(len(result.Guidance), "record")) |
| 83 |
return path, nil |
| 84 |
} |
| 85 |
|
| 86 |
func catalogInfo(packageName string, result *assertions.PackageResult) *assertions.GenInfo { |
| 87 |
numeric := assertions.NumericGuidance(result.Guidance) |
| 88 |
boolean := assertions.BooleanGuidance(result.Guidance) |
| 89 |
return &assertions.GenInfo{ |
| 90 |
PackageName: packageName, |
| 91 |
ExpectedVals: result.Expects, |
| 92 |
NumericGuidanceVals: numeric, |
| 93 |
BooleanGuidanceVals: boolean, |
| 94 |
AssertPackageName: common.AssertPackageName(), |
| 95 |
VersionText: "antithesis-go-toolexec", |
| 96 |
CreateDate: time.Now().Format("Mon Jan 2 15:04:05 MST 2006"), |
| 97 |
HasAssertions: len(result.Expects) > 0, |
| 98 |
HasNumericGuidance: len(numeric) > 0, |
| 99 |
HasBooleanGuidance: len(boolean) > 0, |
| 100 |
ConstMap: assertions.ConstMap(result.Expects), |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// resolves the types for this package using the archives named in the importcfg |
| 105 |
// the compiler was handed |
| 106 |
func typeCheck(importPath string, fset *token.FileSet, files []*ast.File, args []string) (*types.Info, error) { |
| 107 |
importCfg := flagValue(args, "-importcfg") |
| 108 |
if importCfg == "" { |
| 109 |
return nil, fmt.Errorf("no -importcfg in the compile invocation for %s", importPath) |
| 110 |
} |
| 111 |
cfg, err := readImportCfg(importCfg) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
|
| 116 |
config := types.Config{ |
| 117 |
Importer: importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) { |
| 118 |
archive, ok := cfg.archives[path] |
| 119 |
if !ok { |
| 120 |
return nil, fmt.Errorf("no archive for %s in %s", path, importCfg) |
| 121 |
} |
| 122 |
return os.Open(archive) |
| 123 |
}), |
| 124 |
GoVersion: languageVersion(args), |
| 125 |
// Sizes are only consulted for constant overflow |
| 126 |
Sizes: types.SizesFor("gc", buildArch()), |
| 127 |
} |
| 128 |
info := &types.Info{ |
| 129 |
Uses: map[*ast.Ident]types.Object{}, |
| 130 |
Defs: map[*ast.Ident]types.Object{}, |
| 131 |
} |
| 132 |
if _, err := config.Check(importPath, fset, files, info); err != nil { |
| 133 |
return nil, fmt.Errorf("type-checking %s for cataloguing: %w", importPath, err) |
| 134 |
} |
| 135 |
return info, nil |
| 136 |
} |
| 137 |
|
| 138 |
// languageVersion recovers the -lang value the compiler was given, in the form |
| 139 |
// go/types expects. |
| 140 |
func languageVersion(args []string) string { |
| 141 |
lang := flagValue(args, "-lang") |
| 142 |
if lang == "" { |
| 143 |
return "" |
| 144 |
} |
| 145 |
if !strings.HasPrefix(lang, "go") { |
| 146 |
lang = "go" + lang |
| 147 |
} |
| 148 |
return lang |
| 149 |
} |
| 150 |
|
| 151 |
// flagValue reads a flag written either as "-name value" or "-name=value". |
| 152 |
func flagValue(args []string, name string) string { |
| 153 |
for i, arg := range args { |
| 154 |
if arg == name && i+1 < len(args) { |
| 155 |
return args[i+1] |
| 156 |
} |
| 157 |
if value, ok := strings.CutPrefix(arg, name+"="); ok { |
| 158 |
return value |
| 159 |
} |
| 160 |
} |
| 161 |
return "" |
| 162 |
} |
| 163 |
|
| 164 |
// buildArch reports the GOARCH this build targets |
| 165 |
func buildArch() string { |
| 166 |
if arch := strings.TrimSpace(os.Getenv("GOARCH")); arch != "" { |
| 167 |
return arch |
| 168 |
} |
| 169 |
return runtime.GOARCH |
| 170 |
} |