-
Notifications
You must be signed in to change notification settings - Fork 1
Python branch2 #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Python branch2 #23
Changes from 133 commits
Commits
Show all changes
135 commits
Select commit
Hold shift + click to select a range
2e64b43
Upload the python project Fuzz test script
joyguoguo 16338dc
feat: Add OSS-Fuzz submodule tracking main branch
joyguoguo ca103e9
chore: Switch oss-fuzz submodule to personal fork
joyguoguo f39e727
Switch oss-fuzz submodule to personal fork
joyguoguo d691eea
move the valid_project file
joyguoguo 25b0191
move the .py file
joyguoguo fcf80a9
create build_oss_fuzz.py
joyguoguo 3e8e7f4
create run_fuzz_target.py
joyguoguo def645e
split the pool.py into build_oss_fuzz and run_fuzz_target
joyguoguo 1251bcd
delete the .sh files
joyguoguo 21017f1
translate to english
joyguoguo bb5f14a
fuzz_runner_pool.py:74
joyguoguo 1b9b010
edit stdout
joyguoguo 49e9ddd
添加空值检查
joyguoguo 6e5221d
modify stdout, delete pool.py
joyguoguo 4a5befa
indentation level check
joyguoguo 7e6c1a2
Remove build log write files
joyguoguo a7e447e
Remove build log write files
joyguoguo fb4da2a
use logging mdule
joyguoguo e582af6
use precise logging
joyguoguo 02f9269
use logging
joyguoguo 15a7095
use precise exception log info
joyguoguo 9a471aa
correct type problems
joyguoguo 026614b
correct some mistakes
joyguoguo 74f44dc
correct some mistakes
joyguoguo 413c2dd
correct
joyguoguo c8b755d
modify discover fuzz target
joyguoguo 4b92185
modify the oss-fuzz dir
joyguoguo a8e58e8
Redirect the output to an empty device without retaining any output
joyguoguo ae7e7c1
add always yes
joyguoguo 2dc8f97
split the build script
joyguoguo ebb68c9
split the build script
joyguoguo 5b12877
build scripts test successfully
joyguoguo 40588d4
build.py
joyguoguo 5b52393
collect targets first and then run
joyguoguo 8285e3f
list, tuple, ptional
joyguoguo 7e9add9
list,tuple,optional
joyguoguo 6afc919
translate
joyguoguo ea76d9d
build_fuzz.py, run_fuzz_all_target.py
joyguoguo b0f7b86
correct
joyguoguo 19fa83e
original
joyguoguo 47cf6e9
record input
joyguoguo 76c63ac
Fatal error in main program: cannot unpack non-iterable NoneType object
joyguoguo 1c608dc
name 'target_functions' is not defined
joyguoguo 34ef833
准备大改
joyguoguo 510cbe7
create modify file script add"print(data)" to each fuzz_.py
joyguoguo 61009ab
build_fuzzer script
joyguoguo d3bbc13
modify tuple dict list
joyguoguo 62847f3
remove stdout stderr in build fuzz
joyguoguo 4a5f9ff
test successfully
joyguoguo 3f736cb
rename run fuzz ds to run fuzz print1
joyguoguo d5c2b27
add print(data ) to fuzz target and rename the file with "_print1"
joyguoguo d74a6bf
oss -fuzz change
joyguoguo c6a6c23
rename the print1.py
joyguoguo 9d5c9b6
modify the exegesis
joyguoguo 521045e
modify
joyguoguo 6c18832
modify log name
joyguoguo 679ec48
type error
joyguoguo b4d1811
list dict tuple
joyguoguo d030841
type error
joyguoguo 3f9df9d
construct errors module
joyguoguo 85b7904
run_command module
joyguoguo 40102b4
combine the run_command instrument to one file
joyguoguo e0c7740
remove the run_command
joyguoguo 0631504
modify
joyguoguo 5e28b5c
mytype check
joyguoguo 368c0e4
mytype
joyguoguo 1be1dfa
mytype
joyguoguo 09ba145
mytype
joyguoguo 7a1f248
translate
joyguoguo 6a062bb
remove run command
joyguoguo 26eceeb
timeout - shell instrument
joyguoguo 7e91c6c
correct in out error and return Popen directly
joyguoguo f9edfaf
ready to change from rust script
joyguoguo 3821133
修改build_image
joyguoguo 6e5c764
y/n
joyguoguo 0029059
correct repo_id and repo_name in main
joyguoguo 1d815eb
test build_image 构建日志
joyguoguo 4333456
add build_fuzzer
joyguoguo 3a0565f
fuzz and testgen
joyguoguo 779fb40
correct run_one_target
joyguoguo 0afec3b
fuzz ok
joyguoguo ba61ca1
transform
joyguoguo 58d1f76
testgen need to ^ help: add `;` here
joyguoguo 9236822
test successful
joyguoguo 310b079
example output project
joyguoguo 5ceb873
type error
joyguoguo d31a333
English ver
joyguoguo 6717dad
delete privious scripts
joyguoguo 1266606
python template
joyguoguo d42927f
python template
joyguoguo f8941f1
correct the template
joyguoguo b357a52
ver2 wrong template
joyguoguo 1637e23
ok
joyguoguo 85b6ed9
testgen file change into copy the original and then add input_data =b""
joyguoguo 618d156
only read b' ' inputs
joyguoguo 5a7f513
remove transform
joyguoguo d5fd84d
clean the inputs and testgen
joyguoguo bcc22b0
set max_file
joyguoguo 020c970
max input file
joyguoguo 8787982
input b""
joyguoguo 606c34f
modify the method of writing files into PIPE
joyguoguo 503063c
use max total time; remove size monitor
joyguoguo 708c8a9
修改并行错误, 写入方法还是直接写入文件 延时控制为max total time
joyguoguo f417e19
补充日志输出
joyguoguo 2a27db9
模板生成成功
joyguoguo f583702
testgen完成
joyguoguo 68c656a
删除冗余, 修改代码
joyguoguo 60fbb7a
更换为未删除冗余版本
joyguoguo 641998d
template插入data=b""
joyguoguo 73aac90
translation
joyguoguo 4c94beb
A complete script for building the processes of build_image, build_fu…
joyguoguo 08bc375
Resolved conflicts with main branch while preserving python-branch2 c…
joyguoguo 5a47088
delete some imports
joyguoguo a16d664
use ASTfor transform and testgen
joyguoguo 730e458
use AST
joyguoguo 40e3807
Set up command line arguments
joyguoguo e3e5546
use fire
joyguoguo ab6813d
use FIre
joyguoguo f2c7485
black formatter
joyguoguo 2ebcee3
deal the data after closing the file
joyguoguo 7aa3f03
when doing line-matching, check for # This is a test template in the …
joyguoguo 11ca42f
when doing line-matching, check for # This is a test template in the …
joyguoguo a0bbe56
delete UnicodeDecodeError
joyguoguo 0067af3
apply transformations on the original unmodified fuzz targets.
joyguoguo f456add
put all AST related class/module/function in another file and import…
joyguoguo 509a4c4
put all AST related class/module/function in another file and import…
joyguoguo 1686058
translation
joyguoguo dd0a8ab
use relative address
joyguoguo b9956f3
use relative address
joyguoguo e771b99
remove the class outside of the function
joyguoguo 4bc7c94
add tuple's type
joyguoguo e527188
Properly handle indentation and process data after the file is closed.
joyguoguo bb2d9e8
correct tne relative path
joyguoguo 8eb4971
add black to requirements.txt
joyguoguo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| # ast_utils.py | ||
| import ast | ||
| import astunparse | ||
| import logging | ||
| import os | ||
| import re | ||
|
|
||
|
|
||
| class TestFunctionTransformer(ast.NodeTransformer): | ||
| """AST transformer for test function conversion""" | ||
|
|
||
| def visit_FunctionDef(self, node): | ||
| # First, process main function (remove it) | ||
| if node.name == "main": | ||
| return None | ||
|
|
||
| # Process TestInput/TestOneInput functions | ||
| if node.name in ["TestInput", "TestOneInput"]: | ||
| # a. Record parameter name (assume only one parameter) | ||
| param_name = None | ||
| if node.args.args: | ||
| param_name = node.args.args[0].arg | ||
|
|
||
| # b. Rename function to test_ | ||
| node.name = "test_" | ||
|
|
||
| # c. Remove parameters (set argument list to empty) | ||
| node.args = ast.arguments( | ||
| posonlyargs=[], | ||
| args=[], | ||
| vararg=None, | ||
| kwonlyargs=[], | ||
| kw_defaults=[], | ||
| kwarg=None, | ||
| defaults=[], | ||
| ) | ||
|
|
||
| # d. Insert param_name = b"" at the beginning of the function body | ||
| if param_name: | ||
| self.add_param_assignment(node, param_name) | ||
|
|
||
| # Ensure traversing child nodes continues | ||
| self.generic_visit(node) | ||
| return node | ||
|
|
||
| def add_param_assignment(self, node, param_name): | ||
| """Add param_name = b"..." at the beginning of the function body with an inline comment""" | ||
| # Create a compound value containing assignment and comment | ||
| value_with_comment = ast.JoinedStr( | ||
| values=[ | ||
| ast.FormattedValue(value=ast.Constant(value=b""), conversion=-1), | ||
| ast.Constant(value=" # This is a test template"), | ||
| ] | ||
| ) | ||
|
|
||
| # Create an assignment node | ||
| assign_node = ast.Assign( | ||
| targets=[ast.Name(id=param_name, ctx=ast.Store())], value=value_with_comment | ||
| ) | ||
|
|
||
| # If there is a docstring, insert after the docstring | ||
| if ( | ||
| node.body | ||
| and isinstance(node.body[0], ast.Expr) | ||
| and isinstance(node.body[0].value, ast.Constant) | ||
| and isinstance(node.body[0].value.value, str) | ||
| ): | ||
| # Insert right after the docstring | ||
| node.body.insert(1, assign_node) | ||
| else: | ||
| # Insert at the beginning of the function | ||
| node.body.insert(0, assign_node) | ||
|
|
||
| def remove_print_param(self, node, param_name): | ||
| """Remove print statements for the specific parameter""" | ||
| new_body = [] | ||
| for stmt in node.body: | ||
| # Skip print(param_name) calls | ||
| if ( | ||
| isinstance(stmt, ast.Expr) | ||
| and isinstance(stmt.value, ast.Call) | ||
| and isinstance(stmt.value.func, ast.Name) | ||
| and stmt.value.func.id == "print" | ||
| and any( | ||
| isinstance(arg, ast.Name) and arg.id == param_name | ||
| for arg in stmt.value.args | ||
| ) | ||
| ): | ||
| continue | ||
| new_body.append(stmt) | ||
| node.body = new_body | ||
|
|
||
| def visit_If(self, node): | ||
| """Remove if __name__ == '__main__' blocks""" | ||
| # Check if this is the main function guard | ||
| if ( | ||
| isinstance(node.test, ast.Compare) | ||
| and isinstance(node.test.left, ast.Name) | ||
| and node.test.left.id == "__name__" | ||
| and isinstance(node.test.ops[0], ast.Eq) | ||
| and isinstance(node.test.comparators[0], ast.Constant) | ||
| and node.test.comparators[0].value == "__main__" | ||
| ): | ||
|
|
||
| # Remove the entire if block | ||
| return None | ||
|
|
||
| # Ensure traversing child nodes continues | ||
| self.generic_visit(node) | ||
| return node | ||
|
|
||
|
|
||
| class TestGenTransformer(ast.NodeTransformer): | ||
| def __init__(self, idx, fuzz_input): | ||
| self.idx = idx | ||
| self.fuzz_input = fuzz_input | ||
| self.found_test_function = False | ||
|
|
||
| def visit_FunctionDef(self, node): | ||
| if node.name == "test_": | ||
| self.found_test_function = True | ||
|
|
||
| # 1. Modify function name | ||
| node.name = f"test_{self.idx}" | ||
|
|
||
| # 2. Find and replace assignment statements with the special comment | ||
| for i, stmt in enumerate(node.body): | ||
| # Check if it's an assignment statement | ||
| if isinstance(stmt, ast.Assign): | ||
| # Check if the value is a compound value with a comment | ||
| if ( | ||
| isinstance(stmt.value, ast.JoinedStr) | ||
| and len(stmt.value.values) >= 2 | ||
| and isinstance(stmt.value.values[1], ast.Constant) | ||
| and stmt.value.values[1].value == " # This is a test template" | ||
| ): | ||
| # Replace with new fuzz input | ||
| stmt.value = ast.Constant(value=self.fuzz_input) | ||
| break | ||
| return node | ||
|
|
||
|
|
||
| def generate_test_template(target_name: str, repo_path: str): | ||
| """ | ||
| Generate Python test template using AST for more precise code transformations | ||
| """ | ||
| src_file = os.path.join(repo_path, target_name) | ||
| logging.info(f"Generating test template for {src_file}") | ||
| if not src_file.endswith(".py"): | ||
| src_file += ".py" | ||
| if not os.path.exists(src_file): | ||
| logging.error(f"Source target file not found: {src_file}") | ||
| return None | ||
|
|
||
| with open(src_file, "r", encoding="utf-8") as f: | ||
| original_code = f.read() | ||
|
|
||
| # --- 1. Keep shebang but remove license comments --- | ||
| shebang = "" | ||
| if original_code.startswith("#!"): | ||
| shebang, original_code = original_code.split("\n", 1) | ||
| shebang += "\n" | ||
|
|
||
| license_pattern = re.compile( | ||
| r"^(?:\s*#.*\n)*\s*#.*limitations\s+under\s+the\s+license.*\n", | ||
| re.IGNORECASE | re.MULTILINE, | ||
| ) | ||
| code_no_license = re.sub(license_pattern, "", original_code, count=1) | ||
|
|
||
| # --- 2. Parse code to AST --- | ||
| try: | ||
| tree = ast.parse(code_no_license) | ||
| except SyntaxError as e: | ||
| logging.error(f"Syntax error in {src_file}: {e}") | ||
| return None | ||
|
|
||
| # --- 3. AST transformation --- | ||
| transformer = TestFunctionTransformer() | ||
| new_tree = transformer.visit(tree) | ||
| ast.fix_missing_locations(new_tree) | ||
|
|
||
| # --- 4. Generate cleaned code --- | ||
| cleaned_code = astunparse.unparse(new_tree) | ||
|
|
||
| # --- 5. Output to tests-gen directory --- | ||
| template_dir = os.path.join(repo_path, "tests-gen") | ||
| os.makedirs(template_dir, exist_ok=True) | ||
|
|
||
| init_path = os.path.join(template_dir, "__init__.py") | ||
| if not os.path.exists(init_path): | ||
| with open(init_path, "w", encoding="utf-8") as f: | ||
| f.write("") | ||
|
|
||
| # Use the base part of target_name (remove extension) as the output file name | ||
| base_target_name = os.path.splitext(target_name)[0] | ||
| template_path = os.path.join(template_dir, f"{base_target_name}.py") | ||
| with open(template_path, "w", encoding="utf-8") as f: | ||
| f.write(shebang + cleaned_code.strip() + "\n") | ||
|
|
||
| logging.info(f"Generated cleaned template: {template_path}") | ||
| return template_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import shutil | ||
| import fire | ||
|
|
||
| def clean_project_dirs(root_dir="/fuzz/oss-fuzz/projects"): | ||
| """ | ||
| 清理 oss-fuzz 项目目录下的 fuzz_inputs 和 tests-gen 文件夹 | ||
|
|
||
| Args: | ||
| root_dir (str): 项目的根目录路径 | ||
| """ | ||
| removed_files = 0 | ||
| removed_dirs = 0 | ||
|
|
||
| for project in os.listdir(root_dir): | ||
| project_path = os.path.join(root_dir, project) | ||
| if not os.path.isdir(project_path): | ||
| continue | ||
|
|
||
| # 删除 fuzz_inputs 文件夹 | ||
| fuzz_inputs_path = os.path.join(project_path, "fuzz_inputs") | ||
| if os.path.isdir(fuzz_inputs_path): | ||
| shutil.rmtree(fuzz_inputs_path) | ||
| print(f"🗑️ Removed dir: {fuzz_inputs_path}") | ||
| removed_dirs += 1 | ||
|
|
||
| # 删除 tests-gen 文件夹 | ||
| tests_gen_path = os.path.join(project_path, "tests-gen") | ||
| if os.path.isdir(tests_gen_path): | ||
| shutil.rmtree(tests_gen_path) | ||
| print(f"🗑️ Removed dir: {tests_gen_path}") | ||
| removed_dirs += 1 | ||
|
|
||
| # 如果需要删除 .inputs.py 文件,取消注释以下代码 | ||
| # for fname in os.listdir(project_path): | ||
| # if fname.endswith(".inputs.py"): | ||
| # file_path = os.path.join(project_path, fname) | ||
| # os.remove(file_path) | ||
| # print(f"🗑️ Removed file: {file_path}") | ||
| # removed_files += 1 | ||
|
|
||
| print(f"\n✅ Done. Removed {removed_files} files and {removed_dirs} directories.") | ||
|
|
||
| if __name__ == "__main__": | ||
| fire.Fire(clean_project_dirs) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fuzz/oss-fuzz/projectsis the relative path. Please see https://www.geeksforgeeks.org/linux-unix/absolute-relative-pathnames-unix/