michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "storage_test_harness.h" michael@0: #include "prthread.h" michael@0: #include "nsIEventTarget.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: michael@0: #include "sqlite3.h" michael@0: michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: michael@0: using mozilla::ReentrantMonitor; michael@0: using mozilla::ReentrantMonitorAutoEnter; michael@0: michael@0: /** michael@0: * Verify that mozIStorageAsyncStatement's life-cycle never triggers a mutex on michael@0: * the caller (generally main) thread. We do this by decorating the sqlite michael@0: * mutex logic with our own code that checks what thread it is being invoked on michael@0: * and sets a flag if it is invoked on the main thread. We are able to easily michael@0: * decorate the SQLite mutex logic because SQLite allows us to retrieve the michael@0: * current function pointers being used and then provide a new set. michael@0: */ michael@0: michael@0: /* ===== Mutex Watching ===== */ michael@0: michael@0: sqlite3_mutex_methods orig_mutex_methods; michael@0: sqlite3_mutex_methods wrapped_mutex_methods; michael@0: michael@0: bool mutex_used_on_watched_thread = false; michael@0: PRThread *watched_thread = nullptr; michael@0: /** michael@0: * Ugly hack to let us figure out what a connection's async thread is. If we michael@0: * were MOZILLA_INTERNAL_API and linked as such we could just include michael@0: * mozStorageConnection.h and just ask Connection directly. But that turns out michael@0: * poorly. michael@0: * michael@0: * When the thread a mutex is invoked on isn't watched_thread we save it to this michael@0: * variable. michael@0: */ michael@0: PRThread *last_non_watched_thread = nullptr; michael@0: michael@0: /** michael@0: * Set a flag if the mutex is used on the thread we are watching, but always michael@0: * call the real mutex function. michael@0: */ michael@0: extern "C" void wrapped_MutexEnter(sqlite3_mutex *mutex) michael@0: { michael@0: PRThread *curThread = ::PR_GetCurrentThread(); michael@0: if (curThread == watched_thread) michael@0: mutex_used_on_watched_thread = true; michael@0: else michael@0: last_non_watched_thread = curThread; michael@0: orig_mutex_methods.xMutexEnter(mutex); michael@0: } michael@0: michael@0: extern "C" int wrapped_MutexTry(sqlite3_mutex *mutex) michael@0: { michael@0: if (::PR_GetCurrentThread() == watched_thread) michael@0: mutex_used_on_watched_thread = true; michael@0: return orig_mutex_methods.xMutexTry(mutex); michael@0: } michael@0: michael@0: michael@0: #define do_check_ok(aInvoc) do_check_true((aInvoc) == SQLITE_OK) michael@0: michael@0: void hook_sqlite_mutex() michael@0: { michael@0: // We need to initialize and teardown SQLite to get it to set up the michael@0: // default mutex handlers for us so we can steal them and wrap them. michael@0: do_check_ok(sqlite3_initialize()); michael@0: do_check_ok(sqlite3_shutdown()); michael@0: do_check_ok(::sqlite3_config(SQLITE_CONFIG_GETMUTEX, &orig_mutex_methods)); michael@0: do_check_ok(::sqlite3_config(SQLITE_CONFIG_GETMUTEX, &wrapped_mutex_methods)); michael@0: wrapped_mutex_methods.xMutexEnter = wrapped_MutexEnter; michael@0: wrapped_mutex_methods.xMutexTry = wrapped_MutexTry; michael@0: do_check_ok(::sqlite3_config(SQLITE_CONFIG_MUTEX, &wrapped_mutex_methods)); michael@0: } michael@0: michael@0: /** michael@0: * Call to clear the watch state and to set the watching against this thread. michael@0: * michael@0: * Check |mutex_used_on_watched_thread| to see if the mutex has fired since michael@0: * this method was last called. Since we're talking about the current thread, michael@0: * there are no race issues to be concerned about michael@0: */ michael@0: void watch_for_mutex_use_on_this_thread() michael@0: { michael@0: watched_thread = ::PR_GetCurrentThread(); michael@0: mutex_used_on_watched_thread = false; michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Thread Wedgers michael@0: michael@0: /** michael@0: * A runnable that blocks until code on another thread invokes its unwedge michael@0: * method. By dispatching this to a thread you can ensure that no subsequent michael@0: * runnables dispatched to the thread will execute until you invoke unwedge. michael@0: * michael@0: * The wedger is self-dispatching, just construct it with its target. michael@0: */ michael@0: class ThreadWedger : public nsRunnable michael@0: { michael@0: public: michael@0: ThreadWedger(nsIEventTarget *aTarget) michael@0: : mReentrantMonitor("thread wedger") michael@0: , unwedged(false) michael@0: { michael@0: aTarget->Dispatch(this, aTarget->NS_DISPATCH_NORMAL); michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: ReentrantMonitorAutoEnter automon(mReentrantMonitor); michael@0: michael@0: if (!unwedged) michael@0: automon.Wait(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void unwedge() michael@0: { michael@0: ReentrantMonitorAutoEnter automon(mReentrantMonitor); michael@0: unwedged = true; michael@0: automon.Notify(); michael@0: } michael@0: michael@0: private: michael@0: ReentrantMonitor mReentrantMonitor; michael@0: bool unwedged; michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Async Helpers michael@0: michael@0: /** michael@0: * A horrible hack to figure out what the connection's async thread is. By michael@0: * creating a statement and async dispatching we can tell from the mutex who michael@0: * is the async thread, PRThread style. Then we map that to an nsIThread. michael@0: */ michael@0: already_AddRefed michael@0: get_conn_async_thread(mozIStorageConnection *db) michael@0: { michael@0: // Make sure we are tracking the current thread as the watched thread michael@0: watch_for_mutex_use_on_this_thread(); michael@0: michael@0: // - statement with nothing to bind michael@0: nsCOMPtr stmt; michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("SELECT 1"), michael@0: getter_AddRefs(stmt)); michael@0: blocking_async_execute(stmt); michael@0: stmt->Finalize(); michael@0: michael@0: nsCOMPtr threadMan = michael@0: do_GetService("@mozilla.org/thread-manager;1"); michael@0: nsCOMPtr asyncThread; michael@0: threadMan->GetThreadFromPRThread(last_non_watched_thread, michael@0: getter_AddRefs(asyncThread)); michael@0: michael@0: // Additionally, check that the thread we get as the background thread is the michael@0: // same one as the one we report from getInterface. michael@0: nsCOMPtr target = do_GetInterface(db); michael@0: nsCOMPtr allegedAsyncThread = do_QueryInterface(target); michael@0: PRThread *allegedPRThread; michael@0: (void)allegedAsyncThread->GetPRThread(&allegedPRThread); michael@0: do_check_eq(allegedPRThread, last_non_watched_thread); michael@0: return asyncThread.forget(); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Tests michael@0: michael@0: void michael@0: test_TrueAsyncStatement() michael@0: { michael@0: // (only the first test needs to call this) michael@0: hook_sqlite_mutex(); michael@0: michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // Start watching for forbidden mutex usage. michael@0: watch_for_mutex_use_on_this_thread(); michael@0: michael@0: // - statement with nothing to bind michael@0: nsCOMPtr stmt; michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("CREATE TABLE test (id INTEGER PRIMARY KEY)"), michael@0: getter_AddRefs(stmt) michael@0: ); michael@0: blocking_async_execute(stmt); michael@0: stmt->Finalize(); michael@0: do_check_false(mutex_used_on_watched_thread); michael@0: michael@0: // - statement with something to bind ordinally michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("INSERT INTO test (id) VALUES (?)"), michael@0: getter_AddRefs(stmt) michael@0: ); michael@0: stmt->BindInt32ByIndex(0, 1); michael@0: blocking_async_execute(stmt); michael@0: stmt->Finalize(); michael@0: do_check_false(mutex_used_on_watched_thread); michael@0: michael@0: // - statement with something to bind by name michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("INSERT INTO test (id) VALUES (:id)"), michael@0: getter_AddRefs(stmt) michael@0: ); michael@0: nsCOMPtr paramsArray; michael@0: stmt->NewBindingParamsArray(getter_AddRefs(paramsArray)); michael@0: nsCOMPtr params; michael@0: paramsArray->NewBindingParams(getter_AddRefs(params)); michael@0: params->BindInt32ByName(NS_LITERAL_CSTRING("id"), 2); michael@0: paramsArray->AddParams(params); michael@0: params = nullptr; michael@0: stmt->BindParameters(paramsArray); michael@0: paramsArray = nullptr; michael@0: blocking_async_execute(stmt); michael@0: stmt->Finalize(); michael@0: do_check_false(mutex_used_on_watched_thread); michael@0: michael@0: // - now, make sure creating a sync statement does trigger our guard. michael@0: // (If this doesn't happen, our test is bunk and it's important to know that.) michael@0: nsCOMPtr syncStmt; michael@0: db->CreateStatement(NS_LITERAL_CSTRING("SELECT * FROM test"), michael@0: getter_AddRefs(syncStmt)); michael@0: syncStmt->Finalize(); michael@0: do_check_true(mutex_used_on_watched_thread); michael@0: michael@0: blocking_async_close(db); michael@0: } michael@0: michael@0: /** michael@0: * Test that cancellation before a statement is run successfully stops the michael@0: * statement from executing. michael@0: */ michael@0: void michael@0: test_AsyncCancellation() michael@0: { michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: michael@0: // -- wedge the thread michael@0: nsCOMPtr target(get_conn_async_thread(db)); michael@0: do_check_true(target); michael@0: nsRefPtr wedger (new ThreadWedger(target)); michael@0: michael@0: // -- create statements and cancel them michael@0: // - async michael@0: nsCOMPtr asyncStmt; michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("CREATE TABLE asyncTable (id INTEGER PRIMARY KEY)"), michael@0: getter_AddRefs(asyncStmt) michael@0: ); michael@0: michael@0: nsRefPtr asyncSpin(new AsyncStatementSpinner()); michael@0: nsCOMPtr asyncPend; michael@0: (void)asyncStmt->ExecuteAsync(asyncSpin, getter_AddRefs(asyncPend)); michael@0: do_check_true(asyncPend); michael@0: asyncPend->Cancel(); michael@0: michael@0: // - sync michael@0: nsCOMPtr syncStmt; michael@0: db->CreateStatement( michael@0: NS_LITERAL_CSTRING("CREATE TABLE syncTable (id INTEGER PRIMARY KEY)"), michael@0: getter_AddRefs(syncStmt) michael@0: ); michael@0: michael@0: nsRefPtr syncSpin(new AsyncStatementSpinner()); michael@0: nsCOMPtr syncPend; michael@0: (void)syncStmt->ExecuteAsync(syncSpin, getter_AddRefs(syncPend)); michael@0: do_check_true(syncPend); michael@0: syncPend->Cancel(); michael@0: michael@0: // -- unwedge the async thread michael@0: wedger->unwedge(); michael@0: michael@0: // -- verify that both statements report they were canceled michael@0: asyncSpin->SpinUntilCompleted(); michael@0: do_check_true(asyncSpin->completionReason == michael@0: mozIStorageStatementCallback::REASON_CANCELED); michael@0: michael@0: syncSpin->SpinUntilCompleted(); michael@0: do_check_true(syncSpin->completionReason == michael@0: mozIStorageStatementCallback::REASON_CANCELED); michael@0: michael@0: // -- verify that neither statement constructed their tables michael@0: nsresult rv; michael@0: bool exists; michael@0: rv = db->TableExists(NS_LITERAL_CSTRING("asyncTable"), &exists); michael@0: do_check_true(rv == NS_OK); michael@0: do_check_false(exists); michael@0: rv = db->TableExists(NS_LITERAL_CSTRING("syncTable"), &exists); michael@0: do_check_true(rv == NS_OK); michael@0: do_check_false(exists); michael@0: michael@0: // -- cleanup michael@0: asyncStmt->Finalize(); michael@0: syncStmt->Finalize(); michael@0: blocking_async_close(db); michael@0: } michael@0: michael@0: /** michael@0: * Test that the destructor for an asynchronous statement which has a michael@0: * sqlite3_stmt will dispatch that statement to the async thread for michael@0: * finalization rather than trying to finalize it on the main thread michael@0: * (and thereby running afoul of our mutex use detector). michael@0: */ michael@0: void test_AsyncDestructorFinalizesOnAsyncThread() michael@0: { michael@0: // test_TrueAsyncStatement called hook_sqlite_mutex() for us michael@0: michael@0: nsCOMPtr db(getMemoryDatabase()); michael@0: watch_for_mutex_use_on_this_thread(); michael@0: michael@0: // -- create an async statement michael@0: nsCOMPtr stmt; michael@0: db->CreateAsyncStatement( michael@0: NS_LITERAL_CSTRING("CREATE TABLE test (id INTEGER PRIMARY KEY)"), michael@0: getter_AddRefs(stmt) michael@0: ); michael@0: michael@0: // -- execute it so it gets a sqlite3_stmt that needs to be finalized michael@0: blocking_async_execute(stmt); michael@0: do_check_false(mutex_used_on_watched_thread); michael@0: michael@0: // -- forget our reference michael@0: stmt = nullptr; michael@0: michael@0: // -- verify the mutex was not touched michael@0: do_check_false(mutex_used_on_watched_thread); michael@0: michael@0: // -- make sure the statement actually gets finalized / cleanup michael@0: // the close will assert if we failed to finalize! michael@0: blocking_async_close(db); michael@0: } michael@0: michael@0: void (*gTests[])(void) = { michael@0: // this test must be first because it hooks the mutex mechanics michael@0: test_TrueAsyncStatement, michael@0: test_AsyncCancellation, michael@0: test_AsyncDestructorFinalizesOnAsyncThread michael@0: }; michael@0: michael@0: const char *file = __FILE__; michael@0: #define TEST_NAME "true async statement" michael@0: #define TEST_FILE file michael@0: #include "storage_test_harness_tail.h"