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 "test/test_fixture.h"
25 #include "test/unit_spirv.h"
26
27 namespace spvtest {
28
29 template <typename T>
30 class ValidateBase : public ::testing::Test,
31 public ::testing::WithParamInterface<T> {
32 public:
33 ValidateBase();
34
35 virtual void TearDown();
36
37 // Returns the a spv_const_binary struct
38 spv_const_binary get_const_binary();
39
40 // Checks that 'code' is valid SPIR-V text representation and stores the
41 // binary version for further method calls.
42 void CompileSuccessfully(std::string code,
43 spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
44
45 // Overwrites the word at index 'index' with the given word.
46 // For testing purposes, it is often useful to be able to manipulate the
47 // assembled binary before running the validator on it.
48 // This function overwrites the word at the given index with a new word.
49 void OverwriteAssembledBinary(uint32_t index, uint32_t word);
50
51 // Performs validation on the SPIR-V code.
52 spv_result_t ValidateInstructions(spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
53
54 // Performs validation. Returns the status and stores validation state into
55 // the vstate_ member.
56 spv_result_t ValidateAndRetrieveValidationState(
57 spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
58
59 // Destroys the stored binary.
DestroyBinary()60 void DestroyBinary() {
61 spvBinaryDestroy(binary_);
62 binary_ = nullptr;
63 }
64
65 // Destroys the stored diagnostic.
DestroyDiagnostic()66 void DestroyDiagnostic() {
67 spvDiagnosticDestroy(diagnostic_);
68 diagnostic_ = nullptr;
69 }
70
71 std::string getDiagnosticString();
72 spv_position_t getErrorPosition();
73 spv_validator_options getValidatorOptions();
74
75 spv_binary binary_;
76 spv_diagnostic diagnostic_;
77 spv_validator_options options_;
78 std::unique_ptr<spvtools::val::ValidationState_t> vstate_;
79 };
80
81 template <typename T>
ValidateBase()82 ValidateBase<T>::ValidateBase() : binary_(nullptr), diagnostic_(nullptr) {
83 // Initialize to default command line options. Different tests can then
84 // specialize specific options as necessary.
85 options_ = spvValidatorOptionsCreate();
86 }
87
88 template <typename T>
get_const_binary()89 spv_const_binary ValidateBase<T>::get_const_binary() {
90 return spv_const_binary(binary_);
91 }
92
93 template <typename T>
TearDown()94 void ValidateBase<T>::TearDown() {
95 if (diagnostic_) {
96 spvDiagnosticPrint(diagnostic_);
97 }
98 DestroyBinary();
99 DestroyDiagnostic();
100 spvValidatorOptionsDestroy(options_);
101 }
102
103 template <typename T>
CompileSuccessfully(std::string code,spv_target_env env)104 void ValidateBase<T>::CompileSuccessfully(std::string code,
105 spv_target_env env) {
106 DestroyBinary();
107 spv_diagnostic diagnostic = nullptr;
108 ASSERT_EQ(SPV_SUCCESS,
109 spvTextToBinary(ScopedContext(env).context, code.c_str(),
110 code.size(), &binary_, &diagnostic))
111 << "ERROR: " << diagnostic->error
112 << "\nSPIR-V could not be compiled into binary:\n"
113 << code;
114 spvDiagnosticDestroy(diagnostic);
115 }
116
117 template <typename T>
OverwriteAssembledBinary(uint32_t index,uint32_t word)118 void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
119 ASSERT_TRUE(index < binary_->wordCount)
120 << "OverwriteAssembledBinary: The given index is larger than the binary "
121 "word count.";
122 binary_->code[index] = word;
123 }
124
125 template <typename T>
ValidateInstructions(spv_target_env env)126 spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
127 DestroyDiagnostic();
128 return spvValidateWithOptions(ScopedContext(env).context, options_,
129 get_const_binary(), &diagnostic_);
130 }
131
132 template <typename T>
ValidateAndRetrieveValidationState(spv_target_env env)133 spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
134 spv_target_env env) {
135 DestroyDiagnostic();
136 return spvtools::val::ValidateBinaryAndKeepValidationState(
137 ScopedContext(env).context, options_, get_const_binary()->code,
138 get_const_binary()->wordCount, &diagnostic_, &vstate_);
139 }
140
141 template <typename T>
getDiagnosticString()142 std::string ValidateBase<T>::getDiagnosticString() {
143 return diagnostic_ == nullptr ? std::string()
144 : std::string(diagnostic_->error);
145 }
146
147 template <typename T>
getValidatorOptions()148 spv_validator_options ValidateBase<T>::getValidatorOptions() {
149 return options_;
150 }
151
152 template <typename T>
getErrorPosition()153 spv_position_t ValidateBase<T>::getErrorPosition() {
154 return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position;
155 }
156
157 } // namespace spvtest
158
159 #endif // TEST_VAL_VAL_FIXTURES_H_
160