Skip to content

Commit f942f78

Browse files
Merge pull request #210 from lefterislazar/structs
AST refactoring - Singleton simplification
2 parents fd6b3ce + 9273b80 commit f942f78

File tree

14 files changed

+727
-567
lines changed

14 files changed

+727
-567
lines changed

src/Act/Bounds.hs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,50 +122,50 @@ mkEthEnvBounds vars = catMaybes $ mkBound <$> nub vars
122122
where
123123
mkBound :: EthEnv -> Maybe (Exp ABoolean)
124124
mkBound e = case lookup e globalEnv of
125-
Just AInteger -> Just $ bound (toAbiType e) (IntEnv nowhere e)
125+
Just t -> Just $ bound t (IntEnv nowhere e)
126126
_ -> Nothing
127127

128-
toAbiType :: EthEnv -> AbiType
129-
toAbiType env = case env of
130-
Caller -> AbiAddressType
131-
Callvalue -> AbiUIntType 256
132-
Calldepth -> AbiUIntType 10
133-
Origin -> AbiAddressType
134-
Blockhash -> AbiBytesType 32
135-
Blocknumber -> AbiUIntType 256
136-
Difficulty -> AbiUIntType 256
137-
Chainid -> AbiUIntType 256
138-
Gaslimit -> AbiUIntType 256
139-
Coinbase -> AbiAddressType
140-
Timestamp -> AbiUIntType 256
141-
This -> AbiAddressType
142-
Nonce -> AbiUIntType 256
128+
-- toValueType :: EthEnv -> TValueType AInteger
129+
-- toValueType env = case env of
130+
-- Caller -> TAddress
131+
-- Callvalue -> TInteger 256 Unsigned
132+
-- Calldepth -> TInteger 80 Unsigned
133+
-- Origin -> TAddress
134+
-- Blockhash -> TInteger 256 Unsigned --AbiBytesType 32
135+
-- Blocknumber -> TInteger 256 Unsigned
136+
-- Difficulty -> TInteger 256 Unsigned
137+
-- Chainid -> TInteger 256 Unsigned
138+
-- Gaslimit -> TInteger 256 Unsigned
139+
-- Coinbase -> TAddress
140+
-- Timestamp -> TInteger 256 Unsigned
141+
-- This -> TAddress
142+
-- Nonce -> TInteger 256 Unsigned
143143

144144
-- | Extracts bounds from the AbiTypes of Integer variables in storage
145145
mkStorageBounds :: [StorageUpdate] -> When -> [Exp ABoolean]
146146
mkStorageBounds refs t = concatMap mkBound refs
147147
where
148148
mkBound :: StorageUpdate -> [Exp ABoolean]
149-
mkBound (Update SInteger item _) = [mkSItemBounds t item]
150-
mkBound (Update typ item@(Item _ (PrimitiveType at) _) _) | isJust $ flattenArrayAbiType at =
151-
maybe [] (\Refl -> mkSItemBounds t <$> expandItem item) $ testEquality (flattenSType typ) SInteger
149+
mkBound (Update (TInteger _ _) item _) = [mkSItemBounds t item]
150+
mkBound (Update TAddress item _) = [mkSItemBounds t item]
151+
mkBound (Update typ item@(Item (TArray _ _) _) _) =
152+
maybe [] (\Refl -> mkSItemBounds t <$> expandItem item) $ testEquality (toSType $ fst $ flattenValueType typ) SInteger
152153
mkBound _ = []
153154

154155
mkSItemBounds :: When -> TItem AInteger Storage -> Exp ABoolean
155-
mkSItemBounds whn item@(Item _ (PrimitiveType vt) _) = bound vt (VarRef nowhere whn SStorage item)
156-
mkSItemBounds _ (Item _ (ContractType _) _) = LitBool nowhere True
156+
mkSItemBounds whn item@(Item vt _) = bound vt (VarRef nowhere whn SStorage item)
157157

158158
mkCItemBounds :: TItem AInteger Calldata -> Exp ABoolean
159-
mkCItemBounds item@(Item _ (PrimitiveType vt) _) = bound vt (VarRef nowhere Pre SCalldata item)
160-
mkCItemBounds (Item _ (ContractType _) _) = LitBool nowhere True
159+
mkCItemBounds item@(Item vt _) = bound vt (VarRef nowhere Pre SCalldata item)
161160

162161
mkLocationBounds :: [Location] -> [Exp ABoolean]
163162
mkLocationBounds refs = concatMap mkBound refs
164163
where
165164
mkBound :: Location -> [Exp ABoolean]
166-
mkBound (Loc SInteger rk item) = [mkItemBounds rk item]
167-
mkBound (Loc typ rk item@(Item _ (PrimitiveType at) _)) | isJust $ flattenArrayAbiType at =
168-
maybe [] (\Refl -> mkItemBounds rk <$> expandItem item) $ testEquality (flattenSType typ) SInteger
165+
mkBound (Loc (TInteger _ _) rk item) = [mkItemBounds rk item]
166+
mkBound (Loc TAddress rk item) = [mkItemBounds rk item]
167+
mkBound (Loc typ rk item@(Item (TArray _ _) _)) =
168+
maybe [] (\Refl -> mkItemBounds rk <$> expandItem item) $ testEquality (toSType $ fst $ flattenValueType typ) SInteger
169169
mkBound _ = []
170170

171171
mkItemBounds :: SRefKind k -> TItem AInteger k -> Exp ABoolean
@@ -174,11 +174,11 @@ mkLocationBounds refs = concatMap mkBound refs
174174

175175
mkCallDataBounds :: [Decl] -> [Exp ABoolean]
176176
mkCallDataBounds = concatMap $ \(Decl argtyp name) -> case argtyp of
177-
(AbiArg typ) ->
178-
case typ of
177+
(AbiArg typ) -> case typ of
179178
-- Array element bounds are applied lazily when needed in mkCalldataLocationBounds
180-
(AbiArrayType _ _) -> []
181-
_ -> case fromAbiType typ of
182-
AInteger -> [bound typ (_Var typ name)]
183-
_ -> []
179+
(AbiArrayType _ _) -> []
180+
_ -> case fromAbiType' typ of
181+
ValueType t@(TInteger _ _) -> [bound t (_Var t name)]
182+
ValueType TAddress -> [bound TAddress (_Var TAddress name)]
183+
_ -> []
184184
(ContractArg _ cid) -> [bound AbiAddressType (Address cid (_Var AbiAddressType name))]

src/Act/Consistency.hs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ checkCases (Act _ contracts) solver' smttimeout debug = do
140140
type ModelCtx = Reader Model
141141

142142
mkBounds :: TypedExp -> Int -> [Exp ABoolean]
143-
mkBounds (TExp SInteger _ e) b = [LEQ nowhere (LitInt nowhere 0) e, LT nowhere e (LitInt nowhere $ toInteger b)]
143+
mkBounds (TExp (TInteger _ _) e) b = [LEQ nowhere (LitInt nowhere 0) e, LT nowhere e (LitInt nowhere $ toInteger b)]
144144
mkBounds _ _ = error "Internal Error: Expected Integral Index"
145145

146146
mkRefBounds :: Ref a -> [Exp ABoolean]
@@ -150,7 +150,7 @@ mkRefBounds (SField _ ref _ _) = mkRefBounds ref
150150
mkRefBounds _ = []
151151

152152
mkStorageBounds :: Location -> [Exp ABoolean]
153-
mkStorageBounds (Loc _ _ (Item _ _ ref)) = mkRefBounds ref
153+
mkStorageBounds (Loc _ _ (Item _ ref)) = mkRefBounds ref
154154

155155
-- TODO: There are locs that don't need to be checked, e.g. assignment locs cannot be out of bounds
156156
mkConstrArrayBoundsQuery :: Constructor -> (Id, [Location], SMTExp, SolverInstance -> IO Model)
@@ -224,7 +224,7 @@ checkArrayBounds (Act _ contracts) solver' smttimeout debug =
224224
errorMsg str = render (pretty str <> line) >> exitFailure
225225

226226
checkBound :: TypedExp -> Int -> ModelCtx Bool
227-
checkBound (TExp SInteger _ e) b =
227+
checkBound (TExp (TInteger _ _) e) b =
228228
[ (0 <= toInteger idx) && (toInteger idx < toInteger b) | idx <- modelEval e ]
229229
checkBound _ _ = error "Internal Error: Expected Integer indices"
230230

@@ -235,7 +235,7 @@ checkRefBounds (SField _ ref _ _) = checkRefBounds ref
235235
checkRefBounds _ = pure True
236236

237237
checkLocationBounds :: Location -> ModelCtx DocAnsi
238-
checkLocationBounds (Loc _ _ item@(Item _ _ ref)) = do
238+
checkLocationBounds (Loc _ _ item@(Item _ ref)) = do
239239
cond <- checkRefBounds ref
240240
if cond then pure $ string ""
241241
else do
@@ -245,7 +245,7 @@ checkLocationBounds (Loc _ _ item@(Item _ _ ref)) = do
245245
(AlexPn _ l c) = posnFromRef ref
246246

247247
printIdx :: TypedExp -> Int -> ModelCtx DocAnsi
248-
printIdx te@(TExp SInteger _ e) b = do
248+
printIdx te@(TExp (TInteger _ _) e) b = do
249249
idx <- modelEval e
250250
if (toInteger idx < toInteger b) && (0 <= toInteger idx)
251251
then pure $ string "[" <> string (prettyTypedExp te) <> string "]"
@@ -271,7 +271,7 @@ printOutOfBoundsRef (SVar _ _ id') = pure $ string id'
271271
printOutOfBoundsRef (CVar _ _ id') = pure $ string id'
272272

273273
printOutOfBoundsItem :: TItem a k-> ModelCtx DocAnsi
274-
printOutOfBoundsItem (Item _ _ ref) = printOutOfBoundsRef ref
274+
printOutOfBoundsItem (Item _ ref) = printOutOfBoundsRef ref
275275

276276

277277
--- ** No rewrite aliasing ** ---
@@ -356,16 +356,16 @@ checkAliasing (l1, l2) = do
356356
ixs2 = ixsFromLocation l2
357357

358358
compareIdx :: TypedExp -> TypedExp -> ModelCtx Bool
359-
compareIdx (TExp SInteger Atomic e1) (TExp SInteger Atomic e2) =
359+
compareIdx (TExp (TInteger _ _) e1) (TExp (TInteger _ _) e2) =
360360
[ a == b | a <- modelEval e1, b <- modelEval e2 ]
361-
compareIdx (TExp SBoolean Atomic e1) (TExp SBoolean Atomic e2) =
361+
compareIdx (TExp TBoolean e1) (TExp TBoolean e2) =
362362
[ a == b | a <- modelEval e1, b <- modelEval e2 ]
363-
compareIdx (TExp SByteStr Atomic e1) (TExp SByteStr Atomic e2) =
363+
compareIdx (TExp TByteStr e1) (TExp TByteStr e2) =
364364
[ a == b | a <- modelEval e1, b <- modelEval e2 ]
365365
compareIdx _ _ = pure False
366366

367367
printAliased :: TypedExp -> ModelCtx DocAnsi
368-
printAliased te@(TExp SInteger _ e) = do
368+
printAliased te@(TExp (TInteger _ _) e) = do
369369
e' <- modelEval e
370370
pure $ string "[(" <> string (prettyTypedExp te) <> string ") = " <> string (show e') <> string "]"
371371
printAliased _ = error "Internal Error: Expected Integer indices"
@@ -381,29 +381,31 @@ printAliasedRef (SVar _ _ id') = pure $ string id'
381381
printAliasedRef (CVar _ _ id') = pure $ string id'
382382

383383
printAliasedLoc :: Location -> ModelCtx DocAnsi
384-
printAliasedLoc (Loc _ _ (Item _ _ ref)) = do
384+
printAliasedLoc (Loc _ _ (Item _ ref)) = do
385385
r <- printAliasedRef ref
386386
pure $ string "Line " <> string (show l) <> string " Column " <> string (show c) <> string ": " <> r
387387
where
388388
(AlexPn _ l c ) = posnFromRef ref
389389

390-
modelExpand :: SType (AArray a) -> Exp (AArray a) -> ModelCtx [Exp (Base (AArray a))]
391-
modelExpand (SSArray SInteger) (Array _ l) = pure l
392-
modelExpand (SSArray SBoolean) (Array _ l) = pure l
393-
modelExpand (SSArray SByteStr) (Array _ l) = pure l
394-
modelExpand (SSArray SContract) (Array _ l) = pure l
395-
modelExpand (SSArray s@(SSArray _)) (Array _ l) = concat <$> mapM (modelExpand s) l
390+
modelExpand :: TValueType (AArray a) -> Exp (AArray a) -> ModelCtx [Exp (Base (AArray a))]
391+
modelExpand (TArray _ (TInteger _ _)) (Array _ l) = pure l
392+
modelExpand (TArray _ TAddress) (Array _ l) = pure l
393+
modelExpand (TArray _ TBoolean) (Array _ l) = pure l
394+
modelExpand (TArray _ TByteStr) (Array _ l) = pure l
395+
modelExpand (TArray _ (TContract _)) (Array _ l) = pure l
396+
modelExpand (TArray _ (TStruct _)) (Array _ l) = pure l
397+
modelExpand (TArray _ t@(TArray _ _)) (Array _ l) = concat <$> mapM (modelExpand t) l
396398
modelExpand typ (VarRef _ whn SStorage item) = do
397399
model <- ask
398400
case lookup (_Loc SStorage item) $ if whn == Pre then _mprestate model else _mpoststate model of
399-
Just (TExp sType _ e') -> case testEquality typ sType of
401+
Just (TExp sType e') -> case testEquality typ sType of
400402
Just Refl -> pure $ expandArrayExpr sType e'
401403
_ -> error "modelEval: Storage Location given does not match type"
402404
_ -> error $ "modelEval: Storage Location not found in model" <> show item
403405
modelExpand typ (VarRef _ _ SCalldata item) = do
404406
model <- ask
405407
case lookup (_Loc SCalldata item) $ _mcalllocs model of
406-
Just (TExp sType _ e') -> case testEquality typ sType of
408+
Just (TExp sType e') -> case testEquality typ sType of
407409
Just Refl -> pure $ expandArrayExpr sType e'
408410
_ -> error "modelEval: Storage Location given does not match type"
409411
_ -> error $ "modelEval: Storage Location not found in model" <> show item
@@ -436,23 +438,30 @@ modelEval e = case e of
436438
UIntMin _ a -> pure $ uintmin a
437439
UIntMax _ a -> pure $ uintmax a
438440

439-
Eq _ SInteger x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
440-
Eq _ SBoolean x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
441-
Eq _ SByteStr x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
442-
Eq p s@(SSArray _) a b -> do
441+
Eq _ (TInteger _ _) x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
442+
Eq _ TAddress x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
443+
Eq _ TBoolean x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
444+
Eq _ TByteStr x y -> [ x' == y' | x' <- modelEval x, y' <- modelEval y]
445+
Eq _ (TStruct _) _ _ -> error "modelEval: TODO: Structs"
446+
Eq _ (TContract _) _ _ -> error "modelEval: TODO: Structs"
447+
Eq p s@(TArray _ _) a b -> do
443448
a' <- modelExpand s a
444449
b' <- modelExpand s b
445-
let s' = flattenSType s
450+
let s' = fst $ flattenValueType s
446451
eqs = (uncurry $ Eq p s') <$> zip a' b'
447452
expanded = foldr (And nowhere) (LitBool nowhere True) eqs
448453
modelEval expanded
449-
NEq _ SInteger x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
450-
NEq _ SBoolean x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
451-
NEq _ SByteStr x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
452-
NEq p s@(SSArray _) a b -> do
454+
455+
NEq _ (TInteger _ _) x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
456+
NEq _ TAddress x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
457+
NEq _ TBoolean x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
458+
NEq _ TByteStr x y -> [ x' /= y' | x' <- modelEval x, y' <- modelEval y]
459+
NEq _ (TStruct _) _ _ -> error "modelEval: TODO: Structs"
460+
NEq _ (TContract _) _ _ -> error "modelEval: TODO: Structs"
461+
NEq p s@(TArray _ _) a b -> do
453462
a' <- modelExpand s a
454463
b' <- modelExpand s b
455-
let s' = flattenSType s
464+
let s' = fst $ flattenValueType s
456465
eqs = (uncurry $ NEq p s') <$> zip a' b'
457466
expanded = foldr (Or nowhere) (LitBool nowhere False) eqs
458467
modelEval expanded
@@ -463,10 +472,10 @@ modelEval e = case e of
463472
SSArray SType -> mapM modelEval l
464473

465474
Create _ _ _ -> error "modelEval of contracts not supported"
466-
VarRef _ whn SStorage item -> do
475+
VarRef _ whn SStorage item@(Item vt _) -> do
467476
model <- ask
468477
case lookup (_Loc SStorage item) $ if whn == Pre then _mprestate model else _mpoststate model of
469-
Just (TExp sType _ e') -> case testEquality (sing @a) sType of
478+
Just (TExp vType e') -> case testEquality vt vType of
470479
Just Refl -> case e' of
471480
(LitInt _ i) -> pure i
472481
(LitBool _ b) -> pure b
@@ -475,10 +484,10 @@ modelEval e = case e of
475484
_ -> error "modelEval: Model did not return a literal"
476485
_ -> error "modelEval: Storage Location given does not match type"
477486
_ -> error $ "modelEval: Storage Location not found in model" <> show item
478-
VarRef _ _ SCalldata item -> do
487+
VarRef _ _ SCalldata item@(Item vt _) -> do
479488
model <- ask
480489
case lookup (_Loc SCalldata item) $ _mcalllocs model of
481-
Just (TExp sType _ e') -> case testEquality (sing @a) sType of
490+
Just (TExp vType e') -> case testEquality vt vType of
482491
Just Refl -> case e' of
483492
(LitInt _ i) -> pure i
484493
(LitBool _ b) -> pure b
@@ -491,7 +500,7 @@ modelEval e = case e of
491500
IntEnv _ env -> do
492501
model <- ask
493502
case lookup env $ _menvironment model of
494-
Just (TExp sType _ e') -> case testEquality (sing @a) sType of
503+
Just (TExp sType e') -> case testEquality (sing @a) (toSType sType) of
495504
Just Refl -> case e' of
496505
(LitInt _ i) -> pure i
497506
_ -> error "modelEval: Model did not return an Integer literal"

0 commit comments

Comments
 (0)