1/*
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
8#include "src/gpu/GrShaderCaps.h"
9#include "src/sksl/SkSLCompiler.h"
10
11#include "fuzz/Fuzz.h"
12
13bool FuzzSKSL2SPIRV(sk_sp<SkData> bytes) {
14    std::unique_ptr<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
15    SkSL::Compiler compiler(caps.get());
16    SkSL::String output;
17    SkSL::Program::Settings settings;
18
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
26    std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
27                                                    SkSL::ProgramKind::kFragment,
28                                                    SkSL::String((const char*) bytes->data(),
29                                                                 bytes->size()),
30                                                    settings);
31    if (!program || !compiler.toSPIRV(*program, &output)) {
32        return false;
33    }
34    return true;
35}
36
37#if defined(SK_BUILD_FOR_LIBFUZZER)
38extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
39    if (size > 3000) {
40        return 0;
41    }
42    auto bytes = SkData::MakeWithoutCopy(data, size);
43    FuzzSKSL2SPIRV(bytes);
44    return 0;
45}
46#endif
47