From 0ba21bef2b7d1fc20befd1497ee5829888ac65de Mon Sep 17 00:00:00 2001 From: Adam Korczynski Date: Sat, 18 Jan 2025 19:23:44 +0000 Subject: [PATCH] Add PatternParser fuzzer Signed-off-by: Adam Korczynski --- src/fuzzers/cpp/CMakeLists.txt | 2 +- src/fuzzers/cpp/PatternParserFuzzer.cpp | 129 ++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/fuzzers/cpp/PatternParserFuzzer.cpp diff --git a/src/fuzzers/cpp/CMakeLists.txt b/src/fuzzers/cpp/CMakeLists.txt index 0b7d4f622..7e8da1d1c 100644 --- a/src/fuzzers/cpp/CMakeLists.txt +++ b/src/fuzzers/cpp/CMakeLists.txt @@ -15,7 +15,7 @@ # limitations under the License. # -set(ALL_LOG4CXX_FUZZERS PatternLayoutFuzzer XMLLayoutFuzzer) +set(ALL_LOG4CXX_FUZZERS PatternLayoutFuzzer XMLLayoutFuzzer PatternParserFuzzer) set(LOG4CXX_CHAR "utf-8") # Get the most recent Git commit ID diff --git a/src/fuzzers/cpp/PatternParserFuzzer.cpp b/src/fuzzers/cpp/PatternParserFuzzer.cpp new file mode 100644 index 000000000..081e3b7ff --- /dev/null +++ b/src/fuzzers/cpp/PatternParserFuzzer.cpp @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 "stdint.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace log4cxx; +using namespace log4cxx::helpers; +using namespace log4cxx::spi; +using namespace log4cxx::pattern; + +#define RULES_PUT(spec, cls) \ + map.insert(PatternMap::value_type(LOG4CXX_STR(spec), (PatternConstructor) cls ::newInstance)) + +PatternMap getFormatSpecifiers() +{ + PatternMap map; + RULES_PUT("c", LoggerPatternConverter); + RULES_PUT("logger", LoggerPatternConverter); + + RULES_PUT("C", ClassNamePatternConverter); + RULES_PUT("class", ClassNamePatternConverter); + + RULES_PUT("d", DatePatternConverter); + RULES_PUT("date", DatePatternConverter); + + RULES_PUT("F", FileLocationPatternConverter); + RULES_PUT("file", FileLocationPatternConverter); + + RULES_PUT("l", FullLocationPatternConverter); + + RULES_PUT("L", LineLocationPatternConverter); + RULES_PUT("line", LineLocationPatternConverter); + + RULES_PUT("m", MessagePatternConverter); + RULES_PUT("message", MessagePatternConverter); + + RULES_PUT("n", LineSeparatorPatternConverter); + + RULES_PUT("M", MethodLocationPatternConverter); + RULES_PUT("method", MethodLocationPatternConverter); + + RULES_PUT("p", LevelPatternConverter); + RULES_PUT("level", LevelPatternConverter); + + RULES_PUT("r", RelativeTimePatternConverter); + RULES_PUT("relative", RelativeTimePatternConverter); + + RULES_PUT("t", ThreadPatternConverter); + RULES_PUT("thread", ThreadPatternConverter); + + RULES_PUT("T", ThreadUsernamePatternConverter); + RULES_PUT("threadname", ThreadUsernamePatternConverter); + + RULES_PUT("x", NDCPatternConverter); + RULES_PUT("ndc", NDCPatternConverter); + + RULES_PUT("X", PropertiesPatternConverter); + RULES_PUT("properties", PropertiesPatternConverter); + + RULES_PUT("throwable", ThrowableInformationPatternConverter); + + return map; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + // Create a FuzzedDataProvider which we + // will use to create strings from "data". + FuzzedDataProvider fdp(data, size); + + std::string loggerStr = fdp.ConsumeRandomLengthString(); + std::string content = fdp.ConsumeRandomLengthString(); + + // Create the event + log4cxx::LogString logger = LOG4CXX_STR(loggerStr); + log4cxx::LevelPtr level = log4cxx::Level::getInfo(); + log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr( + new log4cxx::spi::LoggingEvent( + logger, level, LOG4CXX_STR(content), LOG4CXX_LOCATION)); + + + log4cxx::helpers::Pool p; + PatternMap patternMap = getFormatSpecifiers(); + std::vector converters; + std::vector fields; + + log4cxx::LogString pattern = LOG4CXX_STR(fdp.ConsumeRandomLengthString()); + PatternParser::parse(pattern, converters, fields, patternMap); + + return 0; +}