fuzzer_pass_add_parameters.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. : 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. auto num_new_parameters =
  50. GetFuzzerContext()->GetRandomNumberOfNewParameters(
  51. GetNumberOfParameters(function));
  52. for (uint32_t i = 0; i < num_new_parameters; ++i) {
  53. ApplyTransformation(TransformationAddParameter(
  54. function.result_id(), GetFuzzerContext()->GetFreshId(),
  55. FindOrCreateZeroConstant(
  56. type_candidates[GetFuzzerContext()->RandomIndex(
  57. type_candidates)]),
  58. GetFuzzerContext()->GetFreshId()));
  59. }
  60. }
  61. }
  62. std::vector<uint32_t> FuzzerPassAddParameters::ComputeTypeCandidates() const {
  63. std::vector<uint32_t> result;
  64. for (const auto* type_inst : GetIRContext()->module()->GetTypes()) {
  65. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  66. // the number of types we support here is limited by the number of types
  67. // supported by |FindOrCreateZeroConstant|.
  68. switch (type_inst->opcode()) {
  69. case SpvOpTypeBool:
  70. case SpvOpTypeInt:
  71. case SpvOpTypeFloat:
  72. case SpvOpTypeArray:
  73. case SpvOpTypeMatrix:
  74. case SpvOpTypeVector:
  75. case SpvOpTypeStruct: {
  76. result.push_back(type_inst->result_id());
  77. } break;
  78. default:
  79. // Ignore other types.
  80. break;
  81. }
  82. }
  83. return result;
  84. }
  85. uint32_t FuzzerPassAddParameters::GetNumberOfParameters(
  86. const opt::Function& function) const {
  87. const auto* type = GetIRContext()->get_type_mgr()->GetType(
  88. function.DefInst().GetSingleWordInOperand(1));
  89. assert(type && type->AsFunction());
  90. return static_cast<uint32_t>(type->AsFunction()->param_types().size());
  91. }
  92. } // namespace fuzz
  93. } // namespace spvtools