1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "android-base/file.h"
18 #include "android/log.h"
19 #include "fuzzer/FuzzedDataProvider.h"
20 #include "utils/Printer.h"
21 #include "utils/String8.h"
22 static constexpr int MAX_STR_SIZE = 1000;
23 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
25     FuzzedDataProvider dataProvider(data, size);
26     android::String8 outStr = android::String8();
27     // Line indent/formatting
28     uint indent = dataProvider.ConsumeIntegral<uint>();
29     std::string prefix = dataProvider.ConsumeRandomLengthString(MAX_STR_SIZE);
30     std::string line = dataProvider.ConsumeRandomLengthString(MAX_STR_SIZE);
31 
32     // Misc properties
33     std::string logTag = dataProvider.ConsumeRandomLengthString(MAX_STR_SIZE);
34     android_LogPriority priority =
35             static_cast<android_LogPriority>(dataProvider.ConsumeIntegral<int>());
36     bool ignoreBlankLines = dataProvider.ConsumeBool();
37 
38     TemporaryFile tf;
39     android::FdPrinter filePrinter = android::FdPrinter(tf.fd, indent, prefix.c_str());
40     android::String8Printer stringPrinter = android::String8Printer(&outStr);
41     android::PrefixPrinter printer = android::PrefixPrinter(stringPrinter, prefix.c_str());
42     android::LogPrinter logPrinter =
43             android::LogPrinter(logTag.c_str(), priority, prefix.c_str(), ignoreBlankLines);
44 
45     printer.printLine(line.c_str());
46     printer.printFormatLine("%s", line.c_str());
47     logPrinter.printLine(line.c_str());
48     logPrinter.printFormatLine("%s", line.c_str());
49     filePrinter.printLine(line.c_str());
50     filePrinter.printFormatLine("%s", line.c_str());
51     return 0;
52 }
53