1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gtest/gtest.h"
#include "source/table.h"
#include "spirv-tools/libspirv.h"
namespace spvtools {
namespace {
// TODO(antiagainst): Use public C API for setting the consumer once exists.
#ifndef SPIRV_TOOLS_SHAREDLIB
void SetContextMessageConsumer(spv_context context, MessageConsumer consumer) {
spvtools::SetContextMessageConsumer(context, consumer);
}
#else
void SetContextMessageConsumer(spv_context, MessageConsumer) {}
#endif
// The default consumer is a null std::function.
TEST(CInterface, DefaultConsumerNullDiagnosticForValidInput) {
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
const char input_text[] =
"OpCapability Shader\n"
"OpCapability Linkage\n"
"OpMemoryModel Logical GLSL450";
spv_binary binary = nullptr;
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
{
// Sadly the compiler don't allow me to feed binary directly to
// spvValidate().
spv_const_binary_t b{binary->code, binary->wordCount};
EXPECT_EQ(SPV_SUCCESS, spvValidate(context, &b, nullptr));
}
spv_text text = nullptr;
EXPECT_EQ(SPV_SUCCESS, spvBinaryToText(context, binary->code,
binary->wordCount, 0, &text, nullptr));
spvTextDestroy(text);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
// The default consumer is a null std::function.
TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidAssembling) {
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
const char input_text[] = "%1 = OpName";
spv_binary binary = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(context, input_text, sizeof(input_text), &binary,
nullptr));
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
// The default consumer is a null std::function.
TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidDiassembling) {
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
const char input_text[] = "OpNop";
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
// Change OpNop to an invalid (wordcount|opcode) word.
binary->code[binary->wordCount - 1] = 0xffffffff;
spv_text text = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
nullptr));
spvTextDestroy(text);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
// The default consumer is a null std::function.
TEST(CInterface, DefaultConsumerNullDiagnosticForInvalidValidating) {
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
const char input_text[] = "OpNop";
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
spv_const_binary_t b{binary->code, binary->wordCount};
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, nullptr));
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
TEST(CInterface, SpecifyConsumerNullDiagnosticForAssembling) {
const char input_text[] = " OpName\n";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t level, const char* source,
const spv_position_t& position, const char* message) {
++invocation;
EXPECT_EQ(SPV_MSG_ERROR, level);
// The error happens at scanning the beginning of second line.
EXPECT_STREQ("input", source);
EXPECT_EQ(1u, position.line);
EXPECT_EQ(0u, position.column);
EXPECT_EQ(12u, position.index);
EXPECT_STREQ(
"Expected operand for OpName instruction, but found the end of the "
"stream.",
message);
});
spv_binary binary = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(context, input_text, sizeof(input_text), &binary,
nullptr));
#ifndef SPIRV_TOOLS_SHAREDLIB
EXPECT_EQ(1, invocation);
#endif
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
TEST(CInterface, SpecifyConsumerNullDiagnosticForDisassembling) {
const char input_text[] = "OpNop";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t level, const char* source,
const spv_position_t& position, const char* message) {
++invocation;
EXPECT_EQ(SPV_MSG_ERROR, level);
EXPECT_STREQ("input", source);
EXPECT_EQ(0u, position.line);
EXPECT_EQ(0u, position.column);
EXPECT_EQ(1u, position.index);
EXPECT_STREQ("Invalid opcode: 65535", message);
});
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
// Change OpNop to an invalid (wordcount|opcode) word.
binary->code[binary->wordCount - 1] = 0xffffffff;
spv_text text = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
nullptr));
#ifndef SPIRV_TOOLS_SHAREDLIB
EXPECT_EQ(1, invocation);
#endif
spvTextDestroy(text);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
TEST(CInterface, SpecifyConsumerNullDiagnosticForValidating) {
const char input_text[] = "OpNop";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t level, const char* source,
const spv_position_t& position, const char* message) {
++invocation;
EXPECT_EQ(SPV_MSG_ERROR, level);
EXPECT_STREQ("input", source);
EXPECT_EQ(0u, position.line);
EXPECT_EQ(0u, position.column);
// TODO(antiagainst): what validation reports is not a word offset here.
// It is inconsistent with diassembler. Should be fixed.
EXPECT_EQ(1u, position.index);
EXPECT_STREQ(
"Nop cannot appear before the memory model instruction\n"
" OpNop\n",
message);
});
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
spv_const_binary_t b{binary->code, binary->wordCount};
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, nullptr));
#ifndef SPIRV_TOOLS_SHAREDLIB
EXPECT_EQ(1, invocation);
#endif
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
// When having both a consumer and an diagnostic object, the diagnostic object
// should take priority.
TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForAssembling) {
const char input_text[] = " OpName";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
const char*) { ++invocation; });
spv_binary binary = nullptr;
spv_diagnostic diagnostic = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(context, input_text, sizeof(input_text), &binary,
&diagnostic));
EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
EXPECT_STREQ(
"Expected operand for OpName instruction, but found the end of the "
"stream.",
diagnostic->error);
spvDiagnosticDestroy(diagnostic);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForDisassembling) {
const char input_text[] = "OpNop";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
const char*) { ++invocation; });
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
// Change OpNop to an invalid (wordcount|opcode) word.
binary->code[binary->wordCount - 1] = 0xffffffff;
spv_diagnostic diagnostic = nullptr;
spv_text text = nullptr;
EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryToText(context, binary->code, binary->wordCount, 0, &text,
&diagnostic));
EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
EXPECT_STREQ("Invalid opcode: 65535", diagnostic->error);
spvTextDestroy(text);
spvDiagnosticDestroy(diagnostic);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
TEST(CInterface, SpecifyConsumerSpecifyDiagnosticForValidating) {
const char input_text[] = "OpNop";
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
int invocation = 0;
SetContextMessageConsumer(
context,
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
const char*) { ++invocation; });
spv_binary binary = nullptr;
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(context, input_text,
sizeof(input_text), &binary, nullptr));
spv_diagnostic diagnostic = nullptr;
spv_const_binary_t b{binary->code, binary->wordCount};
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, spvValidate(context, &b, &diagnostic));
EXPECT_EQ(0, invocation); // Consumer should not be invoked at all.
EXPECT_STREQ(
"Nop cannot appear before the memory model instruction\n"
" OpNop\n",
diagnostic->error);
spvDiagnosticDestroy(diagnostic);
spvBinaryDestroy(binary);
spvContextDestroy(context);
}
} // namespace
} // namespace spvtools
|