Debian Codegraph @

tools/antithesis-go-toolexec/symbols.go

raw
1 package main
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9
10 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
11 )
12
13 // Symbol tables are written under GOCACHE rather than straight into the image's
14 // symbols directory. This ensures that a cache hit still has its symbol table since
15 // the two are only ever discarded together.
16
17 const shardDirName = "antithesis-symbols"
18
19 // indexSuffix marks the files recording which symbol table belongs to which
20 // package, so the link step can report a gap instead of silently shipping one.
21 const indexSuffix = ".pkg"
22
23 // shardDirectory returns the per-build-cache directory holding symbol tables.
24 func shardDirectory() (string, error) {
25 cache := strings.TrimSpace(os.Getenv("GOCACHE"))
26 if cache == "" {
27 // GOCACHE is set for every tool invocation the go command makes, so an
28 // empty value means we are being run some other way.
29 return "", errors.New("GOCACHE is not set; this tool must be run via 'go build -toolexec'")
30 }
31 dir := filepath.Join(cache, shardDirName)
32 if err := os.MkdirAll(dir, 0o755); err != nil {
33 return "", fmt.Errorf("creating %s: %w", dir, err)
34 }
35 return dir, nil
36 }
37
38 // recordShard notes that importPath's edges are described by symbolName.
39 func recordShard(shardDir, importPath, symbolName string) error {
40 path := filepath.Join(shardDir, escapeImportPath(importPath)+indexSuffix)
41 if err := os.WriteFile(path, []byte(symbolName+"\n"), 0o644); err != nil {
42 return fmt.Errorf("recording symbol table for %s: %w", importPath, err)
43 }
44 return nil
45 }
46
47 // instrumentedImportPaths lists every package known to have been instrumented into
48 // this build cache. The link step uses it to tell an uninstrumented build (nothing
49 // to do) from an instrumented one (the SDK must be linkable).
50 func instrumentedImportPaths(cfg *Config) []string {
51 shardDir, err := shardDirectory()
52 if err != nil {
53 return nil
54 }
55 entries, err := os.ReadDir(shardDir)
56 if err != nil {
57 return nil
58 }
59 var paths []string
60 for _, entry := range entries {
61 if name := entry.Name(); strings.HasSuffix(name, indexSuffix) {
62 paths = append(paths, unescapeImportPath(strings.TrimSuffix(name, indexSuffix)))
63 }
64 }
65 return paths
66 }
67
68 // collectShards copies symbol tables into the directory the image build harvests.
69 //
70 // It runs at link time because that is the only step guaranteed to happen on a
71 // build that produces a binary. Copying everything present, rather than only what
72 // this binary references, means the destination can be a superset.
73 func collectShards(cfg *Config) error {
74 shardDir, err := shardDirectory()
75 if err != nil {
76 return err
77 }
78 entries, err := os.ReadDir(shardDir)
79 if err != nil {
80 if os.IsNotExist(err) {
81 return nil
82 }
83 return err
84 }
85
86 available := map[string]bool{}
87 var indexed []string
88 for _, entry := range entries {
89 switch name := entry.Name(); {
90 case strings.HasSuffix(name, common.SYMBOLS_FILE_SUFFIX):
91 available[name] = true
92 case strings.HasSuffix(name, indexSuffix):
93 indexed = append(indexed, name)
94 }
95 }
96 if len(available) == 0 && len(indexed) == 0 {
97 return nil
98 }
99
100 // A recorded package with no symbol table means someone removed files from
101 // under us. Say so rather than shipping a binary we cannot attribute.
102 var missing []string
103 for _, index := range indexed {
104 body, err := os.ReadFile(filepath.Join(shardDir, index))
105 if err != nil {
106 return err
107 }
108 symbolName := strings.TrimSpace(string(body))
109 if symbolName != "" && !available[symbolName] {
110 missing = append(missing, fmt.Sprintf("%s (%s)",
111 unescapeImportPath(strings.TrimSuffix(index, indexSuffix)), symbolName))
112 }
113 }
114 if len(missing) > 0 {
115 return fmt.Errorf("symbol tables missing from %s for %d instrumented %s: %s\n"+
116 "Remove the build cache (go clean -cache) and rebuild",
117 shardDir, len(missing), common.Pluralize(len(missing), "package"), strings.Join(missing, ", "))
118 }
119
120 if err := os.MkdirAll(cfg.SymbolsDir, 0o755); err != nil {
121 return fmt.Errorf("creating %s: %w", cfg.SymbolsDir, err)
122 }
123 copied := 0
124 for name := range available {
125 body, err := os.ReadFile(filepath.Join(shardDir, name))
126 if err != nil {
127 return err
128 }
129 if err := os.WriteFile(filepath.Join(cfg.SymbolsDir, name), body, 0o644); err != nil {
130 return fmt.Errorf("copying %s to %s: %w", name, cfg.SymbolsDir, err)
131 }
132 copied++
133 }
134 progressf("collected %d symbol %s into %s",
135 copied, common.Pluralize(copied, "table"), cfg.SymbolsDir)
136 return nil
137 }
138
139 // escapeImportPath makes an import path usable as a file name. Import paths may
140 // contain '/' and, for case-insensitive filesystems, upper-case letters that the
141 // module cache would encode; here a single reversible substitution is enough.
142 func escapeImportPath(importPath string) string {
143 return strings.ReplaceAll(importPath, "/", "%")
144 }
145
146 func unescapeImportPath(name string) string {
147 return strings.ReplaceAll(name, "%", "/")
148 }