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/. */
7 #ifndef mozilla_ChaosMode_h
8 #define mozilla_ChaosMode_h
10 #include "mozilla/Atomics.h"
11 #include "mozilla/EnumSet.h"
20 // Altering thread scheduling.
21 ThreadScheduling
= 0x1,
22 // Altering network request scheduling.
23 NetworkScheduling
= 0x2,
24 // Altering timer scheduling.
25 TimerScheduling
= 0x4,
26 // Read and write less-than-requested amounts.
28 // Iterate over hash tables in random order.
29 HashTableIteration
= 0x10,
30 // Randomly refuse to use cached version of image (when allowed by spec).
32 // Delay dispatching threads to encourage dispatched tasks to run.
33 TaskDispatching
= 0x40,
34 // Delay task running to encourage sending threads to run.
40 extern MFBT_DATA Atomic
<uint32_t, SequentiallyConsistent
,
41 recordreplay::Behavior::DontPreserve
>
43 extern MFBT_DATA ChaosFeature gChaosFeatures
;
47 * When "chaos mode" is activated, code that makes implicitly nondeterministic
48 * choices is encouraged to make random and extreme choices, to test more
49 * code paths and uncover bugs.
53 static void SetChaosFeature(ChaosFeature aChaosFeature
) {
54 detail::gChaosFeatures
= aChaosFeature
;
57 static bool isActive(ChaosFeature aFeature
) {
58 if (detail::gChaosModeCounter
> 0) {
61 return detail::gChaosFeatures
& aFeature
;
65 * Increase the chaos mode activation level. An equivalent number of
66 * calls to leaveChaosMode must be made in order to restore the original
67 * chaos mode state. If the activation level is nonzero all chaos mode
68 * features are activated.
70 static void enterChaosMode() { detail::gChaosModeCounter
++; }
73 * Decrease the chaos mode activation level. See enterChaosMode().
75 static void leaveChaosMode() {
76 MOZ_ASSERT(detail::gChaosModeCounter
> 0);
77 detail::gChaosModeCounter
--;
81 * Returns a somewhat (but not uniformly) random uint32_t < aBound.
82 * Not to be used for anything except ChaosMode, since it's not very random.
84 static uint32_t randomUint32LessThan(uint32_t aBound
) {
85 MOZ_ASSERT(aBound
!= 0);
86 return uint32_t(rand()) % aBound
;
90 } /* namespace mozilla */
92 #endif /* mozilla_ChaosMode_h */