| 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 |
|
| 7 |
#ifndef mozilla_ChaosMode_h |
| 8 |
#define mozilla_ChaosMode_h |
| 9 |
|
| 10 |
#include "mozilla/Atomics.h" |
| 11 |
#include "mozilla/EnumSet.h" |
| 12 |
|
| 13 |
#include <stdint.h> |
| 14 |
#include <stdlib.h> |
| 15 |
|
| 16 |
namespace mozilla { |
| 17 |
|
| 18 |
enum class ChaosFeature : uint32_t { |
| 19 |
None = 0x0, |
| 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. |
| 27 |
IOAmounts = 0x8, |
| 28 |
// Iterate over hash tables in random order. |
| 29 |
HashTableIteration = 0x10, |
| 30 |
// Randomly refuse to use cached version of image (when allowed by spec). |
| 31 |
ImageCache = 0x20, |
| 32 |
// Delay dispatching threads to encourage dispatched tasks to run. |
| 33 |
TaskDispatching = 0x40, |
| 34 |
// Delay task running to encourage sending threads to run. |
| 35 |
TaskRunning = 0x80, |
| 36 |
Any = 0xffffffff, |
| 37 |
}; |
| 38 |
|
| 39 |
namespace detail { |
| 40 |
extern MFBT_DATA Atomic<uint32_t, Relaxed> gChaosModeCounter; |
| 41 |
extern MFBT_DATA ChaosFeature gChaosFeatures; |
| 42 |
} // namespace detail |
| 43 |
|
| 44 |
/** |
| 45 |
* When "chaos mode" is activated, code that makes implicitly nondeterministic |
| 46 |
* choices is encouraged to make random and extreme choices, to test more |
| 47 |
* code paths and uncover bugs. |
| 48 |
*/ |
| 49 |
class ChaosMode { |
| 50 |
public: |
| 51 |
static void SetChaosFeature(ChaosFeature aChaosFeature) { |
| 52 |
detail::gChaosFeatures = aChaosFeature; |
| 53 |
} |
| 54 |
|
| 55 |
static bool isActive(ChaosFeature aFeature) { |
| 56 |
return detail::gChaosModeCounter > 0 && |
| 57 |
(uint32_t(detail::gChaosFeatures) & uint32_t(aFeature)); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Increase the chaos mode activation level. An equivalent number of |
| 62 |
* calls to leaveChaosMode must be made in order to restore the original |
| 63 |
* chaos mode state. |
| 64 |
*/ |
| 65 |
static void enterChaosMode() { detail::gChaosModeCounter++; } |
| 66 |
|
| 67 |
/** |
| 68 |
* Decrease the chaos mode activation level. See enterChaosMode(). |
| 69 |
*/ |
| 70 |
static void leaveChaosMode() { |
| 71 |
MOZ_ASSERT(detail::gChaosModeCounter > 0); |
| 72 |
detail::gChaosModeCounter--; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns a somewhat (but not uniformly) random uint32_t < aBound. |
| 77 |
* Not to be used for anything except ChaosMode, since it's not very random. |
| 78 |
*/ |
| 79 |
static uint32_t randomUint32LessThan(uint32_t aBound) { |
| 80 |
MOZ_ASSERT(aBound != 0); |
| 81 |
return uint32_t(rand()) % aBound; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns a somewhat (but not uniformly) random int32_t <= aLow and >= aHigh. |
| 86 |
* Not to be used for anything except ChaosMode, since it's not very random. |
| 87 |
*/ |
| 88 |
static int32_t randomInt32InRange(int32_t aLow, int32_t aHigh) { |
| 89 |
MOZ_ASSERT(aHigh >= aLow); |
| 90 |
return (int32_t(rand()) % (aHigh - aLow + 1)) + aLow; |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
} /* namespace mozilla */ |
| 95 |
|
| 96 |
#endif /* mozilla_ChaosMode_h */ |