transformation_access_chain.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_access_chain.h"
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. TransformationAccessChain::TransformationAccessChain(
  21. const spvtools::fuzz::protobufs::TransformationAccessChain& message)
  22. : message_(message) {}
  23. TransformationAccessChain::TransformationAccessChain(
  24. uint32_t fresh_id, uint32_t pointer_id,
  25. const std::vector<uint32_t>& index_id,
  26. const protobufs::InstructionDescriptor& instruction_to_insert_before) {
  27. message_.set_fresh_id(fresh_id);
  28. message_.set_pointer_id(pointer_id);
  29. for (auto id : index_id) {
  30. message_.add_index_id(id);
  31. }
  32. *message_.mutable_instruction_to_insert_before() =
  33. instruction_to_insert_before;
  34. }
  35. bool TransformationAccessChain::IsApplicable(
  36. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  37. // The result id must be fresh
  38. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  39. return false;
  40. }
  41. // The pointer id must exist and have a type.
  42. auto pointer = ir_context->get_def_use_mgr()->GetDef(message_.pointer_id());
  43. if (!pointer || !pointer->type_id()) {
  44. return false;
  45. }
  46. // The type must indeed be a pointer
  47. auto pointer_type = ir_context->get_def_use_mgr()->GetDef(pointer->type_id());
  48. if (pointer_type->opcode() != SpvOpTypePointer) {
  49. return false;
  50. }
  51. // The described instruction to insert before must exist and be a suitable
  52. // point where an OpAccessChain instruction could be inserted.
  53. auto instruction_to_insert_before =
  54. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  55. if (!instruction_to_insert_before) {
  56. return false;
  57. }
  58. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  59. SpvOpAccessChain, instruction_to_insert_before)) {
  60. return false;
  61. }
  62. // Do not allow making an access chain from a null or undefined pointer, as
  63. // we do not want to allow accessing such pointers. This might be acceptable
  64. // in dead blocks, but we conservatively avoid it.
  65. switch (pointer->opcode()) {
  66. case SpvOpConstantNull:
  67. case SpvOpUndef:
  68. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3185): When
  69. // fuzzing for real we would like an 'assert(false)' here. But we also
  70. // want to be able to write negative unit tests.
  71. return false;
  72. default:
  73. break;
  74. }
  75. // The pointer on which the access chain is to be based needs to be available
  76. // (according to dominance rules) at the insertion point.
  77. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  78. ir_context, instruction_to_insert_before, message_.pointer_id())) {
  79. return false;
  80. }
  81. // We now need to use the given indices to walk the type structure of the
  82. // base type of the pointer, making sure that (a) the indices correspond to
  83. // integers, and (b) these integer values are in-bounds.
  84. // Start from the base type of the pointer.
  85. uint32_t subobject_type_id = pointer_type->GetSingleWordInOperand(1);
  86. // Consider the given index ids in turn.
  87. for (auto index_id : message_.index_id()) {
  88. // Try to get the integer value associated with this index is. The first
  89. // component of the result will be false if the id did not correspond to an
  90. // integer. Otherwise, the integer with which the id is associated is the
  91. // second component.
  92. std::pair<bool, uint32_t> maybe_index_value =
  93. GetIndexValue(ir_context, index_id);
  94. if (!maybe_index_value.first) {
  95. // There was no integer: this index is no good.
  96. return false;
  97. }
  98. // Try to walk down the type using this index. This will yield 0 if the
  99. // type is not a composite or the index is out of bounds, and the id of
  100. // the next type otherwise.
  101. subobject_type_id = fuzzerutil::WalkOneCompositeTypeIndex(
  102. ir_context, subobject_type_id, maybe_index_value.second);
  103. if (!subobject_type_id) {
  104. // Either the type was not a composite (so that too many indices were
  105. // provided), or the index was out of bounds.
  106. return false;
  107. }
  108. }
  109. // At this point, |subobject_type_id| is the type of the value targeted by
  110. // the new access chain. The result type of the access chain should be a
  111. // pointer to this type, with the same storage class as for the original
  112. // pointer. Such a pointer type needs to exist in the module.
  113. //
  114. // We do not use the type manager to look up this type, due to problems
  115. // associated with pointers to isomorphic structs being regarded as the same.
  116. return fuzzerutil::MaybeGetPointerType(
  117. ir_context, subobject_type_id,
  118. static_cast<SpvStorageClass>(
  119. pointer_type->GetSingleWordInOperand(0))) != 0;
  120. }
  121. void TransformationAccessChain::Apply(
  122. opt::IRContext* ir_context,
  123. TransformationContext* transformation_context) const {
  124. // The operands to the access chain are the pointer followed by the indices.
  125. // The result type of the access chain is determined by where the indices
  126. // lead. We thus push the pointer to a sequence of operands, and then follow
  127. // the indices, pushing each to the operand list and tracking the type
  128. // obtained by following it. Ultimately this yields the type of the
  129. // component reached by following all the indices, and the result type is
  130. // a pointer to this component type.
  131. opt::Instruction::OperandList operands;
  132. // Add the pointer id itself.
  133. operands.push_back({SPV_OPERAND_TYPE_ID, {message_.pointer_id()}});
  134. // Start walking the indices, starting with the pointer's base type.
  135. auto pointer_type = ir_context->get_def_use_mgr()->GetDef(
  136. ir_context->get_def_use_mgr()->GetDef(message_.pointer_id())->type_id());
  137. uint32_t subobject_type_id = pointer_type->GetSingleWordInOperand(1);
  138. // Go through the index ids in turn.
  139. for (auto index_id : message_.index_id()) {
  140. // Add the index id to the operands.
  141. operands.push_back({SPV_OPERAND_TYPE_ID, {index_id}});
  142. // Get the integer value associated with the index id.
  143. uint32_t index_value = GetIndexValue(ir_context, index_id).second;
  144. // Walk to the next type in the composite object using this index.
  145. subobject_type_id = fuzzerutil::WalkOneCompositeTypeIndex(
  146. ir_context, subobject_type_id, index_value);
  147. }
  148. // The access chain's result type is a pointer to the composite component that
  149. // was reached after following all indices. The storage class is that of the
  150. // original pointer.
  151. uint32_t result_type = fuzzerutil::MaybeGetPointerType(
  152. ir_context, subobject_type_id,
  153. static_cast<SpvStorageClass>(pointer_type->GetSingleWordInOperand(0)));
  154. // Add the access chain instruction to the module, and update the module's id
  155. // bound.
  156. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  157. FindInstruction(message_.instruction_to_insert_before(), ir_context)
  158. ->InsertBefore(MakeUnique<opt::Instruction>(
  159. ir_context, SpvOpAccessChain, result_type, message_.fresh_id(),
  160. operands));
  161. // Conservatively invalidate all analyses.
  162. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  163. // If the base pointer's pointee value was irrelevant, the same is true of the
  164. // pointee value of the result of this access chain.
  165. if (transformation_context->GetFactManager()->PointeeValueIsIrrelevant(
  166. message_.pointer_id())) {
  167. transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  168. message_.fresh_id());
  169. }
  170. }
  171. protobufs::Transformation TransformationAccessChain::ToMessage() const {
  172. protobufs::Transformation result;
  173. *result.mutable_access_chain() = message_;
  174. return result;
  175. }
  176. std::pair<bool, uint32_t> TransformationAccessChain::GetIndexValue(
  177. opt::IRContext* ir_context, uint32_t index_id) const {
  178. auto index_instruction = ir_context->get_def_use_mgr()->GetDef(index_id);
  179. if (!index_instruction || !spvOpcodeIsConstant(index_instruction->opcode())) {
  180. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3179) We could
  181. // allow non-constant indices when looking up non-structs, using clamping
  182. // to ensure they are in-bounds.
  183. return {false, 0};
  184. }
  185. auto index_type =
  186. ir_context->get_def_use_mgr()->GetDef(index_instruction->type_id());
  187. if (index_type->opcode() != SpvOpTypeInt ||
  188. index_type->GetSingleWordInOperand(0) != 32) {
  189. return {false, 0};
  190. }
  191. return {true, index_instruction->GetSingleWordInOperand(0)};
  192. }
  193. } // namespace fuzz
  194. } // namespace spvtools