• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
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 "fuzzers/tint_regex_fuzzer/cli.h"
16 
17 #include <cstring>
18 #include <iostream>
19 #include <limits>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 
24 namespace tint {
25 namespace fuzzers {
26 namespace regex_fuzzer {
27 namespace {
28 
29 const char* const kHelpMessage = R"(
30 This is a fuzzer for the Tint compiler that works by mutating a WGSL shader.
31 
32 Below is a list of all supported parameters for this fuzzer. You may want to
33 run it with -help=1 to check out libfuzzer parameters.
34 
35   -tint_fuzzing_target=
36                        Specifies the shading language to target during fuzzing.
37                        This must be one or a combination of `wgsl`, `spv`, `hlsl`,
38                        `msl` (without `) separated by commas. By default it's
39                        `wgsl,msl,hlsl,spv`.
40 
41   -tint_help
42                        Show this message. Note that there is also a -help=1
43                        parameter that will display libfuzzer's help message.
44 )";
45 
HasPrefix(const char * str,const char * prefix)46 bool HasPrefix(const char* str, const char* prefix) {
47   return strncmp(str, prefix, strlen(prefix)) == 0;
48 }
49 
InvalidParam(const char * param)50 [[noreturn]] void InvalidParam(const char* param) {
51   std::cout << "Invalid value for " << param << std::endl;
52   std::cout << kHelpMessage << std::endl;
53   exit(1);
54 }
55 
ParseFuzzingTarget(const char * value,FuzzingTarget * out)56 bool ParseFuzzingTarget(const char* value, FuzzingTarget* out) {
57   if (!strcmp(value, "wgsl")) {
58     *out = FuzzingTarget::kWgsl;
59   } else if (!strcmp(value, "spv")) {
60     *out = FuzzingTarget::kSpv;
61   } else if (!strcmp(value, "msl")) {
62     *out = FuzzingTarget::kMsl;
63   } else if (!strcmp(value, "hlsl")) {
64     *out = FuzzingTarget::kHlsl;
65   } else {
66     return false;
67   }
68   return true;
69 }
70 
71 }  // namespace
72 
ParseCliParams(int * argc,char ** argv)73 CliParams ParseCliParams(int* argc, char** argv) {
74   CliParams cli_params;
75   auto help = false;
76 
77   for (int i = *argc - 1; i > 0; --i) {
78     auto param = argv[i];
79     auto recognized_parameter = true;
80 
81     if (HasPrefix(param, "-tint_fuzzing_target=")) {
82       auto result = FuzzingTarget::kNone;
83 
84       std::stringstream ss(param + sizeof("-tint_fuzzing_target=") - 1);
85       for (std::string value; std::getline(ss, value, ',');) {
86         auto tmp = FuzzingTarget::kNone;
87         if (!ParseFuzzingTarget(value.c_str(), &tmp)) {
88           InvalidParam(param);
89         }
90         result = result | tmp;
91       }
92 
93       if (result == FuzzingTarget::kNone) {
94         InvalidParam(param);
95       }
96 
97       cli_params.fuzzing_target = result;
98     } else if (!strcmp(param, "-tint_help")) {
99       help = true;
100     } else {
101       recognized_parameter = false;
102     }
103 
104     if (recognized_parameter) {
105       // Remove the recognized parameter from the list of all parameters by
106       // swapping it with the last one. This will suppress warnings in the
107       // libFuzzer about unrecognized parameters. By default, libFuzzer thinks
108       // that all user-defined parameters start with two dashes. However, we are
109       // forced to use a single one to make the fuzzer compatible with the
110       // ClusterFuzz.
111       std::swap(argv[i], argv[*argc - 1]);
112       *argc -= 1;
113     }
114   }
115 
116   if (help) {
117     std::cout << kHelpMessage << std::endl;
118     exit(0);
119   }
120 
121   return cli_params;
122 }
123 
124 }  // namespace regex_fuzzer
125 }  // namespace fuzzers
126 }  // namespace tint
127