fuzzer_pass_replace_parameter_with_global.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_parameter_with_global.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_parameter_with_global.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. FuzzerPassReplaceParameterWithGlobal::FuzzerPassReplaceParameterWithGlobal(
  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. FuzzerPassReplaceParameterWithGlobal::~FuzzerPassReplaceParameterWithGlobal() =
  29. default;
  30. void FuzzerPassReplaceParameterWithGlobal::Apply() {
  31. for (const auto& function : *GetIRContext()->module()) {
  32. if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(),
  33. function.result_id())) {
  34. continue;
  35. }
  36. if (!GetFuzzerContext()->ChoosePercentage(
  37. GetFuzzerContext()->GetChanceOfReplacingParametersWithGlobals())) {
  38. continue;
  39. }
  40. auto params =
  41. fuzzerutil::GetParameters(GetIRContext(), function.result_id());
  42. // Make sure at least one parameter can be replaced. Also checks that the
  43. // function has at least one parameter.
  44. if (std::none_of(params.begin(), params.end(),
  45. [this](const opt::Instruction* param) {
  46. const auto* param_type =
  47. GetIRContext()->get_type_mgr()->GetType(
  48. param->type_id());
  49. assert(param_type && "Parameter has invalid type");
  50. return TransformationReplaceParameterWithGlobal::
  51. CanReplaceFunctionParameterType(*param_type);
  52. })) {
  53. continue;
  54. }
  55. // Select id of a parameter to replace.
  56. const opt::Instruction* replaced_param = nullptr;
  57. const opt::analysis::Type* param_type = nullptr;
  58. do {
  59. replaced_param = GetFuzzerContext()->RemoveAtRandomIndex(&params);
  60. param_type =
  61. GetIRContext()->get_type_mgr()->GetType(replaced_param->type_id());
  62. assert(param_type && "Parameter has invalid type");
  63. } while (!TransformationReplaceParameterWithGlobal::
  64. CanReplaceFunctionParameterType(*param_type));
  65. assert(replaced_param && "Unable to find a parameter to replace");
  66. // Make sure type id for the global variable exists in the module.
  67. FindOrCreatePointerType(replaced_param->type_id(), SpvStorageClassPrivate);
  68. // Make sure initializer for the global variable exists in the module.
  69. FindOrCreateZeroConstant(replaced_param->type_id());
  70. ApplyTransformation(TransformationReplaceParameterWithGlobal(
  71. GetFuzzerContext()->GetFreshId(), replaced_param->result_id(),
  72. GetFuzzerContext()->GetFreshId()));
  73. }
  74. }
  75. } // namespace fuzz
  76. } // namespace spvtools