fuzzer_pass_replace_params_with_struct.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_replace_params_with_struct.h"
  15. #include <numeric>
  16. #include <vector>
  17. #include "source/fuzz/fuzzer_context.h"
  18. #include "source/fuzz/fuzzer_util.h"
  19. #include "source/fuzz/transformation_replace_params_with_struct.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. FuzzerPassReplaceParamsWithStruct::FuzzerPassReplaceParamsWithStruct(
  23. opt::IRContext* ir_context, TransformationContext* transformation_context,
  24. FuzzerContext* fuzzer_context,
  25. protobufs::TransformationSequence* transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations) {}
  28. FuzzerPassReplaceParamsWithStruct::~FuzzerPassReplaceParamsWithStruct() =
  29. default;
  30. void FuzzerPassReplaceParamsWithStruct::Apply() {
  31. for (const auto& function : *GetIRContext()->module()) {
  32. auto params =
  33. fuzzerutil::GetParameters(GetIRContext(), function.result_id());
  34. if (params.empty() || fuzzerutil::FunctionIsEntryPoint(
  35. GetIRContext(), function.result_id())) {
  36. continue;
  37. }
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()->GetChanceOfReplacingParametersWithStruct())) {
  40. continue;
  41. }
  42. std::vector<uint32_t> parameter_index(params.size());
  43. std::iota(parameter_index.begin(), parameter_index.end(), 0);
  44. // Remove the indices of unsupported parameters.
  45. auto new_end = std::remove_if(
  46. parameter_index.begin(), parameter_index.end(),
  47. [this, &params](uint32_t index) {
  48. const auto* type =
  49. GetIRContext()->get_type_mgr()->GetType(params[index]->type_id());
  50. assert(type && "Parameter has invalid type");
  51. return !TransformationReplaceParamsWithStruct::
  52. IsParameterTypeSupported(*type);
  53. });
  54. // std::remove_if changes the vector so that removed elements are placed at
  55. // the end (i.e. [new_end, parameter_index.end()) is a range of removed
  56. // elements). However, the size of the vector is not changed so we need to
  57. // change that explicitly by popping those elements from the vector.
  58. parameter_index.erase(new_end, parameter_index.end());
  59. if (parameter_index.empty()) {
  60. continue;
  61. }
  62. // Select |num_replaced_params| parameters at random. We shuffle the vector
  63. // of indices for randomization and shrink it to select first
  64. // |num_replaced_params| parameters.
  65. auto num_replaced_params = std::min<size_t>(
  66. parameter_index.size(),
  67. GetFuzzerContext()->GetRandomNumberOfParametersReplacedWithStruct(
  68. static_cast<uint32_t>(params.size())));
  69. GetFuzzerContext()->Shuffle(&parameter_index);
  70. parameter_index.resize(num_replaced_params);
  71. // Make sure OpTypeStruct exists in the module.
  72. std::vector<uint32_t> component_type_ids;
  73. for (auto index : parameter_index) {
  74. component_type_ids.push_back(params[index]->type_id());
  75. }
  76. FindOrCreateStructType(component_type_ids);
  77. // Map parameters' indices to parameters' ids.
  78. std::vector<uint32_t> parameter_id;
  79. for (auto index : parameter_index) {
  80. parameter_id.push_back(params[index]->result_id());
  81. }
  82. std::unordered_map<uint32_t, uint32_t> caller_id_to_fresh_id;
  83. for (const auto* inst :
  84. fuzzerutil::GetCallers(GetIRContext(), function.result_id())) {
  85. caller_id_to_fresh_id[inst->result_id()] =
  86. GetFuzzerContext()->GetFreshId();
  87. }
  88. ApplyTransformation(TransformationReplaceParamsWithStruct(
  89. parameter_id, GetFuzzerContext()->GetFreshId(),
  90. GetFuzzerContext()->GetFreshId(), caller_id_to_fresh_id));
  91. }
  92. }
  93. } // namespace fuzz
  94. } // namespace spvtools