1fd4e5da5Sopenharmony_ci// Copyright (c) 2019 Google Inc.
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include <cstdint>
16fd4e5da5Sopenharmony_ci#include <cstring>  // memcpy
17fd4e5da5Sopenharmony_ci#include <vector>
18fd4e5da5Sopenharmony_ci
19fd4e5da5Sopenharmony_ci#include "source/spirv_target_env.h"
20fd4e5da5Sopenharmony_ci#include "spirv-tools/libspirv.hpp"
21fd4e5da5Sopenharmony_ci#include "test/fuzzers/random_generator.h"
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24fd4e5da5Sopenharmony_ci  if (size < 4) {
25fd4e5da5Sopenharmony_ci    // There are not enough bytes to constitute a binary that can be
26fd4e5da5Sopenharmony_ci    // disassembled.
27fd4e5da5Sopenharmony_ci    return 0;
28fd4e5da5Sopenharmony_ci  }
29fd4e5da5Sopenharmony_ci
30fd4e5da5Sopenharmony_ci  spvtools::fuzzers::RandomGenerator random_gen(data, size);
31fd4e5da5Sopenharmony_ci  const spv_context context = spvContextCreate(random_gen.GetTargetEnv());
32fd4e5da5Sopenharmony_ci  if (context == nullptr) {
33fd4e5da5Sopenharmony_ci    return 0;
34fd4e5da5Sopenharmony_ci  }
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ci  std::vector<uint32_t> input;
37fd4e5da5Sopenharmony_ci  input.resize(size >> 2);
38fd4e5da5Sopenharmony_ci  size_t count = 0;
39fd4e5da5Sopenharmony_ci  for (size_t i = 0; (i + 3) < size; i += 4) {
40fd4e5da5Sopenharmony_ci    input[count++] = data[i] | (data[i + 1] << 8) | (data[i + 2] << 16) |
41fd4e5da5Sopenharmony_ci                     (data[i + 3]) << 24;
42fd4e5da5Sopenharmony_ci  }
43fd4e5da5Sopenharmony_ci
44fd4e5da5Sopenharmony_ci  std::vector<char> input_str;
45fd4e5da5Sopenharmony_ci  size_t char_count = input.size() * sizeof(uint32_t) / sizeof(char);
46fd4e5da5Sopenharmony_ci  input_str.resize(char_count);
47fd4e5da5Sopenharmony_ci  memcpy(input_str.data(), input.data(), input.size() * sizeof(uint32_t));
48fd4e5da5Sopenharmony_ci
49fd4e5da5Sopenharmony_ci  spv_text text = nullptr;
50fd4e5da5Sopenharmony_ci  spv_diagnostic diagnostic = nullptr;
51fd4e5da5Sopenharmony_ci
52fd4e5da5Sopenharmony_ci  for (uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
53fd4e5da5Sopenharmony_ci       options <
54fd4e5da5Sopenharmony_ci       (SPV_BINARY_TO_TEXT_OPTION_PRINT | SPV_BINARY_TO_TEXT_OPTION_COLOR |
55fd4e5da5Sopenharmony_ci        SPV_BINARY_TO_TEXT_OPTION_INDENT |
56fd4e5da5Sopenharmony_ci        SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET |
57fd4e5da5Sopenharmony_ci        SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
58fd4e5da5Sopenharmony_ci        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
59fd4e5da5Sopenharmony_ci       options++) {
60fd4e5da5Sopenharmony_ci    spvBinaryToText(context, input.data(), input.size(), options, &text,
61fd4e5da5Sopenharmony_ci                    &diagnostic);
62fd4e5da5Sopenharmony_ci    if (diagnostic) {
63fd4e5da5Sopenharmony_ci      spvDiagnosticDestroy(diagnostic);
64fd4e5da5Sopenharmony_ci      diagnostic = nullptr;
65fd4e5da5Sopenharmony_ci    }
66fd4e5da5Sopenharmony_ci
67fd4e5da5Sopenharmony_ci    if (text) {
68fd4e5da5Sopenharmony_ci      spvTextDestroy(text);
69fd4e5da5Sopenharmony_ci      text = nullptr;
70fd4e5da5Sopenharmony_ci    }
71fd4e5da5Sopenharmony_ci  }
72fd4e5da5Sopenharmony_ci
73fd4e5da5Sopenharmony_ci  spvContextDestroy(context);
74fd4e5da5Sopenharmony_ci  return 0;
75fd4e5da5Sopenharmony_ci}
76