fuzzer_pass_obfuscate_constants.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef SOURCE_FUZZ_FUZZER_PASS_OBFUSCATE_CONSTANTS_H_
  15. #define SOURCE_FUZZ_FUZZER_PASS_OBFUSCATE_CONSTANTS_H_
  16. #include <vector>
  17. #include "source/fuzz/fuzzer_pass.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. // A fuzzer pass for turning uses of constants into more complex forms.
  21. // Examples include replacing 'true' with '42 < 52', and replacing '42' with
  22. // 'a.b.c' if 'a.b.c' is known to hold the value '42'.
  23. class FuzzerPassObfuscateConstants : public FuzzerPass {
  24. public:
  25. FuzzerPassObfuscateConstants(
  26. opt::IRContext* ir_context, TransformationContext* transformation_context,
  27. FuzzerContext* fuzzer_context,
  28. protobufs::TransformationSequence* transformations,
  29. bool ignore_inapplicable_transformations);
  30. void Apply() override;
  31. private:
  32. // Applies 0 or more transformations to potentially obfuscate the constant
  33. // use represented by |constant_use|. The |depth| parameter controls how
  34. // deeply obfuscation can recurse.
  35. void ObfuscateConstant(uint32_t depth,
  36. const protobufs::IdUseDescriptor& constant_use);
  37. // This method will try to turn |constant_use|, required to be a use of a
  38. // boolean constant, into a binary expression on scalar constants, which may
  39. // themselves be recursively obfuscated.
  40. void ObfuscateBoolConstant(uint32_t depth,
  41. const protobufs::IdUseDescriptor& constant_use);
  42. // This method will try to turn |constant_use|, required to be a use of a
  43. // scalar constant, into the value loaded from a uniform known to have the
  44. // same value as the constant (if one exists).
  45. void ObfuscateScalarConstant(uint32_t depth,
  46. const protobufs::IdUseDescriptor& constant_use);
  47. // Applies a transformation to replace the boolean constant usage represented
  48. // by |bool_constant_use| with a binary expression involving
  49. // |float_constant_id_1| and |float_constant_id_2|, which must not be equal
  50. // to one another. Possibly further obfuscates the uses of these float
  51. // constants. The |depth| parameter controls how deeply obfuscation can
  52. // recurse.
  53. void ObfuscateBoolConstantViaFloatConstantPair(
  54. uint32_t depth, const protobufs::IdUseDescriptor& bool_constant_use,
  55. uint32_t float_constant_id_1, uint32_t float_constant_id_2);
  56. // Similar to the above, but for signed int constants.
  57. void ObfuscateBoolConstantViaSignedIntConstantPair(
  58. uint32_t depth, const protobufs::IdUseDescriptor& bool_constant_use,
  59. uint32_t signed_int_constant_id_1, uint32_t signed_int_constant_id_2);
  60. // Similar to the above, but for unsigned int constants.
  61. void ObfuscateBoolConstantViaUnsignedIntConstantPair(
  62. uint32_t depth, const protobufs::IdUseDescriptor& bool_constant_use,
  63. uint32_t unsigned_int_constant_id_1, uint32_t unsigned_int_constant_id_2);
  64. // A helper method to capture the common parts of the above methods.
  65. // The method is used to obfuscate the boolean constant usage represented by
  66. // |bool_constant_use| by replacing it with '|constant_id_1| OP
  67. // |constant_id_2|', where 'OP' is chosen from either |greater_than_opcodes|
  68. // or |less_than_opcodes|.
  69. //
  70. // The two constant ids must not represent the same value, and thus
  71. // |greater_than_opcodes| may include 'greater than or equal' opcodes
  72. // (similar for |less_than_opcodes|).
  73. void ObfuscateBoolConstantViaConstantPair(
  74. uint32_t depth, const protobufs::IdUseDescriptor& bool_constant_use,
  75. const std::vector<spv::Op>& greater_than_opcodes,
  76. const std::vector<spv::Op>& less_than_opcodes, uint32_t constant_id_1,
  77. uint32_t constant_id_2, bool first_constant_is_larger);
  78. // A helper method to determine whether input operand |in_operand_index| of
  79. // |inst| is the id of a constant, and add an id use descriptor to
  80. // |candidate_constant_uses| if so. The other parameters are used for id use
  81. // descriptor construction.
  82. void MaybeAddConstantIdUse(
  83. const opt::Instruction& inst, uint32_t in_operand_index,
  84. uint32_t base_instruction_result_id,
  85. const std::map<spv::Op, uint32_t>& skipped_opcode_count,
  86. std::vector<protobufs::IdUseDescriptor>* constant_uses);
  87. // Returns a vector of unique words that denote constants. Every such constant
  88. // is used in |FactConstantUniform| and has type with id equal to |type_id|.
  89. std::vector<std::vector<uint32_t>> GetConstantWordsFromUniformsForType(
  90. uint32_t type_id);
  91. };
  92. } // namespace fuzz
  93. } // namespace spvtools
  94. #endif // SOURCE_FUZZ_FUZZER_PASS_OBFUSCATE_CONSTANTS_H_