Skip to content

Commit decc589

Browse files
authored
Create sub_smartinv.py
1 parent 1b5e94e commit decc589

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

tests/sub_smartinv.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import argparse
2+
import os
3+
import shutil
4+
import sys
5+
import subprocess
6+
import argparse
7+
8+
9+
10+
results_dir = '/Users/sallywang/SmartInv/SmartInv/results/contexts'
11+
large_exp_results_dir = "/Users/sallywang/SmartInv/SmartInv/large_exp_results"
12+
refined_exp_results = "/Users/sallywang/SmartInv/SmartInv/refined_exp_results"
13+
14+
15+
def runcmd(cmd, verbose=False, *args, **kwargs):
16+
17+
process = subprocess.Popen(
18+
cmd,
19+
stdout = subprocess.PIPE,
20+
stderr = subprocess.PIPE,
21+
text = True,
22+
shell = True
23+
)
24+
std_out, std_err = process.communicate()
25+
with open('./stdout.txt', 'w') as f:
26+
f.write(std_out)
27+
#f.close()
28+
if verbose:
29+
print(std_out.strip(), std_err)
30+
31+
def runcmd_out(cmd, verbose=False, *args, **kwargs):
32+
33+
process = subprocess.Popen(
34+
cmd,
35+
stdout = subprocess.PIPE,
36+
stderr = subprocess.PIPE,
37+
text = True,
38+
shell = True
39+
)
40+
std_out, std_err = process.communicate()
41+
with open('./stdout.txt', 'w') as f:
42+
f.write(std_out)
43+
#f.close()
44+
print(std_out)
45+
print(std_err)
46+
if verbose:
47+
print(std_out.strip(), std_err)
48+
return std_out, std_err
49+
50+
def parse_arguments(args):
51+
parser = argparse.ArgumentParser(sys.argv[0])
52+
parser.add_argument('--file', type=str, required=False, default='')
53+
parser.add_argument('--contract', type=str, required=False, default='')
54+
parser.add_argument('--exp1', type=bool, required=False, default=True)
55+
parser.add_argument('--exp2', type=bool, required=False, default=True)
56+
parser.add_argument('--large_exp', type=bool, required=False, default=True)
57+
parser.add_argument('--refined_exp', type=bool, required=False, default=True)
58+
parser.add_argument('--easy', type=bool, required=False, default=True)
59+
parser.add_argument('--heavy', type=bool, required=False, default=False)
60+
parser.add_argument('--auto', type=bool, required=False, default=False)
61+
parser.add_argument('--VeriSol', type=str, required=False, default=None)
62+
args = parser.parse_args()
63+
return parser.parse_args()
64+
65+
66+
def clean_paths(verified_dir, contract_name):
67+
if os.path.isfile("./__SolToBoogieTest_out.bpl"):
68+
runcmd(f"mv __SolToBoogieTest_out.bpl {verified_dir}/{contract_name}")
69+
if os.path.isfile("./boogie.txt"):
70+
runcmd(f"mv boogie.txt {verified_dir}/{contract_name}")
71+
if os.path.isfile("./corral.txt"):
72+
runcmd(f"mv corral.txt {verified_dir}/{contract_name}")
73+
if os.path.isfile("./corral_counterex.txt"):
74+
runcmd(f"mv corral_counterex.txt {verified_dir}/{contract_name}")
75+
if os.path.isfile("./corral_out.bpl"):
76+
runcmd(f"mv corral_out.bpl {verified_dir}/{contract_name}")
77+
if os.path.isfile("./corral_out_trace.txt"):
78+
runcmd(f"mv corral_out_trace.txt {verified_dir}/{contract_name}")
79+
if os.path.isfile("./stdout.txt"):
80+
runcmd(f"mv stdout.txt {verified_dir}/{contract_name}")
81+
82+
def auto_compiler_detect(contract_file):
83+
file = open(contract_file, "r")
84+
if "pragma solidity ^0.4" in file.read():
85+
runcmd("solc-select install 0.4.0")
86+
runcmd("solc-select use 0.4.0")
87+
elif "pragma solidity ^0.5" in file.read():
88+
runcmd("solc-select install 0.5.0")
89+
runcmd("solc-select use 0.5.0")
90+
elif "pragma solidity ^0.6" in file.read():
91+
runcmd("solc-select install 0.6.0")
92+
runcmd("solc-select use 0.6.0")
93+
elif "pragma solidity ^0.7" in file.read():
94+
runcmd("solc-select install 0.7.0")
95+
runcmd("solc-select use 0.7.0")
96+
elif "pragma solidity ^0.8" in file.read():
97+
runcmd("solc-select install 0.8.0")
98+
runcmd("solc-select use 0.8.0")
99+
100+
101+
def run_veriSmart(contract_file):
102+
auto_compiler_detect(contract_file)
103+
if (os.path.isdir(large_exp_results_dir)) is False:
104+
os.mkdir(large_exp_results_dir)
105+
if (os.path.isdir(f"{large_exp_results_dir}/veriSmart")) is False:
106+
os.mkdir(f"{large_exp_results_dir}/veriSmart")
107+
if (os.path.isdir(f"{large_exp_results_dir}/veriSmart/{contract_file}")) is False:
108+
os.mkdir(f"{large_exp_results_dir}/veriSmart/{contract_file}")
109+
results_dir = f"{large_exp_results_dir}/veriSmart/{contract_file}"
110+
std_out, std_err = runcmd_out(f"~/VeriSmart-public/main.native -input {contract_file} -verify_timeout 60 -z3timeout 10000")
111+
result_file = open(f"{results_dir}/report", "w+")
112+
result_file.write(std_out)
113+
result_file.write(std_err)
114+
result_file.close()
115+
116+
def run_smartest(contract_file):
117+
auto_compiler_detect(contract_file)
118+
if (os.path.isdir(large_exp_results_dir)) is False:
119+
os.mkdir(large_exp_results_dir)
120+
if (os.path.isdir(f"{large_exp_results_dir}/smartest")) is False:
121+
os.mkdir(f"{large_exp_results_dir}/smartest")
122+
if (os.path.isdir(f"{large_exp_results_dir}/smartest/{contract_file}")) is False:
123+
os.mkdir(f"{large_exp_results_dir}/smartest/{contract_file}")
124+
results_dir = f"{large_exp_results_dir}/smartest/{contract_file}"
125+
std_out, std_err = runcmd_out(f"~/VeriSmart-public/main.native -input {contract_file} -mode exploit -exploit_timeout 10000 leak")
126+
result_file = open(f"{results_dir}/report", "w+")
127+
result_file.write(std_out)
128+
result_file.write(std_err)
129+
result_file.close()
130+
131+
def run_manticore(contract_file):
132+
if (os.path.isdir(large_exp_results_dir)) is False:
133+
os.mkdir(large_exp_results_dir)
134+
if (os.path.isdir(f"{large_exp_results_dir}/manticore")) is False:
135+
os.mkdir(f"{large_exp_results_dir}/manticore")
136+
if (os.path.isdir(f"{large_exp_results_dir}/manticore/{contract_file}")) is False:
137+
os.mkdir(f"{large_exp_results_dir}/manticore/{contract_file}")
138+
results_dir = f"{large_exp_results_dir}/manticore/{contract_file}"
139+
std_out, std_err = runcmd_out(f"manticore {contract_file}")
140+
result_file = open(f"{results_dir}/report", "w+")
141+
result_file.write(std_out)
142+
result_file.write(std_err)
143+
result_file.close()
144+
145+
def run_mythril(contract_file):
146+
if (os.path.isdir(large_exp_results_dir)) is False:
147+
os.mkdir(large_exp_results_dir)
148+
if (os.path.isdir(f"{large_exp_results_dir}/mythril")) is False:
149+
os.mkdir(f"{large_exp_results_dir}/mythril")
150+
if (os.path.isdir(f"{large_exp_results_dir}/mythril/{contract_file}")) is False:
151+
os.mkdir(f"{large_exp_results_dir}/mythril/{contract_file}")
152+
results_dir = f"{large_exp_results_dir}/mythril/{contract_file}"
153+
std_out, std_err = runcmd_out(f"docker run -v $(pwd):/tmp mythril/myth analyze /tmp/{contract_file}")
154+
result_file = open(f"{results_dir}/report", "w+")
155+
result_file.write(std_out)
156+
result_file.write(std_err)
157+
result_file.close()
158+
159+
def run_large_scale_exp():
160+
test_dir = "/Users/sallywang/SmartInv/SmartInv/tests/contracts"
161+
for filename in os.listdir(test_dir):
162+
#run_veriSmart(filename)
163+
#run_smartest(filename)
164+
run_manticore(filename)
165+
#run_mythril(filename)
166+
167+
168+
169+
def main():
170+
args = parse_arguments(sys.argv[1:])
171+
if args.large_exp == True:
172+
run_large_scale_exp()
173+
174+
if __name__ == "__main__":
175+
main()

0 commit comments

Comments
 (0)