fuzzer_pass_adjust_selection_controls.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_adjust_selection_controls.h"
  15. #include "source/fuzz/transformation_set_selection_control.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. FuzzerPassAdjustSelectionControls::FuzzerPassAdjustSelectionControls(
  19. opt::IRContext* ir_context, TransformationContext* transformation_context,
  20. FuzzerContext* fuzzer_context,
  21. protobufs::TransformationSequence* transformations,
  22. bool ignore_inapplicable_transformations)
  23. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  24. transformations, ignore_inapplicable_transformations) {}
  25. void FuzzerPassAdjustSelectionControls::Apply() {
  26. // Consider every merge instruction in the module (via looking through all
  27. // functions and blocks).
  28. for (auto& function : *GetIRContext()->module()) {
  29. for (auto& block : function) {
  30. if (auto merge_inst = block.GetMergeInst()) {
  31. // Ignore the instruction if it is not a selection merge.
  32. if (merge_inst->opcode() != spv::Op::OpSelectionMerge) {
  33. continue;
  34. }
  35. // Choose randomly whether to change the selection control for this
  36. // instruction.
  37. if (!GetFuzzerContext()->ChoosePercentage(
  38. GetFuzzerContext()->GetChanceOfAdjustingSelectionControl())) {
  39. continue;
  40. }
  41. // The choices to change the selection control to are the set of valid
  42. // controls, minus the current control.
  43. std::vector<uint32_t> choices;
  44. for (auto control : {spv::SelectionControlMask::MaskNone,
  45. spv::SelectionControlMask::Flatten,
  46. spv::SelectionControlMask::DontFlatten}) {
  47. if (control ==
  48. spv::SelectionControlMask(merge_inst->GetSingleWordOperand(1))) {
  49. continue;
  50. }
  51. choices.push_back(uint32_t(control));
  52. }
  53. // Apply the transformation and add it to the output transformation
  54. // sequence.
  55. TransformationSetSelectionControl transformation(
  56. block.id(), choices[GetFuzzerContext()->RandomIndex(choices)]);
  57. ApplyTransformation(transformation);
  58. }
  59. }
  60. }
  61. }
  62. } // namespace fuzz
  63. } // namespace spvtools