Coverage Report

Created: 2021-08-22 09:07

/src/skia/fuzz/oss_fuzz/FuzzSKSL2Pipeline.cpp
Line
Count
Source (jump to first uncovered line)
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
16
11.5k
bool FuzzSKSL2Pipeline(sk_sp<SkData> bytes) {
17
11.5k
    sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
18
11.5k
    SkSL::Compiler compiler(caps.get());
19
11.5k
    SkSL::Program::Settings settings;
20
11.5k
    std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
21
11.5k
                                                    SkSL::ProgramKind::kRuntimeShader,
22
11.5k
                                                    SkSL::String((const char*) bytes->data(),
23
11.5k
                                                                 bytes->size()),
24
11.5k
                                                    settings);
25
11.5k
    if (!program) {
26
7.71k
        return false;
27
7.71k
    }
28
29
3.87k
    class Callbacks : public SkSL::PipelineStage::Callbacks {
30
3.87k
        using String = SkSL::String;
31
32
0
        String declareUniform(const SkSL::VarDeclaration* decl) override {
33
0
            return String(decl->var().name());
34
0
        }
35
36
0
        void defineFunction(const char* /*decl*/, const char* /*body*/, bool /*isMain*/) override {}
37
784
        void defineStruct(const char* /*definition*/) override {}
38
3.68k
        void declareGlobal(const char* /*declaration*/) override {}
39
40
0
        String sampleShader(int index, String coords) override {
41
0
            return "sample(" + SkSL::to_string(index) + ", " + coords + ")";
42
0
        }
43
44
0
        String sampleColorFilter(int index, String color) override {
45
0
            String result = "sample(" + SkSL::to_string(index);
46
0
            if (!color.empty()) {
47
0
                result += ", " + color;
48
0
            }
49
0
            result += ")";
50
0
            return result;
51
0
        }
52
53
0
        String sampleBlender(int index, String src, String dst) override {
54
0
            return "sample(" + SkSL::to_string(index) + ", " + src + ", " + dst + ")";
55
0
        }
56
3.87k
    };
57
58
3.87k
    Callbacks callbacks;
59
3.87k
    SkSL::PipelineStage::ConvertProgram(*program, "coords", "inColor", "half4(1)", &callbacks);
60
3.87k
    return true;
61
3.87k
}
62
63
#if defined(SK_BUILD_FOR_LIBFUZZER)
64
183k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
65
183k
    if (size > 3000) {
66
151
        return 0;
67
151
    }
68
183k
    auto bytes = SkData::MakeWithoutCopy(data, size);
69
183k
    FuzzSKSL2Pipeline(bytes);
70
183k
    return 0;
71
183k
}
72
#endif