Skip to content

Commit 985751a

Browse files
committed
Fixing bug #664
1 parent 54eb53a commit 985751a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/EVM/SMT.hs

+9-1
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,20 @@ discoverMaxReads props benv senv = bufMap
420420

421421
-- | Returns an SMT2 object with all buffers referenced from the input props declared, and with the appropriate cex extraction metadata attached
422422
declareBufs :: [Prop] -> BufEnv -> StoreEnv -> SMT2
423-
declareBufs props bufEnv storeEnv = SMT2 ("; buffers" : fmap declareBuf allBufs <> ("; buffer lengths" : fmap declareLength allBufs)) cexvars mempty
423+
declareBufs props bufEnv storeEnv =
424+
SMT2 (smtBufNames <> smtBufLengths <> smtEmptyRelations) cexvars mempty
424425
where
426+
smtBufNames = "; buffers" : fmap declareBuf allBufs
427+
smtBufLengths = "; buffer lengths" : fmap declareLength allBufs
428+
smtEmptyRelations = "; empty buffer relations" : concatMap emptyRelation allBufs
425429
cexvars = (mempty :: CexVars){ buffers = discoverMaxReads props bufEnv storeEnv }
426430
allBufs = fmap fromLazyText $ Map.keys cexvars.buffers
427431
declareBuf n = "(declare-fun " <> n <> " () (Array (_ BitVec 256) (_ BitVec 8)))"
428432
declareLength n = "(declare-fun " <> n <> "_length" <> " () (_ BitVec 256))"
433+
emptyRelation buf =
434+
let bufLen = buf <> "_length"
435+
in ["(assert (=> (= " <> bufLen <> " (_ bv0 256)) (= " <> buf <> " ((as const Buf) #b00000000)) ))"
436+
, "(assert (=> (= " <> buf <> " ((as const Buf) #b00000000)) (= " <> bufLen <> " (_ bv0 256)) ))" ]
429437

430438
-- Given a list of variable names, create an SMT2 object with the variables declared
431439
declareVars :: [Builder] -> SMT2

test/test.hs

+7
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ tests = testGroup "hevm"
515515
let e = BufLength (CopySlice (Lit 0x2) (Lit 0x2) (Lit 0x1) (ConcreteBuf "") (ConcreteBuf ""))
516516
b <- checkEquiv e (Expr.simplify e)
517517
assertBoolM "Simplifier failed" b
518+
, test "simp-empty-buflength" $ do
519+
let e = PEq (BufLength (AbstractBuf "mybuf")) (Lit 0)
520+
let simp = Expr.simplifyProp e
521+
let simpExpected = PEq (AbstractBuf "mybuf") (ConcreteBuf "")
522+
assertEqualM "buflen-to-empty" simp simpExpected
523+
ret <- checkEquivPropAndLHS e simpExpected
524+
assertBoolM "Must be equivalent" ret
518525
, test "simp-max-buflength" $ do
519526
let simp = Expr.simplify $ Max (Lit 0) (BufLength (AbstractBuf "txdata"))
520527
assertEqualM "max-buflength rules" simp $ BufLength (AbstractBuf "txdata")

0 commit comments

Comments
 (0)