Coverage Report

Created: 2026-01-05 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc_fuzzer.cc
Line
Count
Source
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
792
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
24
  // Skip iteration if data not enough
25
792
  if (size == 0) {
26
0
    return 0;
27
0
  }
28
29
  // Prepare GLSL shader content with valid version
30
792
  std::string shader_content(reinterpret_cast<const char *>(data), size);
31
32
  // Prepare Compiler and options
33
792
  shaderc::Compiler compiler;
34
792
  shaderc::CompileOptions options;
35
792
  options.SetOptimizationLevel(shaderc_optimization_level_performance);
36
37
  // Preprocessing
38
792
  shaderc::PreprocessedSourceCompilationResult preprocess_result =
39
792
      compiler.PreprocessGlsl(shader_content, shaderc_glsl_vertex_shader,
40
792
                              "input.glsl", options);
41
792
  if (preprocess_result.GetCompilationStatus() ==
42
792
      shaderc_compilation_status_success) {
43
451
    std::string preprocessed_code(preprocess_result.cbegin(),
44
451
                                  preprocess_result.cend());
45
451
  } else {
46
341
    return 0;
47
341
  }
48
49
  // Compile to SPIR-V binary
50
451
  shaderc::SpvCompilationResult binary_result = compiler.CompileGlslToSpv(
51
451
      shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
52
451
  if (binary_result.GetCompilationStatus() ==
53
451
      shaderc_compilation_status_success) {
54
31
    std::vector<uint32_t> spirv_binary(binary_result.cbegin(),
55
31
                                       binary_result.cend());
56
31
  }
57
58
  // Compile to SPIR-V assembly
59
451
  shaderc::AssemblyCompilationResult assembly_result =
60
451
      compiler.CompileGlslToSpvAssembly(
61
451
          shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
62
451
  if (assembly_result.GetCompilationStatus() ==
63
451
      shaderc_compilation_status_success) {
64
31
    std::string spirv_assembly(assembly_result.cbegin(),
65
31
                               assembly_result.cend());
66
31
  }
67
68
  // Compile with C API
69
451
  shaderc_compiler_t c_compiler = shaderc_compiler_initialize();
70
451
  shaderc_compilation_result_t c_result = shaderc_compile_into_spv(
71
451
      c_compiler, shader_content.c_str(), shader_content.size(),
72
451
      shaderc_glsl_vertex_shader, "main.vert", "main", nullptr);
73
74
451
  if (shaderc_result_get_compilation_status(c_result) ==
75
451
      shaderc_compilation_status_success) {
76
37
    std::vector<uint32_t> spirv_c_binary(shaderc_result_get_length(c_result) /
77
37
                                         sizeof(uint32_t));
78
37
    std::memcpy(spirv_c_binary.data(), shaderc_result_get_bytes(c_result),
79
37
                shaderc_result_get_length(c_result));
80
37
  }
81
451
  shaderc_result_release(c_result);
82
451
  shaderc_compiler_release(c_compiler);
83
84
451
  return 0;
85
792
}