Aditya Wazir | 054c936 | 2021-06-09 11:05:05 +0530 | [diff] [blame] | 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: |
| 31 | TestTextOutput() {} |
| 32 | virtual ~TestTextOutput() {} |
| 33 | |
| 34 | virtual status_t print(const char* /*txt*/, size_t /*len*/) { return NO_ERROR; } |
| 35 | virtual void moveIndent(int /*delta*/) { return; } |
| 36 | virtual void pushBundle() { return; } |
| 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 | |
| 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); |
Akhilesh Sanikop | 09ef988 | 2024-04-18 14:04:38 +0530 | [diff] [blame^] | 61 | /** |
| 62 | * Filtering out strings based on "-w" argument. Since it leads to timeout. |
| 63 | */ |
| 64 | if(strcmp(argument.c_str(), "-w") == 0) { |
| 65 | continue; |
| 66 | } |
Aditya Wazir | 054c936 | 2021-06-09 11:05:05 +0530 | [diff] [blame] | 67 | arguments.emplace_back(argument); |
| 68 | } |
| 69 | } |
| 70 | vector<string_view> argSV; |
| 71 | for (auto& argument : arguments) { |
| 72 | argSV.emplace_back(argument.c_str()); |
| 73 | } |
| 74 | int32_t in = open("/dev/null", O_RDWR | O_CREAT); |
| 75 | int32_t out = open("/dev/null", O_RDWR | O_CREAT); |
| 76 | int32_t err = open("/dev/null", O_RDWR | O_CREAT); |
| 77 | TestTextOutput output; |
| 78 | TestTextOutput error; |
| 79 | RunMode runMode = mFDP->ConsumeBool() ? RunMode::kStandalone : RunMode::kLibrary; |
| 80 | cmdMain(argSV, output, error, in, out, err, runMode); |
| 81 | delete mFDP; |
| 82 | close(in); |
| 83 | close(out); |
| 84 | close(err); |
| 85 | } |
| 86 | |
| 87 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 88 | CmdFuzzer cmdFuzzer; |
| 89 | cmdFuzzer.process(data, size); |
| 90 | return 0; |
| 91 | } |