Skip to content

Commit 95f8b8a

Browse files
committed
changed example, added pipeline for testing
1 parent e232644 commit 95f8b8a

4 files changed

Lines changed: 87 additions & 16 deletions

File tree

.github/workflows/build_cmake.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,48 @@ jobs:
187187
- name: Build
188188
shell: bash
189189
run: cmake --build --preset build-gcc-arm-${{ matrix.buildtype }}
190+
191+
linux-fuzzing:
192+
name: ${{ matrix.os }}, ${{ matrix.compiler.name }}, C++${{ matrix.cxx }}, ${{ matrix.buildtype }}
193+
runs-on: ${{ matrix.os }}
194+
strategy:
195+
fail-fast: false # 'false' means Don't stop matrix workflows even if some matrix entry fails.
196+
matrix:
197+
os: [ubuntu-22.04]
198+
buildtype: [debug]
199+
compiler: [{name: 'Clang 15', preset: clang-15, pkgs: 'clang-15 llvm-15'}]
200+
cxx: [20]
201+
202+
steps:
203+
- uses: actions/checkout@v3
204+
205+
- name: Cache
206+
uses: actions/cache@v3
207+
env:
208+
cache-name: cache-conan-modules
209+
with:
210+
path: |
211+
${{ env.CONAN_USER_HOME }}
212+
~/.cache/pip
213+
key: ${{ runner.os }}-${{ env.BUILD_TYPE }}-${{ hashFiles('CMakeLists.txt') }}-${{ hashFiles('cmake/Conan.cmake') }}
214+
215+
- name: Install conan
216+
shell: bash
217+
run: |
218+
python3 -m pip install --upgrade pip setuptools conan==1.59
219+
source ~/.profile
220+
221+
- name: Install dependencies
222+
run: |
223+
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
224+
sudo apt update
225+
sudo apt install ninja-build ${{ matrix.compiler.pkgs }}
226+
shell: bash
227+
228+
- name: Configure via CMake
229+
shell: bash
230+
run: cmake --preset unixlike-${{ matrix.compiler.preset }}-${{ matrix.buildtype }}
231+
232+
- name: Build
233+
shell: bash
234+
run: cmake --build --preset build-unixlike-${{ matrix.compiler.preset }}-${{ matrix.buildtype }}

CMakePresets.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@
350350
"CMAKE_TRY_COMPILE_TARGET_TYPE": "STATIC_LIBRARY",
351351
"CMAKE_CROSSCOMPILING": "TRUE"
352352
}
353+
},
354+
{
355+
"name": "clang-15-fuzzing",
356+
"description": "Clang 15 fuzzer test",
357+
"binaryDir": "${sourceDir}/out/build/${presetName}",
358+
"installDir": "${sourceDir}/out/install/${presetName}",
359+
"cacheVariables": {
360+
"CMAKE_C_COMPILER": "clang-15",
361+
"CMAKE_CXX_COMPILER": "clang++-15",
362+
"CMAKE_BUILD_TYPE": "Debug",
363+
"ENABLE_COVERAGE": "ON",
364+
"ENABLE_SANITIZER_ADDRESS": "ON",
365+
"ENABLE_SANITIZER_LEAK": "ON",
366+
"ENABLE_SANITIZER_UNDEFINED_BEHAVIOR": "ON",
367+
"ENABLE_SANITIZER_MEMORY": "OFF",
368+
"ENABLE_FUZZING": "ON"
369+
}
353370
}
354371
],
355372
"buildPresets": [
@@ -444,6 +461,10 @@
444461
{
445462
"name": "build-gcc-arm-release",
446463
"configurePreset": "gcc-arm-release"
464+
},
465+
{
466+
"name": "build-clang-15-fuzzing",
467+
"configurePreset": "clang-15-fuzzing"
447468
}
448469
],
449470
"testPresets": [
@@ -612,6 +633,13 @@
612633
"inherits": "test-common",
613634
"configuration": "Release",
614635
"configurePreset": "win32-gcc-x64-mingw-release"
636+
},
637+
{
638+
"name": "test-clang-15-fuzzing",
639+
"displayName": "Strict",
640+
"description": "Enable output and stop on failure",
641+
"inherits": "test-common",
642+
"configurePreset": "clang-15-fuzzing"
615643
}
616644
]
617645
}

fuzz_test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TARGET_COMPILE_OPTIONS(fuzz_tester PRIVATE -fsanitize=fuzzer,undefined,address)
1313

1414
# Allow short runs during automated testing to see if something new breaks
1515
SET(FUZZ_RUNTIME
16-
10
17-
CACHE STRING "Number of seconds to run fuzz tests during ctest run") # Default of 10 seconds
16+
10
17+
CACHE STRING "Number of seconds to run fuzz tests during ctest run") # Default of 10 seconds
1818

1919
ADD_TEST(NAME fuzz_tester_run COMMAND fuzz_tester -max_total_time=${FUZZ_RUNTIME})

fuzz_test/fuzz_tester.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
#include <fmt/format.h>
2-
#include <iterator>
3-
#include <utility>
1+
#include <stdint.h>
2+
#include <stddef.h>
43

5-
[[nodiscard]] auto sum_values(const uint8_t *Data, size_t Size)
6-
{
7-
constexpr auto scale = 1000;
84

9-
int value = 0;
10-
for (std::size_t offset = 0; offset < Size; ++offset) { value += static_cast<int>(*std::next(Data, static_cast<long>(offset))) * scale; }
11-
return value;
5+
// example comes from here: https://github.com/google/fuzzing/blob/master/tutorial/libFuzzer/fuzz_me.cc
6+
// Documentation can be found here: https://llvm.org/docs/LibFuzzer.html
7+
bool FuzzMe(const uint8_t *Data, size_t DataSize) {
8+
return DataSize >= 3 &&
9+
Data[0] == 'F' &&
10+
Data[1] == 'U' &&
11+
Data[2] == 'Z' &&
12+
Data[3] == 'Z'; // :‑<
1213
}
1314

14-
// Fuzzer that attempts to invoke undefined behavior for signed integer overflow
15-
// cppcheck-suppress unusedFunction symbolName=LLVMFuzzerTestOneInput
16-
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
17-
{
18-
fmt::print("Value sum: {}, len{}\n", sum_values(Data, Size), Size);
15+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
16+
FuzzMe(Data, Size);
1917
return 0;
2018
}

0 commit comments

Comments
 (0)