函数返回基于参数(Z3,Python)的错误值

时间:2015-10-18 13:25:59

标签: z3 smt z3py

我正在使用简单的函数(Tab)模拟一个数组,它并没有按预期工作。如果我在SMT2中编码它,它运行良好,但在Z3py它不起作用。

from z3 import *

A = BitVec('A', 8)
B1 = BitVec('B1', 8)
B2 = BitVec('B2', 8)
B3 = BitVec('B3', 8)
B4 = BitVec('B4', 8)

# Emulate Array
def Tab(N):
  if N == 0x01: return B1
  if N == 0x02: return B2
  if N == 0x03: return B3
  if N == 0x04: return B4

s = Solver()
s.add(A == 0x01)
s.add(Tab(A + 0x02) == 0x09 )
s.check()
m = s.model()

print (m)
print("Pos:", m.eval(A + 0x02))
print("Tab(3a):", m.eval(Tab(A + 0x02)))
print("Tab(3):", m.eval(Tab(0x03)))
print("B1: ", m[B1])
print("B2: ", m[B2])
print("B3: ", m[B3])
print("B4: ", m[B4])
print("B3n:", m.eval(B3))

输出:

[B1 = 9, A = 1] <- Model
Pos: 3          <- this is OK
Tab(3a): 9      <- this is OK (based on our condition)
Tab(3):  B3     <- why does this return name B3? (should return None or 9)
B1:  9          <- this is BAD, here should be None
B2:  None
B3:  None       <- this is BAD, here should be 9
B4:  None
B3n: B3         <- why does this return name B3?! (should return None or 9)

从输出中我们看到Tab总是为参数0x03而不是B3返回B1。看起来A + 0x02被计算为0x01,其中它被用作参数。我做错了什么,还是一些Z3py实现错误?或者这与BitVec术语的工作方式有关吗?

1 个答案:

答案 0 :(得分:0)

这看起来与此帖中的问题相同:How to correctly use Solver() command in Python API of Z3 with declared function

问题是class MyClass(object): def __init__(self, i): self.i = i def __str__(self): return str(self.i) mylist = ['1234567','8910111','1213144','7654321'] inslist = [MyClass(i) for i in mylist] print inslist[0] 没有创建Z3 if-then-else表达式;它确切地检查if N == 0x01: ...是否是具有特定值1的Python-int。要获得所需的表达式,您需要使用Z3的If(...) function