diff --git a/CMakeLists.txt b/CMakeLists.txt index 2125bc5..671ddc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,9 @@ option (WITH_UNWIND "Enable libunwind support" ON) cmake_dependent_option (WITH_GMOCK "Use Google Mock" ON WITH_GTEST OFF) +set (WITH_FUZZING none CACHE STRING "Fuzzing engine") +set_property (CACHE WITH_FUZZING PROPERTY STRINGS none libfuzzer ossfuzz) + if (NOT WITH_UNWIND) set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON) endif (NOT WITH_UNWIND) @@ -748,6 +751,22 @@ endif (WITH_PKGCONFIG) # Unit testing +if (NOT WITH_FUZZING STREQUAL "none") + add_executable (fuzz_demangle + src/fuzz_demangle.cc + ) + + if (WITH_FUZZING STREQUAL "ossfuzz") + set (LIB_FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE}) + target_link_libraries (fuzz_demangle PRIVATE glog ${LIB_FUZZING_ENGINE}) + elseif (WITH_FUZZING STREQUAL "libfuzzer") + target_compile_options (fuzz_demangle PRIVATE -fsanitize=fuzzer) + target_link_libraries (fuzz_demangle PRIVATE glog) + else (WITH_FUZZING STREQUAL "libfuzzer") + message (FATAL_ERROR "Unsupported fuzzing engine ${WITH_FUZZING}") + endif (WITH_FUZZING STREQUAL "ossfuzz") +endif (NOT WITH_FUZZING STREQUAL "none") + if (BUILD_TESTING) add_library (glogtest STATIC $ diff --git a/src/fuzz_demangle.cc b/src/fuzz_demangle.cc new file mode 100644 index 0000000..c6ee4b5 --- /dev/null +++ b/src/fuzz_demangle.cc @@ -0,0 +1,32 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +#include + +#include "demangle.h" + +extern "C" int LLVMFuzzerTestOneInput(const unsigned char *Data, + unsigned Size) { + if (Size >= 4095) { + return 0; + } + char Buffer[Size + 1]; + std::memcpy(Buffer, Data, Size); + Buffer[Size] = 0; + char demangled[4096]; + google::Demangle(Buffer, demangled, Size); + return 0; +}