transformation_composite_construct.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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* ir_context,
  38. const TransformationContext& transformation_context) const {
  39. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  40. // We require the id for the composite constructor to be unused.
  41. return false;
  42. }
  43. auto insert_before =
  44. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  45. if (!insert_before) {
  46. // The instruction before which the composite should be inserted was not
  47. // found.
  48. return false;
  49. }
  50. auto composite_type =
  51. ir_context->get_type_mgr()->GetType(message_.composite_type_id());
  52. if (!fuzzerutil::IsCompositeType(composite_type)) {
  53. // The type must actually be a composite.
  54. return false;
  55. }
  56. // If the type is an array, matrix, struct or vector, the components need to
  57. // be suitable for constructing something of that type.
  58. if (composite_type->AsArray() &&
  59. !ComponentsForArrayConstructionAreOK(ir_context,
  60. *composite_type->AsArray())) {
  61. return false;
  62. }
  63. if (composite_type->AsMatrix() &&
  64. !ComponentsForMatrixConstructionAreOK(ir_context,
  65. *composite_type->AsMatrix())) {
  66. return false;
  67. }
  68. if (composite_type->AsStruct() &&
  69. !ComponentsForStructConstructionAreOK(ir_context,
  70. *composite_type->AsStruct())) {
  71. return false;
  72. }
  73. if (composite_type->AsVector() &&
  74. !ComponentsForVectorConstructionAreOK(ir_context,
  75. *composite_type->AsVector())) {
  76. return false;
  77. }
  78. // Now check whether every component being used to initialize the composite is
  79. // available at the desired program point.
  80. for (auto component : message_.component()) {
  81. auto* inst = ir_context->get_def_use_mgr()->GetDef(component);
  82. if (!inst) {
  83. return false;
  84. }
  85. // We should be able to create a synonym of |component| if it's not
  86. // irrelevant.
  87. if (!transformation_context.GetFactManager()->IdIsIrrelevant(component) &&
  88. !fuzzerutil::CanMakeSynonymOf(ir_context, transformation_context,
  89. inst)) {
  90. return false;
  91. }
  92. if (!fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, insert_before,
  93. component)) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. void TransformationCompositeConstruct::Apply(
  100. opt::IRContext* ir_context,
  101. TransformationContext* transformation_context) const {
  102. // Use the base and offset information from the transformation to determine
  103. // where in the module a new instruction should be inserted.
  104. auto insert_before_inst =
  105. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  106. auto destination_block = ir_context->get_instr_block(insert_before_inst);
  107. auto insert_before = fuzzerutil::GetIteratorForInstruction(
  108. destination_block, insert_before_inst);
  109. // Prepare the input operands for an OpCompositeConstruct instruction.
  110. opt::Instruction::OperandList in_operands;
  111. for (auto& component_id : message_.component()) {
  112. in_operands.push_back({SPV_OPERAND_TYPE_ID, {component_id}});
  113. }
  114. // Insert an OpCompositeConstruct instruction.
  115. insert_before.InsertBefore(MakeUnique<opt::Instruction>(
  116. ir_context, SpvOpCompositeConstruct, message_.composite_type_id(),
  117. message_.fresh_id(), in_operands));
  118. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  119. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  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. // except in the case of a vector where a single vector id can span multiple
  123. // components.
  124. auto composite_type =
  125. ir_context->get_type_mgr()->GetType(message_.composite_type_id());
  126. uint32_t index = 0;
  127. for (auto component : message_.component()) {
  128. auto component_type = ir_context->get_type_mgr()->GetType(
  129. ir_context->get_def_use_mgr()->GetDef(component)->type_id());
  130. if (composite_type->AsVector() && component_type->AsVector()) {
  131. // The case where the composite being constructed is a vector and the
  132. // component provided for construction is also a vector is special. It
  133. // requires adding a synonym fact relating each element of the sub-vector
  134. // to the corresponding element of the composite being constructed.
  135. assert(component_type->AsVector()->element_type() ==
  136. composite_type->AsVector()->element_type());
  137. assert(component_type->AsVector()->element_count() <
  138. composite_type->AsVector()->element_count());
  139. for (uint32_t subvector_index = 0;
  140. subvector_index < component_type->AsVector()->element_count();
  141. subvector_index++) {
  142. if (!transformation_context->GetFactManager()->IdIsIrrelevant(
  143. component)) {
  144. transformation_context->GetFactManager()->AddFactDataSynonym(
  145. MakeDataDescriptor(component, {subvector_index}),
  146. MakeDataDescriptor(message_.fresh_id(), {index}), ir_context);
  147. }
  148. index++;
  149. }
  150. } else {
  151. // The other cases are simple: the component is made directly synonymous
  152. // with the element of the composite being constructed.
  153. if (!transformation_context->GetFactManager()->IdIsIrrelevant(
  154. component)) {
  155. transformation_context->GetFactManager()->AddFactDataSynonym(
  156. MakeDataDescriptor(component, {}),
  157. MakeDataDescriptor(message_.fresh_id(), {index}), ir_context);
  158. }
  159. index++;
  160. }
  161. }
  162. }
  163. bool TransformationCompositeConstruct::ComponentsForArrayConstructionAreOK(
  164. opt::IRContext* ir_context, const opt::analysis::Array& array_type) const {
  165. if (array_type.length_info().words[0] !=
  166. opt::analysis::Array::LengthInfo::kConstant) {
  167. // We only handle constant-sized arrays.
  168. return false;
  169. }
  170. if (array_type.length_info().words.size() != 2) {
  171. // We only handle the case where the array size can be captured in a single
  172. // word.
  173. return false;
  174. }
  175. // Get the array size.
  176. auto array_size = array_type.length_info().words[1];
  177. if (static_cast<uint32_t>(message_.component().size()) != array_size) {
  178. // The number of components must match the array size.
  179. return false;
  180. }
  181. // Check that each component is the result id of an instruction whose type is
  182. // the array's element type.
  183. for (auto component_id : message_.component()) {
  184. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  185. if (inst == nullptr || !inst->type_id()) {
  186. // The component does not correspond to an instruction with a result
  187. // type.
  188. return false;
  189. }
  190. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  191. assert(component_type);
  192. if (component_type != array_type.element_type()) {
  193. // The component's type does not match the array's element type.
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. bool TransformationCompositeConstruct::ComponentsForMatrixConstructionAreOK(
  200. opt::IRContext* ir_context,
  201. const opt::analysis::Matrix& matrix_type) const {
  202. if (static_cast<uint32_t>(message_.component().size()) !=
  203. matrix_type.element_count()) {
  204. // The number of components must match the number of columns of the matrix.
  205. return false;
  206. }
  207. // Check that each component is the result id of an instruction whose type is
  208. // the matrix's column type.
  209. for (auto component_id : message_.component()) {
  210. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  211. if (inst == nullptr || !inst->type_id()) {
  212. // The component does not correspond to an instruction with a result
  213. // type.
  214. return false;
  215. }
  216. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  217. assert(component_type);
  218. if (component_type != matrix_type.element_type()) {
  219. // The component's type does not match the matrix's column type.
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. bool TransformationCompositeConstruct::ComponentsForStructConstructionAreOK(
  226. opt::IRContext* ir_context,
  227. const opt::analysis::Struct& struct_type) const {
  228. if (static_cast<uint32_t>(message_.component().size()) !=
  229. struct_type.element_types().size()) {
  230. // The number of components must match the number of fields of the struct.
  231. return false;
  232. }
  233. // Check that each component is the result id of an instruction those type
  234. // matches the associated field type.
  235. for (uint32_t field_index = 0;
  236. field_index < struct_type.element_types().size(); field_index++) {
  237. auto inst = ir_context->get_def_use_mgr()->GetDef(
  238. message_.component()[field_index]);
  239. if (inst == nullptr || !inst->type_id()) {
  240. // The component does not correspond to an instruction with a result
  241. // type.
  242. return false;
  243. }
  244. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  245. assert(component_type);
  246. if (component_type != struct_type.element_types()[field_index]) {
  247. // The component's type does not match the corresponding field type.
  248. return false;
  249. }
  250. }
  251. return true;
  252. }
  253. bool TransformationCompositeConstruct::ComponentsForVectorConstructionAreOK(
  254. opt::IRContext* ir_context,
  255. const opt::analysis::Vector& vector_type) const {
  256. uint32_t base_element_count = 0;
  257. auto element_type = vector_type.element_type();
  258. for (auto& component_id : message_.component()) {
  259. auto inst = ir_context->get_def_use_mgr()->GetDef(component_id);
  260. if (inst == nullptr || !inst->type_id()) {
  261. // The component does not correspond to an instruction with a result
  262. // type.
  263. return false;
  264. }
  265. auto component_type = ir_context->get_type_mgr()->GetType(inst->type_id());
  266. assert(component_type);
  267. if (component_type == element_type) {
  268. base_element_count++;
  269. } else if (component_type->AsVector() &&
  270. component_type->AsVector()->element_type() == element_type) {
  271. base_element_count += component_type->AsVector()->element_count();
  272. } else {
  273. // The component was not appropriate; e.g. no type corresponding to the
  274. // given id was found, or the type that was found was not compatible
  275. // with the vector being constructed.
  276. return false;
  277. }
  278. }
  279. // The number of components provided (when vector components are flattened
  280. // out) needs to match the length of the vector being constructed.
  281. return base_element_count == vector_type.element_count();
  282. }
  283. protobufs::Transformation TransformationCompositeConstruct::ToMessage() const {
  284. protobufs::Transformation result;
  285. *result.mutable_composite_construct() = message_;
  286. return result;
  287. }
  288. } // namespace fuzz
  289. } // namespace spvtools