transformation_add_constant_composite_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (c) 2019 Google LLC
  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 "source/fuzz/transformation_add_constant_composite.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "test/fuzz/fuzz_test_util.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. namespace {
  21. TEST(TransformationAddConstantCompositeTest, BasicTest) {
  22. std::string shader = R"(
  23. OpCapability Shader
  24. %1 = OpExtInstImport "GLSL.std.450"
  25. OpMemoryModel Logical GLSL450
  26. OpEntryPoint Fragment %4 "main"
  27. OpExecutionMode %4 OriginUpperLeft
  28. OpSource ESSL 310
  29. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeFloat 32
  32. %7 = OpTypeVector %6 2
  33. %8 = OpTypeMatrix %7 3
  34. %11 = OpConstant %6 0
  35. %12 = OpConstant %6 1
  36. %14 = OpConstant %6 2
  37. %15 = OpConstant %6 3
  38. %17 = OpConstant %6 4
  39. %18 = OpConstant %6 5
  40. %21 = OpTypeInt 32 1
  41. %22 = OpTypeInt 32 0
  42. %23 = OpConstant %22 3
  43. %24 = OpTypeArray %21 %23
  44. %25 = OpTypeBool
  45. %26 = OpTypeStruct %24 %25
  46. %29 = OpConstant %21 1
  47. %30 = OpConstant %21 2
  48. %31 = OpConstant %21 3
  49. %33 = OpConstantFalse %25
  50. %35 = OpTypeVector %6 3
  51. %38 = OpConstant %6 6
  52. %39 = OpConstant %6 7
  53. %40 = OpConstant %6 8
  54. %4 = OpFunction %2 None %3
  55. %5 = OpLabel
  56. OpReturn
  57. OpFunctionEnd
  58. )";
  59. const auto env = SPV_ENV_UNIVERSAL_1_4;
  60. const auto consumer = nullptr;
  61. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  62. spvtools::ValidatorOptions validator_options;
  63. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  64. kConsoleMessageConsumer));
  65. TransformationContext transformation_context(
  66. MakeUnique<FactManager>(context.get()), validator_options);
  67. // Too few ids
  68. ASSERT_FALSE(TransformationAddConstantComposite(103, 8, {100, 101}, false)
  69. .IsApplicable(context.get(), transformation_context));
  70. // Too many ids
  71. ASSERT_FALSE(TransformationAddConstantComposite(101, 7, {14, 15, 14}, false)
  72. .IsApplicable(context.get(), transformation_context));
  73. // Id already in use
  74. ASSERT_FALSE(TransformationAddConstantComposite(40, 7, {11, 12}, false)
  75. .IsApplicable(context.get(), transformation_context));
  76. // %39 is not a type
  77. ASSERT_FALSE(TransformationAddConstantComposite(100, 39, {11, 12}, false)
  78. .IsApplicable(context.get(), transformation_context));
  79. {
  80. // %100 = OpConstantComposite %7 %11 %12
  81. TransformationAddConstantComposite transformation(100, 7, {11, 12}, false);
  82. ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(100));
  83. ASSERT_EQ(nullptr, context->get_constant_mgr()->FindDeclaredConstant(100));
  84. ASSERT_TRUE(
  85. transformation.IsApplicable(context.get(), transformation_context));
  86. ApplyAndCheckFreshIds(transformation, context.get(),
  87. &transformation_context);
  88. ASSERT_EQ(spv::Op::OpConstantComposite,
  89. context->get_def_use_mgr()->GetDef(100)->opcode());
  90. ASSERT_EQ(0.0F, context->get_constant_mgr()
  91. ->FindDeclaredConstant(100)
  92. ->AsVectorConstant()
  93. ->GetComponents()[0]
  94. ->GetFloat());
  95. ASSERT_EQ(1.0F, context->get_constant_mgr()
  96. ->FindDeclaredConstant(100)
  97. ->AsVectorConstant()
  98. ->GetComponents()[1]
  99. ->GetFloat());
  100. }
  101. TransformationAddConstantComposite transformations[] = {
  102. // %101 = OpConstantComposite %7 %14 %15
  103. TransformationAddConstantComposite(101, 7, {14, 15}, false),
  104. // %102 = OpConstantComposite %7 %17 %18
  105. TransformationAddConstantComposite(102, 7, {17, 18}, false),
  106. // %103 = OpConstantComposite %8 %100 %101 %102
  107. TransformationAddConstantComposite(103, 8, {100, 101, 102}, false),
  108. // %104 = OpConstantComposite %24 %29 %30 %31
  109. TransformationAddConstantComposite(104, 24, {29, 30, 31}, false),
  110. // %105 = OpConstantComposite %26 %104 %33
  111. TransformationAddConstantComposite(105, 26, {104, 33}, false),
  112. // %106 = OpConstantComposite %35 %38 %39 %40
  113. TransformationAddConstantComposite(106, 35, {38, 39, 40}, false),
  114. // Same constants but with an irrelevant fact applied.
  115. // %107 = OpConstantComposite %7 %11 %12
  116. TransformationAddConstantComposite(107, 7, {11, 12}, true),
  117. // %108 = OpConstantComposite %7 %14 %15
  118. TransformationAddConstantComposite(108, 7, {14, 15}, true),
  119. // %109 = OpConstantComposite %7 %17 %18
  120. TransformationAddConstantComposite(109, 7, {17, 18}, true),
  121. // %110 = OpConstantComposite %8 %100 %101 %102
  122. TransformationAddConstantComposite(110, 8, {100, 101, 102}, true),
  123. // %111 = OpConstantComposite %24 %29 %30 %31
  124. TransformationAddConstantComposite(111, 24, {29, 30, 31}, true),
  125. // %112 = OpConstantComposite %26 %104 %33
  126. TransformationAddConstantComposite(112, 26, {104, 33}, true),
  127. // %113 = OpConstantComposite %35 %38 %39 %40
  128. TransformationAddConstantComposite(113, 35, {38, 39, 40}, true)};
  129. for (auto& transformation : transformations) {
  130. ASSERT_TRUE(
  131. transformation.IsApplicable(context.get(), transformation_context));
  132. ApplyAndCheckFreshIds(transformation, context.get(),
  133. &transformation_context);
  134. }
  135. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  136. kConsoleMessageConsumer));
  137. for (uint32_t id = 100; id <= 106; ++id) {
  138. ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(id));
  139. }
  140. for (uint32_t id = 107; id <= 113; ++id) {
  141. ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(id));
  142. }
  143. std::string after_transformation = R"(
  144. OpCapability Shader
  145. %1 = OpExtInstImport "GLSL.std.450"
  146. OpMemoryModel Logical GLSL450
  147. OpEntryPoint Fragment %4 "main"
  148. OpExecutionMode %4 OriginUpperLeft
  149. OpSource ESSL 310
  150. %2 = OpTypeVoid
  151. %3 = OpTypeFunction %2
  152. %6 = OpTypeFloat 32
  153. %7 = OpTypeVector %6 2
  154. %8 = OpTypeMatrix %7 3
  155. %11 = OpConstant %6 0
  156. %12 = OpConstant %6 1
  157. %14 = OpConstant %6 2
  158. %15 = OpConstant %6 3
  159. %17 = OpConstant %6 4
  160. %18 = OpConstant %6 5
  161. %21 = OpTypeInt 32 1
  162. %22 = OpTypeInt 32 0
  163. %23 = OpConstant %22 3
  164. %24 = OpTypeArray %21 %23
  165. %25 = OpTypeBool
  166. %26 = OpTypeStruct %24 %25
  167. %29 = OpConstant %21 1
  168. %30 = OpConstant %21 2
  169. %31 = OpConstant %21 3
  170. %33 = OpConstantFalse %25
  171. %35 = OpTypeVector %6 3
  172. %38 = OpConstant %6 6
  173. %39 = OpConstant %6 7
  174. %40 = OpConstant %6 8
  175. %100 = OpConstantComposite %7 %11 %12
  176. %101 = OpConstantComposite %7 %14 %15
  177. %102 = OpConstantComposite %7 %17 %18
  178. %103 = OpConstantComposite %8 %100 %101 %102
  179. %104 = OpConstantComposite %24 %29 %30 %31
  180. %105 = OpConstantComposite %26 %104 %33
  181. %106 = OpConstantComposite %35 %38 %39 %40
  182. %107 = OpConstantComposite %7 %11 %12
  183. %108 = OpConstantComposite %7 %14 %15
  184. %109 = OpConstantComposite %7 %17 %18
  185. %110 = OpConstantComposite %8 %100 %101 %102
  186. %111 = OpConstantComposite %24 %29 %30 %31
  187. %112 = OpConstantComposite %26 %104 %33
  188. %113 = OpConstantComposite %35 %38 %39 %40
  189. %4 = OpFunction %2 None %3
  190. %5 = OpLabel
  191. OpReturn
  192. OpFunctionEnd
  193. )";
  194. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  195. }
  196. TEST(TransformationAddConstantCompositeTest, DisallowBufferBlockDecoration) {
  197. std::string shader = R"(
  198. OpCapability Shader
  199. %1 = OpExtInstImport "GLSL.std.450"
  200. OpMemoryModel Logical GLSL450
  201. OpEntryPoint GLCompute %4 "main"
  202. OpExecutionMode %4 LocalSize 1 1 1
  203. OpSource ESSL 320
  204. OpName %4 "main"
  205. OpName %7 "buf"
  206. OpMemberName %7 0 "a"
  207. OpMemberName %7 1 "b"
  208. OpName %9 ""
  209. OpMemberDecorate %7 0 Offset 0
  210. OpMemberDecorate %7 1 Offset 4
  211. OpDecorate %7 BufferBlock
  212. OpDecorate %9 DescriptorSet 0
  213. OpDecorate %9 Binding 0
  214. %2 = OpTypeVoid
  215. %3 = OpTypeFunction %2
  216. %6 = OpTypeInt 32 1
  217. %10 = OpConstant %6 42
  218. %7 = OpTypeStruct %6 %6
  219. %8 = OpTypePointer Uniform %7
  220. %9 = OpVariable %8 Uniform
  221. %4 = OpFunction %2 None %3
  222. %5 = OpLabel
  223. OpReturn
  224. OpFunctionEnd
  225. )";
  226. const auto env = SPV_ENV_UNIVERSAL_1_0;
  227. const auto consumer = nullptr;
  228. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  229. spvtools::ValidatorOptions validator_options;
  230. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  231. kConsoleMessageConsumer));
  232. TransformationContext transformation_context(
  233. MakeUnique<FactManager>(context.get()), validator_options);
  234. ASSERT_FALSE(TransformationAddConstantComposite(100, 7, {10, 10}, false)
  235. .IsApplicable(context.get(), transformation_context));
  236. }
  237. TEST(TransformationAddConstantCompositeTest, DisallowBlockDecoration) {
  238. std::string shader = R"(
  239. OpCapability Shader
  240. %1 = OpExtInstImport "GLSL.std.450"
  241. OpMemoryModel Logical GLSL450
  242. OpEntryPoint GLCompute %4 "main" %9
  243. OpExecutionMode %4 LocalSize 1 1 1
  244. OpSource ESSL 320
  245. OpName %4 "main"
  246. OpName %7 "buf"
  247. OpMemberName %7 0 "a"
  248. OpMemberName %7 1 "b"
  249. OpName %9 ""
  250. OpMemberDecorate %7 0 Offset 0
  251. OpMemberDecorate %7 1 Offset 4
  252. OpDecorate %7 Block
  253. OpDecorate %9 DescriptorSet 0
  254. OpDecorate %9 Binding 0
  255. %2 = OpTypeVoid
  256. %3 = OpTypeFunction %2
  257. %6 = OpTypeInt 32 1
  258. %10 = OpConstant %6 42
  259. %7 = OpTypeStruct %6 %6
  260. %8 = OpTypePointer StorageBuffer %7
  261. %9 = OpVariable %8 StorageBuffer
  262. %4 = OpFunction %2 None %3
  263. %5 = OpLabel
  264. OpReturn
  265. OpFunctionEnd
  266. )";
  267. const auto env = SPV_ENV_UNIVERSAL_1_5;
  268. const auto consumer = nullptr;
  269. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  270. spvtools::ValidatorOptions validator_options;
  271. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  272. kConsoleMessageConsumer));
  273. TransformationContext transformation_context(
  274. MakeUnique<FactManager>(context.get()), validator_options);
  275. ASSERT_FALSE(TransformationAddConstantComposite(100, 7, {10, 10}, false)
  276. .IsApplicable(context.get(), transformation_context));
  277. }
  278. } // namespace
  279. } // namespace fuzz
  280. } // namespace spvtools