/src/spirv-tools/test/fuzzers/spvtools_as_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 | 12.5k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
24 | 12.5k | spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0; |
25 | 12.5k | if (size > 0) { |
26 | 12.5k | spvtools::fuzzers::RandomGenerator random_gen(data, size); |
27 | 12.5k | target_env = random_gen.GetTargetEnv(); |
28 | 12.5k | } |
29 | | |
30 | 12.5k | const spv_context context = spvContextCreate(target_env); |
31 | 12.5k | if (context == nullptr) { |
32 | 0 | return 0; |
33 | 0 | } |
34 | | |
35 | 12.5k | std::vector<char> contents; |
36 | 12.5k | contents.resize(size); |
37 | 12.5k | memcpy(contents.data(), data, size); |
38 | | |
39 | 12.5k | spv_binary binary = nullptr; |
40 | 12.5k | spv_diagnostic diagnostic = nullptr; |
41 | 12.5k | spvTextToBinaryWithOptions(context, contents.data(), contents.size(), |
42 | 12.5k | SPV_TEXT_TO_BINARY_OPTION_NONE, &binary, |
43 | 12.5k | &diagnostic); |
44 | 12.5k | if (diagnostic) { |
45 | 6.72k | spvDiagnosticPrint(diagnostic); |
46 | 6.72k | spvDiagnosticDestroy(diagnostic); |
47 | 6.72k | diagnostic = nullptr; |
48 | 6.72k | } |
49 | | |
50 | 12.5k | if (binary) { |
51 | 7.10k | spvBinaryDestroy(binary); |
52 | 7.10k | binary = nullptr; |
53 | 7.10k | } |
54 | | |
55 | 12.5k | spvTextToBinaryWithOptions(context, contents.data(), contents.size(), |
56 | 12.5k | SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS, |
57 | 12.5k | &binary, &diagnostic); |
58 | 12.5k | if (diagnostic) { |
59 | 6.91k | spvDiagnosticPrint(diagnostic); |
60 | 6.91k | spvDiagnosticDestroy(diagnostic); |
61 | 6.91k | diagnostic = nullptr; |
62 | 6.91k | } |
63 | | |
64 | 12.5k | if (binary) { |
65 | 6.87k | spvBinaryDestroy(binary); |
66 | 6.87k | binary = nullptr; |
67 | 6.87k | } |
68 | | |
69 | 12.5k | spvContextDestroy(context); |
70 | | |
71 | 12.5k | return 0; |
72 | 12.5k | } |