fuzzer_pass_add_dead_blocks.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_pass_add_dead_blocks.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_add_dead_block.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassAddDeadBlocks::FuzzerPassAddDeadBlocks(
  20. opt::IRContext* ir_context, TransformationContext* transformation_context,
  21. FuzzerContext* fuzzer_context,
  22. protobufs::TransformationSequence* transformations)
  23. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  24. transformations) {}
  25. FuzzerPassAddDeadBlocks::~FuzzerPassAddDeadBlocks() = default;
  26. void FuzzerPassAddDeadBlocks::Apply() {
  27. // We iterate over all blocks in the module collecting up those at which we
  28. // might add a branch to a new dead block. We then loop over all such
  29. // candidates and actually apply transformations. This separation is to
  30. // avoid modifying the module as we traverse it.
  31. std::vector<TransformationAddDeadBlock> candidate_transformations;
  32. for (auto& function : *GetIRContext()->module()) {
  33. for (auto& block : function) {
  34. if (!GetFuzzerContext()->ChoosePercentage(
  35. GetFuzzerContext()->GetChanceOfAddingDeadBlock())) {
  36. continue;
  37. }
  38. // Make sure the module contains a boolean constant equal to
  39. // |condition_value|.
  40. bool condition_value = GetFuzzerContext()->ChooseEven();
  41. FindOrCreateBoolConstant(condition_value);
  42. // We speculatively create a transformation, and then apply it (below) if
  43. // it turns out to be applicable. This avoids duplicating the logic for
  44. // applicability checking.
  45. //
  46. // It means that fresh ids for transformations that turn out not to be
  47. // applicable end up being unused.
  48. candidate_transformations.emplace_back(TransformationAddDeadBlock(
  49. GetFuzzerContext()->GetFreshId(), block.id(), condition_value));
  50. }
  51. }
  52. // Apply all those transformations that are in fact applicable.
  53. for (auto& transformation : candidate_transformations) {
  54. if (transformation.IsApplicable(GetIRContext(),
  55. *GetTransformationContext())) {
  56. transformation.Apply(GetIRContext(), GetTransformationContext());
  57. *GetTransformations()->add_transformation() = transformation.ToMessage();
  58. }
  59. }
  60. }
  61. } // namespace fuzz
  62. } // namespace spvtools