Convert spirv-cross fuzzers to use shaderc.

BUG=dawn:95

Change-Id: I0fedb7b068de39c41b1b1d8aa2b42a96fdb584fb
Reviewed-on: https://dawn-review.googlesource.com/c/4140
Commit-Queue: Frank Henigman <fjhenigman@chromium.org>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/DEPS b/DEPS
index 0d9f6fe..15f9725 100644
--- a/DEPS
+++ b/DEPS
@@ -68,7 +68,7 @@
     'condition': 'dawn_standalone',
   },
   'third_party/shaderc': {
-    'url': '{chromium_git}/external/github.com/google/shaderc@ce7d92182b8cc9c72e99efb5fab1eae3c2084887',
+    'url': '{chromium_git}/external/github.com/google/shaderc@634dd3545cbccb9362f16f41b3b75703f290a9fd',
     'condition': 'dawn_standalone',
   },
 
diff --git a/src/fuzzers/BUILD.gn b/src/fuzzers/BUILD.gn
index 9df69eb..0c48266 100644
--- a/src/fuzzers/BUILD.gn
+++ b/src/fuzzers/BUILD.gn
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import("../../scripts/dawn_overrides_with_defaults.gni")
 import("//build_overrides/build.gni")
 
 dawn_top_level = "../.."
@@ -76,7 +77,7 @@
     "DawnSPIRVCrossFuzzer.h",
   ]
   public_deps = [
-    "${dawn_top_level}/third_party:spirv_cross_full_for_fuzzers",
+    "${dawn_shaderc_dir}:libshaderc_spvc",
   ]
 }
 
@@ -111,7 +112,7 @@
 # Uses Dawn specific options and varies input data
 dawn_fuzzer_test("dawn_spirv_cross_hlsl_fast_fuzzer") {
   sources = [
-    "DawnSPIRVCrossGLSLFastFuzzer.cpp",
+    "DawnSPIRVCrossHLSLFastFuzzer.cpp",
   ]
   deps = [
     ":dawn_spirv_cross_fuzzer_common",
@@ -124,7 +125,7 @@
 # Varies both the options and input data
 dawn_fuzzer_test("dawn_spirv_cross_hlsl_full_fuzzer") {
   sources = [
-    "DawnSPIRVCrossGLSLFullFuzzer.cpp",
+    "DawnSPIRVCrossHLSLFullFuzzer.cpp",
   ]
   deps = [
     ":dawn_spirv_cross_fuzzer_common",
@@ -137,7 +138,7 @@
 # Uses Dawn specific options and varies input data
 dawn_fuzzer_test("dawn_spirv_cross_msl_fast_fuzzer") {
   sources = [
-    "DawnSPIRVCrossGLSLFastFuzzer.cpp",
+    "DawnSPIRVCrossMSLFastFuzzer.cpp",
   ]
   deps = [
     ":dawn_spirv_cross_fuzzer_common",
@@ -150,7 +151,7 @@
 # Varies both the options and input data
 dawn_fuzzer_test("dawn_spirv_cross_msl_full_fuzzer") {
   sources = [
-    "DawnSPIRVCrossGLSLFullFuzzer.cpp",
+    "DawnSPIRVCrossMSLFullFuzzer.cpp",
   ]
   deps = [
     ":dawn_spirv_cross_fuzzer_common",
@@ -165,12 +166,10 @@
 
   deps = [
     "${dawn_top_level}:dawn_common",
-    "${dawn_top_level}:libdawn_static",
     "${dawn_top_level}:libdawn_native_static",
+    "${dawn_top_level}:libdawn_static",
     "${dawn_top_level}:libdawn_wire_static",
   ]
 
-  additional_configs = [
-    "${dawn_top_level}:dawn_internal",
-  ]
+  additional_configs = [ "${dawn_top_level}:dawn_internal" ]
 }
diff --git a/src/fuzzers/DawnSPIRVCrossFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossFuzzer.cpp
index c37b215..f080adb 100644
--- a/src/fuzzers/DawnSPIRVCrossFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossFuzzer.cpp
@@ -65,43 +65,26 @@
     }
 
     int Run(const uint8_t* data, size_t size, Task task) {
-        if (!data || size < 1)
-            return 0;
-
         size_t sizeInU32 = size / sizeof(uint32_t);
         const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data);
         std::vector<uint32_t> input(u32Data, u32Data + sizeInU32);
 
-        task(input);
+        if (input.size() != 0) {
+            task(input);
+        }
+
         return 0;
     }
 
-    template <class Options>
-    int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions<Options> task) {
-        if (!data || size < sizeof(Options) + 1)
+    int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions task) {
+        shaderc_spvc::CompileOptions options;
+        size_t used = options.SetForFuzzing(data, size);
+        if (used == 0) {
+            // not enough data to set options
             return 0;
+        }
 
-        Options options = *reinterpret_cast<const Options*>(data);
-        data += sizeof(options);
-        size -= sizeof(options);
-
-        std::vector<uint32_t> input(data, data + (4 * (size / 4)));
-
-        task(input, options);
-
-        return 0;
+        return Run(data + used, size - used, std::bind(task, std::placeholders::_1, options));
     }
 
-    template int RunWithOptions<spirv_cross::CompilerGLSL::Options>(
-        const uint8_t*,
-        size_t,
-        TaskWithOptions<spirv_cross::CompilerGLSL::Options>);
-    template int RunWithOptions<spirv_cross::CompilerHLSL::Options>(
-        const uint8_t*,
-        size_t,
-        TaskWithOptions<spirv_cross::CompilerHLSL::Options>);
-    template int RunWithOptions<CombinedOptions>(const uint8_t*,
-                                                 size_t,
-                                                 TaskWithOptions<CombinedOptions>);
-
 }  // namespace DawnSPIRVCrossFuzzer
diff --git a/src/fuzzers/DawnSPIRVCrossFuzzer.h b/src/fuzzers/DawnSPIRVCrossFuzzer.h
index 47e3040..2f017e3 100644
--- a/src/fuzzers/DawnSPIRVCrossFuzzer.h
+++ b/src/fuzzers/DawnSPIRVCrossFuzzer.h
@@ -16,19 +16,13 @@
 #include <functional>
 #include <vector>
 
-#include "spirv-cross/spirv_glsl.hpp"
-#include "spirv-cross/spirv_hlsl.hpp"
+#include "shaderc/spvc.hpp"
 
 namespace DawnSPIRVCrossFuzzer {
 
-    struct CombinedOptions {
-        spirv_cross::CompilerGLSL::Options glsl;
-        spirv_cross::CompilerHLSL::Options hlsl;
-    };
-
     using Task = std::function<int(const std::vector<uint32_t>&)>;
-    template <class Options>
-    using TaskWithOptions = std::function<int(const std::vector<uint32_t>&, Options)>;
+    using TaskWithOptions =
+        std::function<int(const std::vector<uint32_t>&, shaderc_spvc::CompileOptions)>;
 
     // Used to wrap code that may fire a SIGABRT. Do not allocate anything local within |exec|, as
     // it is not guaranteed to return.
@@ -38,7 +32,6 @@
     int Run(const uint8_t* data, size_t size, Task task);
 
     // Used to fuzz by mutating both the input data and options to the compiler
-    template <class Options>
-    int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions<Options> task);
+    int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions task);
 
 }  // namespace DawnSPIRVCrossFuzzer
diff --git a/src/fuzzers/DawnSPIRVCrossGLSLFastFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossGLSLFastFuzzer.cpp
index 53b06cf..2a36cf1 100644
--- a/src/fuzzers/DawnSPIRVCrossGLSLFastFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossGLSLFastFuzzer.cpp
@@ -19,22 +19,18 @@
 #include "DawnSPIRVCrossFuzzer.h"
 
 namespace {
-
     int GLSLFastFuzzTask(const std::vector<uint32_t>& input) {
-        std::unique_ptr<spirv_cross::CompilerGLSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
-            compiler = std::make_unique<spirv_cross::CompilerGLSL>(input);
-        });
-        if (compiler == nullptr) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        // Using the options that are used by Dawn, they appear in ShaderModuleGL.cpp
-        spirv_cross::CompilerGLSL::Options options;
-        options.version = 440;
-        compiler->set_common_options(options);
-
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
+            // Using the options that are used by Dawn, they appear in ShaderModuleGL.cpp
+            shaderc_spvc::CompileOptions options;
+            options.SetOutputLanguageVersion(440);
+            compiler.CompileSpvToGlsl(input.data(), input.size(), options);
+        });
 
         return 0;
     }
diff --git a/src/fuzzers/DawnSPIRVCrossGLSLFullFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossGLSLFullFuzzer.cpp
index 2ff9fef..b592517 100644
--- a/src/fuzzers/DawnSPIRVCrossGLSLFullFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossGLSLFullFuzzer.cpp
@@ -20,19 +20,15 @@
 
 namespace {
 
-    int GLSLFullFuzzTask(const std::vector<uint32_t>& input,
-                         spirv_cross::CompilerGLSL::Options options) {
-        std::unique_ptr<spirv_cross::CompilerGLSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
-            compiler = std::make_unique<spirv_cross::CompilerGLSL>(input);
-        });
-        if (compiler == nullptr) {
+    int GLSLFullFuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        compiler->set_common_options(options);
-
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
+            compiler.CompileSpvToGlsl(input.data(), input.size(), options);
+        });
 
         return 0;
     }
@@ -40,6 +36,5 @@
 }  // namespace
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-    return DawnSPIRVCrossFuzzer::RunWithOptions<spirv_cross::CompilerGLSL::Options>(
-        data, size, GLSLFullFuzzTask);
+    return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, GLSLFullFuzzTask);
 }
diff --git a/src/fuzzers/DawnSPIRVCrossHLSLFastFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossHLSLFastFuzzer.cpp
index 9d9cd9d..6c75b39 100644
--- a/src/fuzzers/DawnSPIRVCrossHLSLFastFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossHLSLFastFuzzer.cpp
@@ -18,30 +18,23 @@
 
 #include "DawnSPIRVCrossFuzzer.h"
 
-#include "spirv-cross/spirv_hlsl.hpp"
-
 namespace {
 
     int FuzzTask(const std::vector<uint32_t>& input) {
-        std::unique_ptr<spirv_cross::CompilerHLSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
-            compiler = std::make_unique<spirv_cross::CompilerHLSL>(input);
-        });
-        if (compiler == nullptr) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        // Using the options that are used by Dawn, they appear in ShaderModuleD3D12.cpp
-        spirv_cross::CompilerGLSL::Options options_glsl;
-        options_glsl.vertex.fixup_clipspace = true;
-        options_glsl.vertex.flip_vert_y = true;
-        compiler->set_common_options(options_glsl);
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
+            shaderc_spvc::CompileOptions options;
 
-        spirv_cross::CompilerHLSL::Options options_hlsl;
-        options_hlsl.shader_model = 51;
-        compiler->set_hlsl_options(options_hlsl);
-
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
+            // Using the options that are used by Dawn, they appear in ShaderModuleD3D12.cpp
+            options.SetFixupClipspace(true);
+            options.SetFlipVertY(true);
+            options.SetShaderModel(51);
+            compiler.CompileSpvToHlsl(input.data(), input.size(), options);
+        });
 
         return 0;
     }
diff --git a/src/fuzzers/DawnSPIRVCrossHLSLFullFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossHLSLFullFuzzer.cpp
index 2433752..c060173 100644
--- a/src/fuzzers/DawnSPIRVCrossHLSLFullFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossHLSLFullFuzzer.cpp
@@ -18,29 +18,22 @@
 
 #include "DawnSPIRVCrossFuzzer.h"
 
-#include "spirv-cross/spirv_hlsl.hpp"
-
 namespace {
-    int FuzzTask(const std::vector<uint32_t>& input,
-                 DawnSPIRVCrossFuzzer::CombinedOptions options) {
-        std::unique_ptr<spirv_cross::CompilerHLSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
-            compiler = std::make_unique<spirv_cross::CompilerHLSL>(input);
-        });
-        if (compiler == nullptr) {
+    int FuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        compiler->set_common_options(options.glsl);
-        compiler->set_hlsl_options(options.hlsl);
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
+            compiler.CompileSpvToHlsl(input.data(), input.size(), options);
+        });
 
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
         return 0;
     }
 
 }  // namespace
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-    return DawnSPIRVCrossFuzzer::RunWithOptions<DawnSPIRVCrossFuzzer::CombinedOptions>(data, size,
-                                                                                       FuzzTask);
+    return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, FuzzTask);
 }
diff --git a/src/fuzzers/DawnSPIRVCrossMSLFastFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossMSLFastFuzzer.cpp
index 4b0da62..fc30193 100644
--- a/src/fuzzers/DawnSPIRVCrossMSLFastFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossMSLFastFuzzer.cpp
@@ -18,24 +18,21 @@
 
 #include "DawnSPIRVCrossFuzzer.h"
 
-#include "spirv-cross/spirv_msl.hpp"
-
 namespace {
 
     int FuzzTask(const std::vector<uint32_t>& input) {
-        std::unique_ptr<spirv_cross::CompilerMSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap(
-            [&compiler, input]() { compiler = std::make_unique<spirv_cross::CompilerMSL>(input); });
-        if (compiler == nullptr) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        // Using the options that are used by Dawn, they appear in ShaderModuleMTL.mm
-        spirv_cross::CompilerGLSL::Options options;
-        options.vertex.flip_vert_y = true;
-        compiler->spirv_cross::CompilerGLSL::set_common_options(options);
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
+            shaderc_spvc::CompileOptions options;
 
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
+            // Using the options that are used by Dawn, they appear in ShaderModuleMTL.mm
+            options.SetFlipVertY(true);
+            compiler.CompileSpvToMsl(input.data(), input.size(), options);
+        });
 
         return 0;
     }
diff --git a/src/fuzzers/DawnSPIRVCrossMSLFullFuzzer.cpp b/src/fuzzers/DawnSPIRVCrossMSLFullFuzzer.cpp
index 7b66edd9..0549f25 100644
--- a/src/fuzzers/DawnSPIRVCrossMSLFullFuzzer.cpp
+++ b/src/fuzzers/DawnSPIRVCrossMSLFullFuzzer.cpp
@@ -18,21 +18,17 @@
 
 #include "DawnSPIRVCrossFuzzer.h"
 
-#include "spirv-cross/spirv_msl.hpp"
-
 namespace {
 
-    int FuzzTask(const std::vector<uint32_t>& input, spirv_cross::CompilerGLSL::Options options) {
-        std::unique_ptr<spirv_cross::CompilerMSL> compiler;
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap(
-            [&compiler, input]() { compiler = std::make_unique<spirv_cross::CompilerMSL>(input); });
-        if (compiler == nullptr) {
+    int FuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
+        shaderc_spvc::Compiler compiler;
+        if (!compiler.IsValid()) {
             return 0;
         }
 
-        compiler->spirv_cross::CompilerGLSL::set_common_options(options);
-
-        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
+        DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
+            compiler.CompileSpvToMsl(input.data(), input.size(), options);
+        });
 
         return 0;
     }
@@ -40,6 +36,5 @@
 }  // namespace
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-    return DawnSPIRVCrossFuzzer::RunWithOptions<spirv_cross::CompilerGLSL::Options>(data, size,
-                                                                                    FuzzTask);
+    return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, FuzzTask);
 }