transformation_add_local_variable.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2020 Google LLC
  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_local_variable.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddLocalVariable::TransformationAddLocalVariable(
  19. spvtools::fuzz::protobufs::TransformationAddLocalVariable message)
  20. : message_(std::move(message)) {}
  21. TransformationAddLocalVariable::TransformationAddLocalVariable(
  22. uint32_t fresh_id, uint32_t type_id, uint32_t function_id,
  23. uint32_t initializer_id, bool value_is_irrelevant) {
  24. message_.set_fresh_id(fresh_id);
  25. message_.set_type_id(type_id);
  26. message_.set_function_id(function_id);
  27. message_.set_initializer_id(initializer_id);
  28. message_.set_value_is_irrelevant(value_is_irrelevant);
  29. }
  30. bool TransformationAddLocalVariable::IsApplicable(
  31. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  32. // The provided id must be fresh.
  33. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  34. return false;
  35. }
  36. // The pointer type id must indeed correspond to a pointer, and it must have
  37. // function storage class.
  38. auto type_instruction =
  39. ir_context->get_def_use_mgr()->GetDef(message_.type_id());
  40. if (!type_instruction ||
  41. type_instruction->opcode() != spv::Op::OpTypePointer ||
  42. spv::StorageClass(type_instruction->GetSingleWordInOperand(0)) !=
  43. spv::StorageClass::Function) {
  44. return false;
  45. }
  46. // The initializer must...
  47. auto initializer_instruction =
  48. ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  49. // ... exist, ...
  50. if (!initializer_instruction) {
  51. return false;
  52. }
  53. // ... be a constant, ...
  54. if (!spvOpcodeIsConstant(initializer_instruction->opcode())) {
  55. return false;
  56. }
  57. // ... and have the same type as the pointee type.
  58. if (initializer_instruction->type_id() !=
  59. type_instruction->GetSingleWordInOperand(1)) {
  60. return false;
  61. }
  62. // The function to which the local variable is to be added must exist.
  63. return fuzzerutil::FindFunction(ir_context, message_.function_id());
  64. }
  65. void TransformationAddLocalVariable::Apply(
  66. opt::IRContext* ir_context,
  67. TransformationContext* transformation_context) const {
  68. opt::Instruction* new_instruction = fuzzerutil::AddLocalVariable(
  69. ir_context, message_.fresh_id(), message_.type_id(),
  70. message_.function_id(), message_.initializer_id());
  71. // Inform the def-use manager about the new instruction.
  72. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
  73. ir_context->set_instr_block(
  74. new_instruction,
  75. fuzzerutil::FindFunction(ir_context, message_.function_id())
  76. ->entry()
  77. .get());
  78. if (message_.value_is_irrelevant()) {
  79. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  80. message_.fresh_id());
  81. }
  82. }
  83. protobufs::Transformation TransformationAddLocalVariable::ToMessage() const {
  84. protobufs::Transformation result;
  85. *result.mutable_add_local_variable() = message_;
  86. return result;
  87. }
  88. std::unordered_set<uint32_t> TransformationAddLocalVariable::GetFreshIds()
  89. const {
  90. return {message_.fresh_id()};
  91. }
  92. } // namespace fuzz
  93. } // namespace spvtools