xref: /aosp_15_r20/frameworks/native/libs/binder/tests/unit_fuzzers/TextOutputFuzz.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 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 "../../file.h"
18 
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include <binder/Parcel.h>
22 #include <binder/TextOutput.h>
23 #include "android-base/test_utils.h"
24 
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <cstddef>
29 #include <limits>
30 
31 // Fuzzer for the TextOutput class. These were lifted from the existing
32 // test suite.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)33 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34     FuzzedDataProvider fdp(data, size);
35     CapturedStderr cap;
36 
37     while (fdp.remaining_bytes() > 1) {
38         switch (fdp.ConsumeIntegral<uint8_t>() % 3) {
39             case 0: {
40                 std::string input = fdp.ConsumeBytesAsString(
41                         fdp.ConsumeIntegralInRange<size_t>(0, fdp.remaining_bytes()));
42                 android::aerr << input << android::endl;
43                 break;
44             }
45             case 1: {
46                 std::string str = fdp.ConsumeRandomLengthString(fdp.remaining_bytes());
47                 android::HexDump input(str.c_str(), sizeof(str.c_str()));
48                 android::aerr << input << android::endl;
49                 break;
50             }
51             case 2: {
52                 android::TypeCode input(fdp.ConsumeIntegral<uint32_t>());
53                 android::aerr << input << android::endl;
54             }
55         }
56     }
57     cap.Stop();
58 
59     return 0;
60 }
61