transformation_add_parameter.cpp 5.4 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/transformation_add_parameter.h"
  15. #include <source/spirv_constant.h>
  16. #include "source/fuzz/fuzzer_util.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationAddParameter::TransformationAddParameter(
  20. const protobufs::TransformationAddParameter& message)
  21. : message_(message) {}
  22. TransformationAddParameter::TransformationAddParameter(
  23. uint32_t function_id, uint32_t parameter_fresh_id, uint32_t initializer_id,
  24. uint32_t function_type_fresh_id) {
  25. message_.set_function_id(function_id);
  26. message_.set_parameter_fresh_id(parameter_fresh_id);
  27. message_.set_initializer_id(initializer_id);
  28. message_.set_function_type_fresh_id(function_type_fresh_id);
  29. }
  30. bool TransformationAddParameter::IsApplicable(
  31. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  32. // Check that function exists
  33. const auto* function =
  34. fuzzerutil::FindFunction(ir_context, message_.function_id());
  35. if (!function ||
  36. fuzzerutil::FunctionIsEntryPoint(ir_context, function->result_id())) {
  37. return false;
  38. }
  39. // Check that |initializer_id| is valid.
  40. const auto* initializer_inst =
  41. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  42. if (!initializer_inst) {
  43. return false;
  44. }
  45. // Check that initializer's type is valid.
  46. const auto* initializer_type =
  47. ir_context->get_type_mgr()->GetType(initializer_inst->type_id());
  48. if (!initializer_type || initializer_type->AsVoid()) {
  49. return false;
  50. }
  51. return fuzzerutil::IsFreshId(ir_context, message_.parameter_fresh_id()) &&
  52. fuzzerutil::IsFreshId(ir_context, message_.function_type_fresh_id()) &&
  53. message_.parameter_fresh_id() != message_.function_type_fresh_id();
  54. }
  55. void TransformationAddParameter::Apply(
  56. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  57. // Find the function that will be transformed
  58. auto* function = fuzzerutil::FindFunction(ir_context, message_.function_id());
  59. assert(function && "Can't find the function");
  60. auto parameter_type_id =
  61. fuzzerutil::GetTypeId(ir_context, message_.initializer_id());
  62. assert(parameter_type_id != 0 && "Initializer has invalid type");
  63. // Add new parameters to the function.
  64. function->AddParameter(MakeUnique<opt::Instruction>(
  65. ir_context, SpvOpFunctionParameter, parameter_type_id,
  66. message_.parameter_fresh_id(), opt::Instruction::OperandList()));
  67. fuzzerutil::UpdateModuleIdBound(ir_context, message_.parameter_fresh_id());
  68. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
  69. // Add an PointeeValueIsIrrelevant fact if the parameter is a pointer.
  70. // Fix all OpFunctionCall instructions.
  71. ir_context->get_def_use_mgr()->ForEachUser(
  72. &function->DefInst(), [this](opt::Instruction* call) {
  73. if (call->opcode() != SpvOpFunctionCall ||
  74. call->GetSingleWordInOperand(0) != message_.function_id()) {
  75. return;
  76. }
  77. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3177):
  78. // it would be good to mark this usage of |id| as irrelevant, so that
  79. // we can perform some interesting transformations on it later.
  80. call->AddOperand({SPV_OPERAND_TYPE_ID, {message_.initializer_id()}});
  81. });
  82. auto* old_function_type = fuzzerutil::GetFunctionType(ir_context, function);
  83. assert(old_function_type && "Function must have a valid type");
  84. if (ir_context->get_def_use_mgr()->NumUsers(old_function_type) == 1) {
  85. // Adjust existing function type if it is used only by this function.
  86. old_function_type->AddOperand({SPV_OPERAND_TYPE_ID, {parameter_type_id}});
  87. // We must make sure that all dependencies of |old_function_type| are
  88. // evaluated before |old_function_type| (i.e. the domination rules are not
  89. // broken). Thus, we move |old_function_type| to the end of the list of all
  90. // types in the module.
  91. old_function_type->RemoveFromList();
  92. ir_context->AddType(std::unique_ptr<opt::Instruction>(old_function_type));
  93. } else {
  94. // Otherwise, either create a new type or use an existing one.
  95. std::vector<uint32_t> type_ids;
  96. type_ids.reserve(old_function_type->NumInOperands() + 1);
  97. for (uint32_t i = 0, n = old_function_type->NumInOperands(); i < n; ++i) {
  98. type_ids.push_back(old_function_type->GetSingleWordInOperand(i));
  99. }
  100. type_ids.push_back(parameter_type_id);
  101. function->DefInst().SetInOperand(
  102. 1, {fuzzerutil::FindOrCreateFunctionType(
  103. ir_context, message_.function_type_fresh_id(), type_ids)});
  104. }
  105. // Make sure our changes are analyzed.
  106. ir_context->InvalidateAnalysesExceptFor(
  107. opt::IRContext::Analysis::kAnalysisNone);
  108. }
  109. protobufs::Transformation TransformationAddParameter::ToMessage() const {
  110. protobufs::Transformation result;
  111. *result.mutable_add_parameter() = message_;
  112. return result;
  113. }
  114. } // namespace fuzz
  115. } // namespace spvtools