transformation_load.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_load.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationLoad::TransformationLoad(
  20. const spvtools::fuzz::protobufs::TransformationLoad& message)
  21. : message_(message) {}
  22. TransformationLoad::TransformationLoad(
  23. uint32_t fresh_id, uint32_t pointer_id,
  24. const protobufs::InstructionDescriptor& instruction_to_insert_before) {
  25. message_.set_fresh_id(fresh_id);
  26. message_.set_pointer_id(pointer_id);
  27. *message_.mutable_instruction_to_insert_before() =
  28. instruction_to_insert_before;
  29. }
  30. bool TransformationLoad::IsApplicable(
  31. opt::IRContext* context,
  32. const spvtools::fuzz::FactManager& /*unused*/) const {
  33. // The result id must be fresh.
  34. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  35. return false;
  36. }
  37. // The pointer must exist and have a type.
  38. auto pointer = context->get_def_use_mgr()->GetDef(message_.pointer_id());
  39. if (!pointer || !pointer->type_id()) {
  40. return false;
  41. }
  42. // The type must indeed be a pointer type.
  43. auto pointer_type = context->get_def_use_mgr()->GetDef(pointer->type_id());
  44. assert(pointer_type && "Type id must be defined.");
  45. if (pointer_type->opcode() != SpvOpTypePointer) {
  46. return false;
  47. }
  48. // We do not want to allow loading from null or undefined pointers, as it is
  49. // not clear how punishing the consequences of doing so are from a semantics
  50. // point of view.
  51. switch (pointer->opcode()) {
  52. case SpvOpConstantNull:
  53. case SpvOpUndef:
  54. return false;
  55. default:
  56. break;
  57. }
  58. // Determine which instruction we should be inserting before.
  59. auto insert_before =
  60. FindInstruction(message_.instruction_to_insert_before(), context);
  61. // It must exist, ...
  62. if (!insert_before) {
  63. return false;
  64. }
  65. // ... and it must be legitimate to insert a store before it.
  66. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpLoad, insert_before)) {
  67. return false;
  68. }
  69. // The pointer needs to be available at the insertion point.
  70. return fuzzerutil::IdIsAvailableBeforeInstruction(context, insert_before,
  71. message_.pointer_id());
  72. }
  73. void TransformationLoad::Apply(opt::IRContext* context,
  74. spvtools::fuzz::FactManager* /*unused*/) const {
  75. uint32_t result_type = fuzzerutil::GetPointeeTypeIdFromPointerType(
  76. context, fuzzerutil::GetTypeId(context, message_.pointer_id()));
  77. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  78. FindInstruction(message_.instruction_to_insert_before(), context)
  79. ->InsertBefore(MakeUnique<opt::Instruction>(
  80. context, SpvOpLoad, result_type, message_.fresh_id(),
  81. opt::Instruction::OperandList(
  82. {{SPV_OPERAND_TYPE_ID, {message_.pointer_id()}}})));
  83. context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  84. }
  85. protobufs::Transformation TransformationLoad::ToMessage() const {
  86. protobufs::Transformation result;
  87. *result.mutable_load() = message_;
  88. return result;
  89. }
  90. } // namespace fuzz
  91. } // namespace spvtools