Add fuzz testing for demangle (#878)

This commit is contained in:
Catena cyber 2023-02-24 22:45:32 +01:00 committed by GitHub
parent 1b59cb0905
commit 846c3fe55b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 0 deletions

View File

@ -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
$<TARGET_OBJECTS:glog_internal>

32
src/fuzz_demangle.cc Normal file
View File

@ -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;
}