transformation_add_global_undef.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2019 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_global_undef.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/opt/reflect.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationAddGlobalUndef::TransformationAddGlobalUndef(
  20. spvtools::fuzz::protobufs::TransformationAddGlobalUndef message)
  21. : message_(std::move(message)) {}
  22. TransformationAddGlobalUndef::TransformationAddGlobalUndef(uint32_t fresh_id,
  23. uint32_t type_id) {
  24. message_.set_fresh_id(fresh_id);
  25. message_.set_type_id(type_id);
  26. }
  27. bool TransformationAddGlobalUndef::IsApplicable(
  28. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  29. // A fresh id is required.
  30. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  31. return false;
  32. }
  33. auto type = ir_context->get_def_use_mgr()->GetDef(message_.type_id());
  34. // The type must exist, and must not be a function or pointer type.
  35. return type != nullptr && opt::IsTypeInst(type->opcode()) &&
  36. type->opcode() != spv::Op::OpTypeFunction &&
  37. type->opcode() != spv::Op::OpTypePointer;
  38. }
  39. void TransformationAddGlobalUndef::Apply(
  40. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  41. auto new_instruction = MakeUnique<opt::Instruction>(
  42. ir_context, spv::Op::OpUndef, message_.type_id(), message_.fresh_id(),
  43. opt::Instruction::OperandList());
  44. auto new_instruction_ptr = new_instruction.get();
  45. ir_context->module()->AddGlobalValue(std::move(new_instruction));
  46. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  47. // Inform the def-use manager about the new instruction.
  48. ir_context->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr);
  49. }
  50. protobufs::Transformation TransformationAddGlobalUndef::ToMessage() const {
  51. protobufs::Transformation result;
  52. *result.mutable_add_global_undef() = message_;
  53. return result;
  54. }
  55. std::unordered_set<uint32_t> TransformationAddGlobalUndef::GetFreshIds() const {
  56. return {message_.fresh_id()};
  57. }
  58. } // namespace fuzz
  59. } // namespace spvtools