blob: ba857778d12a26863075957c79594b997108afa4 [file] [log] [blame]
Kevin Lubicke9c1ce82019-03-11 11:09:40 -04001/*
2 * Copyright 2019 Google, LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrShaderCaps.h"
9#include "src/sksl/SkSLCompiler.h"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "fuzz/Fuzz.h"
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040012
13bool FuzzSKSL2SPIRV(sk_sp<SkData> bytes) {
Brian Osmand7e76592020-11-02 12:26:22 -050014 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
15 SkSL::Compiler compiler(caps.get());
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040016 SkSL::String output;
17 SkSL::Program::Settings settings;
John Stilesb0697082021-08-16 14:46:10 -040018
19 // This tells the compiler where the rt-flip uniform will live should it be required. For
20 // fuzzing purposes we don't care where that is, but the compiler will report an error if we
21 // leave them at their default invalid values, or if the offset overlaps another uniform.
22 settings.fRTFlipOffset = 16384;
23 settings.fRTFlipSet = 0;
24 settings.fRTFlipBinding = 0;
25
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040026 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
John Stilesdbd4e6f2021-02-16 13:29:15 -050027 SkSL::ProgramKind::kFragment,
Kevin Lubick39cbe462019-03-11 15:38:00 -040028 SkSL::String((const char*) bytes->data(),
29 bytes->size()),
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040030 settings);
31 if (!program || !compiler.toSPIRV(*program, &output)) {
32 return false;
33 }
34 return true;
35}
36
Kevin Lubick493f89e2020-09-14 08:37:35 -040037#if defined(SK_BUILD_FOR_LIBFUZZER)
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040038extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
Zepeng Hu5b35f212020-06-14 19:03:34 +000039 if (size > 3000) {
40 return 0;
41 }
Kevin Lubicke9c1ce82019-03-11 11:09:40 -040042 auto bytes = SkData::MakeWithoutCopy(data, size);
43 FuzzSKSL2SPIRV(bytes);
44 return 0;
45}
46#endif