• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         SpvMagicNumber,
40         SpvVersion,
41         SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
42         id_bound,  // NOTE: Bound
43         0u,        // NOTE: Schema; reserved
44 
45         SpvOpCapability | 2u << SpvWordCountShift,
46         SpvCapabilityShader,
47 
48         SpvOpMemoryModel | 3u << SpvWordCountShift,
49         SpvAddressingModelLogical,
50         SpvMemoryModelSimple,
51 
52         SpvOpTypeBool | 2u << SpvWordCountShift,
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(SpvOpConstantTrue | 3u << SpvWordCountShift);
64       binary.push_back(1u);      // NOTE: Type ID
65       binary.push_back(2u + i);  // NOTE: Result ID
66     }
67   }
TearDown()68   void TearDown() override { binaries.clear(); }
69 
70   spvtest::Binaries binaries;
71 };
72 
CreateBinary(uint32_t id_bound)73 spvtest::Binary CreateBinary(uint32_t id_bound) {
74   return {
75       // clang-format off
76       // Header
77       SpvMagicNumber,
78       SpvVersion,
79       SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
80       id_bound,  // NOTE: Bound
81       0u,        // NOTE: Schema; reserved
82 
83       // OpCapability Shader
84       SpvOpCapability | 2u << SpvWordCountShift,
85       SpvCapabilityShader,
86 
87       // OpMemoryModel Logical Simple
88       SpvOpMemoryModel | 3u << SpvWordCountShift,
89       SpvAddressingModelLogical,
90       SpvMemoryModelSimple
91       // clang-format on
92   };
93 }
94 
TEST_F(IdsLimit,DISABLED_UnderLimit)95 TEST_F(IdsLimit, DISABLED_UnderLimit) {
96   spvtest::Binary linked_binary;
97   ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
98   EXPECT_THAT(GetErrorMessage(), std::string());
99   EXPECT_EQ(0x3FFFFFu, linked_binary[3]);
100 }
101 
TEST_F(IdsLimit,DISABLED_OverLimit)102 TEST_F(IdsLimit, DISABLED_OverLimit) {
103   spvtest::Binary& binary = binaries.back();
104 
105   const uint32_t id_bound = binary[3];
106   binary[3] = id_bound + 1u;
107 
108   binary.push_back(SpvOpConstantFalse | 3u << SpvWordCountShift);
109   binary.push_back(1u);        // NOTE: Type ID
110   binary.push_back(id_bound);  // NOTE: Result ID
111 
112   spvtest::Binary linked_binary;
113   ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
114   EXPECT_THAT(
115       GetErrorMessage(),
116       HasSubstr("The minimum limit of IDs, 4194303, was exceeded: 4194304 is "
117                 "the current ID bound."));
118   EXPECT_EQ(0x400000u, linked_binary[3]);
119 }
120 
TEST_F(IdsLimit,DISABLED_Overflow)121 TEST_F(IdsLimit, DISABLED_Overflow) {
122   spvtest::Binaries binaries = {CreateBinary(0xFFFFFFFFu),
123                                 CreateBinary(0x00000002u)};
124 
125   spvtest::Binary linked_binary;
126 
127   EXPECT_EQ(SPV_ERROR_INVALID_DATA, Link(binaries, &linked_binary));
128   EXPECT_THAT(
129       GetErrorMessage(),
130       HasSubstr("Too many IDs (4294967296): combining all modules would "
131                 "overflow the 32-bit word of the SPIR-V header."));
132 }
133 
134 }  // namespace
135 }  // namespace spvtools
136