fuzzer_pass_propagate_instructions_down.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2020 Vasyl Teliman
  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_propagate_instructions_down.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/transformation_propagate_instruction_down.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassPropagateInstructionsDown::FuzzerPassPropagateInstructionsDown(
  20. opt::IRContext* ir_context, TransformationContext* transformation_context,
  21. FuzzerContext* fuzzer_context,
  22. protobufs::TransformationSequence* transformations,
  23. bool ignore_inapplicable_transformations)
  24. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  25. transformations, ignore_inapplicable_transformations) {}
  26. void FuzzerPassPropagateInstructionsDown::Apply() {
  27. for (const auto& function : *GetIRContext()->module()) {
  28. std::vector<const opt::BasicBlock*> reachable_blocks;
  29. for (const auto& block : function) {
  30. if (GetIRContext()->IsReachable(block)) {
  31. reachable_blocks.push_back(&block);
  32. }
  33. }
  34. for (const auto* block : reachable_blocks) {
  35. if (!GetFuzzerContext()->ChoosePercentage(
  36. GetFuzzerContext()->GetChanceOfPropagatingInstructionsDown())) {
  37. continue;
  38. }
  39. if (TransformationPropagateInstructionDown::IsApplicableToBlock(
  40. GetIRContext(), block->id())) {
  41. // Record fresh ids for every successor of the |block| that we can
  42. // propagate an instruction into.
  43. std::map<uint32_t, uint32_t> fresh_ids;
  44. for (auto id :
  45. TransformationPropagateInstructionDown::GetAcceptableSuccessors(
  46. GetIRContext(), block->id())) {
  47. fresh_ids[id] = GetFuzzerContext()->GetFreshId();
  48. }
  49. ApplyTransformation(TransformationPropagateInstructionDown(
  50. block->id(), GetFuzzerContext()->GetFreshId(), fresh_ids));
  51. }
  52. }
  53. }
  54. }
  55. } // namespace fuzz
  56. } // namespace spvtools