Add fuzzers for various emission paths

BUG=tint:58,tint:59,tint:194,tint:195

Change-Id: Ibca5efbcd608489dcb7c6c45a5fcf596b97165dc
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/36901
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 2943ab0..57c77f3 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1324,6 +1324,50 @@
     }
   }
 
+  if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
+    fuzzer_test("tint_wgsl_reader_wgsl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_wgsl_reader_wgsl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_wgsl_reader_src",
+        ":libtint_wgsl_writer_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_wgsl_reader && tint_build_spv_writer) {
+    fuzzer_test("tint_wgsl_reader_spv_writer_fuzzer") {
+      sources = [ "fuzzers/tint_wgsl_reader_spv_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_spv_writer_src",
+        ":libtint_wgsl_reader_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_wgsl_reader && tint_build_hlsl_writer) {
+    fuzzer_test("tint_wgsl_reader_hlsl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_wgsl_reader_hlsl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_hlsl_writer_src",
+        ":libtint_wgsl_reader_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_wgsl_reader && tint_build_msl_writer) {
+    fuzzer_test("tint_wgsl_reader_msl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_wgsl_reader_msl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_msl_writer_src",
+        ":libtint_wgsl_reader_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
   if (tint_build_spv_reader) {
     fuzzer_test("tint_spv_reader_fuzzer") {
       sources = [ "fuzzers/tint_spv_reader_fuzzer.cc" ]
@@ -1334,6 +1378,50 @@
     }
   }
 
+  if (tint_build_spv_reader && tint_build_wgsl_writer) {
+    fuzzer_test("tint_spv_reader_wgsl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_spv_reader_wgsl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_spv_reader_src",
+        ":libtint_wgsl_writer_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_spv_reader && tint_build_spv_writer) {
+    fuzzer_test("tint_spv_reader_spv_writer_fuzzer") {
+      sources = [ "fuzzers/tint_spv_reader_spv_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_spv_reader_src",
+        ":libtint_spv_writer_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_spv_reader && tint_build_hlsl_writer) {
+    fuzzer_test("tint_spv_reader_hlsl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_spv_reader_hlsl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_hlsl_writer_src",
+        ":libtint_spv_reader_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
+  if (tint_build_spv_reader && tint_build_msl_writer) {
+    fuzzer_test("tint_spv_reader_msl_writer_fuzzer") {
+      sources = [ "fuzzers/tint_spv_reader_msl_writer_fuzzer.cc" ]
+      deps = [
+        ":libtint_msl_writer_src",
+        ":libtint_spv_reader_src",
+        ":tint_fuzzer_common",
+      ]
+    }
+  }
+
   if (tint_build_wgsl_reader && tint_build_wgsl_writer) {
     fuzzer_test("tint_ast_clone_fuzzer") {
       sources = [ "fuzzers/tint_ast_clone_fuzzer.cc" ]
diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt
index dc585f1..27016d0 100644
--- a/fuzzers/CMakeLists.txt
+++ b/fuzzers/CMakeLists.txt
@@ -21,14 +21,47 @@
   target_compile_options(${NAME} PRIVATE -Wno-missing-prototypes)
 endfunction()
 
-if (${TINT_BUILD_WGSL_READER})
+if ({$TINT_BUILD_WGSL_READER})
   add_tint_fuzzer(tint_wgsl_reader_fuzzer)
 endif()
 
+if ({$TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_WGSL_WRITER})
+  add_tint_fuzzer(tint_wgsl_reader_wgsl_writer_fuzzer)
+endif()
+
+if ({$TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_SPV_WRITER})
+  add_tint_fuzzer(tint_wgsl_reader_spv_writer_fuzzer)
+endif()
+
+if ({$TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_HLSL_WRITER})
+  add_tint_fuzzer(tint_wgsl_reader_hlsl_writer_fuzzer)
+endif()
+
+if ({$TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_MSL_WRITER})
+  add_tint_fuzzer(tint_wgsl_reader_msl_writer_fuzzer)
+endif()
+
 if (${TINT_BUILD_SPV_READER})
   add_tint_fuzzer(tint_spv_reader_fuzzer)
 endif()
 
+if (${TINT_BUILD_SPV_READER} AND ${TINT_BUILD_WGSL_WRITER})
+  add_tint_fuzzer(tint_spv_reader_wgsl_writer_fuzzer)
+endif()
+
+if (${TINT_BUILD_SPV_READER} AND ${TINT_BUILD_SPV_WRITER})
+  add_tint_fuzzer(tint_spv_reader_spv_writer_fuzzer)
+endif()
+
+if (${TINT_BUILD_SPV_READER} AND ${TINT_BUILD_HLSL_WRITER})
+  add_tint_fuzzer(tint_spv_reader_hlsl_writer_fuzzer)
+endif()
+
+if (${TINT_BUILD_SPV_READER} AND ${TINT_BUILD_MSL_WRITER})
+  add_tint_fuzzer(tint_spv_reader_msl_writer_fuzzer)
+endif()
+
+
 if (${TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_WGSL_WRITER})
   add_tint_fuzzer(tint_ast_clone_fuzzer)
 endif()
diff --git a/fuzzers/tint_common_fuzzer.cc b/fuzzers/tint_common_fuzzer.cc
index f211b49..3cabcb1 100644
--- a/fuzzers/tint_common_fuzzer.cc
+++ b/fuzzers/tint_common_fuzzer.cc
@@ -16,12 +16,14 @@
 
 #include <memory>
 #include <string>
+#include <utility>
 #include <vector>
 
 namespace tint {
 namespace fuzzers {
 
-CommonFuzzer::CommonFuzzer(InputFormat input) : input_(input) {}
+CommonFuzzer::CommonFuzzer(InputFormat input, OutputFormat output)
+    : input_(input), output_(output) {}
 CommonFuzzer::~CommonFuzzer() = default;
 
 int CommonFuzzer::Run(const uint8_t* data, size_t size) {
@@ -58,8 +60,62 @@
       break;
   }
 
-  if (parser) {
-    parser->Parse();
+  if (!parser) {
+    return 0;
+  }
+
+  if (!parser->Parse()) {
+    return 0;
+  }
+
+  if (output_ == OutputFormat::kNone) {
+    return 0;
+  }
+
+  auto mod = parser->module();
+  if (!mod.IsValid()) {
+    return 0;
+  }
+
+  tint::TypeDeterminer td(&mod);
+  if (!td.Determine()) {
+    return 0;
+  }
+
+  tint::Validator v;
+  if (!v.Validate(&mod)) {
+    return 0;
+  }
+
+  std::unique_ptr<tint::writer::Writer> writer;
+
+  switch (output_) {
+    case OutputFormat::kWGSL:
+#if TINT_BUILD_WGSL_WRITER
+      writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(mod));
+#endif  // TINT_BUILD_WGSL_WRITER
+      break;
+    case OutputFormat::kSpv:
+#if TINT_BUILD_SPV_WRITER
+      writer = std::make_unique<tint::writer::spirv::Generator>(std::move(mod));
+#endif  // TINT_BUILD_SPV_WRITER
+      break;
+    case OutputFormat::kHLSL:
+#if TINT_BUILD_HLSL_WRITER
+      writer = std::make_unique<tint::writer::hlsl::Generator>(std::move(mod));
+#endif  // TINT_BUILD_HLSL_WRITER
+      break;
+    case OutputFormat::kMSL:
+#if TINT_BUILD_MSL_WRITER
+      writer = std::make_unique<tint::writer::msl::Generator>(std::move(mod));
+#endif  // TINT_BUILD_MSL_WRITER
+      break;
+    case OutputFormat::kNone:
+      break;
+  }
+
+  if (writer) {
+    writer->Generate();
   }
 
   return 0;
diff --git a/fuzzers/tint_common_fuzzer.h b/fuzzers/tint_common_fuzzer.h
index 6e1f2b4..79c9bb8 100644
--- a/fuzzers/tint_common_fuzzer.h
+++ b/fuzzers/tint_common_fuzzer.h
@@ -22,15 +22,18 @@
 
 enum class InputFormat { kWGSL, kSpv, kNone };
 
+enum class OutputFormat { kWGSL, kSpv, kHLSL, kMSL, kNone };
+
 class CommonFuzzer {
  public:
-  explicit CommonFuzzer(InputFormat input);
+  explicit CommonFuzzer(InputFormat input, OutputFormat output);
   ~CommonFuzzer();
 
   int Run(const uint8_t* data, size_t size);
 
  private:
   InputFormat input_;
+  OutputFormat output_;
 };
 
 }  // namespace fuzzers
diff --git a/fuzzers/tint_spv_reader_fuzzer.cc b/fuzzers/tint_spv_reader_fuzzer.cc
index aeb6ae7..b90c09a 100644
--- a/fuzzers/tint_spv_reader_fuzzer.cc
+++ b/fuzzers/tint_spv_reader_fuzzer.cc
@@ -16,7 +16,13 @@
 
 #include "fuzzers/tint_common_fuzzer.h"
 
+namespace tint {
+namespace fuzzers {
+
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  tint::fuzzers::CommonFuzzer fuzzer(tint::fuzzers::InputFormat::kSpv);
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kSpv, OutputFormat::kNone);
   return fuzzer.Run(data, size);
 }
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_spv_reader_hlsl_writer_fuzzer.cc b/fuzzers/tint_spv_reader_hlsl_writer_fuzzer.cc
new file mode 100644
index 0000000..990002b
--- /dev/null
+++ b/fuzzers/tint_spv_reader_hlsl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <vector>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kSpv, OutputFormat::kHLSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_spv_reader_msl_writer_fuzzer.cc b/fuzzers/tint_spv_reader_msl_writer_fuzzer.cc
new file mode 100644
index 0000000..cfff9ba
--- /dev/null
+++ b/fuzzers/tint_spv_reader_msl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <vector>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kSpv, OutputFormat::kMSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_spv_reader_spv_writer_fuzzer.cc b/fuzzers/tint_spv_reader_spv_writer_fuzzer.cc
new file mode 100644
index 0000000..ac2d0dd
--- /dev/null
+++ b/fuzzers/tint_spv_reader_spv_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <vector>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kSpv, OutputFormat::kSpv);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_spv_reader_wgsl_writer_fuzzer.cc b/fuzzers/tint_spv_reader_wgsl_writer_fuzzer.cc
new file mode 100644
index 0000000..30a4f1a
--- /dev/null
+++ b/fuzzers/tint_spv_reader_wgsl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <vector>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kSpv, OutputFormat::kWGSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_wgsl_reader_fuzzer.cc b/fuzzers/tint_wgsl_reader_fuzzer.cc
index 99edc29..280700e 100644
--- a/fuzzers/tint_wgsl_reader_fuzzer.cc
+++ b/fuzzers/tint_wgsl_reader_fuzzer.cc
@@ -16,7 +16,13 @@
 
 #include "fuzzers/tint_common_fuzzer.h"
 
+namespace tint {
+namespace fuzzers {
+
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  tint::fuzzers::CommonFuzzer fuzzer(tint::fuzzers::InputFormat::kWGSL);
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kNone);
   return fuzzer.Run(data, size);
 }
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_wgsl_reader_hlsl_writer_fuzzer.cc b/fuzzers/tint_wgsl_reader_hlsl_writer_fuzzer.cc
new file mode 100644
index 0000000..f10dcf5
--- /dev/null
+++ b/fuzzers/tint_wgsl_reader_hlsl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <string>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kHLSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_wgsl_reader_msl_writer_fuzzer.cc b/fuzzers/tint_wgsl_reader_msl_writer_fuzzer.cc
new file mode 100644
index 0000000..028cab3
--- /dev/null
+++ b/fuzzers/tint_wgsl_reader_msl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <string>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kMSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_wgsl_reader_spv_writer_fuzzer.cc b/fuzzers/tint_wgsl_reader_spv_writer_fuzzer.cc
new file mode 100644
index 0000000..885f9a9
--- /dev/null
+++ b/fuzzers/tint_wgsl_reader_spv_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <string>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kSpv);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint
diff --git a/fuzzers/tint_wgsl_reader_wgsl_writer_fuzzer.cc b/fuzzers/tint_wgsl_reader_wgsl_writer_fuzzer.cc
new file mode 100644
index 0000000..8568425
--- /dev/null
+++ b/fuzzers/tint_wgsl_reader_wgsl_writer_fuzzer.cc
@@ -0,0 +1,28 @@
+// 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.
+
+#include <string>
+
+#include "fuzzers/tint_common_fuzzer.h"
+
+namespace tint {
+namespace fuzzers {
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
+  tint::fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kWGSL);
+  return fuzzer.Run(data, size);
+}
+
+}  // namespace fuzzers
+}  // namespace tint