fuzzer_pass.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/fuzzer_pass.h"
  15. #include <set>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_add_constant_boolean.h"
  19. #include "source/fuzz/transformation_add_constant_composite.h"
  20. #include "source/fuzz/transformation_add_constant_null.h"
  21. #include "source/fuzz/transformation_add_constant_scalar.h"
  22. #include "source/fuzz/transformation_add_global_undef.h"
  23. #include "source/fuzz/transformation_add_type_boolean.h"
  24. #include "source/fuzz/transformation_add_type_float.h"
  25. #include "source/fuzz/transformation_add_type_function.h"
  26. #include "source/fuzz/transformation_add_type_int.h"
  27. #include "source/fuzz/transformation_add_type_matrix.h"
  28. #include "source/fuzz/transformation_add_type_pointer.h"
  29. #include "source/fuzz/transformation_add_type_struct.h"
  30. #include "source/fuzz/transformation_add_type_vector.h"
  31. namespace spvtools {
  32. namespace fuzz {
  33. FuzzerPass::FuzzerPass(opt::IRContext* ir_context,
  34. TransformationContext* transformation_context,
  35. FuzzerContext* fuzzer_context,
  36. protobufs::TransformationSequence* transformations)
  37. : ir_context_(ir_context),
  38. transformation_context_(transformation_context),
  39. fuzzer_context_(fuzzer_context),
  40. transformations_(transformations) {}
  41. FuzzerPass::~FuzzerPass() = default;
  42. std::vector<opt::Instruction*> FuzzerPass::FindAvailableInstructions(
  43. opt::Function* function, opt::BasicBlock* block,
  44. const opt::BasicBlock::iterator& inst_it,
  45. std::function<bool(opt::IRContext*, opt::Instruction*)>
  46. instruction_is_relevant) const {
  47. // TODO(afd) The following is (relatively) simple, but may end up being
  48. // prohibitively inefficient, as it walks the whole dominator tree for
  49. // every instruction that is considered.
  50. std::vector<opt::Instruction*> result;
  51. // Consider all global declarations
  52. for (auto& global : GetIRContext()->module()->types_values()) {
  53. if (instruction_is_relevant(GetIRContext(), &global)) {
  54. result.push_back(&global);
  55. }
  56. }
  57. // Consider all function parameters
  58. function->ForEachParam(
  59. [this, &instruction_is_relevant, &result](opt::Instruction* param) {
  60. if (instruction_is_relevant(GetIRContext(), param)) {
  61. result.push_back(param);
  62. }
  63. });
  64. // Consider all previous instructions in this block
  65. for (auto prev_inst_it = block->begin(); prev_inst_it != inst_it;
  66. ++prev_inst_it) {
  67. if (instruction_is_relevant(GetIRContext(), &*prev_inst_it)) {
  68. result.push_back(&*prev_inst_it);
  69. }
  70. }
  71. // Walk the dominator tree to consider all instructions from dominating
  72. // blocks
  73. auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(function);
  74. for (auto next_dominator = dominator_analysis->ImmediateDominator(block);
  75. next_dominator != nullptr;
  76. next_dominator =
  77. dominator_analysis->ImmediateDominator(next_dominator)) {
  78. for (auto& dominating_inst : *next_dominator) {
  79. if (instruction_is_relevant(GetIRContext(), &dominating_inst)) {
  80. result.push_back(&dominating_inst);
  81. }
  82. }
  83. }
  84. return result;
  85. }
  86. void FuzzerPass::ForEachInstructionWithInstructionDescriptor(
  87. std::function<
  88. void(opt::Function* function, opt::BasicBlock* block,
  89. opt::BasicBlock::iterator inst_it,
  90. const protobufs::InstructionDescriptor& instruction_descriptor)>
  91. action) {
  92. // Consider every block in every function.
  93. for (auto& function : *GetIRContext()->module()) {
  94. for (auto& block : function) {
  95. // We now consider every instruction in the block, randomly deciding
  96. // whether to apply a transformation before it.
  97. // In order for transformations to insert new instructions, they need to
  98. // be able to identify the instruction to insert before. We describe an
  99. // instruction via its opcode, 'opc', a base instruction 'base' that has a
  100. // result id, and the number of instructions with opcode 'opc' that we
  101. // should skip when searching from 'base' for the desired instruction.
  102. // (An instruction that has a result id is represented by its own opcode,
  103. // itself as 'base', and a skip-count of 0.)
  104. std::vector<std::tuple<uint32_t, SpvOp, uint32_t>>
  105. base_opcode_skip_triples;
  106. // The initial base instruction is the block label.
  107. uint32_t base = block.id();
  108. // Counts the number of times we have seen each opcode since we reset the
  109. // base instruction.
  110. std::map<SpvOp, uint32_t> skip_count;
  111. // Consider every instruction in the block. The label is excluded: it is
  112. // only necessary to consider it as a base in case the first instruction
  113. // in the block does not have a result id.
  114. for (auto inst_it = block.begin(); inst_it != block.end(); ++inst_it) {
  115. if (inst_it->HasResultId()) {
  116. // In the case that the instruction has a result id, we use the
  117. // instruction as its own base, and clear the skip counts we have
  118. // collected.
  119. base = inst_it->result_id();
  120. skip_count.clear();
  121. }
  122. const SpvOp opcode = inst_it->opcode();
  123. // Invoke the provided function, which might apply a transformation.
  124. action(&function, &block, inst_it,
  125. MakeInstructionDescriptor(
  126. base, opcode,
  127. skip_count.count(opcode) ? skip_count.at(opcode) : 0));
  128. if (!inst_it->HasResultId()) {
  129. skip_count[opcode] =
  130. skip_count.count(opcode) ? skip_count.at(opcode) + 1 : 1;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. uint32_t FuzzerPass::FindOrCreateBoolType() {
  137. if (auto existing_id = fuzzerutil::MaybeGetBoolType(GetIRContext())) {
  138. return existing_id;
  139. }
  140. auto result = GetFuzzerContext()->GetFreshId();
  141. ApplyTransformation(TransformationAddTypeBoolean(result));
  142. return result;
  143. }
  144. uint32_t FuzzerPass::FindOrCreateIntegerType(uint32_t width, bool is_signed) {
  145. opt::analysis::Integer int_type(width, is_signed);
  146. auto existing_id = GetIRContext()->get_type_mgr()->GetId(&int_type);
  147. if (existing_id) {
  148. return existing_id;
  149. }
  150. auto result = GetFuzzerContext()->GetFreshId();
  151. ApplyTransformation(TransformationAddTypeInt(result, width, is_signed));
  152. return result;
  153. }
  154. uint32_t FuzzerPass::FindOrCreateFloatType(uint32_t width) {
  155. opt::analysis::Float float_type(width);
  156. auto existing_id = GetIRContext()->get_type_mgr()->GetId(&float_type);
  157. if (existing_id) {
  158. return existing_id;
  159. }
  160. auto result = GetFuzzerContext()->GetFreshId();
  161. ApplyTransformation(TransformationAddTypeFloat(result, width));
  162. return result;
  163. }
  164. uint32_t FuzzerPass::FindOrCreateFunctionType(
  165. uint32_t return_type_id, const std::vector<uint32_t>& argument_id) {
  166. // FindFunctionType has a sigle argument for OpTypeFunction operands
  167. // so we will have to copy them all in this vector
  168. std::vector<uint32_t> type_ids(argument_id.size() + 1);
  169. type_ids[0] = return_type_id;
  170. std::copy(argument_id.begin(), argument_id.end(), type_ids.begin() + 1);
  171. // Check if type exists
  172. auto existing_id = fuzzerutil::FindFunctionType(GetIRContext(), type_ids);
  173. if (existing_id) {
  174. return existing_id;
  175. }
  176. auto result = GetFuzzerContext()->GetFreshId();
  177. ApplyTransformation(
  178. TransformationAddTypeFunction(result, return_type_id, argument_id));
  179. return result;
  180. }
  181. uint32_t FuzzerPass::FindOrCreateVectorType(uint32_t component_type_id,
  182. uint32_t component_count) {
  183. assert(component_count >= 2 && component_count <= 4 &&
  184. "Precondition: component count must be in range [2, 4].");
  185. opt::analysis::Type* component_type =
  186. GetIRContext()->get_type_mgr()->GetType(component_type_id);
  187. assert(component_type && "Precondition: the component type must exist.");
  188. opt::analysis::Vector vector_type(component_type, component_count);
  189. auto existing_id = GetIRContext()->get_type_mgr()->GetId(&vector_type);
  190. if (existing_id) {
  191. return existing_id;
  192. }
  193. auto result = GetFuzzerContext()->GetFreshId();
  194. ApplyTransformation(
  195. TransformationAddTypeVector(result, component_type_id, component_count));
  196. return result;
  197. }
  198. uint32_t FuzzerPass::FindOrCreateMatrixType(uint32_t column_count,
  199. uint32_t row_count) {
  200. assert(column_count >= 2 && column_count <= 4 &&
  201. "Precondition: column count must be in range [2, 4].");
  202. assert(row_count >= 2 && row_count <= 4 &&
  203. "Precondition: row count must be in range [2, 4].");
  204. uint32_t column_type_id =
  205. FindOrCreateVectorType(FindOrCreateFloatType(32), row_count);
  206. opt::analysis::Type* column_type =
  207. GetIRContext()->get_type_mgr()->GetType(column_type_id);
  208. opt::analysis::Matrix matrix_type(column_type, column_count);
  209. auto existing_id = GetIRContext()->get_type_mgr()->GetId(&matrix_type);
  210. if (existing_id) {
  211. return existing_id;
  212. }
  213. auto result = GetFuzzerContext()->GetFreshId();
  214. ApplyTransformation(
  215. TransformationAddTypeMatrix(result, column_type_id, column_count));
  216. return result;
  217. }
  218. uint32_t FuzzerPass::FindOrCreateStructType(
  219. const std::vector<uint32_t>& component_type_ids) {
  220. if (auto existing_id =
  221. fuzzerutil::MaybeGetStructType(GetIRContext(), component_type_ids)) {
  222. return existing_id;
  223. }
  224. auto new_id = GetFuzzerContext()->GetFreshId();
  225. ApplyTransformation(TransformationAddTypeStruct(new_id, component_type_ids));
  226. return new_id;
  227. }
  228. uint32_t FuzzerPass::FindOrCreatePointerType(uint32_t base_type_id,
  229. SpvStorageClass storage_class) {
  230. // We do not use the type manager here, due to problems related to isomorphic
  231. // but distinct structs not being regarded as different.
  232. auto existing_id = fuzzerutil::MaybeGetPointerType(
  233. GetIRContext(), base_type_id, storage_class);
  234. if (existing_id) {
  235. return existing_id;
  236. }
  237. auto result = GetFuzzerContext()->GetFreshId();
  238. ApplyTransformation(
  239. TransformationAddTypePointer(result, storage_class, base_type_id));
  240. return result;
  241. }
  242. uint32_t FuzzerPass::FindOrCreatePointerToIntegerType(
  243. uint32_t width, bool is_signed, SpvStorageClass storage_class) {
  244. return FindOrCreatePointerType(FindOrCreateIntegerType(width, is_signed),
  245. storage_class);
  246. }
  247. uint32_t FuzzerPass::FindOrCreateIntegerConstant(
  248. const std::vector<uint32_t>& words, uint32_t width, bool is_signed,
  249. bool is_irrelevant) {
  250. auto int_type_id = FindOrCreateIntegerType(width, is_signed);
  251. if (auto constant_id = fuzzerutil::MaybeGetScalarConstant(
  252. GetIRContext(), *GetTransformationContext(), words, int_type_id,
  253. is_irrelevant)) {
  254. return constant_id;
  255. }
  256. auto result = GetFuzzerContext()->GetFreshId();
  257. ApplyTransformation(TransformationAddConstantScalar(result, int_type_id,
  258. words, is_irrelevant));
  259. return result;
  260. }
  261. uint32_t FuzzerPass::FindOrCreateFloatConstant(
  262. const std::vector<uint32_t>& words, uint32_t width, bool is_irrelevant) {
  263. auto float_type_id = FindOrCreateFloatType(width);
  264. opt::analysis::FloatConstant float_constant(
  265. GetIRContext()->get_type_mgr()->GetType(float_type_id)->AsFloat(), words);
  266. if (auto constant_id = fuzzerutil::MaybeGetScalarConstant(
  267. GetIRContext(), *GetTransformationContext(), words, float_type_id,
  268. is_irrelevant)) {
  269. return constant_id;
  270. }
  271. auto result = GetFuzzerContext()->GetFreshId();
  272. ApplyTransformation(TransformationAddConstantScalar(result, float_type_id,
  273. words, is_irrelevant));
  274. return result;
  275. }
  276. uint32_t FuzzerPass::FindOrCreateBoolConstant(bool value, bool is_irrelevant) {
  277. auto bool_type_id = FindOrCreateBoolType();
  278. if (auto constant_id = fuzzerutil::MaybeGetScalarConstant(
  279. GetIRContext(), *GetTransformationContext(), {value ? 1u : 0u},
  280. bool_type_id, is_irrelevant)) {
  281. return constant_id;
  282. }
  283. auto result = GetFuzzerContext()->GetFreshId();
  284. ApplyTransformation(
  285. TransformationAddConstantBoolean(result, value, is_irrelevant));
  286. return result;
  287. }
  288. uint32_t FuzzerPass::FindOrCreateConstant(const std::vector<uint32_t>& words,
  289. uint32_t type_id,
  290. bool is_irrelevant) {
  291. assert(type_id && "Constant's type id can't be 0.");
  292. const auto* type = GetIRContext()->get_type_mgr()->GetType(type_id);
  293. assert(type && "Type does not exist.");
  294. if (type->AsBool()) {
  295. assert(words.size() == 1);
  296. return FindOrCreateBoolConstant(words[0], is_irrelevant);
  297. } else if (const auto* integer = type->AsInteger()) {
  298. return FindOrCreateIntegerConstant(words, integer->width(),
  299. integer->IsSigned(), is_irrelevant);
  300. } else if (const auto* floating = type->AsFloat()) {
  301. return FindOrCreateFloatConstant(words, floating->width(), is_irrelevant);
  302. }
  303. // This assertion will fail in debug build but not in release build
  304. // so we return 0 to make compiler happy.
  305. assert(false && "Constant type is not supported");
  306. return 0;
  307. }
  308. uint32_t FuzzerPass::FindOrCreateCompositeConstant(
  309. const std::vector<uint32_t>& component_ids, uint32_t type_id,
  310. bool is_irrelevant) {
  311. if (auto existing_constant = fuzzerutil::MaybeGetCompositeConstant(
  312. GetIRContext(), *GetTransformationContext(), component_ids, type_id,
  313. is_irrelevant)) {
  314. return existing_constant;
  315. }
  316. uint32_t result = GetFuzzerContext()->GetFreshId();
  317. ApplyTransformation(TransformationAddConstantComposite(
  318. result, type_id, component_ids, is_irrelevant));
  319. return result;
  320. }
  321. uint32_t FuzzerPass::FindOrCreateGlobalUndef(uint32_t type_id) {
  322. for (auto& inst : GetIRContext()->types_values()) {
  323. if (inst.opcode() == SpvOpUndef && inst.type_id() == type_id) {
  324. return inst.result_id();
  325. }
  326. }
  327. auto result = GetFuzzerContext()->GetFreshId();
  328. ApplyTransformation(TransformationAddGlobalUndef(result, type_id));
  329. return result;
  330. }
  331. uint32_t FuzzerPass::FindOrCreateNullConstant(uint32_t type_id) {
  332. // Find existing declaration
  333. opt::analysis::NullConstant null_constant(
  334. GetIRContext()->get_type_mgr()->GetType(type_id));
  335. auto existing_constant =
  336. GetIRContext()->get_constant_mgr()->FindConstant(&null_constant);
  337. // Return if found
  338. if (existing_constant) {
  339. return GetIRContext()
  340. ->get_constant_mgr()
  341. ->GetDefiningInstruction(existing_constant)
  342. ->result_id();
  343. }
  344. // Create new if not found
  345. auto result = GetFuzzerContext()->GetFreshId();
  346. ApplyTransformation(TransformationAddConstantNull(result, type_id));
  347. return result;
  348. }
  349. std::pair<std::vector<uint32_t>, std::map<uint32_t, std::vector<uint32_t>>>
  350. FuzzerPass::GetAvailableBasicTypesAndPointers(
  351. SpvStorageClass storage_class) const {
  352. // Records all of the basic types available in the module.
  353. std::set<uint32_t> basic_types;
  354. // For each basic type, records all the associated pointer types that target
  355. // the basic type and that have |storage_class| as their storage class.
  356. std::map<uint32_t, std::vector<uint32_t>> basic_type_to_pointers;
  357. for (auto& inst : GetIRContext()->types_values()) {
  358. // For each basic type that we come across, record type, and the fact that
  359. // we cannot yet have seen any pointers that use the basic type as its
  360. // pointee type.
  361. //
  362. // For pointer types with basic pointee types, associate the pointer type
  363. // with the basic type.
  364. switch (inst.opcode()) {
  365. case SpvOpTypeBool:
  366. case SpvOpTypeFloat:
  367. case SpvOpTypeInt:
  368. case SpvOpTypeMatrix:
  369. case SpvOpTypeVector:
  370. // These are all basic types.
  371. basic_types.insert(inst.result_id());
  372. basic_type_to_pointers.insert({inst.result_id(), {}});
  373. break;
  374. case SpvOpTypeArray:
  375. // An array type is basic if its base type is basic.
  376. if (basic_types.count(inst.GetSingleWordInOperand(0))) {
  377. basic_types.insert(inst.result_id());
  378. basic_type_to_pointers.insert({inst.result_id(), {}});
  379. }
  380. break;
  381. case SpvOpTypeStruct: {
  382. // A struct type is basic if all of its members are basic.
  383. bool all_members_are_basic_types = true;
  384. for (uint32_t i = 0; i < inst.NumInOperands(); i++) {
  385. if (!basic_types.count(inst.GetSingleWordInOperand(i))) {
  386. all_members_are_basic_types = false;
  387. break;
  388. }
  389. }
  390. if (all_members_are_basic_types) {
  391. basic_types.insert(inst.result_id());
  392. basic_type_to_pointers.insert({inst.result_id(), {}});
  393. }
  394. break;
  395. }
  396. case SpvOpTypePointer: {
  397. // We are interested in the pointer if its pointee type is basic and it
  398. // has the right storage class.
  399. auto pointee_type = inst.GetSingleWordInOperand(1);
  400. if (inst.GetSingleWordInOperand(0) == storage_class &&
  401. basic_types.count(pointee_type)) {
  402. // The pointer has the desired storage class, and its pointee type is
  403. // a basic type, so we are interested in it. Associate it with its
  404. // basic type.
  405. basic_type_to_pointers.at(pointee_type).push_back(inst.result_id());
  406. }
  407. break;
  408. }
  409. default:
  410. break;
  411. }
  412. }
  413. return {{basic_types.begin(), basic_types.end()}, basic_type_to_pointers};
  414. }
  415. uint32_t FuzzerPass::FindOrCreateZeroConstant(
  416. uint32_t scalar_or_composite_type_id, bool is_irrelevant) {
  417. auto type_instruction =
  418. GetIRContext()->get_def_use_mgr()->GetDef(scalar_or_composite_type_id);
  419. assert(type_instruction && "The type instruction must exist.");
  420. switch (type_instruction->opcode()) {
  421. case SpvOpTypeBool:
  422. return FindOrCreateBoolConstant(false, is_irrelevant);
  423. case SpvOpTypeFloat: {
  424. auto width = type_instruction->GetSingleWordInOperand(0);
  425. auto num_words = (width + 32 - 1) / 32;
  426. return FindOrCreateFloatConstant(std::vector<uint32_t>(num_words, 0),
  427. width, is_irrelevant);
  428. }
  429. case SpvOpTypeInt: {
  430. auto width = type_instruction->GetSingleWordInOperand(0);
  431. auto num_words = (width + 32 - 1) / 32;
  432. return FindOrCreateIntegerConstant(
  433. std::vector<uint32_t>(num_words, 0), width,
  434. type_instruction->GetSingleWordInOperand(1), is_irrelevant);
  435. }
  436. case SpvOpTypeArray: {
  437. auto component_type_id = type_instruction->GetSingleWordInOperand(0);
  438. auto num_components =
  439. fuzzerutil::GetArraySize(*type_instruction, GetIRContext());
  440. return FindOrCreateCompositeConstant(
  441. std::vector<uint32_t>(
  442. num_components,
  443. FindOrCreateZeroConstant(component_type_id, is_irrelevant)),
  444. scalar_or_composite_type_id, is_irrelevant);
  445. }
  446. case SpvOpTypeMatrix:
  447. case SpvOpTypeVector: {
  448. auto component_type_id = type_instruction->GetSingleWordInOperand(0);
  449. auto num_components = type_instruction->GetSingleWordInOperand(1);
  450. return FindOrCreateCompositeConstant(
  451. std::vector<uint32_t>(
  452. num_components,
  453. FindOrCreateZeroConstant(component_type_id, is_irrelevant)),
  454. scalar_or_composite_type_id, is_irrelevant);
  455. }
  456. case SpvOpTypeStruct: {
  457. std::vector<uint32_t> field_zero_ids;
  458. for (uint32_t index = 0; index < type_instruction->NumInOperands();
  459. index++) {
  460. field_zero_ids.push_back(FindOrCreateZeroConstant(
  461. type_instruction->GetSingleWordInOperand(index), is_irrelevant));
  462. }
  463. return FindOrCreateCompositeConstant(
  464. field_zero_ids, scalar_or_composite_type_id, is_irrelevant);
  465. }
  466. default:
  467. assert(false && "Unknown type.");
  468. return 0;
  469. }
  470. }
  471. } // namespace fuzz
  472. } // namespace spvtools