Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 Code Intelligence GmbH |
| 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 | package com.code_intelligence.jazzer.driver; |
| 18 | |
| 19 | import static java.lang.System.err; |
| 20 | import static java.lang.System.exit; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| 26 | import java.util.stream.Collectors; |
| 27 | import java.util.stream.Stream; |
| 28 | |
| 29 | /** |
| 30 | * Static options that determine the runtime behavior of the fuzzer, set via Java properties. |
| 31 | * |
| 32 | * <p>Each option corresponds to a command-line argument of the driver of the same name. |
| 33 | * |
| 34 | * <p>Every public field should be deeply immutable. |
| 35 | */ |
Fabian Meumertzheim | 07ce617 | 2022-08-11 10:44:16 +0200 | [diff] [blame] | 36 | public final class Opt { |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 37 | private static final char SYSTEM_DELIMITER = |
| 38 | System.getProperty("os.name").startsWith("Windows") ? ';' : ':'; |
| 39 | |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 40 | public static final String autofuzz = stringSetting("autofuzz", ""); |
| 41 | public static final List<String> autofuzzIgnore = stringListSetting("autofuzz_ignore", ','); |
| 42 | public static final String coverageDump = stringSetting("coverage_dump", ""); |
| 43 | public static final String coverageReport = stringSetting("coverage_report", ""); |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 44 | public static final List<String> customHookIncludes = stringListSetting("custom_hook_includes"); |
| 45 | public static final List<String> customHookExcludes = stringListSetting("custom_hook_excludes"); |
| 46 | public static final List<String> customHooks = stringListSetting("custom_hooks"); |
| 47 | public static final List<String> disabledHooks = stringListSetting("disabled_hooks"); |
| 48 | public static final String dumpClassesDir = stringSetting("dump_classes_dir", ""); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 49 | public static final boolean hooks = boolSetting("hooks", true); |
Fabian Meumertzheim | 5a2137e | 2022-08-11 10:00:30 +0200 | [diff] [blame] | 50 | public static final String idSyncFile = stringSetting("id_sync_file", null); |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 51 | public static final List<String> instrumentationIncludes = |
| 52 | stringListSetting("instrumentation_includes"); |
| 53 | public static final List<String> instrumentationExcludes = |
| 54 | stringListSetting("instrumentation_excludes"); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 55 | public static final Set<Long> ignore = |
| 56 | Collections.unmodifiableSet(stringListSetting("ignore", ',') |
| 57 | .stream() |
| 58 | .map(Long::parseUnsignedLong) |
| 59 | .collect(Collectors.toSet())); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 60 | public static final String reproducerPath = stringSetting("reproducer_path", "."); |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 61 | public static final String targetClass = stringSetting("target_class", ""); |
| 62 | public static final List<String> trace = stringListSetting("trace"); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 63 | |
| 64 | // The values of these settings depend on autofuzz. |
| 65 | public static final List<String> targetArgs = autofuzz.isEmpty() |
| 66 | ? stringListSetting("target_args", ' ') |
| 67 | : Collections.unmodifiableList( |
| 68 | Stream.concat(Stream.of(autofuzz), autofuzzIgnore.stream()).collect(Collectors.toList())); |
| 69 | public static final long keepGoing = |
Fabian Meumertzheim | 6ee1c1c | 2022-08-18 10:11:50 +0200 | [diff] [blame^] | 70 | uint64Setting("keep_going", autofuzz.isEmpty() ? 1 : Long.MAX_VALUE); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 71 | |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 72 | // Default to false if hooks is false to mimic the original behavior of the native fuzz target |
| 73 | // runner, but still support hooks = false && dedup = true. |
Fabian Meumertzheim | c90c073 | 2022-08-11 12:22:10 +0200 | [diff] [blame] | 74 | public static final boolean dedup = boolSetting("dedup", hooks); |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 75 | |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 76 | static { |
| 77 | if (!targetClass.isEmpty() && !autofuzz.isEmpty()) { |
| 78 | err.println("--target_class and --autofuzz cannot be specified together"); |
| 79 | exit(1); |
| 80 | } |
| 81 | if (!stringListSetting("target_args", ' ').isEmpty() && !autofuzz.isEmpty()) { |
| 82 | err.println("--target_args and --autofuzz cannot be specified together"); |
| 83 | exit(1); |
| 84 | } |
| 85 | if (autofuzz.isEmpty() && !autofuzzIgnore.isEmpty()) { |
| 86 | err.println("--autofuzz_ignore requires --autofuzz"); |
| 87 | exit(1); |
| 88 | } |
| 89 | if ((!ignore.isEmpty() || keepGoing > 1) && !dedup) { |
| 90 | // --autofuzz implicitly sets keepGoing to Integer.MAX_VALUE. |
| 91 | err.println("--nodedup is not supported with --ignore, --keep_going, or --autofuzz"); |
| 92 | exit(1); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private static final String optionsPrefix = "jazzer."; |
| 97 | |
| 98 | private static String stringSetting(String name, String defaultValue) { |
| 99 | return System.getProperty(optionsPrefix + name, defaultValue); |
| 100 | } |
| 101 | |
Fabian Meumertzheim | 193908f | 2022-08-11 11:38:38 +0200 | [diff] [blame] | 102 | private static List<String> stringListSetting(String name) { |
| 103 | return stringListSetting(name, SYSTEM_DELIMITER); |
| 104 | } |
| 105 | |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 106 | private static List<String> stringListSetting(String name, char separator) { |
| 107 | String value = System.getProperty(optionsPrefix + name); |
| 108 | if (value == null || value.isEmpty()) { |
| 109 | return Collections.emptyList(); |
| 110 | } |
| 111 | return splitOnUnescapedSeparator(value, separator); |
| 112 | } |
| 113 | |
| 114 | private static boolean boolSetting(String name, boolean defaultValue) { |
| 115 | String value = System.getProperty(optionsPrefix + name); |
| 116 | if (value == null) { |
| 117 | return defaultValue; |
| 118 | } |
| 119 | return Boolean.parseBoolean(value); |
| 120 | } |
| 121 | |
Fabian Meumertzheim | 6ee1c1c | 2022-08-18 10:11:50 +0200 | [diff] [blame^] | 122 | private static long uint64Setting(String name, long defaultValue) { |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 123 | String value = System.getProperty(optionsPrefix + name); |
| 124 | if (value == null) { |
| 125 | return defaultValue; |
| 126 | } |
Fabian Meumertzheim | 6ee1c1c | 2022-08-18 10:11:50 +0200 | [diff] [blame^] | 127 | return Long.parseUnsignedLong(value, 10); |
Fabian Meumertzheim | 678ceb5 | 2022-07-28 18:58:47 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Split value into non-empty takens separated by separator. Backslashes can be used to escape |
| 132 | * separators (or backslashes). |
| 133 | * |
| 134 | * @param value the string to split |
| 135 | * @param separator a single character to split on (backslash is not allowed) |
| 136 | * @return an immutable list of tokens obtained by splitting value on separator |
| 137 | */ |
| 138 | static List<String> splitOnUnescapedSeparator(String value, char separator) { |
| 139 | if (separator == '\\') { |
| 140 | throw new IllegalArgumentException("separator '\\' is not supported"); |
| 141 | } |
| 142 | ArrayList<String> tokens = new ArrayList<>(); |
| 143 | StringBuilder currentToken = new StringBuilder(); |
| 144 | boolean inEscapeState = false; |
| 145 | for (int pos = 0; pos < value.length(); pos++) { |
| 146 | char c = value.charAt(pos); |
| 147 | if (inEscapeState) { |
| 148 | currentToken.append(c); |
| 149 | inEscapeState = false; |
| 150 | } else if (c == '\\') { |
| 151 | inEscapeState = true; |
| 152 | } else if (c == separator) { |
| 153 | // Do not emit empty tokens between consecutive separators. |
| 154 | if (currentToken.length() > 0) { |
| 155 | tokens.add(currentToken.toString()); |
| 156 | } |
| 157 | currentToken.setLength(0); |
| 158 | } else { |
| 159 | currentToken.append(c); |
| 160 | } |
| 161 | } |
| 162 | if (currentToken.length() > 0) { |
| 163 | tokens.add(currentToken.toString()); |
| 164 | } |
| 165 | return Collections.unmodifiableList(tokens); |
| 166 | } |
| 167 | } |