-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
641 lines (517 loc) · 15.8 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/nicolasdilley/gomela/promela"
"github.com/nicolasdilley/gomela/stats"
)
type ProjectResult struct {
Name string // the name of the project
Num_states int // The overall number of states needed to verify the whole program
Infer_timing int // time in milli to infer the promela model
Models []VerificationRun // Verification run for all Models
Safety_error bool // is there any safety errors
Global_deadlock bool // is there any global deadlock
Spin_timing int // time in milli to verify the program with spin
Godel_timing int // time in milli to verify the program with godel
Migoinfer_timing int // time in milli to model the program with migoinfer
}
type VerificationInfo struct {
multi_list *string
multi_projects *string
single_project *string
num_concurrency_primitive_as_global int
unused_mutex int
unused_wg int
unused_chan int
// single_file *string
}
var (
NUM_OF_MODELS int = 0
NUM_OF_EXECUTABLE_MODELS int = 0
RESULTS_FOLDER = "result"
PROJECTS_FOLDER = "projects"
AUTHOR_PROJECT_SEP = "--"
PACKAGE_MODEL_SEP = "++"
)
func main() {
// connecting to github to parse all git projects
// add timestamps to name of folder
_, err := os.Stat(PROJECTS_FOLDER)
if os.IsNotExist(err) { // create the projects folder if not there
errDir := os.MkdirAll(PROJECTS_FOLDER, 0755)
if errDir != nil {
log.Fatal(err)
}
}
f, _ := os.OpenFile("./"+RESULTS_FOLDER+"/package_errors.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
os.Stderr = f
defer f.Close()
ver := &VerificationInfo{}
projects := flag.String("p", "", "a folder that contains all the projects.")
ver.multi_list = flag.String("l", "", "a .csv is also given as args and contains a list of github.com projects with their commits to parse.")
ver.multi_projects = flag.String("mp", "", "Recursively loop through the folder given and parse all folder that contains a go file.")
ver.single_project = flag.String("s", "", "a single project is given to parse. Format \"creator/project_name\"")
flag.Parse()
if *projects != "" {
PROJECTS_FOLDER = *projects
}
switch os.Args[1] {
case "model": // the user wants to generate the model
model(ver)
fmt.Println("Num of global concurrency primitives ", ver.num_concurrency_primitive_as_global)
case "verify": // the user wants to verify a .pml file with specifics bounds
if len(os.Args) > 2 && strings.Contains(os.Args[2], ".pml") {
model_to_verify := os.Args[2]
// parse how many comm pars are in the file
content, err := ioutil.ReadFile(model_to_verify)
if err != nil {
panic("Please provide a valid file , err : " + err.Error())
}
mand_params, opt_params := findNumCommParam(string(content))
if len(os.Args)-3-mand_params-opt_params != 0 {
panic("Please provide a value for each comm parameter in the order they appear in the program, num params = " + fmt.Sprint(mand_params+opt_params) + ", num args given " + fmt.Sprint(len(os.Args)))
} else {
verifyModelWithSpecificValues(string(content), os.Args[3:])
}
} else {
panic("Please provide a .pml file : ie. gomela verify hello.pml")
}
case "full_stats": // Generate a set of stats for each projects and models
stats.Stats()
case "stats":
printStats()
case "fs": // Full scale model -> verify -> stats
model(ver)
verify(ver, RESULTS_FOLDER)
case "bmc":
if len(os.Args) > 2 {
verify(ver, os.Args[2])
} else {
panic("Please provide a folder that contains the .pml that you want to parse or a pml that you want to verify.")
}
case "commit": // produce a list of commit from given list of projects
commit(ver)
case "sanity": // remove the .pml files that do nothing
if len(os.Args) > 2 {
del := false
if len(os.Args) > 3 {
del = true
}
num_unsain := sanity(ver, os.Args[2], del)
fmt.Println("Removed a total of ", num_unsain, " files which did not contain any concurrent interactions")
fmt.Println("Num of mutex : ", ver.unused_mutex)
fmt.Println("Num of wg : ", ver.unused_wg)
fmt.Println("Num of chan : ", ver.unused_chan)
} else {
panic("You need to provide a folder containing the .pml or a .pml file.")
}
default:
panic("You need to provide a projects list as a .csv file. ie 'gomela commit projects.csv'")
}
}
func findNumCommParam(content string) (int, int) {
mand_params := 0
opt_params := 0
lines := strings.Split(content, "\n")
for _, line := range lines {
if strings.Contains(line, "num_mand_comm_params") {
splitted := strings.Split(line, "num_mand_comm_params=")
n, _ := strconv.Atoi(splitted[1])
mand_params += n
}
if strings.Contains(line, "num_opt_comm_params") {
splitted := strings.Split(line, "num_opt_comm_params=")
n, _ := strconv.Atoi(splitted[1])
opt_params += n
}
}
return mand_params, opt_params
}
func sanity(ver *VerificationInfo, path string, del bool) int {
unsain_file := 0
if strings.Contains(path, ".pml") {
if !sanityCheckFile(ver, path, del) {
unsain_file++
}
} else {
f, err := os.Stat(path)
if err != nil {
panic("Could not read file/folder " + path)
}
if f.IsDir() {
filepath.Walk(path,
func(fpath string, info os.FileInfo, err error) error {
if info.IsDir() && fpath != path {
unsain_file += sanity(ver, fpath, del)
return filepath.SkipDir
} else if strings.Contains(fpath, ".pml") {
if !sanityCheckFile(ver, fpath, del) {
unsain_file++
}
}
return nil
})
} else {
panic("The file provided should be a .pml file or a directory")
}
}
return unsain_file
}
func sanityCheckFile(ver *VerificationInfo, path string, del bool) bool {
data, e := ioutil.ReadFile(path)
if e != nil {
panic("Could not read file " + path)
}
model := strings.Split(string(data), "/*")[0]
used := false
for _, line := range strings.Split(model, "\n") {
if strings.Contains(line, ".update!") { // add
used = true
}
if strings.Contains(line, ".wait?") { // add
used = true
}
if strings.Contains(line, ".Lock!") { // add
used = true
}
if strings.Contains(line, ".RLock!") { // add
used = true
}
if strings.Contains(line, ".Unlock!") { // add
used = true
}
if strings.Contains(line, ".RUnlock!") { // add
used = true
}
if strings.Contains(line, ".deq?") { // add
used = true
}
if strings.Contains(line, ".rcving!") { // add
used = true
}
if strings.Contains(line, ".sending!") { // add
used = true
}
if strings.Contains(line, ".sync") { // add
used = true
}
if strings.Contains(line, ".enq!") { // add
used = true
}
}
if !used {
for _, line := range strings.Split(model, "\n") {
if strings.Contains(line, "run mutexMonitor") { // Mutex
fmt.Println(line)
ver.unused_mutex++
}
if strings.Contains(line, "run wgMonitor") { // wg
ver.unused_wg++
}
if strings.Contains(line, "run sync_monitor") { // Chan
ver.unused_chan++
}
}
// check whether it contains a channel, wg or mutex
if del {
os.Remove(path)
}
return false
}
return true
}
func commit(ver *VerificationInfo) {
// parse multiple projects
if len(os.Args) > 2 {
if strings.HasSuffix(os.Args[2], ".csv") || strings.HasSuffix(os.Args[2], ".txt") {
// parse each projects
data, e := ioutil.ReadFile(os.Args[2])
if e != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", os.Args[2], e)
return
}
projects_commit := ""
proj_listings := strings.Split(string(data), "\n")
fmt.Println(len(proj_listings), " projects to parse")
for _, project := range proj_listings[:len(proj_listings)-1] {
if strings.Contains(project, ",") {
project_info := strings.Split(project, ",")
if len(project_info) > 1 {
panic("Commit already present")
} else {
// clone repo and get commit
commit := CloneRepoAndGetCommit(project)
projects_commit += project + "," + commit + "\n"
}
} else {
// clone repo and get commit
commit := CloneRepoAndGetCommit(project)
projects_commit += project + "," + commit + "\n"
}
}
ioutil.WriteFile("commits.csv", []byte(projects_commit), 0664)
} else {
fmt.Println("Please provide a .csv or .txt file containing the list of projects to be parsed")
}
}
}
// genreate a model based on input and return the flags left
func model(ver *VerificationInfo) []string {
t := time.Now().Local().Format("2006-01-02--15:04:05")
RESULTS_FOLDER += t
os.Mkdir(RESULTS_FOLDER, os.ModePerm)
promela.CreateCSV(RESULTS_FOLDER)
switch os.Args[2] {
case "l":
// parse multiple projects
if len(os.Args) > 3 {
if strings.HasSuffix(os.Args[3], ".csv") {
// parse each projects
data, e := ioutil.ReadFile(os.Args[3])
if e != nil {
panic(fmt.Sprintf("prevent panic by handling failure accessing a path %q: %v\n", os.Args[3], e))
}
proj_listings := strings.Split(string(data), "\n")
fmt.Println(len(proj_listings), " projects to parse")
for _, project := range proj_listings[:len(proj_listings)-1] {
if strings.Contains(project, ",") {
project_info := strings.Split(project, ",")
if len(proj_listings) > 1 {
parseProject(project_info[0], project_info[1], ver)
}
} else {
parseProject(project, "master", ver)
}
}
fmt.Println(NUM_OF_EXECUTABLE_MODELS, "/", NUM_OF_MODELS, " executable models overall.")
} else {
fmt.Println("Please provide a .csv file containing the list of projects to be parsed")
}
}
return os.Args[3:]
case "mp":
path := os.Args[3]
// PROJECTS_FOLDER = path
path, _ = filepath.Abs(path)
files, _ := ioutil.ReadDir(path)
for _, f := range files {
parseFolder(path+"/"+f.Name(), ver)
}
return os.Args[3:]
case "s":
// parse project given
parseProject(os.Args[3], "master", ver)
return os.Args[3:]
default:
path := os.Args[2]
// PROJECTS_FOLDER = path
_, err := ioutil.ReadDir(path)
if err != nil {
panic("please give a valid folder to parse.")
}
packages := []string{}
filepath.Walk(path, func(path string, file os.FileInfo, err error) error {
if file.IsDir() {
if file.Name() != "vendor" && file.Name() != "third_party" {
path, _ = filepath.Abs(path)
packages = append(packages, path)
} else {
return filepath.SkipDir
}
}
return nil
})
inferProject(path, filepath.Base(path), "", packages, ver)
if len(os.Args) > 3 {
return os.Args[3:]
}
return []string{}
}
if len(os.Args) > 4 {
return os.Args[4:]
}
return []string{}
}
func verify(ver *VerificationInfo, toParse string) {
// toPrint := "Model, Opt, #states, Time (ms), Channel Safety Error, Global Deadlock, Error, Comm param info, Link,\n"
toPrint := ""
// check if toParse is a folder or a .pml file
f, err := os.Stat(toParse)
if err != nil {
panic("The folder given cannot be parsed")
}
bounds_to_check := []interface{}{}
bounds_index := 3
for bounds_index < len(os.Args) {
_, err := strconv.Atoi(os.Args[bounds_index])
if err != nil {
bounds_index++
} else {
break
}
}
if len(os.Args) > bounds_index {
for _, b := range os.Args[bounds_index:] {
num, err := strconv.Atoi(b)
if err != nil {
panic("Should provide a number for the bounds. Got : " + b)
}
bounds_to_check = append(bounds_to_check, num)
}
}
if f.IsDir() {
RESULTS_FOLDER = toParse
// Print CSV
f, err := os.OpenFile("./"+toParse+"/verification.csv",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Could not create file verification.csv")
return
}
if _, err := f.WriteString(toPrint); err != nil {
panic(err)
}
projects, err := ioutil.ReadDir(RESULTS_FOLDER)
if err != nil {
fmt.Println("Could not read folder :", RESULTS_FOLDER)
}
for _, p := range projects {
if p.IsDir() {
//verify the models inside the projects
models, err := ioutil.ReadDir(RESULTS_FOLDER + "/" + p.Name())
if err != nil {
fmt.Println("Could not read folder :", p)
}
VerifyModels(models, p.Name(), bounds_to_check)
}
}
} else { // a single .pml has been given as arg
path, _ := filepath.Abs("./")
RESULTS_FOLDER = path
VerifyModels([]os.FileInfo{f}, "", bounds_to_check)
}
}
func parseFolder(path string, ver *VerificationInfo) {
files, _ := ioutil.ReadDir(path)
containsGoFile := false
for _, f := range files {
if strings.Contains(f.Name(), ".go") {
containsGoFile = true
}
}
if containsGoFile {
packages := []string{}
filepath.Walk(path, func(path string, file os.FileInfo, err error) error {
if file.IsDir() {
if file.Name() != "vendor" && file.Name() != "third_party" {
path, _ = filepath.Abs(path)
packages = append(packages, path)
} else {
return filepath.SkipDir
}
}
return nil
})
inferProject(path, filepath.Base(path), "", packages, ver)
} else {
for _, f := range files {
parseFolder(path+"/"+f.Name(), ver)
}
}
}
func parseProject(project_name string, commit string, ver *VerificationInfo) {
fmt.Println("Infering : " + project_name)
path_to_dir, commit_hash, err := CloneRepo(project_name, commit)
if err == nil {
packages := []string{}
err = filepath.Walk(path_to_dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path_to_dir, err)
return err
}
if info.IsDir() {
if info.Name() != "vendor" {
packages = append(packages, "."+strings.TrimPrefix(path, path_to_dir))
} else {
return filepath.SkipDir
}
}
return nil
})
inferProject(path_to_dir, project_name, commit_hash, packages, ver)
if err != nil {
fmt.Printf("Error walking the path %q: %v\n", path_to_dir, err)
}
} else {
fmt.Println("Could not download project ", project_name)
}
}
type Param struct {
Name string
Mandatory bool
}
func printStats() {
if len(os.Args) > 2 {
// read file
data, e := ioutil.ReadFile(os.Args[2])
if e != nil {
panic("The file provided " + os.Args[2] + " could not be open")
}
// print stats
lines := strings.Split(string(data), "\n")
params := []Param{}
for _, line := range lines {
if strings.Contains(line, "//") && (strings.Contains(line, " opt ") || strings.Contains(line, " mand ")) {
splitted := strings.Split(line, "//")
splitted = strings.Split(splitted[1], " ")
param := Param{Name: splitted[2], Mandatory: splitted[1] == "mand"}
params = append(params, param)
}
}
fmt.Println("Num comm params : ", len(params))
fmt.Println("Mandatory Param : ")
for _, param := range params {
if param.Mandatory {
fmt.Println(param.Name)
}
}
fmt.Println("Optionnal Param : ")
for _, param := range params {
if !param.Mandatory {
fmt.Println(param.Name)
}
}
} else {
panic("Please provide a .pml file : ie, gomela stats hello.pml")
}
}
func inferProject(path string, dir_name string, commit string, packages []string, ver *VerificationInfo) {
// Partition program
dir_name = strings.Replace(dir_name, "/", AUTHOR_PROJECT_SEP, -1)
f, ast_map := GenerateAst(path, packages, dir_name)
if f != nil {
projects_folder, _ := filepath.Abs(PROJECTS_FOLDER)
ParseAst(f, dir_name, commit, ast_map, ver, RESULTS_FOLDER, projects_folder)
// Have a way to give values to individual candidates and unknown parameter
} else {
fmt.Println("Error while parsing project")
}
}
func colorise(flag bool) string {
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
if flag {
return red("true")
}
return green("false")
}