/src/logging-log4cxx/src/fuzzers/cpp/PatternLayoutFuzzer.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 <iostream> // for `cout`, `endl` |
19 | | #include <iomanip> // for `put_time()` |
20 | | #include <ctime> // for `time()` |
21 | | #include <fuzzer/FuzzedDataProvider.h> |
22 | | #include <log4cxx/propertyconfigurator.h> |
23 | | #include <log4cxx/appenderskeleton.h> |
24 | | |
25 | | using namespace LOG4CXX_NS; |
26 | | |
27 | | namespace LOG4CXX_NS::fuzzer { |
28 | | |
29 | | /** |
30 | | * Appender encoding incoming log events using the provided layout. |
31 | | * It is intended for appender-agnostic fuzzing. |
32 | | */ |
33 | | class EncodingAppender : public AppenderSkeleton { |
34 | | |
35 | | public: |
36 | | DECLARE_LOG4CXX_OBJECT(EncodingAppender) |
37 | 16 | BEGIN_LOG4CXX_CAST_MAP() |
38 | 16 | LOG4CXX_CAST_ENTRY(EncodingAppender) |
39 | 16 | LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton) |
40 | 4 | END_LOG4CXX_CAST_MAP() |
41 | | |
42 | 2 | EncodingAppender(const LayoutPtr& layout = {}) : AppenderSkeleton(layout) {} |
43 | | |
44 | 115k | void append( const spi::LoggingEventPtr& event ) override { |
45 | 115k | LogString msg; |
46 | 115k | getLayout()->format(msg, event); |
47 | 115k | } |
48 | | |
49 | | }; // class |
50 | | |
51 | | IMPLEMENT_LOG4CXX_OBJECT(EncodingAppender) |
52 | | |
53 | | } // namespace |
54 | | |
55 | | static std::time_t logTime; |
56 | | static std::tm* logTimeBuffer; |
57 | | #define LOG(stream) \ |
58 | 8 | logTime = std::time(nullptr); \ |
59 | 8 | logTimeBuffer = std::localtime(&logTime); \ |
60 | 8 | stream << std::put_time(logTimeBuffer, "%Y-%m-%d %H:%M:%S") << " [" << __FILE_NAME__ << ":" << __LINE__ << "] " |
61 | | |
62 | | static int INITIALIZED = 0; |
63 | | |
64 | 903 | static void init() { |
65 | 903 | if (INITIALIZED != 0) { |
66 | 901 | return; |
67 | 901 | } |
68 | 2 | LOG(std::cout) << "INFO: Produced using the Git commit ID: " << GIT_COMMIT_ID << std::endl; |
69 | 2 | auto programFilePath = spi::Configurator::properties().getProperty(LOG4CXX_STR("PROGRAM_FILE_PATH.PARENT_PATH")); |
70 | 2 | PropertyConfigurator::configure(programFilePath + LOG4CXX_STR("/PatternLayoutFuzzer.properties")); |
71 | 2 | INITIALIZED = 1; |
72 | 2 | } |
73 | | |
74 | 115k | #define MAX_STRING_LENGTH 512 |
75 | | |
76 | 903 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
77 | 903 | init(); |
78 | 903 | LoggerPtr logger = Logger::getRootLogger(); |
79 | 903 | FuzzedDataProvider dataProvider(data, size); |
80 | 116k | while (dataProvider.remaining_bytes() > 0) { |
81 | 115k | std::string message = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH); |
82 | 115k | LOG4CXX_INFO(logger, message); |
83 | 115k | } |
84 | 903 | return 0; |
85 | 903 | } |