Skip to content

Commit 7b0947c

Browse files
committed
updated README and added comments to custom mutator
2 parents da36d8d + 00f7fbb commit 7b0947c

File tree

8 files changed

+157
-92
lines changed

8 files changed

+157
-92
lines changed

.devcontainer/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM mcr.microsoft.com/devcontainers/cpp:1-ubuntu-22.04
2+
3+
#ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="3.22.2"
4+
5+
# Optionally install the cmake for vcpkg
6+
#COPY ./reinstall-cmake.sh /tmp/
7+
8+
#RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \
9+
# chmod +x /tmp/reinstall-cmake.sh && /tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \
10+
# fi \
11+
# && rm -f /tmp/reinstall-cmake.sh
12+
13+
# [Optional] Uncomment this section to install additional vcpkg ports.
14+
# RUN su vscode -c "${VCPKG_ROOT}/vcpkg install <your-port-name-here>"
15+
16+
# [Optional] Uncomment this section to install additional packages.
17+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
18+
&& apt-get -y install --no-install-recommends cmake clang llvm lcov
19+
RUN --mount=type=secret,id=credentials \
20+
export CIFUZZ_CREDENTIALS=$(cat /run/secrets/credentials) &&\
21+
sh -c "$(curl -fsSL http://downloads.code-intelligence.com/assets/install-cifuzz.sh)" $CIFUZZ_CREDENTIALS latest &&\
22+
export CIFUZZ_CREDENTIALS=""
23+
24+
25+
RUN sysctl vm.mmap_rnd_bits=30
26+
RUN cifuzz completion bash > /etc/bash_completion.d/cifuzz

.devcontainer/devcontainer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
3+
{
4+
"name": "Cifuzz",
5+
"build": {
6+
"dockerfile": "Dockerfile",
7+
"options": [
8+
"--secret=id=credentials,env=CIFUZZ_CREDENTIALS"
9+
]
10+
},
11+
"features": {
12+
"ghcr.io/devcontainers-community/features/llvm:3": {}
13+
},
14+
"customizations": {
15+
"vscode": {
16+
"extensions": [
17+
"ryanluker.vscode-coverage-gutters",
18+
"ms-vscode.cpptools",
19+
"ms-vscode.cpptools-extension-pack",
20+
"jeff-hykin.better-cpp-syntax"
21+
]
22+
}
23+
}
24+
25+
// Features to add to the dev container. More info: https://containers.dev/features.
26+
// "features": {},
27+
28+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
29+
// "forwardPorts": [],
30+
31+
// Use 'postCreateCommand' to run commands after the container is created.
32+
// "postCreateCommand": "gcc -v",
33+
34+
// Configure tool-specific properties.
35+
// "customizations": {},
36+
37+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
38+
// "remoteUser": "root"
39+
}

.devcontainer/reinstall-cmake.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
#
7+
set -e
8+
9+
CMAKE_VERSION=${1:-"none"}
10+
11+
if [ "${CMAKE_VERSION}" = "none" ]; then
12+
echo "No CMake version specified, skipping CMake reinstallation"
13+
exit 0
14+
fi
15+
16+
# Cleanup temporary directory and associated files when exiting the script.
17+
cleanup() {
18+
EXIT_CODE=$?
19+
set +e
20+
if [[ -n "${TMP_DIR}" ]]; then
21+
echo "Executing cleanup of tmp files"
22+
rm -Rf "${TMP_DIR}"
23+
fi
24+
exit $EXIT_CODE
25+
}
26+
trap cleanup EXIT
27+
28+
29+
echo "Installing CMake..."
30+
apt-get -y purge --auto-remove cmake
31+
mkdir -p /opt/cmake
32+
33+
architecture=$(dpkg --print-architecture)
34+
case "${architecture}" in
35+
arm64)
36+
ARCH=aarch64 ;;
37+
amd64)
38+
ARCH=x86_64 ;;
39+
*)
40+
echo "Unsupported architecture ${architecture}."
41+
exit 1
42+
;;
43+
esac
44+
45+
CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh"
46+
CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt"
47+
TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX)
48+
49+
echo "${TMP_DIR}"
50+
cd "${TMP_DIR}"
51+
52+
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O
53+
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O
54+
55+
sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}"
56+
sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license
57+
58+
ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
59+
ln -s /opt/cmake/bin/ctest /usr/local/bin/ctest

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for more information:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
# https://containers.dev/guide/dependabot
6+
7+
version: 2
8+
updates:
9+
- package-ecosystem: "devcontainers"
10+
directory: "/"
11+
schedule:
12+
interval: weekly

.github/workflows/main.yaml

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

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ build
44
cmake-build-debug
55

66
/**/.cifuzz-*
7-
/**/*fuzzer_inputs
8-
/**/*fuzz_test_inputs
7+
/**/*fuzz*_inputs
8+
9+
/**/lcov.info

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ they are not enough to uncover memory-corruption bugs.
1313
On the other hand, fuzz testing has established itself as the best practical method to find these
1414
issues in large code bases such as Google Chrome.
1515

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/simple_examples/explore_me.cpp#L10):
16+
In this example, we demonstrate how you can use CI Fuzz to integrate fuzz testing into your C/C++ projects. The example project uses [CMake](https://cmake.org/) as the build system and contains the following use cases:
17+
* [Simple Checks Example](src/simple_examples/explore_me.cpp#L10):
2018
A simple example that triggers a buffer over when the input parameters satisfy certain criteria.
2119
We show that CI Fuzz can quickly generate a test case that trigger this bug.
2220
* [Complex Checks Example](src/simple_examples/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.
21+
A more complex example that triggers a use-after-free bug when the input parameters satisfy certain criteria. In this example, the checks are more complex and involve Base64 encoding and XORing with constant value, making it more challenging to find the correct combination of input parameters that trigger the bug.
22+
* [Stateful Example](src/state_example):
23+
An example that demonstrates the challenges of creating high-quality fuzz tests for complex projects with a large public API. This fuzz test was created with an early version of Code Intelligence auto-generation features, but it is still an excellent example on how to test a large API that keeps state between the calls.
24+
* [Structure Aware Inputs Example](src/advanced_examples/explore_me.cpp#L8):
25+
An example that shows how to fuzz an API that requires structured inputs, with the use of the FuzzedDataProvider helper class.
26+
27+
* [Custom Mutator Example](src/advanced_examples/custom_mutator_example_checks_test.cpp#L37):
28+
An example that is build on top of the [Structure Aware Inputs Example](src/advanced_examples/explore_me.cpp#L8) and shows how to utilize custom mutators to change how the inputs are mutated.
29+
30+
If you want to use the devcontainer environment then export your cifuzz download token to a environment var called "CIFUZZ_CREDENTIALS" like `export CIFUZZ_CREDENTIALS=[my_token]`.

src/advanced_examples/custom_mutator_example_checks_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ FUZZ_TEST(const uint8_t *data, size_t size) {
3434
}
3535

3636
extern "C" size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
37+
38+
/**
39+
Custom mutator example. In this case we only print out once that we are in a custom mutator and then use te regular one,
40+
but you can also change the Data how you like. Make sure to return the new length.
41+
*/
3742
extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
3843
size_t MaxSize, unsigned int Seed) {
3944

@@ -43,5 +48,6 @@ extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
4348
Printed = true;
4449
}
4550

51+
// make sure to return the new Size (that needs to be <= MaxSize) as return value!
4652
return LLVMFuzzerMutate(Data, Size, MaxSize);
4753
}

0 commit comments

Comments
 (0)