1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestDeadlockDetector.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,566 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: sw=4 ts=4 et : 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "mozilla/ArrayUtils.h" 1.11 + 1.12 +#include "prenv.h" 1.13 +#include "prerror.h" 1.14 +#include "prio.h" 1.15 +#include "prproces.h" 1.16 + 1.17 +#include "nsMemory.h" 1.18 + 1.19 +#include "mozilla/CondVar.h" 1.20 +#include "mozilla/ReentrantMonitor.h" 1.21 +#include "mozilla/Mutex.h" 1.22 + 1.23 +#include "TestHarness.h" 1.24 + 1.25 +using namespace mozilla; 1.26 + 1.27 +static PRThread* 1.28 +spawn(void (*run)(void*), void* arg) 1.29 +{ 1.30 + return PR_CreateThread(PR_SYSTEM_THREAD, 1.31 + run, 1.32 + arg, 1.33 + PR_PRIORITY_NORMAL, 1.34 + PR_GLOBAL_THREAD, 1.35 + PR_JOINABLE_THREAD, 1.36 + 0); 1.37 +} 1.38 + 1.39 +#define PASS() \ 1.40 + do { \ 1.41 + passed(__FUNCTION__); \ 1.42 + return NS_OK; \ 1.43 + } while (0) 1.44 + 1.45 +#define FAIL(why) \ 1.46 + do { \ 1.47 + fail("%s | %s - %s", __FILE__, __FUNCTION__, why); \ 1.48 + return NS_ERROR_FAILURE; \ 1.49 + } while (0) 1.50 + 1.51 +//----------------------------------------------------------------------------- 1.52 + 1.53 +static const char* sPathToThisBinary; 1.54 +static const char* sAssertBehaviorEnv = "XPCOM_DEBUG_BREAK=abort"; 1.55 + 1.56 +class Subprocess 1.57 +{ 1.58 +public: 1.59 + // not available until process finishes 1.60 + int32_t mExitCode; 1.61 + nsCString mStdout; 1.62 + nsCString mStderr; 1.63 + 1.64 + Subprocess(const char* aTestName) { 1.65 + // set up stdio redirection 1.66 + PRFileDesc* readStdin; PRFileDesc* writeStdin; 1.67 + PRFileDesc* readStdout; PRFileDesc* writeStdout; 1.68 + PRFileDesc* readStderr; PRFileDesc* writeStderr; 1.69 + PRProcessAttr* pattr = PR_NewProcessAttr(); 1.70 + 1.71 + NS_ASSERTION(pattr, "couldn't allocate process attrs"); 1.72 + 1.73 + NS_ASSERTION(PR_SUCCESS == PR_CreatePipe(&readStdin, &writeStdin), 1.74 + "couldn't create child stdin pipe"); 1.75 + NS_ASSERTION(PR_SUCCESS == PR_SetFDInheritable(readStdin, true), 1.76 + "couldn't set child stdin inheritable"); 1.77 + PR_ProcessAttrSetStdioRedirect(pattr, PR_StandardInput, readStdin); 1.78 + 1.79 + NS_ASSERTION(PR_SUCCESS == PR_CreatePipe(&readStdout, &writeStdout), 1.80 + "couldn't create child stdout pipe"); 1.81 + NS_ASSERTION(PR_SUCCESS == PR_SetFDInheritable(writeStdout, true), 1.82 + "couldn't set child stdout inheritable"); 1.83 + PR_ProcessAttrSetStdioRedirect(pattr, PR_StandardOutput, writeStdout); 1.84 + 1.85 + NS_ASSERTION(PR_SUCCESS == PR_CreatePipe(&readStderr, &writeStderr), 1.86 + "couldn't create child stderr pipe"); 1.87 + NS_ASSERTION(PR_SUCCESS == PR_SetFDInheritable(writeStderr, true), 1.88 + "couldn't set child stderr inheritable"); 1.89 + PR_ProcessAttrSetStdioRedirect(pattr, PR_StandardError, writeStderr); 1.90 + 1.91 + // set up argv with test name to run 1.92 + char* const newArgv[3] = { 1.93 + strdup(sPathToThisBinary), 1.94 + strdup(aTestName), 1.95 + 0 1.96 + }; 1.97 + 1.98 + // make sure the child will abort if an assertion fails 1.99 + NS_ASSERTION(PR_SUCCESS == PR_SetEnv(sAssertBehaviorEnv), 1.100 + "couldn't set XPCOM_DEBUG_BREAK env var"); 1.101 + 1.102 + PRProcess* proc; 1.103 + NS_ASSERTION(proc = PR_CreateProcess(sPathToThisBinary, 1.104 + newArgv, 1.105 + 0, // inherit environment 1.106 + pattr), 1.107 + "couldn't create process"); 1.108 + PR_Close(readStdin); 1.109 + PR_Close(writeStdout); 1.110 + PR_Close(writeStderr); 1.111 + 1.112 + mProc = proc; 1.113 + mStdinfd = writeStdin; 1.114 + mStdoutfd = readStdout; 1.115 + mStderrfd = readStderr; 1.116 + 1.117 + free(newArgv[0]); 1.118 + free(newArgv[1]); 1.119 + PR_DestroyProcessAttr(pattr); 1.120 + } 1.121 + 1.122 + void RunToCompletion(uint32_t aWaitMs) 1.123 + { 1.124 + PR_Close(mStdinfd); 1.125 + 1.126 + PRPollDesc pollfds[2]; 1.127 + int32_t nfds; 1.128 + bool stdoutOpen = true, stderrOpen = true; 1.129 + char buf[4096]; 1.130 + 1.131 + PRIntervalTime now = PR_IntervalNow(); 1.132 + PRIntervalTime deadline = now + PR_MillisecondsToInterval(aWaitMs); 1.133 + 1.134 + while ((stdoutOpen || stderrOpen) && now < deadline) { 1.135 + nfds = 0; 1.136 + if (stdoutOpen) { 1.137 + pollfds[nfds].fd = mStdoutfd; 1.138 + pollfds[nfds].in_flags = PR_POLL_READ; 1.139 + pollfds[nfds].out_flags = 0; 1.140 + ++nfds; 1.141 + } 1.142 + if (stderrOpen) { 1.143 + pollfds[nfds].fd = mStderrfd; 1.144 + pollfds[nfds].in_flags = PR_POLL_READ; 1.145 + pollfds[nfds].out_flags = 0; 1.146 + ++nfds; 1.147 + } 1.148 + 1.149 + int32_t rv = PR_Poll(pollfds, nfds, deadline - now); 1.150 + NS_ASSERTION(0 <= rv, PR_ErrorToName(PR_GetError())); 1.151 + 1.152 + if (0 == rv) { // timeout 1.153 + fputs("(timed out!)\n", stderr); 1.154 + Finish(false); // abnormal 1.155 + return; 1.156 + } 1.157 + 1.158 + for (int32_t i = 0; i < nfds; ++i) { 1.159 + if (!pollfds[i].out_flags) 1.160 + continue; 1.161 + 1.162 + bool isStdout = mStdoutfd == pollfds[i].fd; 1.163 + int32_t len = 0; 1.164 + 1.165 + if (PR_POLL_READ & pollfds[i].out_flags) { 1.166 + len = PR_Read(pollfds[i].fd, buf, sizeof(buf) - 1); 1.167 + NS_ASSERTION(0 <= len, PR_ErrorToName(PR_GetError())); 1.168 + } 1.169 + else if (!(PR_POLL_HUP & pollfds[i].out_flags)) { 1.170 + NS_ERROR(PR_ErrorToName(PR_GetError())); 1.171 + } 1.172 + 1.173 + if (0 < len) { 1.174 + buf[len] = '\0'; 1.175 + if (isStdout) 1.176 + mStdout += buf; 1.177 + else 1.178 + mStderr += buf; 1.179 + } 1.180 + else if (isStdout) { 1.181 + stdoutOpen = false; 1.182 + } 1.183 + else { 1.184 + stderrOpen = false; 1.185 + } 1.186 + } 1.187 + 1.188 + now = PR_IntervalNow(); 1.189 + } 1.190 + 1.191 + if (stdoutOpen) 1.192 + fputs("(stdout still open!)\n", stderr); 1.193 + if (stderrOpen) 1.194 + fputs("(stderr still open!)\n", stderr); 1.195 + if (now > deadline) 1.196 + fputs("(timed out!)\n", stderr); 1.197 + 1.198 + Finish(!stdoutOpen && !stderrOpen && now <= deadline); 1.199 + } 1.200 + 1.201 +private: 1.202 + void Finish(bool normalExit) { 1.203 + if (!normalExit) { 1.204 + PR_KillProcess(mProc); 1.205 + mExitCode = -1; 1.206 + int32_t dummy; 1.207 + PR_WaitProcess(mProc, &dummy); 1.208 + } 1.209 + else { 1.210 + PR_WaitProcess(mProc, &mExitCode); // this had better not block ... 1.211 + } 1.212 + 1.213 + PR_Close(mStdoutfd); 1.214 + PR_Close(mStderrfd); 1.215 + } 1.216 + 1.217 + PRProcess* mProc; 1.218 + PRFileDesc* mStdinfd; // writeable 1.219 + PRFileDesc* mStdoutfd; // readable 1.220 + PRFileDesc* mStderrfd; // readable 1.221 +}; 1.222 + 1.223 +//----------------------------------------------------------------------------- 1.224 +// Harness for checking detector errors 1.225 +bool 1.226 +CheckForDeadlock(const char* test, const char* const* findTokens) 1.227 +{ 1.228 + Subprocess proc(test); 1.229 + proc.RunToCompletion(5000); 1.230 + 1.231 + if (0 == proc.mExitCode) 1.232 + return false; 1.233 + 1.234 + int32_t idx = 0; 1.235 + for (const char* const* tp = findTokens; *tp; ++tp) { 1.236 + const char* const token = *tp; 1.237 +#ifdef MOZILLA_INTERNAL_API 1.238 + idx = proc.mStderr.Find(token, false, idx); 1.239 +#else 1.240 + nsCString tokenCString(token); 1.241 + idx = proc.mStderr.Find(tokenCString, idx); 1.242 +#endif 1.243 + if (-1 == idx) { 1.244 + printf("(missed token '%s' in output)\n", token); 1.245 + puts("----------------------------------\n"); 1.246 + puts(proc.mStderr.get()); 1.247 + puts("----------------------------------\n"); 1.248 + return false; 1.249 + } 1.250 + idx += strlen(token); 1.251 + } 1.252 + 1.253 + return true; 1.254 +} 1.255 + 1.256 +//----------------------------------------------------------------------------- 1.257 +// Single-threaded sanity tests 1.258 + 1.259 +// Stupidest possible deadlock. 1.260 +int 1.261 +Sanity_Child() 1.262 +{ 1.263 + mozilla::Mutex m1("dd.sanity.m1"); 1.264 + m1.Lock(); 1.265 + m1.Lock(); 1.266 + return 0; // not reached 1.267 +} 1.268 + 1.269 +nsresult 1.270 +Sanity() 1.271 +{ 1.272 + const char* const tokens[] = { 1.273 + "###!!! ERROR: Potential deadlock detected", 1.274 + "=== Cyclical dependency starts at\n--- Mutex : dd.sanity.m1", 1.275 + "=== Cycle completed at\n--- Mutex : dd.sanity.m1", 1.276 + "###!!! Deadlock may happen NOW!", // better catch these easy cases... 1.277 + "###!!! ASSERTION: Potential deadlock detected", 1.278 + 0 1.279 + }; 1.280 + if (CheckForDeadlock("Sanity", tokens)) { 1.281 + PASS(); 1.282 + } else { 1.283 + FAIL("deadlock not detected"); 1.284 + } 1.285 +} 1.286 + 1.287 +// Slightly less stupid deadlock. 1.288 +int 1.289 +Sanity2_Child() 1.290 +{ 1.291 + mozilla::Mutex m1("dd.sanity2.m1"); 1.292 + mozilla::Mutex m2("dd.sanity2.m2"); 1.293 + m1.Lock(); 1.294 + m2.Lock(); 1.295 + m1.Lock(); 1.296 + return 0; // not reached 1.297 +} 1.298 + 1.299 +nsresult 1.300 +Sanity2() 1.301 +{ 1.302 + const char* const tokens[] = { 1.303 + "###!!! ERROR: Potential deadlock detected", 1.304 + "=== Cyclical dependency starts at\n--- Mutex : dd.sanity2.m1", 1.305 + "--- Next dependency:\n--- Mutex : dd.sanity2.m2", 1.306 + "=== Cycle completed at\n--- Mutex : dd.sanity2.m1", 1.307 + "###!!! Deadlock may happen NOW!", // better catch these easy cases... 1.308 + "###!!! ASSERTION: Potential deadlock detected", 1.309 + 0 1.310 + }; 1.311 + if (CheckForDeadlock("Sanity2", tokens)) { 1.312 + PASS(); 1.313 + } else { 1.314 + FAIL("deadlock not detected"); 1.315 + } 1.316 +} 1.317 + 1.318 + 1.319 +int 1.320 +Sanity3_Child() 1.321 +{ 1.322 + mozilla::Mutex m1("dd.sanity3.m1"); 1.323 + mozilla::Mutex m2("dd.sanity3.m2"); 1.324 + mozilla::Mutex m3("dd.sanity3.m3"); 1.325 + mozilla::Mutex m4("dd.sanity3.m4"); 1.326 + 1.327 + m1.Lock(); 1.328 + m2.Lock(); 1.329 + m3.Lock(); 1.330 + m4.Lock(); 1.331 + m4.Unlock(); 1.332 + m3.Unlock(); 1.333 + m2.Unlock(); 1.334 + m1.Unlock(); 1.335 + 1.336 + m4.Lock(); 1.337 + m1.Lock(); 1.338 + return 0; 1.339 +} 1.340 + 1.341 +nsresult 1.342 +Sanity3() 1.343 +{ 1.344 + const char* const tokens[] = { 1.345 + "###!!! ERROR: Potential deadlock detected", 1.346 + "=== Cyclical dependency starts at\n--- Mutex : dd.sanity3.m1", 1.347 + "--- Next dependency:\n--- Mutex : dd.sanity3.m2", 1.348 + "--- Next dependency:\n--- Mutex : dd.sanity3.m3", 1.349 + "--- Next dependency:\n--- Mutex : dd.sanity3.m4", 1.350 + "=== Cycle completed at\n--- Mutex : dd.sanity3.m1", 1.351 + "###!!! ASSERTION: Potential deadlock detected", 1.352 + 0 1.353 + }; 1.354 + if (CheckForDeadlock("Sanity3", tokens)) { 1.355 + PASS(); 1.356 + } else { 1.357 + FAIL("deadlock not detected"); 1.358 + } 1.359 +} 1.360 + 1.361 + 1.362 +int 1.363 +Sanity4_Child() 1.364 +{ 1.365 + mozilla::ReentrantMonitor m1("dd.sanity4.m1"); 1.366 + mozilla::Mutex m2("dd.sanity4.m2"); 1.367 + m1.Enter(); 1.368 + m2.Lock(); 1.369 + m1.Enter(); 1.370 + return 0; 1.371 +} 1.372 + 1.373 +nsresult 1.374 +Sanity4() 1.375 +{ 1.376 + const char* const tokens[] = { 1.377 + "Re-entering ReentrantMonitor after acquiring other resources", 1.378 + "###!!! ERROR: Potential deadlock detected", 1.379 + "=== Cyclical dependency starts at\n--- ReentrantMonitor : dd.sanity4.m1", 1.380 + "--- Next dependency:\n--- Mutex : dd.sanity4.m2", 1.381 + "=== Cycle completed at\n--- ReentrantMonitor : dd.sanity4.m1", 1.382 + "###!!! ASSERTION: Potential deadlock detected", 1.383 + 0 1.384 + }; 1.385 + if (CheckForDeadlock("Sanity4", tokens)) { 1.386 + PASS(); 1.387 + } else { 1.388 + FAIL("deadlock not detected"); 1.389 + } 1.390 +} 1.391 + 1.392 +//----------------------------------------------------------------------------- 1.393 +// Multithreaded tests 1.394 + 1.395 +mozilla::Mutex* ttM1; 1.396 +mozilla::Mutex* ttM2; 1.397 + 1.398 +static void 1.399 +TwoThreads_thread(void* arg) 1.400 +{ 1.401 + int32_t m1First = NS_PTR_TO_INT32(arg); 1.402 + if (m1First) { 1.403 + ttM1->Lock(); 1.404 + ttM2->Lock(); 1.405 + ttM2->Unlock(); 1.406 + ttM1->Unlock(); 1.407 + } 1.408 + else { 1.409 + ttM2->Lock(); 1.410 + ttM1->Lock(); 1.411 + ttM1->Unlock(); 1.412 + ttM2->Unlock(); 1.413 + } 1.414 +} 1.415 + 1.416 +int 1.417 +TwoThreads_Child() 1.418 +{ 1.419 + ttM1 = new mozilla::Mutex("dd.twothreads.m1"); 1.420 + ttM2 = new mozilla::Mutex("dd.twothreads.m2"); 1.421 + if (!ttM1 || !ttM2) 1.422 + NS_RUNTIMEABORT("couldn't allocate mutexes"); 1.423 + 1.424 + PRThread* t1 = spawn(TwoThreads_thread, (void*) 0); 1.425 + PR_JoinThread(t1); 1.426 + 1.427 + PRThread* t2 = spawn(TwoThreads_thread, (void*) 1); 1.428 + PR_JoinThread(t2); 1.429 + 1.430 + return 0; 1.431 +} 1.432 + 1.433 +nsresult 1.434 +TwoThreads() 1.435 +{ 1.436 + const char* const tokens[] = { 1.437 + "###!!! ERROR: Potential deadlock detected", 1.438 + "=== Cyclical dependency starts at\n--- Mutex : dd.twothreads.m2", 1.439 + "--- Next dependency:\n--- Mutex : dd.twothreads.m1", 1.440 + "=== Cycle completed at\n--- Mutex : dd.twothreads.m2", 1.441 + "###!!! ASSERTION: Potential deadlock detected", 1.442 + 0 1.443 + }; 1.444 + 1.445 + if (CheckForDeadlock("TwoThreads", tokens)) { 1.446 + PASS(); 1.447 + } else { 1.448 + FAIL("deadlock not detected"); 1.449 + } 1.450 +} 1.451 + 1.452 + 1.453 +mozilla::Mutex* cndMs[4]; 1.454 +const uint32_t K = 100000; 1.455 + 1.456 +static void 1.457 +ContentionNoDeadlock_thread(void* arg) 1.458 +{ 1.459 + int32_t starti = NS_PTR_TO_INT32(arg); 1.460 + 1.461 + for (uint32_t k = 0; k < K; ++k) { 1.462 + for (int32_t i = starti; i < (int32_t) ArrayLength(cndMs); ++i) 1.463 + cndMs[i]->Lock(); 1.464 + // comment out the next two lines for deadlocking fun! 1.465 + for (int32_t i = ArrayLength(cndMs) - 1; i >= starti; --i) 1.466 + cndMs[i]->Unlock(); 1.467 + 1.468 + starti = (starti + 1) % 3; 1.469 + } 1.470 +} 1.471 + 1.472 +int 1.473 +ContentionNoDeadlock_Child() 1.474 +{ 1.475 + PRThread* threads[3]; 1.476 + 1.477 + for (uint32_t i = 0; i < ArrayLength(cndMs); ++i) 1.478 + cndMs[i] = new mozilla::Mutex("dd.cnd.ms"); 1.479 + 1.480 + for (int32_t i = 0; i < (int32_t) ArrayLength(threads); ++i) 1.481 + threads[i] = spawn(ContentionNoDeadlock_thread, NS_INT32_TO_PTR(i)); 1.482 + 1.483 + for (uint32_t i = 0; i < ArrayLength(threads); ++i) 1.484 + PR_JoinThread(threads[i]); 1.485 + 1.486 + for (uint32_t i = 0; i < ArrayLength(cndMs); ++i) 1.487 + delete cndMs[i]; 1.488 + 1.489 + return 0; 1.490 +} 1.491 + 1.492 +nsresult 1.493 +ContentionNoDeadlock() 1.494 +{ 1.495 + const char * func = __func__; 1.496 + Subprocess proc(func); 1.497 + proc.RunToCompletion(60000); 1.498 + if (0 != proc.mExitCode) { 1.499 + printf("(expected 0 == return code, got %d)\n", proc.mExitCode); 1.500 + puts("(output)\n----------------------------------\n"); 1.501 + puts(proc.mStdout.get()); 1.502 + puts("----------------------------------\n"); 1.503 + puts("(error output)\n----------------------------------\n"); 1.504 + puts(proc.mStderr.get()); 1.505 + puts("----------------------------------\n"); 1.506 + 1.507 + FAIL("deadlock"); 1.508 + } 1.509 + PASS(); 1.510 +} 1.511 + 1.512 + 1.513 + 1.514 +//----------------------------------------------------------------------------- 1.515 + 1.516 +int 1.517 +main(int argc, char** argv) 1.518 +{ 1.519 + if (1 < argc) { 1.520 + // XXX can we run w/o scoped XPCOM? 1.521 + const char* test = argv[1]; 1.522 + ScopedXPCOM xpcom(test); 1.523 + if (xpcom.failed()) 1.524 + return 1; 1.525 + 1.526 + // running in a spawned process. call the specificed child function. 1.527 + if (!strcmp("Sanity", test)) 1.528 + return Sanity_Child(); 1.529 + if (!strcmp("Sanity2", test)) 1.530 + return Sanity2_Child(); 1.531 + if (!strcmp("Sanity3", test)) 1.532 + return Sanity3_Child(); 1.533 + if (!strcmp("Sanity4", test)) 1.534 + return Sanity4_Child(); 1.535 + 1.536 + if (!strcmp("TwoThreads", test)) 1.537 + return TwoThreads_Child(); 1.538 + if (!strcmp("ContentionNoDeadlock", test)) 1.539 + return ContentionNoDeadlock_Child(); 1.540 + 1.541 + fail("%s | %s - unknown child test", __FILE__, __FUNCTION__); 1.542 + return 2; 1.543 + } 1.544 + 1.545 + ScopedXPCOM xpcom("XPCOM deadlock detector correctness (" __FILE__ ")"); 1.546 + if (xpcom.failed()) 1.547 + return 1; 1.548 + 1.549 + // in the first invocation of this process. we will be the "driver". 1.550 + int rv = 0; 1.551 + 1.552 + sPathToThisBinary = argv[0]; 1.553 + 1.554 + if (NS_FAILED(Sanity())) 1.555 + rv = 1; 1.556 + if (NS_FAILED(Sanity2())) 1.557 + rv = 1; 1.558 + if (NS_FAILED(Sanity3())) 1.559 + rv = 1; 1.560 + if (NS_FAILED(Sanity4())) 1.561 + rv = 1; 1.562 + 1.563 + if (NS_FAILED(TwoThreads())) 1.564 + rv = 1; 1.565 + if (NS_FAILED(ContentionNoDeadlock())) 1.566 + rv = 1; 1.567 + 1.568 + return rv; 1.569 +}