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 authored and sergiud committed Feb 24, 2023
1 parent 1b59cb0 commit ebc7804
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
$<TARGET_OBJECTS:glog_internal>
Expand Down
32 changes: 32 additions & 0 deletions src/fuzz_demangle.cc
Original file line number Diff line number Diff line change
@@ -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 <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 ebc7804

Please sign in to comment.