blob: b5abe478f68109c3a71d750b1d9b0b12e7ef7729 [file] [log] [blame]
Vasyl Teliman0b3611b2021-06-24 18:10:46 +00001// Copyright 2021 The Tint Authors.
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#ifndef FUZZERS_TINT_SPIRV_TOOLS_FUZZER_UTIL_H_
16#define FUZZERS_TINT_SPIRV_TOOLS_FUZZER_UTIL_H_
17
18#include <sstream>
19#include <string>
20#include <vector>
21
22#include "fuzzers/tint_common_fuzzer.h"
23#include "fuzzers/tint_spirv_tools_fuzzer/mutator.h"
24
25#include "spirv-tools/libspirv.hpp"
26
27namespace tint {
28namespace fuzzers {
29namespace spvtools_fuzzer {
30namespace util {
31
32/// @param buffer will be used to output errors by the returned message
33/// consumer. Must remain in scope as long as the returned consumer is in
34/// scope.
35/// @return the message consumer that will print errors to the `buffer`.
36spvtools::MessageConsumer GetBufferMessageConsumer(std::stringstream* buffer);
37
38/// Output errors from the SPV -> WGSL conversion.
39///
40/// @param message - the error message.
41/// @param data - invalid SPIR-V binary.
42/// @param size - the size of `data`.
43/// @param error_dir - the directory, to which the binary will be printed to.
44/// If it's empty, the invalid binary and supplemental files will not be
45/// printed. Otherwise, it must have a `spv/` subdirectory.
46void LogSpvError(const std::string& message,
47 const uint8_t* data,
48 size_t size,
49 const std::string& error_dir);
50
51/// Output errors from the WGSL -> `output_format` conversion.
52///
53/// @param message - the error message.
54/// @param data - the SPIR-V binary that generated the WGSL binary.
55/// @param size - the size of `data`.
56/// @param wgsl - the invalid WGSL binary.
57/// @param output_format - the format which we attempted to convert `wgsl` to.
58/// @param error_dir - the directory, to which the binary will be printed out.
59/// If it's empty, the invalid binary and supplemental files will not be
60/// printed. Otherwise, it must have a `wgsl/` subdirectory.
61void LogWgslError(const std::string& message,
62 const uint8_t* data,
63 size_t size,
64 const std::string& wgsl,
65 OutputFormat output_format,
66 const std::string& error_dir);
67
68/// Output errors produced by the mutator.
69///
70/// @param mutator - the mutator with invalid state.
71/// @param error_dir - the directory, to which invalid files will be printed to.
72/// If it's empty, the invalid binary and supplemental files will not be
73/// printed. Otherwise, it must have a `mutator/` subdirectory.
74void LogMutatorError(const Mutator& mutator, const std::string& error_dir);
75
76/// Reads SPIR-V binary from `path` into `out`. Returns `true` if successful and
77/// `false` otherwise (in this case, `out` is unchanged).
78///
79/// @param path - the path to the SPIR-V binary.
80/// @param out - may be a `nullptr`. In this case, `false` is returned.
81/// @return `true` if successful and `false` otherwise.
82bool ReadBinary(const std::string& path, std::vector<uint32_t>* out);
83
84/// Writes `binary` into `path`.
85///
86/// @param path - the path to write `binary` to.
87/// @param binary - SPIR-V binary.
88/// @return whether the operation was successful.
89bool WriteBinary(const std::string& path, const std::vector<uint32_t>& binary);
90
91} // namespace util
92} // namespace spvtools_fuzzer
93} // namespace fuzzers
94} // namespace tint
95
96#endif // FUZZERS_TINT_SPIRV_TOOLS_FUZZER_UTIL_H_