Coverage Report

Created: 2025-07-04 07:23

/src/shaderc_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2024 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <cstring>
16
#include <iostream>
17
#include <string>
18
#include <vector>
19
20
#include <shaderc/shaderc.h>
21
#include <shaderc/shaderc.hpp>
22
23
5.90k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
24
  // Skip iteration if data not enough
25
5.90k
  if (size == 0) {
26
0
    return 0;
27
0
  }
28
29
  // Prepare GLSL shader content with valid version
30
5.90k
  std::string shader_content(reinterpret_cast<const char *>(data), size);
31
32
  // Prepare Compiler and options
33
5.90k
  shaderc::Compiler compiler;
34
5.90k
  shaderc::CompileOptions options;
35
5.90k
  options.SetOptimizationLevel(shaderc_optimization_level_performance);
36
37
  // Preprocessing
38
5.90k
  shaderc::PreprocessedSourceCompilationResult preprocess_result =
39
5.90k
      compiler.PreprocessGlsl(shader_content, shaderc_glsl_vertex_shader,
40
5.90k
                              "input.glsl", options);
41
5.90k
  if (preprocess_result.GetCompilationStatus() ==
42
5.90k
      shaderc_compilation_status_success) {
43
4.65k
    std::string preprocessed_code(preprocess_result.cbegin(),
44
4.65k
                                  preprocess_result.cend());
45
4.65k
  } else {
46
1.24k
    return 0;
47
1.24k
  }
48
49
  // Compile to SPIR-V binary
50
4.65k
  shaderc::SpvCompilationResult binary_result = compiler.CompileGlslToSpv(
51
4.65k
      shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
52
4.65k
  if (binary_result.GetCompilationStatus() ==
53
4.65k
      shaderc_compilation_status_success) {
54
423
    std::vector<uint32_t> spirv_binary(binary_result.cbegin(),
55
423
                                       binary_result.cend());
56
423
  }
57
58
  // Compile to SPIR-V assembly
59
4.65k
  shaderc::AssemblyCompilationResult assembly_result =
60
4.65k
      compiler.CompileGlslToSpvAssembly(
61
4.65k
          shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
62
4.65k
  if (assembly_result.GetCompilationStatus() ==
63
4.65k
      shaderc_compilation_status_success) {
64
423
    std::string spirv_assembly(assembly_result.cbegin(),
65
423
                               assembly_result.cend());
66
423
  }
67
68
  // Compile with C API
69
4.65k
  shaderc_compiler_t c_compiler = shaderc_compiler_initialize();
70
4.65k
  shaderc_compilation_result_t c_result = shaderc_compile_into_spv(
71
4.65k
      c_compiler, shader_content.c_str(), shader_content.size(),
72
4.65k
      shaderc_glsl_vertex_shader, "main.vert", "main", nullptr);
73
74
4.65k
  if (shaderc_result_get_compilation_status(c_result) ==
75
4.65k
      shaderc_compilation_status_success) {
76
491
    std::vector<uint32_t> spirv_c_binary(shaderc_result_get_length(c_result) /
77
491
                                         sizeof(uint32_t));
78
491
    std::memcpy(spirv_c_binary.data(), shaderc_result_get_bytes(c_result),
79
491
                shaderc_result_get_length(c_result));
80
491
  }
81
4.65k
  shaderc_result_release(c_result);
82
4.65k
  shaderc_compiler_release(c_compiler);
83
84
4.65k
  return 0;
85
5.90k
}