| 1 |
// Copyright (c) 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <cassert> |
| 16 |
#include <cerrno> |
| 17 |
#include <cstring> |
| 18 |
#include <fstream> |
| 19 |
#include <memory> |
| 20 |
#include <random> |
| 21 |
#include <sstream> |
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include "source/fuzz/force_render_red.h" |
| 25 |
#include "source/fuzz/fuzzer.h" |
| 26 |
#include "source/fuzz/fuzzer_util.h" |
| 27 |
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h" |
| 28 |
#include "source/fuzz/pseudo_random_generator.h" |
| 29 |
#include "source/fuzz/replayer.h" |
| 30 |
#include "source/fuzz/shrinker.h" |
| 31 |
#include "source/opt/build_module.h" |
| 32 |
#include "source/opt/ir_context.h" |
| 33 |
#include "source/opt/log.h" |
| 34 |
#include "source/spirv_fuzzer_options.h" |
| 35 |
#include "source/util/make_unique.h" |
| 36 |
#include "source/util/string_utils.h" |
| 37 |
#include "tools/io.h" |
| 38 |
#include "tools/util/cli_consumer.h" |
| 39 |
|
| 40 |
namespace { |
| 41 |
|
| 42 |
// Check that the std::system function can actually be used. |
| 43 |
bool CheckExecuteCommand() { |
| 44 |
int res = std::system(nullptr); |
| 45 |
return res != 0; |
| 46 |
} |
| 47 |
|
| 48 |
// Execute a command using the shell. |
| 49 |
// Returns true if and only if the command's exit status was 0. |
| 50 |
bool ExecuteCommand(const std::string& command) { |
| 51 |
errno = 0; |
| 52 |
int status = std::system(command.c_str()); |
| 53 |
assert(errno == 0 && "failed to execute command"); |
| 54 |
// The result returned by 'system' is implementation-defined, but is |
| 55 |
// usually the case that the returned value is 0 when the command's exit |
| 56 |
// code was 0. We are assuming that here, and that's all we depend on. |
| 57 |
return status == 0; |
| 58 |
} |
| 59 |
|
| 60 |
// Status and actions to perform after parsing command-line arguments. |
| 61 |
enum class FuzzActions { |
| 62 |
FORCE_RENDER_RED, // Turn the shader into a form such that it is guaranteed |
| 63 |
// to render a red image. |
| 64 |
FUZZ, // Run the fuzzer to apply transformations in a randomized fashion. |
| 65 |
REPLAY, // Replay an existing sequence of transformations. |
| 66 |
SHRINK, // Shrink an existing sequence of transformations with respect to an |
| 67 |
// interestingness function. |
| 68 |
STOP // Do nothing. |
| 69 |
}; |
| 70 |
|
| 71 |
struct FuzzStatus { |
| 72 |
FuzzActions action; |
| 73 |
int code; |
| 74 |
}; |
| 75 |
|
| 76 |
void PrintUsage(const char* program) { |
| 77 |
// NOTE: Please maintain flags in lexicographical order. |
| 78 |
printf( |
| 79 |
R"(%s - Fuzzes an equivalent SPIR-V binary based on a given binary. |
| 80 |
|
| 81 |
USAGE: %s [options] <input.spv> -o <output.spv> \ |
| 82 |
--donors=<donors.txt> |
| 83 |
USAGE: %s [options] <input.spv> -o <output.spv> \ |
| 84 |
--shrink=<input.transformations> -- <interestingness_test> [args...] |
| 85 |
|
| 86 |
The SPIR-V binary is read from <input.spv>. If <input.facts> is also present, |
| 87 |
facts about the SPIR-V binary are read from this file. |
| 88 |
|
| 89 |
The transformed SPIR-V binary is written to <output.spv>. Human-readable and |
| 90 |
binary representations of the transformations that were applied are written to |
| 91 |
<output.transformations_json> and <output.transformations>, respectively. |
| 92 |
|
| 93 |
When passing --shrink=<input.transformations> an <interestingness_test> |
| 94 |
must also be provided; this is the path to a script that returns 0 if and only |
| 95 |
if a given SPIR-V binary is interesting. The SPIR-V binary will be passed to |
| 96 |
the script as an argument after any other provided arguments [args...]. The |
| 97 |
"--" characters are optional but denote that all arguments that follow are |
| 98 |
positional arguments and thus will be forwarded to the interestingness script, |
| 99 |
and not parsed by %s. |
| 100 |
|
| 101 |
NOTE: The fuzzer is a work in progress. |
| 102 |
|
| 103 |
Options (in lexicographical order): |
| 104 |
|
| 105 |
-h, --help |
| 106 |
Print this help. |
| 107 |
--donors= |
| 108 |
File specifying a series of donor files, one per line. Must be |
| 109 |
provided if the tool is invoked in fuzzing mode; incompatible |
| 110 |
with replay and shrink modes. The file should be empty if no |
| 111 |
donors are to be used. |
| 112 |
--enable-all-passes |
| 113 |
By default, spirv-fuzz follows the philosophy of "swarm testing" |
| 114 |
(Groce et al., 2012): only a subset of fuzzer passes are enabled |
| 115 |
on any given fuzzer run, with the subset being chosen randomly. |
| 116 |
This flag instead forces *all* fuzzer passes to be enabled. When |
| 117 |
running spirv-fuzz many times this is likely to produce *less* |
| 118 |
diverse fuzzed modules than when swarm testing is used. The |
| 119 |
purpose of the flag is to allow that hypothesis to be tested. |
| 120 |
--force-render-red |
| 121 |
Transforms the input shader into a shader that writes red to the |
| 122 |
output buffer, and then captures the original shader as the body |
| 123 |
of a conditional with a dynamically false guard. Exploits input |
| 124 |
facts to make the guard non-obviously false. This option is a |
| 125 |
helper for massaging crash-inducing tests into a runnable |
| 126 |
format; it does not perform any fuzzing. |
| 127 |
--fuzzer-pass-validation |
| 128 |
Run the validator after applying each fuzzer pass during |
| 129 |
fuzzing. Aborts fuzzing early if an invalid binary is created. |
| 130 |
Useful for debugging spirv-fuzz. |
| 131 |
--repeated-pass-strategy= |
| 132 |
Available strategies are: |
| 133 |
- looped (the default): a sequence of fuzzer passes is chosen at |
| 134 |
the start of fuzzing, via randomly choosing enabled passes, and |
| 135 |
augmenting these choices with fuzzer passes that it is |
| 136 |
recommended to run subsequently. Fuzzing then involves |
| 137 |
repeatedly applying this fixed sequence of passes. |
| 138 |
- random: each time a fuzzer pass is requested, this strategy |
| 139 |
either provides one at random from the set of enabled passes, |
| 140 |
or provides a pass that has been recommended based on a pass |
| 141 |
that was used previously. |
| 142 |
- simple: each time a fuzzer pass is requested, one is provided |
| 143 |
at random from the set of enabled passes. |
| 144 |
--replay |
| 145 |
File from which to read a sequence of transformations to replay |
| 146 |
(instead of fuzzing) |
| 147 |
--replay-range= |
| 148 |
Signed 32-bit integer. If set to a positive value N, only the |
| 149 |
first N transformations will be applied during replay. If set to |
| 150 |
a negative value -N, all but the final N transformations will be |
| 151 |
applied during replay. If set to 0 (the default), all |
| 152 |
transformations will be applied during replay. Ignored unless |
| 153 |
--replay is used. |
| 154 |
--replay-validation |
| 155 |
Run the validator after applying each transformation during |
| 156 |
replay (including the replay that occurs during shrinking). |
| 157 |
Aborts if an invalid binary is created. Useful for debugging |
| 158 |
spirv-fuzz. |
| 159 |
--seed= |
| 160 |
Unsigned 32-bit integer seed to control random number |
| 161 |
generation. |
| 162 |
--shrink= |
| 163 |
File from which to read a sequence of transformations to shrink |
| 164 |
(instead of fuzzing) |
| 165 |
--shrinker-step-limit= |
| 166 |
Unsigned 32-bit integer specifying maximum number of steps the |
| 167 |
shrinker will take before giving up. Ignored unless --shrink |
| 168 |
is used. |
| 169 |
--shrinker-temp-file-prefix= |
| 170 |
Specifies a temporary file prefix that will be used to output |
| 171 |
temporary shader files during shrinking. A number and .spv |
| 172 |
extension will be added. The default is "temp_", which will |
| 173 |
cause files like "temp_0001.spv" to be output to the current |
| 174 |
directory. Ignored unless --shrink is used. |
| 175 |
--version |
| 176 |
Display fuzzer version information. |
| 177 |
|
| 178 |
Supported validator options are as follows. See `spirv-val --help` for details. |
| 179 |
--before-hlsl-legalization |
| 180 |
--relax-block-layout |
| 181 |
--relax-logical-pointer |
| 182 |
--relax-struct-store |
| 183 |
--scalar-block-layout |
| 184 |
--skip-block-layout |
| 185 |
)", |
| 186 |
program, program, program, program); |
| 187 |
} |
| 188 |
|
| 189 |
// Message consumer for this tool. Used to emit diagnostics during |
| 190 |
// initialization and setup. Note that |source| and |position| are irrelevant |
| 191 |
// here because we are still not processing a SPIR-V input file. |
| 192 |
void FuzzDiagnostic(spv_message_level_t level, const char* /*source*/, |
| 193 |
const spv_position_t& /*position*/, const char* message) { |
| 194 |
if (level == SPV_MSG_ERROR) { |
| 195 |
fprintf(stderr, "error: "); |
| 196 |
} |
| 197 |
fprintf(stderr, "%s\n", message); |
| 198 |
} |
| 199 |
|
| 200 |
FuzzStatus ParseFlags( |
| 201 |
int argc, const char** argv, std::string* in_binary_file, |
| 202 |
std::string* out_binary_file, std::string* donors_file, |
| 203 |
std::string* replay_transformations_file, |
| 204 |
std::vector<std::string>* interestingness_test, |
| 205 |
std::string* shrink_transformations_file, |
| 206 |
std::string* shrink_temp_file_prefix, |
| 207 |
spvtools::fuzz::Fuzzer::RepeatedPassStrategy* repeated_pass_strategy, |
| 208 |
spvtools::FuzzerOptions* fuzzer_options, |
| 209 |
spvtools::ValidatorOptions* validator_options) { |
| 210 |
uint32_t positional_arg_index = 0; |
| 211 |
bool only_positional_arguments_remain = false; |
| 212 |
bool force_render_red = false; |
| 213 |
|
| 214 |
*repeated_pass_strategy = |
| 215 |
spvtools::fuzz::Fuzzer::RepeatedPassStrategy::kLoopedWithRecommendations; |
| 216 |
|
| 217 |
for (int argi = 1; argi < argc; ++argi) { |
| 218 |
const char* cur_arg = argv[argi]; |
| 219 |
if ('-' == cur_arg[0] && !only_positional_arguments_remain) { |
| 220 |
if (0 == strcmp(cur_arg, "--version")) { |
| 221 |
spvtools::Logf(FuzzDiagnostic, SPV_MSG_INFO, nullptr, {}, "%s\n", |
| 222 |
spvSoftwareVersionDetailsString()); |
| 223 |
return {FuzzActions::STOP, 0}; |
| 224 |
} else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) { |
| 225 |
PrintUsage(argv[0]); |
| 226 |
return {FuzzActions::STOP, 0}; |
| 227 |
} else if (0 == strcmp(cur_arg, "-o")) { |
| 228 |
if (out_binary_file->empty() && argi + 1 < argc) { |
| 229 |
*out_binary_file = std::string(argv[++argi]); |
| 230 |
} else { |
| 231 |
PrintUsage(argv[0]); |
| 232 |
return {FuzzActions::STOP, 1}; |
| 233 |
} |
| 234 |
} else if (0 == strncmp(cur_arg, "--donors=", sizeof("--donors=") - 1)) { |
| 235 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 236 |
*donors_file = std::string(split_flag.second); |
| 237 |
} else if (0 == strncmp(cur_arg, "--enable-all-passes", |
| 238 |
sizeof("--enable-all-passes") - 1)) { |
| 239 |
fuzzer_options->enable_all_passes(); |
| 240 |
} else if (0 == strncmp(cur_arg, "--force-render-red", |
| 241 |
sizeof("--force-render-red") - 1)) { |
| 242 |
force_render_red = true; |
| 243 |
} else if (0 == strncmp(cur_arg, "--fuzzer-pass-validation", |
| 244 |
sizeof("--fuzzer-pass-validation") - 1)) { |
| 245 |
fuzzer_options->enable_fuzzer_pass_validation(); |
| 246 |
} else if (0 == strncmp(cur_arg, "--replay=", sizeof("--replay=") - 1)) { |
| 247 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 248 |
*replay_transformations_file = std::string(split_flag.second); |
| 249 |
} else if (0 == strncmp(cur_arg, "--repeated-pass-strategy=", |
| 250 |
sizeof("--repeated-pass-strategy=") - 1)) { |
| 251 |
std::string strategy = spvtools::utils::SplitFlagArgs(cur_arg).second; |
| 252 |
if (strategy == "looped") { |
| 253 |
*repeated_pass_strategy = spvtools::fuzz::Fuzzer:: |
| 254 |
RepeatedPassStrategy::kLoopedWithRecommendations; |
| 255 |
} else if (strategy == "random") { |
| 256 |
*repeated_pass_strategy = spvtools::fuzz::Fuzzer:: |
| 257 |
RepeatedPassStrategy::kRandomWithRecommendations; |
| 258 |
} else if (strategy == "simple") { |
| 259 |
*repeated_pass_strategy = |
| 260 |
spvtools::fuzz::Fuzzer::RepeatedPassStrategy::kSimple; |
| 261 |
} else { |
| 262 |
std::stringstream ss; |
| 263 |
ss << "Unknown repeated pass strategy '" << strategy << "'" |
| 264 |
<< std::endl; |
| 265 |
ss << "Valid options are 'looped', 'random' and 'simple'."; |
| 266 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, ss.str().c_str()); |
| 267 |
return {FuzzActions::STOP, 1}; |
| 268 |
} |
| 269 |
} else if (0 == strncmp(cur_arg, "--replay-range=", |
| 270 |
sizeof("--replay-range=") - 1)) { |
| 271 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 272 |
char* end = nullptr; |
| 273 |
errno = 0; |
| 274 |
const auto replay_range = |
| 275 |
static_cast<int32_t>(strtol(split_flag.second.c_str(), &end, 10)); |
| 276 |
assert(end != split_flag.second.c_str() && errno == 0); |
| 277 |
fuzzer_options->set_replay_range(replay_range); |
| 278 |
} else if (0 == strncmp(cur_arg, "--replay-validation", |
| 279 |
sizeof("--replay-validation") - 1)) { |
| 280 |
fuzzer_options->enable_replay_validation(); |
| 281 |
} else if (0 == strncmp(cur_arg, "--shrink=", sizeof("--shrink=") - 1)) { |
| 282 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 283 |
*shrink_transformations_file = std::string(split_flag.second); |
| 284 |
} else if (0 == strncmp(cur_arg, "--seed=", sizeof("--seed=") - 1)) { |
| 285 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 286 |
char* end = nullptr; |
| 287 |
errno = 0; |
| 288 |
const auto seed = |
| 289 |
static_cast<uint32_t>(strtol(split_flag.second.c_str(), &end, 10)); |
| 290 |
assert(end != split_flag.second.c_str() && errno == 0); |
| 291 |
fuzzer_options->set_random_seed(seed); |
| 292 |
} else if (0 == strncmp(cur_arg, "--shrinker-step-limit=", |
| 293 |
sizeof("--shrinker-step-limit=") - 1)) { |
| 294 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 295 |
char* end = nullptr; |
| 296 |
errno = 0; |
| 297 |
const auto step_limit = |
| 298 |
static_cast<uint32_t>(strtol(split_flag.second.c_str(), &end, 10)); |
| 299 |
assert(end != split_flag.second.c_str() && errno == 0); |
| 300 |
fuzzer_options->set_shrinker_step_limit(step_limit); |
| 301 |
} else if (0 == strncmp(cur_arg, "--shrinker-temp-file-prefix=", |
| 302 |
sizeof("--shrinker-temp-file-prefix=") - 1)) { |
| 303 |
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg); |
| 304 |
*shrink_temp_file_prefix = std::string(split_flag.second); |
| 305 |
} else if (0 == strcmp(cur_arg, "--before-hlsl-legalization")) { |
| 306 |
validator_options->SetBeforeHlslLegalization(true); |
| 307 |
} else if (0 == strcmp(cur_arg, "--relax-logical-pointer")) { |
| 308 |
validator_options->SetRelaxLogicalPointer(true); |
| 309 |
} else if (0 == strcmp(cur_arg, "--relax-block-layout")) { |
| 310 |
validator_options->SetRelaxBlockLayout(true); |
| 311 |
} else if (0 == strcmp(cur_arg, "--scalar-block-layout")) { |
| 312 |
validator_options->SetScalarBlockLayout(true); |
| 313 |
} else if (0 == strcmp(cur_arg, "--skip-block-layout")) { |
| 314 |
validator_options->SetSkipBlockLayout(true); |
| 315 |
} else if (0 == strcmp(cur_arg, "--relax-struct-store")) { |
| 316 |
validator_options->SetRelaxStructStore(true); |
| 317 |
} else if (0 == strcmp(cur_arg, "--")) { |
| 318 |
only_positional_arguments_remain = true; |
| 319 |
} else { |
| 320 |
std::stringstream ss; |
| 321 |
ss << "Unrecognized argument: " << cur_arg << std::endl; |
| 322 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, ss.str().c_str()); |
| 323 |
PrintUsage(argv[0]); |
| 324 |
return {FuzzActions::STOP, 1}; |
| 325 |
} |
| 326 |
} else if (positional_arg_index == 0) { |
| 327 |
// Binary input file name |
| 328 |
assert(in_binary_file->empty()); |
| 329 |
*in_binary_file = std::string(cur_arg); |
| 330 |
positional_arg_index++; |
| 331 |
} else { |
| 332 |
interestingness_test->push_back(std::string(cur_arg)); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if (in_binary_file->empty()) { |
| 337 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "No input file specified"); |
| 338 |
return {FuzzActions::STOP, 1}; |
| 339 |
} |
| 340 |
|
| 341 |
if (out_binary_file->empty()) { |
| 342 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "-o required"); |
| 343 |
return {FuzzActions::STOP, 1}; |
| 344 |
} |
| 345 |
|
| 346 |
auto const_fuzzer_options = |
| 347 |
static_cast<spv_const_fuzzer_options>(*fuzzer_options); |
| 348 |
if (force_render_red) { |
| 349 |
if (!replay_transformations_file->empty() || |
| 350 |
!shrink_transformations_file->empty() || |
| 351 |
const_fuzzer_options->replay_validation_enabled) { |
| 352 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 353 |
"The --force-render-red argument cannot be used with any " |
| 354 |
"other arguments except -o."); |
| 355 |
return {FuzzActions::STOP, 1}; |
| 356 |
} |
| 357 |
return {FuzzActions::FORCE_RENDER_RED, 0}; |
| 358 |
} |
| 359 |
|
| 360 |
if (replay_transformations_file->empty() && |
| 361 |
shrink_transformations_file->empty() && |
| 362 |
static_cast<spv_const_fuzzer_options>(*fuzzer_options) |
| 363 |
->replay_validation_enabled) { |
| 364 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 365 |
"The --replay-validation argument can only be used with " |
| 366 |
"one of the --replay or --shrink arguments."); |
| 367 |
return {FuzzActions::STOP, 1}; |
| 368 |
} |
| 369 |
|
| 370 |
if (shrink_transformations_file->empty() && !interestingness_test->empty()) { |
| 371 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 372 |
"Too many positional arguments specified; extra positional " |
| 373 |
"arguments are used as the interestingness function, which " |
| 374 |
"are only valid with the --shrink option."); |
| 375 |
return {FuzzActions::STOP, 1}; |
| 376 |
} |
| 377 |
|
| 378 |
if (!shrink_transformations_file->empty() && interestingness_test->empty()) { |
| 379 |
spvtools::Error( |
| 380 |
FuzzDiagnostic, nullptr, {}, |
| 381 |
"The --shrink option requires an interestingness function."); |
| 382 |
return {FuzzActions::STOP, 1}; |
| 383 |
} |
| 384 |
|
| 385 |
if (!replay_transformations_file->empty() || |
| 386 |
!shrink_transformations_file->empty()) { |
| 387 |
// Donors should not be provided when replaying or shrinking: they only make |
| 388 |
// sense during fuzzing. |
| 389 |
if (!donors_file->empty()) { |
| 390 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 391 |
"The --donors argument is not compatible with --replay " |
| 392 |
"nor --shrink."); |
| 393 |
return {FuzzActions::STOP, 1}; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (!replay_transformations_file->empty()) { |
| 398 |
// A replay transformations file was given, thus the tool is being invoked |
| 399 |
// in replay mode. |
| 400 |
if (!shrink_transformations_file->empty()) { |
| 401 |
spvtools::Error( |
| 402 |
FuzzDiagnostic, nullptr, {}, |
| 403 |
"The --replay and --shrink arguments are mutually exclusive."); |
| 404 |
return {FuzzActions::STOP, 1}; |
| 405 |
} |
| 406 |
return {FuzzActions::REPLAY, 0}; |
| 407 |
} |
| 408 |
|
| 409 |
if (!shrink_transformations_file->empty()) { |
| 410 |
// The tool is being invoked in shrink mode. |
| 411 |
assert(!interestingness_test->empty() && |
| 412 |
"An error should have been raised if --shrink was provided without " |
| 413 |
"an interestingness test."); |
| 414 |
return {FuzzActions::SHRINK, 0}; |
| 415 |
} |
| 416 |
|
| 417 |
// The tool is being invoked in fuzz mode. |
| 418 |
if (donors_file->empty()) { |
| 419 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 420 |
"Fuzzing requires that the --donors option is used."); |
| 421 |
return {FuzzActions::STOP, 1}; |
| 422 |
} |
| 423 |
return {FuzzActions::FUZZ, 0}; |
| 424 |
} |
| 425 |
|
| 426 |
bool ParseTransformations( |
| 427 |
const std::string& transformations_file, |
| 428 |
spvtools::fuzz::protobufs::TransformationSequence* transformations) { |
| 429 |
std::ifstream transformations_stream; |
| 430 |
transformations_stream.open(transformations_file, |
| 431 |
std::ios::in | std::ios::binary); |
| 432 |
auto parse_success = |
| 433 |
transformations->ParseFromIstream(&transformations_stream); |
| 434 |
transformations_stream.close(); |
| 435 |
if (!parse_success) { |
| 436 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 437 |
("Error reading transformations from file '" + |
| 438 |
transformations_file + "'") |
| 439 |
.c_str()); |
| 440 |
return false; |
| 441 |
} |
| 442 |
return true; |
| 443 |
} |
| 444 |
|
| 445 |
bool Replay(const spv_target_env& target_env, |
| 446 |
spv_const_fuzzer_options fuzzer_options, |
| 447 |
spv_validator_options validator_options, |
| 448 |
const std::vector<uint32_t>& binary_in, |
| 449 |
const spvtools::fuzz::protobufs::FactSequence& initial_facts, |
| 450 |
const std::string& replay_transformations_file, |
| 451 |
std::vector<uint32_t>* binary_out, |
| 452 |
spvtools::fuzz::protobufs::TransformationSequence* |
| 453 |
transformations_applied) { |
| 454 |
spvtools::fuzz::protobufs::TransformationSequence transformation_sequence; |
| 455 |
if (!ParseTransformations(replay_transformations_file, |
| 456 |
&transformation_sequence)) { |
| 457 |
return false; |
| 458 |
} |
| 459 |
|
| 460 |
uint32_t num_transformations_to_apply; |
| 461 |
if (fuzzer_options->replay_range > 0) { |
| 462 |
// We have a positive replay range, N. We would like transformations |
| 463 |
// [0, N), truncated to the number of available transformations if N is too |
| 464 |
// large. |
| 465 |
num_transformations_to_apply = static_cast<uint32_t>( |
| 466 |
std::min(fuzzer_options->replay_range, |
| 467 |
transformation_sequence.transformation_size())); |
| 468 |
} else { |
| 469 |
// We have non-positive replay range, -N (where N may be 0). We would like |
| 470 |
// transformations [0, num_transformations - N), or no transformations if N |
| 471 |
// is too large. |
| 472 |
num_transformations_to_apply = static_cast<uint32_t>( |
| 473 |
std::max(0, transformation_sequence.transformation_size() + |
| 474 |
fuzzer_options->replay_range)); |
| 475 |
} |
| 476 |
|
| 477 |
auto replay_result = |
| 478 |
spvtools::fuzz::Replayer( |
| 479 |
target_env, spvtools::utils::CLIMessageConsumer, binary_in, |
| 480 |
initial_facts, transformation_sequence, num_transformations_to_apply, |
| 481 |
fuzzer_options->replay_validation_enabled, validator_options) |
| 482 |
.Run(); |
| 483 |
replay_result.transformed_module->module()->ToBinary(binary_out, false); |
| 484 |
*transformations_applied = std::move(replay_result.applied_transformations); |
| 485 |
return replay_result.status == |
| 486 |
spvtools::fuzz::Replayer::ReplayerResultStatus::kComplete; |
| 487 |
} |
| 488 |
|
| 489 |
bool Shrink(const spv_target_env& target_env, |
| 490 |
spv_const_fuzzer_options fuzzer_options, |
| 491 |
spv_validator_options validator_options, |
| 492 |
const std::vector<uint32_t>& binary_in, |
| 493 |
const spvtools::fuzz::protobufs::FactSequence& initial_facts, |
| 494 |
const std::string& shrink_transformations_file, |
| 495 |
const std::string& shrink_temp_file_prefix, |
| 496 |
const std::vector<std::string>& interestingness_command, |
| 497 |
std::vector<uint32_t>* binary_out, |
| 498 |
spvtools::fuzz::protobufs::TransformationSequence* |
| 499 |
transformations_applied) { |
| 500 |
spvtools::fuzz::protobufs::TransformationSequence transformation_sequence; |
| 501 |
if (!ParseTransformations(shrink_transformations_file, |
| 502 |
&transformation_sequence)) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
assert(!interestingness_command.empty() && |
| 506 |
"An error should have been raised because the interestingness_command " |
| 507 |
"is empty."); |
| 508 |
std::stringstream joined; |
| 509 |
joined << interestingness_command[0]; |
| 510 |
for (size_t i = 1, size = interestingness_command.size(); i < size; ++i) { |
| 511 |
joined << " " << interestingness_command[i]; |
| 512 |
} |
| 513 |
std::string interestingness_command_joined = joined.str(); |
| 514 |
|
| 515 |
spvtools::fuzz::Shrinker::InterestingnessFunction interestingness_function = |
| 516 |
[interestingness_command_joined, shrink_temp_file_prefix]( |
| 517 |
std::vector<uint32_t> binary, uint32_t reductions_applied) -> bool { |
| 518 |
std::stringstream ss; |
| 519 |
ss << shrink_temp_file_prefix << std::setw(4) << std::setfill('0') |
| 520 |
<< reductions_applied << ".spv"; |
| 521 |
const auto spv_file = ss.str(); |
| 522 |
const std::string command = interestingness_command_joined + " " + spv_file; |
| 523 |
auto write_file_succeeded = |
| 524 |
WriteFile(spv_file.c_str(), "wb", &binary[0], binary.size()); |
| 525 |
(void)(write_file_succeeded); |
| 526 |
assert(write_file_succeeded); |
| 527 |
return ExecuteCommand(command); |
| 528 |
}; |
| 529 |
|
| 530 |
auto shrink_result = |
| 531 |
spvtools::fuzz::Shrinker( |
| 532 |
target_env, spvtools::utils::CLIMessageConsumer, binary_in, |
| 533 |
initial_facts, transformation_sequence, interestingness_function, |
| 534 |
fuzzer_options->shrinker_step_limit, |
| 535 |
fuzzer_options->replay_validation_enabled, validator_options) |
| 536 |
.Run(); |
| 537 |
|
| 538 |
*binary_out = std::move(shrink_result.transformed_binary); |
| 539 |
*transformations_applied = std::move(shrink_result.applied_transformations); |
| 540 |
return spvtools::fuzz::Shrinker::ShrinkerResultStatus::kComplete == |
| 541 |
shrink_result.status || |
| 542 |
spvtools::fuzz::Shrinker::ShrinkerResultStatus::kStepLimitReached == |
| 543 |
shrink_result.status; |
| 544 |
} |
| 545 |
|
| 546 |
bool Fuzz(const spv_target_env& target_env, |
| 547 |
spv_const_fuzzer_options fuzzer_options, |
| 548 |
spv_validator_options validator_options, |
| 549 |
const std::vector<uint32_t>& binary_in, |
| 550 |
const spvtools::fuzz::protobufs::FactSequence& initial_facts, |
| 551 |
const std::string& donors, |
| 552 |
spvtools::fuzz::Fuzzer::RepeatedPassStrategy repeated_pass_strategy, |
| 553 |
std::vector<uint32_t>* binary_out, |
| 554 |
spvtools::fuzz::protobufs::TransformationSequence* |
| 555 |
transformations_applied) { |
| 556 |
auto message_consumer = spvtools::utils::CLIMessageConsumer; |
| 557 |
|
| 558 |
std::vector<spvtools::fuzz::fuzzerutil::ModuleSupplier> donor_suppliers; |
| 559 |
|
| 560 |
std::ifstream donors_file(donors); |
| 561 |
if (!donors_file) { |
| 562 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "Error opening donors file"); |
| 563 |
return false; |
| 564 |
} |
| 565 |
std::string donor_filename; |
| 566 |
while (std::getline(donors_file, donor_filename)) { |
| 567 |
donor_suppliers.emplace_back( |
| 568 |
[donor_filename, message_consumer, |
| 569 |
target_env]() -> std::unique_ptr<spvtools::opt::IRContext> { |
| 570 |
std::vector<uint32_t> donor_binary; |
| 571 |
if (!ReadFile<uint32_t>(donor_filename.c_str(), "rb", |
| 572 |
&donor_binary)) { |
| 573 |
return nullptr; |
| 574 |
} |
| 575 |
return spvtools::BuildModule(target_env, message_consumer, |
| 576 |
donor_binary.data(), |
| 577 |
donor_binary.size()); |
| 578 |
}); |
| 579 |
} |
| 580 |
|
| 581 |
auto fuzz_result = |
| 582 |
spvtools::fuzz::Fuzzer( |
| 583 |
target_env, message_consumer, binary_in, initial_facts, |
| 584 |
donor_suppliers, |
| 585 |
spvtools::MakeUnique<spvtools::fuzz::PseudoRandomGenerator>( |
| 586 |
fuzzer_options->has_random_seed |
| 587 |
? fuzzer_options->random_seed |
| 588 |
: static_cast<uint32_t>(std::random_device()())), |
| 589 |
fuzzer_options->all_passes_enabled, repeated_pass_strategy, |
| 590 |
fuzzer_options->fuzzer_pass_validation_enabled, validator_options) |
| 591 |
.Run(); |
| 592 |
*binary_out = std::move(fuzz_result.transformed_binary); |
| 593 |
*transformations_applied = std::move(fuzz_result.applied_transformations); |
| 594 |
if (fuzz_result.status != |
| 595 |
spvtools::fuzz::Fuzzer::FuzzerResultStatus::kComplete) { |
| 596 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "Error running fuzzer"); |
| 597 |
return false; |
| 598 |
} |
| 599 |
return true; |
| 600 |
} |
| 601 |
|
| 602 |
} // namespace |
| 603 |
|
| 604 |
// Dumps |binary| to file |filename|. Useful for interactive debugging. |
| 605 |
void DumpShader(const std::vector<uint32_t>& binary, const char* filename) { |
| 606 |
auto write_file_succeeded = |
| 607 |
WriteFile(filename, "wb", &binary[0], binary.size()); |
| 608 |
if (!write_file_succeeded) { |
| 609 |
std::cerr << "Failed to dump shader" << std::endl; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
// Dumps the SPIRV-V module in |context| to file |filename|. Useful for |
| 614 |
// interactive debugging. |
| 615 |
void DumpShader(spvtools::opt::IRContext* context, const char* filename) { |
| 616 |
std::vector<uint32_t> binary; |
| 617 |
context->module()->ToBinary(&binary, false); |
| 618 |
DumpShader(binary, filename); |
| 619 |
} |
| 620 |
|
| 621 |
// Dumps |transformations| to file |filename| in binary format. Useful for |
| 622 |
// interactive debugging. |
| 623 |
void DumpTransformationsBinary( |
| 624 |
const spvtools::fuzz::protobufs::TransformationSequence& transformations, |
| 625 |
const char* filename) { |
| 626 |
std::ofstream transformations_file; |
| 627 |
transformations_file.open(filename, std::ios::out | std::ios::binary); |
| 628 |
transformations.SerializeToOstream(&transformations_file); |
| 629 |
transformations_file.close(); |
| 630 |
} |
| 631 |
|
| 632 |
// Dumps |transformations| to file |filename| in JSON format. Useful for |
| 633 |
// interactive debugging. |
| 634 |
void DumpTransformationsJson( |
| 635 |
const spvtools::fuzz::protobufs::TransformationSequence& transformations, |
| 636 |
const char* filename) { |
| 637 |
std::string json_string; |
| 638 |
auto json_options = google::protobuf::util::JsonOptions(); |
| 639 |
json_options.add_whitespace = true; |
| 640 |
auto json_generation_status = google::protobuf::util::MessageToJsonString( |
| 641 |
transformations, &json_string, json_options); |
| 642 |
if (json_generation_status == google::protobuf::util::Status::OK) { |
| 643 |
std::ofstream transformations_json_file(filename); |
| 644 |
transformations_json_file << json_string; |
| 645 |
transformations_json_file.close(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_3; |
| 650 |
|
| 651 |
int main(int argc, const char** argv) { |
| 652 |
std::string in_binary_file; |
| 653 |
std::string out_binary_file; |
| 654 |
std::string donors_file; |
| 655 |
std::string replay_transformations_file; |
| 656 |
std::vector<std::string> interestingness_test; |
| 657 |
std::string shrink_transformations_file; |
| 658 |
std::string shrink_temp_file_prefix = "temp_"; |
| 659 |
spvtools::fuzz::Fuzzer::RepeatedPassStrategy repeated_pass_strategy; |
| 660 |
|
| 661 |
spvtools::FuzzerOptions fuzzer_options; |
| 662 |
spvtools::ValidatorOptions validator_options; |
| 663 |
|
| 664 |
FuzzStatus status = |
| 665 |
ParseFlags(argc, argv, &in_binary_file, &out_binary_file, &donors_file, |
| 666 |
&replay_transformations_file, &interestingness_test, |
| 667 |
&shrink_transformations_file, &shrink_temp_file_prefix, |
| 668 |
&repeated_pass_strategy, &fuzzer_options, &validator_options); |
| 669 |
|
| 670 |
if (status.action == FuzzActions::STOP) { |
| 671 |
return status.code; |
| 672 |
} |
| 673 |
|
| 674 |
std::vector<uint32_t> binary_in; |
| 675 |
if (!ReadFile<uint32_t>(in_binary_file.c_str(), "rb", &binary_in)) { |
| 676 |
return 1; |
| 677 |
} |
| 678 |
|
| 679 |
spvtools::fuzz::protobufs::FactSequence initial_facts; |
| 680 |
|
| 681 |
// If not found, dot_pos will be std::string::npos, which can be used in |
| 682 |
// substr to mean "the end of the string"; there is no need to check the |
| 683 |
// result. |
| 684 |
size_t dot_pos = in_binary_file.rfind('.'); |
| 685 |
std::string in_facts_file = in_binary_file.substr(0, dot_pos) + ".facts"; |
| 686 |
std::ifstream facts_input(in_facts_file); |
| 687 |
if (facts_input) { |
| 688 |
std::string facts_json_string((std::istreambuf_iterator<char>(facts_input)), |
| 689 |
std::istreambuf_iterator<char>()); |
| 690 |
facts_input.close(); |
| 691 |
if (google::protobuf::util::Status::OK != |
| 692 |
google::protobuf::util::JsonStringToMessage(facts_json_string, |
| 693 |
&initial_facts)) { |
| 694 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "Error reading facts data"); |
| 695 |
return 1; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
std::vector<uint32_t> binary_out; |
| 700 |
spvtools::fuzz::protobufs::TransformationSequence transformations_applied; |
| 701 |
|
| 702 |
spv_target_env target_env = kDefaultEnvironment; |
| 703 |
|
| 704 |
switch (status.action) { |
| 705 |
case FuzzActions::FORCE_RENDER_RED: |
| 706 |
if (!spvtools::fuzz::ForceRenderRed(target_env, validator_options, |
| 707 |
binary_in, initial_facts, |
| 708 |
&binary_out)) { |
| 709 |
return 1; |
| 710 |
} |
| 711 |
break; |
| 712 |
case FuzzActions::FUZZ: |
| 713 |
if (!Fuzz(target_env, fuzzer_options, validator_options, binary_in, |
| 714 |
initial_facts, donors_file, repeated_pass_strategy, &binary_out, |
| 715 |
&transformations_applied)) { |
| 716 |
return 1; |
| 717 |
} |
| 718 |
break; |
| 719 |
case FuzzActions::REPLAY: |
| 720 |
if (!Replay(target_env, fuzzer_options, validator_options, binary_in, |
| 721 |
initial_facts, replay_transformations_file, &binary_out, |
| 722 |
&transformations_applied)) { |
| 723 |
return 1; |
| 724 |
} |
| 725 |
break; |
| 726 |
case FuzzActions::SHRINK: { |
| 727 |
if (!CheckExecuteCommand()) { |
| 728 |
std::cerr << "could not find shell interpreter for executing a command" |
| 729 |
<< std::endl; |
| 730 |
return 1; |
| 731 |
} |
| 732 |
if (!Shrink(target_env, fuzzer_options, validator_options, binary_in, |
| 733 |
initial_facts, shrink_transformations_file, |
| 734 |
shrink_temp_file_prefix, interestingness_test, &binary_out, |
| 735 |
&transformations_applied)) { |
| 736 |
return 1; |
| 737 |
} |
| 738 |
} break; |
| 739 |
default: |
| 740 |
assert(false && "Unknown fuzzer action."); |
| 741 |
break; |
| 742 |
} |
| 743 |
|
| 744 |
if (!WriteFile<uint32_t>(out_binary_file.c_str(), "wb", binary_out.data(), |
| 745 |
binary_out.size())) { |
| 746 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, "Error writing out binary"); |
| 747 |
return 1; |
| 748 |
} |
| 749 |
|
| 750 |
if (status.action != FuzzActions::FORCE_RENDER_RED) { |
| 751 |
// If not found, dot_pos will be std::string::npos, which can be used in |
| 752 |
// substr to mean "the end of the string"; there is no need to check the |
| 753 |
// result. |
| 754 |
dot_pos = out_binary_file.rfind('.'); |
| 755 |
std::string output_file_prefix = out_binary_file.substr(0, dot_pos); |
| 756 |
std::ofstream transformations_file; |
| 757 |
transformations_file.open(output_file_prefix + ".transformations", |
| 758 |
std::ios::out | std::ios::binary); |
| 759 |
bool success = |
| 760 |
transformations_applied.SerializeToOstream(&transformations_file); |
| 761 |
transformations_file.close(); |
| 762 |
if (!success) { |
| 763 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 764 |
"Error writing out transformations binary"); |
| 765 |
return 1; |
| 766 |
} |
| 767 |
|
| 768 |
std::string json_string; |
| 769 |
auto json_options = google::protobuf::util::JsonOptions(); |
| 770 |
json_options.add_whitespace = true; |
| 771 |
auto json_generation_status = google::protobuf::util::MessageToJsonString( |
| 772 |
transformations_applied, &json_string, json_options); |
| 773 |
if (json_generation_status != google::protobuf::util::Status::OK) { |
| 774 |
spvtools::Error(FuzzDiagnostic, nullptr, {}, |
| 775 |
"Error writing out transformations in JSON format"); |
| 776 |
return 1; |
| 777 |
} |
| 778 |
|
| 779 |
std::ofstream transformations_json_file(output_file_prefix + |
| 780 |
".transformations_json"); |
| 781 |
transformations_json_file << json_string; |
| 782 |
transformations_json_file.close(); |
| 783 |
} |
| 784 |
|
| 785 |
return 0; |
| 786 |
} |