1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <cstddef>
16
17 #include <fuzzer/FuzzedDataProvider.h>
18
19 #include "spdlog/spdlog.h"
20 #include "spdlog/sinks/basic_file_sink.h"
21 #include "spdlog/pattern_formatter.h"
22
23
24 std::string my_formatter_txt = "custom-flag";
25
26 class my_formatter_flag : public spdlog::custom_flag_formatter
27 {
28
29 public:
format(const spdlog::details::log_msg &,const std::tm &,spdlog::memory_buf_t & dest)30 void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
31 {
32 dest.append(my_formatter_txt.data(), my_formatter_txt.data() + my_formatter_txt.size());
33 }
34
clone() const35 std::unique_ptr<custom_flag_formatter> clone() const override
36 {
37 return spdlog::details::make_unique<my_formatter_flag>();
38 }
39 };
40
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42 if (size == 0) {
43 return 0;
44 }
45
46 static std::shared_ptr<spdlog::logger> my_logger;
47 if (!my_logger.get()) {
48 my_logger = spdlog::basic_logger_mt("basic_logger", "/dev/null");
49 spdlog::set_default_logger(my_logger);
50 }
51
52 FuzzedDataProvider stream(data, size);
53
54 const unsigned long size_arg = stream.ConsumeIntegral<unsigned long>();
55 const unsigned long int_arg = stream.ConsumeIntegral<unsigned long>();
56 const char flag = (char)(stream.ConsumeIntegral<char>());
57 const std::string pattern = stream.ConsumeRandomLengthString();
58 my_formatter_txt = stream.ConsumeRandomLengthString();
59 const std::string string_arg = stream.ConsumeRandomLengthString();
60 const std::string format_string = stream.ConsumeRemainingBytesAsString();
61
62 using spdlog::details::make_unique;
63 auto formatter = make_unique<spdlog::pattern_formatter>();
64 formatter->add_flag<my_formatter_flag>(flag).set_pattern(pattern);
65 spdlog::set_formatter(std::move(formatter));
66
67 spdlog::info(format_string.c_str(), size_arg, int_arg, string_arg);
68
69 return 0;
70 }
71