fuzzer_pass_add_parameters.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_parameters.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_parameter.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassAddParameters::FuzzerPassAddParameters(
  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 FuzzerPassAddParameters::Apply() {
  29. // Compute type candidates for the new parameter.
  30. std::vector<uint32_t> type_candidates;
  31. for (const auto& type_inst : GetIRContext()->module()->GetTypes()) {
  32. if (TransformationAddParameter::IsParameterTypeSupported(
  33. GetIRContext(), type_inst->result_id())) {
  34. type_candidates.push_back(type_inst->result_id());
  35. }
  36. }
  37. if (type_candidates.empty()) {
  38. // The module contains no suitable types to use in new parameters.
  39. return;
  40. }
  41. // Iterate over all functions in the module.
  42. for (const auto& function : *GetIRContext()->module()) {
  43. // Skip all entry-point functions - we don't want to change those.
  44. if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(),
  45. function.result_id())) {
  46. continue;
  47. }
  48. if (GetNumberOfParameters(function) >=
  49. GetFuzzerContext()->GetMaximumNumberOfFunctionParameters()) {
  50. continue;
  51. }
  52. if (!GetFuzzerContext()->ChoosePercentage(
  53. GetFuzzerContext()->GetChanceOfAddingParameters())) {
  54. continue;
  55. }
  56. auto num_new_parameters =
  57. GetFuzzerContext()->GetRandomNumberOfNewParameters(
  58. GetNumberOfParameters(function));
  59. for (uint32_t i = 0; i < num_new_parameters; ++i) {
  60. auto current_type_id =
  61. type_candidates[GetFuzzerContext()->RandomIndex(type_candidates)];
  62. auto current_type =
  63. GetIRContext()->get_type_mgr()->GetType(current_type_id);
  64. std::map<uint32_t, uint32_t> call_parameter_ids;
  65. // Consider the case when a pointer type was selected.
  66. if (current_type->kind() == opt::analysis::Type::kPointer) {
  67. auto storage_class = fuzzerutil::GetStorageClassFromPointerType(
  68. GetIRContext(), current_type_id);
  69. switch (storage_class) {
  70. case spv::StorageClass::Function: {
  71. // In every caller find or create a local variable that has the
  72. // selected type.
  73. for (auto* instr :
  74. fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
  75. auto block = GetIRContext()->get_instr_block(instr);
  76. auto function_id = block->GetParent()->result_id();
  77. uint32_t variable_id =
  78. FindOrCreateLocalVariable(current_type_id, function_id, true);
  79. call_parameter_ids[instr->result_id()] = variable_id;
  80. }
  81. } break;
  82. case spv::StorageClass::Private:
  83. case spv::StorageClass::Workgroup: {
  84. // If there exists at least one caller, find or create a global
  85. // variable that has the selected type.
  86. std::vector<opt::Instruction*> callers =
  87. fuzzerutil::GetCallers(GetIRContext(), function.result_id());
  88. if (!callers.empty()) {
  89. uint32_t variable_id =
  90. FindOrCreateGlobalVariable(current_type_id, true);
  91. for (auto* instr : callers) {
  92. call_parameter_ids[instr->result_id()] = variable_id;
  93. }
  94. }
  95. } break;
  96. default:
  97. break;
  98. }
  99. } else {
  100. // If there exists at least one caller, find or create a zero constant
  101. // that has the selected type.
  102. std::vector<opt::Instruction*> callers =
  103. fuzzerutil::GetCallers(GetIRContext(), function.result_id());
  104. if (!callers.empty()) {
  105. uint32_t constant_id =
  106. FindOrCreateZeroConstant(current_type_id, true);
  107. for (auto* instr :
  108. fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
  109. call_parameter_ids[instr->result_id()] = constant_id;
  110. }
  111. }
  112. }
  113. ApplyTransformation(TransformationAddParameter(
  114. function.result_id(), GetFuzzerContext()->GetFreshId(),
  115. current_type_id, std::move(call_parameter_ids),
  116. GetFuzzerContext()->GetFreshId()));
  117. }
  118. }
  119. }
  120. uint32_t FuzzerPassAddParameters::GetNumberOfParameters(
  121. const opt::Function& function) const {
  122. const auto* type = GetIRContext()->get_type_mgr()->GetType(
  123. function.DefInst().GetSingleWordInOperand(1));
  124. assert(type && type->AsFunction());
  125. return static_cast<uint32_t>(type->AsFunction()->param_types().size());
  126. }
  127. } // namespace fuzz
  128. } // namespace spvtools