-
Notifications
You must be signed in to change notification settings - Fork 22
/
value.py
585 lines (463 loc) · 15.4 KB
/
value.py
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
# Copyright 2014-2015 The Alive authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy, operator
from common import *
from codegen import CVariable, CFieldAccess
def allTyEqual(vars, Ty):
c = [vars[0].type.typevar == Ty]
for i in range(1, len(vars)):
c += [vars[0].type == vars[i].type]
return c
def mkTyEqual(types):
return [types[0] == types[i] for i in range(1, len(types))]
def create_mem_if_needed(ptr, val, state, qvars):
# if we are dealing with an arbitrary pointer, assume it points to something
# that can (arbitrarily) hold 7 elements.
if isinstance(val.type, PtrType):
block_size = val.type.getSize()
elif isinstance(val.type, UnknownType) and val.type.myType == Type.Ptr:
block_size = val.type.types[Type.Ptr].getSize()
else:
return
num_elems = 7
size = block_size * num_elems
state.addInputMem(ptr, qvars, block_size, num_elems)
class Type:
Int, Ptr, Array, Unknown = list(range(4))
def __repr__(self):
return ''
def typeMismatch(self, expected):
raise ParseError('%s type required' % expected, str(self))
def ensureIntType(self, size = None):
self.typeMismatch('int')
def ensurePtrType(self):
self.typeMismatch('pointer')
def ensureFirstClass(self):
self.typeMismatch('first class')
def ensureIntPtrOrVector(self):
self.typeMismatch('int/ptr/vector')
################################
def getMostSpecificType(t1, t2):
def _ErrorOnTypeMismatch(cond):
if cond:
raise ParseError('Type mismatch: %s vs %s' % (t1, t2))
if isinstance(t1, UnknownType):
return t2
if isinstance(t2, UnknownType):
return t1
_ErrorOnTypeMismatch(t1.__class__ != t2.__class__)
if isinstance(t1, IntType):
_ErrorOnTypeMismatch(t1.defined and t2.defined and
t1.getSize() != t2.getSize())
return t1 if t1.defined else t2
if isinstance(t1, PtrType):
t1id = id(t1.type)
return t1 if id(getMostSpecificType(t1.type, t2.type)) == t1id else t2
assert False
################################
class UnknownType(Type):
def __init__(self, d = 0):
self.types = {self.Int: IntType(),
self.Ptr: PtrType(depth = d),
self.Array: ArrayType(depth = d),
}
self.myType = self.Unknown
def ensureIntType(self, size = None):
return IntType(size)
def ensurePtrType(self):
return PtrType()
def ensureFirstClass(self):
# Restrict to ints, pointers, FPs, vectors
del self.types[self.Array]
return self
def ensureIntPtrOrVector(self):
# only ints, ptrs, or vectors of ints/ptrs
del self.types[self.Array]
return self
def setName(self, name):
self.typevar = Int('t_' + name)
for t in self.types.values():
t.setName(name)
def _getSizeUnknown(self, idx):
if idx == len(self.types)-1:
return self.types[idx].getSize()
return If(self.typevar == idx,
self.types[idx].getSize(),
self._getSizeUnknown(idx+1))
def getSize(self):
if self.myType != self.Unknown:
return self.types[self.myType].getSize()
return self._getSizeUnknown(0)
def getIntType(self, c):
if self.myType == self.Unknown and self.Int in self.types:
self.myType = self.Int
c += [self.typevar == self.Int]
if self.myType == self.Int:
return self.types[self.Int]
return None
def getPointeeType(self):
assert self.myType == self.Unknown or self.myType == self.Ptr
self.myType = self.Ptr
return self.types[self.Ptr].getPointeeType()
def getUnderlyingType(self):
assert self.myType == self.Ptr or self.myType == self.Array
return self.types[self.myType].getUnderlyingType()
def fixupTypes(self, types):
self.myType = types.get_interp(self.typevar).as_long()
self.types[self.myType].fixupTypes(types)
def __eq__(self, other):
if self.myType != self.Unknown:
return And(self.typevar == self.myType,
self.types[self.myType] == other)
for i,type in self.types.items():
if isinstance(other, type.__class__):
self.myType = i
return And(self.typevar == i, type == other)
assert isinstance(other, UnknownType)
c = []
for i,type in self.types.items():
if i in other.types:
c += [And(self.typevar == i,
other.typevar == i,
type == other.types[i])]
return mk_or(c)
def _intcmp(self, op, other):
c = []
op1 = self.getIntType(c)
if op1 is None:
return BoolVal(False)
return mk_and(c + [op(op1, other)])
def __lt__(self, other):
return self._intcmp(operator.lt, other)
def __gt__(self, other):
return self._intcmp(operator.gt, other)
def __ge__(self, other):
return self._intcmp(operator.ge, other)
def ensureTypeDepth(self, depth):
c = []
for i in range(len(self.types)):
c += [Or(self.types[i].ensureTypeDepth(depth), self.typevar != i)]
return mk_and(c)
def getTypeConstraints(self):
if self.myType != self.Unknown:
return self.types[self.myType].getTypeConstraints()
return mk_or([t.getTypeConstraints() for t in self.types.values()])
################################
class NamedType(UnknownType):
def __init__(self, name):
UnknownType.__init__(self)
self.type = UnknownType()
self.name = name
def __repr__(self):
return self.name
def ensureIntType(self, size = None):
self.myType = self.Int
if size != None:
self.types[self.Int] = IntType(size)
self.type = self.type.ensureIntType(size)
return self
def ensurePtrType(self):
self.myType = self.Ptr
self.type = self.type.ensurePtrType()
return self
def setName(self, name):
UnknownType.setName(self, self.name)
self.type.setName(name)
def getTypeConstraints(self):
return And(self.type == self,
UnknownType.getTypeConstraints(self),
self.type.getTypeConstraints())
################################
class IntType(Type):
def __init__(self, size = None):
if size == None:
self.defined = False
return
self.size = size
self.defined = True
assert isinstance(self.size, int)
def ensureIntType(self, size = None):
assert self.defined == False or size == None or size == self.size
if size != None:
self.size = size
self.defined = True
return self
def ensureFirstClass(self):
return self
def ensureIntPtrOrVector(self):
return self
def setName(self, name):
self.typevar = Int('t_' + name)
self.bitsvar = Int('size_' + name)
def __repr__(self):
if self.defined:
return 'i' + str(self.size)
return ''
def getSize(self):
if hasattr(self, 'size'):
return self.size
return self.bitsvar
def fixupTypes(self, types):
size = types.get_interp(self.bitsvar).as_long()
assert self.defined == False or self.size == size
self.size = size
def __eq__(self, other):
if isinstance(other, IntType):
return self.bitsvar == other.bitsvar
if isinstance(other, int):
return self.bitsvar == other
if isinstance(other, UnknownType):
return other == self
return BoolVal(False)
def _cmp(self, op, other):
if isinstance(other, IntType):
return op(self.bitsvar, other.bitsvar)
if isinstance(other, int):
return op(self.bitsvar, other)
if isinstance(other, UnknownType):
c = []
op2 = other.getIntType(c)
return mk_and(c + [op(self.bitsvar, op2.bitsvar)])
assert False
def __lt__(self, other):
return self._cmp(operator.lt, other)
def __gt__(self, other):
return self._cmp(operator.gt, other)
def __ge__(self, other):
return self._cmp(operator.ge, other)
def ensureTypeDepth(self, depth):
return BoolVal(depth == 0)
def getTypeConstraints(self):
c = [self.typevar == Type.Int]
if self.defined:
c += [self.bitsvar == self.getSize()]
else:
# Integers are assumed to be up to 64 bits.
# We bias towards 4/8 bits, as counterexamples become easier to understand
c += [Or(self.bitsvar == 8, self.bitsvar == 4,
And(self.bitsvar > 0, self.bitsvar <= 64))]
return And(c)
################################
class PtrType(Type):
def __init__(self, type = None, depth = 0):
if type is None:
# limit type nesting to 1 level
if depth >= 0:
type = IntType()
else:
type = UnknownType(depth+1)
self.type = type
assert isinstance(self.type, Type)
def ensurePtrType(self):
return self
def ensureFirstClass(self):
return self
def ensureIntPtrOrVector(self):
return self
def __repr__(self):
return str(self.type) + '*'
def setName(self, name):
self.typevar = Int('t_' + name)
self.type.setName('*' + name)
def getSize(self):
if hasattr(self, 'size'):
return self.size
return Int('ptrsize')
def getPointeeType(self):
return self.type
def getUnderlyingType(self):
return self.type
def __eq__(self, other):
if isinstance(other, PtrType):
return self.type == other.type
if isinstance(other, UnknownType):
return other == self
return BoolVal(False)
def fixupTypes(self, types):
self.size = get_ptr_size()
self.type.fixupTypes(types)
def ensureTypeDepth(self, depth):
return BoolVal(False) if depth == 0 else self.type.ensureTypeDepth(depth-1)
def getTypeConstraints(self):
return And(self.typevar == Type.Ptr,
self.type.getTypeConstraints())
################################
class ArrayType(Type):
def __init__(self, elems = None, type = None, depth = 0):
if elems is None:
assert type is None
# limit type nesting to 1 level
if depth >= 0:
type = IntType()
else:
type = UnknownType(depth+1)
elems = Input('#' + mk_unique_id(), IntType(4)) # enough for [1,7]
self.elems = TypeFixedValue(elems, 1, 7)
self.type = type
assert isinstance(self.type, Type)
def __repr__(self):
return '[%s x %s]' % (self.elems, self.type)
def setName(self, name):
self.typevar = Int('t_' + name)
self.type.setName('[' + name + ']')
self.elems.setName(name, 'elems')
def __eq__(self, other):
if isinstance(other, ArrayType):
return And(self.type == other.type, self.elems == other.elems)
if isinstance(other, UnknownType):
return other == self
return BoolVal(False)
def getSize(self):
return self.elems.getValue() * self.type.getSize()
def getUnderlyingType(self):
return self.type
def fixupTypes(self, types):
self.elems.fixupTypes(types)
self.type.fixupTypes(types)
def ensureTypeDepth(self, depth):
return BoolVal(False) if depth == 0 else self.type.ensureTypeDepth(depth-1)
def getTypeConstraints(self):
return And(self.typevar == Type.Array,
self.elems.getTypeConstraints(),
self.type.getTypeConstraints())
################################
class Value:
def __deepcopy__(self, m):
# Disable deep copy.
return self
def getName(self):
return self.name
def getUniqueName(self):
return self.name
def isConst(self):
return False
def setName(self, name):
self.name = name
if hasattr(self, 'type'):
self.type = copy.deepcopy(self.type)
self.type.setName(name)
for attr in dir(self):
a = getattr(self, attr)
if isinstance(a, TypeFixedValue):
a.setName(name, attr)
elif isinstance(a, Type) and attr != 'type':
a = copy.deepcopy(a)
a.setName('%s_%s_%s' % (name, attr, mk_unique_id()))
setattr(self, attr, a)
elif isinstance(a, list):
newa = []
for e in a:
if isinstance(e, Type):
e = copy.deepcopy(e)
e.setName('%s_%s_%s' % (name, attr, mk_unique_id()))
newa += [e]
setattr(self, attr, newa)
def getTypeConstraints(self):
c = []
for attr in dir(self):
a = getattr(self, attr)
if isinstance(a, (Type, Value)):
c += [a.getTypeConstraints()]
elif isinstance(a, list):
for e in a:
if isinstance(e, (Type, Value)):
c += [e.getTypeConstraints()]
return mk_and(c)
def fixupTypes(self, types):
for attr in dir(self):
a = getattr(self, attr)
if isinstance(a, (Type, Value)):
a.fixupTypes(types)
elif isinstance(a, list):
for e in a:
if isinstance(e, (Type, Value)):
e.fixupTypes(types)
def countUsers(self, m):
for attr in dir(self):
a = getattr(self, attr)
if isinstance(a, Value):
name = a.getUniqueName()
m[name] = m.get(name, 0) + 1
################################
class TypeFixedValue(Value):
def __init__(self, v, min, max):
assert isinstance(v, Value)
assert isinstance(v.type, IntType)
self.v = v
self.min = min
self.max = max
def setName(self, name, attr):
self.name = self.v.getName()
self.smtvar = Int('val_%s_%s' % (name, attr))
def getValue(self):
return getattr(self, 'val', self.smtvar)
def getType(self):
return self.v.type
def __repr__(self):
return str(self.v)
def __eq__(self, other):
assert isinstance(other, TypeFixedValue)
return self.smtvar == other.smtvar
def toSMT(self, defined, poison, state, qvars):
return self.val
def getTypeConstraints(self):
c = [self.v.getTypeConstraints()]
if self.v.isConst():
c += [self.smtvar == self.v.val]
if not self.v.type.defined:
c += [self.v.type == self.max.bit_length() + int(self.max >= 0)]
else:
if self.v.type.defined:
mymin = min(self.min, (1 << self.v.type.getSize()) - 1)
mymax = min(self.max, (1 << self.v.type.getSize()) - 1)
else:
mymin = self.min
mymax = self.max
c += [self.smtvar >= mymin, self.smtvar <= mymax]
if not self.v.type.defined:
c += [self.v.type >= self.max.bit_length() + int(self.max >= 0)]
return mk_and(c)
def fixupTypes(self, types):
self.v.fixupTypes(types)
self.val = types.get_interp(self.smtvar).as_long()
################################
class Input(Value):
def __init__(self, name, type):
self.type = type
self.setName(name)
assert isinstance(self.type, Type)
def __repr__(self):
return self.getName()
def toSMT(self, defined, poison, state, qvars):
v = BitVec(self.name, self.type.getSize())
create_mem_if_needed(v, self, state, [])
return v
def register_types(self, manager):
if self.name[0] == 'C':
min = IntType()
else:
min = UnknownType()
manager.register_type(self, self.type, min)
def _ensure_constant(self):
name = self.getName()
if name[0] != 'C':
raise AliveError('Input {0} used in an expression'.format(name))
def get_APInt_or_u64(self, manager):
return self.get_APInt(manager)
def get_APInt(self, manager):
self._ensure_constant()
return manager.get_cexp(self).arr('getValue', [])
def get_Value(self, manager):
assert False
# this should have been called through the manager