Skip to content

Commit 0f1aa0c

Browse files
committed
ABI parser to extract function signatures
This aims to identify the function entry points in the bytecode
1 parent 49b85cd commit 0f1aa0c

File tree

112 files changed

+868
-2581
lines changed

Some content is hidden

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

112 files changed

+868
-2581
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ dependencies {
4242
implementation 'io.github.cdimascio:dotenv-java:3.0.0'
4343

4444
implementation 'org.json:json:20210307'
45+
46+
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
4547
}
4648

4749

scripts/python/journal/compile.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def compile_solidity_sources(source_dir, json_dir):
175175
output_file = os.path.join(json_dir, f"{os.path.splitext(filename)[0]}.json")
176176

177177
# Command to compile and save the bytecode in JSON format
178-
command = f"solc --combined-json bin,bin-runtime --pretty-json {input_file} > {output_file} 2> /dev/null"
178+
command = f"solc --combined-json bin,bin-runtime,abi --pretty-json {input_file} > {output_file} 2> /dev/null"
179179

180180
# Execute the compilation command
181181
try:
@@ -189,13 +189,16 @@ def compile_solidity_sources(source_dir, json_dir):
189189

190190
print(f"Compiled successfully {count_success}/{count_success + count_failure} files.")
191191

192-
def extract_and_save_longest_bytecode(bytecode_dir, json_dir, is_ethersolve=False, file_index=None):
192+
def extract_and_save_longest_bytecode(bytecode_dir, json_dir, is_ethersolve=False, file_index=None, abi_dir=None):
193193
"""
194194
Extracts the longest bytecode based on file size from each .json file and saves it in the specified output directory.
195195
"""
196196
# Clear and create bytecode directory
197197
clear_directory(bytecode_dir)
198198
os.makedirs(bytecode_dir, exist_ok=True)
199+
if abi_dir is not None:
200+
clear_directory(abi_dir)
201+
os.makedirs(abi_dir, exist_ok=True)
199202

200203
# List all .json files in the source directory
201204
num_files = [f for f in os.listdir(json_dir) if f.endswith('.json')]
@@ -219,6 +222,7 @@ def extract_and_save_longest_bytecode(bytecode_dir, json_dir, is_ethersolve=Fals
219222
longest_bytecode = None
220223
longest_contract_name = None
221224
max_bytecode_length = 0 # Variable to store the maximum file size
225+
abi = None
222226

223227
# Find the contract with the longest bytecode
224228
for contract_name, contract_data in contracts.items():
@@ -233,23 +237,26 @@ def extract_and_save_longest_bytecode(bytecode_dir, json_dir, is_ethersolve=Fals
233237
max_bytecode_length = bytecode_length
234238
longest_bytecode = bytecode
235239
longest_contract_name = contract_name
240+
abi = contract_data.get("abi")
236241

237242
# Save the longest bytecode, if it exists
238243
if longest_bytecode:
239-
bytecode_filename = os.path.join(
240-
bytecode_dir, f"{os.path.splitext(json_filename)[0]}.bytecode"
241-
)
242-
244+
base_filename = os.path.splitext(json_filename)[0]
245+
243246
if file_index is not None:
244-
file_id = file_index.get(os.path.splitext(json_filename)[0]) # Match string name to integer
245-
# Add a sequential number to the filename
246-
bytecode_filename = os.path.join(
247-
bytecode_dir, f"{file_id}.bytecode"
248-
)
249-
247+
file_id = file_index.get(base_filename) # Match string name to integer
248+
base_filename = str(file_id) if file_id is not None else base_filename
249+
250+
bytecode_filename = os.path.join(bytecode_dir, f"{base_filename}.bytecode")
251+
250252
with open(bytecode_filename, 'w') as bytecode_file:
251253
bytecode_file.write("0x" + longest_bytecode)
252-
# print(f"Extracted longest bytecode from {longest_contract_name} to {bytecode_filename}")
254+
255+
# Save ABI if available
256+
if abi and abi_dir is not None:
257+
abi_filename = os.path.join(abi_dir, f"{base_filename}.abi.json")
258+
with open(abi_filename, 'w') as abi_file:
259+
json.dump(json.loads(abi), abi_file, indent=4)
253260
# Update the progress bar
254261
pbar.update(1)
255262

@@ -326,12 +333,15 @@ def extract_and_save_bytecode(bytecode_dir, json_dir, is_ethersolve=False, file_
326333
if args.longest_bytecode:
327334
# EVMLiSA
328335
extract_and_save_longest_bytecode('./vanilla-solidifi/bytecode/evmlisa',
329-
'./vanilla-solidifi/json')
336+
'./vanilla-solidifi/json',
337+
abi_dir='./vanilla-solidifi/abi/evmlisa')
330338
extract_and_save_longest_bytecode('./reentrancy-solidifi/bytecode/evmlisa',
331-
'./reentrancy-solidifi/json')
339+
'./reentrancy-solidifi/json',
340+
abi_dir='./reentrancy-solidifi/abi/evmlisa')
332341
# TX-ORIGIN
333342
extract_and_save_longest_bytecode('./tx-origin-solidifi/bytecode/evmlisa',
334-
'./tx-origin-solidifi/json')
343+
'./tx-origin-solidifi/json',
344+
abi_dir='./tx-origin-solidifi/abi/evmlisa')
335345

336346
# EtherSolve
337347
extract_and_save_longest_bytecode('./vanilla-solidifi/bytecode/ethersolve',

scripts/python/journal/reentrancy-solidifi/json/buggy_1.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_10.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_11.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_12.json

Lines changed: 0 additions & 41 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_13.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_14.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_15.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

scripts/python/journal/reentrancy-solidifi/json/buggy_16.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)