1 // Copyright (c) 2017 Pierre Moreau
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 #include <string>
16
17 #include "gmock/gmock.h"
18 #include "test/link/linker_fixture.h"
19
20 namespace spvtools {
21 namespace {
22
23 using ::testing::HasSubstr;
24
25 class IdsLimit : public spvtest::LinkerTest {
26 public:
IdsLimit()27 IdsLimit() { binaries.reserve(2u); }
28
SetUp()29 void SetUp() override {
30 const uint32_t id_bound = SPV_LIMIT_RESULT_ID_BOUND - 1u;
31 const uint32_t constant_count =
32 id_bound -
33 2u; // One ID is used for TypeBool, and (constant_count + 1) < id_bound
34
35 // This is needed, as otherwise the ID bound will get reset to 1 while
36 // running the RemoveDuplicates pass.
37 spvtest::Binary common_binary = {
38 // clang-format off
39 spv::MagicNumber,
40 spv::Version,
41 SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
42 id_bound, // NOTE: Bound
43 0u, // NOTE: Schema; reserved
44
45 static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
46 static_cast<uint32_t>(spv::Capability::Shader),
47
48 static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
49 static_cast<uint32_t>(spv::AddressingModel::Logical),
50 static_cast<uint32_t>(spv::MemoryModel::Simple),
51
52 static_cast<uint32_t>(spv::Op::OpTypeBool) | 2u << spv::WordCountShift,
53 1u // NOTE: Result ID
54 // clang-format on
55 };
56
57 binaries.push_back({});
58 spvtest::Binary& binary = binaries.back();
59 binary.reserve(common_binary.size() + constant_count * 3u);
60 binary.insert(binary.end(), common_binary.cbegin(), common_binary.cend());
61
62 for (uint32_t i = 0u; i < constant_count; ++i) {
63 binary.push_back(static_cast<uint32_t>(spv::Op::OpConstantTrue) |
64 3u << spv::WordCountShift);
65 binary.push_back(1u); // NOTE: Type ID
66 binary.push_back(2u + i); // NOTE: Result ID
67 }
68 }
TearDown()69 void TearDown() override { binaries.clear(); }
70
71 spvtest::Binaries binaries;
72 };
73
CreateBinary(uint32_t id_bound)74 spvtest::Binary CreateBinary(uint32_t id_bound) {
75 return {
76 // clang-format off
77 // Header
78 spv::MagicNumber,
79 spv::Version,
80 SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
81 id_bound, // NOTE: Bound
82 0u, // NOTE: Schema; reserved
83
84 // OpCapability Shader
85 static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
86 static_cast<uint32_t>(spv::Capability::Shader),
87
88 // OpMemoryModel Logical Simple
89 static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
90 static_cast<uint32_t>(spv::AddressingModel::Logical),
91 static_cast<uint32_t>(spv::MemoryModel::Simple)
92 // clang-format on
93 };
94 }
95
TEST_F(IdsLimit,DISABLED_UnderLimit)96 TEST_F(IdsLimit, DISABLED_UnderLimit) {
97 spvtest::Binary linked_binary;
98 ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
99 EXPECT_THAT(GetErrorMessage(), std::string());
100 EXPECT_EQ(0x3FFFFFu, linked_binary[3]);
101 }
102
TEST_F(IdsLimit,DISABLED_OverLimit)103 TEST_F(IdsLimit, DISABLED_OverLimit) {
104 spvtest::Binary& binary = binaries.back();
105
106 const uint32_t id_bound = binary[3];
107 binary[3] = id_bound + 1u;
108
109 binary.push_back(static_cast<uint32_t>(spv::Op::OpConstantFalse) |
110 3u << spv::WordCountShift);
111 binary.push_back(1u); // NOTE: Type ID
112 binary.push_back(id_bound); // NOTE: Result ID
113
114 spvtest::Binary linked_binary;
115 ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
116 EXPECT_THAT(
117 GetErrorMessage(),
118 HasSubstr("The minimum limit of IDs, 4194303, was exceeded: 4194304 is "
119 "the current ID bound."));
120 EXPECT_EQ(0x400000u, linked_binary[3]);
121 }
122
TEST_F(IdsLimit,DISABLED_Overflow)123 TEST_F(IdsLimit, DISABLED_Overflow) {
124 spvtest::Binaries binaries = {CreateBinary(0xFFFFFFFFu),
125 CreateBinary(0x00000002u)};
126
127 spvtest::Binary linked_binary;
128
129 EXPECT_EQ(SPV_ERROR_INVALID_DATA, Link(binaries, &linked_binary));
130 EXPECT_THAT(
131 GetErrorMessage(),
132 HasSubstr("Too many IDs (4294967296): combining all modules would "
133 "overflow the 32-bit word of the SPIR-V header."));
134 }
135
136 } // namespace
137 } // namespace spvtools
138