-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
342 lines (296 loc) · 7.44 KB
/
main.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
package main
import (
"fault/ast"
"fault/execute"
"fault/listener"
"fault/llvm"
"fault/preprocess"
"fault/reachability"
"fault/smt"
resultlog "fault/smt/log"
smtvar "fault/smt/variables"
"fault/swaps"
"fault/types"
"fault/util"
"fault/visualize"
"flag"
"fmt"
"log"
"os"
gopath "path"
"strings"
_ "github.com/olekukonko/tablewriter"
)
func parse(data string, path string, file string, filetype string, reach bool, visu bool) (*ast.Spec, *listener.FaultListener, *types.Checker, string, map[string]string) {
//Confirm that the filetype and file declaration match
if !validate_filetype(data, filetype) {
log.Fatalf("malformatted file: declaration does not match filetype.")
}
flags := make(map[string]bool)
flags["specType"] = (filetype == "fspec")
flags["testing"] = false
flags["skipRun"] = false
lstnr := listener.Execute(data, path, flags)
pre := preprocess.Execute(lstnr)
ty := types.Execute(pre.Processed, pre)
sw := swaps.NewPrecompiler(ty)
tree := sw.Swap(ty.Checked)
var visual string
if visu {
vis := visualize.NewVisual(ty.Checked)
vis.Build()
visual = vis.Render()
}
if reach {
r := reachability.NewTracer()
r.Scan(ty.Checked)
}
return tree, lstnr, ty, visual, sw.Alias
}
func validate_filetype(data string, filetype string) bool {
if filetype == "fspec" && data[0:4] == "spec" {
return true
}
if filetype == "fsystem" && data[0:6] == "system" {
return true
}
return false
}
func smt2(ir string, compiler *llvm.Compiler) *smt.Generator {
generator := smt.NewGenerator()
generator.LoadMeta(compiler)
generator.Run(ir)
return generator
}
func plainSolve(smt string) {
ex := execute.NewModelChecker()
ex.LoadModel(smt, nil, nil, nil, nil)
ok, err := ex.Check()
if err != nil {
log.Fatalf("model checker has failed: %s", err)
}
if !ok {
fmt.Println("Fault could not find a failure case.")
return
}
scenario, err := ex.PlainSolve()
if err != nil {
log.Fatalf("error found fetching solution from solver: %s", err)
}
fmt.Println(scenario)
}
func probability(smt string, uncertains map[string][]float64, unknowns []string, results map[string][]*smtvar.VarChange, rlog *resultlog.ResultLog) (*execute.ModelChecker, map[string]execute.Scenario) {
ex := execute.NewModelChecker()
ex.LoadModel(smt, uncertains, unknowns, results, rlog)
ok, err := ex.Check()
if err != nil {
log.Fatalf("model checker has failed: %s", err)
}
if !ok {
fmt.Println("Fault could not find a failure case.")
return ex, nil
}
scenario, err := ex.Solve()
if err != nil {
log.Fatalf("error found fetching solution from solver: %s", err)
}
data := ex.Filter(scenario)
return ex, data
}
func run(filepath string, mode string, input string, output string, reach bool) {
filetype := util.DetectMode(filepath)
if filetype == "" {
log.Fatal("file provided is not a .fspec or .fsystem file")
}
filepath = util.Filepath(filepath)
uncertains := make(map[string][]float64)
unknowns := []string{}
data, err := os.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
d := string(data)
path := gopath.Dir(filepath)
switch input {
case "fspec":
tree, lstnr, ty, visual, alias := parse(d, path, filepath, filetype, reach, output == "visualize")
if lstnr == nil {
log.Fatal("Fault parser returned nil")
}
if mode == "ast" {
fmt.Println(lstnr.AST)
return
}
compiler := llvm.Execute(tree, ty.SpecStructs, lstnr.Uncertains, lstnr.Unknowns, alias, false)
uncertains = compiler.Uncertains
unknowns = compiler.Unknowns
if mode == "ir" {
fmt.Println(compiler.GetIR())
return
}
if !compiler.IsValid && visual != "" {
fmt.Println(visual)
fmt.Printf("\n\n")
return
}
if !compiler.IsValid {
fmt.Println("Fault found nothing to run. Missing run block or start block.")
return
}
generator := smt.Execute(compiler)
if mode == "smt" {
fmt.Println(generator.SMT())
return
}
if output == "smt" {
plainSolve(generator.SMT())
return
}
mc, data := probability(generator.SMT(), uncertains, unknowns, generator.Results, generator.Log)
if output == "visualize" {
fmt.Println(visual)
fmt.Printf("\n\n")
mc.Mermaid()
return
}
if data != nil && output == "legacy" {
mc.LoadMeta(generator.Forks)
mc.Format(data)
return
}
if data != nil && output == "static" {
mc.LoadMeta(generator.Forks)
mc.Static(data)
return
}
if data != nil {
mc.LoadMeta(generator.Forks)
mc.EventLog(data)
}
case "ll":
compiler := llvm.NewCompiler()
compiler.Uncertains = uncertains
compiler.Unknowns = unknowns
generator := smt2(d, compiler)
if mode == "smt" {
fmt.Println(generator.SMT())
return
}
if output == "smt" {
plainSolve(generator.SMT())
return
}
mc, data := probability(generator.SMT(), uncertains, unknowns, generator.Results, generator.Log)
if mode == "visualize" {
mc.Mermaid()
return
}
if data != nil && output == "legacy" {
mc.LoadMeta(generator.Forks)
mc.Format(data)
return
}
if data != nil && output == "static" {
mc.LoadMeta(generator.Forks)
mc.Static(data)
return
}
if data != nil {
mc.LoadMeta(generator.Forks)
mc.EventLog(data)
}
case "smt2":
if output == "smt" {
plainSolve(d)
return
}
mc, data := probability(d, uncertains, unknowns, make(map[string][]*smtvar.VarChange), &resultlog.ResultLog{})
if mode == "visualize" {
mc.Mermaid()
return
}
if data != nil && output == "legacy" {
mc.Format(data)
return
}
if data != nil && output == "static" {
mc.Static(data)
return
}
if data != nil {
fmt.Println("~~~~~~~~~~\n Fault found the following scenario\n~~~~~~~~~~")
mc.EventLog(data)
}
}
}
func main() {
var mode string
var input string
var output string
var filepath string
var reach bool
modeCommand := flag.String("m", "check", "stop compiler at certain milestones: ast, ir, smt, or check")
inputCommand := flag.String("i", "fspec", "format of the input file (default: fspec)")
fpCommand := flag.String("f", "", "path to file to compile")
reachCommand := flag.Bool("complete", false, "make sure the transitions to all defined states are specified in the model")
outputCommand := flag.String("format", "log", "format of the output: log, static, smt, legacy, or visualize")
flag.Parse()
if *fpCommand == "" {
flag.PrintDefaults()
fmt.Println("must provide path of file to compile")
os.Exit(1)
}
filepath = *fpCommand
if *modeCommand == "" {
mode = "check"
} else {
mode = strings.ToLower(*modeCommand)
switch mode {
case "ast":
case "ir":
case "smt":
case "check":
default:
fmt.Printf("%s is not a valid mode\n", mode)
os.Exit(1)
}
}
if *outputCommand == "" {
output = "log"
} else {
output = strings.ToLower(*outputCommand)
switch output {
case "static":
case "log":
case "legacy":
case "visualize":
case "smt":
default:
fmt.Printf("%s is not a valid mode\n", output)
os.Exit(1)
}
}
//Check if solver is set
if mode == "check" &&
os.Getenv("SOLVERCMD") == "" || os.Getenv("SOLVERARG") == "" {
fmt.Printf("\n no solver configured, defaulting to SMT output without model checking. Please set SOLVERCMD and SOLVERARG variables.\n\n")
mode = "smt"
}
if *inputCommand == "" {
input = "fspec"
} else {
input = strings.ToLower(*inputCommand)
switch input {
case "fspec":
case "ll":
case "smt2":
default:
fmt.Printf("%s is not a valid input format\n", input)
os.Exit(1)
}
}
if *reachCommand {
reach = true
}
run(filepath, mode, input, output, reach)
}