Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 7 | #include <fuzzer/FuzzedDataProvider.h> |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <string> |
| 12 | #include <tuple> |
| 13 | |
| 14 | #include "base/check.h" |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
| 17 | #include "base/strings/utf_string_conversions.h" |
| 18 | #include "build/build_config.h" |
| 19 | |
| 20 | namespace base { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | CommandLine::StringType GenerateNativeString(FuzzedDataProvider& provider) { |
| 25 | const std::string raw_string = provider.ConsumeRandomLengthString(); |
| 26 | #if BUILDFLAG(IS_WIN) |
| 27 | return UTF8ToWide(raw_string); |
| 28 | #else |
| 29 | return raw_string; |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | CommandLine::StringVector GenerateNativeStringVector( |
| 34 | FuzzedDataProvider& provider) { |
| 35 | CommandLine::StringVector strings( |
| 36 | provider.ConsumeIntegralInRange<int>(0, 100)); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 37 | for (auto& item : strings) { |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 38 | item = GenerateNativeString(provider); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 39 | } |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 40 | return strings; |
| 41 | } |
| 42 | |
| 43 | FilePath GenerateFilePath(FuzzedDataProvider& provider) { |
| 44 | return FilePath(GenerateNativeString(provider)); |
| 45 | } |
| 46 | |
| 47 | bool IsForbiddenSwitchCharacter(char c) { |
| 48 | return IsAsciiWhitespace(c) || c == '=' || c != ToLowerASCII(c); |
| 49 | } |
| 50 | |
| 51 | bool IsValidSwitchName(const std::string& text) { |
| 52 | // This duplicates the logic in command_line.cc, but it's not exposed in form |
| 53 | // of public interface. |
Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 54 | return !text.empty() && |
| 55 | !std::ranges::any_of(text, IsForbiddenSwitchCharacter) && |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 56 | !StartsWith(text, "-") && !StartsWith(text, "/"); |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 62 | FuzzedDataProvider provider(data, size); |
| 63 | |
| 64 | // Create a randomly initialized command line. |
| 65 | CommandLine command_line(CommandLine::NO_PROGRAM); |
| 66 | switch (provider.ConsumeIntegralInRange<int>(0, 3)) { |
| 67 | case 0: |
| 68 | // Leave empty. |
| 69 | break; |
| 70 | case 1: |
| 71 | command_line = CommandLine(GenerateFilePath(provider)); |
| 72 | break; |
| 73 | case 2: |
| 74 | command_line = CommandLine(GenerateNativeStringVector(provider)); |
| 75 | break; |
| 76 | case 3: |
| 77 | #if BUILDFLAG(IS_WIN) |
| 78 | command_line.ParseFromString(GenerateNativeString(provider)); |
| 79 | #endif |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | // Do a few mutations of the command line. |
| 84 | while (provider.remaining_bytes() > 0) { |
| 85 | switch (provider.ConsumeIntegralInRange<int>(0, 4)) { |
| 86 | case 0: { |
| 87 | // Add a switch. |
| 88 | std::string name = provider.ConsumeRandomLengthString(); |
| 89 | if (IsValidSwitchName(name)) { |
| 90 | CommandLine::StringType value = GenerateNativeString(provider); |
| 91 | command_line.AppendSwitchNative(name, value); |
| 92 | CHECK(command_line.HasSwitch(name)); |
| 93 | CHECK(command_line.GetSwitchValueNative(name) == value); |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | case 1: { |
| 98 | // Remove a switch. |
| 99 | std::string name = provider.ConsumeRandomLengthString(); |
| 100 | if (IsValidSwitchName(name)) { |
| 101 | command_line.RemoveSwitch(name); |
| 102 | CHECK(!command_line.HasSwitch(name)); |
| 103 | CHECK(command_line.GetSwitchValueNative(name).empty()); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | case 2: { |
| 108 | // Add an argument. |
| 109 | CommandLine::StringType arg = GenerateNativeString(provider); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 110 | if (!arg.empty() && IsStringASCII(arg)) { |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 111 | command_line.AppendArgNative(arg); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 112 | } |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | case 3: { |
| 116 | // Add a wrapper. |
| 117 | CommandLine::StringType wrapper = GenerateNativeString(provider); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 118 | if (!wrapper.empty()) { |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 119 | command_line.PrependWrapper(wrapper); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 120 | } |
Maksim Ivanov | 71181d3 | 2022-11-04 03:22:31 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | case 4: { |
| 124 | // Check a switch. |
| 125 | std::string name = provider.ConsumeRandomLengthString(); |
| 126 | if (IsValidSwitchName(name)) { |
| 127 | std::ignore = command_line.HasSwitch(name); |
| 128 | std::ignore = command_line.GetSwitchValueNative(name); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Smoke-test various accessors. |
| 135 | std::ignore = command_line.GetCommandLineString(); |
| 136 | std::ignore = command_line.GetArgumentsString(); |
| 137 | #if BUILDFLAG(IS_WIN) |
| 138 | std::ignore = command_line.GetCommandLineStringForShell(); |
| 139 | std::ignore = command_line.GetCommandLineStringWithUnsafeInsertSequences(); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | } // namespace base |