fuzzer_pass_adjust_function_controls.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_function_controls.h"
  15. #include "source/fuzz/transformation_set_function_control.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. FuzzerPassAdjustFunctionControls::FuzzerPassAdjustFunctionControls(
  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. FuzzerPassAdjustFunctionControls::~FuzzerPassAdjustFunctionControls() = default;
  24. void FuzzerPassAdjustFunctionControls::Apply() {
  25. // Consider every function in the module.
  26. for (auto& function : *GetIRContext()->module()) {
  27. // Randomly decide whether to adjust this function's controls.
  28. if (GetFuzzerContext()->ChoosePercentage(
  29. GetFuzzerContext()->GetChanceOfAdjustingFunctionControl())) {
  30. // Grab the function control mask for the function in its present form.
  31. uint32_t existing_function_control_mask =
  32. function.DefInst().GetSingleWordInOperand(0);
  33. // For the new mask, we first randomly select one of three basic masks:
  34. // None, Inline or DontInline. These are always valid (and are mutually
  35. // exclusive).
  36. std::vector<uint32_t> basic_function_control_masks = {
  37. SpvFunctionControlMaskNone, SpvFunctionControlInlineMask,
  38. SpvFunctionControlDontInlineMask};
  39. uint32_t new_function_control_mask =
  40. basic_function_control_masks[GetFuzzerContext()->RandomIndex(
  41. basic_function_control_masks)];
  42. // We now consider the Pure and Const mask bits. If these are already
  43. // set on the function then it's OK to keep them, but also interesting
  44. // to consider dropping them, so we decide randomly in each case.
  45. for (auto mask_bit :
  46. {SpvFunctionControlPureMask, SpvFunctionControlConstMask}) {
  47. if ((existing_function_control_mask & mask_bit) &&
  48. GetFuzzerContext()->ChooseEven()) {
  49. new_function_control_mask |= mask_bit;
  50. }
  51. }
  52. // Create and add a transformation.
  53. TransformationSetFunctionControl transformation(
  54. function.DefInst().result_id(), new_function_control_mask);
  55. assert(transformation.IsApplicable(GetIRContext(), *GetFactManager()) &&
  56. "Transformation should be applicable by construction.");
  57. transformation.Apply(GetIRContext(), GetFactManager());
  58. *GetTransformations()->add_transformation() = transformation.ToMessage();
  59. }
  60. }
  61. }
  62. } // namespace fuzz
  63. } // namespace spvtools