transformation_composite_construct.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. protobufs::TransformationCompositeConstruct message)
  23. : message_(std::move(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* ir_context, const TransformationContext& /*unused*/) const {
  38. if (!fuzzerutil::IsFreshId(ir_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(), ir_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. ir_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() &&
  58. !ComponentsForArrayConstructionAreOK(ir_context,
  59. *composite_type->AsArray())) {
  60. return false;
  61. }
  62. if (composite_type->AsMatrix() &&
  63. !ComponentsForMatrixConstructionAreOK(ir_context,
  64. *composite_type->AsMatrix())) {
  65. return false;
  66. }
  67. if (composite_type->AsStruct() &&
  68. !ComponentsForStructConstructionAreOK(ir_context,
  69. *composite_type->AsStruct())) {
  70. return false;
  71. }
  72. if (composite_type->AsVector() &&
  73. !ComponentsForVectorConstructionAreOK(ir_context,
  74. *composite_type->AsVector())) {
  75. return false;
  76. }
  77. // Now check whether every component being used to initialize the composite is
  78. // available at the desired program point.
  79. for (auto component : message_.component()) {
  80. auto* inst = ir_context->get_def_use_mgr()->GetDef(component);
  81. if (!inst) {
  82. return false;
  83. }
  84. if (!fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, insert_before,
  85. component)) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. void TransformationCompositeConstruct::Apply(
  92. opt::IRContext* ir_context,
  93. TransformationContext* transformation_context) const {
  94. // Use the base and offset information from the transformation to determine
  95. // where in the module a new instruction should be inserted.
  96. auto insert_before_inst =
  97. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  98. auto destination_block = ir_context->get_instr_block(insert_before_inst);
  99. auto insert_before = fuzzerutil::GetIteratorForInstruction(
  100. destination_block, insert_before_inst);
  101. // Prepare the input operands for an OpCompositeConstruct instruction.
  102. opt::Instruction::OperandList in_operands;
  103. for (auto& component_id : message_.component()) {
  104. in_operands.push_back({SPV_OPERAND_TYPE_ID, {component_id}});
  105. }
  106. // Insert an OpCompositeConstruct instruction.
  107. auto new_instruction = MakeUnique<opt::Instruction>(
  108. ir_context, spv::Op::OpCompositeConstruct, message_.composite_type_id(),
  109. message_.fresh_id(), in_operands);
  110. auto new_instruction_ptr = new_instruction.get();
  111. insert_before.InsertBefore(std::move(new_instruction));
  112. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
  113. ir_context->set_instr_block(new_instruction_ptr, destination_block);
  114. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  115. // No analyses need to be invalidated since the transformation is local to a
  116. // block and the def-use and instruction-to-block mappings have been updated.
  117. AddDataSynonymFacts(ir_context, transformation_context);
  118. }
  119. bool TransformationCompositeConstruct::ComponentsForArrayConstructionAreOK(
  120. opt::IRContext* ir_context, const opt::analysis::Array& array_type) const {
  121. if (array_type.length_info().words[0] !=
  122. opt::analysis::Array::LengthInfo::kConstant) {
  123. // We only handle constant-sized arrays.
  124. return false;
  125. }
  126. if (array_type.length_info().words.size() != 2) {
  127. // We only handle the case where the array size can be captured in a single
  128. // word.
  129. return false;
  130. }
  131. // Get the array size.
  132. auto array_size = array_type.length_info().words[1];
  133. if (static_cast<uint32_t>(message_.component().size()) != array_size) {
  134. // The number of components must match the array size.
  135. return false;
  136. }
  137. // Check that each component is the result id of an instruction whose type is
  138. // the array's element type.
  139. for (auto component_id : message_.component()) {
  140. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  141. if (inst == nullptr || !inst->type_id()) {
  142. // The component does not correspond to an instruction with a result
  143. // type.
  144. return false;
  145. }
  146. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  147. assert(component_type);
  148. if (component_type != array_type.element_type()) {
  149. // The component's type does not match the array's element type.
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. bool TransformationCompositeConstruct::ComponentsForMatrixConstructionAreOK(
  156. opt::IRContext* ir_context,
  157. const opt::analysis::Matrix& matrix_type) const {
  158. if (static_cast<uint32_t>(message_.component().size()) !=
  159. matrix_type.element_count()) {
  160. // The number of components must match the number of columns of the matrix.
  161. return false;
  162. }
  163. // Check that each component is the result id of an instruction whose type is
  164. // the matrix's column type.
  165. for (auto component_id : message_.component()) {
  166. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  167. if (inst == nullptr || !inst->type_id()) {
  168. // The component does not correspond to an instruction with a result
  169. // type.
  170. return false;
  171. }
  172. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  173. assert(component_type);
  174. if (component_type != matrix_type.element_type()) {
  175. // The component's type does not match the matrix's column type.
  176. return false;
  177. }
  178. }
  179. return true;
  180. }
  181. bool TransformationCompositeConstruct::ComponentsForStructConstructionAreOK(
  182. opt::IRContext* ir_context,
  183. const opt::analysis::Struct& struct_type) const {
  184. if (static_cast<uint32_t>(message_.component().size()) !=
  185. struct_type.element_types().size()) {
  186. // The number of components must match the number of fields of the struct.
  187. return false;
  188. }
  189. // Check that each component is the result id of an instruction those type
  190. // matches the associated field type.
  191. for (uint32_t field_index = 0;
  192. field_index < struct_type.element_types().size(); field_index++) {
  193. auto inst = ir_context->get_def_use_mgr()->GetDef(
  194. message_.component()[field_index]);
  195. if (inst == nullptr || !inst->type_id()) {
  196. // The component does not correspond to an instruction with a result
  197. // type.
  198. return false;
  199. }
  200. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  201. assert(component_type);
  202. if (component_type != struct_type.element_types()[field_index]) {
  203. // The component's type does not match the corresponding field type.
  204. return false;
  205. }
  206. }
  207. return true;
  208. }
  209. bool TransformationCompositeConstruct::ComponentsForVectorConstructionAreOK(
  210. opt::IRContext* ir_context,
  211. const opt::analysis::Vector& vector_type) const {
  212. uint32_t base_element_count = 0;
  213. auto element_type = vector_type.element_type();
  214. for (auto& component_id : message_.component()) {
  215. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  216. if (inst == nullptr || !inst->type_id()) {
  217. // The component does not correspond to an instruction with a result
  218. // type.
  219. return false;
  220. }
  221. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  222. assert(component_type);
  223. if (component_type == element_type) {
  224. base_element_count++;
  225. } else if (component_type->AsVector() &&
  226. component_type->AsVector()->element_type() == element_type) {
  227. base_element_count += component_type->AsVector()->element_count();
  228. } else {
  229. // The component was not appropriate; e.g. no type corresponding to the
  230. // given id was found, or the type that was found was not compatible
  231. // with the vector being constructed.
  232. return false;
  233. }
  234. }
  235. // The number of components provided (when vector components are flattened
  236. // out) needs to match the length of the vector being constructed.
  237. return base_element_count == vector_type.element_count();
  238. }
  239. protobufs::Transformation TransformationCompositeConstruct::ToMessage() const {
  240. protobufs::Transformation result;
  241. *result.mutable_composite_construct() = message_;
  242. return result;
  243. }
  244. std::unordered_set<uint32_t> TransformationCompositeConstruct::GetFreshIds()
  245. const {
  246. return {message_.fresh_id()};
  247. }
  248. void TransformationCompositeConstruct::AddDataSynonymFacts(
  249. opt::IRContext* ir_context,
  250. TransformationContext* transformation_context) const {
  251. // If the result id of the composite we are constructing is irrelevant (e.g.
  252. // because it is in a dead block) then we do not make any synonyms.
  253. if (transformation_context->GetFactManager()->IdIsIrrelevant(
  254. message_.fresh_id())) {
  255. return;
  256. }
  257. // Inform the fact manager that we now have new synonyms: every component of
  258. // the composite is synonymous with the id used to construct that component
  259. // (so long as it is legitimate to create a synonym from that id), except in
  260. // the case of a vector where a single vector id can span multiple components.
  261. auto composite_type =
  262. ir_context->get_type_mgr()->GetType(message_.composite_type_id());
  263. uint32_t index = 0;
  264. for (auto component : message_.component()) {
  265. auto component_type = ir_context->get_type_mgr()->GetType(
  266. ir_context->get_def_use_mgr()->GetDef(component)->type_id());
  267. // Whether the component is a vector being packed into a vector determines
  268. // how we should keep track of the indices associated with components.
  269. const bool packing_vector_into_vector =
  270. composite_type->AsVector() && component_type->AsVector();
  271. if (!fuzzerutil::CanMakeSynonymOf(
  272. ir_context, *transformation_context,
  273. *ir_context->get_def_use_mgr()->GetDef(component))) {
  274. // We can't make a synonym of this component, so we skip on to the next
  275. // component. In the case where we're packing a vector into a vector we
  276. // have to skip as many components of the resulting vectors as there are
  277. // elements of the component vector.
  278. index += packing_vector_into_vector
  279. ? component_type->AsVector()->element_count()
  280. : 1;
  281. continue;
  282. }
  283. if (packing_vector_into_vector) {
  284. // The case where the composite being constructed is a vector and the
  285. // component provided for construction is also a vector is special. It
  286. // requires adding a synonym fact relating each element of the sub-vector
  287. // to the corresponding element of the composite being constructed.
  288. assert(component_type->AsVector()->element_type() ==
  289. composite_type->AsVector()->element_type());
  290. assert(component_type->AsVector()->element_count() <
  291. composite_type->AsVector()->element_count());
  292. for (uint32_t subvector_index = 0;
  293. subvector_index < component_type->AsVector()->element_count();
  294. subvector_index++) {
  295. transformation_context->GetFactManager()->AddFactDataSynonym(
  296. MakeDataDescriptor(component, {subvector_index}),
  297. MakeDataDescriptor(message_.fresh_id(), {index}));
  298. index++;
  299. }
  300. } else {
  301. // The other cases are simple: the component is made directly synonymous
  302. // with the element of the composite being constructed.
  303. transformation_context->GetFactManager()->AddFactDataSynonym(
  304. MakeDataDescriptor(component, {}),
  305. MakeDataDescriptor(message_.fresh_id(), {index}));
  306. index++;
  307. }
  308. }
  309. }
  310. } // namespace fuzz
  311. } // namespace spvtools