@@ -20,12 +20,67 @@ def __init__(self) -> None:
2020 self .visited_hookaddr = False
2121
2222class EVMTest (unittest .TestCase ):
23- def test_underflow (self ):
24- ql = Qiling (archtype = "evm" , verbose = 4 )
23+ def test_underflow_code (self ):
24+ ql = Qiling (code = "0x6060604052341561000f57600080fd5b60405160208061031c833981016040528080519060200190919050508060018190556000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050610299806100836000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806318160ddd1461005c57806370a0823114610085578063a9059cbb146100d2575b600080fd5b341561006757600080fd5b61006f61012c565b6040518082815260200191505060405180910390f35b341561009057600080fd5b6100bc600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610132565b6040518082815260200191505060405180910390f35b34156100dd57600080fd5b610112600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061017a565b604051808215151515815260200191505060405180910390f35b60015481565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600080826000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205403101515156101cb57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060019050929150505600a165627a7a7230582098f1551a391a3e65b3ce45cfa2b3fa5f91eea9a3e7181a81454e025ea0d7151c0029" ,archtype = "evm" , verbose = 4 )
2525 testcheck = Checklist ()
26- code = '0x6060604052341561000f57600080fd5b60405160208061031c833981016040528080519060200190919050508060018190556000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050610299806100836000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806318160ddd1461005c57806370a0823114610085578063a9059cbb146100d2575b600080fd5b341561006757600080fd5b61006f61012c565b6040518082815260200191505060405180910390f35b341561009057600080fd5b6100bc600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610132565b6040518082815260200191505060405180910390f35b34156100dd57600080fd5b610112600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061017a565b604051808215151515815260200191505060405180910390f35b60015481565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600080826000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205403101515156101cb57600080fd5b816000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060019050929150505600a165627a7a7230582098f1551a391a3e65b3ce45cfa2b3fa5f91eea9a3e7181a81454e025ea0d7151c0029'
2726 argu = ql .arch .evm .abi .convert (['uint256' ], [20 ])
28- code = code + argu
27+ code = ql .code + argu
28+
29+ user1 = ql .arch .evm .create_account (balance = 100 * 10 ** 18 )
30+ user2 = ql .arch .evm .create_account (balance = 100 * 10 ** 18 )
31+ c1 = ql .arch .evm .create_account ()
32+
33+ def hookcode_test (ql , * argv ):
34+ testcheck .visited_hookcode = True
35+
36+ def hookinsn_test (ql , * argv ):
37+ testcheck .visited_hookinsn = True
38+
39+ def hookaddr_test (ql ):
40+ testcheck .visited_hookaddr = True
41+
42+ h0 = ql .hook_code (hookcode_test )
43+ h1 = ql .hook_address (hookaddr_test , 10 )
44+
45+ # message1: deploy runtime code
46+ msg0 = ql .arch .evm .create_message (user1 , b'' , code = code , contract_address = c1 )
47+ ql .run (code = msg0 )
48+
49+ ql .hook_del (h0 )
50+ ql .hook_del (h1 )
51+ h2 = ql .hook_insn (hookinsn_test , 'PUSH4' )
52+
53+ # # SMART CONTRACT DEPENDENT - message2: check balance of user1, should be 20
54+ def check_balance (sender , destination ):
55+ call_data = '0x70a08231' + ql .arch .evm .abi .convert (['address' ], [sender ])
56+ msg2 = ql .arch .evm .create_message (sender , destination , call_data )
57+ return ql .run (code = msg2 )
58+
59+ result = check_balance (user1 , c1 )
60+ print ('\n \n user1 balance =' , int (result .output .hex ()[2 :], 16 ))
61+ ql .hook_del (h2 )
62+
63+ # SMART CONTRACT DEPENDENT - message3: transform 21 from user1 to user2
64+ call_data = '0xa9059cbb' + ql .arch .evm .abi .convert (['address' ], [user2 ]) + \
65+ ql .arch .evm .abi .convert (['uint256' ], [21 ])
66+ msg1 = ql .arch .evm .create_message (user1 , c1 , call_data )
67+ result = ql .run (code = msg1 )
68+ print ('\n \n is success =' , int (result .output .hex ()[2 :], 16 ))
69+
70+ # message4: check balance of user1, should be MAX - 1
71+ result = check_balance (user1 , c1 )
72+ print ('\n \n user1 balance =' , hex (int (result .output .hex ()[2 :], 16 )))
73+
74+ self .assertEqual (hex (int (result .output .hex ()[2 :], 16 )), '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' )
75+ self .assertTrue (testcheck .visited_hookaddr )
76+ self .assertTrue (testcheck .visited_hookcode )
77+ self .assertTrue (testcheck .visited_hookinsn )
78+
79+ def test_underflow_filename (self ):
80+ ql = Qiling (["rootfs/evm/undeflow.hex" ], archtype = "evm" , verbose = 4 )
81+ testcheck = Checklist ()
82+ argu = ql .arch .evm .abi .convert (['uint256' ], [20 ])
83+ code = ql .code + argu
2984
3085 user1 = ql .arch .evm .create_account (balance = 100 * 10 ** 18 )
3186 user2 = ql .arch .evm .create_account (balance = 100 * 10 ** 18 )
0 commit comments