transformation_composite_construct.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright (c) 2019 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_composite_construct.h"
  15. #include "source/fuzz/data_descriptor.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/opt/instruction.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. TransformationCompositeConstruct::TransformationCompositeConstruct(
  22. const protobufs::TransformationCompositeConstruct& message)
  23. : message_(message) {}
  24. TransformationCompositeConstruct::TransformationCompositeConstruct(
  25. uint32_t composite_type_id, std::vector<uint32_t> component,
  26. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  27. uint32_t fresh_id) {
  28. message_.set_composite_type_id(composite_type_id);
  29. for (auto a_component : component) {
  30. message_.add_component(a_component);
  31. }
  32. *message_.mutable_instruction_to_insert_before() =
  33. instruction_to_insert_before;
  34. message_.set_fresh_id(fresh_id);
  35. }
  36. bool TransformationCompositeConstruct::IsApplicable(
  37. opt::IRContext* context, const FactManager& /*fact_manager*/) const {
  38. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  39. // We require the id for the composite constructor to be unused.
  40. return false;
  41. }
  42. auto insert_before =
  43. FindInstruction(message_.instruction_to_insert_before(), context);
  44. if (!insert_before) {
  45. // The instruction before which the composite should be inserted was not
  46. // found.
  47. return false;
  48. }
  49. auto composite_type =
  50. context->get_type_mgr()->GetType(message_.composite_type_id());
  51. if (!fuzzerutil::IsCompositeType(composite_type)) {
  52. // The type must actually be a composite.
  53. return false;
  54. }
  55. // If the type is an array, matrix, struct or vector, the components need to
  56. // be suitable for constructing something of that type.
  57. if (composite_type->AsArray() && !ComponentsForArrayConstructionAreOK(
  58. context, *composite_type->AsArray())) {
  59. return false;
  60. }
  61. if (composite_type->AsMatrix() && !ComponentsForMatrixConstructionAreOK(
  62. context, *composite_type->AsMatrix())) {
  63. return false;
  64. }
  65. if (composite_type->AsStruct() && !ComponentsForStructConstructionAreOK(
  66. context, *composite_type->AsStruct())) {
  67. return false;
  68. }
  69. if (composite_type->AsVector() && !ComponentsForVectorConstructionAreOK(
  70. context, *composite_type->AsVector())) {
  71. return false;
  72. }
  73. // Now check whether every component being used to initialize the composite is
  74. // available at the desired program point.
  75. for (auto& component : message_.component()) {
  76. auto component_inst = context->get_def_use_mgr()->GetDef(component);
  77. if (!context->get_instr_block(component)) {
  78. // The component does not have a block; that means it is in global scope,
  79. // which is OK. (Whether the component actually corresponds to an
  80. // instruction is checked above when determining whether types are
  81. // suitable.)
  82. continue;
  83. }
  84. // Check whether the component is available.
  85. if (insert_before->HasResultId() &&
  86. insert_before->result_id() == component) {
  87. // This constitutes trying to use an id right before it is defined. The
  88. // special case is needed due to an instruction always dominating itself.
  89. return false;
  90. }
  91. if (!context
  92. ->GetDominatorAnalysis(
  93. context->get_instr_block(&*insert_before)->GetParent())
  94. ->Dominates(component_inst, &*insert_before)) {
  95. // The instruction defining the component must dominate the instruction we
  96. // wish to insert the composite before.
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. void TransformationCompositeConstruct::Apply(opt::IRContext* context,
  103. FactManager* fact_manager) const {
  104. // Use the base and offset information from the transformation to determine
  105. // where in the module a new instruction should be inserted.
  106. auto insert_before_inst =
  107. FindInstruction(message_.instruction_to_insert_before(), context);
  108. auto destination_block = context->get_instr_block(insert_before_inst);
  109. auto insert_before = fuzzerutil::GetIteratorForInstruction(
  110. destination_block, insert_before_inst);
  111. // Prepare the input operands for an OpCompositeConstruct instruction.
  112. opt::Instruction::OperandList in_operands;
  113. for (auto& component_id : message_.component()) {
  114. in_operands.push_back({SPV_OPERAND_TYPE_ID, {component_id}});
  115. }
  116. // Insert an OpCompositeConstruct instruction.
  117. insert_before.InsertBefore(MakeUnique<opt::Instruction>(
  118. context, SpvOpCompositeConstruct, message_.composite_type_id(),
  119. message_.fresh_id(), in_operands));
  120. // Inform the fact manager that we now have new synonyms: every component of
  121. // the composite is synonymous with the id used to construct that component.
  122. auto composite_type =
  123. context->get_type_mgr()->GetType(message_.composite_type_id());
  124. uint32_t index = 0;
  125. for (auto component : message_.component()) {
  126. // Decide how many contiguous composite elements are represented by the
  127. // components. This is always 1, except in the case of a vector that is
  128. // constructed by smaller vectors.
  129. uint32_t num_contiguous_elements;
  130. if (composite_type->AsVector()) {
  131. // The vector case is a bit fiddly, because one argument to a vector
  132. // constructor can cover more than one element.
  133. auto component_type = context->get_type_mgr()->GetType(
  134. context->get_def_use_mgr()->GetDef(component)->type_id());
  135. if (component_type->AsVector()) {
  136. assert(component_type->AsVector()->element_type() ==
  137. composite_type->AsVector()->element_type());
  138. num_contiguous_elements = component_type->AsVector()->element_count();
  139. } else {
  140. assert(component_type == composite_type->AsVector()->element_type());
  141. num_contiguous_elements = 1;
  142. }
  143. } else {
  144. // The non-vector cases are all easy: the constructor has exactly the same
  145. // number of arguments as the number of sub-components, so we can just
  146. // increment the index.
  147. num_contiguous_elements = 1;
  148. }
  149. fact_manager->AddFactDataSynonym(
  150. MakeDataDescriptor(component, {}, 1),
  151. MakeDataDescriptor(message_.fresh_id(), {index},
  152. num_contiguous_elements));
  153. index += num_contiguous_elements;
  154. }
  155. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  156. context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  157. }
  158. bool TransformationCompositeConstruct::ComponentsForArrayConstructionAreOK(
  159. opt::IRContext* context, const opt::analysis::Array& array_type) const {
  160. if (array_type.length_info().words[0] !=
  161. opt::analysis::Array::LengthInfo::kConstant) {
  162. // We only handle constant-sized arrays.
  163. return false;
  164. }
  165. if (array_type.length_info().words.size() != 2) {
  166. // We only handle the case where the array size can be captured in a single
  167. // word.
  168. return false;
  169. }
  170. // Get the array size.
  171. auto array_size = array_type.length_info().words[1];
  172. if (static_cast<uint32_t>(message_.component().size()) != array_size) {
  173. // The number of components must match the array size.
  174. return false;
  175. }
  176. // Check that each component is the result id of an instruction whose type is
  177. // the array's element type.
  178. for (auto component_id : message_.component()) {
  179. auto inst = context->get_def_use_mgr()->GetDef(component_id);
  180. if (inst == nullptr || !inst->type_id()) {
  181. // The component does not correspond to an instruction with a result
  182. // type.
  183. return false;
  184. }
  185. auto component_type = context->get_type_mgr()->GetType(inst->type_id());
  186. assert(component_type);
  187. if (component_type != array_type.element_type()) {
  188. // The component's type does not match the array's element type.
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. bool TransformationCompositeConstruct::ComponentsForMatrixConstructionAreOK(
  195. opt::IRContext* context, const opt::analysis::Matrix& matrix_type) const {
  196. if (static_cast<uint32_t>(message_.component().size()) !=
  197. matrix_type.element_count()) {
  198. // The number of components must match the number of columns of the matrix.
  199. return false;
  200. }
  201. // Check that each component is the result id of an instruction whose type is
  202. // the matrix's column type.
  203. for (auto component_id : message_.component()) {
  204. auto inst = context->get_def_use_mgr()->GetDef(component_id);
  205. if (inst == nullptr || !inst->type_id()) {
  206. // The component does not correspond to an instruction with a result
  207. // type.
  208. return false;
  209. }
  210. auto component_type = context->get_type_mgr()->GetType(inst->type_id());
  211. assert(component_type);
  212. if (component_type != matrix_type.element_type()) {
  213. // The component's type does not match the matrix's column type.
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. bool TransformationCompositeConstruct::ComponentsForStructConstructionAreOK(
  220. opt::IRContext* context, const opt::analysis::Struct& struct_type) const {
  221. if (static_cast<uint32_t>(message_.component().size()) !=
  222. struct_type.element_types().size()) {
  223. // The number of components must match the number of fields of the struct.
  224. return false;
  225. }
  226. // Check that each component is the result id of an instruction those type
  227. // matches the associated field type.
  228. for (uint32_t field_index = 0;
  229. field_index < struct_type.element_types().size(); field_index++) {
  230. auto inst =
  231. context->get_def_use_mgr()->GetDef(message_.component()[field_index]);
  232. if (inst == nullptr || !inst->type_id()) {
  233. // The component does not correspond to an instruction with a result
  234. // type.
  235. return false;
  236. }
  237. auto component_type = context->get_type_mgr()->GetType(inst->type_id());
  238. assert(component_type);
  239. if (component_type != struct_type.element_types()[field_index]) {
  240. // The component's type does not match the corresponding field type.
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. bool TransformationCompositeConstruct::ComponentsForVectorConstructionAreOK(
  247. opt::IRContext* context, const opt::analysis::Vector& vector_type) const {
  248. uint32_t base_element_count = 0;
  249. auto element_type = vector_type.element_type();
  250. for (auto& component_id : message_.component()) {
  251. auto inst = context->get_def_use_mgr()->GetDef(component_id);
  252. if (inst == nullptr || !inst->type_id()) {
  253. // The component does not correspond to an instruction with a result
  254. // type.
  255. return false;
  256. }
  257. auto component_type = context->get_type_mgr()->GetType(inst->type_id());
  258. assert(component_type);
  259. if (component_type == element_type) {
  260. base_element_count++;
  261. } else if (component_type->AsVector() &&
  262. component_type->AsVector()->element_type() == element_type) {
  263. base_element_count += component_type->AsVector()->element_count();
  264. } else {
  265. // The component was not appropriate; e.g. no type corresponding to the
  266. // given id was found, or the type that was found was not compatible
  267. // with the vector being constructed.
  268. return false;
  269. }
  270. }
  271. // The number of components provided (when vector components are flattened
  272. // out) needs to match the length of the vector being constructed.
  273. return base_element_count == vector_type.element_count();
  274. }
  275. protobufs::Transformation TransformationCompositeConstruct::ToMessage() const {
  276. protobufs::Transformation result;
  277. *result.mutable_composite_construct() = message_;
  278. return result;
  279. }
  280. } // namespace fuzz
  281. } // namespace spvtools