| 1 |
package coverage |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
|
| 8 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 9 |
commonconfig "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/config" |
| 10 |
covconfig "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/config" |
| 11 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/instrumentor" |
| 12 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/symboltable" |
| 13 |
) |
| 14 |
|
| 15 |
type goInstrumentor interface { |
| 16 |
// Rewrite the file at path, returning the instrumented source (or "" if it should be skipped) |
| 17 |
Instrument(path string) (string, error) |
| 18 |
// The running total of coverage edges recorded so far. |
| 19 |
Edges() int |
| 20 |
} |
| 21 |
|
| 22 |
type astRewritingInstrumentor struct { |
| 23 |
*instrumentor.Instrumentor |
| 24 |
} |
| 25 |
|
| 26 |
func (l astRewritingInstrumentor) Edges() int { return l.CurrentEdge } |
| 27 |
|
| 28 |
// Capitalized struct items are accessed outside this file |
| 29 |
type CoverageInstrumentor struct { |
| 30 |
GoInstrumentor goInstrumentor |
| 31 |
SymTable *symboltable.SymbolTable |
| 32 |
UsingSymbols string |
| 33 |
NotifierPackage string |
| 34 |
PreviousEdge int |
| 35 |
FilesInstrumented int |
| 36 |
FilesSkipped int |
| 37 |
} |
| 38 |
|
| 39 |
type NotifierInfo struct { |
| 40 |
InstrumentationPackageName string |
| 41 |
SymbolTableName string |
| 42 |
NotifierPackage string |
| 43 |
EdgeCount int |
| 44 |
} |
| 45 |
|
| 46 |
func (cI *CoverageInstrumentor) WriteNotifierSource(notifierDir string, edge_count int) { |
| 47 |
notifierInfo := NotifierInfo{ |
| 48 |
InstrumentationPackageName: common.InstrumentationPackageName(), |
| 49 |
SymbolTableName: cI.UsingSymbols, |
| 50 |
EdgeCount: edge_count, |
| 51 |
NotifierPackage: cI.NotifierPackage, |
| 52 |
} |
| 53 |
|
| 54 |
GenerateNotifierSource(notifierDir, ¬ifierInfo) |
| 55 |
} |
| 56 |
|
| 57 |
func (cI *CoverageInstrumentor) InstrumentFile(file_name string) string { |
| 58 |
var err error |
| 59 |
instrumented := "" |
| 60 |
common.Logger.Printf(common.Normal, "Instrumenting %s", file_name) |
| 61 |
cI.PreviousEdge = cI.GoInstrumentor.Edges() |
| 62 |
if instrumented, err = cI.GoInstrumentor.Instrument(file_name); err != nil { |
| 63 |
common.Logger.Printf(common.Normal, "Error: File %s produced error %s; simply copying source", file_name, err) |
| 64 |
return "" |
| 65 |
} |
| 66 |
|
| 67 |
return instrumented |
| 68 |
} |
| 69 |
|
| 70 |
func (cI *CoverageInstrumentor) WrapUp() (edge_count int) { |
| 71 |
if err := cI.SymTable.Close(); err != nil { |
| 72 |
common.Logger.Printf(common.Normal, "Error Could not close symbol table %s: %s", cI.SymTable.Path, err) |
| 73 |
} |
| 74 |
common.Logger.Printf(common.Normal, "Symbol table: %s", cI.SymTable.Path) |
| 75 |
edge_count = cI.GoInstrumentor.Edges() |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
func (cI *CoverageInstrumentor) SummarizeWork(numFiles int) { |
| 80 |
numFilesSkipped := (numFiles - cI.FilesInstrumented) + cI.FilesSkipped |
| 81 |
numEdges := cI.GoInstrumentor.Edges() |
| 82 |
common.Logger.Printf(common.Normal, "%d '.go' %s instrumented, %d %s skipped, %d %s identified", |
| 83 |
numFiles, common.Pluralize(numFiles, "file"), |
| 84 |
numFilesSkipped, common.Pluralize(numFilesSkipped, "file"), |
| 85 |
numEdges, common.Pluralize(numEdges, "edge")) |
| 86 |
} |
| 87 |
|
| 88 |
func NewCoverageInstrumentor(cc *commonconfig.CommonConfig, cov *covconfig.CoverageConfig) *CoverageInstrumentor { |
| 89 |
notifierModuleName := common.FullNotifierName(cc.FilesHash) |
| 90 |
|
| 91 |
common.Logger.Printf(common.Normal, "Writing instrumented source to %s", cc.CustomerDirectory) |
| 92 |
|
| 93 |
symbolTableFileBasename := fmt.Sprintf("%s%s-%s", cov.SymtablePrefix, common.SYMBOLS_FILE_HASH_PREFIX, cc.FilesHash) |
| 94 |
symbolTableFilename := symbolTableFileBasename + common.SYMBOLS_FILE_SUFFIX |
| 95 |
symbolsPath := filepath.Join(cov.SymbolsDirectory, symbolTableFilename) |
| 96 |
symTable, err := symboltable.CreateSymbolTableFile(symbolsPath, symbolTableFileBasename) |
| 97 |
if err != nil { |
| 98 |
common.Logger.Fatalf("Could not write symbol table header: %s", err.Error()) |
| 99 |
} |
| 100 |
|
| 101 |
// Default to the proven AST-rewriting instrumentor; opt into the |
| 102 |
// source-editing (edit-buffer) implementation with -source_editing. |
| 103 |
var goInst goInstrumentor |
| 104 |
if cov.UseSourceEditing { |
| 105 |
common.Logger.Printf(common.Normal, "Using the source-editing (edit-buffer) instrumentor") |
| 106 |
goInst = instrumentor.CreateSourceEditingInstrumentor(cc.InputDirectory, notifierModuleName, symTable) |
| 107 |
} else { |
| 108 |
goInst = astRewritingInstrumentor{instrumentor.CreateInstrumentor(cc.InputDirectory, notifierModuleName, symTable)} |
| 109 |
} |
| 110 |
|
| 111 |
cI := CoverageInstrumentor{ |
| 112 |
GoInstrumentor: goInst, |
| 113 |
SymTable: symTable, |
| 114 |
UsingSymbols: symbolTableFilename, |
| 115 |
PreviousEdge: 0, |
| 116 |
FilesInstrumented: 0, |
| 117 |
FilesSkipped: cc.FilesSkipped, |
| 118 |
NotifierPackage: common.NotifierPackage(cc.FilesHash), |
| 119 |
} |
| 120 |
return &cI |
| 121 |
} |
| 122 |
|
| 123 |
func (cI *CoverageInstrumentor) WriteInstrumentedOutput(cc *commonconfig.CommonConfig, fileName string, instrumentedSource string) { |
| 124 |
// skip over the base inputDirectory from the inputfilename, |
| 125 |
// and create the output directories needed |
| 126 |
skipLength := len(cc.InputDirectory) |
| 127 |
outputPath := filepath.Join(cc.CustomerDirectory, fileName[skipLength:]) |
| 128 |
outputSubdirectory := filepath.Dir(outputPath) |
| 129 |
os.MkdirAll(outputSubdirectory, 0755) |
| 130 |
|
| 131 |
common.Logger.Printf(common.Info, "Writing instrumented file %s with edges %d–%d", outputPath, cI.PreviousEdge, cI.GoInstrumentor.Edges()) |
| 132 |
|
| 133 |
if err := common.WriteTextFile(instrumentedSource, outputPath); err == nil { |
| 134 |
cI.FilesInstrumented++ |
| 135 |
} |
| 136 |
} |