fuzzer_pass_add_bit_instruction_synonyms.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2020 André Perez Maselco
  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_add_bit_instruction_synonyms.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/fuzz/transformation_add_bit_instruction_synonym.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassAddBitInstructionSynonyms::FuzzerPassAddBitInstructionSynonyms(
  21. opt::IRContext* ir_context, TransformationContext* transformation_context,
  22. FuzzerContext* fuzzer_context,
  23. protobufs::TransformationSequence* transformations,
  24. bool ignore_inapplicable_transformations)
  25. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  26. transformations, ignore_inapplicable_transformations) {}
  27. void FuzzerPassAddBitInstructionSynonyms::Apply() {
  28. for (auto& function : *GetIRContext()->module()) {
  29. for (auto& block : function) {
  30. for (auto& instruction : block) {
  31. // This fuzzer pass can add a *lot* of ids. We bail out early if we hit
  32. // the recommended id limit.
  33. if (GetIRContext()->module()->id_bound() >=
  34. GetFuzzerContext()->GetIdBoundLimit()) {
  35. return;
  36. }
  37. // Randomly decides whether the transformation will be applied.
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()->GetChanceOfAddingBitInstructionSynonym())) {
  40. continue;
  41. }
  42. // Make sure fuzzer never applies a transformation to a bitwise
  43. // instruction with differently signed operands, only integer operands
  44. // are supported and bitwise operations are supported only.
  45. if (!TransformationAddBitInstructionSynonym::IsInstructionSupported(
  46. GetIRContext(), &instruction)) {
  47. continue;
  48. }
  49. // Make sure all bit indexes are defined as 32-bit unsigned integers.
  50. uint32_t width = GetIRContext()
  51. ->get_type_mgr()
  52. ->GetType(instruction.type_id())
  53. ->AsInteger()
  54. ->width();
  55. for (uint32_t i = 0; i < width; i++) {
  56. FindOrCreateIntegerConstant({i}, 32, false, false);
  57. }
  58. // Applies the add bit instruction synonym transformation.
  59. ApplyTransformation(TransformationAddBitInstructionSynonym(
  60. instruction.result_id(),
  61. GetFuzzerContext()->GetFreshIds(
  62. TransformationAddBitInstructionSynonym::GetRequiredFreshIdCount(
  63. GetIRContext(), &instruction))));
  64. }
  65. }
  66. }
  67. }
  68. } // namespace fuzz
  69. } // namespace spvtools