Skip to content

Commit

Permalink
Adds fuzz testing for demangle
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Feb 24, 2023
1 parent 1b59cb0 commit f12c012
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON)
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)
Expand Down Expand Up @@ -748,6 +750,21 @@ endif (WITH_PKGCONFIG)

# Unit testing

if (WITH_FUZZING AND 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 (WITH_FUZZING AND NOT WITH_FUZZING STREQUAL "none")

if (BUILD_TESTING)
add_library (glogtest STATIC
$<TARGET_OBJECTS:glog_internal>
Expand Down
30 changes: 30 additions & 0 deletions src/fuzz_demangle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 <cstring>
#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;
}

0 comments on commit f12c012

Please sign in to comment.