-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcongo.go
297 lines (260 loc) · 7.6 KB
/
congo.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
package congo
import (
"bytes"
"go/ast"
"go/format"
"go/token"
"go/types"
"io"
"github.com/ajalab/congo/interp"
"github.com/ajalab/congo/log"
"github.com/ajalab/congo/solver"
"golang.org/x/tools/go/ssa"
"github.com/pkg/errors"
)
// Program is a type that contains information of the target program.
type Program struct {
runnerFile *ast.File
runnerTypesInfo *types.Info
runnerPackage *ssa.Package
targetPackage *ssa.Package
congoSymbolPackage *ssa.Package
}
// ExecuteOption is a type that contains options to perform concolic execution on a target function.
type ExecuteOption struct {
MaxExec uint `key:"maxexec"`
MinCoverage float64 `key:"cover"`
}
var defaultExecuteOption = &ExecuteOption{
MaxExec: 10,
MinCoverage: 1.0,
}
// Fill fills the fields in ExecuteOption with those in src.
func (eo *ExecuteOption) Fill(src *ExecuteOption, overwrite bool) *ExecuteOption {
if eo == nil || src == nil {
return eo
}
if overwrite {
if src.MaxExec != 0 {
eo.MaxExec = src.MaxExec
}
if src.MinCoverage != 0 {
eo.MinCoverage = src.MinCoverage
}
} else {
if eo.MaxExec == 0 {
eo.MaxExec = src.MaxExec
}
if eo.MinCoverage == 0.0 {
eo.MinCoverage = src.MinCoverage
}
}
return eo
}
// Target is a type that contains the single target of concolic testing (function and set of symbols).
type Target struct {
name string
f *ssa.Function
runnerName string
symbols []ssa.Value
*ExecuteOption
}
// Congo is a type that contains the program and dict of targets
// (keys are names of target function).
type Congo struct {
program *Program
targets map[string]*Target
}
// Execute executes concolic execution.
// The iteration time is bounded by maxExec and stopped when minCoverage is accomplished.
func (c *Congo) Execute(funcName string) (*ExecuteResult, error) {
target, ok := c.targets[funcName]
if !ok {
return nil, errors.Errorf("function %s does not exist", funcName)
}
n := len(target.symbols)
solutions := make([]solver.Solution, n)
covered := make(map[*ssa.BasicBlock]struct{})
coverage := 0.0
var runResults []*RunResult
for i, symbol := range target.symbols {
solutions[i] = solver.NewIndefinite(symbol.Type())
}
for i := uint(0); i < target.MaxExec; i++ {
values := make([]interface{}, n)
// Assign a zero value if the concrete value is nil.
for j, sol := range solutions {
values[j] = sol.Concretize(zero)
}
log.Info.Printf("[%d] run: %v", i, values)
// Interpret the program with the current symbol values.
result, err := c.Run(funcName, values)
if err != nil {
log.Info.Printf("[%d] panic", i)
}
// Update the covered blocks.
nNewCoveredBlks := 0
for _, instr := range result.Instrs {
b := instr.Block()
if b.Parent() == target.f {
if _, ok := covered[b]; !ok {
covered[b] = struct{}{}
nNewCoveredBlks++
}
}
}
// Record the concrete values if new blocks are covered.
if nNewCoveredBlks > 0 {
runResults = append(runResults, &RunResult{
symbolValues: values,
returnValues: result.Return,
panicked: result.ExitCode != 0,
})
}
// Compute the coverage and exit if it exceeds the minCoverage.
// Also exit when the execution count minus one is equal to maxExec to avoid unnecessary constraint solver call.
coverage = float64(len(covered)) / float64(len(target.f.Blocks))
log.Info.Printf("[%d] coverage: %.3f", i, coverage)
if coverage >= target.MinCoverage {
log.Info.Printf("[%d] stop because the coverage criteria has been satisfied.", i)
break
}
if i == target.MaxExec-1 {
log.Info.Printf("[%d] stop because the runnign count has reached the limit", i)
}
z3Solver, err := solver.CreateZ3Solver(target.symbols, result.Instrs, result.ExitCode == 0)
if err != nil {
return nil, errors.Wrap(err, "failed to create a solver")
}
branches := z3Solver.Branches()
queue, queueAfter := make([]int, 0), make([]int, 0)
for j := len(branches) - 1; j >= 0; j-- {
branch := branches[j]
switch branch := branch.(type) {
case *solver.BranchIf:
b := branch.Other()
if _, ok := covered[b]; !ok {
queue = append(queue, j)
} else {
queueAfter = append(queueAfter, j)
}
case *solver.BranchDeref:
queue = append(queue, j)
}
}
queue = append(queue, queueAfter...)
for _, j := range queue {
log.Info.Printf("[%d] negate %d", i, j)
solutions, err = z3Solver.Solve(j)
if err == nil {
log.Info.Printf("[%d] sat %d", i, j)
break
} else if _, ok := err.(solver.UnsatError); ok {
log.Info.Printf("[%d] unsat %d", i, j)
} else {
return nil, errors.Wrap(err, "failed to solve assertions")
}
}
z3Solver.Close()
}
symbolTypes := make([]types.Type, n)
for i, symbol := range target.symbols {
symbolTypes[i] = symbol.Type()
}
return &ExecuteResult{
Coverage: coverage,
SymbolTypes: symbolTypes,
RunResults: runResults,
runnerFile: c.program.runnerFile,
runnerTypesInfo: c.program.runnerTypesInfo,
runnerPackage: c.program.runnerPackage.Pkg,
runnerFuncName: target.runnerName,
targetPackage: c.program.targetPackage.Pkg,
congoSymbolPackage: c.program.congoSymbolPackage.Pkg,
targetFuncSig: target.f.Signature,
targetFuncName: funcName,
}, nil
}
// Run runs the program by the interpreter provided by interp module.
func (c *Congo) Run(funcName string, values []interface{}) (*interp.CongoInterpResult, error) {
target, ok := c.targets[funcName]
if !ok {
return nil, errors.Errorf("function %s does not exist", funcName)
}
n := len(values)
symbolValues := make([]interp.SymbolicValue, n)
for i, symbol := range target.symbols {
symbolValues[i] = interp.SymbolicValue{
Value: values[i],
Type: symbol.Type(),
}
}
interp.CapturedOutput = new(bytes.Buffer)
mode := interp.DisableRecover // interp.EnableTracing
return interp.Interpret(
c.program.runnerPackage,
target.f,
target.runnerName,
symbolValues,
mode,
&types.StdSizes{WordSize: 8, MaxAlign: 8},
"",
[]string{},
)
}
// DumpRunnerAST dumps the runner AST file into dest.
func (c *Congo) DumpRunnerAST(dest io.Writer) error {
return format.Node(dest, token.NewFileSet(), c.program.runnerFile)
}
// DumpSSA dumps the SSA-format code into dest.
func (c *Congo) DumpSSA(dest io.Writer) error {
var err error
_, err = c.program.runnerPackage.Func("main").WriteTo(dest)
if err != nil {
return err
}
for _, target := range c.targets {
_, err = target.f.WriteTo(dest)
if err != nil {
break
}
}
return err
}
// Funcs returns the list of function names that Congo loaded as execution target.
func (c *Congo) Funcs() []string {
names := make([]string, len(c.targets))
i := 0
for name := range c.targets {
names[i] = name
i++
}
return names
}
// Target returns the target which has the given name.
func (c *Congo) Target(name string) *Target {
return c.targets[name]
}
// ExecuteResult is a type that contains the result of Execute.
// TODO(ajalab):
// ReturnValues has type []interp.value so it is meaningless to make this property public.
// We use reflection to extract values from interp.value for now.
type ExecuteResult struct {
Coverage float64 // achieved coverage.
SymbolTypes []types.Type
RunResults []*RunResult
runnerFile *ast.File
runnerTypesInfo *types.Info
runnerPackage *types.Package
runnerFuncName string
targetPackage *types.Package
congoSymbolPackage *types.Package
targetFuncSig *types.Signature
targetFuncName string
}
// RunResult is a type that contains the result of Run.
type RunResult struct {
symbolValues []interface{}
returnValues interface{}
panicked bool
}