transformation_access_chain.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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* context,
  37. const spvtools::fuzz::FactManager& /*unused*/) const {
  38. // The result id must be fresh
  39. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  40. return false;
  41. }
  42. // The pointer id must exist and have a type.
  43. auto pointer = context->get_def_use_mgr()->GetDef(message_.pointer_id());
  44. if (!pointer || !pointer->type_id()) {
  45. return false;
  46. }
  47. // The type must indeed be a pointer
  48. auto pointer_type = context->get_def_use_mgr()->GetDef(pointer->type_id());
  49. if (pointer_type->opcode() != SpvOpTypePointer) {
  50. return false;
  51. }
  52. // The described instruction to insert before must exist and be a suitable
  53. // point where an OpAccessChain instruction could be inserted.
  54. auto instruction_to_insert_before =
  55. FindInstruction(message_.instruction_to_insert_before(), context);
  56. if (!instruction_to_insert_before) {
  57. return false;
  58. }
  59. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  60. SpvOpAccessChain, instruction_to_insert_before)) {
  61. return false;
  62. }
  63. // Do not allow making an access chain from a null or undefined pointer, as
  64. // we do not want to allow accessing such pointers. This might be acceptable
  65. // in dead blocks, but we conservatively avoid it.
  66. switch (pointer->opcode()) {
  67. case SpvOpConstantNull:
  68. case SpvOpUndef:
  69. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3185): When
  70. // fuzzing for real we would like an 'assert(false)' here. But we also
  71. // want to be able to write negative unit tests.
  72. return false;
  73. default:
  74. break;
  75. }
  76. // The pointer on which the access chain is to be based needs to be available
  77. // (according to dominance rules) at the insertion point.
  78. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  79. context, instruction_to_insert_before, message_.pointer_id())) {
  80. return false;
  81. }
  82. // We now need to use the given indices to walk the type structure of the
  83. // base type of the pointer, making sure that (a) the indices correspond to
  84. // integers, and (b) these integer values are in-bounds.
  85. // Start from the base type of the pointer.
  86. uint32_t subobject_type_id = pointer_type->GetSingleWordInOperand(1);
  87. // Consider the given index ids in turn.
  88. for (auto index_id : message_.index_id()) {
  89. // Try to get the integer value associated with this index is. The first
  90. // component of the result will be false if the id did not correspond to an
  91. // integer. Otherwise, the integer with which the id is associated is the
  92. // second component.
  93. std::pair<bool, uint32_t> maybe_index_value =
  94. GetIndexValue(context, index_id);
  95. if (!maybe_index_value.first) {
  96. // There was no integer: this index is no good.
  97. return false;
  98. }
  99. // Try to walk down the type using this index. This will yield 0 if the
  100. // type is not a composite or the index is out of bounds, and the id of
  101. // the next type otherwise.
  102. subobject_type_id = fuzzerutil::WalkOneCompositeTypeIndex(
  103. context, subobject_type_id, maybe_index_value.second);
  104. if (!subobject_type_id) {
  105. // Either the type was not a composite (so that too many indices were
  106. // provided), or the index was out of bounds.
  107. return false;
  108. }
  109. }
  110. // At this point, |subobject_type_id| is the type of the value targeted by
  111. // the new access chain. The result type of the access chain should be a
  112. // pointer to this type, with the same storage class as for the original
  113. // pointer. Such a pointer type needs to exist in the module.
  114. //
  115. // We do not use the type manager to look up this type, due to problems
  116. // associated with pointers to isomorphic structs being regarded as the same.
  117. return fuzzerutil::MaybeGetPointerType(
  118. context, subobject_type_id,
  119. static_cast<SpvStorageClass>(
  120. pointer_type->GetSingleWordInOperand(0))) != 0;
  121. }
  122. void TransformationAccessChain::Apply(
  123. opt::IRContext* context, spvtools::fuzz::FactManager* fact_manager) 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 = context->get_def_use_mgr()->GetDef(
  136. 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(context, index_id).second;
  144. // Walk to the next type in the composite object using this index.
  145. subobject_type_id = fuzzerutil::WalkOneCompositeTypeIndex(
  146. 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. 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(context, message_.fresh_id());
  157. FindInstruction(message_.instruction_to_insert_before(), context)
  158. ->InsertBefore(
  159. MakeUnique<opt::Instruction>(context, SpvOpAccessChain, result_type,
  160. message_.fresh_id(), operands));
  161. // Conservatively invalidate all analyses.
  162. 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 (fact_manager->PointeeValueIsIrrelevant(message_.pointer_id())) {
  166. fact_manager->AddFactValueOfPointeeIsIrrelevant(message_.fresh_id());
  167. }
  168. }
  169. protobufs::Transformation TransformationAccessChain::ToMessage() const {
  170. protobufs::Transformation result;
  171. *result.mutable_access_chain() = message_;
  172. return result;
  173. }
  174. std::pair<bool, uint32_t> TransformationAccessChain::GetIndexValue(
  175. opt::IRContext* context, uint32_t index_id) const {
  176. auto index_instruction = context->get_def_use_mgr()->GetDef(index_id);
  177. if (!index_instruction || !spvOpcodeIsConstant(index_instruction->opcode())) {
  178. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3179) We could
  179. // allow non-constant indices when looking up non-structs, using clamping
  180. // to ensure they are in-bounds.
  181. return {false, 0};
  182. }
  183. auto index_type =
  184. context->get_def_use_mgr()->GetDef(index_instruction->type_id());
  185. if (index_type->opcode() != SpvOpTypeInt ||
  186. index_type->GetSingleWordInOperand(0) != 32) {
  187. return {false, 0};
  188. }
  189. return {true, index_instruction->GetSingleWordInOperand(0)};
  190. }
  191. } // namespace fuzz
  192. } // namespace spvtools