fuzzer_context.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_context.h"
  15. #include <cmath>
  16. namespace spvtools {
  17. namespace fuzz {
  18. namespace {
  19. // Default probabilities for applying various transformations.
  20. // All values are percentages.
  21. // Keep them in alphabetical order.
  22. const uint32_t kDefaultChanceOfAddingDeadBreak = 20;
  23. const uint32_t kDefaultChanceOfAddingDeadContinue = 20;
  24. const uint32_t kDefaultChanceOfCopyingObject = 20;
  25. const uint32_t kDefaultChanceOfMovingBlockDown = 25;
  26. const uint32_t kDefaultChanceOfObfuscatingConstant = 20;
  27. const uint32_t kDefaultChanceOfSplittingBlock = 20;
  28. // Default functions for controlling how deep to go during recursive
  29. // generation/transformation. Keep them in alphabetical order.
  30. const std::function<bool(uint32_t, RandomGenerator*)>
  31. kDefaultGoDeeperInConstantObfuscation =
  32. [](uint32_t current_depth, RandomGenerator* random_generator) -> bool {
  33. double chance = 1.0 / std::pow(3.0, static_cast<float>(current_depth + 1));
  34. return random_generator->RandomDouble() < chance;
  35. };
  36. } // namespace
  37. FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
  38. uint32_t min_fresh_id)
  39. : random_generator_(random_generator),
  40. next_fresh_id_(min_fresh_id),
  41. chance_of_adding_dead_break_(kDefaultChanceOfAddingDeadBreak),
  42. chance_of_adding_dead_continue_(kDefaultChanceOfAddingDeadContinue),
  43. chance_of_copying_object_(kDefaultChanceOfCopyingObject),
  44. chance_of_moving_block_down_(kDefaultChanceOfMovingBlockDown),
  45. chance_of_obfuscating_constant_(kDefaultChanceOfObfuscatingConstant),
  46. chance_of_splitting_block_(kDefaultChanceOfSplittingBlock),
  47. go_deeper_in_constant_obfuscation_(
  48. kDefaultGoDeeperInConstantObfuscation) {}
  49. FuzzerContext::~FuzzerContext() = default;
  50. uint32_t FuzzerContext::GetFreshId() { return next_fresh_id_++; }
  51. bool FuzzerContext::ChooseEven() { return random_generator_->RandomBool(); }
  52. bool FuzzerContext::ChoosePercentage(uint32_t percentage_chance) {
  53. assert(percentage_chance <= 100);
  54. return random_generator_->RandomPercentage() < percentage_chance;
  55. }
  56. } // namespace fuzz
  57. } // namespace spvtools