1 /*
2 * Copyright (C) 2021 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 <binder/TextOutput.h>
18 #include <cmd.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <string>
22 #include <vector>
23
24 #include <fuzzer/FuzzedDataProvider.h>
25
26 using namespace std;
27 using namespace android;
28
29 class TestTextOutput : public TextOutput {
30 public:
TestTextOutput()31 TestTextOutput() {}
~TestTextOutput()32 virtual ~TestTextOutput() {}
33
print(const char *,size_t)34 virtual status_t print(const char* /*txt*/, size_t /*len*/) { return NO_ERROR; }
moveIndent(int)35 virtual void moveIndent(int /*delta*/) { return; }
pushBundle()36 virtual void pushBundle() { return; }
popBundle()37 virtual void popBundle() { return; }
38 };
39
40 class CmdFuzzer {
41 public:
42 void process(const uint8_t* data, size_t size);
43
44 private:
45 FuzzedDataProvider* mFDP = nullptr;
46 };
47
process(const uint8_t * data,size_t size)48 void CmdFuzzer::process(const uint8_t* data, size_t size) {
49 mFDP = new FuzzedDataProvider(data, size);
50 vector<string> arguments;
51 if (mFDP->ConsumeBool()) {
52 if (mFDP->ConsumeBool()) {
53 arguments = {"-w", "media.aaudio"};
54 } else {
55 arguments = {"-l"};
56 }
57 } else {
58 while (mFDP->remaining_bytes() > 0) {
59 size_t sizestr = mFDP->ConsumeIntegralInRange<size_t>(1, mFDP->remaining_bytes());
60 string argument = mFDP->ConsumeBytesAsString(sizestr);
61 arguments.emplace_back(argument);
62 }
63 }
64 vector<string_view> argSV;
65 for (auto& argument : arguments) {
66 argSV.emplace_back(argument.c_str());
67 }
68 int32_t in = open("/dev/null", O_RDWR | O_CREAT);
69 int32_t out = open("/dev/null", O_RDWR | O_CREAT);
70 int32_t err = open("/dev/null", O_RDWR | O_CREAT);
71 TestTextOutput output;
72 TestTextOutput error;
73 RunMode runMode = mFDP->ConsumeBool() ? RunMode::kStandalone : RunMode::kLibrary;
74 cmdMain(argSV, output, error, in, out, err, runMode);
75 delete mFDP;
76 close(in);
77 close(out);
78 close(err);
79 }
80
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
82 CmdFuzzer cmdFuzzer;
83 cmdFuzzer.process(data, size);
84 return 0;
85 }
86