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<GrShaderCaps> 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                                                     SkSL::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         using String = SkSL::String;
31 
32         String declareUniform(const SkSL::VarDeclaration* decl) override {
33             return 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         String sampleShader(int index, String coords) override {
42             return "child_" + SkSL::to_string(index) + ".eval(" + coords + ")";
43         }
44 
45         String sampleColorFilter(int index, String color) override {
46             return "child_" + SkSL::to_string(index) + ".eval(" + color + ")";
47         }
48 
49         String sampleBlender(int index, String src, String dst) override {
50             return "child_" + SkSL::to_string(index) + ".eval(" + src + ", " + dst + ")";
51         }
52     };
53 
54     Callbacks callbacks;
55     SkSL::PipelineStage::ConvertProgram(*program, "coords", "inColor", "half4(1)", &callbacks);
56     return true;
57 }
58 
59 #if defined(SK_BUILD_FOR_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
61     if (size > 3000) {
62         return 0;
63     }
64     auto bytes = SkData::MakeWithoutCopy(data, size);
65     FuzzSKSL2Pipeline(bytes);
66     return 0;
67 }
68 #endif
69