• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sk_sp<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 decl->var().name();
34         }
35 
36         void defineFunction(const char* /*decl*/, const char* /*body*/, bool /*isMain*/) override {}
37         void defineStruct(const char* /*definition*/) override {}
38         void declareGlobal(const char* /*declaration*/) override {}
39 
40         String sampleChild(int index, String coords, String color) override {
41             String result = "sample(" + SkSL::to_string(index);
42             if (!coords.empty()) {
43                 result += ", " + coords;
44             }
45             if (!color.empty()) {
46                 result += ", " + color;
47             }
48             result += ")";
49             return result;
50         }
51     };
52 
53     Callbacks callbacks;
54     SkSL::PipelineStage::ConvertProgram(*program, "coords", "inColor", &callbacks);
55     return true;
56 }
57 
58 #if defined(SK_BUILD_FOR_LIBFUZZER)
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
60     if (size > 3000) {
61         return 0;
62     }
63     auto bytes = SkData::MakeWithoutCopy(data, size);
64     FuzzSKSL2Pipeline(bytes);
65     return 0;
66 }
67 #endif
68