/src/logging-log4cxx/src/fuzzers/cpp/XMLLayoutFuzzer.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 <iostream> |
20 | | #include <string> |
21 | | #include <cwchar> |
22 | | #include <log4cxx/logstring.h> |
23 | | #include <log4cxx/ndc.h> |
24 | | #include <log4cxx/xml/xmllayout.h> |
25 | | #include <log4cxx/helpers/stringhelper.h> |
26 | | #include <fuzzer/FuzzedDataProvider.h> |
27 | | #include <log4cxx/mdc.h> |
28 | | #include <log4cxx/helpers/transcoder.h> |
29 | | |
30 | | using namespace log4cxx; |
31 | | using namespace log4cxx::helpers; |
32 | | using namespace log4cxx::spi; |
33 | | namespace |
34 | | { |
35 | | const int MaxKeyLength = 50; |
36 | | const int MaxValueLength = 500; |
37 | | const int MaxMessageLength = 2000; |
38 | | } |
39 | | |
40 | 3.07k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
41 | | // Setup XMLLayout |
42 | 3.07k | log4cxx::xml::XMLLayout layout; |
43 | 3.07k | Pool p; |
44 | | |
45 | | // Create random strings |
46 | 3.07k | FuzzedDataProvider fdp(data, size); |
47 | 3.07k | std::string key1 = fdp.ConsumeRandomLengthString(MaxKeyLength); |
48 | 3.07k | std::string val1 = fdp.ConsumeRandomLengthString(MaxValueLength); |
49 | 3.07k | std::string key2 = fdp.ConsumeRandomLengthString(MaxKeyLength); |
50 | 3.07k | std::string val2 = fdp.ConsumeRandomLengthString(MaxValueLength); |
51 | 3.07k | std::string ndcMessage = fdp.ConsumeRandomLengthString(MaxMessageLength); |
52 | 3.07k | std::string loggerString = fdp.ConsumeRandomLengthString(MaxKeyLength); |
53 | 3.07k | std::string propkey = fdp.ConsumeRandomLengthString(MaxKeyLength); |
54 | 3.07k | std::string propval = fdp.ConsumeRandomLengthString(MaxValueLength); |
55 | 3.07k | std::string content = fdp.ConsumeRemainingBytesAsString(); |
56 | | |
57 | 3.07k | log4cxx::LevelPtr level = log4cxx::Level::getInfo(); |
58 | 3.07k | log4cxx::NDC::push(ndcMessage); |
59 | 3.07k | LogString logstring1; |
60 | 3.07k | LogString key1LogString; |
61 | 3.07k | LogString val1LogString; |
62 | 3.07k | LogString propkeyLogString; |
63 | 3.07k | LogString propvalLogString; |
64 | 3.07k | LogString logger; |
65 | | |
66 | 3.07k | Transcoder::decode(content, logstring1); |
67 | 3.07k | Transcoder::decode(loggerString, logger); |
68 | 3.07k | Transcoder::decode(key1, key1LogString); |
69 | 3.07k | Transcoder::decode(val1, val1LogString); |
70 | 3.07k | Transcoder::decode(propkey, propkeyLogString); |
71 | 3.07k | Transcoder::decode(propval, propvalLogString); |
72 | 3.07k | log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr( |
73 | 3.07k | new log4cxx::spi::LoggingEvent( |
74 | 3.07k | logger, level, logstring1, LOG4CXX_LOCATION)); |
75 | | // Set properties |
76 | 3.07k | layout.setProperties(true); |
77 | 3.07k | event->setProperty(propkeyLogString, propvalLogString); |
78 | | |
79 | | |
80 | | // Set MDC |
81 | 3.07k | log4cxx::MDC::put(key2, val2); |
82 | | |
83 | | // Location info |
84 | 3.07k | layout.setLocationInfo(true); |
85 | | |
86 | | // Call the target API |
87 | 3.07k | log4cxx::LogString result; |
88 | 3.07k | layout.format(result, event, p); |
89 | | |
90 | | // Clean up |
91 | 3.07k | log4cxx::NDC::clear(); |
92 | 3.07k | log4cxx::MDC::clear(); |
93 | 3.07k | return 0; |
94 | 3.07k | } |