[DawnLPM] Templatize dawnlpm build target

* Templatize dawnlpm build target.
* Add second build target that always fuzzes with a static wgsl script.
* Increase fuzzers max_len

Change-Id: I319d2e3f1f5c3e23c06e882c4a97fe83c52094b7
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/154220
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Brendon Tiszka <tiszka@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/fuzzers/BUILD.gn b/src/dawn/fuzzers/BUILD.gn
index da6ef8d..a6296b3 100644
--- a/src/dawn/fuzzers/BUILD.gn
+++ b/src/dawn/fuzzers/BUILD.gn
@@ -65,6 +65,7 @@
   import("//third_party/protobuf/proto_library.gni")
   import("${dawn_root}/generator/dawn_generator.gni")
 
+  # Generate the fuzzer's serializer in `.cpp`
   dawn_json_lpm_generator("dawn_lpmfuzz_cpp") {
     target = "dawn_lpmfuzz_cpp"
     outputs = [
@@ -83,7 +84,7 @@
     ]
   }
 
-  # Copy handwritten `.proto` files to build directory
+  # Copy handwritten `.proto` file to build directory
   copy("copy_dawn_custom_lpm") {
     sources = [ "${dawn_root}/src/dawn/fuzzers/lpmfuzz/dawn_custom_lpm.proto" ]
     outputs = [ "$root_out_dir/gen/third_party/dawn/src/dawn/fuzzers/lpmfuzz/dawn_custom_lpm.proto" ]
@@ -105,11 +106,11 @@
     ]
   }
 
-  fuzzer_test("dawn_lpm_fuzzer_vulkan_backend") {
-    sources = get_target_outputs(":dawn_lpmfuzz_cpp")
-    sources += get_target_outputs(":dawn_lpm_proto_gen")
-
-    sources += [
+  # Compile dawnlpm fuzzer with extensive tint coverage
+  dawn_lpm_template("dawn_lpm_fuzzer_vulkan_backend_with_tint") {
+    defines = [ "DAWNLPM_FUZZ_TINT=1" ]
+    dawn_lpm_sources = get_target_outputs(":dawn_lpmfuzz_cpp")
+    dawn_lpm_sources += [
       "lpmfuzz/DawnLPMFuzzer.cpp",
       "lpmfuzz/DawnLPMFuzzer.h",
       "lpmfuzz/DawnLPMFuzzerAndVulkanBackend.cpp",
@@ -118,17 +119,20 @@
       "lpmfuzz/DawnLPMSerializerCustom.cpp",
       "lpmfuzz/DawnLPMSerializerCustom.h",
     ]
+  }
 
-    deps = [
-      ":dawn_lpm_proto",
-      ":dawn_lpmfuzz_cpp",
-      "${dawn_root}/src/dawn:cpp",
-      "${dawn_root}/src/dawn:proc",
-      "${dawn_root}/src/dawn/common",
-      "${dawn_root}/src/dawn/native:static",
-      "${dawn_root}/src/dawn/utils",
-      "${dawn_root}/src/dawn/wire:static",
-      "//third_party/libprotobuf-mutator",
+  # Compile dawnlpm api fuzzer
+  dawn_lpm_template("dawn_lpm_fuzzer_vulkan_backend") {
+    defines = [ "DAWNLPM_FUZZ_TINT=0" ]
+    dawn_lpm_sources = get_target_outputs(":dawn_lpmfuzz_cpp")
+    dawn_lpm_sources += [
+      "lpmfuzz/DawnLPMFuzzer.cpp",
+      "lpmfuzz/DawnLPMFuzzer.h",
+      "lpmfuzz/DawnLPMFuzzerAndVulkanBackend.cpp",
+      "lpmfuzz/DawnLPMObjectStore.cpp",
+      "lpmfuzz/DawnLPMObjectStore.h",
+      "lpmfuzz/DawnLPMSerializerCustom.cpp",
+      "lpmfuzz/DawnLPMSerializerCustom.h",
     ]
   }
 }
diff --git a/src/dawn/fuzzers/dawn_fuzzers.gni b/src/dawn/fuzzers/dawn_fuzzers.gni
index e27b1bb..69b57c2 100644
--- a/src/dawn/fuzzers/dawn_fuzzers.gni
+++ b/src/dawn/fuzzers/dawn_fuzzers.gni
@@ -14,6 +14,39 @@
 
 # Build flag for dawn lpm fuzzers
 
+import("../../../scripts/dawn_overrides_with_defaults.gni")
+
+import("//build_overrides/build.gni")
+import("//testing/libfuzzer/fuzzer_test.gni")
+
 declare_args() {
   is_dawn_lpm_fuzzer = is_linux
 }
+
+template("dawn_lpm_template") {
+  # Extract some common parameters (if needed)
+  assert(is_dawn_lpm_fuzzer)
+  assert(defined(invoker.dawn_lpm_sources))
+  assert(defined(invoker.defines))
+
+  fuzzer_test("${target_name}") {
+    sources = invoker.dawn_lpm_sources
+    defines = invoker.defines
+    deps = [
+      ":dawn_lpm_proto",
+      ":dawn_lpmfuzz_cpp",
+      "${dawn_root}/src/dawn:cpp",
+      "${dawn_root}/src/dawn:proc",
+      "${dawn_root}/src/dawn/common",
+      "${dawn_root}/src/dawn/native:static",
+      "${dawn_root}/src/dawn/utils",
+      "${dawn_root}/src/dawn/wire:static",
+      "//third_party/libprotobuf-mutator",
+    ]
+
+    libfuzzer_options = [
+      "max_len=2111000",
+      "len_control=0",
+    ]
+  }
+}
diff --git a/src/dawn/fuzzers/lpmfuzz/DawnLPMSerializerCustom.cpp b/src/dawn/fuzzers/lpmfuzz/DawnLPMSerializerCustom.cpp
index d982f87..005fbe7 100644
--- a/src/dawn/fuzzers/lpmfuzz/DawnLPMSerializerCustom.cpp
+++ b/src/dawn/fuzzers/lpmfuzz/DawnLPMSerializerCustom.cpp
@@ -48,7 +48,7 @@
             WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
             wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
 
-            if (command.devicecreateshadermodule().has_code()) {
+            if (DAWNLPM_FUZZ_TINT && command.devicecreateshadermodule().has_code()) {
                 wgsl_desc.code = command.devicecreateshadermodule().code().c_str();
             } else {
                 wgsl_desc.code =