fuzzer_pass_construct_composites_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright (c) 2020 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/fuzzer_pass_construct_composites.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/pseudo_random_generator.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. TEST(FuzzerPassConstructCompositesTest, IsomorphicStructs) {
  23. // This test declares various isomorphic structs, and a struct that is made up
  24. // of these isomorphic structs. The pass to construct composites is then
  25. // applied several times to check that no issues arise related to using a
  26. // value of one struct type when a value of an isomorphic struct type is
  27. // required.
  28. std::string shader = R"(
  29. OpCapability Shader
  30. %1 = OpExtInstImport "GLSL.std.450"
  31. OpMemoryModel Logical GLSL450
  32. OpEntryPoint Fragment %4 "main"
  33. OpExecutionMode %4 OriginUpperLeft
  34. OpSource ESSL 310
  35. %2 = OpTypeVoid
  36. %3 = OpTypeFunction %2
  37. %6 = OpTypeFloat 32
  38. %7 = OpConstant %6 0
  39. %8 = OpTypeStruct %6 %6 %6
  40. %9 = OpTypeStruct %6 %6 %6
  41. %10 = OpTypeStruct %6 %6 %6
  42. %11 = OpTypeStruct %6 %6 %6
  43. %12 = OpTypeStruct %6 %6 %6
  44. %13 = OpTypeStruct %8 %9 %10 %11 %12
  45. %14 = OpConstantComposite %8 %7 %7 %7
  46. %15 = OpConstantComposite %9 %7 %7 %7
  47. %16 = OpConstantComposite %10 %7 %7 %7
  48. %17 = OpConstantComposite %11 %7 %7 %7
  49. %18 = OpConstantComposite %12 %7 %7 %7
  50. %4 = OpFunction %2 None %3
  51. %5 = OpLabel
  52. OpNop
  53. OpNop
  54. OpNop
  55. OpNop
  56. OpNop
  57. OpNop
  58. OpNop
  59. OpNop
  60. OpNop
  61. OpNop
  62. OpNop
  63. OpNop
  64. OpNop
  65. OpNop
  66. OpNop
  67. OpNop
  68. OpReturn
  69. OpFunctionEnd
  70. )";
  71. const auto env = SPV_ENV_UNIVERSAL_1_3;
  72. const auto consumer = nullptr;
  73. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  74. false);
  75. for (uint32_t i = 0; i < 10; i++) {
  76. const auto context =
  77. BuildModule(env, consumer, shader, kFuzzAssembleOption);
  78. spvtools::ValidatorOptions validator_options;
  79. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  80. context.get(), validator_options, kConsoleMessageConsumer));
  81. TransformationContext transformation_context(
  82. MakeUnique<FactManager>(context.get()), validator_options);
  83. protobufs::TransformationSequence transformation_sequence;
  84. FuzzerPassConstructComposites fuzzer_pass(
  85. context.get(), &transformation_context, &fuzzer_context,
  86. &transformation_sequence, false);
  87. fuzzer_pass.Apply();
  88. // We just check that the result is valid.
  89. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  90. context.get(), validator_options, kConsoleMessageConsumer));
  91. }
  92. }
  93. TEST(FuzzerPassConstructCompositesTest, IsomorphicArrays) {
  94. // This test declares various isomorphic arrays, and a struct that is made up
  95. // of these isomorphic arrays. The pass to construct composites is then
  96. // applied several times to check that no issues arise related to using a
  97. // value of one array type when a value of an isomorphic array type is
  98. // required.
  99. std::string shader = R"(
  100. OpCapability Shader
  101. %1 = OpExtInstImport "GLSL.std.450"
  102. OpMemoryModel Logical GLSL450
  103. OpEntryPoint Fragment %4 "main"
  104. OpExecutionMode %4 OriginUpperLeft
  105. OpSource ESSL 310
  106. %2 = OpTypeVoid
  107. %3 = OpTypeFunction %2
  108. %6 = OpTypeFloat 32
  109. %50 = OpTypeInt 32 0
  110. %51 = OpConstant %50 3
  111. %7 = OpConstant %6 0
  112. %8 = OpTypeArray %6 %51
  113. %9 = OpTypeArray %6 %51
  114. %10 = OpTypeArray %6 %51
  115. %11 = OpTypeArray %6 %51
  116. %12 = OpTypeArray %6 %51
  117. %13 = OpTypeStruct %8 %9 %10 %11 %12
  118. %14 = OpConstantComposite %8 %7 %7 %7
  119. %15 = OpConstantComposite %9 %7 %7 %7
  120. %16 = OpConstantComposite %10 %7 %7 %7
  121. %17 = OpConstantComposite %11 %7 %7 %7
  122. %18 = OpConstantComposite %12 %7 %7 %7
  123. %4 = OpFunction %2 None %3
  124. %5 = OpLabel
  125. OpNop
  126. OpNop
  127. OpNop
  128. OpNop
  129. OpNop
  130. OpNop
  131. OpNop
  132. OpNop
  133. OpNop
  134. OpNop
  135. OpNop
  136. OpNop
  137. OpNop
  138. OpNop
  139. OpNop
  140. OpNop
  141. OpReturn
  142. OpFunctionEnd
  143. )";
  144. const auto env = SPV_ENV_UNIVERSAL_1_3;
  145. const auto consumer = nullptr;
  146. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  147. false);
  148. for (uint32_t i = 0; i < 10; i++) {
  149. const auto context =
  150. BuildModule(env, consumer, shader, kFuzzAssembleOption);
  151. spvtools::ValidatorOptions validator_options;
  152. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  153. context.get(), validator_options, kConsoleMessageConsumer));
  154. TransformationContext transformation_context(
  155. MakeUnique<FactManager>(context.get()), validator_options);
  156. protobufs::TransformationSequence transformation_sequence;
  157. FuzzerPassConstructComposites fuzzer_pass(
  158. context.get(), &transformation_context, &fuzzer_context,
  159. &transformation_sequence, false);
  160. fuzzer_pass.Apply();
  161. // We just check that the result is valid.
  162. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  163. context.get(), validator_options, kConsoleMessageConsumer));
  164. }
  165. }
  166. } // namespace
  167. } // namespace fuzz
  168. } // namespace spvtools