fuzzer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/fuzzer.h"
  15. #include <cassert>
  16. #include <memory>
  17. #include <sstream>
  18. #include "fuzzer_pass_adjust_memory_operands_masks.h"
  19. #include "source/fuzz/fact_manager.h"
  20. #include "source/fuzz/fuzzer_context.h"
  21. #include "source/fuzz/fuzzer_pass_add_dead_breaks.h"
  22. #include "source/fuzz/fuzzer_pass_add_dead_continues.h"
  23. #include "source/fuzz/fuzzer_pass_add_no_contraction_decorations.h"
  24. #include "source/fuzz/fuzzer_pass_add_useful_constructs.h"
  25. #include "source/fuzz/fuzzer_pass_adjust_function_controls.h"
  26. #include "source/fuzz/fuzzer_pass_adjust_loop_controls.h"
  27. #include "source/fuzz/fuzzer_pass_adjust_selection_controls.h"
  28. #include "source/fuzz/fuzzer_pass_apply_id_synonyms.h"
  29. #include "source/fuzz/fuzzer_pass_construct_composites.h"
  30. #include "source/fuzz/fuzzer_pass_copy_objects.h"
  31. #include "source/fuzz/fuzzer_pass_obfuscate_constants.h"
  32. #include "source/fuzz/fuzzer_pass_permute_blocks.h"
  33. #include "source/fuzz/fuzzer_pass_split_blocks.h"
  34. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  35. #include "source/fuzz/pseudo_random_generator.h"
  36. #include "source/opt/build_module.h"
  37. #include "source/spirv_fuzzer_options.h"
  38. #include "source/util/make_unique.h"
  39. namespace spvtools {
  40. namespace fuzz {
  41. namespace {
  42. const uint32_t kIdBoundGap = 100;
  43. const uint32_t kTransformationLimit = 500;
  44. const uint32_t kChanceOfApplyingAnotherPass = 85;
  45. template <typename T>
  46. void MaybeAddPass(
  47. std::vector<std::unique_ptr<FuzzerPass>>* passes,
  48. opt::IRContext* ir_context, FactManager* fact_manager,
  49. FuzzerContext* fuzzer_context,
  50. protobufs::TransformationSequence* transformation_sequence_out) {
  51. if (fuzzer_context->ChooseEven()) {
  52. passes->push_back(MakeUnique<T>(ir_context, fact_manager, fuzzer_context,
  53. transformation_sequence_out));
  54. }
  55. }
  56. } // namespace
  57. struct Fuzzer::Impl {
  58. explicit Impl(spv_target_env env, uint32_t random_seed,
  59. bool validate_after_each_pass)
  60. : target_env(env),
  61. seed(random_seed),
  62. validate_after_each_fuzzer_pass(validate_after_each_pass) {}
  63. bool ApplyPassAndCheckValidity(FuzzerPass* pass,
  64. const opt::IRContext& ir_context,
  65. const spvtools::SpirvTools& tools) const;
  66. const spv_target_env target_env; // Target environment.
  67. const uint32_t seed; // Seed for random number generator.
  68. bool validate_after_each_fuzzer_pass; // Determines whether the validator
  69. // should be invoked after every fuzzer pass.
  70. MessageConsumer consumer; // Message consumer.
  71. };
  72. Fuzzer::Fuzzer(spv_target_env env, uint32_t seed,
  73. bool validate_after_each_fuzzer_pass)
  74. : impl_(MakeUnique<Impl>(env, seed, validate_after_each_fuzzer_pass)) {}
  75. Fuzzer::~Fuzzer() = default;
  76. void Fuzzer::SetMessageConsumer(MessageConsumer c) {
  77. impl_->consumer = std::move(c);
  78. }
  79. bool Fuzzer::Impl::ApplyPassAndCheckValidity(
  80. FuzzerPass* pass, const opt::IRContext& ir_context,
  81. const spvtools::SpirvTools& tools) const {
  82. pass->Apply();
  83. if (validate_after_each_fuzzer_pass) {
  84. std::vector<uint32_t> binary_to_validate;
  85. ir_context.module()->ToBinary(&binary_to_validate, false);
  86. if (!tools.Validate(&binary_to_validate[0], binary_to_validate.size())) {
  87. consumer(SPV_MSG_INFO, nullptr, {},
  88. "Binary became invalid during fuzzing (set a breakpoint to "
  89. "inspect); stopping.");
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. Fuzzer::FuzzerResultStatus Fuzzer::Run(
  96. const std::vector<uint32_t>& binary_in,
  97. const protobufs::FactSequence& initial_facts,
  98. std::vector<uint32_t>* binary_out,
  99. protobufs::TransformationSequence* transformation_sequence_out) const {
  100. // Check compatibility between the library version being linked with and the
  101. // header files being used.
  102. GOOGLE_PROTOBUF_VERIFY_VERSION;
  103. spvtools::SpirvTools tools(impl_->target_env);
  104. tools.SetMessageConsumer(impl_->consumer);
  105. if (!tools.IsValid()) {
  106. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  107. "Failed to create SPIRV-Tools interface; stopping.");
  108. return Fuzzer::FuzzerResultStatus::kFailedToCreateSpirvToolsInterface;
  109. }
  110. // Initial binary should be valid.
  111. if (!tools.Validate(&binary_in[0], binary_in.size())) {
  112. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  113. "Initial binary is invalid; stopping.");
  114. return Fuzzer::FuzzerResultStatus::kInitialBinaryInvalid;
  115. }
  116. // Build the module from the input binary.
  117. std::unique_ptr<opt::IRContext> ir_context = BuildModule(
  118. impl_->target_env, impl_->consumer, binary_in.data(), binary_in.size());
  119. assert(ir_context);
  120. // Make a PRNG from the seed passed to the fuzzer on creation.
  121. PseudoRandomGenerator random_generator(impl_->seed);
  122. // The fuzzer will introduce new ids into the module. The module's id bound
  123. // gives the smallest id that can be used for this purpose. We add an offset
  124. // to this so that there is a sizeable gap between the ids used in the
  125. // original module and the ids used for fuzzing, as a readability aid.
  126. //
  127. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2541) consider the
  128. // case where the maximum id bound is reached.
  129. auto minimum_fresh_id = ir_context->module()->id_bound() + kIdBoundGap;
  130. FuzzerContext fuzzer_context(&random_generator, minimum_fresh_id);
  131. FactManager fact_manager;
  132. fact_manager.AddFacts(impl_->consumer, initial_facts, ir_context.get());
  133. // Add some essential ingredients to the module if they are not already
  134. // present, such as boolean constants.
  135. FuzzerPassAddUsefulConstructs add_useful_constructs(
  136. ir_context.get(), &fact_manager, &fuzzer_context,
  137. transformation_sequence_out);
  138. if (!impl_->ApplyPassAndCheckValidity(&add_useful_constructs, *ir_context,
  139. tools)) {
  140. return Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule;
  141. }
  142. // Apply some semantics-preserving passes.
  143. std::vector<std::unique_ptr<FuzzerPass>> passes;
  144. while (passes.empty()) {
  145. MaybeAddPass<FuzzerPassAddDeadBreaks>(&passes, ir_context.get(),
  146. &fact_manager, &fuzzer_context,
  147. transformation_sequence_out);
  148. MaybeAddPass<FuzzerPassAddDeadContinues>(&passes, ir_context.get(),
  149. &fact_manager, &fuzzer_context,
  150. transformation_sequence_out);
  151. MaybeAddPass<FuzzerPassApplyIdSynonyms>(&passes, ir_context.get(),
  152. &fact_manager, &fuzzer_context,
  153. transformation_sequence_out);
  154. MaybeAddPass<FuzzerPassConstructComposites>(&passes, ir_context.get(),
  155. &fact_manager, &fuzzer_context,
  156. transformation_sequence_out);
  157. MaybeAddPass<FuzzerPassCopyObjects>(&passes, ir_context.get(),
  158. &fact_manager, &fuzzer_context,
  159. transformation_sequence_out);
  160. MaybeAddPass<FuzzerPassObfuscateConstants>(&passes, ir_context.get(),
  161. &fact_manager, &fuzzer_context,
  162. transformation_sequence_out);
  163. MaybeAddPass<FuzzerPassPermuteBlocks>(&passes, ir_context.get(),
  164. &fact_manager, &fuzzer_context,
  165. transformation_sequence_out);
  166. MaybeAddPass<FuzzerPassSplitBlocks>(&passes, ir_context.get(),
  167. &fact_manager, &fuzzer_context,
  168. transformation_sequence_out);
  169. }
  170. bool is_first = true;
  171. while (static_cast<uint32_t>(
  172. transformation_sequence_out->transformation_size()) <
  173. kTransformationLimit &&
  174. (is_first ||
  175. fuzzer_context.ChoosePercentage(kChanceOfApplyingAnotherPass))) {
  176. is_first = false;
  177. if (!impl_->ApplyPassAndCheckValidity(
  178. passes[fuzzer_context.RandomIndex(passes)].get(), *ir_context,
  179. tools)) {
  180. return Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule;
  181. }
  182. }
  183. // Now apply some passes that it does not make sense to apply repeatedly,
  184. // as they do not unlock other passes.
  185. std::vector<std::unique_ptr<FuzzerPass>> final_passes;
  186. MaybeAddPass<FuzzerPassAdjustFunctionControls>(
  187. &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
  188. transformation_sequence_out);
  189. MaybeAddPass<FuzzerPassAdjustLoopControls>(&final_passes, ir_context.get(),
  190. &fact_manager, &fuzzer_context,
  191. transformation_sequence_out);
  192. MaybeAddPass<FuzzerPassAdjustMemoryOperandsMasks>(
  193. &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
  194. transformation_sequence_out);
  195. MaybeAddPass<FuzzerPassAdjustSelectionControls>(
  196. &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
  197. transformation_sequence_out);
  198. MaybeAddPass<FuzzerPassAddNoContractionDecorations>(
  199. &final_passes, ir_context.get(), &fact_manager, &fuzzer_context,
  200. transformation_sequence_out);
  201. for (auto& pass : final_passes) {
  202. if (!impl_->ApplyPassAndCheckValidity(pass.get(), *ir_context, tools)) {
  203. return Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule;
  204. }
  205. }
  206. // Encode the module as a binary.
  207. ir_context->module()->ToBinary(binary_out, false);
  208. return Fuzzer::FuzzerResultStatus::kComplete;
  209. }
  210. } // namespace fuzz
  211. } // namespace spvtools