Bug Summary

File:var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp
Warning:line 1544, column 5
Value stored to 'srv' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name mozStorageConnection.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/storage -fcoverage-compilation-dir=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/storage -resource-dir /usr/lib/llvm-18/lib/clang/18 -include /var/lib/jenkins/workspace/firefox-scan-build/config/gcc_hidden.h -include /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D MOZ_SQLITE_PERSIST_AUXILIARY_FILES=1 -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D STATIC_EXPORTABLE_JS_API -I /var/lib/jenkins/workspace/firefox-scan-build/storage -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/storage -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/ipc/ipdl/_ipdlheaders -I /var/lib/jenkins/workspace/firefox-scan-build/ipc/chromium/src -I /var/lib/jenkins/workspace/firefox-scan-build/dom/base -I /var/lib/jenkins/workspace/firefox-scan-build/third_party/sqlite3/src -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -fdeprecated-macro -ferror-limit 19 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-05-16-034744-15991-1 -x c++ /var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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#include "BaseVFS.h"
8#include "ErrorList.h"
9#include "nsError.h"
10#include "nsThreadUtils.h"
11#include "nsIFile.h"
12#include "nsIFileURL.h"
13#include "nsIXPConnect.h"
14#include "mozilla/AppShutdown.h"
15#include "mozilla/CheckedInt.h"
16#include "mozilla/Telemetry.h"
17#include "mozilla/Mutex.h"
18#include "mozilla/CondVar.h"
19#include "mozilla/Attributes.h"
20#include "mozilla/ErrorNames.h"
21#include "mozilla/Unused.h"
22#include "mozilla/dom/quota/QuotaObject.h"
23#include "mozilla/ScopeExit.h"
24#include "mozilla/SpinEventLoopUntil.h"
25#include "mozilla/StaticPrefs_storage.h"
26
27#include "mozIStorageCompletionCallback.h"
28#include "mozIStorageFunction.h"
29
30#include "mozStorageAsyncStatementExecution.h"
31#include "mozStorageSQLFunctions.h"
32#include "mozStorageConnection.h"
33#include "mozStorageService.h"
34#include "mozStorageStatement.h"
35#include "mozStorageAsyncStatement.h"
36#include "mozStorageArgValueArray.h"
37#include "mozStoragePrivateHelpers.h"
38#include "mozStorageStatementData.h"
39#include "ObfuscatingVFS.h"
40#include "QuotaVFS.h"
41#include "StorageBaseStatementInternal.h"
42#include "SQLCollations.h"
43#include "FileSystemModule.h"
44#include "mozStorageHelper.h"
45
46#include "mozilla/Assertions.h"
47#include "mozilla/Logging.h"
48#include "mozilla/Printf.h"
49#include "mozilla/ProfilerLabels.h"
50#include "mozilla/RefPtr.h"
51#include "nsComponentManagerUtils.h"
52#include "nsProxyRelease.h"
53#include "nsStringFwd.h"
54#include "nsURLHelper.h"
55
56#define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH524288000 524288000 // 500 MiB
57
58// Maximum size of the pages cache per connection.
59#define MAX_CACHE_SIZE_KIBIBYTES2048 2048 // 2 MiB
60
61mozilla::LazyLogModule gStorageLog("mozStorage");
62
63// Checks that the protected code is running on the main-thread only if the
64// connection was also opened on it.
65#ifdef DEBUG1
66# define CHECK_MAINTHREAD_ABUSE()do { do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 66); } } while (false); } while (0)
\
67 do { \
68 NS_WARNING_ASSERTION( \do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 72); } } while (false)
69 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 72); } } while (false)
70 !NS_IsMainThread(), \do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 72); } } while (false)
71 "Using Storage synchronous API on main-thread, but " \do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 72); } } while (false)
72 "the connection was opened on another thread.")do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 72); } } while (false)
; \
73 } while (0)
74#else
75# define CHECK_MAINTHREAD_ABUSE()do { do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 75); } } while (false); } while (0)
\
76 do { /* Nothing */ \
77 } while (0)
78#endif
79
80namespace mozilla::storage {
81
82using mozilla::dom::quota::QuotaObject;
83using mozilla::Telemetry::AccumulateCategoricalKeyed;
84using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
85using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
86
87namespace {
88
89int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
90 if (NS_SUCCEEDED(aXPCOMResultCode)((bool)(__builtin_expect(!!(!NS_FAILED_impl(aXPCOMResultCode)
), 1)))
) {
91 return SQLITE_OK0;
92 }
93
94 switch (aXPCOMResultCode) {
95 case NS_ERROR_FILE_CORRUPTED:
96 return SQLITE_CORRUPT11;
97 case NS_ERROR_FILE_ACCESS_DENIED:
98 return SQLITE_CANTOPEN14;
99 case NS_ERROR_STORAGE_BUSY:
100 return SQLITE_BUSY5;
101 case NS_ERROR_FILE_IS_LOCKED:
102 return SQLITE_LOCKED6;
103 case NS_ERROR_FILE_READ_ONLY:
104 return SQLITE_READONLY8;
105 case NS_ERROR_STORAGE_IOERR:
106 return SQLITE_IOERR10;
107 case NS_ERROR_FILE_NO_DEVICE_SPACE:
108 return SQLITE_FULL13;
109 case NS_ERROR_OUT_OF_MEMORY:
110 return SQLITE_NOMEM7;
111 case NS_ERROR_UNEXPECTED:
112 return SQLITE_MISUSE21;
113 case NS_ERROR_ABORT:
114 return SQLITE_ABORT4;
115 case NS_ERROR_STORAGE_CONSTRAINT:
116 return SQLITE_CONSTRAINT19;
117 default:
118 return SQLITE_ERROR1;
119 }
120
121 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!")do { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"Must return in switch above!" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 121); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "Must return in switch above!" ")"
); do { *((volatile int*)__null) = 121; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); __builtin_unreachable
(); } while (false)
;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125//// Variant Specialization Functions (variantToSQLiteT)
126
127int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
128 ::sqlite3_result_int(aCtx, aValue);
129 return SQLITE_OK0;
130}
131
132int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
133 ::sqlite3_result_int64(aCtx, aValue);
134 return SQLITE_OK0;
135}
136
137int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
138 ::sqlite3_result_double(aCtx, aValue);
139 return SQLITE_OK0;
140}
141
142int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
143 CheckedInt<int32_t> length(aValue.Length());
144 if (!length.isValid()) {
145 return SQLITE_MISUSE21;
146 }
147 ::sqlite3_result_text(aCtx, aValue.get(), length.value(), SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
148 return SQLITE_OK0;
149}
150
151int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
152 CheckedInt<int32_t> n_bytes =
153 CheckedInt<int32_t>(aValue.Length()) * sizeof(char16_t);
154 if (!n_bytes.isValid()) {
155 return SQLITE_MISUSE21;
156 }
157 ::sqlite3_result_text16(aCtx, aValue.get(), n_bytes.value(),
158 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
159 return SQLITE_OK0;
160}
161
162int sqlite3_T_null(sqlite3_context* aCtx) {
163 ::sqlite3_result_null(aCtx);
164 return SQLITE_OK0;
165}
166
167int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
168 ::sqlite3_result_blob(aCtx, aData, aSize, free);
169 return SQLITE_OK0;
170}
171
172#include "variantToSQLiteT_impl.h"
173
174////////////////////////////////////////////////////////////////////////////////
175//// Modules
176
177struct Module {
178 const char* name;
179 int (*registerFunc)(sqlite3*, const char*);
180};
181
182Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
183
184////////////////////////////////////////////////////////////////////////////////
185//// Local Functions
186
187int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
188 switch (aReason) {
189 case SQLITE_TRACE_STMT0x01: {
190 // aP is a pointer to the prepared statement.
191 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
192 // aX is a pointer to a string containing the unexpanded SQL or a comment,
193 // starting with "--"" in case of a trigger.
194 char* expanded = static_cast<char*>(aX);
195 // Simulate what sqlite_trace was doing.
196 if (!::strncmp(expanded, "--", 2)) {
197 MOZ_LOG(gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_STMT on %p: '%s'", aClosure, expanded
); } } while (0)
198 ("TRACE_STMT on %p: '%s'", aClosure, expanded))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_STMT on %p: '%s'", aClosure, expanded
); } } while (0)
;
199 } else {
200 char* sql = ::sqlite3_expanded_sql(stmt);
201 MOZ_LOG(gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_STMT on %p: '%s'", aClosure, sql); }
} while (0)
202 ("TRACE_STMT on %p: '%s'", aClosure, sql))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_STMT on %p: '%s'", aClosure, sql); }
} while (0)
;
203 ::sqlite3_free(sql);
204 }
205 break;
206 }
207 case SQLITE_TRACE_PROFILE0x02: {
208 // aX is pointer to a 64bit integer containing nanoseconds it took to
209 // execute the last command.
210 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
211 if (time > 0) {
212 MOZ_LOG(gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_TIME on %p: %lldms", aClosure, time
); } } while (0)
213 ("TRACE_TIME on %p: %lldms", aClosure, time))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "TRACE_TIME on %p: %lldms", aClosure, time
); } } while (0)
;
214 }
215 break;
216 }
217 }
218 return 0;
219}
220
221void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
222 sqlite3_value** aArgv) {
223 void* userData = ::sqlite3_user_data(aCtx);
224
225 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
226
227 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
228 if (!arguments) return;
229
230 nsCOMPtr<nsIVariant> result;
231 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
232 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
233 nsAutoCString errorMessage;
234 GetErrorName(rv, errorMessage);
235 errorMessage.InsertLiteral("User function returned ", 0);
236 errorMessage.Append('!');
237
238 NS_WARNING(errorMessage.get())NS_DebugBreak(NS_DEBUG_WARNING, errorMessage.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 238)
;
239
240 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
241 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
242 return;
243 }
244 int retcode = variantToSQLiteT(aCtx, result);
245 if (retcode != SQLITE_OK0) {
246 NS_WARNING("User function returned invalid data type!")NS_DebugBreak(NS_DEBUG_WARNING, "User function returned invalid data type!"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 246)
;
247 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
248 -1);
249 }
250}
251
252RefPtr<QuotaObject> GetQuotaObject(sqlite3_file* aFile, bool obfuscatingVFS) {
253 return obfuscatingVFS
254 ? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile)
255 : mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile);
256}
257
258/**
259 * This code is heavily based on the sample at:
260 * http://www.sqlite.org/unlock_notify.html
261 */
262class UnlockNotification {
263 public:
264 UnlockNotification()
265 : mMutex("UnlockNotification mMutex"),
266 mCondVar(mMutex, "UnlockNotification condVar"),
267 mSignaled(false) {}
268
269 void Wait() {
270 MutexAutoLock lock(mMutex);
271 while (!mSignaled) {
272 (void)mCondVar.Wait();
273 }
274 }
275
276 void Signal() {
277 MutexAutoLock lock(mMutex);
278 mSignaled = true;
279 (void)mCondVar.Notify();
280 }
281
282 private:
283 Mutex mMutex MOZ_UNANNOTATED;
284 CondVar mCondVar;
285 bool mSignaled;
286};
287
288void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
289 for (int i = 0; i < aArgsSize; i++) {
290 UnlockNotification* notification =
291 static_cast<UnlockNotification*>(aArgs[i]);
292 notification->Signal();
293 }
294}
295
296int WaitForUnlockNotify(sqlite3* aDatabase) {
297 UnlockNotification notification;
298 int srv =
299 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
300 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(srv == 6 || srv == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(srv == 6 || srv == 0))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("srv == 6 || srv == 0"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 300); AnnotateMozCrashReason("MOZ_ASSERT" "(" "srv == 6 || srv == 0"
")"); do { *((volatile int*)__null) = 300; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
301 if (srv == SQLITE_OK0) {
302 notification.Wait();
303 }
304
305 return srv;
306}
307
308////////////////////////////////////////////////////////////////////////////////
309//// Local Classes
310
311class AsyncCloseConnection final : public Runnable {
312 public:
313 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
314 nsIRunnable* aCallbackEvent)
315 : Runnable("storage::AsyncCloseConnection"),
316 mConnection(aConnection),
317 mNativeConnection(aNativeConnection),
318 mCallbackEvent(aCallbackEvent) {}
319
320 NS_IMETHODvirtual nsresult Run() override {
321 // Make sure we don't dispatch to the current thread.
322 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn
))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 322); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 322; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
323
324 nsCOMPtr<nsIRunnable> event =
325 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
326 mConnection, &Connection::shutdownAsyncThread);
327 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event))do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(NS_DispatchToMainThread(event))), 1)))), 1))) { } else { do {
static_assert( mozilla::detail::AssertionConditionType<decltype
(false)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(NS_DispatchToMainThread(event))" ")"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 327); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(NS_DispatchToMainThread(event))" ")"); do
{ *((volatile int*)__null) = 327; __attribute__((nomerge)) ::
abort(); } while (false); } } while (false); } } while (false
)
;
328
329 // Internal close.
330 (void)mConnection->internalClose(mNativeConnection);
331
332 // Callback
333 if (mCallbackEvent) {
334 nsCOMPtr<nsIThread> thread;
335 (void)NS_GetMainThread(getter_AddRefs(thread));
336 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
337 }
338
339 return NS_OK;
340 }
341
342 ~AsyncCloseConnection() override {
343 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
344 mConnection.forget());
345 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
346 mCallbackEvent.forget());
347 }
348
349 private:
350 RefPtr<Connection> mConnection;
351 sqlite3* mNativeConnection;
352 nsCOMPtr<nsIRunnable> mCallbackEvent;
353};
354
355/**
356 * An event used to initialize the clone of a connection.
357 *
358 * Must be executed on the clone's async execution thread.
359 */
360class AsyncInitializeClone final : public Runnable {
361 public:
362 /**
363 * @param aConnection The connection being cloned.
364 * @param aClone The clone.
365 * @param aReadOnly If |true|, the clone is read only.
366 * @param aCallback A callback to trigger once initialization
367 * is complete. This event will be called on
368 * aClone->eventTargetOpenedOn.
369 */
370 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
371 const bool aReadOnly,
372 mozIStorageCompletionCallback* aCallback)
373 : Runnable("storage::AsyncInitializeClone"),
374 mConnection(aConnection),
375 mClone(aClone),
376 mReadOnly(aReadOnly),
377 mCallback(aCallback) {
378 MOZ_ASSERT(NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 378); AnnotateMozCrashReason("MOZ_ASSERT" "(" "NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 378; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
379 }
380
381 NS_IMETHODvirtual nsresult Run() override {
382 MOZ_ASSERT(!NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("!NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 382); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 382; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
383 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
384 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
385 return Dispatch(rv, nullptr);
386 }
387 return Dispatch(NS_OK,
388 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone)static_cast<nsISupports*>(static_cast<mozIStorageAsyncConnection
*>(mClone))
);
389 }
390
391 private:
392 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
393 RefPtr<CallbackComplete> event =
394 new CallbackComplete(aResult, aValue, mCallback.forget());
395 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
396 }
397
398 ~AsyncInitializeClone() override {
399 nsCOMPtr<nsIThread> thread;
400 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
401 MOZ_ASSERT(NS_SUCCEEDED(rv))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 401); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
")"); do { *((volatile int*)__null) = 401; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
402
403 // Handle ambiguous nsISupports inheritance.
404 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
405 mConnection.forget());
406 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
407
408 // Generally, the callback will be released by CallbackComplete.
409 // However, if for some reason Run() is not executed, we still
410 // need to ensure that it is released here.
411 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
412 mCallback.forget());
413 }
414
415 RefPtr<Connection> mConnection;
416 RefPtr<Connection> mClone;
417 const bool mReadOnly;
418 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
419};
420
421/**
422 * A listener for async connection closing.
423 */
424class CloseListener final : public mozIStorageCompletionCallback {
425 public:
426 NS_DECL_ISUPPORTSpublic: virtual nsresult QueryInterface(const nsIID& aIID
, void** aInstancePtr) override; virtual MozExternalRefCountType
AddRef(void) override; virtual MozExternalRefCountType Release
(void) override; using HasThreadSafeRefCnt = std::false_type;
protected: nsAutoRefCnt mRefCnt; nsAutoOwningThread _mOwningThread
; public:
427 CloseListener() : mClosed(false) {}
428
429 NS_IMETHODvirtual nsresult Complete(nsresult, nsISupports*) override {
430 mClosed = true;
431 return NS_OK;
432 }
433
434 bool mClosed;
435
436 private:
437 ~CloseListener() = default;
438};
439
440NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)MozExternalRefCountType CloseListener::AddRef(void) { static_assert
(!std::is_destructible_v<CloseListener>, "Reference-counted class "
"CloseListener" " should not have a public destructor. " "Make this class's destructor non-public"
); do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 440); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { *((volatile int*)__null) =
440; __attribute__((nomerge)) ::abort(); } while (false); } }
while (false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("CloseListener" != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!("CloseListener" != nullptr))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("\"CloseListener\" != nullptr"
" (" "Must specify a name" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 440); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"CloseListener\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 440; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("CloseListener" " not thread-safe"); nsrefcnt
count = ++mRefCnt; NS_LogAddRef((this), (count), ("CloseListener"
), (uint32_t)(sizeof(*this))); return count; } MozExternalRefCountType
CloseListener::Release(void) { do { static_assert( mozilla::
detail::AssertionConditionType<decltype(int32_t(mRefCnt) >
0)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(int32_t(mRefCnt) > 0))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0" " (" "dup release"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 440); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0"
") (" "dup release" ")"); do { *((volatile int*)__null) = 440
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("CloseListener" != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!("CloseListener" != nullptr))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("\"CloseListener\" != nullptr"
" (" "Must specify a name" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 440); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"CloseListener\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 440; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("CloseListener" " not thread-safe"); const char
* const nametmp = "CloseListener"; nsrefcnt count = --mRefCnt
; NS_LogRelease((this), (count), (nametmp)); if (count == 0) {
mRefCnt = 1; delete (this); return 0; } return count; } nsresult
CloseListener::QueryInterface(const nsIID& aIID, void** aInstancePtr
) { do { if (!(aInstancePtr)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "QueryInterface requires a non-NULL destination!", "aInstancePtr"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 440); MOZ_PretendNoReturn(); } } while (0); nsresult rv = NS_ERROR_FAILURE
; static_assert(1 > 0, "Need more arguments to NS_INTERFACE_TABLE"
); static const QITableEntry table[] = { {&mozilla::detail
::kImplementedIID<CloseListener, mozIStorageCompletionCallback
>, int32_t( reinterpret_cast<char*>(static_cast<mozIStorageCompletionCallback
*>((CloseListener*)0x1000)) - reinterpret_cast<char*>
((CloseListener*)0x1000))}, {&mozilla::detail::kImplementedIID
<CloseListener, nsISupports>, int32_t(reinterpret_cast<
char*>(static_cast<nsISupports*>( static_cast<mozIStorageCompletionCallback
*>((CloseListener*)0x1000))) - reinterpret_cast<char*>
((CloseListener*)0x1000))}, { nullptr, 0 } } ; static_assert(
(sizeof(table) / sizeof(table[0])) > 1, "need at least 1 interface"
); rv = NS_TableDrivenQI(static_cast<void*>(this), aIID
, aInstancePtr, table); return rv; }
441
442class AsyncVacuumEvent final : public Runnable {
443 public:
444 AsyncVacuumEvent(Connection* aConnection,
445 mozIStorageCompletionCallback* aCallback,
446 bool aUseIncremental, int32_t aSetPageSize)
447 : Runnable("storage::AsyncVacuum"),
448 mConnection(aConnection),
449 mCallback(aCallback),
450 mUseIncremental(aUseIncremental),
451 mSetPageSize(aSetPageSize),
452 mStatus(NS_ERROR_UNEXPECTED) {}
453
454 NS_IMETHODvirtual nsresult Run() override {
455 // This is initially dispatched to the helper thread, then re-dispatched
456 // to the opener thread, where it will callback.
457 if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
458 // Send the completion event.
459 if (mCallback) {
460 mozilla::Unused << mCallback->Complete(mStatus, nullptr);
461 }
462 return NS_OK;
463 }
464
465 // Ensure to invoke the callback regardless of errors.
466 auto guard = MakeScopeExit([&]() {
467 mConnection->mIsStatementOnHelperThreadInterruptible = false;
468 mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
469 this, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
470 });
471
472 // Get list of attached databases.
473 nsCOMPtr<mozIStorageStatement> stmt;
474 nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
475 "PRAGMA database_list"_ns,
476 getter_AddRefs(stmt));
477 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 477); return rv; } } while (false)
;
478 // We must accumulate names and loop through them later, otherwise VACUUM
479 // will see an ongoing statement and bail out.
480 nsTArray<nsCString> schemaNames;
481 bool hasResult = false;
482 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
483 nsAutoCString name;
484 rv = stmt->GetUTF8String(1, name);
485 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !name.EqualsLiteral("temp")) {
486 schemaNames.AppendElement(name);
487 }
488 }
489 mStatus = NS_OK;
490 // Mark this vacuum as an interruptible operation, so it can be interrupted
491 // if the connection closes during shutdown.
492 mConnection->mIsStatementOnHelperThreadInterruptible = true;
493 for (const nsCString& schemaName : schemaNames) {
494 rv = this->Vacuum(schemaName);
495 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
496 // This is sub-optimal since it's only keeping the last error reason,
497 // but it will do for now.
498 mStatus = rv;
499 }
500 }
501 return mStatus;
502 }
503
504 nsresult Vacuum(const nsACString& aSchemaName) {
505 // Abort if we're in shutdown.
506 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
507 return NS_ERROR_ABORT;
508 }
509 int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
510 if (!removablePages) {
511 // There's no empty pages to remove, so skip this vacuum for now.
512 return NS_OK;
513 }
514 nsresult rv;
515 bool needsFullVacuum = true;
516
517 if (mSetPageSize) {
518 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
519 query.Append(aSchemaName);
520 query.AppendLiteral(".page_size = ");
521 query.AppendInt(mSetPageSize);
522 nsCOMPtr<mozIStorageStatement> stmt;
523 rv = mConnection->ExecuteSimpleSQL(query);
524 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 524); return rv; } } while (false)
;
525 }
526
527 // Check auto_vacuum.
528 {
529 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
530 query.Append(aSchemaName);
531 query.AppendLiteral(".auto_vacuum");
532 nsCOMPtr<mozIStorageStatement> stmt;
533 rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
534 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 534); return rv; } } while (false)
;
535 bool hasResult = false;
536 bool changeAutoVacuum = false;
537 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
538 bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
539 changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
540 if (isIncrementalVacuum && !changeAutoVacuum) {
541 needsFullVacuum = false;
542 }
543 }
544 // Changing auto_vacuum is only supported on the main schema.
545 if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
546 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
547 query.Append(aSchemaName);
548 query.AppendLiteral(".auto_vacuum = ");
549 query.AppendInt(mUseIncremental ? 2 : 0);
550 rv = mConnection->ExecuteSimpleSQL(query);
551 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 551); return rv; } } while (false)
;
552 }
553 }
554
555 if (needsFullVacuum) {
556 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"VACUUM ");
557 query.Append(aSchemaName);
558 rv = mConnection->ExecuteSimpleSQL(query);
559 // TODO (Bug 1818039): Report failed vacuum telemetry.
560 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 560); return rv; } } while (false)
;
561 } else {
562 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
563 query.Append(aSchemaName);
564 query.AppendLiteral(".incremental_vacuum(");
565 query.AppendInt(removablePages);
566 query.AppendLiteral(")");
567 rv = mConnection->ExecuteSimpleSQL(query);
568 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 568); return rv; } } while (false)
;
569 }
570
571 return NS_OK;
572 }
573
574 ~AsyncVacuumEvent() override {
575 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
576 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
577 }
578
579 private:
580 RefPtr<Connection> mConnection;
581 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
582 bool mUseIncremental;
583 int32_t mSetPageSize;
584 Atomic<nsresult> mStatus;
585};
586
587/**
588 * A runnable to perform an SQLite database backup when there may be one or more
589 * open connections on that database.
590 */
591class AsyncBackupDatabaseFile final : public Runnable, public nsITimerCallback {
592 public:
593 NS_DECL_ISUPPORTS_INHERITEDpublic: virtual nsresult QueryInterface(const nsIID& aIID
, void** aInstancePtr) override; virtual MozExternalRefCountType
AddRef(void) override; virtual MozExternalRefCountType Release
(void) override;
594
595 /**
596 * @param aConnection The connection to the database being backed up.
597 * @param aNativeConnection The native connection to the database being backed
598 * up.
599 * @param aDestinationFile The destination file for the created backup.
600 * @param aCallback A callback to trigger once the backup process has
601 * completed. The callback will be supplied with an nsresult
602 * indicating whether or not the backup was successfully
603 * created. This callback will be called on the
604 * mConnection->eventTargetOpenedOn thread.
605 * @throws
606 */
607 AsyncBackupDatabaseFile(Connection* aConnection, sqlite3* aNativeConnection,
608 nsIFile* aDestinationFile,
609 mozIStorageCompletionCallback* aCallback,
610 int32_t aPagesPerStep, uint32_t aStepDelayMs)
611 : Runnable("storage::AsyncBackupDatabaseFile"),
612 mConnection(aConnection),
613 mNativeConnection(aNativeConnection),
614 mDestinationFile(aDestinationFile),
615 mCallback(aCallback),
616 mPagesPerStep(aPagesPerStep),
617 mStepDelayMs(aStepDelayMs),
618 mBackupFile(nullptr),
619 mBackupHandle(nullptr) {
620 MOZ_ASSERT(NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 620); AnnotateMozCrashReason("MOZ_ASSERT" "(" "NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 620; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
621 }
622
623 NS_IMETHODvirtual nsresult Run() override {
624 MOZ_ASSERT(!NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("!NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 624); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 624; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
625
626 nsAutoString path;
627 nsresult rv = mDestinationFile->GetPath(path);
628 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
629 return Dispatch(rv, nullptr);
630 }
631 // Put a .tmp on the end of the destination while the backup is underway.
632 // This extension will be stripped off after the backup successfully
633 // completes.
634 path.AppendLiteral(".tmp");
635
636 int srv = ::sqlite3_open(NS_ConvertUTF16toUTF8(path).get(), &mBackupFile);
637 if (srv != SQLITE_OK0) {
638 return Dispatch(NS_ERROR_FAILURE, nullptr);
639 }
640
641 static const char* mainDBName = "main";
642
643 mBackupHandle = ::sqlite3_backup_init(mBackupFile, mainDBName,
644 mNativeConnection, mainDBName);
645 if (!mBackupHandle) {
646 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK)do { if ((__builtin_expect(!!(::sqlite3_close(mBackupFile) ==
0), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "::sqlite3_close(mBackupFile) == SQLITE_OK"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 646); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "::sqlite3_close(mBackupFile) == SQLITE_OK" ")"); do {
*((volatile int*)__null) = 646; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false); } } while (false)
;
647 return Dispatch(NS_ERROR_FAILURE, nullptr);
648 }
649
650 return DoStep();
651 }
652
653 NS_IMETHODvirtual nsresult
654 Notify(nsITimer* aTimer) override { return DoStep(); }
655
656 private:
657 nsresult DoStep() {
658#define DISPATCH_AND_RETURN_IF_FAILED(rv) \
659 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) { \
660 return Dispatch(rv, nullptr); \
661 }
662
663 // This guard is used to close the backup database in the event of
664 // some failure throughout this process. We release the exit guard
665 // only if we complete the backup successfully, or defer to another
666 // later call to DoStep.
667 auto guard = MakeScopeExit([&]() {
668 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK)do { if ((__builtin_expect(!!(::sqlite3_close(mBackupFile) ==
0), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "::sqlite3_close(mBackupFile) == SQLITE_OK"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 668); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "::sqlite3_close(mBackupFile) == SQLITE_OK" ")"); do {
*((volatile int*)__null) = 668; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false); } } while (false)
;
669 mBackupFile = nullptr;
670 });
671
672 MOZ_ASSERT(!NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("!NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 672); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 672; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
673 nsAutoString originalPath;
674 nsresult rv = mDestinationFile->GetPath(originalPath);
675 DISPATCH_AND_RETURN_IF_FAILED(rv);
676
677 nsAutoString tempPath = originalPath;
678 tempPath.AppendLiteral(".tmp");
679
680 nsCOMPtr<nsIFile> file =
681 do_CreateInstance("@mozilla.org/file/local;1", &rv);
682 DISPATCH_AND_RETURN_IF_FAILED(rv);
683
684 rv = file->InitWithPath(tempPath);
685 DISPATCH_AND_RETURN_IF_FAILED(rv);
686
687 int srv = ::sqlite3_backup_step(mBackupHandle, mPagesPerStep);
688 if (srv == SQLITE_OK0 || srv == SQLITE_BUSY5 || srv == SQLITE_LOCKED6) {
689 // We're continuing the backup later. Release the guard to avoid closing
690 // the database.
691 guard.release();
692 // Queue up the next step
693 return NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, mStepDelayMs,
694 nsITimer::TYPE_ONE_SHOT,
695 GetCurrentSerialEventTarget());
696 }
697#ifdef DEBUG1
698 if (srv != SQLITE_DONE101) {
699 nsCString warnMsg;
700 warnMsg.AppendLiteral(
701 "The SQLite database copy could not be completed due to an error: ");
702 warnMsg.Append(::sqlite3_errmsg(mBackupFile));
703 NS_WARNING(warnMsg.get())NS_DebugBreak(NS_DEBUG_WARNING, warnMsg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 703)
;
704 }
705#endif
706
707 (void)::sqlite3_backup_finish(mBackupHandle);
708 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK)do { if ((__builtin_expect(!!(::sqlite3_close(mBackupFile) ==
0), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "::sqlite3_close(mBackupFile) == SQLITE_OK"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 708); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "::sqlite3_close(mBackupFile) == SQLITE_OK" ")"); do {
*((volatile int*)__null) = 708; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false); } } while (false)
;
709 mBackupFile = nullptr;
710
711 // The database is already closed, so we can release this guard now.
712 guard.release();
713
714 if (srv != SQLITE_DONE101) {
715 NS_WARNING("Failed to create database copy.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to create database copy."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 715)
;
716
717 // The partially created database file is not useful. Let's remove it.
718 rv = file->Remove(false);
719 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
720 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, "Removing a partially backed up SQLite database file failed."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 721)
721 "Removing a partially backed up SQLite database file failed.")NS_DebugBreak(NS_DEBUG_WARNING, "Removing a partially backed up SQLite database file failed."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 721)
;
722 }
723
724 return Dispatch(convertResultCode(srv), nullptr);
725 }
726
727 // Now that we've successfully created the copy, we'll strip off the .tmp
728 // extension.
729
730 nsAutoString leafName;
731 rv = mDestinationFile->GetLeafName(leafName);
732 DISPATCH_AND_RETURN_IF_FAILED(rv);
733
734 rv = file->RenameTo(nullptr, leafName);
735 DISPATCH_AND_RETURN_IF_FAILED(rv);
736
737#undef DISPATCH_AND_RETURN_IF_FAILED
738 return Dispatch(NS_OK, nullptr);
739 }
740
741 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
742 RefPtr<CallbackComplete> event =
743 new CallbackComplete(aResult, aValue, mCallback.forget());
744 return mConnection->eventTargetOpenedOn->Dispatch(event,
745 NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
746 }
747
748 ~AsyncBackupDatabaseFile() override {
749 nsresult rv;
750 nsCOMPtr<nsIThread> thread =
751 do_QueryInterface(mConnection->eventTargetOpenedOn, &rv);
752 MOZ_ASSERT(NS_SUCCEEDED(rv))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 752); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
")"); do { *((volatile int*)__null) = 752; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
753
754 // Handle ambiguous nsISupports inheritance.
755 NS_ProxyRelease("AsyncBackupDatabaseFile::mConnection", thread,
756 mConnection.forget());
757 NS_ProxyRelease("AsyncBackupDatabaseFile::mDestinationFile", thread,
758 mDestinationFile.forget());
759
760 // Generally, the callback will be released by CallbackComplete.
761 // However, if for some reason Run() is not executed, we still
762 // need to ensure that it is released here.
763 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
764 mCallback.forget());
765 }
766
767 RefPtr<Connection> mConnection;
768 sqlite3* mNativeConnection;
769 nsCOMPtr<nsITimer> mTimer;
770 nsCOMPtr<nsIFile> mDestinationFile;
771 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
772 int32_t mPagesPerStep;
773 uint32_t mStepDelayMs;
774 sqlite3* mBackupFile;
775 sqlite3_backup* mBackupHandle;
776};
777
778NS_IMPL_ISUPPORTS_INHERITED(AsyncBackupDatabaseFile, Runnable, nsITimerCallback)nsresult AsyncBackupDatabaseFile::QueryInterface(const nsIID&
aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 778); MOZ_PretendNoReturn(); } } while (0); nsresult rv = NS_ERROR_FAILURE
; static_assert(1 > 0, "Need more arguments to NS_INTERFACE_TABLE_INHERITED"
); static const QITableEntry table[] = { {&mozilla::detail
::kImplementedIID<AsyncBackupDatabaseFile, nsITimerCallback
>, int32_t( reinterpret_cast<char*>(static_cast<nsITimerCallback
*>((AsyncBackupDatabaseFile*)0x1000)) - reinterpret_cast<
char*>((AsyncBackupDatabaseFile*)0x1000))}, { nullptr, 0 }
} ; static_assert((sizeof(table) / sizeof(table[0])) > 1,
"need at least 1 interface"); rv = NS_TableDrivenQI(static_cast
<void*>(this), aIID, aInstancePtr, table); if (((bool)(
__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) return rv; return
Runnable::QueryInterface(aIID, aInstancePtr); } MozExternalRefCountType
AsyncBackupDatabaseFile::AddRef(void) { static_assert(!std::
is_destructible_v<AsyncBackupDatabaseFile>, "Reference-counted class "
"AsyncBackupDatabaseFile" " should not have a public destructor. "
"Make this class's destructor non-public"); nsrefcnt r = Runnable
::AddRef(); if constexpr (::mozilla::detail::ShouldLogInheritedRefcnt
<AsyncBackupDatabaseFile>) { NS_LogAddRef((this), (r), (
"AsyncBackupDatabaseFile"), (uint32_t)(sizeof(*this))); } return
r; } MozExternalRefCountType AsyncBackupDatabaseFile::Release
(void) { nsrefcnt r = Runnable::Release(); if constexpr (::mozilla
::detail::ShouldLogInheritedRefcnt<AsyncBackupDatabaseFile
>) { NS_LogRelease((this), (r), ("AsyncBackupDatabaseFile"
)); } return r; }
779
780} // namespace
781
782////////////////////////////////////////////////////////////////////////////////
783//// Connection
784
785Connection::Connection(Service* aService, int aFlags,
786 ConnectionOperation aSupportedOperations,
787 const nsCString& aTelemetryFilename, bool aInterruptible,
788 bool aIgnoreLockingMode)
789 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
790 sharedDBMutex("Connection::sharedDBMutex"),
791 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
792 mIsStatementOnHelperThreadInterruptible(false),
793 mDBConn(nullptr),
794 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
795 mDestroying(false),
796 mProgressHandler(nullptr),
797 mStorageService(aService),
798 mFlags(aFlags),
799 mTransactionNestingLevel(0),
800 mSupportedOperations(aSupportedOperations),
801 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
802 aInterruptible),
803 mIgnoreLockingMode(aIgnoreLockingMode),
804 mAsyncExecutionThreadShuttingDown(false),
805 mConnectionClosed(false),
806 mGrowthChunkSize(0) {
807 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mIgnoreLockingMode || mFlags & 0x00000001)>::
isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!mIgnoreLockingMode || mFlags & 0x00000001))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("!mIgnoreLockingMode || mFlags & 0x00000001"
" (" "Can't ignore locking for a non-readonly connection!" ")"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 808); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mIgnoreLockingMode || mFlags & 0x00000001"
") (" "Can't ignore locking for a non-readonly connection!" ")"
); do { *((volatile int*)__null) = 808; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
808 "Can't ignore locking for a non-readonly connection!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mIgnoreLockingMode || mFlags & 0x00000001)>::
isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!mIgnoreLockingMode || mFlags & 0x00000001))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("!mIgnoreLockingMode || mFlags & 0x00000001"
" (" "Can't ignore locking for a non-readonly connection!" ")"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 808); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mIgnoreLockingMode || mFlags & 0x00000001"
") (" "Can't ignore locking for a non-readonly connection!" ")"
); do { *((volatile int*)__null) = 808; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
809 mStorageService->registerConnection(this);
810 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!aTelemetryFilename.IsEmpty())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aTelemetryFilename.IsEmpty(
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aTelemetryFilename.IsEmpty()" " (" "A telemetry filename should have been passed-in."
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 811); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aTelemetryFilename.IsEmpty()"
") (" "A telemetry filename should have been passed-in." ")"
); do { *((volatile int*)__null) = 811; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
811 "A telemetry filename should have been passed-in.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!aTelemetryFilename.IsEmpty())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aTelemetryFilename.IsEmpty(
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aTelemetryFilename.IsEmpty()" " (" "A telemetry filename should have been passed-in."
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 811); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aTelemetryFilename.IsEmpty()"
") (" "A telemetry filename should have been passed-in." ")"
); do { *((volatile int*)__null) = 811; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
812 mTelemetryFilename.Assign(aTelemetryFilename);
813}
814
815Connection::~Connection() {
816 // Failsafe Close() occurs in our custom Release method because of
817 // complications related to Close() potentially invoking AsyncClose() which
818 // will increment our refcount.
819 MOZ_ASSERT(!mAsyncExecutionThread,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mAsyncExecutionThread)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mAsyncExecutionThread))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("!mAsyncExecutionThread"
" (" "The async thread has not been shutdown properly!" ")",
"/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 820); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mAsyncExecutionThread"
") (" "The async thread has not been shutdown properly!" ")"
); do { *((volatile int*)__null) = 820; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
820 "The async thread has not been shutdown properly!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mAsyncExecutionThread)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mAsyncExecutionThread))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("!mAsyncExecutionThread"
" (" "The async thread has not been shutdown properly!" ")",
"/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 820); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mAsyncExecutionThread"
") (" "The async thread has not been shutdown properly!" ")"
); do { *((volatile int*)__null) = 820; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
821}
822
823NS_IMPL_ADDREF(Connection)MozExternalRefCountType Connection::AddRef(void) { static_assert
(!std::is_destructible_v<Connection>, "Reference-counted class "
"Connection" " should not have a public destructor. " "Make this class's destructor non-public"
); do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 823); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { *((volatile int*)__null) =
823; __attribute__((nomerge)) ::abort(); } while (false); } }
while (false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("Connection" != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!("Connection" != nullptr))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("\"Connection\" != nullptr"
" (" "Must specify a name" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 823); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"Connection\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 823; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("Connection" " not thread-safe"); nsrefcnt count
= ++mRefCnt; NS_LogAddRef((this), (count), ("Connection"), (
uint32_t)(sizeof(*this))); return count; }
824
825NS_INTERFACE_MAP_BEGIN(Connection)nsresult Connection::QueryInterface(const nsIID& aIID, void
** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak(
NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 825); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface
;
826 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, mozIStorageAsyncConnection>)) foundInterface
= static_cast<mozIStorageAsyncConnection*>(this); else
827 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIInterfaceRequestor>)) foundInterface
= static_cast<nsIInterfaceRequestor*>(this); else
828 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, mozIStorageConnection>)) foundInterface
= static_cast<mozIStorageConnection*>(this); else
829 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsISupports>)) foundInterface = static_cast
<nsISupports*>(static_cast<mozIStorageConnection*>
(this)); else
830NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::COMTypeInfo<nsISupports
, void>::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::COMTypeInfo
<nsISupports, void>::kIID))))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 830); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))"
")"); do { *((volatile int*)__null) = 830; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
831
832// This is identical to what NS_IMPL_RELEASE provides, but with the
833// extra |1 == count| case.
834NS_IMETHODIMP_(MozExternalRefCountType)MozExternalRefCountType Connection::Release(void) {
835 MOZ_ASSERT(0 != mRefCnt, "dup release")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(0 != mRefCnt)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(0 != mRefCnt))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("0 != mRefCnt" " ("
"dup release" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 835); AnnotateMozCrashReason("MOZ_ASSERT" "(" "0 != mRefCnt"
") (" "dup release" ")"); do { *((volatile int*)__null) = 835
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
836 nsrefcnt count = --mRefCnt;
837 NS_LOG_RELEASE(this, count, "Connection")NS_LogRelease((this), (count), ("Connection"));
838 if (1 == count) {
839 // If the refcount went to 1, the single reference must be from
840 // gService->mConnections (in class |Service|). And the code calling
841 // Release is either:
842 // - The "user" code that had created the connection, releasing on any
843 // thread.
844 // - One of Service's getConnections() callers had acquired a strong
845 // reference to the Connection that out-lived the last "user" reference,
846 // and now that just got dropped. Note that this reference could be
847 // getting dropped on the main thread or Connection->eventTargetOpenedOn
848 // (because of the NewRunnableMethod used by minimizeMemory).
849 //
850 // Either way, we should now perform our failsafe Close() and unregister.
851 // However, we only want to do this once, and the reality is that our
852 // refcount could go back up above 1 and down again at any time if we are
853 // off the main thread and getConnections() gets called on the main thread,
854 // so we use an atomic here to do this exactly once.
855 if (mDestroying.compareExchange(false, true)) {
856 // Close the connection, dispatching to the opening event target if we're
857 // not on that event target already and that event target is still
858 // accepting runnables. We do this because it's possible we're on the main
859 // thread because of getConnections(), and we REALLY don't want to
860 // transfer I/O to the main thread if we can avoid it.
861 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
862 // This could cause SpinningSynchronousClose() to be invoked and AddRef
863 // triggered for AsyncCloseConnection's strong ref if the conn was ever
864 // use for async purposes. (Main-thread only, though.)
865 Unused << synchronousClose();
866 } else {
867 nsCOMPtr<nsIRunnable> event =
868 NewRunnableMethod("storage::Connection::synchronousClose", this,
869 &Connection::synchronousClose);
870 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),((bool)(__builtin_expect(!!(NS_FAILED_impl(eventTargetOpenedOn
->Dispatch(event.forget(), nsIEventTarget::DISPATCH_NORMAL
))), 0)))
871 NS_DISPATCH_NORMAL))((bool)(__builtin_expect(!!(NS_FAILED_impl(eventTargetOpenedOn
->Dispatch(event.forget(), nsIEventTarget::DISPATCH_NORMAL
))), 0)))
) {
872 // The event target was dead and so we've just leaked our runnable.
873 // This should not happen because our non-main-thread consumers should
874 // be explicitly closing their connections, not relying on us to close
875 // them for them. (It's okay to let a statement go out of scope for
876 // automatic cleanup, but not a Connection.)
877 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Leaked Connection::synchronousClose(), ownership fail."
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 878); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Leaked Connection::synchronousClose(), ownership fail." ")"
); do { *((volatile int*)__null) = 878; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
878 "Leaked Connection::synchronousClose(), ownership fail.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Leaked Connection::synchronousClose(), ownership fail."
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 878); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Leaked Connection::synchronousClose(), ownership fail." ")"
); do { *((volatile int*)__null) = 878; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
879 Unused << synchronousClose();
880 }
881 }
882
883 // This will drop its strong reference right here, right now.
884 mStorageService->unregisterConnection(this);
885 }
886 } else if (0 == count) {
887 mRefCnt = 1; /* stabilize */
888#if 0 /* enable this to find non-threadsafe destructors: */
889 NS_ASSERT_OWNINGTHREAD(Connection)_mOwningThread.AssertOwnership("Connection" " not thread-safe"
)
;
890#endif
891 delete (this);
892 return 0;
893 }
894 return count;
895}
896
897int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
898 int32_t* aMaxValue) {
899 MOZ_ASSERT(connectionReady(), "A connection must exist at this point")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(connectionReady())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(connectionReady()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("connectionReady()"
" (" "A connection must exist at this point" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 899); AnnotateMozCrashReason("MOZ_ASSERT" "(" "connectionReady()"
") (" "A connection must exist at this point" ")"); do { *((
volatile int*)__null) = 899; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
;
900 int curr = 0, max = 0;
901 DebugOnly<int> rc =
902 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
903 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(convertResultCode
(rc))), 1))))>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(convertResultCode(rc))), 1)))))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("((bool)(__builtin_expect(!!(!NS_FAILED_impl(convertResultCode(rc))), 1)))"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 903); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(convertResultCode(rc))), 1)))"
")"); do { *((volatile int*)__null) = 903; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
904 if (aMaxValue) *aMaxValue = max;
905 return curr;
906}
907
908nsIEventTarget* Connection::getAsyncExecutionTarget() {
909 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr)do { if ((__builtin_expect(!!(!(IsOnCurrentSerialEventTarget(
eventTargetOpenedOn))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 909); return nullptr; } } while (false)
;
910
911 // Don't return the asynchronous event target if we are shutting down.
912 if (mAsyncExecutionThreadShuttingDown) {
913 return nullptr;
914 }
915
916 // Create the async event target if there's none yet.
917 if (!mAsyncExecutionThread) {
918 // Names start with "sqldb:" followed by a recognizable name, like the
919 // database file name, or a specially crafted name like "memory".
920 // This name will be surfaced on https://crash-stats.mozilla.org, so any
921 // sensitive part of the file name (e.g. an URL origin) should be replaced
922 // by passing an explicit telemetryName to openDatabaseWithFileURL.
923 nsAutoCString name("sqldb:"_ns);
924 name.Append(mTelemetryFilename);
925 static nsThreadPoolNaming naming;
926 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
927 getter_AddRefs(mAsyncExecutionThread));
928 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
929 NS_WARNING("Failed to create async thread.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to create async thread."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 929)
;
930 return nullptr;
931 }
932 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
933 }
934
935 return mAsyncExecutionThread;
936}
937
938void Connection::RecordOpenStatus(nsresult rv) {
939 nsCString histogramKey = mTelemetryFilename;
940
941 if (histogramKey.IsEmpty()) {
942 histogramKey.AssignLiteral("unknown");
943 }
944
945 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
946 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
947 return;
948 }
949
950 switch (rv) {
951 case NS_ERROR_FILE_CORRUPTED:
952 AccumulateCategoricalKeyed(histogramKey,
953 LABELS_SQLITE_STORE_OPEN::corrupt);
954 break;
955 case NS_ERROR_STORAGE_IOERR:
956 AccumulateCategoricalKeyed(histogramKey,
957 LABELS_SQLITE_STORE_OPEN::diskio);
958 break;
959 case NS_ERROR_FILE_ACCESS_DENIED:
960 case NS_ERROR_FILE_IS_LOCKED:
961 case NS_ERROR_FILE_READ_ONLY:
962 AccumulateCategoricalKeyed(histogramKey,
963 LABELS_SQLITE_STORE_OPEN::access);
964 break;
965 case NS_ERROR_FILE_NO_DEVICE_SPACE:
966 AccumulateCategoricalKeyed(histogramKey,
967 LABELS_SQLITE_STORE_OPEN::diskspace);
968 break;
969 default:
970 AccumulateCategoricalKeyed(histogramKey,
971 LABELS_SQLITE_STORE_OPEN::failure);
972 }
973}
974
975void Connection::RecordQueryStatus(int srv) {
976 nsCString histogramKey = mTelemetryFilename;
977
978 if (histogramKey.IsEmpty()) {
979 histogramKey.AssignLiteral("unknown");
980 }
981
982 switch (srv) {
983 case SQLITE_OK0:
984 case SQLITE_ROW100:
985 case SQLITE_DONE101:
986
987 // Note that these are returned when we intentionally cancel a statement so
988 // they aren't indicating a failure.
989 case SQLITE_ABORT4:
990 case SQLITE_INTERRUPT9:
991 AccumulateCategoricalKeyed(histogramKey,
992 LABELS_SQLITE_STORE_QUERY::success);
993 break;
994 case SQLITE_CORRUPT11:
995 case SQLITE_NOTADB26:
996 AccumulateCategoricalKeyed(histogramKey,
997 LABELS_SQLITE_STORE_QUERY::corrupt);
998 break;
999 case SQLITE_PERM3:
1000 case SQLITE_CANTOPEN14:
1001 case SQLITE_LOCKED6:
1002 case SQLITE_READONLY8:
1003 AccumulateCategoricalKeyed(histogramKey,
1004 LABELS_SQLITE_STORE_QUERY::access);
1005 break;
1006 case SQLITE_IOERR10:
1007 case SQLITE_NOLFS22:
1008 AccumulateCategoricalKeyed(histogramKey,
1009 LABELS_SQLITE_STORE_QUERY::diskio);
1010 break;
1011 case SQLITE_FULL13:
1012 case SQLITE_TOOBIG18:
1013 AccumulateCategoricalKeyed(histogramKey,
1014 LABELS_SQLITE_STORE_QUERY::diskspace);
1015 break;
1016 case SQLITE_CONSTRAINT19:
1017 case SQLITE_RANGE25:
1018 case SQLITE_MISMATCH20:
1019 case SQLITE_MISUSE21:
1020 AccumulateCategoricalKeyed(histogramKey,
1021 LABELS_SQLITE_STORE_QUERY::misuse);
1022 break;
1023 case SQLITE_BUSY5:
1024 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_QUERY::busy);
1025 break;
1026 default:
1027 AccumulateCategoricalKeyed(histogramKey,
1028 LABELS_SQLITE_STORE_QUERY::failure);
1029 }
1030}
1031
1032nsresult Connection::initialize(const nsACString& aStorageKey,
1033 const nsACString& aName) {
1034 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aStorageKey.Equals(kMozStorageMemoryStorageKey))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(aStorageKey.Equals(kMozStorageMemoryStorageKey)))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("aStorageKey.Equals(kMozStorageMemoryStorageKey)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1034); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aStorageKey.Equals(kMozStorageMemoryStorageKey)"
")"); do { *((volatile int*)__null) = 1034; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1035 NS_ASSERTION(!connectionReady(),do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1036); MOZ_PretendNoReturn(); } } while (0)
1036 "Initialize called on already opened database!")do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1036); MOZ_PretendNoReturn(); } } while (0)
;
1037 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mIgnoreLockingMode)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mIgnoreLockingMode))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("!mIgnoreLockingMode"
" (" "Can't ignore locking on an in-memory db." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1037); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mIgnoreLockingMode"
") (" "Can't ignore locking on an in-memory db." ")"); do { *
((volatile int*)__null) = 1037; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
;
1038 AUTO_PROFILER_LABEL("Connection::initialize", OTHER)mozilla::AutoProfilerLabel raiiObject1038( "Connection::initialize"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
1039
1040 mStorageKey = aStorageKey;
1041 mName = aName;
1042
1043 // in memory database requested, sqlite uses a magic file name
1044
1045 const nsAutoCString path =
1046 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
1047 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
1048
1049 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
1050 basevfs::GetVFSName(true));
1051 if (srv != SQLITE_OK0) {
1052 mDBConn = nullptr;
1053 nsresult rv = convertResultCode(srv);
1054 RecordOpenStatus(rv);
1055 return rv;
1056 }
1057
1058#ifdef MOZ_SQLITE_FTS3_TOKENIZER
1059 srv =
1060 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, 1, 0);
1061 MOZ_ASSERT(srv == SQLITE_OK,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(srv == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(srv == 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("srv == 0" " (" "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1062); AnnotateMozCrashReason("MOZ_ASSERT" "(" "srv == 0" ") ("
"SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled" ")"
); do { *((volatile int*)__null) = 1062; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1062 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(srv == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(srv == 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("srv == 0" " (" "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1062); AnnotateMozCrashReason("MOZ_ASSERT" "(" "srv == 0" ") ("
"SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled" ")"
); do { *((volatile int*)__null) = 1062; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1063#endif
1064
1065 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
1066 // database.
1067
1068 nsresult rv = initializeInternal();
1069 RecordOpenStatus(rv);
1070 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1070); return rv; } } while (false)
;
1071
1072 return NS_OK;
1073}
1074
1075nsresult Connection::initialize(nsIFile* aDatabaseFile) {
1076 NS_ASSERTION(aDatabaseFile, "Passed null file!")do { if (!(aDatabaseFile)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Passed null file!", "aDatabaseFile", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1076); MOZ_PretendNoReturn(); } } while (0)
;
1077 NS_ASSERTION(!connectionReady(),do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1078); MOZ_PretendNoReturn(); } } while (0)
1078 "Initialize called on already opened database!")do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1078); MOZ_PretendNoReturn(); } } while (0)
;
1079 AUTO_PROFILER_LABEL("Connection::initialize", OTHER)mozilla::AutoProfilerLabel raiiObject1079( "Connection::initialize"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
1080
1081 // Do not set mFileURL here since this is database does not have an associated
1082 // URL.
1083 mDatabaseFile = aDatabaseFile;
1084
1085 nsAutoString path;
1086 nsresult rv = aDatabaseFile->GetPath(path);
1087 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1087); return rv; } } while (false)
;
1088
1089 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
1090 int srv;
1091 if (mIgnoreLockingMode) {
1092 exclusive = false;
1093 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1094 "readonly-immutable-nolock");
1095 } else {
1096 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1097 basevfs::GetVFSName(exclusive));
1098 if (exclusive && (srv == SQLITE_LOCKED6 || srv == SQLITE_BUSY5)) {
1099 // Retry without trying to get an exclusive lock.
1100 exclusive = false;
1101 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
1102 mFlags, basevfs::GetVFSName(false));
1103 }
1104 }
1105 if (srv != SQLITE_OK0) {
1106 mDBConn = nullptr;
1107 rv = convertResultCode(srv);
1108 RecordOpenStatus(rv);
1109 return rv;
1110 }
1111
1112 rv = initializeInternal();
1113 if (exclusive &&
1114 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
1115 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
1116 // some cases it may successfully open the database and then lock on the
1117 // first query execution. When initializeInternal fails it closes the
1118 // connection, so we can try to restart it in non-exclusive mode.
1119 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1120 basevfs::GetVFSName(false));
1121 if (srv == SQLITE_OK0) {
1122 rv = initializeInternal();
1123 }
1124 }
1125
1126 RecordOpenStatus(rv);
1127 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1127); return rv; } } while (false)
;
1128
1129 return NS_OK;
1130}
1131
1132nsresult Connection::initialize(nsIFileURL* aFileURL) {
1133 NS_ASSERTION(aFileURL, "Passed null file URL!")do { if (!(aFileURL)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Passed null file URL!"
, "aFileURL", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1133); MOZ_PretendNoReturn(); } } while (0)
;
1134 NS_ASSERTION(!connectionReady(),do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1135); MOZ_PretendNoReturn(); } } while (0)
1135 "Initialize called on already opened database!")do { if (!(!connectionReady())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Initialize called on already opened database!", "!connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1135); MOZ_PretendNoReturn(); } } while (0)
;
1136 AUTO_PROFILER_LABEL("Connection::initialize", OTHER)mozilla::AutoProfilerLabel raiiObject1136( "Connection::initialize"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
1137
1138 nsCOMPtr<nsIFile> databaseFile;
1139 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
1140 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1140); return rv; } } while (false)
;
1141
1142 // Set both mDatabaseFile and mFileURL here.
1143 mFileURL = aFileURL;
1144 mDatabaseFile = databaseFile;
1145
1146 nsAutoCString spec;
1147 rv = aFileURL->GetSpec(spec);
1148 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1148); return rv; } } while (false)
;
1149
1150 // If there is a key specified, we need to use the obfuscating VFS.
1151 nsAutoCString query;
1152 rv = aFileURL->GetQuery(query);
1153 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1153); return rv; } } while (false)
;
1154
1155 bool hasKey = false;
1156 bool hasDirectoryLockId = false;
1157
1158 MOZ_ALWAYS_TRUE(do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1159 URLParams::Parse(query, true,do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1160 [&hasKey, &hasDirectoryLockId](do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1161 const nsACString& aName, const nsACString& aValue) {do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1162 if (aName.EqualsLiteral("key")) {do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1163 hasKey = true;do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1164 return true;do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1165 }do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1166 if (aName.EqualsLiteral("directoryLockId")) {do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1167 hasDirectoryLockId = true;do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1168 return true;do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1169 }do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1170 return true;do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1171 }))do { if ((__builtin_expect(!!(URLParams::Parse(query, true, [
&hasKey, &hasDirectoryLockId]( const nsACString& aName
, const nsACString& aValue) { if (aName.EqualsLiteral("key"
)) { hasKey = true; return true; } if (aName.EqualsLiteral("directoryLockId"
)) { hasDirectoryLockId = true; return true; } return true; }
)), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1171); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "URLParams::Parse(query, true, [&hasKey, &hasDirectoryLockId]( const nsACString& aName, const nsACString& aValue) { if (aName.EqualsLiteral(\"key\")) { hasKey = true; return true; } if (aName.EqualsLiteral(\"directoryLockId\")) { hasDirectoryLockId = true; return true; } return true; })"
")"); do { *((volatile int*)__null) = 1171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
1172
1173 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
1174
1175 const char* const vfs = hasKey ? obfsvfs::GetVFSName()
1176 : hasDirectoryLockId ? quotavfs::GetVFSName()
1177 : basevfs::GetVFSName(exclusive);
1178
1179 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
1180 if (srv != SQLITE_OK0) {
1181 mDBConn = nullptr;
1182 rv = convertResultCode(srv);
1183 RecordOpenStatus(rv);
1184 return rv;
1185 }
1186
1187 rv = initializeInternal();
1188 RecordOpenStatus(rv);
1189 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1189); return rv; } } while (false)
;
1190
1191 return NS_OK;
1192}
1193
1194nsresult Connection::initializeInternal() {
1195 MOZ_ASSERT(mDBConn)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mDBConn)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mDBConn))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mDBConn", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1195); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mDBConn" ")"
); do { *((volatile int*)__null) = 1195; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1196 auto guard = MakeScopeExit([&]() { initializeFailed(); });
1197
1198 mConnectionClosed = false;
1199
1200#ifdef MOZ_SQLITE_FTS3_TOKENIZER
1201 DebugOnly<int> srv2 =
1202 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, 1, 0);
1203 MOZ_ASSERT(srv2 == SQLITE_OK,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(srv2 == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(srv2 == 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("srv2 == 0" " (" "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1204); AnnotateMozCrashReason("MOZ_ASSERT" "(" "srv2 == 0" ") ("
"SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled" ")"
); do { *((volatile int*)__null) = 1204; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1204 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(srv2 == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(srv2 == 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("srv2 == 0" " (" "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1204); AnnotateMozCrashReason("MOZ_ASSERT" "(" "srv2 == 0" ") ("
"SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled" ")"
); do { *((volatile int*)__null) = 1204; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1205#endif
1206
1207 // Properly wrap the database handle's mutex.
1208 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1209
1210 // SQLite tracing can slow down queries (especially long queries)
1211 // significantly. Don't trace unless the user is actively monitoring SQLite.
1212 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gStorageLog, LogLevel
::Debug)), 0))
) {
1213 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT0x01 | SQLITE_TRACE_PROFILE0x02,
1214 tracefunc, this);
1215
1216 MOZ_LOG(do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Opening connection to '%s' (%p)", mTelemetryFilename
.get(), this); } } while (0)
1217 gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Opening connection to '%s' (%p)", mTelemetryFilename
.get(), this); } } while (0)
1218 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Opening connection to '%s' (%p)", mTelemetryFilename
.get(), this); } } while (0)
;
1219 }
1220
1221 int64_t pageSize = Service::kDefaultPageSize;
1222
1223 // Set page_size to the preferred default value. This is effective only if
1224 // the database has just been created, otherwise, if the database does not
1225 // use WAL journal mode, a VACUUM operation will updated its page_size.
1226 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
1227 "PRAGMA page_size = ");
1228 pageSizeQuery.AppendInt(pageSize);
1229 int srv = executeSql(mDBConn, pageSizeQuery.get());
1230 if (srv != SQLITE_OK0) {
1231 return convertResultCode(srv);
1232 }
1233
1234 // Setting the cache_size forces the database open, verifying if it is valid
1235 // or corrupt. So this is executed regardless it being actually needed.
1236 // The cache_size is calculated from the actual page_size, to save memory.
1237 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
1238 "PRAGMA cache_size = ");
1239 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES2048);
1240 srv = executeSql(mDBConn, cacheSizeQuery.get());
1241 if (srv != SQLITE_OK0) {
1242 return convertResultCode(srv);
1243 }
1244
1245 // Register our built-in SQL functions.
1246 srv = registerFunctions(mDBConn);
1247 if (srv != SQLITE_OK0) {
1248 return convertResultCode(srv);
1249 }
1250
1251 // Register our built-in SQL collating sequences.
1252 srv = registerCollations(mDBConn, mStorageService);
1253 if (srv != SQLITE_OK0) {
1254 return convertResultCode(srv);
1255 }
1256
1257 // Set the default synchronous value. Each consumer can switch this
1258 // accordingly to their needs.
1259#if defined(ANDROID)
1260 // Android prefers synchronous = OFF for performance reasons.
1261 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1262#else
1263 // Normal is the suggested value for WAL journals.
1264 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1265#endif
1266
1267 // Initialization succeeded, we can stop guarding for failures.
1268 guard.release();
1269 return NS_OK;
1270}
1271
1272nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1273 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1273); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 1273; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1274 nsresult rv = aStorageFile
1275 ? initialize(aStorageFile)
1276 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1277 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1278 // Shutdown the async thread, since initialization failed.
1279 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1280 mAsyncExecutionThreadShuttingDown = true;
1281 nsCOMPtr<nsIRunnable> event =
1282 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1283 &Connection::shutdownAsyncThread);
1284 Unused << NS_DispatchToMainThread(event);
1285 }
1286 return rv;
1287}
1288
1289void Connection::initializeFailed() {
1290 {
1291 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1292 mConnectionClosed = true;
1293 }
1294 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK)do { if ((__builtin_expect(!!(::sqlite3_close(mDBConn) == 0),
1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "::sqlite3_close(mDBConn) == SQLITE_OK"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1294); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "::sqlite3_close(mDBConn) == SQLITE_OK" ")"); do { *((
volatile int*)__null) = 1294; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false); } } while (false)
;
1295 mDBConn = nullptr;
1296 sharedDBMutex.destroy();
1297}
1298
1299nsresult Connection::databaseElementExists(
1300 enum DatabaseElementType aElementType, const nsACString& aElementName,
1301 bool* _exists) {
1302 if (!connectionReady()) {
1303 return NS_ERROR_NOT_AVAILABLE;
1304 }
1305 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1306 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1307 return rv;
1308 }
1309
1310 // When constructing the query, make sure to SELECT the correct db's
1311 // sqlite_master if the user is prefixing the element with a specific db. ex:
1312 // sample.test
1313 nsCString query("SELECT name FROM (SELECT * FROM ");
1314 nsDependentCSubstring element;
1315 int32_t ind = aElementName.FindChar('.');
1316 if (ind == kNotFound) {
1317 element.Assign(aElementName);
1318 } else {
1319 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1320 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1321 query.Append(db);
1322 }
1323 query.AppendLiteral(
1324 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1325 "'");
1326
1327 switch (aElementType) {
1328 case INDEX:
1329 query.AppendLiteral("index");
1330 break;
1331 case TABLE:
1332 query.AppendLiteral("table");
1333 break;
1334 }
1335 query.AppendLiteral("' AND name ='");
1336 query.Append(element);
1337 query.Append('\'');
1338
1339 sqlite3_stmt* stmt;
1340 int srv = prepareStatement(mDBConn, query, &stmt);
1341 if (srv != SQLITE_OK0) {
1342 RecordQueryStatus(srv);
1343 return convertResultCode(srv);
1344 }
1345
1346 srv = stepStatement(mDBConn, stmt);
1347 // we just care about the return value from step
1348 (void)::sqlite3_finalize(stmt);
1349
1350 RecordQueryStatus(srv);
1351
1352 if (srv == SQLITE_ROW100) {
1353 *_exists = true;
1354 return NS_OK;
1355 }
1356 if (srv == SQLITE_DONE101) {
1357 *_exists = false;
1358 return NS_OK;
1359 }
1360
1361 return convertResultCode(srv);
1362}
1363
1364bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1365 sharedDBMutex.assertCurrentThreadOwns();
1366
1367 for (const auto& data : mFunctions.Values()) {
1368 if (data.function == aInstance) {
1369 return true;
1370 }
1371 }
1372 return false;
1373}
1374
1375/* static */
1376int Connection::sProgressHelper(void* aArg) {
1377 Connection* _this = static_cast<Connection*>(aArg);
1378 return _this->progressHandler();
1379}
1380
1381int Connection::progressHandler() {
1382 sharedDBMutex.assertCurrentThreadOwns();
1383 if (mProgressHandler) {
1384 bool result;
1385 nsresult rv = mProgressHandler->OnProgress(this, &result);
1386 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return 0; // Don't break request
1387 return result ? 1 : 0;
1388 }
1389 return 0;
1390}
1391
1392nsresult Connection::setClosedState() {
1393 // Flag that we are shutting down the async thread, so that
1394 // getAsyncExecutionTarget knows not to expose/create the async thread.
1395 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1396 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED)do { if ((__builtin_expect(!!(!(!(mAsyncExecutionThreadShuttingDown
))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE("
"!(mAsyncExecutionThreadShuttingDown)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1396); return NS_ERROR_UNEXPECTED; } } while (false)
;
1397
1398 mAsyncExecutionThreadShuttingDown = true;
1399
1400 // Set the property to null before closing the connection, otherwise the
1401 // other functions in the module may try to use the connection after it is
1402 // closed.
1403 mDBConn = nullptr;
1404
1405 return NS_OK;
1406}
1407
1408bool Connection::operationSupported(ConnectionOperation aOperationType) {
1409 if (aOperationType == ASYNCHRONOUS) {
1410 // Async operations are supported for all connections, on any thread.
1411 return true;
1412 }
1413 // Sync operations are supported for sync connections (on any thread), and
1414 // async connections on a background thread.
1415 MOZ_ASSERT(aOperationType == SYNCHRONOUS)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aOperationType == SYNCHRONOUS)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aOperationType == SYNCHRONOUS
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"aOperationType == SYNCHRONOUS", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1415); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aOperationType == SYNCHRONOUS"
")"); do { *((volatile int*)__null) = 1415; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1416 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1417}
1418
1419nsresult Connection::ensureOperationSupported(
1420 ConnectionOperation aOperationType) {
1421 if (NS_WARN_IF(!operationSupported(aOperationType))NS_warn_if_impl(!operationSupported(aOperationType), "!operationSupported(aOperationType)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1421)
) {
1422#ifdef DEBUG1
1423 if (NS_IsMainThread()) {
1424 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1425 Unused << xpc->DebugDumpJSStack(false, false, false);
1426 }
1427#endif
1428 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Don't use async connections synchronously on the main thread"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1429); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Don't use async connections synchronously on the main thread"
")"); do { *((volatile int*)__null) = 1429; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1429 "Don't use async connections synchronously on the main thread")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Don't use async connections synchronously on the main thread"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1429); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Don't use async connections synchronously on the main thread"
")"); do { *((volatile int*)__null) = 1429; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1430 return NS_ERROR_NOT_AVAILABLE;
1431 }
1432 return NS_OK;
1433}
1434
1435bool Connection::isConnectionReadyOnThisThread() {
1436 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed)do { if (connectionReady()) { do { static_assert( mozilla::detail
::AssertionConditionType<decltype(!mConnectionClosed)>::
isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!mConnectionClosed))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!mConnectionClosed", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1436); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mConnectionClosed"
")"); do { *((volatile int*)__null) = 1436; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
1437 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1438 return true;
1439 }
1440 return connectionReady();
1441}
1442
1443bool Connection::isClosing() {
1444 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1445 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1446}
1447
1448bool Connection::isClosed() {
1449 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1450 return mConnectionClosed;
1451}
1452
1453bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1454
1455bool Connection::isAsyncExecutionThreadAvailable() {
1456 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1456); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 1456; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1457 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1458}
1459
1460void Connection::shutdownAsyncThread() {
1461 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1461); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 1461; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1462 MOZ_ASSERT(mAsyncExecutionThread)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mAsyncExecutionThread)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mAsyncExecutionThread))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("mAsyncExecutionThread"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1462); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mAsyncExecutionThread"
")"); do { *((volatile int*)__null) = 1462; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1463 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mAsyncExecutionThreadShuttingDown)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mAsyncExecutionThreadShuttingDown
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"mAsyncExecutionThreadShuttingDown", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mAsyncExecutionThreadShuttingDown"
")"); do { *((volatile int*)__null) = 1463; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1464
1465 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown())do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(mAsyncExecutionThread->Shutdown())), 1)))), 1))) { } else
{ do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "NS_SUCCEEDED(mAsyncExecutionThread->Shutdown())"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1465); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(mAsyncExecutionThread->Shutdown())" ")"
); do { *((volatile int*)__null) = 1465; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
1466 mAsyncExecutionThread = nullptr;
1467}
1468
1469nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1470#ifdef DEBUG1
1471 { // Make sure we have marked our async thread as shutting down.
1472 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1473 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mAsyncExecutionThreadShuttingDown)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mAsyncExecutionThreadShuttingDown
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"mAsyncExecutionThreadShuttingDown" " (" "Did not call setClosedState!"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1474); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mAsyncExecutionThreadShuttingDown"
") (" "Did not call setClosedState!" ")"); do { *((volatile int
*)__null) = 1474; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
1474 "Did not call setClosedState!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mAsyncExecutionThreadShuttingDown)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mAsyncExecutionThreadShuttingDown
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"mAsyncExecutionThreadShuttingDown" " (" "Did not call setClosedState!"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1474); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mAsyncExecutionThreadShuttingDown"
") (" "Did not call setClosedState!" ")"); do { *((volatile int
*)__null) = 1474; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
;
1475 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!isClosed(lockedScope))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!isClosed(lockedScope)))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("!isClosed(lockedScope)"
" (" "Unexpected closed state" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1475); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isClosed(lockedScope)"
") (" "Unexpected closed state" ")"); do { *((volatile int*)
__null) = 1475; __attribute__((nomerge)) ::abort(); } while (
false); } } while (false)
;
1476 }
1477#endif // DEBUG
1478
1479 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gStorageLog, LogLevel
::Debug)), 0))
) {
1480 nsAutoCString leafName(":memory");
1481 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1482 MOZ_LOG(gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Closing connection to '%s'", leafName.get
()); } } while (0)
1483 ("Closing connection to '%s'", leafName.get()))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Closing connection to '%s'", leafName.get
()); } } while (0)
;
1484 }
1485
1486 // At this stage, we may still have statements that need to be
1487 // finalized. Attempt to close the database connection. This will
1488 // always disconnect any virtual tables and cleanly finalize their
1489 // internal statements. Once this is done, closing may fail due to
1490 // unfinalized client statements, in which case we need to finalize
1491 // these statements and close again.
1492 {
1493 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1494 mConnectionClosed = true;
1495 }
1496
1497 // Nothing else needs to be done if we don't have a connection here.
1498 if (!aNativeConnection) return NS_OK;
1499
1500 int srv = ::sqlite3_close(aNativeConnection);
1501
1502 if (srv == SQLITE_BUSY5) {
1503 {
1504 // Nothing else should change the connection or statements status until we
1505 // are done here.
1506 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1507 // We still have non-finalized statements. Finalize them.
1508 sqlite3_stmt* stmt = nullptr;
1509 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1510 MOZ_LOG(gStorageLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Auto-finalizing SQL statement '%s' (%p)",
::sqlite3_sql(stmt), stmt); } } while (0)
1511 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Auto-finalizing SQL statement '%s' (%p)",
::sqlite3_sql(stmt), stmt); } } while (0)
1512 stmt))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "Auto-finalizing SQL statement '%s' (%p)",
::sqlite3_sql(stmt), stmt); } } while (0)
;
1513
1514#ifdef DEBUG1
1515 SmprintfPointer msg = ::mozilla::Smprintf(
1516 "SQL statement '%s' (%p) should have been finalized before closing "
1517 "the connection",
1518 ::sqlite3_sql(stmt), stmt);
1519 NS_WARNING(msg.get())NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1519)
;
1520#endif // DEBUG
1521
1522 srv = ::sqlite3_finalize(stmt);
1523
1524#ifdef DEBUG1
1525 if (srv != SQLITE_OK0) {
1526 SmprintfPointer msg = ::mozilla::Smprintf(
1527 "Could not finalize SQL statement (%p)", stmt);
1528 NS_WARNING(msg.get())NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1528)
;
1529 }
1530#endif // DEBUG
1531
1532 // Ensure that the loop continues properly, whether closing has
1533 // succeeded or not.
1534 if (srv == SQLITE_OK0) {
1535 stmt = nullptr;
1536 }
1537 }
1538 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1539 // again, since Sqlite will try to acquire it.
1540 }
1541
1542 // Now that all statements have been finalized, we
1543 // should be able to close.
1544 srv = ::sqlite3_close(aNativeConnection);
Value stored to 'srv' is never read
1545 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Had to forcibly close the database connection because not all "
"the statements have been finalized." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1547); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Had to forcibly close the database connection because not all "
"the statements have been finalized." ")"); do { *((volatile
int*)__null) = 1547; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
1546 "Had to forcibly close the database connection because not all "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Had to forcibly close the database connection because not all "
"the statements have been finalized." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1547); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Had to forcibly close the database connection because not all "
"the statements have been finalized." ")"); do { *((volatile
int*)__null) = 1547; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
1547 "the statements have been finalized.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Had to forcibly close the database connection because not all "
"the statements have been finalized." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1547); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Had to forcibly close the database connection because not all "
"the statements have been finalized." ")"); do { *((volatile
int*)__null) = 1547; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
;
1548 }
1549
1550 if (srv == SQLITE_OK0) {
1551 sharedDBMutex.destroy();
1552 } else {
1553 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "sqlite3_close failed. There are probably outstanding "
"statements that are listed above!" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1555); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"sqlite3_close failed. There are probably outstanding " "statements that are listed above!"
")"); do { *((volatile int*)__null) = 1555; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1554 "sqlite3_close failed. There are probably outstanding "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "sqlite3_close failed. There are probably outstanding "
"statements that are listed above!" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1555); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"sqlite3_close failed. There are probably outstanding " "statements that are listed above!"
")"); do { *((volatile int*)__null) = 1555; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1555 "statements that are listed above!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "sqlite3_close failed. There are probably outstanding "
"statements that are listed above!" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1555); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"sqlite3_close failed. There are probably outstanding " "statements that are listed above!"
")"); do { *((volatile int*)__null) = 1555; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1556 }
1557
1558 return convertResultCode(srv);
1559}
1560
1561nsCString Connection::getFilename() { return mTelemetryFilename; }
1562
1563int Connection::stepStatement(sqlite3* aNativeConnection,
1564 sqlite3_stmt* aStatement) {
1565 MOZ_ASSERT(aStatement)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aStatement)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aStatement))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aStatement", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1565); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aStatement"
")"); do { *((volatile int*)__null) = 1565; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1566
1567 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,mozilla::AutoProfilerLabel raiiObject1568( "Connection::stepStatement"
, ::sqlite3_sql(aStatement), JS::ProfilingCategoryPair::OTHER
)
1568 ::sqlite3_sql(aStatement))mozilla::AutoProfilerLabel raiiObject1568( "Connection::stepStatement"
, ::sqlite3_sql(aStatement), JS::ProfilingCategoryPair::OTHER
)
;
1569
1570 bool checkedMainThread = false;
1571 TimeStamp startTime = TimeStamp::Now();
1572
1573 // The connection may have been closed if the executing statement has been
1574 // created and cached after a call to asyncClose() but before the actual
1575 // sqlite3_close(). This usually happens when other tasks using cached
1576 // statements are asynchronously scheduled for execution and any of them ends
1577 // up after asyncClose. See bug 728653 for details.
1578 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE21;
1579
1580 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1581
1582 int srv;
1583 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8))) {
1584 if (!checkedMainThread) {
1585 checkedMainThread = true;
1586 if (::NS_IsMainThread()) {
1587 NS_WARNING("We won't allow blocking on the main thread!")NS_DebugBreak(NS_DEBUG_WARNING, "We won't allow blocking on the main thread!"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1587)
;
1588 break;
1589 }
1590 }
1591
1592 srv = WaitForUnlockNotify(aNativeConnection);
1593 if (srv != SQLITE_OK0) {
1594 break;
1595 }
1596
1597 ::sqlite3_reset(aStatement);
1598 }
1599
1600 // Report very slow SQL statements to Telemetry
1601 TimeDuration duration = TimeStamp::Now() - startTime;
1602 const uint32_t threshold = NS_IsMainThread()
1603 ? Telemetry::kSlowSQLThresholdForMainThread
1604 : Telemetry::kSlowSQLThresholdForHelperThreads;
1605 if (duration.ToMilliseconds() >= threshold) {
1606 nsDependentCString statementString(::sqlite3_sql(aStatement));
1607 Telemetry::RecordSlowSQLStatement(
1608 statementString, mTelemetryFilename,
1609 static_cast<uint32_t>(duration.ToMilliseconds()));
1610 }
1611
1612 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1613 // Drop off the extended result bits of the result code.
1614 return srv & 0xFF;
1615}
1616
1617int Connection::prepareStatement(sqlite3* aNativeConnection,
1618 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1619 // We should not even try to prepare statements after the connection has
1620 // been closed.
1621 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE21;
1622
1623 bool checkedMainThread = false;
1624
1625 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1626
1627 int srv;
1628 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1629 nullptr)) == SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8))) {
1630 if (!checkedMainThread) {
1631 checkedMainThread = true;
1632 if (::NS_IsMainThread()) {
1633 NS_WARNING("We won't allow blocking on the main thread!")NS_DebugBreak(NS_DEBUG_WARNING, "We won't allow blocking on the main thread!"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1633)
;
1634 break;
1635 }
1636 }
1637
1638 srv = WaitForUnlockNotify(aNativeConnection);
1639 if (srv != SQLITE_OK0) {
1640 break;
1641 }
1642 }
1643
1644 if (srv != SQLITE_OK0) {
1645 nsCString warnMsg;
1646 warnMsg.AppendLiteral("The SQL statement '");
1647 warnMsg.Append(aSQL);
1648 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1649 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1650
1651#ifdef DEBUG1
1652 NS_WARNING(warnMsg.get())NS_DebugBreak(NS_DEBUG_WARNING, warnMsg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1652)
;
1653#endif
1654 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()))do { const ::mozilla::LogModule* moz_real_module = gStorageLog
; if ((__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Error)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Error, "%s", warnMsg.get()); } } while (0)
;
1655 }
1656
1657 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1658 // Drop off the extended result bits of the result code.
1659 int rc = srv & 0xFF;
1660 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1661 // The callers of this function are used to only checking the return value,
1662 // so it is safer to return an error code.
1663 if (rc == SQLITE_OK0 && *_stmt == nullptr) {
1664 return SQLITE_MISUSE21;
1665 }
1666
1667 return rc;
1668}
1669
1670int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1671 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE21;
1672
1673 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString)mozilla::AutoProfilerLabel raiiObject1673( "Connection::executeSql"
, aSqlString, JS::ProfilingCategoryPair::OTHER)
;
1674
1675 TimeStamp startTime = TimeStamp::Now();
1676 int srv =
1677 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1678 RecordQueryStatus(srv);
1679
1680 // Report very slow SQL statements to Telemetry
1681 TimeDuration duration = TimeStamp::Now() - startTime;
1682 const uint32_t threshold = NS_IsMainThread()
1683 ? Telemetry::kSlowSQLThresholdForMainThread
1684 : Telemetry::kSlowSQLThresholdForHelperThreads;
1685 if (duration.ToMilliseconds() >= threshold) {
1686 nsDependentCString statementString(aSqlString);
1687 Telemetry::RecordSlowSQLStatement(
1688 statementString, mTelemetryFilename,
1689 static_cast<uint32_t>(duration.ToMilliseconds()));
1690 }
1691
1692 return srv;
1693}
1694
1695////////////////////////////////////////////////////////////////////////////////
1696//// nsIInterfaceRequestor
1697
1698NS_IMETHODIMPnsresult
1699Connection::GetInterface(const nsIID& aIID, void** _result) {
1700 if (aIID.Equals(NS_GET_IID(nsIEventTarget)(nsIEventTarget::COMTypeInfo<nsIEventTarget, void>::kIID
)
)) {
1701 nsIEventTarget* background = getAsyncExecutionTarget();
1702 NS_IF_ADDREF(background)ns_if_addref(background);
1703 *_result = background;
1704 return NS_OK;
1705 }
1706 return NS_ERROR_NO_INTERFACE;
1707}
1708
1709////////////////////////////////////////////////////////////////////////////////
1710//// mozIStorageConnection
1711
1712NS_IMETHODIMPnsresult
1713Connection::Close() {
1714 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1715 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1716 return rv;
1717 }
1718 return synchronousClose();
1719}
1720
1721nsresult Connection::synchronousClose() {
1722 if (!connectionReady()) {
1723 return NS_ERROR_NOT_INITIALIZED;
1724 }
1725
1726#ifdef DEBUG1
1727 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1728 // event target. We make this check outside of debug code below in
1729 // setClosedState, but this is here to be explicit.
1730 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1730); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 1730; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1731#endif // DEBUG
1732
1733 // Make sure we have not executed any asynchronous statements.
1734 // If this fails, the mDBConn may be left open, resulting in a leak.
1735 // We'll try to finalize the pending statements and close the connection.
1736 if (isAsyncExecutionThreadAvailable()) {
1737#ifdef DEBUG1
1738 if (NS_IsMainThread()) {
1739 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1740 Unused << xpc->DebugDumpJSStack(false, false, false);
1741 }
1742#endif
1743 MOZ_ASSERT(false,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1746); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")"); do { *
((volatile int*)__null) = 1746; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
1744 "Close() was invoked on a connection that executed asynchronous "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1746); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")"); do { *
((volatile int*)__null) = 1746; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
1745 "statements. "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1746); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")"); do { *
((volatile int*)__null) = 1746; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
1746 "Should have used asyncClose().")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1746); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Close() was invoked on a connection that executed asynchronous "
"statements. " "Should have used asyncClose()." ")"); do { *
((volatile int*)__null) = 1746; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
;
1747 // Try to close the database regardless, to free up resources.
1748 Unused << SpinningSynchronousClose();
1749 return NS_ERROR_UNEXPECTED;
1750 }
1751
1752 // setClosedState nullifies our connection pointer, so we take a raw pointer
1753 // off it, to pass it through the close procedure.
1754 sqlite3* nativeConn = mDBConn;
1755 nsresult rv = setClosedState();
1756 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1756); return rv; } } while (false)
;
1757
1758 return internalClose(nativeConn);
1759}
1760
1761NS_IMETHODIMPnsresult
1762Connection::SpinningSynchronousClose() {
1763 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1764 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1765 return rv;
1766 }
1767 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1768 return NS_ERROR_NOT_SAME_THREAD;
1769 }
1770
1771 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1772 // Our only existing caller will never have called close; assert if misused
1773 // so that no new callers assume this works after an AsyncClose.
1774 MOZ_DIAGNOSTIC_ASSERT(connectionReady())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(connectionReady())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(connectionReady()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("connectionReady()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1774); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "connectionReady()"
")"); do { *((volatile int*)__null) = 1774; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1775 if (!connectionReady()) {
1776 return NS_ERROR_UNEXPECTED;
1777 }
1778
1779 RefPtr<CloseListener> listener = new CloseListener();
1780 rv = AsyncClose(listener);
1781 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1781); return rv; } } while (false)
;
1782 MOZ_ALWAYS_TRUE(do { if ((__builtin_expect(!!(SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns
, [&]() { return listener->mClosed; })), 1))) { } else
{ do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1784); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")"); do { *((volatile int*)__null) = 1784; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1783 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,do { if ((__builtin_expect(!!(SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns
, [&]() { return listener->mClosed; })), 1))) { } else
{ do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1784); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")"); do { *((volatile int*)__null) = 1784; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
1784 [&]() { return listener->mClosed; }))do { if ((__builtin_expect(!!(SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns
, [&]() { return listener->mClosed; })), 1))) { } else
{ do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1784); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "SpinEventLoopUntil(\"storage::Connection::SpinningSynchronousClose\"_ns, [&]() { return listener->mClosed; })"
")"); do { *((volatile int*)__null) = 1784; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
1785 MOZ_ASSERT(isClosed(), "The connection should be closed at this point")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isClosed())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isClosed()))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("isClosed()" " (" "The connection should be closed at this point"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1785); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isClosed()"
") (" "The connection should be closed at this point" ")"); do
{ *((volatile int*)__null) = 1785; __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
1786
1787 return rv;
1788}
1789
1790NS_IMETHODIMPnsresult
1791Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1792 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD)do { if ((__builtin_expect(!!(!(NS_IsMainThread())), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "NS_IsMainThread()" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1792); return NS_ERROR_NOT_SAME_THREAD; } } while (false)
;
1793 // Check if AsyncClose or Close were already invoked.
1794 if (!connectionReady()) {
1795 return NS_ERROR_NOT_INITIALIZED;
1796 }
1797 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1798 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1799 return rv;
1800 }
1801
1802 // The two relevant factors at this point are whether we have a database
1803 // connection and whether we have an async execution thread. Here's what the
1804 // states mean and how we handle them:
1805 //
1806 // - (mDBConn && asyncThread): The expected case where we are either an
1807 // async connection or a sync connection that has been used asynchronously.
1808 // Either way the caller must call us and not Close(). Nothing surprising
1809 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1810 // async thread.
1811 //
1812 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1813 // opened the connection synchronously and was planning to use it
1814 // asynchronously, but never got around to using it asynchronously before
1815 // needing to shutdown. This has been observed to happen for the cookie
1816 // service in a case where Firefox shuts itself down almost immediately
1817 // after startup (for unknown reasons). In the Firefox shutdown case,
1818 // we may also fail to create a new async execution thread if one does not
1819 // already exist. (nsThreadManager will refuse to create new threads when
1820 // it has already been told to shutdown.) As such, we need to handle a
1821 // failure to create the async execution thread by falling back to
1822 // synchronous Close() and also dispatching the completion callback because
1823 // at least Places likes to spin a nested event loop that depends on the
1824 // callback being invoked.
1825 //
1826 // Note that we have considered not trying to spin up the async execution
1827 // thread in this case if it does not already exist, but the overhead of
1828 // thread startup (if successful) is significantly less expensive than the
1829 // worst-case potential I/O hit of synchronously closing a database when we
1830 // could close it asynchronously.
1831 //
1832 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1833 // OpenAsyncDatabase encountered a problem opening the database. If it
1834 // happened in all cases AsyncInitDatabase would just shut down the thread
1835 // directly and we would avoid this case. But it doesn't, so for simplicity
1836 // and consistency AsyncCloseConnection knows how to handle this and we
1837 // act like this was the (mDBConn && asyncThread) case in this method.
1838 //
1839 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1840 // Close() or AsyncClose() has already been called (at least) once. This is
1841 // undeniably a misuse case by the caller. We could optimize for this
1842 // case by adding an additional check of mAsyncExecutionThread without using
1843 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1844 // shut it down. But this complicates the method for broken caller code
1845 // whereas we're still correct and safe without the special-case.
1846 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1847
1848 // Create our callback event if we were given a callback. This will
1849 // eventually be dispatched in all cases, even if we fall back to Close() and
1850 // the database wasn't open and we return an error. The rationale is that
1851 // no existing consumer checks our return value and several of them like to
1852 // spin nested event loops until the callback fires. Given that, it seems
1853 // preferable for us to dispatch the callback in all cases. (Except the
1854 // wrong thread misuse case we bailed on up above. But that's okay because
1855 // that is statically wrong whereas these edge cases are dynamic.)
1856 nsCOMPtr<nsIRunnable> completeEvent;
1857 if (aCallback) {
1858 completeEvent = newCompletionEvent(aCallback);
1859 }
1860
1861 if (!asyncThread) {
1862 // We were unable to create an async thread, so we need to fall back to
1863 // using normal Close(). Since there is no async thread, Close() will
1864 // not complain about that. (Close() may, however, complain if the
1865 // connection is closed, but that's okay.)
1866 if (completeEvent) {
1867 // Closing the database is more important than returning an error code
1868 // about a failure to dispatch, especially because all existing native
1869 // callers ignore our return value.
1870 Unused << NS_DispatchToMainThread(completeEvent.forget());
1871 }
1872 MOZ_ALWAYS_SUCCEEDS(synchronousClose())do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(synchronousClose())), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(synchronousClose())" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1872); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(synchronousClose())" ")"); do { *((volatile
int*)__null) = 1872; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false); } } while (false)
;
1873 // Return a success inconditionally here, since Close() is unlikely to fail
1874 // and we want to reassure the consumer that its callback will be invoked.
1875 return NS_OK;
1876 }
1877
1878 // If we're closing the connection during shutdown, and there is an
1879 // interruptible statement running on the helper thread, issue a
1880 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1881 // time (for example a vacuum).
1882 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1883 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1884 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!isClosing())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!isClosing()))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("!isClosing()" " ("
"Must not be closing, see Interrupt()" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1884); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isClosing()"
") (" "Must not be closing, see Interrupt()" ")"); do { *((volatile
int*)__null) = 1884; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
;
1885 DebugOnly<nsresult> rv2 = Interrupt();
1886 MOZ_ASSERT(NS_SUCCEEDED(rv2))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv2)), 1
))))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv2)), 1
)))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv2)), 1)))", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1886); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv2)), 1)))"
")"); do { *((volatile int*)__null) = 1886; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1887 }
1888
1889 // setClosedState nullifies our connection pointer, so we take a raw pointer
1890 // off it, to pass it through the close procedure.
1891 sqlite3* nativeConn = mDBConn;
1892 rv = setClosedState();
1893 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1893); return rv; } } while (false)
;
1894
1895 // Create and dispatch our close event to the background thread.
1896 nsCOMPtr<nsIRunnable> closeEvent =
1897 new AsyncCloseConnection(this, nativeConn, completeEvent);
1898 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
1899 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1899); return rv; } } while (false)
;
1900
1901 return NS_OK;
1902}
1903
1904NS_IMETHODIMPnsresult
1905Connection::AsyncClone(bool aReadOnly,
1906 mozIStorageCompletionCallback* aCallback) {
1907 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER)mozilla::AutoProfilerLabel raiiObject1907( "Connection::AsyncClone"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
1908
1909 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD)do { if ((__builtin_expect(!!(!(NS_IsMainThread())), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "NS_IsMainThread()" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1909); return NS_ERROR_NOT_SAME_THREAD; } } while (false)
;
1910 if (!connectionReady()) {
1911 return NS_ERROR_NOT_INITIALIZED;
1912 }
1913 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1914 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1915 return rv;
1916 }
1917 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1918
1919 int flags = mFlags;
1920 if (aReadOnly) {
1921 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1922 flags = (~SQLITE_OPEN_READWRITE0x00000002 & flags) | SQLITE_OPEN_READONLY0x00000001;
1923 // Turn off SQLITE_OPEN_CREATE.
1924 flags = (~SQLITE_OPEN_CREATE0x00000004 & flags);
1925 }
1926
1927 // The cloned connection will still implement the synchronous API, but throw
1928 // if any synchronous methods are called on the main thread.
1929 RefPtr<Connection> clone =
1930 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename);
1931
1932 RefPtr<AsyncInitializeClone> initEvent =
1933 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1934 // Dispatch to our async thread, since the originating connection must remain
1935 // valid and open for the whole cloning process. This also ensures we are
1936 // properly serialized with a `close` operation, rather than race with it.
1937 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1938 if (!target) {
1939 return NS_ERROR_UNEXPECTED;
1940 }
1941 return target->Dispatch(initEvent, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
1942}
1943
1944nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1945 nsresult rv;
1946 if (!mStorageKey.IsEmpty()) {
1947 rv = aClone->initialize(mStorageKey, mName);
1948 } else if (mFileURL) {
1949 rv = aClone->initialize(mFileURL);
1950 } else {
1951 rv = aClone->initialize(mDatabaseFile);
1952 }
1953 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1954 return rv;
1955 }
1956
1957 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1958
1959 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1960 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1960); return rv; } } while (false)
;
1961
1962 // Re-attach on-disk databases that were attached to the original connection.
1963 {
1964 nsCOMPtr<mozIStorageStatement> stmt;
1965 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1966 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1966); return rv; } } while (false)
;
1967 bool hasResult = false;
1968 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
1969 nsAutoCString name;
1970 rv = stmt->GetUTF8String(1, name);
1971 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !name.EqualsLiteral("main") &&
1972 !name.EqualsLiteral("temp")) {
1973 nsCString path;
1974 rv = stmt->GetUTF8String(2, path);
1975 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !path.IsEmpty()) {
1976 nsCOMPtr<mozIStorageStatement> attachStmt;
1977 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1978 getter_AddRefs(attachStmt));
1979 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1979); return rv; } } while (false)
;
1980 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1981 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1981); return rv; } } while (false)
;
1982 rv = attachStmt->Execute();
1983 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 1983); return rv; } } while (false)
;
1984 }
1985 }
1986 }
1987 }
1988
1989 // Copy over pragmas from the original connection.
1990 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1991 // schema ("main" and any other attached databases), and this implmentation
1992 // fails to propagate them. This is being addressed on trunk.
1993 static const char* pragmas[] = {
1994 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1995 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1996 for (auto& pragma : pragmas) {
1997 // Read-only connections just need cache_size and temp_store pragmas.
1998 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1999 ::strcmp(pragma, "temp_store") != 0) {
2000 continue;
2001 }
2002
2003 nsAutoCString pragmaQuery("PRAGMA ");
2004 pragmaQuery.Append(pragma);
2005 nsCOMPtr<mozIStorageStatement> stmt;
2006 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
2007 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2007); return rv; } } while (false)
;
2008 bool hasResult = false;
2009 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
2010 pragmaQuery.AppendLiteral(" = ");
2011 pragmaQuery.AppendInt(stmt->AsInt32(0));
2012 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
2013 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2013); return rv; } } while (false)
;
2014 }
2015 }
2016
2017 // Copy over temporary tables, triggers, and views from the original
2018 // connections. Entities in `sqlite_temp_master` are only visible to the
2019 // connection that created them.
2020 if (!aReadOnly) {
2021 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
2022 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2022); return rv; } } while (false)
;
2023
2024 nsCOMPtr<mozIStorageStatement> stmt;
2025 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
2026 "WHERE type IN ('table', 'view', "
2027 "'index', 'trigger')"),
2028 getter_AddRefs(stmt));
2029 // Propagate errors, because failing to copy triggers might cause schema
2030 // coherency issues when writing to the database from the cloned connection.
2031 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2031); return rv; } } while (false)
;
2032 bool hasResult = false;
2033 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
2034 nsAutoCString query;
2035 rv = stmt->GetUTF8String(0, query);
2036 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2036); return rv; } } while (false)
;
2037
2038 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
2039 // keyword. We need to add it back, or we'll recreate temporary entities
2040 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
2041 // statements, but those don't need `TEMP` keywords.
2042 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
2043 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
2044 StringBeginsWith(query, "CREATE VIEW "_ns)) {
2045 query.Replace(0, 6, "CREATE TEMP");
2046 }
2047
2048 rv = aClone->ExecuteSimpleSQL(query);
2049 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2049); return rv; } } while (false)
;
2050 }
2051
2052 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
2053 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2053); return rv; } } while (false)
;
2054 }
2055
2056 // Copy any functions that have been added to this connection.
2057 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2058 for (const auto& entry : mFunctions) {
2059 const nsACString& key = entry.GetKey();
2060 Connection::FunctionInfo data = entry.GetData();
2061
2062 rv = aClone->CreateFunction(key, data.numArgs, data.function);
2063 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2064 NS_WARNING("Failed to copy function to cloned connection")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to copy function to cloned connection"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2064)
;
2065 }
2066 }
2067
2068 // Load SQLite extensions that were on this connection.
2069 // Copy into an array rather than holding the mutex while we load extensions.
2070 nsTArray<nsCString> loadedExtensions;
2071 {
2072 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2073 AppendToArray(loadedExtensions, mLoadedExtensions);
2074 }
2075 for (const auto& extension : loadedExtensions) {
2076 (void)aClone->LoadExtension(extension, nullptr);
2077 }
2078
2079 guard.release();
2080 return NS_OK;
2081}
2082
2083NS_IMETHODIMPnsresult
2084Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
2085 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2085); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2085; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2086
2087 AUTO_PROFILER_LABEL("Connection::Clone", OTHER)mozilla::AutoProfilerLabel raiiObject2087( "Connection::Clone"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
2088
2089 if (!connectionReady()) {
2090 return NS_ERROR_NOT_INITIALIZED;
2091 }
2092 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2093 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2094 return rv;
2095 }
2096
2097 int flags = mFlags;
2098 if (aReadOnly) {
2099 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
2100 flags = (~SQLITE_OPEN_READWRITE0x00000002 & flags) | SQLITE_OPEN_READONLY0x00000001;
2101 // Turn off SQLITE_OPEN_CREATE.
2102 flags = (~SQLITE_OPEN_CREATE0x00000004 & flags);
2103 }
2104
2105 RefPtr<Connection> clone =
2106 new Connection(mStorageService, flags, mSupportedOperations,
2107 mTelemetryFilename, mInterruptible);
2108
2109 rv = initializeClone(clone, aReadOnly);
2110 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2111 return rv;
2112 }
2113
2114 NS_IF_ADDREF(*_connection = clone)ns_if_addref(*_connection = clone);
2115 return NS_OK;
2116}
2117
2118NS_IMETHODIMPnsresult
2119Connection::Interrupt() {
2120 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mInterruptible)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mInterruptible))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("mInterruptible"
" (" "Interrupt method not allowed" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2120); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mInterruptible"
") (" "Interrupt method not allowed" ")"); do { *((volatile int
*)__null) = 2120; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
;
2121 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,do { if (SYNCHRONOUS == mSupportedOperations) { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(!IsOnCurrentSerialEventTarget
(eventTargetOpenedOn))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!IsOnCurrentSerialEventTarget
(eventTargetOpenedOn)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2122); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2122; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
2122 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { if (SYNCHRONOUS == mSupportedOperations) { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(!IsOnCurrentSerialEventTarget
(eventTargetOpenedOn))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!IsOnCurrentSerialEventTarget
(eventTargetOpenedOn)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2122); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2122; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2123 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,do { if (ASYNCHRONOUS == mSupportedOperations) { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(IsOnCurrentSerialEventTarget
(eventTargetOpenedOn))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(IsOnCurrentSerialEventTarget
(eventTargetOpenedOn)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2124); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2124; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
2124 IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { if (ASYNCHRONOUS == mSupportedOperations) { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(IsOnCurrentSerialEventTarget
(eventTargetOpenedOn))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(IsOnCurrentSerialEventTarget
(eventTargetOpenedOn)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2124); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2124; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2125
2126 if (!connectionReady()) {
2127 return NS_ERROR_NOT_INITIALIZED;
2128 }
2129
2130 if (isClosing()) { // Closing already in asynchronous case
2131 return NS_OK;
2132 }
2133
2134 {
2135 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
2136 // it is not safe to call sqlite3_interrupt() when
2137 // database connection is closed or might close before
2138 // sqlite3_interrupt() returns.
2139 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2140 if (!isClosed(lockedScope)) {
2141 MOZ_ASSERT(mDBConn)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mDBConn)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mDBConn))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mDBConn", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2141); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mDBConn" ")"
); do { *((volatile int*)__null) = 2141; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2142 ::sqlite3_interrupt(mDBConn);
2143 }
2144 }
2145
2146 return NS_OK;
2147}
2148
2149NS_IMETHODIMPnsresult
2150Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
2151 bool aUseIncremental, int32_t aSetPageSize) {
2152 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD)do { if ((__builtin_expect(!!(!(NS_IsMainThread())), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "NS_IsMainThread()" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2152); return NS_ERROR_NOT_SAME_THREAD; } } while (false)
;
2153 // Abort if we're shutting down.
2154 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2155 return NS_ERROR_ABORT;
2156 }
2157 // Check if AsyncClose or Close were already invoked.
2158 if (!connectionReady()) {
2159 return NS_ERROR_NOT_INITIALIZED;
2160 }
2161 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2162 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2163 return rv;
2164 }
2165 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2166 if (!asyncThread) {
2167 return NS_ERROR_NOT_INITIALIZED;
2168 }
2169
2170 // Create and dispatch our vacuum event to the background thread.
2171 nsCOMPtr<nsIRunnable> vacuumEvent =
2172 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
2173 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
2174 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2174); return rv; } } while (false)
;
2175
2176 return NS_OK;
2177}
2178
2179NS_IMETHODIMPnsresult
2180Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
2181 *_defaultPageSize = Service::kDefaultPageSize;
2182 return NS_OK;
2183}
2184
2185NS_IMETHODIMPnsresult
2186Connection::GetConnectionReady(bool* _ready) {
2187 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(IsOnCurrentSerialEventTarget(eventTargetOpenedOn))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2187); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsOnCurrentSerialEventTarget(eventTargetOpenedOn)"
")"); do { *((volatile int*)__null) = 2187; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2188 *_ready = connectionReady();
2189 return NS_OK;
2190}
2191
2192NS_IMETHODIMPnsresult
2193Connection::GetDatabaseFile(nsIFile** _dbFile) {
2194 if (!connectionReady()) {
2195 return NS_ERROR_NOT_INITIALIZED;
2196 }
2197 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2198 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2199 return rv;
2200 }
2201
2202 NS_IF_ADDREF(*_dbFile = mDatabaseFile)ns_if_addref(*_dbFile = mDatabaseFile);
2203
2204 return NS_OK;
2205}
2206
2207NS_IMETHODIMPnsresult
2208Connection::GetLastInsertRowID(int64_t* _id) {
2209 if (!connectionReady()) {
2210 return NS_ERROR_NOT_INITIALIZED;
2211 }
2212 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2213 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2214 return rv;
2215 }
2216
2217 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2218 *_id = id;
2219
2220 return NS_OK;
2221}
2222
2223NS_IMETHODIMPnsresult
2224Connection::GetAffectedRows(int32_t* _rows) {
2225 if (!connectionReady()) {
2226 return NS_ERROR_NOT_INITIALIZED;
2227 }
2228 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2229 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2230 return rv;
2231 }
2232
2233 *_rows = ::sqlite3_changes(mDBConn);
2234
2235 return NS_OK;
2236}
2237
2238NS_IMETHODIMPnsresult
2239Connection::GetLastError(int32_t* _error) {
2240 if (!connectionReady()) {
2241 return NS_ERROR_NOT_INITIALIZED;
2242 }
2243 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2244 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2245 return rv;
2246 }
2247
2248 *_error = ::sqlite3_errcode(mDBConn);
2249
2250 return NS_OK;
2251}
2252
2253NS_IMETHODIMPnsresult
2254Connection::GetLastErrorString(nsACString& _errorString) {
2255 if (!connectionReady()) {
2256 return NS_ERROR_NOT_INITIALIZED;
2257 }
2258 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2259 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2260 return rv;
2261 }
2262
2263 const char* serr = ::sqlite3_errmsg(mDBConn);
2264 _errorString.Assign(serr);
2265
2266 return NS_OK;
2267}
2268
2269NS_IMETHODIMPnsresult
2270Connection::GetSchemaVersion(int32_t* _version) {
2271 if (!connectionReady()) {
2272 return NS_ERROR_NOT_INITIALIZED;
2273 }
2274 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2275 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2276 return rv;
2277 }
2278
2279 nsCOMPtr<mozIStorageStatement> stmt;
2280 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2281 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY)do { if ((__builtin_expect(!!(!(stmt)), 0))) { NS_DebugBreak(
NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "stmt" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2281); return NS_ERROR_OUT_OF_MEMORY; } } while (false)
;
2282
2283 *_version = 0;
2284 bool hasResult;
2285 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
2286 *_version = stmt->AsInt32(0);
2287 }
2288
2289 return NS_OK;
2290}
2291
2292NS_IMETHODIMPnsresult
2293Connection::SetSchemaVersion(int32_t aVersion) {
2294 if (!connectionReady()) {
2295 return NS_ERROR_NOT_INITIALIZED;
2296 }
2297 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2298 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2299 return rv;
2300 }
2301
2302 nsAutoCString stmt("PRAGMA user_version = "_ns);
2303 stmt.AppendInt(aVersion);
2304
2305 return ExecuteSimpleSQL(stmt);
2306}
2307
2308NS_IMETHODIMPnsresult
2309Connection::CreateStatement(const nsACString& aSQLStatement,
2310 mozIStorageStatement** _stmt) {
2311 NS_ENSURE_ARG_POINTER(_stmt)do { if ((__builtin_expect(!!(!(_stmt)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "_stmt" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2311); return NS_ERROR_INVALID_POINTER; } } while (false)
;
2312 if (!connectionReady()) {
2313 return NS_ERROR_NOT_INITIALIZED;
2314 }
2315 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2316 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2317 return rv;
2318 }
2319
2320 RefPtr<Statement> statement(new Statement());
2321 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY)do { if ((__builtin_expect(!!(!(statement)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "statement" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2321); return NS_ERROR_OUT_OF_MEMORY; } } while (false)
;
2322
2323 rv = statement->initialize(this, mDBConn, aSQLStatement);
2324 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2324); return rv; } } while (false)
;
2325
2326 Statement* rawPtr;
2327 statement.forget(&rawPtr);
2328 *_stmt = rawPtr;
2329 return NS_OK;
2330}
2331
2332NS_IMETHODIMPnsresult
2333Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2334 mozIStorageAsyncStatement** _stmt) {
2335 NS_ENSURE_ARG_POINTER(_stmt)do { if ((__builtin_expect(!!(!(_stmt)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "_stmt" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2335); return NS_ERROR_INVALID_POINTER; } } while (false)
;
2336 if (!connectionReady()) {
2337 return NS_ERROR_NOT_INITIALIZED;
2338 }
2339 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2340 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2341 return rv;
2342 }
2343
2344 RefPtr<AsyncStatement> statement(new AsyncStatement());
2345 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY)do { if ((__builtin_expect(!!(!(statement)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "statement" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2345); return NS_ERROR_OUT_OF_MEMORY; } } while (false)
;
2346
2347 rv = statement->initialize(this, mDBConn, aSQLStatement);
2348 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2348); return rv; } } while (false)
;
2349
2350 AsyncStatement* rawPtr;
2351 statement.forget(&rawPtr);
2352 *_stmt = rawPtr;
2353 return NS_OK;
2354}
2355
2356NS_IMETHODIMPnsresult
2357Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2358 CHECK_MAINTHREAD_ABUSE()do { do { if (!(eventTargetOpenedOn == GetMainThreadSerialEventTarget
() || !NS_IsMainThread())) { NS_DebugBreak(NS_DEBUG_WARNING, "Using Storage synchronous API on main-thread, but "
"the connection was opened on another thread.", "eventTargetOpenedOn == GetMainThreadSerialEventTarget() || !NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2358); } } while (false); } while (0)
;
2359 if (!connectionReady()) {
2360 return NS_ERROR_NOT_INITIALIZED;
2361 }
2362 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2363 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2364 return rv;
2365 }
2366
2367 int srv = executeSql(mDBConn, PromiseFlatCStringTPromiseFlatString<char>(aSQLStatement).get());
2368 return convertResultCode(srv);
2369}
2370
2371NS_IMETHODIMPnsresult
2372Connection::ExecuteAsync(
2373 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2374 mozIStorageStatementCallback* aCallback,
2375 mozIStoragePendingStatement** _handle) {
2376 nsTArray<StatementData> stmts(aStatements.Length());
2377 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2378 nsCOMPtr<StorageBaseStatementInternal> stmt =
2379 do_QueryInterface(aStatements[i]);
2380 NS_ENSURE_STATE(stmt)do { if ((__builtin_expect(!!(!(stmt)), 0))) { NS_DebugBreak(
NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "stmt" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2380); return NS_ERROR_UNEXPECTED; } } while (false)
;
2381
2382 // Obtain our StatementData.
2383 StatementData data;
2384 nsresult rv = stmt->getAsynchronousStatementData(data);
2385 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2385); return rv; } } while (false)
;
2386
2387 NS_ASSERTION(stmt->getOwner() == this,do { if (!(stmt->getOwner() == this)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Statement must be from this database connection!", "stmt->getOwner() == this"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2388); MOZ_PretendNoReturn(); } } while (0)
2388 "Statement must be from this database connection!")do { if (!(stmt->getOwner() == this)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Statement must be from this database connection!", "stmt->getOwner() == this"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2388); MOZ_PretendNoReturn(); } } while (0)
;
2389
2390 // Now append it to our array.
2391 stmts.AppendElement(data);
2392 }
2393
2394 // Dispatch to the background
2395 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2396 aCallback, _handle);
2397}
2398
2399NS_IMETHODIMPnsresult
2400Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2401 mozIStorageStatementCallback* aCallback,
2402 mozIStoragePendingStatement** _handle) {
2403 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD)do { if ((__builtin_expect(!!(!(NS_IsMainThread())), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "NS_IsMainThread()" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2403); return NS_ERROR_NOT_SAME_THREAD; } } while (false)
;
2404
2405 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2406 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2407 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2408 return rv;
2409 }
2410
2411 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2412 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2413 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2414 return rv;
2415 }
2416
2417 pendingStatement.forget(_handle);
2418 return rv;
2419}
2420
2421NS_IMETHODIMPnsresult
2422Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2423 return databaseElementExists(TABLE, aTableName, _exists);
2424}
2425
2426NS_IMETHODIMPnsresult
2427Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2428 return databaseElementExists(INDEX, aIndexName, _exists);
2429}
2430
2431NS_IMETHODIMPnsresult
2432Connection::GetTransactionInProgress(bool* _inProgress) {
2433 if (!connectionReady()) {
2434 return NS_ERROR_NOT_INITIALIZED;
2435 }
2436 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2437 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2438 return rv;
2439 }
2440
2441 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2442 *_inProgress = transactionInProgress(lockedScope);
2443 return NS_OK;
2444}
2445
2446NS_IMETHODIMPnsresult
2447Connection::GetDefaultTransactionType(int32_t* _type) {
2448 *_type = mDefaultTransactionType;
2449 return NS_OK;
2450}
2451
2452NS_IMETHODIMPnsresult
2453Connection::SetDefaultTransactionType(int32_t aType) {
2454 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE)do { if ((__builtin_expect(!!(!(((aType) >= TRANSACTION_DEFERRED
) && ((aType) <= TRANSACTION_EXCLUSIVE))), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "((aType) >= TRANSACTION_DEFERRED) && ((aType) <= TRANSACTION_EXCLUSIVE)"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2454); return NS_ERROR_INVALID_ARG; } } while (false)
;
2455 mDefaultTransactionType = aType;
2456 return NS_OK;
2457}
2458
2459NS_IMETHODIMPnsresult
2460Connection::GetVariableLimit(int32_t* _limit) {
2461 if (!connectionReady()) {
2462 return NS_ERROR_NOT_INITIALIZED;
2463 }
2464 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER9, -1);
2465 if (limit < 0) {
2466 return NS_ERROR_UNEXPECTED;
2467 }
2468 *_limit = limit;
2469 return NS_OK;
2470}
2471
2472NS_IMETHODIMPnsresult
2473Connection::SetVariableLimit(int32_t limit) {
2474 if (!connectionReady()) {
2475 return NS_ERROR_NOT_INITIALIZED;
2476 }
2477 int oldLimit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER9, limit);
2478 if (oldLimit < 0) {
2479 return NS_ERROR_UNEXPECTED;
2480 }
2481 return NS_OK;
2482}
2483
2484NS_IMETHODIMPnsresult
2485Connection::BeginTransaction() {
2486 if (!connectionReady()) {
2487 return NS_ERROR_NOT_INITIALIZED;
2488 }
2489 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2490 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2491 return rv;
2492 }
2493
2494 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2495 return beginTransactionInternal(lockedScope, mDBConn,
2496 mDefaultTransactionType);
2497}
2498
2499nsresult Connection::beginTransactionInternal(
2500 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2501 int32_t aTransactionType) {
2502 if (transactionInProgress(aProofOfLock)) {
2503 return NS_ERROR_FAILURE;
2504 }
2505 nsresult rv;
2506 switch (aTransactionType) {
2507 case TRANSACTION_DEFERRED:
2508 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2509 break;
2510 case TRANSACTION_IMMEDIATE:
2511 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2512 break;
2513 case TRANSACTION_EXCLUSIVE:
2514 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2515 break;
2516 default:
2517 return NS_ERROR_ILLEGAL_VALUE;
2518 }
2519 return rv;
2520}
2521
2522NS_IMETHODIMPnsresult
2523Connection::CommitTransaction() {
2524 if (!connectionReady()) {
2525 return NS_ERROR_NOT_INITIALIZED;
2526 }
2527 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2528 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2529 return rv;
2530 }
2531
2532 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2533 return commitTransactionInternal(lockedScope, mDBConn);
2534}
2535
2536nsresult Connection::commitTransactionInternal(
2537 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2538 if (!transactionInProgress(aProofOfLock)) {
2539 return NS_ERROR_UNEXPECTED;
2540 }
2541 nsresult rv =
2542 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2543 return rv;
2544}
2545
2546NS_IMETHODIMPnsresult
2547Connection::RollbackTransaction() {
2548 if (!connectionReady()) {
2549 return NS_ERROR_NOT_INITIALIZED;
2550 }
2551 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2552 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2553 return rv;
2554 }
2555
2556 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2557 return rollbackTransactionInternal(lockedScope, mDBConn);
2558}
2559
2560nsresult Connection::rollbackTransactionInternal(
2561 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2562 if (!transactionInProgress(aProofOfLock)) {
2563 return NS_ERROR_UNEXPECTED;
2564 }
2565
2566 nsresult rv =
2567 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2568 return rv;
2569}
2570
2571NS_IMETHODIMPnsresult
2572Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2573 if (!connectionReady()) {
2574 return NS_ERROR_NOT_INITIALIZED;
2575 }
2576 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2577 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2578 return rv;
2579 }
2580
2581 SmprintfPointer buf =
2582 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2583 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2584
2585 int srv = executeSql(mDBConn, buf.get());
2586
2587 return convertResultCode(srv);
2588}
2589
2590NS_IMETHODIMPnsresult
2591Connection::CreateFunction(const nsACString& aFunctionName,
2592 int32_t aNumArguments,
2593 mozIStorageFunction* aFunction) {
2594 if (!connectionReady()) {
2595 return NS_ERROR_NOT_INITIALIZED;
2596 }
2597 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2598 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2599 return rv;
2600 }
2601
2602 // Check to see if this function is already defined. We only check the name
2603 // because a function can be defined with the same body but different names.
2604 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2605 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(!(mFunctions.Contains(aFunctionName
)))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE("
"!(mFunctions.Contains(aFunctionName))" ") failed", nullptr,
"/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2605); return NS_ERROR_FAILURE; } } while (false)
;
2606
2607 int srv = ::sqlite3_create_function(
2608 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2609 SQLITE_ANY5, aFunction, basicFunctionHelper, nullptr, nullptr);
2610 if (srv != SQLITE_OK0) return convertResultCode(srv);
2611
2612 FunctionInfo info = {aFunction, aNumArguments};
2613 mFunctions.InsertOrUpdate(aFunctionName, info);
2614
2615 return NS_OK;
2616}
2617
2618NS_IMETHODIMPnsresult
2619Connection::RemoveFunction(const nsACString& aFunctionName) {
2620 if (!connectionReady()) {
2621 return NS_ERROR_NOT_INITIALIZED;
2622 }
2623 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2624 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2625 return rv;
2626 }
2627
2628 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2629 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(mFunctions.Get(aFunctionName,
nullptr))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE("
"mFunctions.Get(aFunctionName, nullptr)" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2629); return NS_ERROR_FAILURE; } } while (false)
;
2630
2631 int srv = ::sqlite3_create_function(
2632 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY5,
2633 nullptr, nullptr, nullptr, nullptr);
2634 if (srv != SQLITE_OK0) return convertResultCode(srv);
2635
2636 mFunctions.Remove(aFunctionName);
2637
2638 return NS_OK;
2639}
2640
2641NS_IMETHODIMPnsresult
2642Connection::SetProgressHandler(int32_t aGranularity,
2643 mozIStorageProgressHandler* aHandler,
2644 mozIStorageProgressHandler** _oldHandler) {
2645 if (!connectionReady()) {
2646 return NS_ERROR_NOT_INITIALIZED;
2647 }
2648 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2649 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2650 return rv;
2651 }
2652
2653 // Return previous one
2654 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2655 NS_IF_ADDREF(*_oldHandler = mProgressHandler)ns_if_addref(*_oldHandler = mProgressHandler);
2656
2657 if (!aHandler || aGranularity <= 0) {
2658 aHandler = nullptr;
2659 aGranularity = 0;
2660 }
2661 mProgressHandler = aHandler;
2662 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2663
2664 return NS_OK;
2665}
2666
2667NS_IMETHODIMPnsresult
2668Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2669 if (!connectionReady()) {
2670 return NS_ERROR_NOT_INITIALIZED;
2671 }
2672 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2673 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2674 return rv;
2675 }
2676
2677 // Return previous one
2678 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2679 NS_IF_ADDREF(*_oldHandler = mProgressHandler)ns_if_addref(*_oldHandler = mProgressHandler);
2680
2681 mProgressHandler = nullptr;
2682 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2683
2684 return NS_OK;
2685}
2686
2687NS_IMETHODIMPnsresult
2688Connection::SetGrowthIncrement(int32_t aChunkSize,
2689 const nsACString& aDatabaseName) {
2690 if (!connectionReady()) {
2691 return NS_ERROR_NOT_INITIALIZED;
2692 }
2693 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2694 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2695 return rv;
2696 }
2697
2698 // Bug 597215: Disk space is extremely limited on Android
2699 // so don't preallocate space. This is also not effective
2700 // on log structured file systems used by Android devices
2701#if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2702 // Don't preallocate if less than 500MiB is available.
2703 int64_t bytesAvailable;
2704 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2705 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2705); return rv; } } while (false)
;
2706 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH524288000) {
2707 return NS_ERROR_FILE_TOO_BIG;
2708 }
2709
2710 int srv = ::sqlite3_file_control(
2711 mDBConn,
2712 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2713 : nullptr,
2714 SQLITE_FCNTL_CHUNK_SIZE6, &aChunkSize);
2715 if (srv == SQLITE_OK0) {
2716 mGrowthChunkSize = aChunkSize;
2717 }
2718#endif
2719 return NS_OK;
2720}
2721
2722int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2723 int32_t freeListPagesCount = 0;
2724 if (!isConnectionReadyOnThisThread()) {
2725 MOZ_ASSERT(false, "Database connection is not ready")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Database connection is not ready"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2725); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Database connection is not ready" ")"); do { *((volatile int
*)__null) = 2725; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false)
;
2726 return freeListPagesCount;
2727 }
2728 {
2729 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
2730 query.Append(aSchemaName);
2731 query.AppendLiteral(".freelist_count");
2732 nsCOMPtr<mozIStorageStatement> stmt;
2733 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2734 MOZ_ASSERT(NS_SUCCEEDED(rv))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2734); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
")"); do { *((volatile int*)__null) = 2734; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2735 bool hasResult = false;
2736 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
2737 freeListPagesCount = stmt->AsInt32(0);
2738 }
2739 }
2740 // If there's no chunk size set, any page is good to be removed.
2741 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2742 return freeListPagesCount;
2743 }
2744 int32_t pageSize;
2745 {
2746 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR"/* " "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
" */ "
"PRAGMA ");
2747 query.Append(aSchemaName);
2748 query.AppendLiteral(".page_size");
2749 nsCOMPtr<mozIStorageStatement> stmt;
2750 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2751 MOZ_ASSERT(NS_SUCCEEDED(rv))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
)))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2751); AnnotateMozCrashReason("MOZ_ASSERT" "(" "((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))"
")"); do { *((volatile int*)__null) = 2751; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2752 bool hasResult = false;
2753 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult))((bool)(__builtin_expect(!!(!NS_FAILED_impl(stmt->ExecuteStep
(&hasResult))), 1)))
&& hasResult) {
2754 pageSize = stmt->AsInt32(0);
2755 } else {
2756 MOZ_ASSERT(false, "Couldn't get page_size")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "Couldn't get page_size"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2756); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"Couldn't get page_size" ")"); do { *((volatile int*)__null)
= 2756; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false)
;
2757 return 0;
2758 }
2759 }
2760 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2761}
2762
2763NS_IMETHODIMPnsresult
2764Connection::LoadExtension(const nsACString& aExtensionName,
2765 mozIStorageCompletionCallback* aCallback) {
2766 AUTO_PROFILER_LABEL("Connection::LoadExtension", OTHER)mozilla::AutoProfilerLabel raiiObject2766( "Connection::LoadExtension"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
2767
2768 // This is a static list of extensions we can load.
2769 // Please use lowercase ASCII names and keep this list alphabetically ordered.
2770 static constexpr nsLiteralCString sSupportedExtensions[] = {
2771 // clang-format off
2772 "fts5"_ns,
2773 // clang-format on
2774 };
2775 if (std::find(std::begin(sSupportedExtensions),
2776 std::end(sSupportedExtensions),
2777 aExtensionName) == std::end(sSupportedExtensions)) {
2778 return NS_ERROR_INVALID_ARG;
2779 }
2780
2781 if (!connectionReady()) {
2782 return NS_ERROR_NOT_INITIALIZED;
2783 }
2784
2785 int srv = ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005,
2786 1, nullptr);
2787 if (srv != SQLITE_OK0) {
2788 return NS_ERROR_UNEXPECTED;
2789 }
2790
2791 // Track the loaded extension for later connection cloning operations.
2792 {
2793 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2794 if (!mLoadedExtensions.EnsureInserted(aExtensionName)) {
2795 // Already loaded, bail out but issue a warning.
2796 NS_WARNING(nsPrintfCString(NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString( "Tried to register '%s' SQLite extension multiple times!"
, TPromiseFlatString<char>(aExtensionName).get()) .get(
), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2799)
2797 "Tried to register '%s' SQLite extension multiple times!",NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString( "Tried to register '%s' SQLite extension multiple times!"
, TPromiseFlatString<char>(aExtensionName).get()) .get(
), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2799)
2798 PromiseFlatCString(aExtensionName).get())NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString( "Tried to register '%s' SQLite extension multiple times!"
, TPromiseFlatString<char>(aExtensionName).get()) .get(
), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2799)
2799 .get())NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString( "Tried to register '%s' SQLite extension multiple times!"
, TPromiseFlatString<char>(aExtensionName).get()) .get(
), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2799)
;
2800 return NS_OK;
2801 }
2802 }
2803
2804 nsAutoCString entryPoint("sqlite3_");
2805 entryPoint.Append(aExtensionName);
2806 entryPoint.AppendLiteral("_init");
2807
2808 RefPtr<Runnable> loadTask = NS_NewRunnableFunction(
2809 "mozStorageConnection::LoadExtension",
2810 [this, self = RefPtr(this), entryPoint,
2811 callback = RefPtr(aCallback)]() mutable {
2812 MOZ_ASSERT(do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
2813 !NS_IsMainThread() ||do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
2814 (operationSupported(Connection::SYNCHRONOUS) &&do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
2815 eventTargetOpenedOn == GetMainThreadSerialEventTarget()),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
2816 "Should happen on main-thread only for synchronous connections "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
2817 "opened on the main thread")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
()))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!NS_IsMainThread() || (operationSupported(Connection
::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget
())))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
" (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2817); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!NS_IsMainThread() || (operationSupported(Connection::SYNCHRONOUS) && eventTargetOpenedOn == GetMainThreadSerialEventTarget())"
") (" "Should happen on main-thread only for synchronous connections "
"opened on the main thread" ")"); do { *((volatile int*)__null
) = 2817; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false)
;
2818#ifdef MOZ_FOLD_LIBS
2819 int srv = ::sqlite3_load_extension(mDBConn,
2820 MOZ_DLL_PREFIX"lib" "nss3" MOZ_DLL_SUFFIX".so",
2821 entryPoint.get(), nullptr);
2822#else
2823 int srv = ::sqlite3_load_extension(
2824 mDBConn, MOZ_DLL_PREFIX"lib" "mozsqlite3" MOZ_DLL_SUFFIX".so",
2825 entryPoint.get(), nullptr);
2826#endif
2827 if (!callback) {
2828 return;
2829 };
2830 RefPtr<Runnable> callbackTask = NS_NewRunnableFunction(
2831 "mozStorageConnection::LoadExtension_callback",
2832 [callback = std::move(callback), srv]() {
2833 (void)callback->Complete(convertResultCode(srv), nullptr);
2834 });
2835 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
2836 MOZ_ALWAYS_SUCCEEDS(callbackTask->Run())do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(callbackTask->Run())), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(callbackTask->Run())" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2836); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(callbackTask->Run())" ")"); do { *((volatile
int*)__null) = 2836; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false); } } while (false)
;
2837 } else {
2838 // Redispatch the callback to the calling thread.
2839 MOZ_ALWAYS_SUCCEEDS(eventTargetOpenedOn->Dispatch(do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget
::DISPATCH_NORMAL))), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2840); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")"); do { *((volatile int*)__null) = 2840; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
2840 callbackTask.forget(), NS_DISPATCH_NORMAL))do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget
::DISPATCH_NORMAL))), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2840); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(eventTargetOpenedOn->Dispatch( callbackTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")"); do { *((volatile int*)__null) = 2840; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2841 }
2842 });
2843
2844 if (NS_IsMainThread() && !operationSupported(Connection::SYNCHRONOUS)) {
2845 // This is a main-thread call to an async-only connection, thus we should
2846 // load the library in the helper thread.
2847 nsIEventTarget* helperThread = getAsyncExecutionTarget();
2848 if (!helperThread) {
2849 return NS_ERROR_NOT_INITIALIZED;
2850 }
2851 MOZ_ALWAYS_SUCCEEDS(do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::
DISPATCH_NORMAL))), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2852); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")"); do { *((volatile int*)__null) = 2852; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
2852 helperThread->Dispatch(loadTask.forget(), NS_DISPATCH_NORMAL))do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::
DISPATCH_NORMAL))), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2852); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(helperThread->Dispatch(loadTask.forget(), nsIEventTarget::DISPATCH_NORMAL))"
")"); do { *((volatile int*)__null) = 2852; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2853 } else {
2854 // In any other case we just load the extension on the current thread.
2855 MOZ_ALWAYS_SUCCEEDS(loadTask->Run())do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(loadTask->Run())), 1)))), 1))) { } else { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(false)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(false))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("false" " (" "NS_SUCCEEDED(loadTask->Run())" ")", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2855); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(loadTask->Run())" ")"); do { *((volatile
int*)__null) = 2855; __attribute__((nomerge)) ::abort(); } while
(false); } } while (false); } } while (false)
;
2856 }
2857 return NS_OK;
2858}
2859
2860NS_IMETHODIMPnsresult
2861Connection::EnableModule(const nsACString& aModuleName) {
2862 if (!connectionReady()) {
2863 return NS_ERROR_NOT_INITIALIZED;
2864 }
2865 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2866 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2867 return rv;
2868 }
2869
2870 for (auto& gModule : gModules) {
2871 struct Module* m = &gModule;
2872 if (aModuleName.Equals(m->name)) {
2873 int srv = m->registerFunc(mDBConn, m->name);
2874 if (srv != SQLITE_OK0) return convertResultCode(srv);
2875
2876 return NS_OK;
2877 }
2878 }
2879
2880 return NS_ERROR_FAILURE;
2881}
2882
2883NS_IMETHODIMPnsresult
2884Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2885 QuotaObject** aJournalQuotaObject) {
2886 MOZ_ASSERT(aDatabaseQuotaObject)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aDatabaseQuotaObject)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aDatabaseQuotaObject))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("aDatabaseQuotaObject"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2886); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDatabaseQuotaObject"
")"); do { *((volatile int*)__null) = 2886; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2887 MOZ_ASSERT(aJournalQuotaObject)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aJournalQuotaObject)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aJournalQuotaObject))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("aJournalQuotaObject"
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2887); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aJournalQuotaObject"
")"); do { *((volatile int*)__null) = 2887; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2888
2889 if (!connectionReady()) {
2890 return NS_ERROR_NOT_INITIALIZED;
2891 }
2892 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2893 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2894 return rv;
2895 }
2896
2897 sqlite3_file* file;
2898 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER7,
2899 &file);
2900 if (srv != SQLITE_OK0) {
2901 return convertResultCode(srv);
2902 }
2903
2904 sqlite3_vfs* vfs;
2905 srv =
2906 ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_VFS_POINTER27, &vfs);
2907 if (srv != SQLITE_OK0) {
2908 return convertResultCode(srv);
2909 }
2910
2911 bool obfusactingVFS = false;
2912
2913 {
2914 const nsDependentCString vfsName{vfs->zName};
2915
2916 if (vfsName == obfsvfs::GetVFSName()) {
2917 obfusactingVFS = true;
2918 } else if (vfsName != quotavfs::GetVFSName()) {
2919 NS_WARNING("Got unexpected vfs")NS_DebugBreak(NS_DEBUG_WARNING, "Got unexpected vfs", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2919)
;
2920 return NS_ERROR_FAILURE;
2921 }
2922 }
2923
2924 RefPtr<QuotaObject> databaseQuotaObject =
2925 GetQuotaObject(file, obfusactingVFS);
2926 if (NS_WARN_IF(!databaseQuotaObject)NS_warn_if_impl(!databaseQuotaObject, "!databaseQuotaObject",
"/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2926)
) {
2927 return NS_ERROR_FAILURE;
2928 }
2929
2930 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER28,
2931 &file);
2932 if (srv != SQLITE_OK0) {
2933 return convertResultCode(srv);
2934 }
2935
2936 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObject(file, obfusactingVFS);
2937 if (NS_WARN_IF(!journalQuotaObject)NS_warn_if_impl(!journalQuotaObject, "!journalQuotaObject", "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2937)
) {
2938 return NS_ERROR_FAILURE;
2939 }
2940
2941 databaseQuotaObject.forget(aDatabaseQuotaObject);
2942 journalQuotaObject.forget(aJournalQuotaObject);
2943 return NS_OK;
2944}
2945
2946SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2947
2948uint32_t Connection::GetTransactionNestingLevel(
2949 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2950 return mTransactionNestingLevel;
2951}
2952
2953uint32_t Connection::IncreaseTransactionNestingLevel(
2954 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2955 return ++mTransactionNestingLevel;
2956}
2957
2958uint32_t Connection::DecreaseTransactionNestingLevel(
2959 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2960 return --mTransactionNestingLevel;
2961}
2962
2963NS_IMETHODIMPnsresult
2964Connection::BackupToFileAsync(nsIFile* aDestinationFile,
2965 mozIStorageCompletionCallback* aCallback,
2966 uint32_t aPagesPerStep, uint32_t aStepDelayMs) {
2967 NS_ENSURE_ARG(aDestinationFile)do { if ((__builtin_expect(!!(!(aDestinationFile)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "aDestinationFile" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2967); return NS_ERROR_INVALID_ARG; } } while (false)
;
2968 NS_ENSURE_ARG(aCallback)do { if ((__builtin_expect(!!(!(aCallback)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "aCallback" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2968); return NS_ERROR_INVALID_ARG; } } while (false)
;
2969 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD)do { if ((__builtin_expect(!!(!(NS_IsMainThread())), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "NS_IsMainThread()" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/storage/mozStorageConnection.cpp"
, 2969); return NS_ERROR_NOT_SAME_THREAD; } } while (false)
;
2970
2971 // Abort if we're shutting down.
2972 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2973 return NS_ERROR_ABORT;
2974 }
2975 // Check if AsyncClose or Close were already invoked.
2976 if (!connectionReady()) {
2977 return NS_ERROR_NOT_INITIALIZED;
2978 }
2979 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2980 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2981 return rv;
2982 }
2983 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2984 if (!asyncThread) {
2985 return NS_ERROR_NOT_INITIALIZED;
2986 }
2987
2988 // The number of pages of the database to copy per step
2989 static constexpr int32_t DEFAULT_PAGES_PER_STEP = 5;
2990 // The number of milliseconds to wait between each step.
2991 static constexpr uint32_t DEFAULT_STEP_DELAY_MS = 250;
2992
2993 CheckedInt<int32_t> pagesPerStep(aPagesPerStep);
2994 if (!pagesPerStep.isValid()) {
2995 return NS_ERROR_INVALID_ARG;
2996 }
2997
2998 if (!pagesPerStep.value()) {
2999 pagesPerStep = DEFAULT_PAGES_PER_STEP;
3000 }
3001
3002 if (!aStepDelayMs) {
3003 aStepDelayMs = DEFAULT_STEP_DELAY_MS;
3004 }
3005
3006 // Create and dispatch our backup event to the execution thread.
3007 nsCOMPtr<nsIRunnable> backupEvent =
3008 new AsyncBackupDatabaseFile(this, mDBConn, aDestinationFile, aCallback,
3009 pagesPerStep.value(), aStepDelayMs);
3010 rv = asyncThread->Dispatch(backupEvent, NS_DISPATCH_NORMALnsIEventTarget::DISPATCH_NORMAL);
3011 return rv;
3012}
3013
3014} // namespace mozilla::storage