ChaosMode.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef mozilla_ChaosMode_h
  7. #define mozilla_ChaosMode_h
  8. #include "mozilla/Atomics.h"
  9. #include "mozilla/EnumSet.h"
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. namespace mozilla {
  13. enum ChaosFeature {
  14. None = 0x0,
  15. // Altering thread scheduling.
  16. ThreadScheduling = 0x1,
  17. // Altering network request scheduling.
  18. NetworkScheduling = 0x2,
  19. // Altering timer scheduling.
  20. TimerScheduling = 0x4,
  21. // Read and write less-than-requested amounts.
  22. IOAmounts = 0x8,
  23. // Iterate over hash tables in random order.
  24. HashTableIteration = 0x10,
  25. // Randomly refuse to use cached version of image (when allowed by spec).
  26. ImageCache = 0x20,
  27. Any = 0xffffffff,
  28. };
  29. namespace detail {
  30. extern MFBT_DATA Atomic<uint32_t> gChaosModeCounter;
  31. extern MFBT_DATA ChaosFeature gChaosFeatures;
  32. } // namespace detail
  33. /**
  34. * When "chaos mode" is activated, code that makes implicitly nondeterministic
  35. * choices is encouraged to make random and extreme choices, to test more
  36. * code paths and uncover bugs.
  37. */
  38. class ChaosMode
  39. {
  40. public:
  41. static void SetChaosFeature(ChaosFeature aChaosFeature)
  42. {
  43. detail::gChaosFeatures = aChaosFeature;
  44. }
  45. static bool isActive(ChaosFeature aFeature)
  46. {
  47. if (detail::gChaosModeCounter > 0) {
  48. return true;
  49. }
  50. return detail::gChaosFeatures & aFeature;
  51. }
  52. /**
  53. * Increase the chaos mode activation level. An equivalent number of
  54. * calls to leaveChaosMode must be made in order to restore the original
  55. * chaos mode state. If the activation level is nonzero all chaos mode
  56. * features are activated.
  57. */
  58. static void enterChaosMode()
  59. {
  60. detail::gChaosModeCounter++;
  61. }
  62. /**
  63. * Decrease the chaos mode activation level. See enterChaosMode().
  64. */
  65. static void leaveChaosMode()
  66. {
  67. MOZ_ASSERT(detail::gChaosModeCounter > 0);
  68. detail::gChaosModeCounter--;
  69. }
  70. /**
  71. * Returns a somewhat (but not uniformly) random uint32_t < aBound.
  72. * Not to be used for anything except ChaosMode, since it's not very random.
  73. */
  74. static uint32_t randomUint32LessThan(uint32_t aBound)
  75. {
  76. MOZ_ASSERT(aBound != 0);
  77. return uint32_t(rand()) % aBound;
  78. }
  79. };
  80. } /* namespace mozilla */
  81. #endif /* mozilla_ChaosMode_h */