/
engine.go
211 lines (182 loc) · 4.23 KB
/
engine.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
package rvsym
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/zyedidia/rvsym/pkg/smt"
)
type Engine struct {
active *Machine
checkpoints []*Checkpoint
smt *smt.Solver
tcs []TestCase
Stats Stats
}
type Stats struct {
Exits map[ExitStatus]int
Steps int
Forks int
}
func NewEngine(segs []Segment, entry uint32, mode EmuMode) *Engine {
s := smt.NewSolver()
mem := NewMemory()
for _, seg := range segs {
words := toWords(seg.data)
for i, word := range words {
addr := int32(seg.addr + uint32(i*4))
mem.WriteWord(smt.Int32{C: addr}, smt.Int32{C: int32(word)}, s)
}
}
machine := NewMachine(int32(entry), mem)
if len(segs) > 0 {
machine.icache = cache{
base: segs[0].addr,
data: segs[0].data,
}
}
switch mode {
case EmuLinux:
// linux mode: use an initial stack pointer and point it at argc (0).
sp := int32(0x7ffff00)
machine.regs[Rsp] = smt.Int32{C: sp}
mem.WriteWord(smt.Int32{C: sp}, smt.Int32{C: 0}, s)
case EmuUnderConstrained:
for i := range machine.regs {
if i == Rzero || i == Rsp {
continue
}
sym := s.AnyInt32()
// machine.markSym(sym, RegNames[i])
machine.regs[i] = sym
}
sp := int32(0x7ffff00)
machine.regs[Rsp] = smt.Int32{C: sp}
}
machine.mode = mode
return &Engine{
active: machine,
smt: s,
Stats: Stats{
Exits: make(map[ExitStatus]int),
},
}
}
func (e *Engine) Step() bool {
e.Stats.Steps++
m := e.active
isz := m.Exec(e.smt)
exited := e.handleExit(m)
if exited {
return e.active != nil
}
br := m.Status.Br
switch {
case m.Status.HasBr && br.cond.Concrete() && br.cond.C:
m.pc = br.pc
case m.Status.HasBr && br.cond.Concrete():
m.pc += isz
case m.Status.HasBr:
var cond, alt smt.Bool
var condpc, altpc int32
if randbool() {
cond, alt = br.cond, br.cond.Not(e.smt)
condpc, altpc = br.pc, m.pc+isz
} else {
alt, cond = br.cond, br.cond.Not(e.smt)
altpc, condpc = br.pc, m.pc+isz
}
e.smt.Push()
e.smt.Assert(alt)
res := e.smt.Check(false)
if res == smt.Sat {
// alt is sat, so we take that branch and checkpoint cond to return
// to later
m.pc = altpc
checkpoint := m.Checkpoint(cond)
checkpoint.pc = condpc
e.checkpoints = append(e.checkpoints, checkpoint)
} else {
// alt was unsat so we go directly to cond
e.smt.Pop()
m.pc = condpc
m.AddCond(cond, true, e.smt)
}
default:
m.pc += isz
}
if !e.handleExit(m) {
m.clearbr()
}
return e.active != nil
}
func (e *Engine) handleExit(m *Machine) bool {
if !e.hasExit(m) {
return false
}
if len(e.checkpoints) > 0 {
e.smt.Pop()
e.active = Restore(e.checkpoints[len(e.checkpoints)-1], e.smt)
e.checkpoints = e.checkpoints[:len(e.checkpoints)-1]
} else {
e.active = nil
}
e.Stats.Exits[m.Status.Exit]++
return true
}
func (e *Engine) hasExit(m *Machine) bool {
if m.Status.Exit != ExitNone {
switch m.Status.Exit {
case ExitNormal, ExitFail:
tc, sat := m.TestCase(e.smt)
if sat {
e.tcs = append(e.tcs, tc)
} else {
m.Status.Exit = ExitUnsat
}
case ExitQuiet:
}
return true
}
return false
}
func (e *Engine) TestCases() []TestCase {
return e.tcs
}
func (e *Engine) NumTestCases() int {
return len(e.tcs)
}
func (e *Engine) Summary() string {
buf := &bytes.Buffer{}
paths := 0
for _, v := range e.Stats.Exits {
paths += v
}
fmt.Fprintln(buf, "--- Summary ---")
fmt.Fprintf(buf, "Instructions executed: %d\n", e.Stats.Steps)
fmt.Fprintf(buf, "Total paths: %d\n", paths)
fmt.Fprintf(buf, "Quiet exits: %d\n", e.Stats.Exits[ExitQuiet])
fmt.Fprintf(buf, "Unsat exits: %d\n", e.Stats.Exits[ExitUnsat])
fmt.Fprintf(buf, "Normal exits: %d\n", e.Stats.Exits[ExitNormal])
fmt.Fprintf(buf, "Failures: %d\n", e.Stats.Exits[ExitFail])
fmt.Fprintln(buf, "---")
return buf.String()
}
func toWords(data []byte) []uint32 {
words := make([]uint32, 0, len(data)/4)
for len(data) > 0 {
if len(data) >= 4 {
words = append(words, binary.LittleEndian.Uint32(data))
data = data[4:]
} else {
if len(data) == 3 {
words = append(words, uint32(data[0])|uint32(data[1])<<8|uint32(data[2])<<16)
} else if len(data) == 2 {
words = append(words, uint32(data[0])|uint32(data[1])<<8)
} else {
words = append(words, uint32(data[0]))
}
data = data[len(data):]
}
}
return words
}