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 #include "src/sksl/codegen/SkSLPipelineStageCodeGenerator.h"
11 #include "src/sksl/ir/SkSLVarDeclarations.h"
12 #include "src/sksl/ir/SkSLVariable.h"
13
14 #include "fuzz/Fuzz.h"
15
FuzzSKSL2Pipeline(sk_sp<SkData> bytes)16 bool FuzzSKSL2Pipeline(sk_sp<SkData> bytes) {
17 std::unique_ptr<SkSL::ShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
18 SkSL::Compiler compiler(caps.get());
19 SkSL::Program::Settings settings;
20 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
21 SkSL::ProgramKind::kRuntimeShader,
22 std::string((const char*) bytes->data(),
23 bytes->size()),
24 settings);
25 if (!program) {
26 return false;
27 }
28
29 class Callbacks : public SkSL::PipelineStage::Callbacks {
30 std::string declareUniform(const SkSL::VarDeclaration* decl) override {
31 return std::string(decl->var().name());
32 }
33
34 void defineFunction(const char* /*decl*/, const char* /*body*/, bool /*isMain*/) override {}
35 void declareFunction(const char* /*decl*/) override {}
36 void defineStruct(const char* /*definition*/) override {}
37 void declareGlobal(const char* /*declaration*/) override {}
38
39 std::string sampleShader(int index, std::string coords) override {
40 return "child_" + std::to_string(index) + ".eval(" + coords + ")";
41 }
42
43 std::string sampleColorFilter(int index, std::string color) override {
44 return "child_" + std::to_string(index) + ".eval(" + color + ")";
45 }
46
47 std::string sampleBlender(int index, std::string src, std::string dst) override {
48 return "child_" + std::to_string(index) + ".eval(" + src + ", " + dst + ")";
49 }
50
51 std::string toLinearSrgb(std::string color) override { return color; }
52 std::string fromLinearSrgb(std::string color) override { return color; }
53 };
54
55 Callbacks callbacks;
56 SkSL::PipelineStage::ConvertProgram(*program, "coords", "inColor", "half4(1)", &callbacks);
57 return true;
58 }
59
60 #if defined(SK_BUILD_FOR_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
62 if (size > 3000) {
63 return 0;
64 }
65 auto bytes = SkData::MakeWithoutCopy(data, size);
66 FuzzSKSL2Pipeline(bytes);
67 return 0;
68 }
69 #endif
70