|
| 1 | +# |
| 2 | +# Copyright (C) 2022 by George Cave - [email protected] |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | +# use this file except in compliance with the License. You may obtain a copy of |
| 6 | +# the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations under |
| 14 | +# the License. |
| 15 | + |
| 16 | +# USAGE: To enable the use of AFL instrumentation, this file needs to be |
| 17 | +# included into the CMake scripts at any point *before* any of the compilers are |
| 18 | +# setup by CMake, typically at/before the first call to project(), or any part |
| 19 | +# before compiler detection/validation occurs. |
| 20 | +# |
| 21 | +# This is since CMake does not support changing the compiler after it has been |
| 22 | +# set. |
| 23 | +# |
| 24 | +# For example for CMakeLists.txt: |
| 25 | +# ~~~ |
| 26 | +# cmake_minimum_required(VERSION 3.15) |
| 27 | +# include(cmake/afl-fuzzing.cmake) |
| 28 | +# project(FoE-Engine C CXX) |
| 29 | +# ~~~ |
| 30 | +# And then configuring CMake with: `cmake .. -DAFL_MODE=LTO |
| 31 | +# -DAFL_ENV_OPTIONS=AFL_LLVM_THREADSAFE_INST=1;AFL_LLVM_LAF_ALL=1` |
| 32 | +# |
| 33 | +# Would setup the AFL compiler to use the LTO mode (afl-clang-lto), and prefix |
| 34 | +# any build calls to have the two given environment settings, ie: |
| 35 | +# `AFL_LLVM_THREADSAFE_INST=1 AFL_LLVM_LAF_ALL=1 afl-clang-lto <...>` |
| 36 | +# |
| 37 | +# NOTE: If using multiple ENV_OPTIONS, delimit via semi-colons and it will be |
| 38 | +# separated correctly. |
| 39 | + |
| 40 | +# Options |
| 41 | +option(AFL "Switch to using an AFL compiler" OFF) |
| 42 | +set(AFL_MODE |
| 43 | + "" |
| 44 | + CACHE |
| 45 | + STRING |
| 46 | + "Use a specific AFL instrumentation mode: LTO, LLVM, GCC-PLUGIN, CLANG, GCC" |
| 47 | +) |
| 48 | +set(AFL_ENV_OPTIONS |
| 49 | + "" |
| 50 | + CACHE STRING |
| 51 | + "Add environmental settings to build calls (check `afl-cc -hh`)") |
| 52 | + |
| 53 | +# Sets up for AFL fuzzing by detecting finding and using AFL compilers and |
| 54 | +# setting a few flags and environmental build flags as requested. |
| 55 | +if(AFL) |
| 56 | + find_program(AFL_C_COMPILER afl-cc) |
| 57 | + find_program(AFL_CXX_COMPILER afl-c++) |
| 58 | + |
| 59 | + if(AFL_C_COMPILER AND AFL_CXX_COMPILER) |
| 60 | + if((CMAKE_C_COMPILER AND NOT CMAKE_C_COMPILER STREQUAL AFL_C_COMPILER) |
| 61 | + OR (CMAKE_CXX_COMPILER AND NOT CMAKE_CXX_COMPILER STREQUAL |
| 62 | + AFL_CXX_COMPILER)) |
| 63 | + # CMake doesn't support changing compilers after they've been set |
| 64 | + message( |
| 65 | + FATAL_ERROR |
| 66 | + "Cannot change to AFL compilers after they have been previously set. Clear the cache, reconfigure and ensure setup_afl is called before the first C or CXX compiler is set, typically before the first project() call." |
| 67 | + ) |
| 68 | + else() |
| 69 | + # Set the AFL compiler |
| 70 | + message(STATUS "Changed to AFL compiler") |
| 71 | + set(CMAKE_C_COMPILER ${AFL_C_COMPILER}) |
| 72 | + set(CMAKE_CXX_COMPILER ${AFL_CXX_COMPILER}) |
| 73 | + |
| 74 | + # Set a specific AFL mode for both compile and link stages |
| 75 | + if(AFL_MODE MATCHES "[Ll][Tt][Oo]") |
| 76 | + message(STATUS "Set AFL to Clang-LTO mode") |
| 77 | + add_compile_options(--afl-lto) |
| 78 | + add_link_options(--afl-lto) |
| 79 | + elseif(AFL_MODE MATCHES "[Ll][Ll][Vv][Mm]") |
| 80 | + message(STATUS "Set AFL to Clang-LLVM mode") |
| 81 | + add_compile_options(--afl-llvm) |
| 82 | + add_link_options(--afl-llvm) |
| 83 | + elseif(AFL_MODE MATCHES "[Gg][Cc][Cc][-_][Pp][Ll][Uu][Gg][Ii][Nn]") |
| 84 | + message(STATUS "Set AFL to GCC-Plugin mode") |
| 85 | + add_compile_options(--afl-gcc-plugin) |
| 86 | + add_link_options(--afl-gcc-plugin) |
| 87 | + elseif(AFL_MODE MATCHES "[Ll][Tt][Oo]") |
| 88 | + message(STATUS "Set AFL to Clang mode") |
| 89 | + add_compile_options(--afl-clang) |
| 90 | + add_link_options(--afl-clang) |
| 91 | + elseif(AFL_MODE MATCHES "[Ll][Tt][Oo]") |
| 92 | + message(STATUS "Set AFL to GCC mode") |
| 93 | + add_compile_options(--afl-gcc) |
| 94 | + add_link_options(--afl-gcc) |
| 95 | + endif() |
| 96 | + |
| 97 | + # Add specified environment options |
| 98 | + if(AFL_ENV_OPTIONS) |
| 99 | + set(CMAKE_C_COMPILER_LAUNCHER ${CMAKE_C_COMPILER_LAUNCHER} |
| 100 | + ${AFL_ENV_OPTIONS}) |
| 101 | + set(CMAKE_CXX_COMPILER_LAUNCHER ${CMAKE_CXX_COMPILER_LAUNCHER} |
| 102 | + ${AFL_ENV_OPTIONS}) |
| 103 | + endif() |
| 104 | + endif() |
| 105 | + else() |
| 106 | + message(FATAL_ERROR "Usable AFL compiler was not found!") |
| 107 | + endif() |
| 108 | +endif() |
0 commit comments