1 // Copyright (c) 2015-2016 The Khronos Group 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 // Common validation fixtures for unit tests
16
17 #ifndef TEST_VAL_VAL_FIXTURES_H_
18 #define TEST_VAL_VAL_FIXTURES_H_
19
20 #include <memory>
21 #include <string>
22
23 #include "source/val/validation_state.h"
24 #include "spirv-tools/libspirv.h"
25 #include "test/test_fixture.h"
26 #include "test/unit_spirv.h"
27
28 namespace spvtest {
29
30 template <typename T>
31 class ValidateBase : public ::testing::Test,
32 public ::testing::WithParamInterface<T> {
33 public:
34 ValidateBase();
35
36 virtual void TearDown();
37
38 // Returns the a spv_const_binary struct
39 spv_const_binary get_const_binary();
40
41 // Assembles the given SPIR-V text, checks that it fails to assemble,
42 // and returns resulting diagnostic. No internal state is updated.
43 std::string CompileFailure(std::string code,
44 spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
45
46 // Checks that 'code' is valid SPIR-V text representation and stores the
47 // binary version for further method calls.
48 void CompileSuccessfully(std::string code,
49 spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
50
51 // Overwrites the word at index 'index' with the given word.
52 // For testing purposes, it is often useful to be able to manipulate the
53 // assembled binary before running the validator on it.
54 // This function overwrites the word at the given index with a new word.
55 void OverwriteAssembledBinary(uint32_t index, uint32_t word);
56
57 // Performs validation on the SPIR-V code.
58 spv_result_t ValidateInstructions(spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
59
60 // Performs validation. Returns the status and stores validation state into
61 // the vstate_ member.
62 spv_result_t ValidateAndRetrieveValidationState(
63 spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
64
65 // Destroys the stored binary.
DestroyBinary()66 void DestroyBinary() {
67 spvBinaryDestroy(binary_);
68 binary_ = nullptr;
69 }
70
71 // Destroys the stored diagnostic.
DestroyDiagnostic()72 void DestroyDiagnostic() {
73 spvDiagnosticDestroy(diagnostic_);
74 diagnostic_ = nullptr;
75 }
76
77 std::string getDiagnosticString();
78 spv_position_t getErrorPosition();
79 spv_validator_options getValidatorOptions();
80
81 spv_binary binary_;
82 spv_diagnostic diagnostic_;
83 spv_validator_options options_;
84 std::unique_ptr<spvtools::val::ValidationState_t> vstate_;
85 };
86
87 template <typename T>
ValidateBase()88 ValidateBase<T>::ValidateBase() : binary_(nullptr), diagnostic_(nullptr) {
89 // Initialize to default command line options. Different tests can then
90 // specialize specific options as necessary.
91 options_ = spvValidatorOptionsCreate();
92 }
93
94 template <typename T>
get_const_binary()95 spv_const_binary ValidateBase<T>::get_const_binary() {
96 return spv_const_binary(binary_);
97 }
98
99 template <typename T>
TearDown()100 void ValidateBase<T>::TearDown() {
101 if (diagnostic_) {
102 spvDiagnosticPrint(diagnostic_);
103 }
104 DestroyBinary();
105 DestroyDiagnostic();
106 spvValidatorOptionsDestroy(options_);
107 }
108
109 template <typename T>
CompileFailure(std::string code,spv_target_env env)110 std::string ValidateBase<T>::CompileFailure(std::string code,
111 spv_target_env env) {
112 spv_diagnostic diagnostic = nullptr;
113 EXPECT_NE(SPV_SUCCESS,
114 spvTextToBinary(ScopedContext(env).context, code.c_str(),
115 code.size(), &binary_, &diagnostic));
116 std::string result(diagnostic->error);
117 spvDiagnosticDestroy(diagnostic);
118 return result;
119 }
120
121 template <typename T>
CompileSuccessfully(std::string code,spv_target_env env)122 void ValidateBase<T>::CompileSuccessfully(std::string code,
123 spv_target_env env) {
124 DestroyBinary();
125 spv_diagnostic diagnostic = nullptr;
126 ScopedContext context(env);
127 auto status = spvTextToBinary(context.context, code.c_str(), code.size(),
128 &binary_, &diagnostic);
129 EXPECT_EQ(SPV_SUCCESS, status)
130 << "ERROR: " << diagnostic->error
131 << "\nSPIR-V could not be compiled into binary:\n"
132 << code;
133 ASSERT_EQ(SPV_SUCCESS, status);
134 spvDiagnosticDestroy(diagnostic);
135 }
136
137 template <typename T>
OverwriteAssembledBinary(uint32_t index,uint32_t word)138 void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
139 ASSERT_TRUE(index < binary_->wordCount)
140 << "OverwriteAssembledBinary: The given index is larger than the binary "
141 "word count.";
142 binary_->code[index] = word;
143 }
144
145 template <typename T>
ValidateInstructions(spv_target_env env)146 spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
147 DestroyDiagnostic();
148 if (binary_ == nullptr) {
149 fprintf(stderr,
150 "ERROR: Attempting to validate a null binary, did you forget to "
151 "call CompileSuccessfully?");
152 fflush(stderr);
153 }
154 assert(binary_ != nullptr);
155 return spvValidateWithOptions(ScopedContext(env).context, options_,
156 get_const_binary(), &diagnostic_);
157 }
158
159 template <typename T>
ValidateAndRetrieveValidationState(spv_target_env env)160 spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
161 spv_target_env env) {
162 DestroyDiagnostic();
163 return spvtools::val::ValidateBinaryAndKeepValidationState(
164 ScopedContext(env).context, options_, get_const_binary()->code,
165 get_const_binary()->wordCount, &diagnostic_, &vstate_);
166 }
167
168 template <typename T>
getDiagnosticString()169 std::string ValidateBase<T>::getDiagnosticString() {
170 return diagnostic_ == nullptr ? std::string()
171 : std::string(diagnostic_->error);
172 }
173
174 template <typename T>
getValidatorOptions()175 spv_validator_options ValidateBase<T>::getValidatorOptions() {
176 return options_;
177 }
178
179 template <typename T>
getErrorPosition()180 spv_position_t ValidateBase<T>::getErrorPosition() {
181 return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position;
182 }
183
184 } // namespace spvtest
185
186 #endif // TEST_VAL_VAL_FIXTURES_H_
187