Add Tint SPIRV->SPIRV to fuzzing code path

Since we are intending to ship using Tint for normalization, fuzzing
should include this transform.

BUG=dawn:531

Change-Id: Ica25c3bbdc6ff392d5e31361734de43fd1c12815
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/29180
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
Ryan Harrison 2020-09-30 17:50:14 +00:00 committed by Commit Bot service account
parent e2cb68b024
commit da5828c06b
2 changed files with 38 additions and 7 deletions

View File

@ -17,8 +17,7 @@ tint_spirv_tools_dir = "//third_party/SPIRV-Tools"
tint_googletest_dir = "//third_party/googletest" tint_googletest_dir = "//third_party/googletest"
tint_spirv_headers_dir = "//third_party/spirv-headers" tint_spirv_headers_dir = "//third_party/spirv-headers"
# Only need the WGSL->SPIR-V transformation tint_build_spv_reader = true
tint_build_spv_reader = false
tint_build_spv_writer = true tint_build_spv_writer = true
tint_build_wgsl_reader = true tint_build_wgsl_reader = true
tint_build_wgsl_writer = false tint_build_wgsl_writer = false

View File

@ -20,6 +20,9 @@
#include <vector> #include <vector>
#include <spirv-tools/libspirv.hpp> #include <spirv-tools/libspirv.hpp>
#ifdef DAWN_ENABLE_WGSL
# include <tint/tint.h>
#endif // DAWN_ENABLE_WGSL
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
@ -31,7 +34,7 @@ namespace {
// Handler to trap signals, so that it doesn't crash the fuzzer when running // Handler to trap signals, so that it doesn't crash the fuzzer when running
// the code under test. The code being fuzzed uses abort() to report errors // the code under test. The code being fuzzed uses abort() to report errors
// like bad input instead of returning an error code. // like bad input instead of returning an error code.
[[noreturn]] static void sigabrt_trap(int sig) { [[noreturn]] static void sigabrt_trap(int) {
std::longjmp(jump_buffer, 1); std::longjmp(jump_buffer, 1);
} }
@ -68,18 +71,47 @@ namespace DawnSPIRVCrossFuzzer {
const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data); const uint32_t* u32Data = reinterpret_cast<const uint32_t*>(data);
std::vector<uint32_t> input(u32Data, u32Data + sizeInU32); std::vector<uint32_t> input(u32Data, u32Data + sizeInU32);
// Using Tint SPIRV->SPIRV to normalize inputs if supported.
#ifdef DAWN_ENABLE_WGSL
tint::Context context;
tint::reader::spirv::Parser parser(&context, input);
if (!parser.Parse()) {
return 0;
}
tint::ast::Module module = parser.module();
if (!module.IsValid()) {
return 0;
}
tint::TypeDeterminer type_determiner(&context, &module);
if (!type_determiner.Determine()) {
return 0;
}
tint::writer::spirv::Generator generator(std::move(module));
if (!generator.Generate()) {
return 0;
}
std::vector<uint32_t> spirv = generator.result();
#else
std::vector<uint32_t> spirv = std::move(input);
#endif
spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1); spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1);
spirvTools.SetMessageConsumer( spirvTools.SetMessageConsumer(
[](spv_message_level_t, const char*, const spv_position_t&, const char*) {}); [](spv_message_level_t, const char*, const spv_position_t&, const char*) {});
// Dawn is responsible to validating input before it goes into // Dawn is responsible to validating inputs before they go to
// SPIRV-Cross. // SPIRV-Cross.
if (!spirvTools.Validate(input.data(), input.size())) { if (!spirvTools.Validate(spirv.data(), spirv.size())) {
return 0; return 0;
} }
if (input.size() != 0) { if (spirv.size() != 0) {
task(input); task(spirv);
} }
return 0; return 0;