fuzzer_pass_add_copy_memory.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_add_copy_memory.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_add_copy_memory.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassAddCopyMemory::FuzzerPassAddCopyMemory(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations,
  25. bool ignore_inapplicable_transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations, ignore_inapplicable_transformations) {}
  28. void FuzzerPassAddCopyMemory::Apply() {
  29. ForEachInstructionWithInstructionDescriptor(
  30. [this](opt::Function* function, opt::BasicBlock* block,
  31. opt::BasicBlock::iterator inst_it,
  32. const protobufs::InstructionDescriptor& instruction_descriptor) {
  33. // Check that we can insert an OpCopyMemory before this instruction.
  34. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpCopyMemory,
  35. inst_it)) {
  36. return;
  37. }
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()->GetChanceOfAddingCopyMemory())) {
  40. return;
  41. }
  42. // Get all instructions available before |inst_it| according to the
  43. // domination rules.
  44. auto instructions = FindAvailableInstructions(
  45. function, block, inst_it,
  46. TransformationAddCopyMemory::IsInstructionSupported);
  47. if (instructions.empty()) {
  48. return;
  49. }
  50. const auto* inst =
  51. instructions[GetFuzzerContext()->RandomIndex(instructions)];
  52. // Decide whether to create global or local variable.
  53. auto storage_class = GetFuzzerContext()->ChooseEven()
  54. ? spv::StorageClass::Private
  55. : spv::StorageClass::Function;
  56. auto pointee_type_id = fuzzerutil::GetPointeeTypeIdFromPointerType(
  57. GetIRContext(), inst->type_id());
  58. // Create a pointer type with |storage_class| if needed.
  59. FindOrCreatePointerType(pointee_type_id, storage_class);
  60. ApplyTransformation(TransformationAddCopyMemory(
  61. instruction_descriptor, GetFuzzerContext()->GetFreshId(),
  62. inst->result_id(), storage_class,
  63. FindOrCreateZeroConstant(pointee_type_id, false)));
  64. });
  65. }
  66. } // namespace fuzz
  67. } // namespace spvtools