Coverage Report

Created: 2026-06-30 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/test/fuzzers/spvtools_dis_fuzzer.cpp
Line
Count
Source
1
// Copyright (c) 2019 Google Inc.
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 <cstdint>
16
#include <cstring>  // memcpy
17
#include <vector>
18
19
#include "source/spirv_target_env.h"
20
#include "spirv-tools/libspirv.hpp"
21
#include "test/fuzzers/random_generator.h"
22
23
0
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24
0
  if (size < 4) {
25
    // There are not enough bytes to constitute a binary that can be
26
    // disassembled.
27
0
    return 0;
28
0
  }
29
30
0
  spvtools::fuzzers::RandomGenerator random_gen(data, size);
31
0
  const spv_context context = spvContextCreate(random_gen.GetTargetEnv());
32
0
  if (context == nullptr) {
33
0
    return 0;
34
0
  }
35
36
0
  std::vector<uint32_t> input;
37
0
  input.resize(size >> 2);
38
0
  size_t count = 0;
39
0
  for (size_t i = 0; (i + 3) < size; i += 4) {
40
0
    input[count++] = data[i] | (data[i + 1] << 8) | (data[i + 2] << 16) |
41
0
                     (data[i + 3]) << 24;
42
0
  }
43
44
0
  std::vector<char> input_str;
45
0
  size_t char_count = input.size() * sizeof(uint32_t) / sizeof(char);
46
0
  input_str.resize(char_count);
47
0
  memcpy(input_str.data(), input.data(), input.size() * sizeof(uint32_t));
48
49
0
  spv_text text = nullptr;
50
0
  spv_diagnostic diagnostic = nullptr;
51
52
0
  for (uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
53
0
       options <
54
0
       (SPV_BINARY_TO_TEXT_OPTION_PRINT | SPV_BINARY_TO_TEXT_OPTION_COLOR |
55
0
        SPV_BINARY_TO_TEXT_OPTION_INDENT |
56
0
        SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET |
57
0
        SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
58
0
        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
59
0
       options++) {
60
0
    spvBinaryToText(context, input.data(), input.size(), options, &text,
61
0
                    &diagnostic);
62
0
    if (diagnostic) {
63
0
      spvDiagnosticDestroy(diagnostic);
64
0
      diagnostic = nullptr;
65
0
    }
66
67
0
    if (text) {
68
0
      spvTextDestroy(text);
69
0
      text = nullptr;
70
0
    }
71
0
  }
72
73
0
  spvContextDestroy(context);
74
0
  return 0;
75
0
}