Add gn build support for spirv-tools + AST fuzzers

Allows the new transformation-based fuzzers to be built using gn, so
that they can be deployed on ClusterFuzz.

Fixes: tint:1002
Fixes: tint:1006

Change-Id: Ib9624e507e40836541eb424e710705345a198db1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/58387
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Vasyl Teliman <vasniktel@gmail.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
diff --git a/fuzzers/BUILD.gn b/fuzzers/BUILD.gn
index 4788197..30f61ba 100644
--- a/fuzzers/BUILD.gn
+++ b/fuzzers/BUILD.gn
@@ -25,7 +25,10 @@
   action("tint_generate_wgsl_corpus") {
     script = "generate_wgsl_corpus.py"
     sources = [ "generate_wgsl_corpus.py" ]
-    args = [ rebase_path("${tint_root_dir}/test", root_build_dir), rebase_path(fuzzer_corpus_wgsl_dir, root_build_dir) ]
+    args = [
+      rebase_path("${tint_root_dir}/test", root_build_dir),
+      rebase_path(fuzzer_corpus_wgsl_dir, root_build_dir),
+    ]
     outputs = [ fuzzer_corpus_wgsl_dir ]
   }
 
@@ -40,10 +43,36 @@
     deps = [ spirv_as_target ]
     script = "generate_spirv_corpus.py"
     sources = [ "generate_spirv_corpus.py" ]
-    args = [ rebase_path("${tint_root_dir}/test", root_build_dir), rebase_path(fuzzer_corpus_spirv_dir, root_build_dir), rebase_path("${spirv_as_out_dir}/spirv-as", root_build_dir) ]
+    args = [
+      rebase_path("${tint_root_dir}/test", root_build_dir),
+      rebase_path(fuzzer_corpus_spirv_dir, root_build_dir),
+      rebase_path("${spirv_as_out_dir}/spirv-as", root_build_dir),
+    ]
     outputs = [ fuzzer_corpus_spirv_dir ]
   }
 
+  tint_ast_fuzzer_common_libfuzzer_options = [
+    "cross_over=0",
+    "max_len=1000000",
+    "mutate_depth=1",
+    "tint_enable_all_mutations=false",
+    "tint_mutation_batch_size=5",
+  ]
+
+  tint_spirv_tools_fuzzer_common_libfuzzer_options = [
+    "cross_over=0",
+    "max_len=1000000",
+    "mutate_depth=1",
+    "tint_enable_all_fuzzer_passes=true",
+    "tint_enable_all_reduce_passes=true",
+    "tint_mutator_cache_size=30",
+    "tint_mutator_type=fuzz,opt,reduce",
+    "tint_opt_batch_size=5",
+    "tint_reduction_batch_size=5",
+    "tint_repeated_pass_strategy=looped",
+    "tint_transformation_batch_size=5",
+  ]
+
   # fuzzer_test doesn't have configs members, so need to define them in an empty
   # source_set.
 
@@ -75,6 +104,15 @@
   }
 
   if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
+    fuzzer_test("tint_ast_wgsl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_ast_fuzzer:tint_ast_fuzzer" ]
+      libfuzzer_options = tint_ast_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=wgsl" ]
+      seed_corpus = fuzzer_corpus_wgsl_dir
+      seed_corpus_deps = [ ":tint_generate_wgsl_corpus" ]
+    }
+
     fuzzer_test("tint_wgsl_reader_wgsl_writer_fuzzer") {
       sources = [ "tint_wgsl_reader_wgsl_writer_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -93,6 +131,15 @@
       seed_corpus_deps = [ ":tint_generate_wgsl_corpus" ]
     }
 
+    fuzzer_test("tint_ast_spv_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_ast_fuzzer:tint_ast_fuzzer" ]
+      libfuzzer_options = tint_ast_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=spv" ]
+      seed_corpus = fuzzer_corpus_wgsl_dir
+      seed_corpus_deps = [ ":tint_generate_wgsl_corpus" ]
+    }
+
     fuzzer_test("tint_binding_remapper_fuzzer") {
       sources = [ "tint_binding_remapper_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -159,6 +206,15 @@
   }
 
   if (tint_build_wgsl_reader && tint_build_hlsl_writer) {
+    fuzzer_test("tint_ast_hlsl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_ast_fuzzer:tint_ast_fuzzer" ]
+      libfuzzer_options = tint_ast_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=hlsl" ]
+      seed_corpus = fuzzer_corpus_wgsl_dir
+      seed_corpus_deps = [ ":tint_generate_wgsl_corpus" ]
+    }
+
     fuzzer_test("tint_wgsl_reader_hlsl_writer_fuzzer") {
       sources = [ "tint_wgsl_reader_hlsl_writer_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -169,6 +225,15 @@
   }
 
   if (tint_build_wgsl_reader && tint_build_msl_writer) {
+    fuzzer_test("tint_ast_msl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_ast_fuzzer:tint_ast_fuzzer" ]
+      libfuzzer_options = tint_ast_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=msl" ]
+      seed_corpus = fuzzer_corpus_wgsl_dir
+      seed_corpus_deps = [ ":tint_generate_wgsl_corpus" ]
+    }
+
     fuzzer_test("tint_wgsl_reader_msl_writer_fuzzer") {
       sources = [ "tint_wgsl_reader_msl_writer_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -194,6 +259,14 @@
       seed_corpus = fuzzer_corpus_spirv_dir
       seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
     }
+    fuzzer_test("tint_spirv_tools_wgsl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_spirv_tools_fuzzer:tint_spirv_tools_fuzzer" ]
+      libfuzzer_options = tint_spirv_tools_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=wgsl" ]
+      seed_corpus = fuzzer_corpus_spirv_dir
+      seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
+    }
   }
 
   if (tint_build_spv_reader && tint_build_spv_writer) {
@@ -203,6 +276,14 @@
       seed_corpus = fuzzer_corpus_spirv_dir
       seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
     }
+    fuzzer_test("tint_spirv_tools_spv_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_spirv_tools_fuzzer:tint_spirv_tools_fuzzer" ]
+      libfuzzer_options = tint_spirv_tools_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=spv" ]
+      seed_corpus = fuzzer_corpus_spirv_dir
+      seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
+    }
   }
 
   if (tint_build_spv_reader && tint_build_hlsl_writer) {
@@ -212,6 +293,14 @@
       seed_corpus = fuzzer_corpus_spirv_dir
       seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
     }
+    fuzzer_test("tint_spirv_tools_hlsl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_spirv_tools_fuzzer:tint_spirv_tools_fuzzer" ]
+      libfuzzer_options = tint_spirv_tools_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=hlsl" ]
+      seed_corpus = fuzzer_corpus_spirv_dir
+      seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
+    }
   }
 
   if (tint_build_spv_reader && tint_build_msl_writer) {
@@ -221,6 +310,14 @@
       seed_corpus = fuzzer_corpus_spirv_dir
       seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
     }
+    fuzzer_test("tint_spirv_tools_msl_writer_fuzzer") {
+      sources = []
+      deps = [ "tint_spirv_tools_fuzzer:tint_spirv_tools_fuzzer" ]
+      libfuzzer_options = tint_spirv_tools_fuzzer_common_libfuzzer_options +
+                          [ "tint_fuzzing_target=msl" ]
+      seed_corpus = fuzzer_corpus_spirv_dir
+      seed_corpus_deps = [ ":tint_generate_spirv_corpus" ]
+    }
   }
 
   if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
@@ -241,16 +338,20 @@
       deps += [ ":tint_wgsl_reader_fuzzer" ]
     }
     if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
-      deps += [ ":tint_wgsl_reader_wgsl_writer_fuzzer" ]
+      deps += [
+        ":tint_ast_wgsl_writer_fuzzer",
+        ":tint_wgsl_reader_wgsl_writer_fuzzer",
+      ]
     }
     if (tint_build_wgsl_reader && tint_build_spv_writer) {
       deps += [
         ":tint_all_transforms_fuzzer",
+        ":tint_ast_spv_writer_fuzzer",
         ":tint_binding_remapper_fuzzer",
-        ":tint_robustness_fuzzer",
         ":tint_first_index_offset_fuzzer",
         ":tint_inspector_fuzzer",
         ":tint_renamer_fuzzer",
+        ":tint_robustness_fuzzer",
         ":tint_single_entry_point_fuzzer",
         ":tint_vertex_pulling_fuzzer",
         ":tint_wgsl_reader_spv_writer_fuzzer",
@@ -258,11 +359,13 @@
     }
     if (tint_build_wgsl_reader && tint_build_hlsl_writer) {
       deps += [
+        ":tint_ast_hlsl_writer_fuzzer",
         ":tint_wgsl_reader_hlsl_writer_fuzzer",
       ]
     }
     if (tint_build_wgsl_reader && tint_build_msl_writer) {
       deps += [
+        ":tint_ast_msl_writer_fuzzer",
         ":tint_wgsl_reader_msl_writer_fuzzer",
       ]
     }
@@ -270,16 +373,28 @@
       deps += [ ":tint_spv_reader_fuzzer" ]
     }
     if (tint_build_spv_reader && tint_build_wgsl_writer) {
-      deps += [ ":tint_spv_reader_wgsl_writer_fuzzer" ]
+      deps += [
+        ":tint_spirv_tools_wgsl_writer_fuzzer",
+        ":tint_spv_reader_wgsl_writer_fuzzer",
+      ]
     }
     if (tint_build_spv_reader && tint_build_spv_writer) {
-      deps += [ ":tint_spv_reader_spv_writer_fuzzer" ]
+      deps += [
+        ":tint_spirv_tools_spv_writer_fuzzer",
+        ":tint_spv_reader_spv_writer_fuzzer",
+      ]
     }
     if (tint_build_spv_reader && tint_build_hlsl_writer) {
-      deps += [ ":tint_spv_reader_hlsl_writer_fuzzer" ]
+      deps += [
+        ":tint_spirv_tools_hlsl_writer_fuzzer",
+        ":tint_spv_reader_hlsl_writer_fuzzer",
+      ]
     }
     if (tint_build_spv_reader && tint_build_msl_writer) {
-      deps += [ ":tint_spv_reader_msl_writer_fuzzer" ]
+      deps += [
+        ":tint_spirv_tools_msl_writer_fuzzer",
+        ":tint_spv_reader_msl_writer_fuzzer",
+      ]
     }
     if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
       deps += [ ":tint_ast_clone_fuzzer" ]
diff --git a/fuzzers/tint_ast_fuzzer/BUILD.gn b/fuzzers/tint_ast_fuzzer/BUILD.gn
new file mode 100644
index 0000000..e021f3e
--- /dev/null
+++ b/fuzzers/tint_ast_fuzzer/BUILD.gn
@@ -0,0 +1,67 @@
+# Copyright 2021 The Tint Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("//build_overrides/build.gni")
+import("../../tint_overrides_with_defaults.gni")
+
+if (build_with_chromium) {
+  import("//third_party/protobuf/proto_library.gni")
+
+  proto_library("tint_ast_fuzzer_proto") {
+    sources = [ "protobufs/tint_ast_fuzzer.proto" ]
+    generate_python = false
+    use_protobuf_full = true
+  }
+
+  source_set("tint_ast_fuzzer") {
+    public_configs = [
+      "${tint_root_dir}/src:tint_config",
+      "${tint_root_dir}/src:tint_common_config",
+    ]
+
+    include_dirs = [ "${target_gen_dir}/../.." ]
+
+    deps = [
+      ":tint_ast_fuzzer_proto",
+      "${tint_root_dir}/fuzzers:tint_fuzzer_common",
+      "//third_party/protobuf:protobuf_full",
+    ]
+
+    sources = [
+      "cli.cc",
+      "cli.h",
+      "fuzzer.cc",
+      "mt_rng.cc",
+      "mt_rng.h",
+      "mutation.cc",
+      "mutation.h",
+      "mutation_finder.cc",
+      "mutation_finder.h",
+      "mutation_finders/replace_identifiers.cc",
+      "mutation_finders/replace_identifiers.h",
+      "mutations/replace_identifier.cc",
+      "mutations/replace_identifier.h",
+      "mutator.cc",
+      "mutator.h",
+      "node_id_map.cc",
+      "node_id_map.h",
+      "probability_context.cc",
+      "probability_context.h",
+      "protobufs/tint_ast_fuzzer.h",
+      "random_number_generator.cc",
+      "random_number_generator.h",
+      "util.h",
+    ]
+  }
+}
diff --git a/fuzzers/tint_spirv_tools_fuzzer/BUILD.gn b/fuzzers/tint_spirv_tools_fuzzer/BUILD.gn
new file mode 100644
index 0000000..174e72b
--- /dev/null
+++ b/fuzzers/tint_spirv_tools_fuzzer/BUILD.gn
@@ -0,0 +1,53 @@
+# Copyright 2021 The Tint Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("//build_overrides/build.gni")
+import("../../tint_overrides_with_defaults.gni")
+
+if (build_with_chromium) {
+  source_set("tint_spirv_tools_fuzzer") {
+    public_configs = [
+      "${tint_root_dir}/src:tint_config",
+      "${tint_root_dir}/src:tint_common_config",
+    ]
+
+    include_dirs = [ "${root_gen_dir}/third_party/vulkan-deps/spirv-tools/src" ]
+
+    deps = [
+      "${tint_root_dir}/fuzzers:tint_fuzzer_common",
+      "${tint_spirv_tools_dir}/:spvtools_fuzz",
+      "${tint_spirv_tools_dir}/:spvtools_opt",
+      "${tint_spirv_tools_dir}/:spvtools_reduce",
+      "//third_party/protobuf:protobuf_full",
+    ]
+
+    sources = [
+      "cli.cc",
+      "cli.h",
+      "fuzzer.cc",
+      "mutator.cc",
+      "mutator.h",
+      "mutator_cache.cc",
+      "mutator_cache.h",
+      "spirv_fuzz_mutator.cc",
+      "spirv_fuzz_mutator.h",
+      "spirv_opt_mutator.cc",
+      "spirv_opt_mutator.h",
+      "spirv_reduce_mutator.cc",
+      "spirv_reduce_mutator.h",
+      "util.cc",
+      "util.h",
+    ]
+  }
+}