Skip to content

Commit

Permalink
Unpack 2 new metadata fields added in our datasets. (#157)
Browse files Browse the repository at this point in the history
* Add new metadata fields as json facts.

* remove print

* Rename exporter classes

* fix possible error
  • Loading branch information
sifislag authored Oct 1, 2024
1 parent 7d94d40 commit e6eda48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import abc
import csv
import json
import os
import src.opcodes as opcodes
import src.basicblock as basicblock
Expand Down Expand Up @@ -85,7 +86,7 @@ def export(self):
Exports the source object to an implementation-specific format.
"""

class TsvExporter(Exporter):
class FactExporter(Exporter):
def __init__(self, output_dir: str):
super().__init__(output_dir)

Expand All @@ -97,10 +98,14 @@ def generate(self, filename: str, entries: List[Any]):
writer = csv.writer(f, delimiter='\t', lineterminator='\n')
writer.writerows(entries)

def generate_json(self, filename: str, entries: Any):
with open(self.get_out_file_path(filename), 'w') as f:
json.dump(entries, f)


class InstructionTsvExporter(TsvExporter):
class EVMBlockExporter(FactExporter):
"""
Prints a textual representation of the given CFG to stdout.
Populates the decompiler's fact files (tsv and json) given the low-level evm blocks
Args:
blocks: low-level evm block representation to be output
Expand Down Expand Up @@ -140,6 +145,8 @@ def process_immutable_refs(immutable_refs: Dict[str, List[Dict[str, int]]]) -> L

self.function_debug_data = process_function_debug_data(metadata.get('function_debug_info', {})) if metadata is not None else []
self.immutable_references = process_immutable_refs(metadata.get('immutable_references', {})) if metadata is not None else []
self.abi = metadata.get('abi', {}) if metadata is not None else {}
self.storage_layout = metadata.get('storage_layout', {}) if metadata is not None else {}

def export(self):
"""
Expand Down Expand Up @@ -221,3 +228,5 @@ def link_or_output_signature_file(signatures_filename_in: str, signatures_filena

self.generate('HighLevelFunctionInfo.facts', self.function_debug_data)
self.generate('ImmutableLoads.facts', self.immutable_references)
self.generate_json('source-abi.json', self.abi)
self.generate_json('source-storage-layout.json', self.storage_layout)
2 changes: 1 addition & 1 deletion src/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def generate_facts(self, contract_filename: str, work_dir: str, out_dir: str) ->

disassemble_start = time.time()
blocks = blockparse.EVMBytecodeParser(bytecode).parse()
exporter.InstructionTsvExporter(work_dir, blocks, True, bytecode, metadata).export()
exporter.EVMBlockExporter(work_dir, blocks, True, bytecode, metadata).export()

os.symlink(join(work_dir, 'bytecode.hex'), join(out_dir, 'bytecode.hex'))

Expand Down

0 comments on commit e6eda48

Please sign in to comment.