ids_limit_test.cpp 4.5 KB

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