transformation_swap_function_variables_test.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright (c) 2021 Mostafa Ashraf
  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_swap_function_variables.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(TransformationSwapFunctionVariables, NotApplicable) {
  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 320
  30. %2 = OpTypeVoid
  31. %3 = OpTypeFunction %2
  32. %6 = OpTypeInt 32 1
  33. %7 = OpTypePointer Function %6
  34. %8 = OpTypeFloat 32
  35. %9 = OpTypePointer Function %8
  36. %10 = OpTypeVector %8 2
  37. %11 = OpTypePointer Function %10
  38. %12 = OpTypeVector %8 3
  39. %13 = OpTypeMatrix %12 3
  40. %14 = OpTypePointer Function %13
  41. %15 = OpTypeFunction %2 %7 %9 %11 %14 %7 %7
  42. %4 = OpFunction %2 None %3
  43. %5 = OpLabel
  44. %24 = OpVariable %7 Function
  45. %25 = OpVariable %9 Function
  46. %26 = OpVariable %11 Function
  47. %27 = OpVariable %14 Function
  48. %28 = OpVariable %7 Function
  49. %29 = OpVariable %7 Function
  50. %30 = OpVariable %7 Function
  51. %32 = OpVariable %9 Function
  52. %34 = OpVariable %11 Function
  53. %36 = OpVariable %14 Function
  54. %38 = OpVariable %7 Function
  55. %40 = OpVariable %7 Function
  56. %31 = OpLoad %6 %24
  57. OpStore %30 %31
  58. %33 = OpLoad %8 %25
  59. OpStore %32 %33
  60. %35 = OpLoad %10 %26
  61. OpStore %34 %35
  62. %37 = OpLoad %13 %27
  63. OpStore %36 %37
  64. %39 = OpLoad %6 %28
  65. OpStore %38 %39
  66. %41 = OpLoad %6 %29
  67. OpStore %40 %41
  68. %42 = OpFunctionCall %2 %22 %30 %32 %34 %36 %38 %40
  69. OpReturn
  70. OpFunctionEnd
  71. %22 = OpFunction %2 None %15
  72. %16 = OpFunctionParameter %7
  73. %17 = OpFunctionParameter %9
  74. %18 = OpFunctionParameter %11
  75. %19 = OpFunctionParameter %14
  76. %20 = OpFunctionParameter %7
  77. %21 = OpFunctionParameter %7
  78. %23 = OpLabel
  79. OpReturn
  80. OpFunctionEnd
  81. )";
  82. const auto env = SPV_ENV_UNIVERSAL_1_5;
  83. const auto consumer = nullptr;
  84. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  85. spvtools::ValidatorOptions validator_options;
  86. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  87. kConsoleMessageConsumer));
  88. TransformationContext transformation_context(
  89. MakeUnique<FactManager>(context.get()), validator_options);
  90. #ifndef NDEBUG
  91. // Can't swap variable with itself.
  92. ASSERT_DEATH(TransformationSwapFunctionVariables(7, 7).IsApplicable(
  93. context.get(), transformation_context),
  94. "Two results ids are equal");
  95. #endif
  96. // Invalid because 200 is not the id of an instruction.
  97. ASSERT_FALSE(TransformationSwapFunctionVariables(1, 200).IsApplicable(
  98. context.get(), transformation_context));
  99. // Invalid because 5 is not the id of an instruction.
  100. ASSERT_FALSE(TransformationSwapFunctionVariables(5, 24).IsApplicable(
  101. context.get(), transformation_context));
  102. // Can't swap two instructions from two different blocks.
  103. ASSERT_FALSE(TransformationSwapFunctionVariables(16, 26).IsApplicable(
  104. context.get(), transformation_context));
  105. }
  106. TEST(TransformationSwapFunctionVariables, IsApplicable) {
  107. std::string shader = R"(
  108. OpCapability Shader
  109. %1 = OpExtInstImport "GLSL.std.450"
  110. OpMemoryModel Logical GLSL450
  111. OpEntryPoint Fragment %4 "main"
  112. OpExecutionMode %4 OriginUpperLeft
  113. OpSource ESSL 320
  114. %2 = OpTypeVoid
  115. %3 = OpTypeFunction %2
  116. %6 = OpTypeInt 32 1
  117. %7 = OpTypePointer Function %6
  118. %8 = OpTypeFloat 32
  119. %9 = OpTypePointer Function %8
  120. %10 = OpTypeVector %8 2
  121. %11 = OpTypePointer Function %10
  122. %12 = OpTypeVector %8 3
  123. %13 = OpTypeMatrix %12 3
  124. %14 = OpTypePointer Function %13
  125. %15 = OpTypeFunction %2 %7 %9 %11 %14 %7 %7
  126. %4 = OpFunction %2 None %3
  127. %5 = OpLabel
  128. %24 = OpVariable %7 Function
  129. %25 = OpVariable %9 Function
  130. %26 = OpVariable %11 Function
  131. %27 = OpVariable %14 Function
  132. %28 = OpVariable %7 Function
  133. %29 = OpVariable %7 Function
  134. %30 = OpVariable %7 Function
  135. %32 = OpVariable %9 Function
  136. %34 = OpVariable %11 Function
  137. %36 = OpVariable %14 Function
  138. %38 = OpVariable %7 Function
  139. %40 = OpVariable %7 Function
  140. %31 = OpLoad %6 %24
  141. OpStore %30 %31
  142. %33 = OpLoad %8 %25
  143. OpStore %32 %33
  144. %35 = OpLoad %10 %26
  145. OpStore %34 %35
  146. %37 = OpLoad %13 %27
  147. OpStore %36 %37
  148. %39 = OpLoad %6 %28
  149. OpStore %38 %39
  150. %41 = OpLoad %6 %29
  151. OpStore %40 %41
  152. %42 = OpFunctionCall %2 %22 %30 %32 %34 %36 %38 %40
  153. OpReturn
  154. OpFunctionEnd
  155. %22 = OpFunction %2 None %15
  156. %16 = OpFunctionParameter %7
  157. %17 = OpFunctionParameter %9
  158. %18 = OpFunctionParameter %11
  159. %19 = OpFunctionParameter %14
  160. %20 = OpFunctionParameter %7
  161. %21 = OpFunctionParameter %7
  162. %23 = OpLabel
  163. OpReturn
  164. OpFunctionEnd
  165. )";
  166. const auto env = SPV_ENV_UNIVERSAL_1_5;
  167. const auto consumer = nullptr;
  168. // Get Unique pointer of IRContext.
  169. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  170. spvtools::ValidatorOptions validator_options;
  171. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  172. kConsoleMessageConsumer));
  173. TransformationContext transformation_context(
  174. MakeUnique<FactManager>(context.get()), validator_options);
  175. // Successful transformations
  176. {
  177. auto first_instruction = context->get_def_use_mgr()->GetDef(24);
  178. auto second_instruction = context->get_def_use_mgr()->GetDef(28);
  179. // Swap two OpVariable instructions in the same function.
  180. TransformationSwapFunctionVariables transformation(24, 28);
  181. ASSERT_TRUE(
  182. transformation.IsApplicable(context.get(), transformation_context));
  183. ApplyAndCheckFreshIds(transformation, context.get(),
  184. &transformation_context);
  185. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  186. context.get(), validator_options, kConsoleMessageConsumer));
  187. ASSERT_EQ(first_instruction, context->get_def_use_mgr()->GetDef(24));
  188. ASSERT_EQ(second_instruction, context->get_def_use_mgr()->GetDef(28));
  189. }
  190. {
  191. auto first_instruction = context->get_def_use_mgr()->GetDef(38);
  192. auto second_instruction = context->get_def_use_mgr()->GetDef(40);
  193. // Swap two OpVariable instructions in the same function.
  194. TransformationSwapFunctionVariables transformation(38, 40);
  195. ASSERT_TRUE(
  196. transformation.IsApplicable(context.get(), transformation_context));
  197. ApplyAndCheckFreshIds(transformation, context.get(),
  198. &transformation_context);
  199. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  200. context.get(), validator_options, kConsoleMessageConsumer));
  201. ASSERT_EQ(first_instruction, context->get_def_use_mgr()->GetDef(38));
  202. ASSERT_EQ(second_instruction, context->get_def_use_mgr()->GetDef(40));
  203. }
  204. std::string after_transformation = R"(
  205. OpCapability Shader
  206. %1 = OpExtInstImport "GLSL.std.450"
  207. OpMemoryModel Logical GLSL450
  208. OpEntryPoint Fragment %4 "main"
  209. OpExecutionMode %4 OriginUpperLeft
  210. OpSource ESSL 320
  211. %2 = OpTypeVoid
  212. %3 = OpTypeFunction %2
  213. %6 = OpTypeInt 32 1
  214. %7 = OpTypePointer Function %6
  215. %8 = OpTypeFloat 32
  216. %9 = OpTypePointer Function %8
  217. %10 = OpTypeVector %8 2
  218. %11 = OpTypePointer Function %10
  219. %12 = OpTypeVector %8 3
  220. %13 = OpTypeMatrix %12 3
  221. %14 = OpTypePointer Function %13
  222. %15 = OpTypeFunction %2 %7 %9 %11 %14 %7 %7
  223. %4 = OpFunction %2 None %3
  224. %5 = OpLabel
  225. %28 = OpVariable %7 Function
  226. %25 = OpVariable %9 Function
  227. %26 = OpVariable %11 Function
  228. %27 = OpVariable %14 Function
  229. %24 = OpVariable %7 Function
  230. %29 = OpVariable %7 Function
  231. %30 = OpVariable %7 Function
  232. %32 = OpVariable %9 Function
  233. %34 = OpVariable %11 Function
  234. %36 = OpVariable %14 Function
  235. %40 = OpVariable %7 Function
  236. %38 = OpVariable %7 Function
  237. %31 = OpLoad %6 %24
  238. OpStore %30 %31
  239. %33 = OpLoad %8 %25
  240. OpStore %32 %33
  241. %35 = OpLoad %10 %26
  242. OpStore %34 %35
  243. %37 = OpLoad %13 %27
  244. OpStore %36 %37
  245. %39 = OpLoad %6 %28
  246. OpStore %38 %39
  247. %41 = OpLoad %6 %29
  248. OpStore %40 %41
  249. %42 = OpFunctionCall %2 %22 %30 %32 %34 %36 %38 %40
  250. OpReturn
  251. OpFunctionEnd
  252. %22 = OpFunction %2 None %15
  253. %16 = OpFunctionParameter %7
  254. %17 = OpFunctionParameter %9
  255. %18 = OpFunctionParameter %11
  256. %19 = OpFunctionParameter %14
  257. %20 = OpFunctionParameter %7
  258. %21 = OpFunctionParameter %7
  259. %23 = OpLabel
  260. OpReturn
  261. OpFunctionEnd
  262. )";
  263. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  264. }
  265. } // namespace
  266. } // namespace fuzz
  267. } // namespace spvtools