fuzzer_pass_add_parameters.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_parameters.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. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations) {}
  27. FuzzerPassAddParameters::~FuzzerPassAddParameters() = default;
  28. void FuzzerPassAddParameters::Apply() {
  29. const auto& type_candidates = ComputeTypeCandidates();
  30. if (type_candidates.empty()) {
  31. // The module contains no suitable types to use in new parameters.
  32. return;
  33. }
  34. // Iterate over all functions in the module.
  35. for (const auto& function : *GetIRContext()->module()) {
  36. // Skip all entry-point functions - we don't want to change those.
  37. if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(),
  38. function.result_id())) {
  39. continue;
  40. }
  41. if (GetNumberOfParameters(function) >=
  42. GetFuzzerContext()->GetMaximumNumberOfFunctionParameters()) {
  43. continue;
  44. }
  45. if (!GetFuzzerContext()->ChoosePercentage(
  46. GetFuzzerContext()->GetChanceOfAddingParameters())) {
  47. continue;
  48. }
  49. const auto* type_inst =
  50. fuzzerutil::GetFunctionType(GetIRContext(), &function);
  51. assert(type_inst);
  52. // -1 because we don't take return type into account.
  53. auto num_old_parameters = type_inst->NumInOperands() - 1;
  54. auto num_new_parameters =
  55. GetFuzzerContext()->GetRandomNumberOfNewParameters(
  56. GetNumberOfParameters(function));
  57. std::vector<uint32_t> all_types(num_old_parameters);
  58. std::vector<uint32_t> new_types(num_new_parameters);
  59. std::vector<uint32_t> parameter_ids(num_new_parameters);
  60. std::vector<uint32_t> constant_ids(num_new_parameters);
  61. // Get type ids for old parameters.
  62. for (uint32_t i = 0; i < num_old_parameters; ++i) {
  63. // +1 since we don't take return type into account.
  64. all_types[i] = type_inst->GetSingleWordInOperand(i + 1);
  65. }
  66. for (uint32_t i = 0; i < num_new_parameters; ++i) {
  67. // Get type ids for new parameters.
  68. new_types[i] =
  69. type_candidates[GetFuzzerContext()->RandomIndex(type_candidates)];
  70. // Create constants to initialize new parameters from.
  71. constant_ids[i] = FindOrCreateZeroConstant(new_types[i]);
  72. }
  73. // Append new parameters to the old ones.
  74. all_types.insert(all_types.end(), new_types.begin(), new_types.end());
  75. // Generate result ids for new parameters.
  76. for (auto& id : parameter_ids) {
  77. id = GetFuzzerContext()->GetFreshId();
  78. }
  79. auto result_type_id = type_inst->GetSingleWordInOperand(0);
  80. ApplyTransformation(TransformationAddParameters(
  81. function.result_id(),
  82. FindOrCreateFunctionType(result_type_id, all_types),
  83. std::move(new_types), std::move(parameter_ids),
  84. std::move(constant_ids)));
  85. }
  86. }
  87. std::vector<uint32_t> FuzzerPassAddParameters::ComputeTypeCandidates() const {
  88. std::vector<uint32_t> result;
  89. for (const auto* type_inst : GetIRContext()->module()->GetTypes()) {
  90. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  91. // the number of types we support here is limited by the number of types
  92. // supported by |FindOrCreateZeroConstant|.
  93. switch (type_inst->opcode()) {
  94. case SpvOpTypeBool:
  95. case SpvOpTypeInt:
  96. case SpvOpTypeFloat:
  97. case SpvOpTypeArray:
  98. case SpvOpTypeMatrix:
  99. case SpvOpTypeVector:
  100. case SpvOpTypeStruct: {
  101. result.push_back(type_inst->result_id());
  102. } break;
  103. default:
  104. // Ignore other types.
  105. break;
  106. }
  107. }
  108. return result;
  109. }
  110. uint32_t FuzzerPassAddParameters::GetNumberOfParameters(
  111. const opt::Function& function) const {
  112. const auto* type = GetIRContext()->get_type_mgr()->GetType(
  113. function.DefInst().GetSingleWordInOperand(1));
  114. assert(type && type->AsFunction());
  115. return static_cast<uint32_t>(type->AsFunction()->param_types().size());
  116. }
  117. } // namespace fuzz
  118. } // namespace spvtools