transformation_load_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_load.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. TEST(TransformationLoadTest, BasicTest) {
  23. std::string shader = R"(
  24. OpCapability Shader
  25. %1 = OpExtInstImport "GLSL.std.450"
  26. OpMemoryModel Logical GLSL450
  27. OpEntryPoint Fragment %4 "main"
  28. OpExecutionMode %4 OriginUpperLeft
  29. OpSource ESSL 310
  30. %2 = OpTypeVoid
  31. %3 = OpTypeFunction %2
  32. %6 = OpTypeInt 32 1
  33. %7 = OpTypeFloat 32
  34. %8 = OpTypeStruct %6 %7
  35. %9 = OpTypePointer Function %8
  36. %10 = OpTypeFunction %6 %9
  37. %14 = OpConstant %6 0
  38. %15 = OpTypePointer Function %6
  39. %51 = OpTypePointer Private %6
  40. %21 = OpConstant %6 2
  41. %23 = OpConstant %6 1
  42. %24 = OpConstant %7 1
  43. %25 = OpTypePointer Function %7
  44. %50 = OpTypePointer Private %7
  45. %34 = OpTypeBool
  46. %35 = OpConstantFalse %34
  47. %60 = OpConstantNull %50
  48. %61 = OpUndef %51
  49. %52 = OpVariable %50 Private
  50. %53 = OpVariable %51 Private
  51. %4 = OpFunction %2 None %3
  52. %5 = OpLabel
  53. %20 = OpVariable %9 Function
  54. %27 = OpVariable %9 Function ; irrelevant
  55. %22 = OpAccessChain %15 %20 %14
  56. %44 = OpCopyObject %9 %20
  57. %26 = OpAccessChain %25 %20 %23
  58. %29 = OpFunctionCall %6 %12 %27
  59. %30 = OpAccessChain %15 %20 %14
  60. %45 = OpCopyObject %15 %30
  61. %33 = OpAccessChain %15 %20 %14
  62. OpSelectionMerge %37 None
  63. OpBranchConditional %35 %36 %37
  64. %36 = OpLabel
  65. %38 = OpAccessChain %15 %20 %14
  66. %40 = OpAccessChain %15 %20 %14
  67. %43 = OpAccessChain %15 %20 %14
  68. OpBranch %37
  69. %37 = OpLabel
  70. OpReturn
  71. OpFunctionEnd
  72. %12 = OpFunction %6 None %10
  73. %11 = OpFunctionParameter %9 ; irrelevant
  74. %13 = OpLabel
  75. %46 = OpCopyObject %9 %11 ; irrelevant
  76. %16 = OpAccessChain %15 %11 %14 ; irrelevant
  77. OpReturnValue %21
  78. OpFunctionEnd
  79. )";
  80. const auto env = SPV_ENV_UNIVERSAL_1_4;
  81. const auto consumer = nullptr;
  82. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  83. spvtools::ValidatorOptions validator_options;
  84. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  85. kConsoleMessageConsumer));
  86. TransformationContext transformation_context(
  87. MakeUnique<FactManager>(context.get()), validator_options);
  88. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  89. 27);
  90. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  91. 11);
  92. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  93. 46);
  94. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  95. 16);
  96. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  97. 52);
  98. transformation_context.GetFactManager()->AddFactBlockIsDead(36);
  99. // Variables with pointee types:
  100. // 52 - ptr_to(7)
  101. // 53 - ptr_to(6)
  102. // 20 - ptr_to(8)
  103. // 27 - ptr_to(8) - irrelevant
  104. // Access chains with pointee type:
  105. // 22 - ptr_to(6)
  106. // 26 - ptr_to(6)
  107. // 30 - ptr_to(6)
  108. // 33 - ptr_to(6)
  109. // 38 - ptr_to(6)
  110. // 40 - ptr_to(6)
  111. // 43 - ptr_to(6)
  112. // 16 - ptr_to(6) - irrelevant
  113. // Copied object with pointee type:
  114. // 44 - ptr_to(8)
  115. // 45 - ptr_to(6)
  116. // 46 - ptr_to(8) - irrelevant
  117. // Function parameters with pointee type:
  118. // 11 - ptr_to(8) - irrelevant
  119. // Pointers that cannot be used:
  120. // 60 - null
  121. // 61 - undefined
  122. // Bad: id is not fresh
  123. ASSERT_FALSE(TransformationLoad(
  124. 33, 33, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  125. .IsApplicable(context.get(), transformation_context));
  126. // Bad: attempt to load from 11 from outside its function
  127. ASSERT_FALSE(TransformationLoad(
  128. 100, 11, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  129. .IsApplicable(context.get(), transformation_context));
  130. // Bad: pointer is not available
  131. ASSERT_FALSE(TransformationLoad(
  132. 100, 33, MakeInstructionDescriptor(45, SpvOpCopyObject, 0))
  133. .IsApplicable(context.get(), transformation_context));
  134. // Bad: attempt to insert before OpVariable
  135. ASSERT_FALSE(TransformationLoad(
  136. 100, 27, MakeInstructionDescriptor(27, SpvOpVariable, 0))
  137. .IsApplicable(context.get(), transformation_context));
  138. // Bad: pointer id does not exist
  139. ASSERT_FALSE(
  140. TransformationLoad(100, 1000,
  141. MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  142. .IsApplicable(context.get(), transformation_context));
  143. // Bad: pointer id exists but does not have a type
  144. ASSERT_FALSE(TransformationLoad(
  145. 100, 5, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  146. .IsApplicable(context.get(), transformation_context));
  147. // Bad: pointer id exists and has a type, but is not a pointer
  148. ASSERT_FALSE(TransformationLoad(
  149. 100, 24, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  150. .IsApplicable(context.get(), transformation_context));
  151. // Bad: attempt to load from null pointer
  152. ASSERT_FALSE(TransformationLoad(
  153. 100, 60, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  154. .IsApplicable(context.get(), transformation_context));
  155. // Bad: attempt to load from undefined pointer
  156. ASSERT_FALSE(TransformationLoad(
  157. 100, 61, MakeInstructionDescriptor(38, SpvOpAccessChain, 0))
  158. .IsApplicable(context.get(), transformation_context));
  159. // Bad: %40 is not available at the program point
  160. ASSERT_FALSE(
  161. TransformationLoad(100, 40, MakeInstructionDescriptor(37, SpvOpReturn, 0))
  162. .IsApplicable(context.get(), transformation_context));
  163. // Bad: The described instruction does not exist
  164. ASSERT_FALSE(TransformationLoad(
  165. 100, 33, MakeInstructionDescriptor(1000, SpvOpReturn, 0))
  166. .IsApplicable(context.get(), transformation_context));
  167. {
  168. TransformationLoad transformation(
  169. 100, 33, MakeInstructionDescriptor(38, SpvOpAccessChain, 0));
  170. ASSERT_TRUE(
  171. transformation.IsApplicable(context.get(), transformation_context));
  172. ApplyAndCheckFreshIds(transformation, context.get(),
  173. &transformation_context);
  174. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  175. context.get(), validator_options, kConsoleMessageConsumer));
  176. }
  177. {
  178. TransformationLoad transformation(
  179. 101, 46, MakeInstructionDescriptor(16, SpvOpReturnValue, 0));
  180. ASSERT_TRUE(
  181. transformation.IsApplicable(context.get(), transformation_context));
  182. ApplyAndCheckFreshIds(transformation, context.get(),
  183. &transformation_context);
  184. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  185. context.get(), validator_options, kConsoleMessageConsumer));
  186. }
  187. {
  188. TransformationLoad transformation(
  189. 102, 16, MakeInstructionDescriptor(16, SpvOpReturnValue, 0));
  190. ASSERT_TRUE(
  191. transformation.IsApplicable(context.get(), transformation_context));
  192. ApplyAndCheckFreshIds(transformation, context.get(),
  193. &transformation_context);
  194. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  195. context.get(), validator_options, kConsoleMessageConsumer));
  196. }
  197. {
  198. TransformationLoad transformation(
  199. 103, 40, MakeInstructionDescriptor(43, SpvOpAccessChain, 0));
  200. ASSERT_TRUE(
  201. transformation.IsApplicable(context.get(), transformation_context));
  202. ApplyAndCheckFreshIds(transformation, context.get(),
  203. &transformation_context);
  204. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  205. context.get(), validator_options, kConsoleMessageConsumer));
  206. }
  207. std::string after_transformation = R"(
  208. OpCapability Shader
  209. %1 = OpExtInstImport "GLSL.std.450"
  210. OpMemoryModel Logical GLSL450
  211. OpEntryPoint Fragment %4 "main"
  212. OpExecutionMode %4 OriginUpperLeft
  213. OpSource ESSL 310
  214. %2 = OpTypeVoid
  215. %3 = OpTypeFunction %2
  216. %6 = OpTypeInt 32 1
  217. %7 = OpTypeFloat 32
  218. %8 = OpTypeStruct %6 %7
  219. %9 = OpTypePointer Function %8
  220. %10 = OpTypeFunction %6 %9
  221. %14 = OpConstant %6 0
  222. %15 = OpTypePointer Function %6
  223. %51 = OpTypePointer Private %6
  224. %21 = OpConstant %6 2
  225. %23 = OpConstant %6 1
  226. %24 = OpConstant %7 1
  227. %25 = OpTypePointer Function %7
  228. %50 = OpTypePointer Private %7
  229. %34 = OpTypeBool
  230. %35 = OpConstantFalse %34
  231. %60 = OpConstantNull %50
  232. %61 = OpUndef %51
  233. %52 = OpVariable %50 Private
  234. %53 = OpVariable %51 Private
  235. %4 = OpFunction %2 None %3
  236. %5 = OpLabel
  237. %20 = OpVariable %9 Function
  238. %27 = OpVariable %9 Function ; irrelevant
  239. %22 = OpAccessChain %15 %20 %14
  240. %44 = OpCopyObject %9 %20
  241. %26 = OpAccessChain %25 %20 %23
  242. %29 = OpFunctionCall %6 %12 %27
  243. %30 = OpAccessChain %15 %20 %14
  244. %45 = OpCopyObject %15 %30
  245. %33 = OpAccessChain %15 %20 %14
  246. OpSelectionMerge %37 None
  247. OpBranchConditional %35 %36 %37
  248. %36 = OpLabel
  249. %100 = OpLoad %6 %33
  250. %38 = OpAccessChain %15 %20 %14
  251. %40 = OpAccessChain %15 %20 %14
  252. %103 = OpLoad %6 %40
  253. %43 = OpAccessChain %15 %20 %14
  254. OpBranch %37
  255. %37 = OpLabel
  256. OpReturn
  257. OpFunctionEnd
  258. %12 = OpFunction %6 None %10
  259. %11 = OpFunctionParameter %9 ; irrelevant
  260. %13 = OpLabel
  261. %46 = OpCopyObject %9 %11 ; irrelevant
  262. %16 = OpAccessChain %15 %11 %14 ; irrelevant
  263. %101 = OpLoad %8 %46
  264. %102 = OpLoad %6 %16
  265. OpReturnValue %21
  266. OpFunctionEnd
  267. )";
  268. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  269. }
  270. } // namespace
  271. } // namespace fuzz
  272. } // namespace spvtools