2016-09-02 22:06:18 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "source/table.h"
|
2016-09-02 22:06:18 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2016-09-02 22:06:18 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-02-02 22:37:14 +00:00
|
|
|
// TODO(antiagainst): Use public C API for setting the consumer once exists.
|
|
|
|
#ifndef SPIRV_TOOLS_SHAREDLIB
|
2018-07-07 13:38:00 +00:00
|
|
|
void SetContextMessageConsumer(spv_context context, MessageConsumer consumer) {
|
|
|
|
spvtools::SetContextMessageConsumer(context, consumer);
|
2018-02-02 22:37:14 +00:00
|
|
|
}
|
|
|
|
#else
|
2018-07-11 13:24:49 +00:00
|
|
|
void SetContextMessageConsumer(spv_context, MessageConsumer) {}
|
2018-02-02 22:37:14 +00:00
|
|
|
#endif
|
2017-11-22 20:18:37 +00:00
|
|
|
|
2016-09-02 22:06:18 +00:00
|
|
|
// The default consumer is a null std::function.
|
|
|
|
TEST(CInterface, DefaultConsumerNullDiagnosticForValidInput) {
|
|
|
|
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
|
|
|
|
const char input_text[] =
|
2017-01-09 16:10:52 +00:00
|
|
|
"OpCapability Shader\n"
|
|
|
|
"OpCapability Linkage\n"
|
|
|
|
"OpMemoryModel Logical GLSL450";
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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[] = "%1 = OpName\n";
|
|
|
|
|
|
|
|
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
|
|
|
|
int invocation = 0;
|
|
|
|
SetContextMessageConsumer(
|
|
|
|
context,
|
2016-09-21 16:44:37 +00:00
|
|
|
[&invocation](spv_message_level_t level, const char* source,
|
2016-09-02 22:06:18 +00:00
|
|
|
const spv_position_t& position, const char* message) {
|
|
|
|
++invocation;
|
2016-09-21 16:44:37 +00:00
|
|
|
EXPECT_EQ(SPV_MSG_ERROR, level);
|
2016-09-02 22:06:18 +00:00
|
|
|
// The error happens at scanning the begining of second line.
|
2016-09-16 19:43:41 +00:00
|
|
|
EXPECT_STREQ("input", source);
|
2016-09-02 22:06:18 +00:00
|
|
|
EXPECT_EQ(1u, position.line);
|
|
|
|
EXPECT_EQ(0u, position.column);
|
|
|
|
EXPECT_EQ(12u, position.index);
|
|
|
|
EXPECT_STREQ("Expected operand, found end of stream.", message);
|
|
|
|
});
|
|
|
|
|
|
|
|
spv_binary binary = nullptr;
|
|
|
|
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
|
|
|
|
spvTextToBinary(context, input_text, sizeof(input_text), &binary,
|
|
|
|
nullptr));
|
2018-02-02 22:37:14 +00:00
|
|
|
#ifndef SPIRV_TOOLS_SHAREDLIB
|
2016-09-02 22:06:18 +00:00
|
|
|
EXPECT_EQ(1, invocation);
|
2018-02-02 22:37:14 +00:00
|
|
|
#endif
|
2016-09-02 22:06:18 +00:00
|
|
|
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,
|
2016-09-21 16:44:37 +00:00
|
|
|
[&invocation](spv_message_level_t level, const char* source,
|
2016-09-02 22:06:18 +00:00
|
|
|
const spv_position_t& position, const char* message) {
|
|
|
|
++invocation;
|
2016-09-21 16:44:37 +00:00
|
|
|
EXPECT_EQ(SPV_MSG_ERROR, level);
|
2016-09-16 19:43:41 +00:00
|
|
|
EXPECT_STREQ("input", source);
|
2016-09-02 22:06:18 +00:00
|
|
|
EXPECT_EQ(0u, position.line);
|
|
|
|
EXPECT_EQ(0u, position.column);
|
|
|
|
EXPECT_EQ(5u, 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));
|
2018-02-02 22:37:14 +00:00
|
|
|
#ifndef SPIRV_TOOLS_SHAREDLIB
|
2016-09-02 22:06:18 +00:00
|
|
|
EXPECT_EQ(1, invocation);
|
2018-02-02 22:37:14 +00:00
|
|
|
#endif
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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,
|
2016-09-21 16:44:37 +00:00
|
|
|
[&invocation](spv_message_level_t level, const char* source,
|
2016-09-02 22:06:18 +00:00
|
|
|
const spv_position_t& position, const char* message) {
|
|
|
|
++invocation;
|
2016-09-21 16:44:37 +00:00
|
|
|
EXPECT_EQ(SPV_MSG_ERROR, level);
|
2016-09-16 19:43:41 +00:00
|
|
|
EXPECT_STREQ("input", source);
|
2016-09-02 22:06:18 +00:00
|
|
|
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);
|
2018-06-19 20:02:44 +00:00
|
|
|
EXPECT_STREQ(
|
|
|
|
"Nop cannot appear before the memory model instruction\n"
|
|
|
|
" OpNop\n",
|
|
|
|
message);
|
2016-09-02 22:06:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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));
|
2018-02-02 22:37:14 +00:00
|
|
|
#ifndef SPIRV_TOOLS_SHAREDLIB
|
2016-09-02 22:06:18 +00:00
|
|
|
EXPECT_EQ(1, invocation);
|
2018-02-02 22:37:14 +00:00
|
|
|
#endif
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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[] = "%1 = OpName";
|
|
|
|
|
|
|
|
auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_1);
|
|
|
|
int invocation = 0;
|
|
|
|
SetContextMessageConsumer(
|
2016-09-21 16:44:37 +00:00
|
|
|
context,
|
|
|
|
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
|
|
|
|
const char*) { ++invocation; });
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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, found end of 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(
|
2016-09-21 16:44:37 +00:00
|
|
|
context,
|
|
|
|
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
|
|
|
|
const char*) { ++invocation; });
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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(
|
2016-09-21 16:44:37 +00:00
|
|
|
context,
|
|
|
|
[&invocation](spv_message_level_t, const char*, const spv_position_t&,
|
|
|
|
const char*) { ++invocation; });
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
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.
|
2018-06-19 20:02:44 +00:00
|
|
|
EXPECT_STREQ(
|
|
|
|
"Nop cannot appear before the memory model instruction\n"
|
|
|
|
" OpNop\n",
|
|
|
|
diagnostic->error);
|
2016-09-02 22:06:18 +00:00
|
|
|
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
spvBinaryDestroy(binary);
|
|
|
|
spvContextDestroy(context);
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|