summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestDeadlockDetector.cpp
blob: 2fa0036fd40f2e7792a39ad2eb1037c0c154c4c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: sw=2 ts=4 et :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ArrayUtils.h"

#include "prthread.h"

#include "nsCOMPtr.h"
#include "nsICrashReporter.h"
#include "nsMemory.h"
#include "nsServiceManagerUtils.h"
#include "nsTArray.h"

#include "mozilla/CondVar.h"
#include "mozilla/RecursiveMutex.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/Mutex.h"

#include "gtest/gtest.h"

using namespace mozilla;

// The code in this file is also used by
// storage/test/gtest/test_deadlock_detector.cpp. The following two macros are
// used to provide the necessary differentiation between this file and that
// file.
#ifndef MUTEX
#  define MUTEX mozilla::Mutex
#endif
#ifndef TESTNAME
#  define TESTNAME(name) XPCOM##name
#endif

static PRThread* spawn(void (*run)(void*), void* arg) {
  return PR_CreateThread(PR_SYSTEM_THREAD, run, arg, PR_PRIORITY_NORMAL,
                         PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
}

// This global variable is defined in toolkit/xre/nsSigHandlers.cpp.
extern unsigned int _gdb_sleep_duration;

/**
 * Simple test fixture that makes sure the gdb sleep setup in the
 * ah crap handler is bypassed during the death tests.
 */
class TESTNAME(DeadlockDetectorTest) : public ::testing::Test {
 protected:
  void SetUp() final {
    mOldSleepDuration = ::_gdb_sleep_duration;
    ::_gdb_sleep_duration = 0;
  }

  void TearDown() final { ::_gdb_sleep_duration = mOldSleepDuration; }

 private:
  unsigned int mOldSleepDuration;
};

static void DisableCrashReporter() {
  nsCOMPtr<nsICrashReporter> crashreporter =
      do_GetService("@mozilla.org/toolkit/crash-reporter;1");
  if (crashreporter) {
    crashreporter->SetEnabled(false);
  }
}

//-----------------------------------------------------------------------------
// Single-threaded sanity tests

// Stupidest possible deadlock.
static int Sanity_Child() {
  DisableCrashReporter();

  MUTEX m1("dd.sanity.m1");
  m1.Lock();
  m1.Lock();
  return 0;  // not reached
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(SanityDeathTest)) {
  const char* const regex =
      "###!!! ERROR: Potential deadlock detected.*"
      "=== Cyclical dependency starts at.*--- Mutex : dd.sanity.m1.*"
      "=== Cycle completed at.*--- Mutex : dd.sanity.m1.*"
      "###!!! Deadlock may happen NOW!.*"  // better catch these easy cases...
      "###!!! ASSERTION: Potential deadlock detected.*";

  ASSERT_DEATH_IF_SUPPORTED(Sanity_Child(), regex);
}

// Slightly less stupid deadlock.
static int Sanity2_Child() {
  DisableCrashReporter();

  MUTEX m1("dd.sanity2.m1");
  MUTEX m2("dd.sanity2.m2");
  m1.Lock();
  m2.Lock();
  m1.Lock();
  return 0;  // not reached
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(Sanity2DeathTest)) {
  const char* const regex =
      "###!!! ERROR: Potential deadlock detected.*"
      "=== Cyclical dependency starts at.*--- Mutex : dd.sanity2.m1.*"
      "--- Next dependency:.*--- Mutex : dd.sanity2.m2.*"
      "=== Cycle completed at.*--- Mutex : dd.sanity2.m1.*"
      "###!!! Deadlock may happen NOW!.*"  // better catch these easy cases...
      "###!!! ASSERTION: Potential deadlock detected.*";

  ASSERT_DEATH_IF_SUPPORTED(Sanity2_Child(), regex);
}

#if 0
// Temporarily disabled, see bug 1370644.
int
Sanity3_Child()
{
    DisableCrashReporter();

    MUTEX m1("dd.sanity3.m1");
    MUTEX m2("dd.sanity3.m2");
    MUTEX m3("dd.sanity3.m3");
    MUTEX m4("dd.sanity3.m4");

    m1.Lock();
    m2.Lock();
    m3.Lock();
    m4.Lock();
    m4.Unlock();
    m3.Unlock();
    m2.Unlock();
    m1.Unlock();

    m4.Lock();
    m1.Lock();
    return 0;
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(Sanity3DeathTest))
{
    const char* const regex =
        "###!!! ERROR: Potential deadlock detected.*"
        "=== Cyclical dependency starts at.*--- Mutex : dd.sanity3.m1.*"
        "--- Next dependency:.*--- Mutex : dd.sanity3.m2.*"
        "--- Next dependency:.*--- Mutex : dd.sanity3.m3.*"
        "--- Next dependency:.*--- Mutex : dd.sanity3.m4.*"
        "=== Cycle completed at.*--- Mutex : dd.sanity3.m1.*"
        "###!!! ASSERTION: Potential deadlock detected.*";

    ASSERT_DEATH_IF_SUPPORTED(Sanity3_Child(), regex);
}
#endif

static int Sanity4_Child() {
  DisableCrashReporter();

  mozilla::ReentrantMonitor m1("dd.sanity4.m1");
  MUTEX m2("dd.sanity4.m2");
  m1.Enter();
  m2.Lock();
  m1.Enter();
  return 0;
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(Sanity4DeathTest)) {
  const char* const regex =
      "Re-entering ReentrantMonitor after acquiring other resources.*"
      "###!!! ERROR: Potential deadlock detected.*"
      "=== Cyclical dependency starts at.*--- ReentrantMonitor : "
      "dd.sanity4.m1.*"
      "--- Next dependency:.*--- Mutex : dd.sanity4.m2.*"
      "=== Cycle completed at.*--- ReentrantMonitor : dd.sanity4.m1.*"
      "###!!! ASSERTION: Potential deadlock detected.*";
  ASSERT_DEATH_IF_SUPPORTED(Sanity4_Child(), regex);
}

static int Sanity5_Child() {
  DisableCrashReporter();

  mozilla::RecursiveMutex m1("dd.sanity4.m1");
  MUTEX m2("dd.sanity4.m2");
  m1.Lock();
  m2.Lock();
  m1.Lock();
  return 0;
}

#if !defined(DISABLE_STORAGE_SANITY5_DEATH_TEST)
TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(Sanity5DeathTest)) {
  const char* const regex =
      "Re-entering RecursiveMutex after acquiring other resources.*"
      "###!!! ERROR: Potential deadlock detected.*"
      "=== Cyclical dependency starts at.*--- RecursiveMutex : dd.sanity4.m1.*"
      "--- Next dependency:.*--- Mutex : dd.sanity4.m2.*"
      "=== Cycle completed at.*--- RecursiveMutex : dd.sanity4.m1.*"
      "###!!! ASSERTION: Potential deadlock detected.*";
  ASSERT_DEATH_IF_SUPPORTED(Sanity5_Child(), regex);
}
#endif

//-----------------------------------------------------------------------------
// Multithreaded tests

/**
 * Helper for passing state to threads in the multithread tests.
 */
struct ThreadState {
  /**
   * Locks to use during the test. This is just a reference and is owned by
   * the main test thread.
   */
  const nsTArray<MUTEX*>& locks;

  /**
   * Integer argument used to identify each thread.
   */
  int id;
};

#if 0
// Temporarily disabled, see bug 1370644.
static void
TwoThreads_thread(void* arg)
{
    ThreadState* state = static_cast<ThreadState*>(arg);

    MUTEX* ttM1 = state->locks[0];
    MUTEX* ttM2 = state->locks[1];

    if (state->id) {
        ttM1->Lock();
        ttM2->Lock();
        ttM2->Unlock();
        ttM1->Unlock();
    }
    else {
        ttM2->Lock();
        ttM1->Lock();
        ttM1->Unlock();
        ttM2->Unlock();
    }
}

int
TwoThreads_Child()
{
    DisableCrashReporter();

    nsTArray<MUTEX*> locks = {
      new MUTEX("dd.twothreads.m1"),
      new MUTEX("dd.twothreads.m2")
    };

    ThreadState state_1 {locks, 0};
    PRThread* t1 = spawn(TwoThreads_thread, &state_1);
    PR_JoinThread(t1);

    ThreadState state_2 {locks, 1};
    PRThread* t2 = spawn(TwoThreads_thread, &state_2);
    PR_JoinThread(t2);

    for (auto& lock : locks) {
      delete lock;
    }

    return 0;
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(TwoThreadsDeathTest))
{
    const char* const regex =
        "###!!! ERROR: Potential deadlock detected.*"
        "=== Cyclical dependency starts at.*--- Mutex : dd.twothreads.m2.*"
        "--- Next dependency:.*--- Mutex : dd.twothreads.m1.*"
        "=== Cycle completed at.*--- Mutex : dd.twothreads.m2.*"
        "###!!! ASSERTION: Potential deadlock detected.*";

    ASSERT_DEATH_IF_SUPPORTED(TwoThreads_Child(), regex);
}
#endif

static void ContentionNoDeadlock_thread(void* arg) {
  const uint32_t K = 100000;

  ThreadState* state = static_cast<ThreadState*>(arg);
  int32_t starti = static_cast<int32_t>(state->id);
  auto& cndMs = state->locks;

  for (uint32_t k = 0; k < K; ++k) {
    for (int32_t i = starti; i < (int32_t)cndMs.Length(); ++i) cndMs[i]->Lock();
    // comment out the next two lines for deadlocking fun!
    for (int32_t i = cndMs.Length() - 1; i >= starti; --i) cndMs[i]->Unlock();

    starti = (starti + 1) % 3;
  }
}

static int ContentionNoDeadlock_Child() {
  const size_t kMutexCount = 4;

  PRThread* threads[3];
  nsTArray<MUTEX*> locks;
  ThreadState states[] = {{locks, 0}, {locks, 1}, {locks, 2}};

  for (uint32_t i = 0; i < kMutexCount; ++i)
    locks.AppendElement(new MUTEX("dd.cnd.ms"));

  for (int32_t i = 0; i < (int32_t)ArrayLength(threads); ++i)
    threads[i] = spawn(ContentionNoDeadlock_thread, states + i);

  for (uint32_t i = 0; i < ArrayLength(threads); ++i) PR_JoinThread(threads[i]);

  for (uint32_t i = 0; i < locks.Length(); ++i) delete locks[i];

  return 0;
}

TEST_F(TESTNAME(DeadlockDetectorTest), TESTNAME(ContentionNoDeadlock)) {
  // Just check that this test runs to completion.
  ASSERT_EQ(ContentionNoDeadlock_Child(), 0);
}