/src/logging-log4cxx/src/fuzzers/cpp/PatternParserFuzzer.cpp
| Line | Count | Source | 
| 1 |  | /* | 
| 2 |  |  * Licensed to the Apache Software Foundation (ASF) under one or more | 
| 3 |  |  * contributor license agreements.  See the NOTICE file distributed with | 
| 4 |  |  * this work for additional information regarding copyright ownership. | 
| 5 |  |  * The ASF licenses this file to You under the Apache License, Version 2.0 | 
| 6 |  |  * (the "License"); you may not use this file except in compliance with | 
| 7 |  |  * the License.  You may obtain a copy of the License at | 
| 8 |  |  * | 
| 9 |  |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  |  * | 
| 11 |  |  * Unless required by applicable law or agreed to in writing, software | 
| 12 |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
| 13 |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 14 |  |  * See the License for the specific language governing permissions and | 
| 15 |  |  * limitations under the License. | 
| 16 |  |  */ | 
| 17 |  |  | 
| 18 |  | #include "stdint.h" | 
| 19 |  | #include <log4cxx/nt/nteventlogappender.h> | 
| 20 |  | #include <log4cxx/logstring.h> | 
| 21 |  | #include <log4cxx/helpers/stringhelper.h> | 
| 22 |  | #include <fuzzer/FuzzedDataProvider.h> | 
| 23 |  | #include <log4cxx/pattern/patternparser.h> | 
| 24 |  | #include <log4cxx/helpers/transcoder.h> | 
| 25 |  |  | 
| 26 |  | #include <log4cxx/pattern/loggerpatternconverter.h> | 
| 27 |  | #include <log4cxx/pattern/literalpatternconverter.h> | 
| 28 |  | #include <log4cxx/pattern/classnamepatternconverter.h> | 
| 29 |  | #include <log4cxx/pattern/datepatternconverter.h> | 
| 30 |  | #include <log4cxx/pattern/filedatepatternconverter.h> | 
| 31 |  | #include <log4cxx/pattern/filelocationpatternconverter.h> | 
| 32 |  | #include <log4cxx/pattern/fulllocationpatternconverter.h> | 
| 33 |  | #include <log4cxx/pattern/integerpatternconverter.h> | 
| 34 |  | #include <log4cxx/pattern/linelocationpatternconverter.h> | 
| 35 |  | #include <log4cxx/pattern/messagepatternconverter.h> | 
| 36 |  | #include <log4cxx/pattern/lineseparatorpatternconverter.h> | 
| 37 |  | #include <log4cxx/pattern/methodlocationpatternconverter.h> | 
| 38 |  | #include <log4cxx/pattern/levelpatternconverter.h> | 
| 39 |  | #include <log4cxx/pattern/relativetimepatternconverter.h> | 
| 40 |  | #include <log4cxx/pattern/threadpatternconverter.h> | 
| 41 |  | #include <log4cxx/pattern/ndcpatternconverter.h> | 
| 42 |  | #include <log4cxx/pattern/propertiespatternconverter.h> | 
| 43 |  | #include <log4cxx/pattern/throwableinformationpatternconverter.h> | 
| 44 |  | #include <log4cxx/pattern/threadusernamepatternconverter.h> | 
| 45 |  |  | 
| 46 |  | namespace | 
| 47 |  | { | 
| 48 |  |     const int MaximumLoggerNameByteCount = 100; | 
| 49 |  |     const int MaximumMessageByteCount = 10000; | 
| 50 |  |     const int MaximumPatternByteCount = 10000; | 
| 51 |  | } | 
| 52 |  | using namespace log4cxx; | 
| 53 |  | using namespace log4cxx::helpers; | 
| 54 |  | using namespace log4cxx::spi; | 
| 55 |  | using namespace log4cxx::pattern; | 
| 56 |  |  | 
| 57 |  | #define RULES_PUT(spec, cls) \ | 
| 58 | 109k |   map.insert(PatternMap::value_type(LOG4CXX_STR(spec), (PatternConstructor) cls ::newInstance)) | 
| 59 |  |  | 
| 60 |  | PatternMap getFormatSpecifiers() | 
| 61 | 3.76k | { | 
| 62 | 3.76k |   PatternMap map; | 
| 63 | 3.76k |   RULES_PUT("c", LoggerPatternConverter); | 
| 64 | 3.76k |   RULES_PUT("logger", LoggerPatternConverter); | 
| 65 |  |  | 
| 66 | 3.76k |   RULES_PUT("C", ClassNamePatternConverter); | 
| 67 | 3.76k |   RULES_PUT("class", ClassNamePatternConverter); | 
| 68 |  |  | 
| 69 | 3.76k |   RULES_PUT("d", DatePatternConverter); | 
| 70 | 3.76k |   RULES_PUT("date", DatePatternConverter); | 
| 71 |  |  | 
| 72 | 3.76k |   RULES_PUT("F", FileLocationPatternConverter); | 
| 73 | 3.76k |   RULES_PUT("file", FileLocationPatternConverter); | 
| 74 |  |  | 
| 75 | 3.76k |   RULES_PUT("l", FullLocationPatternConverter); | 
| 76 |  |  | 
| 77 | 3.76k |   RULES_PUT("L", LineLocationPatternConverter); | 
| 78 | 3.76k |   RULES_PUT("line", LineLocationPatternConverter); | 
| 79 |  |  | 
| 80 | 3.76k |   RULES_PUT("m", MessagePatternConverter); | 
| 81 | 3.76k |   RULES_PUT("message", MessagePatternConverter); | 
| 82 |  |  | 
| 83 | 3.76k |   RULES_PUT("n", LineSeparatorPatternConverter); | 
| 84 |  |  | 
| 85 | 3.76k |   RULES_PUT("M", MethodLocationPatternConverter); | 
| 86 | 3.76k |   RULES_PUT("method", MethodLocationPatternConverter); | 
| 87 |  |  | 
| 88 | 3.76k |   RULES_PUT("p", LevelPatternConverter); | 
| 89 | 3.76k |   RULES_PUT("level", LevelPatternConverter); | 
| 90 |  |  | 
| 91 | 3.76k |   RULES_PUT("r", RelativeTimePatternConverter); | 
| 92 | 3.76k |   RULES_PUT("relative", RelativeTimePatternConverter); | 
| 93 |  |  | 
| 94 | 3.76k |   RULES_PUT("t", ThreadPatternConverter); | 
| 95 | 3.76k |   RULES_PUT("thread", ThreadPatternConverter); | 
| 96 |  |  | 
| 97 | 3.76k |   RULES_PUT("T", ThreadUsernamePatternConverter); | 
| 98 | 3.76k |   RULES_PUT("threadname", ThreadUsernamePatternConverter); | 
| 99 |  |  | 
| 100 | 3.76k |   RULES_PUT("x", NDCPatternConverter); | 
| 101 | 3.76k |   RULES_PUT("ndc", NDCPatternConverter); | 
| 102 |  |  | 
| 103 | 3.76k |   RULES_PUT("X", PropertiesPatternConverter); | 
| 104 | 3.76k |   RULES_PUT("properties", PropertiesPatternConverter); | 
| 105 |  |  | 
| 106 | 3.76k |   RULES_PUT("throwable", ThrowableInformationPatternConverter); | 
| 107 |  |  | 
| 108 | 3.76k |   return map; | 
| 109 | 3.76k | } | 
| 110 |  |  | 
| 111 | 3.76k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 
| 112 |  |   // Create a FuzzedDataProvider which we | 
| 113 |  |   // will use to create strings from "data". | 
| 114 | 3.76k |   FuzzedDataProvider fdp(data, size); | 
| 115 |  |    | 
| 116 | 3.76k |   std::string loggerStr = fdp.ConsumeRandomLengthString(MaximumLoggerNameByteCount); | 
| 117 | 3.76k |   std::string content = fdp.ConsumeRandomLengthString(MaximumMessageByteCount); | 
| 118 | 3.76k |   std::string pattern = fdp.ConsumeRandomLengthString(MaximumPatternByteCount); | 
| 119 |  |  | 
| 120 | 3.76k |   LogString contentLogString; | 
| 121 | 3.76k |   LogString loggerLogString; | 
| 122 | 3.76k |   LogString patternLogString; | 
| 123 |  |  | 
| 124 | 3.76k |   Transcoder::decode(content, contentLogString); | 
| 125 | 3.76k |   Transcoder::decode(loggerStr, loggerLogString); | 
| 126 | 3.76k |   Transcoder::decode(pattern, patternLogString); | 
| 127 |  |  | 
| 128 |  |   // Create the event | 
| 129 | 3.76k |   log4cxx::LogString logger = loggerLogString; | 
| 130 | 3.76k |   log4cxx::LevelPtr level = log4cxx::Level::getInfo(); | 
| 131 | 3.76k |   log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr( | 
| 132 | 3.76k |     new log4cxx::spi::LoggingEvent( | 
| 133 | 3.76k |       logger, level, contentLogString, LOG4CXX_LOCATION)); | 
| 134 |  |  | 
| 135 |  |  | 
| 136 | 3.76k |   log4cxx::helpers::Pool p; | 
| 137 | 3.76k |   PatternMap patternMap = getFormatSpecifiers(); | 
| 138 | 3.76k |   std::vector<PatternConverterPtr> converters; | 
| 139 | 3.76k |   std::vector<FormattingInfoPtr> fields; | 
| 140 |  |  | 
| 141 | 3.76k |   PatternParser::parse(patternLogString, converters, fields, patternMap); | 
| 142 |  |  | 
| 143 | 3.76k |     return 0; | 
| 144 | 3.76k | } |