Skip to content

Commit

Permalink
[Frontend-CPP] Add handling of default constructor call (#1946)
Browse files Browse the repository at this point in the history
* [Frontend-CPP] Add handling of default constructor call

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

* fix formatting

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

---------

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
  • Loading branch information
arthurscchan authored Jan 9, 2025
1 parent df4650c commit 9d8f4e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
9 changes: 8 additions & 1 deletion src/fuzz_introspector/frontends/frontend_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,21 @@ def _process_callsites(stmt: Node) -> list[tuple[str, int, int]]:
ctr_type = stmt.child_by_field_name('type')
if ctr_type:
cls = ctr_type.text.decode()
cls = f'{cls}::{cls}'
cls = f'{cls}::{cls.rsplit("::")[-1]}'
callsites.append(
(cls, stmt.byte_range[1], stmt.start_point.row + 1))

elif stmt.type == 'declaration':
var_type = stmt.child_by_field_name('type').text.decode()
var_name = stmt.child_by_field_name('declarator')

# Handles implicit default constructor call
if var_name.type == 'identifier':
cls = f'{var_type}::{var_type.rsplit("::")[-1]}'
if cls in functions:
callsites.append((cls, stmt.byte_range[1],
stmt.start_point.row + 1))

while var_name.type not in [
'identifier', 'qualified_identifier',
'pointer_declarator', 'array_declarator',
Expand Down
39 changes: 9 additions & 30 deletions src/test/test_frontends_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_tree_sitter_cpp_sample1():
assert len(project.get_source_codes_with_harnesses()) == 1

# Callsite check
assert len(callsites[0].split('\n')) == 6
assert len(callsites[0].split('\n')) == 7
assert (' isPositive '
'src/test/data/source-code/cpp/test-project-1/sample.cpp'
in callsites[0])
Expand Down Expand Up @@ -77,27 +77,9 @@ def test_tree_sitter_cpp_sample3():


def test_tree_sitter_cpp_sample4():
callsites, project = oss_fuzz.analyse_folder(
'c++',
'src/test/data/source-code/cpp/test-project-4',
'LLVMFuzzerTestOneInput',
dump_output=False,
)

# Project check
assert len(project.get_source_codes_with_harnesses()) == 1

# Callsite check
assert len(callsites[0].split('\n')) == 7
assert (' Level1::Level2::Level3::Level4::DeepClass::deepMethod2 '
'src/test/data/source-code/cpp/test-project-4/deep_nested.cc'
in callsites[0])


def test_tree_sitter_cpp_sample5():
_, project = oss_fuzz.analyse_folder(
'c++',
'src/test/data/source-code/cpp/test-project-5',
'src/test/data/source-code/cpp/test-project-4',
'LLVMFuzzerTestOneInput',
dump_output=False,
)
Expand All @@ -107,15 +89,15 @@ def test_tree_sitter_cpp_sample5():
function='LLVMFuzzerTestOneInput',
visited_functions=set())

assert 'ClassOne::processInput' in functions_reached
assert 'NamespaceOne::processInput' in functions_reached
assert 'Level1::Level2::Level3::Level4::DeepClass::deepMethod2' in functions_reached
assert 'printf' in functions_reached
assert 'atoi' in functions_reached


def test_frontend_reachability1():
"""Test reachability of a nested namespace."""
def test_tree_sitter_cpp_sample5():
_, project = oss_fuzz.analyse_folder(
'c++',
'src/test/data/source-code/cpp/test-project-4',
'src/test/data/source-code/cpp/test-project-5',
'LLVMFuzzerTestOneInput',
dump_output=False,
)
Expand All @@ -125,8 +107,5 @@ def test_frontend_reachability1():
function='LLVMFuzzerTestOneInput',
visited_functions=set())

assert 'Level1::Level2::Level3::Level4::DeepClass::deepMethod2' in functions_reached
assert 'printf' in functions_reached

# TODO: revert the below assert. It should be true.
assert 'atoi' not in functions_reached
assert 'ClassOne::processInput' in functions_reached
assert 'NamespaceOne::processInput' in functions_reached

0 comments on commit 9d8f4e6

Please sign in to comment.