fuzzer_pass_replace_params_with_struct.cpp 4.2 KB

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