fuzzer_pass_mutate_pointers.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_mutate_pointers.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/transformation_mutate_pointer.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassMutatePointers::FuzzerPassMutatePointers(
  21. opt::IRContext* ir_context, TransformationContext* transformation_context,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations,
  24. bool ignore_inapplicable_transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations, ignore_inapplicable_transformations) {}
  27. void FuzzerPassMutatePointers::Apply() {
  28. ForEachInstructionWithInstructionDescriptor(
  29. [this](opt::Function* function, opt::BasicBlock* block,
  30. opt::BasicBlock::iterator inst_it,
  31. const protobufs::InstructionDescriptor& instruction_descriptor) {
  32. if (!GetFuzzerContext()->ChoosePercentage(
  33. GetFuzzerContext()->GetChanceOfMutatingPointer())) {
  34. return;
  35. }
  36. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad,
  37. inst_it)) {
  38. return;
  39. }
  40. auto available_pointers = FindAvailableInstructions(
  41. function, block, inst_it,
  42. [](opt::IRContext* ir_context, opt::Instruction* inst) {
  43. return TransformationMutatePointer::IsValidPointerInstruction(
  44. ir_context, *inst);
  45. });
  46. if (available_pointers.empty()) {
  47. return;
  48. }
  49. const auto* pointer_inst =
  50. available_pointers[GetFuzzerContext()->RandomIndex(
  51. available_pointers)];
  52. // Make sure there is an irrelevant constant in the module.
  53. FindOrCreateZeroConstant(fuzzerutil::GetPointeeTypeIdFromPointerType(
  54. GetIRContext(), pointer_inst->type_id()),
  55. true);
  56. ApplyTransformation(TransformationMutatePointer(
  57. pointer_inst->result_id(), GetFuzzerContext()->GetFreshId(),
  58. instruction_descriptor));
  59. });
  60. }
  61. } // namespace fuzz
  62. } // namespace spvtools