Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/ChaosMode.h
Line
Count
Source (jump to first uncovered line)
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 ChaosFeature {
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,
41
                        SequentiallyConsistent,
42
                        recordreplay::Behavior::DontPreserve> gChaosModeCounter;
43
extern MFBT_DATA ChaosFeature gChaosFeatures;
44
} // namespace detail
45
46
/**
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.
50
 */
51
class ChaosMode
52
{
53
public:
54
  static void SetChaosFeature(ChaosFeature aChaosFeature)
55
0
  {
56
0
    detail::gChaosFeatures = aChaosFeature;
57
0
  }
58
59
  static bool isActive(ChaosFeature aFeature)
60
  {
61
    if (detail::gChaosModeCounter > 0) {
62
      return true;
63
    }
64
    return detail::gChaosFeatures & aFeature;
65
  }
66
67
  /**
68
   * Increase the chaos mode activation level. An equivalent number of
69
   * calls to leaveChaosMode must be made in order to restore the original
70
   * chaos mode state. If the activation level is nonzero all chaos mode
71
   * features are activated.
72
   */
73
  static void enterChaosMode()
74
0
  {
75
0
    detail::gChaosModeCounter++;
76
0
  }
77
78
  /**
79
   * Decrease the chaos mode activation level. See enterChaosMode().
80
   */
81
  static void leaveChaosMode()
82
0
  {
83
0
    MOZ_ASSERT(detail::gChaosModeCounter > 0);
84
0
    detail::gChaosModeCounter--;
85
0
  }
Unexecuted instantiation: mozilla::ChaosMode::leaveChaosMode()
Unexecuted instantiation: mozilla::ChaosMode::leaveChaosMode()
86
87
  /**
88
   * Returns a somewhat (but not uniformly) random uint32_t < aBound.
89
   * Not to be used for anything except ChaosMode, since it's not very random.
90
   */
91
  static uint32_t randomUint32LessThan(uint32_t aBound)
92
0
  {
93
0
    MOZ_ASSERT(aBound != 0);
94
0
    return uint32_t(rand()) % aBound;
95
0
  }
Unexecuted instantiation: mozilla::ChaosMode::randomUint32LessThan(unsigned int)
Unexecuted instantiation: mozilla::ChaosMode::randomUint32LessThan(unsigned int)
96
};
97
98
} /* namespace mozilla */
99
100
#endif /* mozilla_ChaosMode_h */