Skip to content

Commit

Permalink
Add support to fuzz with LibFuzzer
Browse files Browse the repository at this point in the history
Both MSVC and clang have support for fuzzing with LibFuzzer. Add a console app that can be used to fuzz with LibFuzzer.
  • Loading branch information
vbaderks committed Dec 27, 2023
1 parent e7b1e58 commit 8731d28
Show file tree
Hide file tree
Showing 22 changed files with 274 additions and 122 deletions.
23 changes: 20 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ endif ()

# The basic options to control what is build extra.
option(CHARLS_BUILD_TESTS "Build test application" ${MASTER_PROJECT})
option(CHARLS_BUILD_FUZZ_TEST "Build AFL fuzzer application" ${MASTER_PROJECT})
option(CHARLS_BUILD_AFL_FUZZ_TEST "Build AFL test fuzzer application" ${MASTER_PROJECT})
option(CHARLS_BUILD_LIBFUZZER_FUZZ_TEST "Build LibFuzzer test fuzzer application" ${MASTER_PROJECT})
option(CHARLS_BUILD_SAMPLES "Build sample applications" ${MASTER_PROJECT})
option(CHARLS_INSTALL "Generate the install target." ${MASTER_PROJECT})

# Provide BUILD_SHARED_LIBS as an option for GUI tools
option(BUILD_SHARED_LIBS "Will control if charls lib is build as shared lib/DLL or static library")

# Provide option to build CharLS with address sanitizer
option(CHARLS_ENABLE_ASAN "Build with address sanitizer enabled." OFF)

# These options are used by the CI pipeline to ensure new warnings are detected quickly.
# Not enabled by default to ensure the CharLS package is end-user friendly.
option(CHARLS_PEDANTIC_WARNINGS "Enable extra warnings and static analysis." OFF)
Expand Down Expand Up @@ -143,6 +147,10 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
set(WARNINGS_AS_ERRORS_FLAG_LINKER LINKER:--fatal-warnings)
endif()

if(NOT APPLE)
set(LIBFUZZER_SUPPORTED 1)
endif()
endif()

if(MSVC)
Expand Down Expand Up @@ -192,6 +200,11 @@ if(MSVC)
add_link_options("/CETCOMPAT")
endif()

# Enable LibFuzzer support for MSVC
if(${ARM_DETECTED} EQUAL 0 AND MSVC_VERSION GREATER_EQUAL 1930)
set(LIBFUZZER_SUPPORTED 1)
endif()

endif()

# When enabled apply the pedantic warnings options and warnings as errors to globally.
Expand Down Expand Up @@ -223,8 +236,12 @@ if(CHARLS_BUILD_TESTS)
)
endif()

if(CHARLS_BUILD_FUZZ_TEST)
add_subdirectory(fuzztest)
if(CHARLS_BUILD_AFL_FUZZ_TEST)
add_subdirectory(fuzzing/afl)
endif()

if(CHARLS_BUILD_LIBFUZZER_FUZZ_TEST AND LIBFUZZER_SUPPORTED)
add_subdirectory(fuzzing/libfuzzer)
endif()

if(CHARLS_BUILD_SAMPLES)
Expand Down
35 changes: 23 additions & 12 deletions CharLS.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32104.313
VisualStudioVersion = 17.9.34407.89
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CharLSTest", "test\CharLSTest.vcxproj", "{7185AD7F-57BA-42C7-A715-239CEA8ADC31}"
EndProject
Expand Down Expand Up @@ -31,10 +31,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CharLSUnitTest", "unittest\
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "convert-cpp", "samples\convert.cpp\convert-cpp.vcxproj", "{E09F024E-A125-48AA-8E9D-7D1302BEAC97}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FuzzTest", "fuzztest\FuzzTest.vcxproj", "{5637C116-ABF5-4274-A71F-34433713A538}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "benchmark", "benchmark\benchmark.vcxproj", "{F961EC29-4ACE-4D5E-B7ED-55681A678A90}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AflFuzzTest", "fuzzing\afl\AflFuzzTest.vcxproj", "{5637C116-ABF5-4274-A71F-34433713A538}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LibFuzzerTest", "fuzzing\libfuzzer\LibFuzzerTest.vcxproj", "{0F21D958-FE76-469A-8562-5D05F9EFE8D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Checked|ARM64 = Checked|ARM64
Expand Down Expand Up @@ -138,6 +140,15 @@ Global
{E09F024E-A125-48AA-8E9D-7D1302BEAC97}.Release|x64.Build.0 = Release|x64
{E09F024E-A125-48AA-8E9D-7D1302BEAC97}.Release|x86.ActiveCfg = Release|Win32
{E09F024E-A125-48AA-8E9D-7D1302BEAC97}.Release|x86.Build.0 = Release|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|ARM64.ActiveCfg = Checked|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|x64.ActiveCfg = Checked|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|x86.ActiveCfg = Checked|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|ARM64.ActiveCfg = Debug|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|x64.ActiveCfg = Debug|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|x86.ActiveCfg = Debug|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|ARM64.ActiveCfg = Release|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|x64.ActiveCfg = Release|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|x86.ActiveCfg = Release|Win32
{5637C116-ABF5-4274-A71F-34433713A538}.Checked|ARM64.ActiveCfg = Checked|ARM64
{5637C116-ABF5-4274-A71F-34433713A538}.Checked|ARM64.Build.0 = Checked|ARM64
{5637C116-ABF5-4274-A71F-34433713A538}.Checked|x64.ActiveCfg = Checked|x64
Expand All @@ -156,15 +167,15 @@ Global
{5637C116-ABF5-4274-A71F-34433713A538}.Release|x64.Build.0 = Release|x64
{5637C116-ABF5-4274-A71F-34433713A538}.Release|x86.ActiveCfg = Release|Win32
{5637C116-ABF5-4274-A71F-34433713A538}.Release|x86.Build.0 = Release|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|ARM64.ActiveCfg = Checked|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|x64.ActiveCfg = Checked|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Checked|x86.ActiveCfg = Checked|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|ARM64.ActiveCfg = Debug|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|x64.ActiveCfg = Debug|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Debug|x86.ActiveCfg = Debug|Win32
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|ARM64.ActiveCfg = Release|ARM64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|x64.ActiveCfg = Release|x64
{F961EC29-4ACE-4D5E-B7ED-55681A678A90}.Release|x86.ActiveCfg = Release|Win32
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Checked|ARM64.ActiveCfg = Checked|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Checked|x64.ActiveCfg = Checked|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Checked|x86.ActiveCfg = Checked|Win32
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Debug|ARM64.ActiveCfg = Debug|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Debug|x64.ActiveCfg = Debug|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Debug|x86.ActiveCfg = Debug|Win32
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Release|ARM64.ActiveCfg = Release|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Release|x64.ActiveCfg = Release|x64
{0F21D958-FE76-469A-8562-5D05F9EFE8D1}.Release|x86.ActiveCfg = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
80 changes: 1 addition & 79 deletions benchmark/benchmark.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{f961ec29-4ace-4d5e-b7ed-55681a678a90}</ProjectGuid>
<RootNamespace>benchmark</RootNamespace>
Expand All @@ -54,34 +54,13 @@
<PropertyGroup>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Checked|Win32'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Checked|x64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Checked|ARM64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
Expand Down Expand Up @@ -146,122 +125,65 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Checked|Win32'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Checked|x64'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Checked|ARM64'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<ClCompile>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions fuzztest/FuzzTest.vcxproj → fuzzing/afl/AflFuzzTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<VCProjectVersion>17.0</VCProjectVersion>
<ProjectGuid>{5637C116-ABF5-4274-A71F-34433713A538}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<ConfigurationType>Application</ConfigurationType>
Expand All @@ -53,12 +53,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Checked|x64'" Label="Configuration" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Checked|ARM64'" Label="Configuration" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<WholeProgramOptimization>true</WholeProgramOptimization>
<EnableASAN>true</EnableASAN>
<EnableFuzzer>true</EnableFuzzer>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<WholeProgramOptimization>true</WholeProgramOptimization>
Expand All @@ -78,7 +79,7 @@
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\CharLS.vcxproj">
<ProjectReference Include="..\..\src\CharLS.vcxproj">
<Project>{1e31f9f1-f175-4082-b3e2-b1f0eca3f44c}</Project>
</ProjectReference>
</ItemGroup>
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions fuzzing/afl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Team CharLS.
# SPDX-License-Identifier: BSD-3-Clause

add_executable(afl-fuzztest "")

target_sources(afl-fuzztest PRIVATE main.cpp)

set_target_properties(afl-fuzztest PROPERTIES CXX_VISIBILITY_PRESET hidden)

target_link_libraries(afl-fuzztest PRIVATE charls)

if(MSVC)
# AFL uses POSIX functions: disable warning about potential unsafe methods.
target_compile_definitions(afl-fuzztest PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
2 changes: 1 addition & 1 deletion fuzztest/main.cpp → fuzzing/afl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int main(const int argc, const char* const argv[]) // NOLINT(bugprone-exception-
vector<uint8_t> destination;
jpegls_decoder::decode(source, destination);
}
catch (const jpegls_error&)
catch (const jpegls_error&) // NOLINT(bugprone-empty-catch)
{
}
}
Expand Down
16 changes: 16 additions & 0 deletions fuzzing/libfuzzer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) Team CharLS.
# SPDX-License-Identifier: BSD-3-Clause

add_executable(libfuzzer-fuzztest "")

target_sources(libfuzzer-fuzztest PRIVATE main.cpp)

set_target_properties(libfuzzer-fuzztest PROPERTIES CXX_VISIBILITY_PRESET hidden)

target_link_libraries(libfuzzer-fuzztest PRIVATE charls)

target_compile_options(libfuzzer-fuzztest PRIVATE "-fsanitize=fuzzer,address")

if(NOT MSVC)
target_link_options(libfuzzer-fuzztest PRIVATE "-fsanitize=fuzzer,address")
endif()
Loading

0 comments on commit 8731d28

Please sign in to comment.