Skip to content

Commit

Permalink
Fixed issues: ethersolve statistics dump, resolved compilation proble…
Browse files Browse the repository at this point in the history
…m with 4.sol and 13.sol, added parser
  • Loading branch information
merendamattia committed Nov 6, 2024
1 parent 088cdcb commit 055eff2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
17 changes: 9 additions & 8 deletions script-python/journal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
Install dependencies:
```
brew install solc-select
solc-select install 0.5.12
solc-select use 0.5.12
pip install tqdm
pip install scipy
git clone https://github.com/DependableSystemsLab/SolidiFI-benchmark
git clone https://github.com/DependableSystemsLab/SolidiFI.git
```

Compile bytecodes:
```
python3.11 compile-all.py
python3.11 compile.py [-h] [--solidifi] [--smartbugs] [--longest-bytecode]
```

Start the benchmark:
Run the benchmark:
```
python3.11 run-benchmark.py
```
python3.11 run-benchmark.py [-h] [--solidifi] [--smartbugs]
```

## Dataset
1. https://github.com/DependableSystemsLab/SolidiFI-benchmark
2. https://github.com/DependableSystemsLab/SolidiFI
3. https://github.com/smartbugs/smartbugs-curated
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import csv
from tqdm import tqdm
import argparse

def clear_directory(directory):
"""
Expand Down Expand Up @@ -73,7 +74,7 @@ def compile_solidity_sources_with_different_version(source_dir, json_dir, versio
# Command to compile and save the bytecode in JSON format
command = (
f"solc-select use {compiled_version} > /dev/null && "
f"solc --combined-json bin-runtime {input_file} > {output_file} 2> /dev/null"
f"solc --combined-json bin,bin-runtime {input_file} > {output_file} 2> /dev/null"
)

# Execute the compilation command
Expand Down Expand Up @@ -105,7 +106,7 @@ def compile_solidity_sources(source_dir, json_dir):
output_file = os.path.join(json_dir, f"{os.path.splitext(filename)[0]}.json")

# Command to compile and save the bytecode in JSON format
command = f"solc --optimize-runs 0 --combined-json bin-runtime --pretty-json {input_file} > {output_file}"
command = f"solc --optimize-runs 0 --combined-json bin,bin-runtime --pretty-json {input_file} > {output_file}"

# Execute the command
try:
Expand Down Expand Up @@ -147,7 +148,10 @@ def extract_and_save_longest_bytecode(bytecode_dir, json_dir, is_ethersolve=Fals

# Find the contract with the longest bytecode
for contract_name, contract_data in contracts.items():
bytecode = contract_data.get("bin-runtime")
if(is_ethersolve):
bytecode = contract_data.get("bin")
else:
bytecode = contract_data.get("bin-runtime")
if bytecode:
bytecode_length = len(bytecode)
# Check if this bytecode is longer than the current longest
Expand Down Expand Up @@ -196,7 +200,11 @@ def extract_and_save_bytecode(bytecode_dir, json_dir, is_ethersolve=False):
count = 1 # Sequential counter for each bytecode in the same JSON

for contract_name, contract_data in contracts.items():
bytecode = contract_data.get("bin-runtime")
if(is_ethersolve):
bytecode = contract_data.get("bin")
else:
bytecode = contract_data.get("bin-runtime")

if bytecode:
# Add a sequential number to the filename
bytecode_filename = os.path.join(
Expand All @@ -210,17 +218,24 @@ def extract_and_save_bytecode(bytecode_dir, json_dir, is_ethersolve=False):
pbar.update(1)

if __name__ == "__main__":
solidifi = False
smartbugs = True
longest_bytecode = False
parser = argparse.ArgumentParser(description="Compile datasets.")
parser.add_argument("--solidifi", action="store_true", help="Run analysis on SolidiFI dataset")
parser.add_argument("--smartbugs", action="store_true", help="Run analysis on SmartBugs dataset")
parser.add_argument("--longest-bytecode", action="store_true", help="Save only the longest bytecode")

if solidifi:
args = parser.parse_args()

if not args.solidifi and not args.smartbugs:
parser.error("At least an arg is required.")
exit(1)

if args.solidifi:
compile_solidity_sources('./reentrancy-solidifi/source-code',
'./reentrancy-solidifi/json')
compile_solidity_sources('./vanilla-solidifi/source-code',
'./vanilla-solidifi/json')

if longest_bytecode:
if args.longest_bytecode:
# EVMLiSA
extract_and_save_longest_bytecode('./vanilla-solidifi/bytecode/evmlisa',
'./vanilla-solidifi/json')
Expand Down Expand Up @@ -249,12 +264,12 @@ def extract_and_save_bytecode(bytecode_dir, json_dir, is_ethersolve=False):
'./reentrancy-solidifi/json',
True)

if smartbugs:
if args.smartbugs:
compile_solidity_sources_with_different_version('./reentrancy-smartbugs/source-code',
'./reentrancy-smartbugs/json',
'./reentrancy-smartbugs/source-code/version.csv')

if longest_bytecode:
if args.longest_bytecode:
extract_and_save_longest_bytecode('./reentrancy-smartbugs/bytecode/evmlisa',
'./reentrancy-smartbugs/json')
extract_and_save_longest_bytecode('./reentrancy-smartbugs/bytecode/ethersolve',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ file,original pragma version,compiled version,notes
1.sol,^0.4.19,0.4.19,
2.sol,^0.4.19,0.4.19,
3.sol,^0.4.19,0.4.19,
4.sol,^0.4.19,0.4.19,
4.sol,^0.4.25,0.4.25,
5.sol,^0.4.19,0.4.19,
6.sol,^0.4.19,0.4.19,
7.sol,^0.4.25,0.4.25,
Expand All @@ -11,7 +11,7 @@ file,original pragma version,compiled version,notes
10.sol,^0.4.19,0.4.19,
11.sol,^0.4.25,0.4.25,
12.sol,^0.4.19,0.4.19,
13.sol,^0.4.19,0.4.19,
13.sol,^0.4.25,0.4.25,
14.sol,^0.4.19,0.4.19,
15.sol,^0.4.19,0.4.19,
16.sol,^0.4.19,0.4.19,
Expand Down
18 changes: 13 additions & 5 deletions script-python/journal/run-benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
import json
from datetime import datetime
import argparse

# Directory paths
max_threads = int(os.cpu_count() / 3) # Core avaiable
Expand Down Expand Up @@ -77,7 +78,7 @@ def plot_results(data_evmlisa, data_ethersolve, data_solidifi):
plt.figure(figsize=(12, 6))

plt.plot(keys1, values1, marker='o', label='Ethersolve', color='purple')
plt.plot(keys2, values2, marker='o', label='SolidiFI', color='red')
plt.plot(keys2, values2, marker='o', label='Truth', color='red')
plt.plot(keys3, values3, marker='o', label='EVMLiSA', color='green')

plt.xlabel('Problem ID')
Expand Down Expand Up @@ -426,12 +427,19 @@ def results_smartbugs(json_path, print_data):
#################################### Main

if __name__ == "__main__":
solidifi = False
smartbugs = True
parser = argparse.ArgumentParser(description="EVMLiSA and EtherSolve analysis.")
parser.add_argument("--solidifi", action="store_true", help="Run analysis on SolidiFI dataset")
parser.add_argument("--smartbugs", action="store_true", help="Run analysis on SmartBugs dataset")

args = parser.parse_args()

if not args.solidifi and not args.smartbugs:
parser.error("At least an arg is required.")
exit(1)

build_evmlisa()

if solidifi:
if args.solidifi:
# SolidiFI dataset
evmlisa_vanilla_thread = threading.Thread(target=evmlisa, kwargs={'bytecode_dir': './vanilla-solidifi/bytecode/evmlisa',
'results_dir': './vanilla-solidifi/results',
Expand Down Expand Up @@ -467,7 +475,7 @@ def results_smartbugs(json_path, print_data):
results_solidifi( './SolidiFI-buggy-contracts/Re-entrancy', 'solidify')
)

if smartbugs:
if args.smartbugs:
evmlisa_thread = threading.Thread(target=evmlisa, kwargs={'bytecode_dir': './reentrancy-smartbugs/bytecode/evmlisa',
'results_dir': './reentrancy-smartbugs/results',
'result_evmlisa_dir': './reentrancy-smartbugs/results/evmlisa'})
Expand Down

0 comments on commit 055eff2

Please sign in to comment.