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