blob: 53179b79b2408fb30764058e81011a653e35869a [file] [log] [blame]
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +02001/*
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
17package com.code_intelligence.jazzer.driver;
18
19import static java.lang.System.err;
20import static java.lang.System.exit;
21
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.List;
25import java.util.Set;
26import java.util.stream.Collectors;
27import 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 Meumertzheim07ce6172022-08-11 10:44:16 +020036public final class Opt {
Fabian Meumertzheim193908f2022-08-11 11:38:38 +020037 private static final char SYSTEM_DELIMITER =
38 System.getProperty("os.name").startsWith("Windows") ? ';' : ':';
39
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020040 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 Meumertzheim193908f2022-08-11 11:38:38 +020044 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 Meumertzheim678ceb52022-07-28 18:58:47 +020049 public static final boolean hooks = boolSetting("hooks", true);
Fabian Meumertzheim5a2137e2022-08-11 10:00:30 +020050 public static final String idSyncFile = stringSetting("id_sync_file", null);
Fabian Meumertzheim193908f2022-08-11 11:38:38 +020051 public static final List<String> instrumentationIncludes =
52 stringListSetting("instrumentation_includes");
53 public static final List<String> instrumentationExcludes =
54 stringListSetting("instrumentation_excludes");
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020055 public static final Set<Long> ignore =
56 Collections.unmodifiableSet(stringListSetting("ignore", ',')
57 .stream()
58 .map(Long::parseUnsignedLong)
59 .collect(Collectors.toSet()));
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020060 public static final String reproducerPath = stringSetting("reproducer_path", ".");
Fabian Meumertzheim193908f2022-08-11 11:38:38 +020061 public static final String targetClass = stringSetting("target_class", "");
62 public static final List<String> trace = stringListSetting("trace");
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020063
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 Meumertzheim6ee1c1c2022-08-18 10:11:50 +020070 uint64Setting("keep_going", autofuzz.isEmpty() ? 1 : Long.MAX_VALUE);
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020071
Fabian Meumertzheim193908f2022-08-11 11:38:38 +020072 // 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 Meumertzheimc90c0732022-08-11 12:22:10 +020074 public static final boolean dedup = boolSetting("dedup", hooks);
Fabian Meumertzheim193908f2022-08-11 11:38:38 +020075
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +020076 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 Meumertzheim193908f2022-08-11 11:38:38 +0200102 private static List<String> stringListSetting(String name) {
103 return stringListSetting(name, SYSTEM_DELIMITER);
104 }
105
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +0200106 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 Meumertzheim6ee1c1c2022-08-18 10:11:50 +0200122 private static long uint64Setting(String name, long defaultValue) {
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +0200123 String value = System.getProperty(optionsPrefix + name);
124 if (value == null) {
125 return defaultValue;
126 }
Fabian Meumertzheim6ee1c1c2022-08-18 10:11:50 +0200127 return Long.parseUnsignedLong(value, 10);
Fabian Meumertzheim678ceb52022-07-28 18:58:47 +0200128 }
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}