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>
This commit is contained in:
Frank Henigman 2019-01-30 16:12:28 +00:00 committed by Commit Bot service account
parent d77fd5f889
commit 4531bd2477
10 changed files with 71 additions and 127 deletions

2
DEPS
View File

@ -68,7 +68,7 @@ deps = {
'condition': 'dawn_standalone', 'condition': 'dawn_standalone',
}, },
'third_party/shaderc': { 'third_party/shaderc': {
'url': '{chromium_git}/external/github.com/google/shaderc@ce7d92182b8cc9c72e99efb5fab1eae3c2084887', 'url': '{chromium_git}/external/github.com/google/shaderc@634dd3545cbccb9362f16f41b3b75703f290a9fd',
'condition': 'dawn_standalone', 'condition': 'dawn_standalone',
}, },

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import("../../scripts/dawn_overrides_with_defaults.gni")
import("//build_overrides/build.gni") import("//build_overrides/build.gni")
dawn_top_level = "../.." dawn_top_level = "../.."
@ -76,7 +77,7 @@ static_library("dawn_spirv_cross_fuzzer_common") {
"DawnSPIRVCrossFuzzer.h", "DawnSPIRVCrossFuzzer.h",
] ]
public_deps = [ public_deps = [
"${dawn_top_level}/third_party:spirv_cross_full_for_fuzzers", "${dawn_shaderc_dir}:libshaderc_spvc",
] ]
} }
@ -111,7 +112,7 @@ dawn_fuzzer_test("dawn_spirv_cross_glsl_full_fuzzer") {
# Uses Dawn specific options and varies input data # Uses Dawn specific options and varies input data
dawn_fuzzer_test("dawn_spirv_cross_hlsl_fast_fuzzer") { dawn_fuzzer_test("dawn_spirv_cross_hlsl_fast_fuzzer") {
sources = [ sources = [
"DawnSPIRVCrossGLSLFastFuzzer.cpp", "DawnSPIRVCrossHLSLFastFuzzer.cpp",
] ]
deps = [ deps = [
":dawn_spirv_cross_fuzzer_common", ":dawn_spirv_cross_fuzzer_common",
@ -124,7 +125,7 @@ dawn_fuzzer_test("dawn_spirv_cross_hlsl_fast_fuzzer") {
# Varies both the options and input data # Varies both the options and input data
dawn_fuzzer_test("dawn_spirv_cross_hlsl_full_fuzzer") { dawn_fuzzer_test("dawn_spirv_cross_hlsl_full_fuzzer") {
sources = [ sources = [
"DawnSPIRVCrossGLSLFullFuzzer.cpp", "DawnSPIRVCrossHLSLFullFuzzer.cpp",
] ]
deps = [ deps = [
":dawn_spirv_cross_fuzzer_common", ":dawn_spirv_cross_fuzzer_common",
@ -137,7 +138,7 @@ dawn_fuzzer_test("dawn_spirv_cross_hlsl_full_fuzzer") {
# Uses Dawn specific options and varies input data # Uses Dawn specific options and varies input data
dawn_fuzzer_test("dawn_spirv_cross_msl_fast_fuzzer") { dawn_fuzzer_test("dawn_spirv_cross_msl_fast_fuzzer") {
sources = [ sources = [
"DawnSPIRVCrossGLSLFastFuzzer.cpp", "DawnSPIRVCrossMSLFastFuzzer.cpp",
] ]
deps = [ deps = [
":dawn_spirv_cross_fuzzer_common", ":dawn_spirv_cross_fuzzer_common",
@ -150,7 +151,7 @@ dawn_fuzzer_test("dawn_spirv_cross_msl_fast_fuzzer") {
# Varies both the options and input data # Varies both the options and input data
dawn_fuzzer_test("dawn_spirv_cross_msl_full_fuzzer") { dawn_fuzzer_test("dawn_spirv_cross_msl_full_fuzzer") {
sources = [ sources = [
"DawnSPIRVCrossGLSLFullFuzzer.cpp", "DawnSPIRVCrossMSLFullFuzzer.cpp",
] ]
deps = [ deps = [
":dawn_spirv_cross_fuzzer_common", ":dawn_spirv_cross_fuzzer_common",
@ -165,12 +166,10 @@ dawn_fuzzer_test("dawn_wire_server_and_frontend_fuzzer") {
deps = [ deps = [
"${dawn_top_level}:dawn_common", "${dawn_top_level}:dawn_common",
"${dawn_top_level}:libdawn_static",
"${dawn_top_level}:libdawn_native_static", "${dawn_top_level}:libdawn_native_static",
"${dawn_top_level}:libdawn_static",
"${dawn_top_level}:libdawn_wire_static", "${dawn_top_level}:libdawn_wire_static",
] ]
additional_configs = [ additional_configs = [ "${dawn_top_level}:dawn_internal" ]
"${dawn_top_level}:dawn_internal",
]
} }

View File

@ -65,43 +65,26 @@ namespace DawnSPIRVCrossFuzzer {
} }
int Run(const uint8_t* data, size_t size, Task task) { int Run(const uint8_t* data, size_t size, Task task) {
if (!data || size < 1)
return 0;
size_t sizeInU32 = size / sizeof(uint32_t); size_t sizeInU32 = size / sizeof(uint32_t);
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);
task(input); if (input.size() != 0) {
task(input);
}
return 0; return 0;
} }
template <class Options> int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions task) {
int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions<Options> task) { shaderc_spvc::CompileOptions options;
if (!data || size < sizeof(Options) + 1) size_t used = options.SetForFuzzing(data, size);
if (used == 0) {
// not enough data to set options
return 0; return 0;
}
Options options = *reinterpret_cast<const Options*>(data); return Run(data + used, size - used, std::bind(task, std::placeholders::_1, options));
data += sizeof(options);
size -= sizeof(options);
std::vector<uint32_t> input(data, data + (4 * (size / 4)));
task(input, options);
return 0;
} }
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 } // namespace DawnSPIRVCrossFuzzer

View File

@ -16,19 +16,13 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
#include "spirv-cross/spirv_glsl.hpp" #include "shaderc/spvc.hpp"
#include "spirv-cross/spirv_hlsl.hpp"
namespace DawnSPIRVCrossFuzzer { namespace DawnSPIRVCrossFuzzer {
struct CombinedOptions {
spirv_cross::CompilerGLSL::Options glsl;
spirv_cross::CompilerHLSL::Options hlsl;
};
using Task = std::function<int(const std::vector<uint32_t>&)>; using Task = std::function<int(const std::vector<uint32_t>&)>;
template <class Options> using TaskWithOptions =
using TaskWithOptions = std::function<int(const std::vector<uint32_t>&, Options)>; 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 // Used to wrap code that may fire a SIGABRT. Do not allocate anything local within |exec|, as
// it is not guaranteed to return. // it is not guaranteed to return.
@ -38,7 +32,6 @@ namespace DawnSPIRVCrossFuzzer {
int Run(const uint8_t* data, size_t size, Task task); 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 // 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 task);
int RunWithOptions(const uint8_t* data, size_t size, TaskWithOptions<Options> task);
} // namespace DawnSPIRVCrossFuzzer } // namespace DawnSPIRVCrossFuzzer

View File

@ -19,22 +19,18 @@
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
namespace { namespace {
int GLSLFastFuzzTask(const std::vector<uint32_t>& input) { int GLSLFastFuzzTask(const std::vector<uint32_t>& input) {
std::unique_ptr<spirv_cross::CompilerGLSL> compiler; shaderc_spvc::Compiler compiler;
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() { if (!compiler.IsValid()) {
compiler = std::make_unique<spirv_cross::CompilerGLSL>(input);
});
if (compiler == nullptr) {
return 0; return 0;
} }
// Using the options that are used by Dawn, they appear in ShaderModuleGL.cpp DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
spirv_cross::CompilerGLSL::Options options; // Using the options that are used by Dawn, they appear in ShaderModuleGL.cpp
options.version = 440; shaderc_spvc::CompileOptions options;
compiler->set_common_options(options); options.SetOutputLanguageVersion(440);
compiler.CompileSpvToGlsl(input.data(), input.size(), options);
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); }); });
return 0; return 0;
} }

View File

@ -20,19 +20,15 @@
namespace { namespace {
int GLSLFullFuzzTask(const std::vector<uint32_t>& input, int GLSLFullFuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
spirv_cross::CompilerGLSL::Options options) { shaderc_spvc::Compiler compiler;
std::unique_ptr<spirv_cross::CompilerGLSL> compiler; if (!compiler.IsValid()) {
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
compiler = std::make_unique<spirv_cross::CompilerGLSL>(input);
});
if (compiler == nullptr) {
return 0; return 0;
} }
compiler->set_common_options(options); DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
compiler.CompileSpvToGlsl(input.data(), input.size(), options);
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); }); });
return 0; return 0;
} }
@ -40,6 +36,5 @@ namespace {
} // namespace } // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
return DawnSPIRVCrossFuzzer::RunWithOptions<spirv_cross::CompilerGLSL::Options>( return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, GLSLFullFuzzTask);
data, size, GLSLFullFuzzTask);
} }

View File

@ -18,30 +18,23 @@
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
#include "spirv-cross/spirv_hlsl.hpp"
namespace { namespace {
int FuzzTask(const std::vector<uint32_t>& input) { int FuzzTask(const std::vector<uint32_t>& input) {
std::unique_ptr<spirv_cross::CompilerHLSL> compiler; shaderc_spvc::Compiler compiler;
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() { if (!compiler.IsValid()) {
compiler = std::make_unique<spirv_cross::CompilerHLSL>(input);
});
if (compiler == nullptr) {
return 0; return 0;
} }
// Using the options that are used by Dawn, they appear in ShaderModuleD3D12.cpp DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
spirv_cross::CompilerGLSL::Options options_glsl; shaderc_spvc::CompileOptions options;
options_glsl.vertex.fixup_clipspace = true;
options_glsl.vertex.flip_vert_y = true;
compiler->set_common_options(options_glsl);
spirv_cross::CompilerHLSL::Options options_hlsl; // Using the options that are used by Dawn, they appear in ShaderModuleD3D12.cpp
options_hlsl.shader_model = 51; options.SetFixupClipspace(true);
compiler->set_hlsl_options(options_hlsl); options.SetFlipVertY(true);
options.SetShaderModel(51);
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); }); compiler.CompileSpvToHlsl(input.data(), input.size(), options);
});
return 0; return 0;
} }

View File

@ -18,29 +18,22 @@
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
#include "spirv-cross/spirv_hlsl.hpp"
namespace { namespace {
int FuzzTask(const std::vector<uint32_t>& input, int FuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
DawnSPIRVCrossFuzzer::CombinedOptions options) { shaderc_spvc::Compiler compiler;
std::unique_ptr<spirv_cross::CompilerHLSL> compiler; if (!compiler.IsValid()) {
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, input]() {
compiler = std::make_unique<spirv_cross::CompilerHLSL>(input);
});
if (compiler == nullptr) {
return 0; return 0;
} }
compiler->set_common_options(options.glsl); DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
compiler->set_hlsl_options(options.hlsl); compiler.CompileSpvToHlsl(input.data(), input.size(), options);
});
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); });
return 0; return 0;
} }
} // namespace } // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
return DawnSPIRVCrossFuzzer::RunWithOptions<DawnSPIRVCrossFuzzer::CombinedOptions>(data, size, return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, FuzzTask);
FuzzTask);
} }

View File

@ -18,24 +18,21 @@
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
#include "spirv-cross/spirv_msl.hpp"
namespace { namespace {
int FuzzTask(const std::vector<uint32_t>& input) { int FuzzTask(const std::vector<uint32_t>& input) {
std::unique_ptr<spirv_cross::CompilerMSL> compiler; shaderc_spvc::Compiler compiler;
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap( if (!compiler.IsValid()) {
[&compiler, input]() { compiler = std::make_unique<spirv_cross::CompilerMSL>(input); });
if (compiler == nullptr) {
return 0; return 0;
} }
// Using the options that are used by Dawn, they appear in ShaderModuleMTL.mm DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input]() {
spirv_cross::CompilerGLSL::Options options; shaderc_spvc::CompileOptions options;
options.vertex.flip_vert_y = true;
compiler->spirv_cross::CompilerGLSL::set_common_options(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; return 0;
} }

View File

@ -18,21 +18,17 @@
#include "DawnSPIRVCrossFuzzer.h" #include "DawnSPIRVCrossFuzzer.h"
#include "spirv-cross/spirv_msl.hpp"
namespace { namespace {
int FuzzTask(const std::vector<uint32_t>& input, spirv_cross::CompilerGLSL::Options options) { int FuzzTask(const std::vector<uint32_t>& input, shaderc_spvc::CompileOptions options) {
std::unique_ptr<spirv_cross::CompilerMSL> compiler; shaderc_spvc::Compiler compiler;
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap( if (!compiler.IsValid()) {
[&compiler, input]() { compiler = std::make_unique<spirv_cross::CompilerMSL>(input); });
if (compiler == nullptr) {
return 0; return 0;
} }
compiler->spirv_cross::CompilerGLSL::set_common_options(options); DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler, &input, &options]() {
compiler.CompileSpvToMsl(input.data(), input.size(), options);
DawnSPIRVCrossFuzzer::ExecuteWithSignalTrap([&compiler]() { compiler->compile(); }); });
return 0; return 0;
} }
@ -40,6 +36,5 @@ namespace {
} // namespace } // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
return DawnSPIRVCrossFuzzer::RunWithOptions<spirv_cross::CompilerGLSL::Options>(data, size, return DawnSPIRVCrossFuzzer::RunWithOptions(data, size, FuzzTask);
FuzzTask);
} }