-
Notifications
You must be signed in to change notification settings - Fork 39
/
value_type.go
363 lines (330 loc) · 8.07 KB
/
value_type.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
package constraint
import (
"github.com/system-pclub/GCatch/GFix/dispatcher/tools/go/ssa"
"go/token"
"strings"
)
func Type_of_value(v ssa.Value) string {
if _,ok := v.(*ssa.Alloc); ok {
return "Alloc"
}
if _,ok := v.(*ssa.BinOp); ok {
return "BinOp"
}
if _,ok := v.(*ssa.Builtin); ok {
return "Builtin"
}
if _,ok := v.(*ssa.Call); ok {
return "Call"
}
if _,ok := v.(*ssa.ChangeInterface); ok {
return "ChangeInterface"
}
if _,ok := v.(*ssa.ChangeType); ok {
return "ChangeType"
}
if _,ok := v.(*ssa.Const); ok {
return "Const"
}
if _,ok := v.(*ssa.Convert); ok {
return "Convert"
}
if _,ok := v.(*ssa.Extract); ok {
return "Extract"
}
if _,ok := v.(*ssa.Field); ok {
return "Field"
}
if _,ok := v.(*ssa.FieldAddr); ok {
return "FieldAddr"
}
if _,ok := v.(*ssa.FreeVar); ok {
return "FreeVar"
}
if _,ok := v.(*ssa.Function); ok {
return "Function"
}
if _,ok := v.(*ssa.Global); ok {
return "Global"
}
if _,ok := v.(*ssa.Index); ok {
return "Index"
}
if _,ok := v.(*ssa.IndexAddr); ok {
return "IndexAddr"
}
if _,ok := v.(*ssa.Lookup); ok {
return "Lookup"
}
if _,ok := v.(*ssa.MakeChan); ok {
return "MakeChan"
}
if _,ok := v.(*ssa.MakeClosure); ok {
return "MakeClosure"
}
if _,ok := v.(*ssa.MakeInterface); ok {
return "MakeInterface"
}
if _,ok := v.(*ssa.MakeMap); ok {
return "MakeMap"
}
if _,ok := v.(*ssa.MakeSlice); ok {
return "MakeSlice"
}
if _,ok := v.(*ssa.Next); ok {
return "Next"
}
if _,ok := v.(*ssa.Parameter); ok {
return "Parameter"
}
if _,ok := v.(*ssa.Phi); ok {
return "Phi"
}
if _,ok := v.(*ssa.Range); ok {
return "Range"
}
if _,ok := v.(*ssa.Select); ok {
return "Select"
}
if _,ok := v.(*ssa.Slice); ok {
return "Slice"
}
if _,ok := v.(*ssa.TypeAssert); ok {
return "TypeAssert"
}
if _,ok := v.(*ssa.UnOp); ok {
return "UnOp"
}
return "unknown"
}
// Now can handle == != < <= > >=
func (s *SMT_set) create_assert_body_for_BinOp(binOp *ssa.BinOp) string {
var v1,v2 string
if v1_const,ok := binOp.X.(*ssa.Const); ok {
if v1_const.IsNil() {
v1 = nil_name(v1_const)
s.Todo = append(s.Todo,v1_const)
} else {
v1 = strip_quote(v1_const.Value.ExactString())
}
} else {
v1 = value_name(binOp.X)
s.Todo = append(s.Todo,binOp.X)
}
if v2_const,ok := binOp.Y.(*ssa.Const); ok {
if v2_const.IsNil() {
v2 = nil_name(v2_const)
s.Todo = append(s.Todo,v2_const)
} else {
v2 = strip_quote(v2_const.Value.ExactString())
}
} else {
v2 = value_name(binOp.Y)
s.Todo = append(s.Todo,binOp.Y)
}
var body string
switch binOp.Op {
case token.EQL:
body = "(= " + v1 + " " + v2 + ")"
case token.NEQ:
body = "(not (= " + v1 + " " + v2 + "))"
case token.GTR:
body = "(> " + v1 + " " + v2 + ")"
case token.GEQ:
body = "(not (< " + v1 + " " + v2 + "))"
case token.LSS:
body = "(< " + v1 + " " + v2 + ")"
case token.LEQ:
body = "(not (> " + v1 + " " + v2 + "))"
default:
return ""
}
return body
}
// Now len() is special. The other calls are the same: declare the value as a new variable
func (s *SMT_set) handle_Call(c *ssa.Call) {
CallCommon := c.Call
//see if c is len()
if CallCommon.IsInvoke() == false {
callee_builtin,ok := CallCommon.Value.(*ssa.Builtin)
if ok {
if callee_builtin.Name() == "len" {
if len(CallCommon.Args) == 1 {
// Now we are sure this is like t2 = len(t1)
argument := CallCommon.Args[0]
type_in_len :=argument.Type().String()
sort_name := type2sort(type_in_len)
// expect type_in_len is like "[][]byte" and then sort_name is "S_S_Byte"
// Now we see if "S_S_Byte" is already declared. If not, declare it
s.sort_dec_or_def(sort_name)
// Now we see if "len_S_S_Byte" is already declared. If not, declare it
len_name := "len_" + sort_name
new_dec_fn := Dec_fn {
len_name,
[]string{sort_name},
[]string{"Int"},
}
if is_dec_fn_in_slice(new_dec_fn,s.Dec_fns) == false {
s.Dec_fns = append_left_fn(s.Dec_fns,new_dec_fn)
}
// Now we see if t2 is declared
c_value_name := value_name(c)
new_dec_const := Dec_const {
c_value_name,
"Int",
c,
}
if is_dec_const_in_slice(new_dec_const,s.Dec_consts) == false {
s.Dec_consts = append_left_const(s.Dec_consts,new_dec_const)
}
// Assert t2
assert_str := "(assert (= " + c_value_name + " (" + len_name + " " + value_name(argument) + ")))"
new_assert := Assert(assert_str)
s.Asserts = append_left_assert(s.Asserts,new_assert)
//Add t1 to s.Todo
s.Todo = append(s.Todo,argument)
return
}
}
}
}
//Other situations: declare t2 directly
c_value_name := value_name(c)
new_dec_const := Dec_const {
c_value_name,
"Int",
c,
}
if is_dec_const_in_slice(new_dec_const,s.Dec_consts) == false {
s.Dec_consts = append_left_const(s.Dec_consts,new_dec_const)
}
}
func type2sort(type_name string) (sort_name string) {
//handling slice
core_type,layers_of_slice := strip_slice_type(type_name)
for i:=0; i < layers_of_slice; i++ {
sort_name += "S_"
}
var core_sort string
switch core_type {
case "byte":
core_sort = "Byte"
case "int":
core_sort = "Int"
case "int64":
core_sort = "Int"
case "string":
core_sort = "String"
case "bool":
core_sort = "Bool"
default:
core_sort = "Sort_" + core_type
}
sort_name += core_sort
//handling defined structs
sort_name = strings.ReplaceAll(sort_name,"*","Ptr_")
sort_name = strings.ReplaceAll(sort_name,"/","_Of_")
return
}
// sort can be "Byte", can be "S_S_Byte"
func (s *SMT_set) sort_dec_or_def(sort string) {
core_sort,layers_of_slice := strip_slice_sort(sort)
// see if the sort has already been declared. Need to go through both default_sorts and s.Dec_sorts
new_dec_sort := Dec_sort{
core_sort,
}
if is_string_in_slice(core_sort,Default_sorts) == false && is_dec_sort_in_slice(new_dec_sort,s.Dec_sorts) == false {
s.Dec_sorts = append_left_dec_sorts(s.Dec_sorts,new_dec_sort)
}
if layers_of_slice == 0 {
return
}
// Now we see if "S_S_Byte" is already defined. If not, define it
new_define := Define_sort{}
new_define.Has_input_output = true
new_define.Input = ""
new_define.Name = sort
new_define.Output = core_sort
for i:=0; i < layers_of_slice; i++ {
new_define.Output = "Array Int " + new_define.Output
}
if is_def_in_slice(new_define,s.Define_sorts) == false {
s.Define_sorts = append_left_define_sorts(s.Define_sorts,new_define)
}
}
// Now only * is handled
func (s *SMT_set) handle_UnOp(u *ssa.UnOp) {
switch u.Op {
default:
// TODO: need alias analysis for load and store. Now we only declare a new const here without any assert
u_type := u.Type().String()
u_sort := type2sort(u_type)
new_dec_const := Dec_const{
Name: value_name(u),
Type: u_sort,
Value: u,
}
s.sort_dec_or_def(u_sort)
if is_dec_const_in_slice(new_dec_const,s.Dec_consts) == false {
s.Dec_consts = append_left_const(s.Dec_consts,new_dec_const)
}
}
}
func strip_slice_sort(str string) (core string,layers int) {
layers = strings.Count(str,"S_")
core = strings.ReplaceAll(str,"S_","")
return
}
func strip_slice_type(str string) (core string,layers int) {
layers = strings.Count(str,"[]")
core = strings.ReplaceAll(str,"[]","")
return
}
func is_dec_fn_in_slice(new Dec_fn, slice []Dec_fn) bool {
for _,elem := range slice {
if new.Name == elem.Name {
return true
}
}
return false
}
func is_dec_const_in_slice(new Dec_const, slice []Dec_const) bool {
for _,elem := range slice {
if new.Name == elem.Name {
return true
}
}
return false
}
func is_dec_sort_in_slice(new Dec_sort, slice []Dec_sort) bool {
for _,elem := range slice {
if new.Name == elem.Name {
return true
}
}
return false
}
func is_def_in_slice(new Define_sort, slice []Define_sort) bool {
for _,elem := range slice {
if new.Name == elem.Name {
return true
}
}
return false
}
func is_assert_in_slice(assert Assert,slice []Assert) bool {
for _,elem := range slice {
if assert == elem {
return true
}
}
return false
}
func is_string_in_slice(str string,slice []string) bool {
for _,elem := range slice {
if str == elem {
return true
}
}
return false
}