Skip to content
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

Add instruction for a C++ fuzz target includes a C file from a C++ project #420

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions llm_toolkit/code_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
NO_MEMBER_ERROR_REGEX = r"error: no member named '.*' in '([^':]*):?.*'"
FILE_NOT_FOUND_ERROR_REGEX = r"fatal error: '([^']*)' file not found"
UNKNOWN_TYPE_ERROR = 'error: unknown type name'
COMPILATION_ERROR_FILE_REGEX = (r'^([^\s.:]+?\.(?:c|h)):\d+:\d+:\s+'
r'(?:error|fatal error):\s.*')
INCLUDE_FILE_REGEX = r'^\s*(#include\s*[<"][^">]+[>"])'

# The following strings identify errors when a C fuzz target attempts to use
# FuzzedDataProvider.
Expand Down Expand Up @@ -461,6 +464,8 @@ def _collect_instructions(benchmark: benchmarklib.Benchmark, errors: list[str],
for error in errors:
instruction += _collect_instruction_file_not_found(benchmark, error,
fuzz_target_source_code)
instruction += _collect_instruction_cpp_target_cpp_projet_include_c_file(
benchmark, error, fuzz_target_source_code)
instruction += _collect_instruction_fdp_in_c_target(benchmark, errors,
fuzz_target_source_code)
instruction += _collect_instruction_no_goto(fuzz_target_source_code)
Expand All @@ -470,6 +475,43 @@ def _collect_instructions(benchmark: benchmarklib.Benchmark, errors: list[str],
return instruction


def _collect_instruction_cpp_target_cpp_projet_include_c_file(
benchmark: benchmarklib.Benchmark, error: str,
fuzz_target_source_code: str) -> str:
"""Collects the instructions when a C++ fuzz target include a C file from a
C++ project."""
# The fuzz target is in C++.
if benchmark.language.lower() == benchmarklib.FileType.C.value:
return ''
# The project is in C++.
if benchmark.file_type == benchmarklib.FileType.C:
return ''

# Yet the error is caused by a C file.
match = re.search(COMPILATION_ERROR_FILE_REGEX, error, re.MULTILINE)
if not match:
return ''
file_basename, _ = os.path.splitext(os.path.basename(match.group(1)))

# The fuzz target includes the file.
matches = re.findall(INCLUDE_FILE_REGEX, fuzz_target_source_code,
re.MULTILINE)
matching_include_lines = [line for line in matches if file_basename in line]
if not matching_include_lines:
return ''
if len(matching_include_lines) > 1:
logging.warning(
'_collect_instruction_cpp_target_cpp_projet_include_c_file matched more'
' than 1 line containing %s: %s', file_basename, matching_include_lines)
matching_include_line = matching_include_lines[0]
fixed_include_line = f'extern "C" {{\n{matching_include_line}\n}}\n'
return (
f'IMPORTANT: This statement <code>{matching_include_line}</code> MUST be '
f'replaced with <code>{fixed_include_line}</code> in the generated fuzz '
'target to fix the compilation error above, because the fuzz target is in'
' C++ but the statement includes a file in C.\n')


def _collect_instruction_file_not_found(benchmark: benchmarklib.Benchmark,
error: str,
fuzz_target_source_code: str) -> str:
Expand Down