transformation_add_constant_null.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_constant_null.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddConstantNull::TransformationAddConstantNull(
  19. spvtools::fuzz::protobufs::TransformationAddConstantNull message)
  20. : message_(std::move(message)) {}
  21. TransformationAddConstantNull::TransformationAddConstantNull(uint32_t fresh_id,
  22. uint32_t type_id) {
  23. message_.set_fresh_id(fresh_id);
  24. message_.set_type_id(type_id);
  25. }
  26. bool TransformationAddConstantNull::IsApplicable(
  27. opt::IRContext* context, const TransformationContext& /*unused*/) const {
  28. // A fresh id is required.
  29. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  30. return false;
  31. }
  32. auto type = context->get_def_use_mgr()->GetDef(message_.type_id());
  33. // The type must exist.
  34. if (!type) {
  35. return false;
  36. }
  37. // The type must be one of the types for which null constants are allowed,
  38. // according to the SPIR-V spec.
  39. return fuzzerutil::IsNullConstantSupported(context, *type);
  40. }
  41. void TransformationAddConstantNull::Apply(
  42. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  43. auto new_instruction = MakeUnique<opt::Instruction>(
  44. ir_context, spv::Op::OpConstantNull, message_.type_id(),
  45. message_.fresh_id(), opt::Instruction::OperandList());
  46. auto new_instruction_ptr = new_instruction.get();
  47. ir_context->module()->AddGlobalValue(std::move(new_instruction));
  48. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  49. // Inform the def-use manager about the new instruction. Invalidate the
  50. // constant manager as we have added a new constant.
  51. ir_context->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr);
  52. ir_context->InvalidateAnalyses(opt::IRContext::kAnalysisConstants);
  53. }
  54. protobufs::Transformation TransformationAddConstantNull::ToMessage() const {
  55. protobufs::Transformation result;
  56. *result.mutable_add_constant_null() = message_;
  57. return result;
  58. }
  59. std::unordered_set<uint32_t> TransformationAddConstantNull::GetFreshIds()
  60. const {
  61. return {message_.fresh_id()};
  62. }
  63. } // namespace fuzz
  64. } // namespace spvtools