|
|
@ -36,7 +36,12 @@ def parse_args() -> argparse.Namespace: |
|
|
|
default=False, |
|
|
|
default=False, |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
parser.add_argument("--retry", help="Retry failed query (default 1). Each retry increases the temperature by 0.1", action="store", default=1) |
|
|
|
parser.add_argument( |
|
|
|
|
|
|
|
"--retry", |
|
|
|
|
|
|
|
help="Retry failed query (default 1). Each retry increases the temperature by 0.1", |
|
|
|
|
|
|
|
action="store", |
|
|
|
|
|
|
|
default=1, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Add default arguments from crytic-compile |
|
|
|
# Add default arguments from crytic-compile |
|
|
|
cryticparser.init(parser) |
|
|
|
cryticparser.init(parser) |
|
|
@ -122,45 +127,25 @@ def _handle_codex( |
|
|
|
return None |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-locals |
|
|
|
# pylint: disable=too-many-locals,too-many-arguments |
|
|
|
def _handle_compilation_unit( |
|
|
|
def _handle_function( |
|
|
|
slither: Slither, |
|
|
|
function: Function, |
|
|
|
compilation_unit: SlitherCompilationUnit, |
|
|
|
|
|
|
|
overwrite: bool, |
|
|
|
overwrite: bool, |
|
|
|
force: bool, |
|
|
|
all_patches: Dict, |
|
|
|
|
|
|
|
logging_file: Optional[str], |
|
|
|
|
|
|
|
slither: Slither, |
|
|
|
retry: int, |
|
|
|
retry: int, |
|
|
|
) -> None: |
|
|
|
force: bool, |
|
|
|
|
|
|
|
) -> bool: |
|
|
|
logging_file = str(uuid.uuid4()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for scope in compilation_unit.scopes.values(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO remove hardcoded filtering |
|
|
|
|
|
|
|
if ( |
|
|
|
if ( |
|
|
|
".t.sol" in scope.filename.absolute |
|
|
|
function.source_mapping.is_dependency |
|
|
|
or "mock" in scope.filename.absolute.lower() |
|
|
|
or function.has_documentation |
|
|
|
or "test" in scope.filename.absolute.lower() |
|
|
|
or function.is_constructor_variables |
|
|
|
): |
|
|
|
): |
|
|
|
continue |
|
|
|
return overwrite |
|
|
|
|
|
|
|
prompt = "Create a natpsec documentation for this solidity code with only notice and dev.\n" |
|
|
|
functions_target: List[Function] = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for contract in scope.contracts.values(): |
|
|
|
|
|
|
|
functions_target += contract.functions_declared |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
functions_target += list(scope.functions) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
all_patches: Dict = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for function in functions_target: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if function.source_mapping.is_dependency or function.has_documentation or function.is_constructor_variables: |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
prompt = ( |
|
|
|
|
|
|
|
"Create a natpsec documentation for this solidity code with only notice and dev.\n" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
src_mapping = function.source_mapping |
|
|
|
src_mapping = function.source_mapping |
|
|
|
content = compilation_unit.core.source_code[src_mapping.filename.absolute] |
|
|
|
content = function.compilation_unit.core.source_code[src_mapping.filename.absolute] |
|
|
|
start = src_mapping.start |
|
|
|
start = src_mapping.start |
|
|
|
end = src_mapping.start + src_mapping.length |
|
|
|
end = src_mapping.start + src_mapping.length |
|
|
|
prompt += content[start:end] |
|
|
|
prompt += content[start:end] |
|
|
@ -174,9 +159,9 @@ def _handle_compilation_unit( |
|
|
|
|
|
|
|
|
|
|
|
openai = codex.openai_module() # type: ignore |
|
|
|
openai = codex.openai_module() # type: ignore |
|
|
|
if openai is None: |
|
|
|
if openai is None: |
|
|
|
return |
|
|
|
raise ImportError |
|
|
|
|
|
|
|
|
|
|
|
if slither.codex_log: |
|
|
|
if logging_file: |
|
|
|
codex.log_codex(logging_file, "Q: " + prompt) |
|
|
|
codex.log_codex(logging_file, "Q: " + prompt) |
|
|
|
|
|
|
|
|
|
|
|
tentative = 0 |
|
|
|
tentative = 0 |
|
|
@ -191,12 +176,10 @@ def _handle_compilation_unit( |
|
|
|
max_tokens=slither.codex_max_tokens, |
|
|
|
max_tokens=slither.codex_max_tokens, |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if slither.codex_log: |
|
|
|
if logging_file: |
|
|
|
codex.log_codex(logging_file, "A: " + str(answer)) |
|
|
|
codex.log_codex(logging_file, "A: " + str(answer)) |
|
|
|
|
|
|
|
|
|
|
|
answer_processed = _handle_codex( |
|
|
|
answer_processed = _handle_codex(answer, src_mapping.starting_column, use_tab, force) |
|
|
|
answer, src_mapping.starting_column, use_tab, force |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
if answer_processed: |
|
|
|
if answer_processed: |
|
|
|
break |
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
@ -206,10 +189,48 @@ def _handle_compilation_unit( |
|
|
|
logger.info(answer) |
|
|
|
logger.info(answer) |
|
|
|
|
|
|
|
|
|
|
|
if not answer_processed: |
|
|
|
if not answer_processed: |
|
|
|
|
|
|
|
return overwrite |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
create_patch(all_patches, src_mapping.filename.absolute, start, start, "", answer_processed) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return overwrite |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_compilation_unit( |
|
|
|
|
|
|
|
slither: Slither, |
|
|
|
|
|
|
|
compilation_unit: SlitherCompilationUnit, |
|
|
|
|
|
|
|
overwrite: bool, |
|
|
|
|
|
|
|
force: bool, |
|
|
|
|
|
|
|
retry: int, |
|
|
|
|
|
|
|
) -> None: |
|
|
|
|
|
|
|
logging_file: Optional[str] |
|
|
|
|
|
|
|
if slither.codex_log: |
|
|
|
|
|
|
|
logging_file = str(uuid.uuid4()) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
logging_file = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for scope in compilation_unit.scopes.values(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Dont send tests file |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
".t.sol" in scope.filename.absolute |
|
|
|
|
|
|
|
or "mock" in scope.filename.absolute.lower() |
|
|
|
|
|
|
|
or "test" in scope.filename.absolute.lower() |
|
|
|
|
|
|
|
): |
|
|
|
continue |
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
create_patch( |
|
|
|
functions_target: List[Function] = [] |
|
|
|
all_patches, src_mapping.filename.absolute, start, start, "", answer_processed |
|
|
|
|
|
|
|
|
|
|
|
for contract in scope.contracts.values(): |
|
|
|
|
|
|
|
functions_target += contract.functions_declared |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
functions_target += list(scope.functions) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
all_patches: Dict = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for function in functions_target: |
|
|
|
|
|
|
|
overwrite = _handle_function( |
|
|
|
|
|
|
|
function, overwrite, all_patches, logging_file, slither, retry, force |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# all_patches["patches"] should have only 1 file |
|
|
|
# all_patches["patches"] should have only 1 file |
|
|
@ -242,10 +263,17 @@ def main() -> None: |
|
|
|
logger.info("Be aware of OpenAI ToS: https://openai.com/api/policies/terms/") |
|
|
|
logger.info("Be aware of OpenAI ToS: https://openai.com/api/policies/terms/") |
|
|
|
slither = Slither(args.project, **vars(args)) |
|
|
|
slither = Slither(args.project, **vars(args)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
for compilation_unit in slither.compilation_units: |
|
|
|
for compilation_unit in slither.compilation_units: |
|
|
|
_handle_compilation_unit( |
|
|
|
_handle_compilation_unit( |
|
|
|
slither, compilation_unit, args.overwrite, args.force_answer_parsing, int(args.retry) |
|
|
|
slither, |
|
|
|
|
|
|
|
compilation_unit, |
|
|
|
|
|
|
|
args.overwrite, |
|
|
|
|
|
|
|
args.force_answer_parsing, |
|
|
|
|
|
|
|
int(args.retry), |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
except ImportError: |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
if __name__ == "__main__": |
|
|
|