-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.go
377 lines (341 loc) · 10.6 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package congo
import (
"go/ast"
"go/constant"
"go/token"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"unicode"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"github.com/pkg/errors"
)
const congoSymbolPackagePath = "github.com/ajalab/congo/symbol"
func loadTargetPackage(targetPackagePath string) (*packages.Package, error) {
conf := &packages.Config{
Mode: packages.LoadSyntax,
}
query := targetPackagePath
if isGoFilePath(query) {
query = "file=" + targetPackagePath
}
pkgs, err := packages.Load(conf, query)
if err != nil {
return nil, errors.Wrapf(err, "failed to load the target package %s", targetPackagePath)
}
if len(pkgs) == 0 {
return nil, errors.Errorf("no packages could be loaded for path %s", targetPackagePath)
}
return pkgs[0], nil
}
func isGoFilePath(path string) bool {
return strings.HasSuffix(path, ".go")
}
// Config specifies the (optional) parameters for concolic execution.
// Options are ignored when a field has the zero value.
type Config struct {
// FuncNames is a list of functions that we generate tests for.
FuncNames []string
// Runner is the path to the Go file that calls the target function.
// Automatically generated if empty string is specified.
Runner string
ExecuteOption
}
// parseAnnotationDirective parses directives from s, which is in the form of "congo:<key>[ <value>]".
// This function requires that the leading and trailing white space
// in s is trimmed beforehand.
func parseAnnotationDirective(s, key string) (string, bool) {
prefix := "congo:"
if !strings.HasPrefix(s, prefix) {
return "", false
}
prefixTrimmed := strings.TrimPrefix(s, prefix)
var read []rune
for _, r := range prefixTrimmed {
if unicode.IsSpace(r) {
break
}
read = append(read, r)
}
readKey := string(read)
if readKey != key {
return "", false
}
value := strings.TrimSpace(strings.TrimPrefix(prefixTrimmed, readKey))
return value, true
}
func parseAnnotation(text string, eo *ExecuteOption) (bool, error) {
const annoTagKey = "key"
if text[:2] != "//" {
return false, nil
}
text = strings.TrimSpace(text[2:])
eoTy := reflect.TypeOf(*eo)
for i := 0; i < eoTy.NumField(); i++ {
f := eoTy.Field(i)
key := f.Tag.Get(annoTagKey)
if value, ok := parseAnnotationDirective(text, key); ok {
switch f.Type.Kind() {
case reflect.Uint:
iv, err := strconv.Atoi(value)
if err != nil {
return false, err
}
reflect.ValueOf(eo).Elem().Field(i).SetUint(uint64(iv))
case reflect.Float64:
fv, err := strconv.ParseFloat(value, 64)
if err != nil {
return false, err
}
reflect.ValueOf(eo).Elem().Field(i).SetFloat(fv)
default:
return false, errors.Errorf("unsupported option tyupe: %s", f.Type)
}
return true, nil
}
}
return false, nil
}
func getExecuteOption(funcDecl *ast.FuncDecl, cgroups []*ast.CommentGroup) (*ExecuteOption, error) {
eo := &ExecuteOption{}
isTarget := false
for _, cgroup := range cgroups {
for _, comment := range cgroup.List {
text := comment.Text
parsed, err := parseAnnotation(text, eo)
if err != nil {
return eo, errors.Wrapf(err, `failed to parse annotation "%s" for %s`, text, funcDecl.Name.Name)
}
isTarget = isTarget || parsed
}
}
if isTarget {
return eo, nil
}
return nil, nil
}
func loadTargetFuncs(
targetPackagePath string,
targetPackage *packages.Package,
funcNames []string,
argEO *ExecuteOption,
) (map[string]*Target, error) {
// cases:
// 1. targetPackagePath is a file path to a Go file and len(funcNames) is 0.
// 2. targetPackagePath is a file path to a Go file and len(funcNames) is greater than 0 .
// 3. targetPackagePath is an import path to the target package and len(funcNames) is 0.
// 4. targetPackagePath is an import path to the target package and len(funcNames) is greater than 0.
var fs []*ast.File
if isGoFilePath(targetPackagePath) {
targetPackageAbsPath, err := filepath.Abs(targetPackagePath)
if err != nil {
return nil, err
}
for i, fpath := range targetPackage.CompiledGoFiles {
if fpath == targetPackageAbsPath {
fs = append(fs, targetPackage.Syntax[i])
break
}
}
} else {
fs = targetPackage.Syntax
}
if len(fs) == 0 {
return nil, errors.Errorf("no files could be loaded for package %s", targetPackagePath)
}
cmaps := make([]ast.CommentMap, len(fs))
for i, f := range fs {
cmaps[i] = ast.NewCommentMap(targetPackage.Fset, f, f.Comments)
}
targets := make(map[string]*Target)
if len(funcNames) > 0 {
// case 2 or 4
FUNC:
for _, name := range funcNames {
for j, f := range fs {
obj := f.Scope.Lookup(name)
if obj == nil {
continue
}
if funcDecl, ok := obj.Decl.(*ast.FuncDecl); ok {
eo, err := getExecuteOption(funcDecl, cmaps[j][funcDecl])
if err != nil {
return nil, errors.Wrapf(err, "failed to parse annotations for function %s", funcDecl.Name)
}
if eo == nil {
eo = &ExecuteOption{}
}
eo.Fill(argEO, true).Fill(defaultExecuteOption, false)
target := &Target{name: name, ExecuteOption: eo}
targets[name] = target
continue FUNC
}
}
return nil, errors.Errorf("function %s does not exist in %s", name, targetPackagePath)
}
return targets, nil
}
// case 1 or 3
for i, f := range fs {
for _, decl := range f.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
name := funcDecl.Name.String()
eo, err := getExecuteOption(funcDecl, cmaps[i][funcDecl])
if err != nil {
return nil, errors.Wrapf(err, "failed to parse annotations for function %s", funcDecl.Name)
}
if eo == nil {
continue
}
eo.Fill(argEO, true).Fill(defaultExecuteOption, false)
target := &Target{name: name, ExecuteOption: eo}
targets[name] = target
}
}
}
return targets, nil
}
// Load loads the target program.
// targetPackagePath is either
// - a file path (e.g., foo/bar.go) to the target package
// - an import path (e.g, github.com/ajalab/congo).
func Load(config *Config, targetPackagePath string) (*Congo, error) {
if config == nil {
config = &Config{}
}
// (Pre)load the target package to
// - get a list of target functions from annotations
// - (optional) generate a test runner
targetPackage, err := loadTargetPackage(targetPackagePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to load package %s", targetPackagePath)
}
targets, err := loadTargetFuncs(targetPackagePath, targetPackage, config.FuncNames, &config.ExecuteOption)
if err != nil {
return nil, errors.Wrapf(err, "failed to load target functions in %s", targetPackage.PkgPath)
}
if len(targets) == 0 {
return nil, errors.Errorf("no target functions could be found in %s", targetPackage.PkgPath)
}
// Generate a runner file if config.Runner is nil.
runnerPackageFPath := config.Runner
if runnerPackageFPath == "" {
runnerPackageFPath, err = generateRunner(targetPackage, targets)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate a runner")
}
defer os.Remove(runnerPackageFPath)
} else {
return nil, errors.New("user-specified runner is not supported yet")
}
// IPath represents an import path.
targetPackageIPath := targetPackage.PkgPath
return load(targets, targetPackageIPath, runnerPackageFPath)
}
func load(targets map[string]*Target, targetPackageIPath, runnerPackagePath string) (*Congo, error) {
pConfig := &packages.Config{
Mode: packages.LoadAllSyntax,
}
pkgs, err := packages.Load(pConfig, runnerPackagePath)
if err != nil {
return nil, errors.Wrap(err, "failed to load packages")
}
if len(pkgs) == 0 {
return nil, errors.New("no packages could be loaded")
}
runnerPackageIdx := -1
for i, pkg := range pkgs {
if len(pkg.Errors) > 0 {
return nil, errors.Errorf("failed to load package %s: %v", pkg.PkgPath, pkg.Errors)
}
// It is possible that pkg.IllTyped becomes true but pkg.Errors has no error records.
if pkg.IllTyped {
return nil, errors.Errorf("package %s contains type error", pkg.PkgPath)
}
if pkg.Name != "runtime" {
runnerPackageIdx = i
}
}
if runnerPackageIdx < 0 {
return nil, errors.New("failed to load the runner package")
}
runnerPackage := pkgs[runnerPackageIdx]
targetPackage := runnerPackage.Imports[targetPackageIPath]
congoSymbolPackage := runnerPackage.Imports[congoSymbolPackagePath]
ssaProg, ssaPkgs := ssautil.AllPackages(pkgs, ssa.BuilderMode(0))
for i, ssaPkg := range ssaPkgs {
if ssaPkg == nil {
return nil, errors.Errorf("failed to compile package %s into SSA form", pkgs[i])
}
}
ssaProg.Build()
runnerPackageSSA := ssaPkgs[runnerPackageIdx]
targetPackageSSA := ssaProg.Package(targetPackage.Types)
congoSymbolPackageSSA := ssaProg.Package(congoSymbolPackage.Types)
symbolType := congoSymbolPackageSSA.Members["SymbolType"].Type()
for _, target := range targets {
// Find references to congo.Symbol
mainFunc := runnerPackageSSA.Func(target.runnerName)
symbolSubstTable := make(map[uint64]struct {
i int
v ssa.Value
})
for _, block := range mainFunc.Blocks {
for _, instr := range block.Instrs {
// expression symbol.Symbols[i].(XXX) is considered as a symbol.
assertInstr, ok := instr.(*ssa.TypeAssert)
if !ok || assertInstr.X.Type() != symbolType {
continue
}
ty := assertInstr.AssertedType
unopInstr, ok := assertInstr.X.(*ssa.UnOp)
if !ok || unopInstr.Op != token.MUL {
return nil, errors.Errorf("Illegal use of Symbol")
}
indexAddrInstr, ok := unopInstr.X.(*ssa.IndexAddr)
if !ok {
return nil, errors.Errorf("Symbol must be used with the index operator")
}
index, ok := indexAddrInstr.Index.(*ssa.Const)
if !ok {
return nil, errors.Errorf("Symbol must be indexed with a constant value")
}
i := index.Uint64()
if subst, ok := symbolSubstTable[i]; ok {
if subst.v.Type() != ty {
return nil, errors.Errorf("Symbol[%d] is used as multiple types", i)
}
indexAddrInstr.Index = ssa.NewConst(constant.MakeUint64(uint64(subst.i)), index.Type())
} else {
newi := len(symbolSubstTable)
indexAddrInstr.Index = ssa.NewConst(constant.MakeUint64(uint64(newi)), index.Type())
symbolSubstTable[i] = struct {
i int
v ssa.Value
}{newi, assertInstr}
}
}
}
symbols := make([]ssa.Value, len(symbolSubstTable))
for _, subst := range symbolSubstTable {
symbols[subst.i] = subst.v
}
target.f = targetPackageSSA.Func(target.name)
target.symbols = symbols
}
program := &Program{
runnerFile: runnerPackage.Syntax[0],
runnerTypesInfo: runnerPackage.TypesInfo,
runnerPackage: runnerPackageSSA,
targetPackage: targetPackageSSA,
congoSymbolPackage: congoSymbolPackageSSA,
}
return &Congo{
program: program,
targets: targets,
}, nil
}