fuzzer_pass_adjust_selection_controls.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, FactManager* fact_manager,
  20. FuzzerContext* fuzzer_context,
  21. protobufs::TransformationSequence* transformations)
  22. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  23. FuzzerPassAdjustSelectionControls::~FuzzerPassAdjustSelectionControls() =
  24. default;
  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() != SpvOpSelectionMerge) {
  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 :
  45. {SpvSelectionControlMaskNone, SpvSelectionControlFlattenMask,
  46. SpvSelectionControlDontFlattenMask}) {
  47. if (control == merge_inst->GetSingleWordOperand(1)) {
  48. continue;
  49. }
  50. choices.push_back(control);
  51. }
  52. // Apply the transformation and add it to the output transformation
  53. // sequence.
  54. TransformationSetSelectionControl transformation(
  55. block.id(), choices[GetFuzzerContext()->RandomIndex(choices)]);
  56. assert(transformation.IsApplicable(GetIRContext(), *GetFactManager()) &&
  57. "Transformation should be applicable by construction.");
  58. transformation.Apply(GetIRContext(), GetFactManager());
  59. *GetTransformations()->add_transformation() =
  60. transformation.ToMessage();
  61. }
  62. }
  63. }
  64. }
  65. } // namespace fuzz
  66. } // namespace spvtools