-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtest.hs
5045 lines (4851 loc) · 217 KB
/
test.hs
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Prelude hiding (LT, GT)
import GHC.TypeLits
import Data.Proxy
import Control.Monad
import Control.Monad.ST (RealWorld, stToIO)
import Control.Monad.State.Strict
import Control.Monad.IO.Unlift
import Control.Monad.Reader (ReaderT)
import Data.Bits hiding (And, Xor)
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.ByteString.Base16 qualified as BS16
import Data.Binary.Put (runPut)
import Data.Binary.Get (runGetOrFail)
import Data.DoubleWord
import Data.Either
import Data.List qualified as List
import Data.Map.Strict qualified as Map
import Data.Maybe
import Data.String.Here
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.IO qualified as T
import Data.Time (diffUTCTime, getCurrentTime)
import Data.Tuple.Extra
import Data.Tree (flatten)
import Data.Typeable
import Data.Vector qualified as V
import Data.Word (Word8, Word64)
import Data.Char (digitToInt)
import GHC.Conc (getNumProcessors)
import System.Directory
import System.Environment
import Test.Tasty
import Test.Tasty.QuickCheck hiding (Failure, Success)
import Test.QuickCheck.Instances.Text()
import Test.QuickCheck.Instances.Natural()
import Test.QuickCheck.Instances.ByteString()
import Test.Tasty.HUnit
import Test.Tasty.Runners hiding (Failure, Success)
import Test.Tasty.ExpectedFailure
import Text.RE.TDFA.String
import Text.RE.Replace
import Witch (unsafeInto, into)
import Data.Containers.ListUtils (nubOrd)
import Optics.Core hiding (pre, re, elements)
import Optics.State
import EVM
import EVM.ABI
import EVM.Assembler
import EVM.Exec
import EVM.Expr qualified as Expr
import EVM.Fetch qualified as Fetch
import EVM.Format (hexText, formatExpr)
import EVM.Precompiled
import EVM.RLP
import EVM.SMT hiding (one)
import EVM.Solidity
import EVM.Solvers
import EVM.Stepper qualified as Stepper
import EVM.SymExec
import EVM.Test.Tracing qualified as Tracing
import EVM.Test.Utils
import EVM.Traversals
import EVM.Types hiding (Env)
import EVM.Effects
import EVM.UnitTest (writeTrace)
import EVM.Expr (maybeLitByteSimp)
testEnv :: Env
testEnv = Env { config = defaultConfig {
dumpQueries = False
, dumpExprs = False
, dumpEndStates = False
, debug = False
, dumpTrace = False
, decomposeStorage = True
} }
putStrLnM :: (MonadUnliftIO m) => String -> m ()
putStrLnM a = liftIO $ putStrLn a
assertEqualM :: (App m, Eq a, Show a, HasCallStack) => String -> a -> a -> m ()
assertEqualM a b c = liftIO $ assertEqual a b c
assertBoolM
:: (MonadUnliftIO m, HasCallStack)
=> String -> Bool -> m ()
assertBoolM a b = liftIO $ assertBool a b
test :: TestName -> ReaderT Env IO () -> TestTree
test a b = testCase a $ runEnv testEnv b
testFuzz :: TestName -> ReaderT Env IO () -> TestTree
testFuzz a b = testCase a $ runEnv (testEnv {config = testEnv.config {numCexFuzz = 100, onlyCexFuzz = True}}) b
prop :: Testable prop => ReaderT Env IO prop -> Property
prop a = ioProperty $ runEnv testEnv a
withDefaultSolver :: App m => (SolverGroup -> m a) -> m a
withDefaultSolver = withSolvers Z3 3 1 Nothing
withCVC5Solver :: App m => (SolverGroup -> m a) -> m a
withCVC5Solver = withSolvers CVC5 3 1 Nothing
main :: IO ()
main = defaultMain tests
-- | run a subset of tests in the repl. p is a tasty pattern:
-- https://github.com/UnkindPartition/tasty/tree/ee6fe7136fbcc6312da51d7f1b396e1a2d16b98a#patterns
runSubSet :: String -> IO ()
runSubSet p = defaultMain . applyPattern p $ tests
tests :: TestTree
tests = testGroup "hevm"
[ Tracing.tests
, testGroup "simplify-storage"
[ test "simplify-storage-array-only-static" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] a;
function transfer(uint acct, uint val1, uint val2) public {
unchecked {
a[0] = val1 + 1;
a[1] = val2 + 2;
assert(a[0]+a[1] == val1 + val2 + 3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
-- This case is somewhat artificial. We can't simplify this using only
-- static rewrite rules, because acct is totally abstract and acct + 1
-- could overflow back to zero. we may be able to do better if we have some
-- smt assisted simplification that can take branch conditions into account.
, expectFail $ test "simplify-storage-array-symbolic-index" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint b;
uint[] a;
function transfer(uint acct, uint val1) public {
unchecked {
a[acct] = val1;
assert(a[acct] == val1);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
-- T.writeFile "symbolic-index.expr" $ formatExpr expr
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, expectFail $ test "simplify-storage-array-of-struct-symbolic-index" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
struct MyStruct {
uint a;
uint b;
}
MyStruct[] arr;
function transfer(uint acct, uint val1, uint val2) public {
unchecked {
arr[acct].a = val1+1;
arr[acct].b = val1+2;
assert(arr[acct].a + arr[acct].b == val1+val2+3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-array-loop-nonstruct" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] a;
function transfer(uint v) public {
for (uint i = 0; i < a.length; i++) {
a[i] = v;
assert(a[i] == v);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256)" [AbiUIntType 256])) [] (defaultVeriOpts { maxIter = Just 5 })
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-map-newtest1" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping (uint => uint) a;
mapping (uint => uint) b;
function fun(uint v, uint i) public {
require(i < 1000);
require(v < 1000);
b[i+v] = v+1;
a[i] = v;
b[i+1] = v+1;
assert(a[i] == v);
assert(b[i+1] == v+1);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "fun(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
(_, [(Qed _)]) <- withDefaultSolver $ \s -> checkAssert s [0x11] c (Just (Sig "fun(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
liftIO $ putStrLn "OK"
, ignoreTest $ test "simplify-storage-map-todo" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping (uint => uint) a;
mapping (uint => uint) b;
function fun(uint v, uint i) public {
require(i < 1000);
require(v < 1000);
a[i] = v;
b[i+1] = v+1;
b[i+v] = 55; // note: this can overwrite b[i+1], hence assert below can fail
assert(a[i] == v);
assert(b[i+1] == v+1);
}
}
|]
-- TODO: expression below contains (load idx1 (store idx1 (store idx1 (store idx0)))), and the idx0
-- is not stripped. This is due to us not doing all we can in this case, see
-- note above readStorage. Decompose remedies this (when it can be decomposed)
-- expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "fun(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
-- putStrLnM $ T.unpack $ formatExpr expr
(_, [Cex _]) <- withDefaultSolver $ \s -> checkAssert s defaultPanicCodes c (Just (Sig "fun(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
liftIO $ putStrLn "OK"
, test "simplify-storage-array-loop-struct" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
struct MyStruct {
uint a;
uint b;
}
MyStruct[] arr;
function transfer(uint v1, uint v2) public {
for (uint i = 0; i < arr.length; i++) {
arr[i].a = v1+1;
arr[i].b = v2+2;
assert(arr[i].a + arr[i].b == v1 + v2 + 3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256)" [AbiUIntType 256, AbiUIntType 256])) [] (defaultVeriOpts { maxIter = Just 5 })
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "decompose-1" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping (address => uint) balances;
function prove_mapping_access(address x, address y) public {
require(x != y);
balances[x] = 1;
balances[y] = 2;
assert(balances[x] != balances[y]);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_mapping_access(address,address)" [AbiAddressType, AbiAddressType])) [] defaultVeriOpts
putStrLnM $ T.unpack $ formatExpr expr
let simpExpr = mapExprM Expr.decomposeStorage expr
-- putStrLnM $ T.unpack $ formatExpr (fromJust simpExpr)
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
, test "decompose-2" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping (address => uint) balances;
function prove_mixed_symoblic_concrete_writes(address x, uint v) public {
balances[x] = v;
balances[address(0)] = balances[x];
assert(balances[address(0)] == v);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_mixed_symoblic_concrete_writes(address,uint256)" [AbiAddressType, AbiUIntType 256])) [] defaultVeriOpts
let simpExpr = mapExprM Expr.decomposeStorage expr
-- putStrLnM $ T.unpack $ formatExpr (fromJust simpExpr)
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
, test "decompose-3" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] a;
function prove_array(uint x, uint v1, uint y, uint v2) public {
require(v1 != v2);
a[x] = v1;
a[y] = v2;
assert(a[x] == a[y]);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_array(uint256,uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
let simpExpr = mapExprM Expr.decomposeStorage expr
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
, test "decompose-4-mixed" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] a;
mapping( uint => uint) balances;
function prove_array(uint x, uint v1, uint y, uint v2) public {
require(v1 != v2);
balances[x] = v1+1;
balances[y] = v1+2;
a[x] = v1;
assert(balances[x] != balances[y]);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_array(uint256,uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
let simpExpr = mapExprM Expr.decomposeStorage expr
-- putStrLnM $ T.unpack $ formatExpr (fromJust simpExpr)
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
, test "decompose-5-mixed" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping (address => uint) balances;
mapping (uint => bool) auth;
uint[] arr;
uint a;
uint b;
function prove_mixed(address x, address y, uint val) public {
b = val+1;
require(x != y);
balances[x] = val;
a = val;
arr[val] = 5;
auth[val+1] = true;
balances[y] = val+2;
if (balances[y] == balances[y]) {
assert(balances[y] == val);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_mixed(address,address,uint256)" [AbiAddressType, AbiAddressType, AbiUIntType 256])) [] defaultVeriOpts
let simpExpr = mapExprM Expr.decomposeStorage expr
-- putStrLnM $ T.unpack $ formatExpr (fromJust simpExpr)
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
-- TODO check what's going on here. Likely the "arbitrary write through array" is the reason why we fail
, expectFail $ test "decompose-6-fail" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] arr;
function prove_mixed(uint val) public {
arr[val] = 5;
arr[val+1] = val+5;
assert(arr[val] == arr[val+1]);
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "prove_mixed(uint256)" [AbiUIntType 256])) [] defaultVeriOpts
let simpExpr = mapExprM Expr.decomposeStorage expr
-- putStrLnM $ T.unpack $ formatExpr (fromJust simpExpr)
assertEqualM "Decompose did not succeed." (isJust simpExpr) True
, test "simplify-storage-map-only-static" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping(uint => uint) items1;
function transfer(uint acct, uint val1, uint val2) public {
unchecked {
items1[0] = val1+1;
items1[1] = val2+2;
assert(items1[0]+items1[1] == val1 + val2 + 3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-map-only-2" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
mapping(uint => uint) items1;
function transfer(uint acct, uint val1, uint val2) public {
unchecked {
items1[acct] = val1+1;
items1[acct+1] = val2+2;
assert(items1[acct]+items1[acct+1] == val1 + val2 + 3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
-- putStrLnM $ T.unpack $ formatExpr expr
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-map-with-struct" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
struct MyStruct {
uint a;
uint b;
}
mapping(uint => MyStruct) items1;
function transfer(uint acct, uint val1, uint val2) public {
unchecked {
items1[acct].a = val1+1;
items1[acct].b = val2+2;
assert(items1[acct].a+items1[acct].b == val1 + val2 + 3);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-map-and-array" $ do
Just c <- solcRuntime "MyContract"
[i|
contract MyContract {
uint[] a;
mapping(uint => uint) items1;
mapping(uint => uint) items2;
function transfer(uint acct, uint val1, uint val2) public {
uint beforeVal1 = items1[acct];
uint beforeVal2 = items2[acct];
unchecked {
items1[acct] = val1+1;
items2[acct] = val2+2;
a[0] = val1 + val2 + 1;
a[1] = val1 + val2 + 2;
assert(items1[acct]+items2[acct]+a[0]+a[1] > beforeVal1 + beforeVal2);
}
}
}
|]
expr <- withDefaultSolver $ \s -> getExpr s c (Just (Sig "transfer(uint256,uint256,uint256)" [AbiUIntType 256, AbiUIntType 256, AbiUIntType 256])) [] defaultVeriOpts
-- putStrLnM $ T.unpack $ formatExpr expr
assertEqualM "Expression is not clean." (badStoresInExpr expr) False
, test "simplify-storage-wordToAddr" $ do
let a = "000000000000000000000000d95322745865822719164b1fc167930754c248de000000000000000000000000000000000000000000000000000000000000004a"
store = ConcreteStore (Map.fromList[(W256 0xebd33f63ba5dda53a45af725baed5628cdad261db5319da5f5d921521fe1161d,W256 0x5842cf)])
expr = SLoad (Keccak (ConcreteBuf (hexStringToByteString a))) store
simpExpr = Expr.wordToAddr expr
simpExpr2 = Expr.concKeccakSimpExpr expr
assertEqualM "Expression should simplify to value." simpExpr (Just $ LitAddr 0x5842cf)
assertEqualM "Expression should simplify to value." simpExpr2 (Lit 0x5842cf)
, test "simplify-storage-wordToAddr-complex" $ do
let a = "000000000000000000000000d95322745865822719164b1fc167930754c248de000000000000000000000000000000000000000000000000000000000000004a"
store = ConcreteStore (Map.fromList[(W256 0xebd33f63ba5dda53a45af725baed5628cdad261db5319da5f5d921521fe1161d,W256 0x5842cf)])
expr = SLoad (Keccak (ConcreteBuf (hexStringToByteString a))) store
writeWChain = WriteWord (Lit 0x32) (Lit 0x72) (WriteWord (Lit 0x0) expr (ConcreteBuf ""))
kecc = Keccak (CopySlice (Lit 0x0) (Lit 0x0) (Lit 0x20) (WriteWord (Lit 0x0) expr (writeWChain)) (ConcreteBuf ""))
keccAnd = (And (Lit 1461501637330902918203684832716283019655932542975) kecc)
outer = And (Lit 1461501637330902918203684832716283019655932542975) (SLoad (keccAnd) (ConcreteStore (Map.fromList[(W256 1184450375068808042203882151692185743185288360635, W256 0xacab)])))
simp = Expr.concKeccakSimpExpr outer
assertEqualM "Expression should simplify to value." simp (Lit 0xacab)
]
, testGroup "StorageTests"
[ test "read-from-sstore" $ assertEqualM ""
(Lit 0xab)
(Expr.readStorage' (Lit 0x0) (SStore (Lit 0x0) (Lit 0xab) (AbstractStore (LitAddr 0x0) Nothing)))
, test "read-from-concrete" $ assertEqualM ""
(Lit 0xab)
(Expr.readStorage' (Lit 0x0) (ConcreteStore $ Map.fromList [(0x0, 0xab)]))
, test "read-past-write" $ assertEqualM ""
(Lit 0xab)
(Expr.readStorage' (Lit 0x0) (SStore (Lit 0x1) (Var "b") (ConcreteStore $ Map.fromList [(0x0, 0xab)])))
, test "accessStorage uses fetchedStorage" $ do
let dummyContract =
(initialContract (RuntimeCode (ConcreteRuntimeCode mempty)))
{ external = True }
vm :: VM Concrete RealWorld <- liftIO $ stToIO $ vmForEthrunCreation ""
-- perform the initial access
vm1 <- liftIO $ stToIO $ execStateT (EVM.accessStorage (LitAddr 0) (Lit 0) (pure . pure ())) vm
-- it should fetch the contract first
vm2 <- case vm1.result of
Just (HandleEffect (Query (PleaseFetchContract _addr _ continue))) ->
liftIO $ stToIO $ execStateT (continue dummyContract) vm1
_ -> internalError "unexpected result"
-- then it should fetch the slow
vm3 <- case vm2.result of
Just (HandleEffect (Query (PleaseFetchSlot _addr _slot continue))) ->
liftIO $ stToIO $ execStateT (continue 1337) vm2
_ -> internalError "unexpected result"
-- perform the same access as for vm1
vm4 <- liftIO $ stToIO $ execStateT (EVM.accessStorage (LitAddr 0) (Lit 0) (pure . pure ())) vm3
-- there won't be query now as accessStorage uses fetch cache
assertBoolM (show vm4.result) (isNothing vm4.result)
]
, testGroup "SimplifierUnitTests"
-- common overflow cases that the simplifier was getting wrong
[ test "writeWord-overflow" $ do
let e = ReadByte (Lit 0x0) (WriteWord (Lit 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd) (Lit 0x0) (ConcreteBuf "\255\255\255\255"))
b <- checkEquiv e (Expr.simplify e)
assertBoolM "Simplifier failed" b
, test "buffer-length-copy-slice-beyond-source1" $ do
let e = BufLength (CopySlice (Lit 0x2) (Lit 0x2) (Lit 0x1) (ConcreteBuf "a") (ConcreteBuf ""))
b <- checkEquiv e (Expr.simplify e)
assertBoolM "Simplifier failed" b
, test "buffer-length-copy-slice-beyond-source2" $ do
let e = BufLength (CopySlice (Lit 0x2) (Lit 0x2) (Lit 0x1) (ConcreteBuf "") (ConcreteBuf ""))
b <- checkEquiv e (Expr.simplify e)
assertBoolM "Simplifier failed" b
, test "simp-readByte1" $ do
let srcOffset = (ReadWord (Lit 0x1) (AbstractBuf "stuff1"))
size = (ReadWord (Lit 0x1) (AbstractBuf "stuff2"))
src = (AbstractBuf "stuff2")
e = ReadByte (Lit 0x0) (CopySlice srcOffset (Lit 0x10) size src (AbstractBuf "dst"))
simp = Expr.simplify e
assertEqualM "readByte simplification" simp (ReadByte (Lit 0x0) (AbstractBuf "dst"))
, test "simp-readByte2" $ do
let srcOffset = (ReadWord (Lit 0x1) (AbstractBuf "stuff1"))
size = (Lit 0x1)
src = (AbstractBuf "stuff2")
e = ReadByte (Lit 0x0) (CopySlice srcOffset (Lit 0x10) size src (AbstractBuf "dst"))
simp = Expr.simplify e
res <- checkEquiv e simp
assertEqualM "readByte simplification" res True
, test "simp-readWord1" $ do
let srcOffset = (ReadWord (Lit 0x1) (AbstractBuf "stuff1"))
size = (ReadWord (Lit 0x1) (AbstractBuf "stuff2"))
src = (AbstractBuf "stuff2")
e = ReadWord (Lit 0x1) (CopySlice srcOffset (Lit 0x40) size src (AbstractBuf "dst"))
simp = Expr.simplify e
assertEqualM "readWord simplification" simp (ReadWord (Lit 0x1) (AbstractBuf "dst"))
, test "simp-readWord2" $ do
let srcOffset = (ReadWord (Lit 0x12) (AbstractBuf "stuff1"))
size = (Lit 0x1)
src = (AbstractBuf "stuff2")
e = ReadWord (Lit 0x12) (CopySlice srcOffset (Lit 0x50) size src (AbstractBuf "dst"))
simp = Expr.simplify e
res <- checkEquiv e simp
assertEqualM "readWord simplification" res True
, test "simp-empty-buflength" $ do
let e = PEq (BufLength (AbstractBuf "mybuf")) (Lit 0)
let simp = Expr.simplifyProp e
let simpExpected = PEq (AbstractBuf "mybuf") (ConcreteBuf "")
assertEqualM "buflen-to-empty" simp simpExpected
ret <- checkEquivPropAndLHS e simpExpected
assertBoolM "Must be equivalent" ret
, test "simp-max-buflength" $ do
let simp = Expr.simplify $ Max (Lit 0) (BufLength (AbstractBuf "txdata"))
assertEqualM "max-buflength rules" simp $ BufLength (AbstractBuf "txdata")
, test "simp-PLT-max" $ do
let simp = Expr.simplifyProp $ PLT (Max (Lit 5) (BufLength (AbstractBuf "txdata"))) (Lit 99)
assertEqualM "max-buflength rules" simp $ PLT (BufLength (AbstractBuf "txdata")) (Lit 99)
, test "simp-assoc-add1" $ do
let simp = Expr.simplify $ Add (Var "a") (Add (Var "b") (Var "c"))
assertEqualM "assoc rules" simp $ Add (Add (Var "a") (Var "b")) (Var "c")
, test "simp-assoc-add2" $ do
let simp = Expr.simplify $ Add (Lit 1) (Add (Var "b") (Var "c"))
assertEqualM "assoc rules" simp $ Add (Add (Lit 1) (Var "b")) (Var "c")
, test "simp-assoc-add3" $ do
let simp = Expr.simplify $ Add (Lit 1) (Add (Lit 2) (Var "c"))
assertEqualM "assoc rules" simp $ Add (Lit 3) (Var "c")
, test "simp-assoc-add4" $ do
let simp = Expr.simplify $ Add (Lit 1) (Add (Var "b") (Lit 2))
assertEqualM "assoc rules" simp $ Add (Lit 3) (Var "b")
, test "simp-assoc-add5" $ do
let simp = Expr.simplify $ Add (Var "a") (Add (Lit 1) (Lit 2))
assertEqualM "assoc rules" simp $ Add (Lit 3) (Var "a")
, test "simp-assoc-add6" $ do
let simp = Expr.simplify $ Add (Lit 7) (Add (Lit 1) (Lit 2))
assertEqualM "assoc rules" simp $ Lit 10
, test "simp-assoc-add-7" $ do
let simp = Expr.simplify $ Add (Var "a") (Add (Var "b") (Lit 2))
assertEqualM "assoc rules" simp $ Add (Add (Lit 2) (Var "a")) (Var "b")
, test "simp-assoc-add8" $ do
let simp = Expr.simplify $ Add (Add (Var "a") (Add (Lit 0x2) (Var "b"))) (Add (Var "c") (Add (Lit 0x2) (Var "d")))
assertEqualM "assoc rules" simp $ Add (Add (Add (Add (Lit 0x4) (Var "a")) (Var "b")) (Var "c")) (Var "d")
, test "simp-assoc-mul1" $ do
let simp = Expr.simplify $ Mul (Var "a") (Mul (Var "b") (Var "c"))
assertEqualM "assoc rules" simp $ Mul (Mul (Var "a") (Var "b")) (Var "c")
, test "simp-assoc-mul2" $ do
let simp = Expr.simplify $ Mul (Lit 2) (Mul (Var "a") (Lit 3))
assertEqualM "assoc rules" simp $ Mul (Lit 6) (Var "a")
, test "simp-zero-write-extend-buffer-len" $ do
let
expr = BufLength $ CopySlice (Lit 0) (Lit 0x10) (Lit 0) (AbstractBuf "buffer") (ConcreteBuf "bimm")
simp = Expr.simplify expr
ret <- checkEquiv expr simp
assertEqualM "Must be equivalent" True ret
, test "bufLength-simp" $ do
let
a = BufLength (ConcreteBuf "ab")
simp = Expr.simplify a
assertEqualM "Must be simplified down to a Lit" simp (Lit 2)
, test "stripWrites-overflow" $ do
-- below eventually boils down to
-- unsafeInto (0xf0000000000000000000000000000000000000000000000000000000000000+1) :: Int
-- which failed before
let
a = ReadByte (Lit 0xf0000000000000000000000000000000000000000000000000000000000000) (WriteByte (And (SHA256 (ConcreteBuf "")) (Lit 0x1)) (LitByte 0) (ConcreteBuf ""))
b = Expr.simplify a
ret <- checkEquiv a b
assertBoolM "must be equivalent" ret
, test "read-beyond-bound (negative-test)" $ do
let
e1 = CopySlice (Lit 1) (Lit 0) (Lit 2) (ConcreteBuf "a") (ConcreteBuf "")
e2 = ConcreteBuf "Definitely not the same!"
equal <- checkEquiv e1 e2
assertBoolM "Should not be equivalent!" $ not equal
]
-- These tests fuzz the simplifier by generating a random expression,
-- applying some simplification rules, and then using the smt encoding to
-- check that the simplified version is semantically equivalent to the
-- unsimplified one
, adjustOption (\(Test.Tasty.QuickCheck.QuickCheckTests n) -> Test.Tasty.QuickCheck.QuickCheckTests (div n 2)) $ testGroup "SimplifierTests"
[ testProperty "buffer-simplification" $ \(expr :: Expr Buf) -> prop $ do
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, testProperty "buffer-simplification-len" $ \(expr :: Expr Buf) -> prop $ do
let simplified = Expr.simplify (BufLength expr)
checkEquivAndLHS (BufLength expr) simplified
, testProperty "store-simplification" $ \(expr :: Expr Storage) -> prop $ do
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, testProperty "load-simplification" $ \(GenWriteStorageLoad expr) -> prop $ do
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, ignoreTest $ testProperty "load-decompose" $ \(GenWriteStorageLoad expr) -> prop $ do
putStrLnM $ T.unpack $ formatExpr expr
let simp = Expr.simplify expr
let decomposed = fromMaybe simp $ mapExprM Expr.decomposeStorage simp
-- putStrLnM $ "-----------------------------------------"
-- putStrLnM $ T.unpack $ formatExpr decomposed
-- putStrLnM $ "\n\n\n\n"
checkEquiv expr decomposed
, testProperty "byte-simplification" $ \(expr :: Expr Byte) -> prop $ do
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, testProperty "word-simplification" $ \(ZeroDepthWord expr) -> prop $ do
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, testProperty "readStorage-equivalance" $ \(store, slot) -> prop $ do
let simplified = Expr.readStorage' slot store
full = SLoad slot store
checkEquiv full simplified
, testProperty "writeStorage-equivalance" $ \(val, GenWriteStorageExpr (slot, store)) -> prop $ do
let simplified = Expr.writeStorage slot val store
full = SStore slot val store
checkEquiv full simplified
, testProperty "readWord-equivalance" $ \(buf, idx) -> prop $ do
let simplified = Expr.readWord idx buf
full = ReadWord idx buf
checkEquiv full simplified
, testProperty "writeWord-equivalance" $ \(idx, val, WriteWordBuf buf) -> prop $ do
let simplified = Expr.writeWord idx val buf
full = WriteWord idx val buf
checkEquiv full simplified
, testProperty "arith-simplification" $ \(_ :: Int) -> prop $ do
expr <- liftIO $ generate . sized $ genWordArith 15
let simplified = Expr.simplify expr
checkEquivAndLHS expr simplified
, testProperty "readByte-equivalance" $ \(buf, idx) -> prop $ do
let simplified = Expr.readByte idx buf
full = ReadByte idx buf
checkEquiv full simplified
-- we currently only simplify concrete writes over concrete buffers so that's what we test here
, testProperty "writeByte-equivalance" $ \(LitOnly val, LitOnly buf, GenWriteByteIdx idx) -> prop $ do
let simplified = Expr.writeByte idx val buf
full = WriteByte idx val buf
checkEquiv full simplified
, testProperty "copySlice-equivalance" $ \(srcOff, GenCopySliceBuf src, GenCopySliceBuf dst, LitWord @300 size) -> prop $ do
-- we bias buffers to be concrete more often than not
dstOff <- liftIO $ generate (maybeBoundedLit 100_000)
let simplified = Expr.copySlice srcOff dstOff size src dst
full = CopySlice srcOff dstOff size src dst
checkEquiv full simplified
, testProperty "indexWord-equivalence" $ \(src, LitWord @50 idx) -> prop $ do
let simplified = Expr.indexWord idx src
full = IndexWord idx src
checkEquiv full simplified
, testProperty "indexWord-mask-equivalence" $ \(src :: Expr EWord, LitWord @35 idx) -> prop $ do
mask <- liftIO $ generate $ do
pow <- arbitrary :: Gen Int
frequency
[ (1, pure $ Lit $ (shiftL 1 (pow `mod` 256)) - 1) -- potentially non byte aligned
, (1, pure $ Lit $ (shiftL 1 ((pow * 8) `mod` 256)) - 1) -- byte aligned
]
let
input = And mask src
simplified = Expr.indexWord idx input
full = IndexWord idx input
checkEquiv full simplified
, testProperty "toList-equivalance" $ \buf -> prop $ do
let
-- transforms the input buffer to give it a known length
fixLength :: Expr Buf -> Gen (Expr Buf)
fixLength = mapExprM go
where
go :: Expr a -> Gen (Expr a)
go = \case
WriteWord _ val b -> liftM3 WriteWord idx (pure val) (pure b)
WriteByte _ val b -> liftM3 WriteByte idx (pure val) (pure b)
CopySlice so _ sz src dst -> liftM5 CopySlice (pure so) idx (pure sz) (pure src) (pure dst)
AbstractBuf _ -> cbuf
e -> pure e
cbuf = do
bs <- arbitrary
pure $ ConcreteBuf bs
idx = do
w <- arbitrary
-- we use 100_000 as an upper bound for indices to keep tests reasonably fast here
pure $ Lit (w `mod` 100_000)
input <- liftIO $ generate $ fixLength buf
case Expr.toList input of
Nothing -> do
putStrLnM "skip"
pure True -- ignore cases where the buf cannot be represented as a list
Just asList -> do
let asBuf = Expr.fromList asList
checkEquiv asBuf input
, testProperty "simplifyProp-equivalence-lit" $ \(LitProp p) -> prop $ do
let simplified = Expr.simplifyProps [p]
case simplified of
[] -> checkEquivProp (PBool True) p
[val@(PBool _)] -> checkEquivProp val p
_ -> liftIO $ assertFailure "must evaluate down to a literal bool"
, testProperty "simplifyProp-equivalence-sym" $ \(p) -> prop $ do
let simplified = Expr.simplifyProp p
checkEquivPropAndLHS p simplified
, testProperty "simplify-joinbytes" $ \(SymbolicJoinBytes exprList) -> prop $ do
let x = joinBytesFromList exprList
let simplified = Expr.simplify x
y <- checkEquiv x simplified
assertBoolM "Must be equal" y
, testProperty "simpProp-equivalence-sym-Prop" $ \(ps :: [Prop]) -> prop $ do
let simplified = pand (Expr.simplifyProps ps)
checkEquivPropAndLHS (pand ps) simplified
, testProperty "simpProp-equivalence-sym-LitProp" $ \(LitProp p) -> prop $ do
let simplified = pand (Expr.simplifyProps [p])
checkEquivPropAndLHS p simplified
, testProperty "storage-slot-simp-property" $ \(StorageExp s) -> prop $ do
-- we have to run `Expr.structureArraySlots` on the unsimplified system, or
-- we'd need some form of minimal simplifier for things to work out. As long as
-- we trust the structureArraySlots, this is fine, as that function is standalone,
-- and quite minimal
let s2 = Expr.structureArraySlots s
let simplified = Expr.simplify s2
checkEquivAndLHS s2 simplified
, test "storage-slot-single" $ do
-- this tests that "" and "0"x32 is not equivalent in Keccak
let x = SLoad (Add (Keccak (ConcreteBuf "")) (Lit 1)) (SStore (Keccak (ConcreteBuf "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL")) (Lit 0) (AbstractStore (SymAddr "stuff") Nothing))
let simplified = Expr.simplify x
y <- checkEquivAndLHS x simplified
assertBoolM "Must be equal" y
, test "word-eq-bug" $ do
-- This test is actually OK because the simplified takes into account that it's impossible to find a
-- near-collision in the keccak hash
let x = (SLoad (Keccak (AbstractBuf "es")) (SStore (Add (Keccak (ConcreteBuf "")) (Lit 0x1)) (Lit 0xacab) (ConcreteStore (Map.empty))))
let simplified = Expr.simplify x
y <- checkEquiv x simplified
assertBoolM "Must be equal, given keccak distance axiom" y
]
{- NOTE: These tests were designed to test behaviour on reading from a buffer such that the indices overflow 2^256.
However, such scenarios are impossible in the real world (the operation would run out of gas). The problem
is that the behaviour of bytecode interpreters does not match the semantics of SMT. Intrepreters just
return all zeroes for any read beyond buffer size, while in SMT reading multiple bytes may lead to overflow
on indices and subsequently to reading from the beginning of the buffer (wrap-around semantics).
, testGroup "concrete-buffer-simplification-large-index" [
test "copy-slice-large-index-nooverflow" $ do
let
e = CopySlice (Lit 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (Lit 0x0) (Lit 0x1) (ConcreteBuf "a") (ConcreteBuf "")
s = Expr.simplify e
equal <- checkEquiv e s
assertEqualM "Must be equal" True equal
, test "copy-slice-overflow-back-into-source" $ do
let
e = CopySlice (Lit 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (Lit 0x0) (Lit 0x2) (ConcreteBuf "a") (ConcreteBuf "")
s = Expr.simplify e
equal <- checkEquiv e s
assertEqualM "Must be equal" True equal
, test "copy-slice-overflow-beyond-source" $ do
let
e = CopySlice (Lit 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (Lit 0x0) (Lit 0x3) (ConcreteBuf "a") (ConcreteBuf "")
s = Expr.simplify e
equal <- checkEquiv e s
assertEqualM "Must be equal" True equal
, test "copy-slice-overflow-beyond-source-into-nonempty" $ do
let
e = CopySlice (Lit 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (Lit 0x0) (Lit 0x3) (ConcreteBuf "a") (ConcreteBuf "b")
s = Expr.simplify e
equal <- checkEquiv e s
assertEqualM "Must be equal" True equal
, test "read-word-overflow-back-into-source" $ do
let
e = ReadWord (Lit 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (ConcreteBuf "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk")
s = Expr.simplify e
equal <- checkEquiv e s
assertEqualM "Must be equal" True equal
]
-}
, testGroup "isUnsat-concrete-tests" [
test "disjunction-left-false" $ do
let
t = [PEq (Var "x") (Lit 1), POr (PEq (Var "x") (Lit 0)) (PEq (Var "y") (Lit 1)), PEq (Var "y") (Lit 2)]
propagated = Expr.constPropagate t
assertEqualM "Must contain PBool False" True ((PBool False) `elem` propagated)
, test "disjunction-right-false" $ do
let
t = [PEq (Var "x") (Lit 1), POr (PEq (Var "y") (Lit 1)) (PEq (Var "x") (Lit 0)), PEq (Var "y") (Lit 2)]
propagated = Expr.constPropagate t
assertEqualM "Must contain PBool False" True ((PBool False) `elem` propagated)
, test "disjunction-both-false" $ do
let
t = [PEq (Var "x") (Lit 1), POr (PEq (Var "x") (Lit 2)) (PEq (Var "x") (Lit 0)), PEq (Var "y") (Lit 2)]
propagated = Expr.constPropagate t
assertEqualM "Must contain PBool False" True ((PBool False) `elem` propagated)
, ignoreTest $ test "disequality-and-equality" $ do
let
t = [PNeg (PEq (Lit 1) (Var "arg1")), PEq (Lit 1) (Var "arg1")]
propagated = Expr.constPropagate t
assertEqualM "Must contain PBool False" True ((PBool False) `elem` propagated)
, test "equality-and-disequality" $ do
let
t = [PEq (Lit 1) (Var "arg1"), PNeg (PEq (Lit 1) (Var "arg1"))]
propagated = Expr.constPropagate t
assertEqualM "Must contain PBool False" True ((PBool False) `elem` propagated)
]
, testGroup "simpProp-concrete-tests" [
test "simpProp-concrete-trues" $ do
let
t = [PBool True, PBool True]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [] simplified
, test "simpProp-concrete-false1" $ do
let
t = [PBool True, PBool False]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-concrete-false2" $ do
let
t = [PBool False, PBool False]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-concrete-or-1" $ do
let
-- a = 5 && (a=4 || a=3) -> False
t = [PEq (Lit 5) (Var "a"), POr (PEq (Var "a") (Lit 4)) (PEq (Var "a") (Lit 3))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, ignoreTest $ test "simpProp-concrete-or-2" $ do
let
-- Currently does not work, because we don't do simplification inside
-- POr/PAnd using canBeSat
-- a = 5 && (a=4 || a=5) -> a=5
t = [PEq (Lit 5) (Var "a"), POr (PEq (Var "a") (Lit 4)) (PEq (Var "a") (Lit 5))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [] simplified
, test "simpProp-concrete-and-1" $ do
let
-- a = 5 && (a=4 && a=3) -> False
t = [PEq (Lit 5) (Var "a"), PAnd (PEq (Var "a") (Lit 4)) (PEq (Var "a") (Lit 3))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-concrete-or-of-or" $ do
let
-- a = 5 && ((a=4 || a=6) || a=3) -> False
t = [PEq (Lit 5) (Var "a"), POr (POr (PEq (Var "a") (Lit 4)) (PEq (Var "a") (Lit 6))) (PEq (Var "a") (Lit 3))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-inner-expr-simp" $ do
let
-- 5+1 = 6
t = [PEq (Add (Lit 5) (Lit 1)) (Var "a")]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PEq (Lit 6) (Var "a")] simplified
, test "simpProp-inner-expr-simp-with-canBeSat" $ do
let
-- 5+1 = 6, 6 != 7
t = [PAnd (PEq (Add (Lit 5) (Lit 1)) (Var "a")) (PEq (Var "a") (Lit 7))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-inner-expr-bitwise-and" $ do
let
-- 1 & 2 != 2
t = [PEq (And (Lit 1) (Lit 2)) (Lit 2)]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PBool False] simplified
, test "simpProp-inner-expr-bitwise-or" $ do
let
-- 2 | 4 == 6
t = [PEq (Or (Lit 2) (Lit 4)) (Lit 6)]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [] simplified
, test "simpProp-constpropagate-1" $ do
let
-- 5+1 = 6
t = [PEq (Add (Lit 5) (Lit 1)) (Var "a"), PEq (Var "b") (Var "a")]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PEq (Lit 6) (Var "a"), PEq (Lit 6) (Var "b")] simplified
, test "simpProp-constpropagate-2" $ do
let
-- 5+1 = 6
t = [PEq (Add (Lit 5) (Lit 1)) (Var "a"), PEq (Var "b") (Var "a"), PEq (Var "c") (Sub (Var "b") (Lit 1))]
simplified = Expr.simplifyProps t
assertEqualM "Must be equal" [PEq (Lit 6) (Var "a"), PEq (Lit 6) (Var "b"), PEq (Lit 5) (Var "c")] simplified
, test "simpProp-constpropagate-3" $ do
let
t = [ PEq (Add (Lit 5) (Lit 1)) (Var "a") -- a = 6
, PEq (Var "b") (Var "a") -- b = 6
, PEq (Var "c") (Sub (Var "b") (Lit 1)) -- c = 5
, PEq (Var "d") (Sub (Var "b") (Var "c"))] -- d = 1
simplified = Expr.simplifyProps t
assertEqualM "Must know d == 1" ((PEq (Lit 1) (Var "d")) `elem` simplified) True
]
, testGroup "MemoryTests"
[ test "read-write-same-byte" $ assertEqualM ""
(LitByte 0x12)
(Expr.readByte (Lit 0x20) (WriteByte (Lit 0x20) (LitByte 0x12) mempty))
, test "read-write-same-word" $ assertEqualM ""
(Lit 0x12)
(Expr.readWord (Lit 0x20) (WriteWord (Lit 0x20) (Lit 0x12) mempty))
, test "read-byte-write-word" $ assertEqualM ""
-- reading at byte 31 a word that's been written should return LSB
(LitByte 0x12)
(Expr.readByte (Lit 0x1f) (WriteWord (Lit 0x0) (Lit 0x12) mempty))
, test "read-byte-write-word2" $ assertEqualM ""
-- Same as above, but offset not 0
(LitByte 0x12)
(Expr.readByte (Lit 0x20) (WriteWord (Lit 0x1) (Lit 0x12) mempty))
,test "read-write-with-offset" $ assertEqualM ""
-- 0x3F = 63 decimal, 0x20 = 32. 0x12 = 18
-- We write 128bits (32 Bytes), representing 18 at offset 32.
-- Hence, when reading out the 63rd byte, we should read out the LSB 8 bits
-- which is 0x12
(LitByte 0x12)
(Expr.readByte (Lit 0x3F) (WriteWord (Lit 0x20) (Lit 0x12) mempty))
,test "read-write-with-offset2" $ assertEqualM ""
-- 0x20 = 32, 0x3D = 61
-- we write 128 bits (32 Bytes) representing 0x10012, at offset 32.
-- we then read out a byte at offset 61.
-- So, at 63 we'd read 0x12, at 62 we'd read 0x00, at 61 we should read 0x1
(LitByte 0x1)
(Expr.readByte (Lit 0x3D) (WriteWord (Lit 0x20) (Lit 0x10012) mempty))
, test "read-write-with-extension-to-zero" $ assertEqualM ""
-- write word and read it at the same place (i.e. 0 offset)
(Lit 0x12)
(Expr.readWord (Lit 0x0) (WriteWord (Lit 0x0) (Lit 0x12) mempty))
, test "read-write-with-extension-to-zero-with-offset" $ assertEqualM ""
-- write word and read it at the same offset of 4
(Lit 0x12)
(Expr.readWord (Lit 0x4) (WriteWord (Lit 0x4) (Lit 0x12) mempty))
, test "read-write-with-extension-to-zero-with-offset2" $ assertEqualM ""
-- write word and read it at the same offset of 16
(Lit 0x12)
(Expr.readWord (Lit 0x20) (WriteWord (Lit 0x20) (Lit 0x12) mempty))
, test "read-word-copySlice-overlap" $ assertEqualM ""
-- we should not recurse into a copySlice if the read index + 32 overlaps the sliced region
(ReadWord (Lit 40) (CopySlice (Lit 0) (Lit 30) (Lit 12) (WriteWord (Lit 10) (Lit 0x64) (AbstractBuf "hi")) (AbstractBuf "hi")))
(Expr.readWord (Lit 40) (CopySlice (Lit 0) (Lit 30) (Lit 12) (WriteWord (Lit 10) (Lit 0x64) (AbstractBuf "hi")) (AbstractBuf "hi")))
, test "indexword-MSB" $ assertEqualM ""
-- 31st is the LSB byte (of 32)
(LitByte 0x78)
(Expr.indexWord (Lit 31) (Lit 0x12345678))
, test "indexword-LSB" $ assertEqualM ""
-- 0th is the MSB byte (of 32), Lit 0xff22bb... is exactly 32 Bytes.
(LitByte 0xff)
(Expr.indexWord (Lit 0) (Lit 0xff22bb4455667788990011223344556677889900112233445566778899001122))
, test "indexword-LSB2" $ assertEqualM ""
-- same as above, but with offset 2
(LitByte 0xbb)
(Expr.indexWord (Lit 2) (Lit 0xff22bb4455667788990011223344556677889900112233445566778899001122))
, test "encodeConcreteStore-overwrite" $
assertEqualM ""
(pure "(store (store ((as const Storage) #x0000000000000000000000000000000000000000000000000000000000000000) (_ bv1 256) (_ bv2 256)) (_ bv3 256) (_ bv4 256))")
(EVM.SMT.encodeConcreteStore $ Map.fromList [(W256 1, W256 2), (W256 3, W256 4)])
, test "indexword-oob-sym" $ assertEqualM ""
-- indexWord should return 0 for oob access
(LitByte 0x0)
(Expr.indexWord (Lit 100) (JoinBytes
(LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0)
(LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0)
(LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0)
(LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0) (LitByte 0)))
, test "stripbytes-concrete-bug" $ assertEqualM ""
(Expr.simplifyReads (ReadByte (Lit 0) (ConcreteBuf "5")))
(LitByte 53)
]
, testGroup "ABI"
[ testProperty "Put/get inverse" $ \x ->
case runGetOrFail (getAbi (abiValueType x)) (runPut (putAbi x)) of
Right ("", _, x') -> x' == x
_ -> False