-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add C frontend tests and move CPP test data (#1944)
* move tests around Signed-off-by: David Korczynski <david@adalogics.com> * add C test for new frontend Signed-off-by: David Korczynski <david@adalogics.com> --------- Signed-off-by: David Korczynski <david@adalogics.com>
- Loading branch information
1 parent
1802e81
commit 9cd9037
Showing
14 changed files
with
174 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,91 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
int unreached_target2(const uint8_t *data) { | ||
return 5; | ||
} | ||
|
||
char *global1 = "FUZZCAFE"; | ||
char *global2 = "FUZZKEYWORD"; | ||
int GLB2 = 0xbeef; | ||
|
||
void unreached_target10(char *val) { | ||
if (strcmp(val, global1) == 0) { | ||
printf("Compare 1\n"); | ||
} | ||
if (strcmp(val, global2) == 0) { | ||
printf("Compare 15\n"); | ||
} | ||
if (((int*)val) == GLB2) { | ||
printf("Compare 3\n"); | ||
} | ||
if (strcmp(val, "RABBIT") == 0) { | ||
printf("Compare 4\n"); | ||
} | ||
printf("Compare 2\n"); | ||
} | ||
|
||
int unreached_target1(const uint8_t *data) { | ||
if (data[0] == 0x11) { | ||
return unreached_target2(data); | ||
} | ||
char *mc = (char*)malloc(12); | ||
if (data[0] == 0x12) { | ||
unreached_target10((char*)data); | ||
return 0; | ||
} | ||
return 5; | ||
} | ||
|
||
int un(char *n1, char *n2, char *n3, char *n5, size_t s1) { | ||
return 0; | ||
} | ||
|
||
int unreached_target3(const uint8_t *data, size_t *theval) { | ||
if (data[0] == 0x11) { | ||
return unreached_target1(data); | ||
} | ||
|
||
return 5; | ||
} | ||
|
||
char *d = "sf"; | ||
int target2(const uint8_t *data) { | ||
if (data[0] == 0x41) return 1; | ||
unreached_target1(d); | ||
return 2; | ||
} | ||
|
||
int target3(const uint8_t *data) { | ||
if (data[0] == 0x42) return 4; | ||
return 3; | ||
} | ||
|
||
int fuzz_entry(const uint8_t *data, size_t size) { | ||
int ret; | ||
if (size == 2) { | ||
ret = target2(data); | ||
} | ||
else if (size == 3) { | ||
ret = target3(data); | ||
} | ||
else { | ||
ret = 1; | ||
} | ||
return ret; | ||
} | ||
|
||
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||
if (size < 10) { | ||
return 0; | ||
} | ||
char *kldfj = (char*)malloc(123); | ||
char *nt = malloc(size+1); | ||
memcpy(nt, data, size); | ||
nt[size] = '\0'; | ||
fuzz_entry(nt, size); | ||
return 0; | ||
} | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,35 @@ | ||
# Copyright 2025 Fuzz Introspector Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Unit testing script for the C frontend""" | ||
|
||
from fuzz_introspector.frontends import oss_fuzz # noqa: E402 | ||
|
||
|
||
def test_simple_sample1(): | ||
callsites, project = oss_fuzz.analyse_folder( | ||
language='c', | ||
directory='src/test/data/source-code/c/simple-sample-1/', | ||
entrypoint='LLVMFuzzerTestOneInput', | ||
) | ||
|
||
functions_reached = project.get_reachable_functions( | ||
source_code=None, | ||
function='LLVMFuzzerTestOneInput', | ||
visited_functions=set()) | ||
|
||
assert 'target3' in functions_reached | ||
assert 'unreached_target3' not in functions_reached | ||
|
||
# Project check | ||
assert len(project.get_source_codes_with_harnesses()) == 1 |
This file contains 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