fuzzer_pass_add_dead_blocks.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <algorithm>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/transformation_add_dead_block.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. namespace {
  21. const size_t kMaxTransformationsInOnePass = 100U;
  22. } // namespace
  23. FuzzerPassAddDeadBlocks::FuzzerPassAddDeadBlocks(
  24. opt::IRContext* ir_context, TransformationContext* transformation_context,
  25. FuzzerContext* fuzzer_context,
  26. protobufs::TransformationSequence* transformations,
  27. bool ignore_inapplicable_transformations)
  28. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  29. transformations, ignore_inapplicable_transformations) {}
  30. void FuzzerPassAddDeadBlocks::Apply() {
  31. // We iterate over all blocks in the module collecting up those at which we
  32. // might add a branch to a new dead block. We then loop over all such
  33. // candidates and actually apply transformations. This separation is to
  34. // avoid modifying the module as we traverse it.
  35. std::vector<TransformationAddDeadBlock> candidate_transformations;
  36. for (auto& function : *GetIRContext()->module()) {
  37. for (auto& block : function) {
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()->GetChanceOfAddingDeadBlock())) {
  40. continue;
  41. }
  42. // Make sure the module contains a boolean constant equal to
  43. // |condition_value|.
  44. bool condition_value = GetFuzzerContext()->ChooseEven();
  45. FindOrCreateBoolConstant(condition_value, false);
  46. // We speculatively create a transformation, and then apply it (below) if
  47. // it turns out to be applicable. This avoids duplicating the logic for
  48. // applicability checking.
  49. //
  50. // It means that fresh ids for transformations that turn out not to be
  51. // applicable end up being unused.
  52. candidate_transformations.emplace_back(TransformationAddDeadBlock(
  53. GetFuzzerContext()->GetFreshId(), block.id(), condition_value));
  54. }
  55. }
  56. // Applying transformations can be expensive as each transformation requires
  57. // dominator information and also invalidates dominator information. We thus
  58. // limit the number of transformations that one application of this fuzzer
  59. // pass can apply. We choose to do this after identifying all the
  60. // transformations that we *might* want to apply, rather than breaking the
  61. // above loops once the limit is reached, to avoid biasing towards
  62. // transformations that target early parts of the module.
  63. GetFuzzerContext()->Shuffle(&candidate_transformations);
  64. for (size_t i = 0; i < std::min(kMaxTransformationsInOnePass,
  65. candidate_transformations.size());
  66. i++) {
  67. MaybeApplyTransformation(candidate_transformations[i]);
  68. }
  69. }
  70. } // namespace fuzz
  71. } // namespace spvtools