xref: /aosp_15_r20/external/skia/fuzz/oss_fuzz/FuzzSKSL2Pipeline.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/sksl/SkSLCompiler.h"
9 #include "src/sksl/SkSLProgramKind.h"
10 #include "src/sksl/SkSLProgramSettings.h"
11 #include "src/sksl/codegen/SkSLPipelineStageCodeGenerator.h"
12 #include "src/sksl/ir/SkSLProgram.h"
13 #include "src/sksl/ir/SkSLVarDeclarations.h"
14 #include "src/sksl/ir/SkSLVariable.h"
15 
16 #include "fuzz/Fuzz.h"
17 
FuzzSKSL2Pipeline(const uint8_t * data,size_t size)18 bool FuzzSKSL2Pipeline(const uint8_t *data, size_t size) {
19     SkSL::Compiler compiler;
20     SkSL::ProgramSettings settings;
21     std::unique_ptr<SkSL::Program> program =
22             compiler.convertProgram(SkSL::ProgramKind::kRuntimeShader,
23                                     std::string(reinterpret_cast<const char*>(data), 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     FuzzSKSL2Pipeline(data, size);
66     return 0;
67 }
68 #endif
69