Skip to content

Commit 1d192fc

Browse files
committed
Refactor the example project
- add explore_me with both simple and complex conditions - add the automotive example into own directory - improve the README.md
1 parent 59c1d8b commit 1d192fc

36 files changed

+594
-389
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
build
22
.vscode
3-
.idea
3+
.idea
4+
cmake-build-debug
5+
6+
/**/.cifuzz-*
7+
/**/*fuzzer_inputs

CMakeLists.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.16)
22

3-
project(AUTOMOTIVE-FUZZING-EXAMPLE)
3+
project(cpp-demo)
44

5-
add_library(AUTOMOTIVE-FUZZING-EXAMPLE
6-
modules/crypto_module/src/crypto_module_1.c
7-
modules/crypto_module/src/crypto_module_2.c
8-
modules/time_module/src/time_module_1.c
9-
modules/GPS_module/src/GPS_module_1.c
10-
modules/key_management_module/src/key_management_module_1.c
11-
)
5+
# Export compilation database
6+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
127

13-
target_include_directories(AUTOMOTIVE-FUZZING-EXAMPLE PRIVATE
14-
modules/crypto_module/src
15-
modules/time_module/src
16-
modules/key_management_module/src
17-
modules/GPS_module/src
18-
)
8+
set(CMAKE_CXX_STANDARD 14)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
11+
# External dependencies
12+
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/external)
13+
14+
enable_testing()
15+
include(googletest)
16+
17+
add_subdirectory(src/explore_me)
18+
add_subdirectory(src/automotive)

README.md

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,29 @@
1-
# automotive-fuzzing-example
2-
For the demo:
3-
- Initialize Project
4-
- Create fuzz test for a function
5-
- To compile it the "extern" functions need to be implemented for this use the scripts in fuzzing/auto-mock-fuzz:
6-
- ```python3 gen_template.py /path_to_project/automotive-fuzzing-example/modules/*/src/*.c /path_to_project/automotive-fuzzing-example/modules/*/src/*.h```
7-
- This will create two excel sheets. The Sheet called testgen_mocks.xlsx will contain information about the functions that are declared as extern
8-
- Fill in the excel sheet like this:
9-
10-
| int GPS_driver_obtain_current_position(uint8_t * position_as_bytes, uint8_t * hmac_as_bytes) | return: RETURN_INT(int) | position_as_bytes: WRITE_BYTES(12) | hmac_as_bytes: WRITE_BYTES(64) | | |
11-
|---------------------------------------------------------------------------------------------------------------------------|-----------------------------|------------------------------------|--------------------------------|------------------------|-----------------------|
12-
| int third_party_library_calc_hmac(uint8_t * const message, int len, char * const key, char * const nonce, uint8_t * hmac) | return: RETURN_INT(int) | message: WRITE_BYTES(len) | key: WRITE_BYTES(64) | nonce: WRITE_BYTES(64) | hmac: WRITE_BYTES(64) |
13-
| uint8_t HSM_get_random_byte() | return: RETURN_INT(uint8_t) | | | | |
14-
| int driver_get_current_time() | return: RETURN_INT(int) | | | | |
15-
- Run the second script to generate the mocking library from this:
16-
- ```python3 gen_tests.py mocklib gen_template/testgen_mocks.xlsx ../mocks```
17-
- This creates mocklib.h and mocklib.cpp in fuzzing/mocks
18-
- Add the mocklib.cpp to the compiler options and also add the include path fuzzing/mocks
19-
- In the fuzztest you need to create a FuzzedDataProvider object and give a pointer to it to the mocking library. Add the following to the beginning of the FUZZ function:
20-
```FuzzedDataProvider fuzz_data(Data, Size);```
21-
```mocklib_set_data(&fuzz_data);```
22-
- You also need to include the FuzzedDataProvider.h and mocklib.h in the fuzztest
23-
- Now the fuzz test can run
24-
- To create a fuzz test for all the functions fill in the excel sheet testgen_functions.xlsx like this:
25-
26-
| enum crypto_return_status crypto_calculate_hmac(const uint8_t * message, int len, crypto_hmac * hmac) | message: ARG_DATA() | len: ARG_SIZE() | hmac: ARG_STRUCT_PTR(crypto_hmac) | |
27-
|-------------------------------------------------------------------------------------------------------|----------------------------------------|--------------------|-----------------------------------|---|
28-
| enum crypto_return_status crypto_set_key(crypto_key key) | key: ARG_STRUCT(crypto_key) | | | |
29-
| enum crypto_return_status crypto_set_nonce(crypto_nonce nonce) | nonce: ARG_STRUCT(crypto_nonce) | | | |
30-
| enum crypto_return_status crypto_verify_hmac(const uint8_t * message, int len, crypto_hmac * hmac) | message: ARG_DATA() | len: ARG_SIZE() | hmac: ARG_STRUCT_PTR(crypto_hmac) | |
31-
| enum crypto_return_status crypto_verify_key(crypto_key key) | key: ARG_STRUCT(crypto_key) | | | |
32-
| enum crypto_return_status crypto_verify_nonce(crypto_nonce * nonce) | nonce: ARG_STRUCT_PTR(crypto_nonce) | | | |
33-
| uint8_t * generate_random_bytes(uint8_t * buffer, uint8_t length) | buffer: ARG_DATA() | length: ARG_SIZE() | | |
34-
| enum GPS_return_status get_current_position(GPS_position * position) | position: ARG_STRUCT_PTR(GPS_position) | | | |
35-
| void key_management_create_key(uint8_t * key, uint8_t length) | key: ARG_DATA() | length: ARG_SIZE() | | |
36-
| void key_management_create_nonce(uint8_t * nonce, uint8_t length) | nonce: ARG_DATA() | length: ARG_SIZE() | | |
37-
| enum GPS_return_status set_destination_postition(GPS_position position) | position: ARG_STRUCT(GPS_position) | | | |
38-
| enum crypto_state crypto_get_state() | | | | |
39-
| void crypto_init() | | | | |
40-
| GPS_position get_destination_position() | | | | |
41-
| enum GPS_return_status init_crypto_module() | | | | |
42-
| int time_current_time() | | | | |
43-
- Then generate the fuzz test with the following command:
44-
- ```python3 gen_tests.py fuzztests gen_template/testgen_functions.xlsx .```
45-
- This will create a file fuzztest.c. Copy its content to your own fuzztest
46-
- Include crypto_module_types.h and GPS_module_types.h in the fuzztest
47-
- Run the fuzztest
1+
<a href="https://www.code-intelligence.com/">
2+
<img src="https://www.code-intelligence.com/hubfs/Logos/CI%20Logos/Logo_quer_white.png" alt="Code Intelligence logo" width="450px">
3+
</a>
484

5+
# Testing C/C++ for Security and Reliability
6+
Building robust C/C++ applications is a highly challenging endeavor that requires thorough testing.
7+
While C/C++ enables us to write high-performance code, the memory-unsafety nature of the language
8+
brings a broad spectrum of security risks. Memory corruption issues constitute the vast majority of
9+
bugs and security vulnerabilities found in C/C++ projects, and their impact is best demonstrated by the
10+
[Heartbleed](https://en.wikipedia.org/wiki/Heartbleed) bug on OpenSSL.
11+
Regular unit and integration tests are essential to test that our code functions correctly,
12+
they are not enough to uncover memory-corruption bugs.
13+
On the other hand, fuzz testing has established itself as the best practical method to find these
14+
issues in large code bases such as Google Chrome.
4915

16+
In this example, we demonstrate how you can use CI Fuzz to integrate fuzz testing into your
17+
C/C++ projects. The example project uses [CMake](https://cmake.org/) as the build system and contains
18+
the following three use cases:
19+
* [Simple Checks Example](src/explore_me/explore_me.cpp#L10):
20+
A simple example that triggers a buffer over when the input parameters satisfy certain criteria.
21+
We show that CI Fuzz can quickly generate a test case that trigger this bug.
22+
* [Complex Checks Example](src/explore_me/explore_me.cpp#L22):
23+
A more complex example that triggers a use-after-free bug when the input parameters satisfy
24+
certain criteria. In this example, the checks are more complex and involve Base64 encoding
25+
and XORing with constant value, making it more challenging to find the correct combination of
26+
input parameters that trigger the bug.
27+
* [Automotive Example](src/automotive):
28+
An example that demonstrates the challenges of creating high-quality fuzz tests for complex
29+
projects with a large public API. We demonstrate how we can automate most of this task with CI Spark.

cmake/external/googletest.cmake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
set(GTEST_TARGET external.googletest)
2+
set(GTEST_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/${GTEST_TARGET})
3+
4+
set(GTEST_INCLUDE_DIRS ${GTEST_INSTALL_DIR}/include)
5+
include_directories(${GTEST_INCLUDE_DIRS})
6+
7+
set(GTEST_LIBRARIES gtest gmock)
8+
set(GTEST_MAIN_LIBRARIES gtest_main)
9+
set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
10+
11+
foreach(lib IN LISTS GTEST_BOTH_LIBRARIES)
12+
if (MSVC)
13+
if (CMAKE_BUILD_TYPE MATCHES Debug)
14+
set(LIB_PATH ${GTEST_INSTALL_DIR}/lib/${lib}d.lib)
15+
else()
16+
set(LIB_PATH ${GTEST_INSTALL_DIR}/lib/${lib}.lib)
17+
endif()
18+
else()
19+
set(LIB_PATH ${GTEST_INSTALL_DIR}/lib/lib${lib}.a)
20+
endif()
21+
list(APPEND GTEST_BUILD_BYPRODUCTS ${LIB_PATH})
22+
23+
add_library(${lib} STATIC IMPORTED)
24+
set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION
25+
${LIB_PATH})
26+
add_dependencies(${lib} ${GTEST_TARGET})
27+
endforeach(lib)
28+
29+
include (ExternalProject)
30+
ExternalProject_Add(${GTEST_TARGET}
31+
PREFIX ${GTEST_TARGET}
32+
GIT_REPOSITORY https://github.com/google/googletest.git
33+
GIT_TAG v1.14.0
34+
UPDATE_COMMAND ""
35+
CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
36+
-DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
37+
-DCMAKE_C_COMPILER_LAUNCHER:FILEPATH=${CMAKE_C_COMPILER_LAUNCHER}
38+
-DCMAKE_CXX_COMPILER_LAUNCHER:FILEPATH=${CMAKE_CXX_COMPILER_LAUNCHER}
39+
CMAKE_ARGS ${CMAKE_ARGS}
40+
-DCMAKE_INSTALL_PREFIX=${GTEST_INSTALL_DIR}
41+
-DCMAKE_INSTALL_LIBDIR=lib
42+
BUILD_BYPRODUCTS ${GTEST_BUILD_BYPRODUCTS}
43+
)

modules/GPS_module/src/GPS_module_1.c

Lines changed: 0 additions & 55 deletions
This file was deleted.

modules/GPS_module/src/GPS_module_1.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

modules/GPS_module/src/GPS_module_types.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

modules/crypto_module/src/crypto_module_1.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

modules/crypto_module/src/crypto_module_1.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)