transformation_load.cpp 3.7 KB

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