/src/logging-log4cxx/src/fuzzers/cpp/PatternConverterFuzzer.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 <fuzzer/FuzzedDataProvider.h> |
19 | | #include <log4cxx/spi/loggingevent.h> |
20 | | #include <log4cxx/helpers/transcoder.h> |
21 | | |
22 | | #include <log4cxx/pattern/patternconverter.h> |
23 | | #include <log4cxx/pattern/loggerpatternconverter.h> |
24 | | #include <log4cxx/pattern/literalpatternconverter.h> |
25 | | #include <log4cxx/pattern/classnamepatternconverter.h> |
26 | | #include <log4cxx/pattern/datepatternconverter.h> |
27 | | #include <log4cxx/pattern/filedatepatternconverter.h> |
28 | | #include <log4cxx/pattern/filelocationpatternconverter.h> |
29 | | #include <log4cxx/pattern/fulllocationpatternconverter.h> |
30 | | #include <log4cxx/pattern/integerpatternconverter.h> |
31 | | #include <log4cxx/pattern/linelocationpatternconverter.h> |
32 | | #include <log4cxx/pattern/messagepatternconverter.h> |
33 | | #include <log4cxx/pattern/lineseparatorpatternconverter.h> |
34 | | #include <log4cxx/pattern/methodlocationpatternconverter.h> |
35 | | #include <log4cxx/pattern/levelpatternconverter.h> |
36 | | #include <log4cxx/pattern/relativetimepatternconverter.h> |
37 | | #include <log4cxx/pattern/threadpatternconverter.h> |
38 | | #include <log4cxx/pattern/ndcpatternconverter.h> |
39 | | #include <log4cxx/pattern/propertiespatternconverter.h> |
40 | | #include <log4cxx/pattern/throwableinformationpatternconverter.h> |
41 | | #include <log4cxx/pattern/threadusernamepatternconverter.h> |
42 | | |
43 | | namespace |
44 | | { |
45 | | const int MaximumOptionByteCount = 1000; |
46 | | const int MaximumNameByteCount = 100; |
47 | | } |
48 | | |
49 | | using namespace log4cxx; |
50 | | using namespace log4cxx::helpers; |
51 | | using namespace log4cxx::spi; |
52 | | using namespace log4cxx::pattern; |
53 | | |
54 | | // Creates options from the FuzzedDataProvider |
55 | 2.10k | auto createOptions(FuzzedDataProvider* fdp) { |
56 | 2.10k | auto opt1Str = fdp->ConsumeRandomLengthString(MaximumOptionByteCount); |
57 | 2.10k | auto opt2Str = fdp->ConsumeRandomLengthString(MaximumOptionByteCount); |
58 | 2.10k | auto opt3Str = fdp->ConsumeRandomLengthString(MaximumOptionByteCount); |
59 | 2.10k | auto opt4Str = fdp->ConsumeRandomLengthString(MaximumOptionByteCount); |
60 | 2.10k | auto opt5Str = fdp->ConsumeRandomLengthString(MaximumOptionByteCount); |
61 | | |
62 | 2.10k | LogString opt1, opt2, opt3, opt4, opt5; |
63 | | |
64 | 2.10k | Transcoder::decode(opt1Str, opt1); |
65 | 2.10k | Transcoder::decode(opt2Str, opt2); |
66 | 2.10k | Transcoder::decode(opt3Str, opt3); |
67 | 2.10k | Transcoder::decode(opt4Str, opt4); |
68 | 2.10k | Transcoder::decode(opt5Str, opt5); |
69 | | |
70 | 2.10k | return std::vector<LogString> |
71 | 2.10k | { opt1 |
72 | 2.10k | , opt2 |
73 | 2.10k | , opt3 |
74 | 2.10k | , opt4 |
75 | 2.10k | , opt5 |
76 | 2.10k | }; |
77 | 2.10k | } |
78 | | |
79 | 2.69k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
80 | | // Create a FuzzedDataProvider which we will use to |
81 | | // create strings from "data". |
82 | 2.69k | FuzzedDataProvider fdp(data, size); |
83 | | |
84 | 2.69k | auto loggerStr = fdp.ConsumeRandomLengthString(MaximumNameByteCount); |
85 | 2.69k | auto contentStr = fdp.ConsumeRandomLengthString(MaximumOptionByteCount); |
86 | | |
87 | 2.69k | LogString logger, content; |
88 | 2.69k | Transcoder::decode(loggerStr, logger); |
89 | 2.69k | Transcoder::decode(contentStr, content); |
90 | | |
91 | | // Create the event |
92 | 2.69k | auto level = Level::getInfo(); |
93 | | |
94 | 2.69k | auto event = std::make_shared<LoggingEvent>( |
95 | 2.69k | logger, level, content, LOG4CXX_LOCATION); |
96 | | // Select a converter and invoke it. |
97 | 2.69k | switch(fdp.ConsumeIntegralInRange<int>(0, 14)) { |
98 | 413 | case 0: { |
99 | 413 | auto nameStr = fdp.ConsumeRandomLengthString(MaximumNameByteCount); |
100 | 413 | auto opionStr = fdp.ConsumeRandomLengthString(MaximumOptionByteCount); |
101 | 413 | LogString name, option; |
102 | 413 | Transcoder::decode(nameStr, name); |
103 | 413 | Transcoder::decode(opionStr, option); |
104 | | |
105 | 413 | PropertiesPatternConverter(name, option).format(event, logger); |
106 | 413 | break; |
107 | 0 | } |
108 | 492 | case 1: { |
109 | 492 | LoggerPatternConverter(createOptions(&fdp)).format(event, logger); |
110 | 492 | break; |
111 | 0 | } |
112 | 124 | case 2: { |
113 | 124 | ClassNamePatternConverter(createOptions(&fdp)).format(event, logger); |
114 | 124 | break; |
115 | 0 | } |
116 | 1.48k | case 3: { |
117 | 1.48k | DatePatternConverter(createOptions(&fdp)).format(event, logger); |
118 | 1.48k | break; |
119 | 0 | } |
120 | 24 | case 4: { |
121 | 24 | FullLocationPatternConverter().format(event, logger); |
122 | 24 | break; |
123 | 0 | } |
124 | 4 | case 5: { |
125 | 4 | LineLocationPatternConverter().format(event, logger); |
126 | 4 | break; |
127 | 0 | } |
128 | 24 | case 6: { |
129 | 24 | MessagePatternConverter().format(event, logger); |
130 | 24 | break; |
131 | 0 | } |
132 | 24 | case 7: { |
133 | 24 | LineSeparatorPatternConverter().format(event, logger); |
134 | 24 | break; |
135 | 0 | } |
136 | 7 | case 8: { |
137 | 7 | MethodLocationPatternConverter().format(event, logger); |
138 | 7 | break; |
139 | 0 | } |
140 | 45 | case 9: { |
141 | 45 | LevelPatternConverter().format(event, logger); |
142 | 45 | } |
143 | 55 | case 10: { |
144 | 55 | RelativeTimePatternConverter().format(event, logger); |
145 | 55 | break; |
146 | 45 | } |
147 | 3 | case 11: { |
148 | 3 | ThreadPatternConverter().format(event, logger); |
149 | 3 | break; |
150 | 45 | } |
151 | 4 | case 12: { |
152 | 4 | ThreadUsernamePatternConverter().format(event, logger); |
153 | 4 | break; |
154 | 45 | } |
155 | 10 | case 13: { |
156 | 10 | NDCPatternConverter().format(event, logger); |
157 | 10 | break; |
158 | 45 | } |
159 | 28 | case 14: { |
160 | 28 | ThrowableInformationPatternConverter(fdp.ConsumeBool()).format(event, logger); |
161 | 28 | break; |
162 | 45 | } |
163 | 2.69k | } |
164 | 2.69k | return 0; |
165 | 2.69k | } |