fuzzer_context.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 kDefaultChanceOfMovingBlockDown = 25;
  25. const uint32_t kDefaultChanceOfObfuscatingConstant = 20;
  26. const uint32_t kDefaultChanceOfSplittingBlock = 20;
  27. // Default functions for controlling how deep to go during recursive
  28. // generation/transformation. Keep them in alphabetical order.
  29. const std::function<bool(uint32_t, RandomGenerator*)>
  30. kDefaultGoDeeperInConstantObfuscation =
  31. [](uint32_t current_depth, RandomGenerator* random_generator) -> bool {
  32. double chance = 1.0 / std::pow(3.0, static_cast<float>(current_depth + 1));
  33. return random_generator->RandomDouble() < chance;
  34. };
  35. } // namespace
  36. FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
  37. uint32_t min_fresh_id)
  38. : random_generator_(random_generator),
  39. next_fresh_id_(min_fresh_id),
  40. chance_of_adding_dead_break_(kDefaultChanceOfAddingDeadBreak),
  41. chance_of_adding_dead_continue_(kDefaultChanceOfAddingDeadContinue),
  42. chance_of_moving_block_down_(kDefaultChanceOfMovingBlockDown),
  43. chance_of_obfuscating_constant_(kDefaultChanceOfObfuscatingConstant),
  44. chance_of_splitting_block_(kDefaultChanceOfSplittingBlock),
  45. go_deeper_in_constant_obfuscation_(
  46. kDefaultGoDeeperInConstantObfuscation) {}
  47. FuzzerContext::~FuzzerContext() = default;
  48. uint32_t FuzzerContext::GetFreshId() { return next_fresh_id_++; }
  49. RandomGenerator* FuzzerContext::GetRandomGenerator() {
  50. return random_generator_;
  51. }
  52. } // namespace fuzz
  53. } // namespace spvtools