Skip to content

Commit fd6b3ce

Browse files
Merge pull request #209 from lefterislazar/rocq-pointers
Rocq Pointers
2 parents dbbfa41 + e42107c commit fd6b3ce

File tree

100 files changed

+1618
-736
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1618
-736
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ hevm_multi_fast=$(filter-out $(hevm_multi_slow), $(hevm_multi_pass))
6060
failing_typing=tests/frontend/pass/dss/vat.act tests/frontend/pass/creation/createMultiple.act tests/frontend/pass/staticstore/staticstore.act
6161

6262

63-
coq-examples = tests/coq/transitions tests/coq/safemath tests/coq/exponent tests/coq/token tests/coq/ERC20-simple tests/coq/ERC20 tests/coq/multi tests/coq/amm
63+
#coq-examples = tests/coq/multi tests/coq/transitions tests/coq/safemath tests/coq/exponent tests/coq/token tests/coq/ERC20-simple tests/coq/ERC20 tests/coq/amm tests/coq/pointers
6464

6565
.PHONY: test-coq $(coq-examples)
6666
test-coq: compiler $(coq-examples)
@@ -129,5 +129,5 @@ tests/hevm/pass/%.act.hevm.pass.fast:
129129
$(eval CONTRACT := $(shell awk '/contract/{ print $$2 }' tests/hevm/pass/$*.sol))
130130
./bin/act hevm --spec tests/hevm/pass/$*.act --sol tests/hevm/pass/$*.sol --solver bitwuzla --smttimeout 100000000
131131

132-
test-ci: test-parse test-type test-invariant test-postcondition test-coq test-hevm
132+
test-ci: test-parse test-type test-invariant test-postcondition test-coq test-hevm-fast
133133
test: test-ci test-cabal

lib/ActLib.v

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@ Definition address := Z.
1111
Record Env : Set :=
1212
{ Callvalue : Z;
1313
Caller : address;
14+
This : address;
1415
Blockhash : Z;
1516
Blocknumber : Z;
1617
Difficulty : Z;
1718
Timestamp : Z;
1819
Gaslimit : Z;
1920
Coinbase : address;
2021
Chainid : Z;
21-
This : address;
2222
Origin : address;
2323
Nonce : Z;
24-
Calldepth : Z }.
24+
Calldepth : Z;
25+
NextAddr : address }.
26+
27+
Definition NextEnv (ENV : Env) : Env :=
28+
{| Callvalue := Callvalue ENV;
29+
Caller := Caller ENV;
30+
This := This ENV;
31+
Blockhash := Blockhash ENV;
32+
Blocknumber := Blocknumber ENV;
33+
Difficulty := Difficulty ENV;
34+
Timestamp := Timestamp ENV;
35+
Gaslimit := Gaslimit ENV;
36+
Coinbase := Coinbase ENV;
37+
Chainid := Chainid ENV;
38+
Origin := Origin ENV;
39+
Nonce := Nonce ENV;
40+
Calldepth := Calldepth ENV;
41+
NextAddr := (NextAddr ENV + 1)%Z |}.
42+
2543

2644
(** * integer bounds *)
2745
Definition UINT_MIN (n : Z) := 0.

src/Act/Bounds.hs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.Type.Equality
1010
import Act.Syntax
1111
import Act.Syntax.TypedExplicit
1212
import Act.Type (globalEnv)
13+
import Debug.Trace
1314

1415

1516
{-|
@@ -30,7 +31,7 @@ addBounds (Act store contracts) = Act store (addBoundsContract <$> contracts)
3031
-- | Adds type bounds for calldata, environment vars, and external storage vars
3132
-- as preconditions
3233
addBoundsConstructor :: Constructor -> Constructor
33-
addBoundsConstructor ctor@(Constructor _ (Interface _ decls) _ pre post invs stateUpdates) =
34+
addBoundsConstructor ctor@(Constructor _ (Interface _ decls) pre post invs stateUpdates) =
3435
ctor { _cpreconditions = pre'
3536
, _cpostconditions = post'
3637
, _invariants = invs' }
@@ -41,7 +42,7 @@ addBoundsConstructor ctor@(Constructor _ (Interface _ decls) _ pre post invs sta
4142
-- The following is sound as values of locations outside local storage
4243
-- already exist as the constructor starts executing,
4344
-- and the constructor cannot modify non-local locations.
44-
<> mkLocationBounds nonlocalLocs
45+
<> mkLocationBounds (toPrestate stateUpdates <$> nonlocalLocs)
4546
invs' = addBoundsInvariant ctor <$> invs
4647
post' = post <> mkStorageBounds stateUpdates Post
4748

@@ -50,15 +51,48 @@ addBoundsConstructor ctor@(Constructor _ (Interface _ decls) _ pre post invs sta
5051
<> concatMap locsFromUpdate stateUpdates
5152
nonlocalLocs = filter (not . isLocalLoc) locs
5253

54+
55+
changeBase :: Location -> StorageUpdate -> Maybe Location
56+
changeBase _ (Update _ (Item _ _ _) e) | isCreate e = Nothing
57+
-- TODO: suppport!
58+
where
59+
isCreate :: Exp a -> Bool
60+
isCreate (Address _ (Create _ _ _)) = True
61+
isCreate (Create _ _ _) = True
62+
isCreate _ = False
63+
changeBase (Loc st SStorage (Item _ vt baseref)) (Update _ (Item _ _ updatedRef) (VarRef _ _ sk' (Item _ _ ru'))) =
64+
Loc st sk' . Item st vt <$> hasBase baseref ru'
65+
where
66+
hasBase :: Ref Storage -> Ref k -> Maybe (Ref k)
67+
hasBase r''@(SVar _ _ _) ru =
68+
if updatedRef == r'' then Just ru else Nothing
69+
hasBase r''@(SArray pn r' vt' ixs) ru =
70+
if updatedRef == r'' then Just ru
71+
else (\_r -> SArray pn _r vt' ixs) <$> hasBase r' ru
72+
hasBase r''@(SMapping pn r' vt' ixs) ru =
73+
if updatedRef == r'' then Just ru
74+
else (\_r -> SMapping pn _r vt' ixs) <$> hasBase r' ru
75+
hasBase r''@(SField pn r' id' id'') ru =
76+
if updatedRef == r'' then Just ru
77+
else (\_r -> SField pn _r id' id'') <$> hasBase r' ru
78+
changeBase _ _ = Nothing
79+
80+
toPrestate :: [StorageUpdate] -> Location -> Location
81+
toPrestate _ loc@(Loc _ SCalldata _) = loc
82+
toPrestate updates loc@(Loc _ SStorage _) =
83+
case mapMaybe (changeBase loc) updates of
84+
[] -> loc
85+
l -> last l
86+
5387
-- | Adds type bounds for calldata, environment vars, and storage vars as preconditions
5488
addBoundsBehaviour :: Behaviour -> Behaviour
55-
addBoundsBehaviour behv@(Behaviour _ _ (Interface _ decls) _ pre cases post stateUpdates ret) =
89+
addBoundsBehaviour behv@(Behaviour _ _ (Interface _ decls) pre cases post stateUpdates ret) =
5690
behv { _preconditions = pre', _postconditions = post' }
5791
where
5892
pre' = nub $ pre
5993
<> mkCallDataBounds decls
6094
<> mkStorageBounds stateUpdates Pre
61-
<> mkLocationBounds locs
95+
<> mkLocationBounds (toPrestate stateUpdates <$> locs)
6296
<> mkEthEnvBounds (ethEnvFromBehaviour behv)
6397
post' = post
6498
<> mkStorageBounds stateUpdates Post
@@ -69,7 +103,7 @@ addBoundsBehaviour behv@(Behaviour _ _ (Interface _ decls) _ pre cases post stat
69103

70104
-- | Adds type bounds for calldata, environment vars, and storage vars
71105
addBoundsInvariant :: Constructor -> Invariant -> Invariant
72-
addBoundsInvariant (Constructor _ (Interface _ decls) _ _ _ _ _) inv@(Invariant _ preconds storagebounds (PredTimed predicate _)) =
106+
addBoundsInvariant (Constructor _ (Interface _ decls) _ _ _ _) inv@(Invariant _ preconds storagebounds (PredTimed predicate _)) =
73107
inv { _ipreconditions = preconds', _istoragebounds = storagebounds' }
74108
where
75109
preconds' = nub $ preconds
@@ -139,9 +173,12 @@ mkLocationBounds refs = concatMap mkBound refs
139173
mkItemBounds SCalldata = mkCItemBounds
140174

141175
mkCallDataBounds :: [Decl] -> [Exp ABoolean]
142-
mkCallDataBounds = concatMap $ \(Decl typ name) -> case typ of
143-
-- Array element bounds are applied lazily when needed in mkCalldataLocationBounds
144-
(AbiArrayType _ _) -> []
145-
_ -> case fromAbiType typ of
146-
AInteger -> [bound typ (_Var typ name)]
147-
_ -> []
176+
mkCallDataBounds = concatMap $ \(Decl argtyp name) -> case argtyp of
177+
(AbiArg typ) ->
178+
case typ of
179+
-- 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+
_ -> []
184+
(ContractArg _ cid) -> [bound AbiAddressType (Address cid (_Var AbiAddressType name))]

src/Act/Consistency.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mkExhaustiveAssertion caseconds =
7070

7171
-- | Create a query for cases
7272
mkCaseQuery :: ([Exp ABoolean] -> Exp ABoolean) -> [Behaviour] -> (Id, SMTExp, (SolverInstance -> IO Model))
73-
mkCaseQuery props behvs@((Behaviour _ _ (Interface ifaceName decls) _ preconds _ _ _ _):_) =
73+
mkCaseQuery props behvs@((Behaviour _ _ (Interface ifaceName decls) preconds _ _ _ _):_) =
7474
(ifaceName, smt, getModel)
7575
where
7676
locs = nub $ concatMap locsFromExp (preconds <> caseconds)
@@ -120,7 +120,7 @@ checkCases (Act _ contracts) solver' smttimeout debug = do
120120

121121
where
122122

123-
sameIface (Behaviour _ _ iface _ _ _ _ _ _) (Behaviour _ _ iface' _ _ _ _ _ _) =
123+
sameIface (Behaviour _ _ iface _ _ _ _ _) (Behaviour _ _ iface' _ _ _ _ _) =
124124
makeIface iface == makeIface iface'
125125

126126
checkRes :: String -> (Id, SMT.SMTResult) -> IO ()
@@ -154,7 +154,7 @@ 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)
157-
mkConstrArrayBoundsQuery constructor@(Constructor _ (Interface ifaceName decls) _ preconds _ _ initialStorage) =
157+
mkConstrArrayBoundsQuery constructor@(Constructor _ (Interface ifaceName decls) preconds _ _ initialStorage) =
158158
(ifaceName, arrayLocs, smt, getModel)
159159
where
160160
-- Declare vars
@@ -171,7 +171,7 @@ mkConstrArrayBoundsQuery constructor@(Constructor _ (Interface ifaceName decls)
171171
getModel = getCtorModel constructor
172172

173173
mkBehvArrayBoundsQuery :: Behaviour -> (Id, [Location], SMTExp, SolverInstance -> IO Model)
174-
mkBehvArrayBoundsQuery behv@(Behaviour _ _ (Interface ifaceName decls) _ preconds caseconds _ stateUpdates _) =
174+
mkBehvArrayBoundsQuery behv@(Behaviour _ _ (Interface ifaceName decls) preconds caseconds _ stateUpdates _) =
175175
(ifaceName, arrayLocs, smt, getModel)
176176
where
177177
-- Declare vars
@@ -280,7 +280,7 @@ mkAliasingAssertion :: [Location] -> Exp ABoolean
280280
mkAliasingAssertion ls = mkOr $ map (uncurry mkEqualityAssertion) $ combine ls
281281

282282
mkAliasingQuery :: Behaviour -> (Id, [[Location]], SMTExp, SolverInstance -> IO Model)
283-
mkAliasingQuery behv@(Behaviour _ _ (Interface ifaceName decls) _ preconds caseconds _ stateUpdates _) =
283+
mkAliasingQuery behv@(Behaviour _ _ (Interface ifaceName decls) preconds caseconds _ stateUpdates _) =
284284
(ifaceName, groupedLocs, smt, getModel)
285285
where
286286
updatedLocs = locFromUpdate <$> stateUpdates
@@ -391,6 +391,7 @@ modelExpand :: SType (AArray a) -> Exp (AArray a) -> ModelCtx [Exp (Base (AArray
391391
modelExpand (SSArray SInteger) (Array _ l) = pure l
392392
modelExpand (SSArray SBoolean) (Array _ l) = pure l
393393
modelExpand (SSArray SByteStr) (Array _ l) = pure l
394+
modelExpand (SSArray SContract) (Array _ l) = pure l
394395
modelExpand (SSArray s@(SSArray _)) (Array _ l) = concat <$> mapM (modelExpand s) l
395396
modelExpand typ (VarRef _ whn SStorage item) = do
396397
model <- ask

0 commit comments

Comments
 (0)