blob: c3c2b9ae2418ddb8a4dda2b7cc353c3237384b49 [file] [log] [blame]
shess5f2c3442017-01-24 02:15:101// Copyright (c) 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "sql/vfs_wrapper.h"
6
7#include <algorithm>
8#include <string>
9#include <vector>
10
11#include "base/debug/leak_annotations.h"
Victor Costan7e74dce2019-01-28 20:15:2512#include "base/files/file_path.h"
shess5f2c3442017-01-24 02:15:1013#include "base/logging.h"
14#include "base/memory/ptr_util.h"
shess3998fbc2017-03-15 16:18:3115#include "base/metrics/histogram_macros.h"
shess5f2c3442017-01-24 02:15:1016#include "base/strings/string_piece.h"
Sergey Ulanov7de7e9f2019-03-09 00:41:0217#include "build/build_config.h"
shess5f2c3442017-01-24 02:15:1018
19#if defined(OS_MACOSX) && !defined(OS_IOS)
shess5f2c3442017-01-24 02:15:1020#include "base/mac/mac_util.h"
shess5f2c3442017-01-24 02:15:1021#endif
22
Sergey Ulanov7de7e9f2019-03-09 00:41:0223#if defined(OS_FUCHSIA)
24#include "sql/vfs_wrapper_fuchsia.h"
25#endif
26
shess5f2c3442017-01-24 02:15:1027namespace sql {
28namespace {
29
30// https://www.sqlite.org/vfs.html - documents the overall VFS system.
31//
32// https://www.sqlite.org/c3ref/vfs.html - VFS methods. This code tucks the
33// wrapped VFS pointer into the wrapper's pAppData pointer.
34//
35// https://www.sqlite.org/c3ref/file.html - instance of an open file. This code
36// allocates a VfsFile for this, which contains a pointer to the wrapped file.
37// Idiomatic SQLite would take the wrapped VFS szOsFile and increase it to store
38// additional data as a prefix.
39
40sqlite3_vfs* GetWrappedVfs(sqlite3_vfs* wrapped_vfs) {
41 return static_cast<sqlite3_vfs*>(wrapped_vfs->pAppData);
42}
43
shess5f2c3442017-01-24 02:15:1044VfsFile* AsVfsFile(sqlite3_file* wrapper_file) {
45 return reinterpret_cast<VfsFile*>(wrapper_file);
46}
47
48sqlite3_file* GetWrappedFile(sqlite3_file* wrapper_file) {
49 return AsVfsFile(wrapper_file)->wrapped_file;
50}
51
52int Close(sqlite3_file* sqlite_file)
53{
54 VfsFile* file = AsVfsFile(sqlite_file);
55
Sergey Ulanov7de7e9f2019-03-09 00:41:0256#if defined(OS_FUCHSIA)
57 FuchsiaVfsUnlock(sqlite_file, SQLITE_LOCK_NONE);
58#endif
59
shess5f2c3442017-01-24 02:15:1060 int r = file->wrapped_file->pMethods->xClose(file->wrapped_file);
61 sqlite3_free(file->wrapped_file);
Sergey Ulanov7de7e9f2019-03-09 00:41:0262
63 // Memory will be freed with sqlite3_free(), so the destructor needs to be
64 // called explicitly.
65 file->~VfsFile();
shess5f2c3442017-01-24 02:15:1066 memset(file, '\0', sizeof(*file));
67 return r;
68}
69
70int Read(sqlite3_file* sqlite_file, void* buf, int amt, sqlite3_int64 ofs)
71{
72 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
73 return wrapped_file->pMethods->xRead(wrapped_file, buf, amt, ofs);
74}
75
76int Write(sqlite3_file* sqlite_file, const void* buf, int amt,
77 sqlite3_int64 ofs)
78{
79 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
80 return wrapped_file->pMethods->xWrite(wrapped_file, buf, amt, ofs);
81}
82
83int Truncate(sqlite3_file* sqlite_file, sqlite3_int64 size)
84{
85 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
86 return wrapped_file->pMethods->xTruncate(wrapped_file, size);
87}
88
89int Sync(sqlite3_file* sqlite_file, int flags)
90{
91 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
92 return wrapped_file->pMethods->xSync(wrapped_file, flags);
93}
94
95int FileSize(sqlite3_file* sqlite_file, sqlite3_int64* size)
96{
97 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
98 return wrapped_file->pMethods->xFileSize(wrapped_file, size);
99}
100
Sergey Ulanov7de7e9f2019-03-09 00:41:02101#if !defined(OS_FUCHSIA)
102
shess5f2c3442017-01-24 02:15:10103int Lock(sqlite3_file* sqlite_file, int file_lock)
104{
105 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
106 return wrapped_file->pMethods->xLock(wrapped_file, file_lock);
107}
108
109int Unlock(sqlite3_file* sqlite_file, int file_lock)
110{
111 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
112 return wrapped_file->pMethods->xUnlock(wrapped_file, file_lock);
113}
114
115int CheckReservedLock(sqlite3_file* sqlite_file, int* result)
116{
117 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
118 return wrapped_file->pMethods->xCheckReservedLock(wrapped_file, result);
119}
120
Sergey Ulanov7de7e9f2019-03-09 00:41:02121#endif // !defined(OS_FUCHSIA)
122
shess5f2c3442017-01-24 02:15:10123int FileControl(sqlite3_file* sqlite_file, int op, void* arg)
124{
125 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
126 return wrapped_file->pMethods->xFileControl(wrapped_file, op, arg);
127}
128
129int SectorSize(sqlite3_file* sqlite_file)
130{
131 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
132 return wrapped_file->pMethods->xSectorSize(wrapped_file);
133}
134
135int DeviceCharacteristics(sqlite3_file* sqlite_file)
136{
137 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
138 return wrapped_file->pMethods->xDeviceCharacteristics(wrapped_file);
139}
140
141int ShmMap(sqlite3_file *sqlite_file, int region, int size,
142 int extend, void volatile **pp) {
143 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
144 return wrapped_file->pMethods->xShmMap(
145 wrapped_file, region, size, extend, pp);
146}
147
148int ShmLock(sqlite3_file *sqlite_file, int ofst, int n, int flags) {
149 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
150 return wrapped_file->pMethods->xShmLock(wrapped_file, ofst, n, flags);
151}
152
153void ShmBarrier(sqlite3_file *sqlite_file) {
154 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
155 wrapped_file->pMethods->xShmBarrier(wrapped_file);
156}
157
158int ShmUnmap(sqlite3_file *sqlite_file, int del) {
159 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
160 return wrapped_file->pMethods->xShmUnmap(wrapped_file, del);
161}
162
163int Fetch(sqlite3_file *sqlite_file, sqlite3_int64 off, int amt, void **pp) {
164 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
165 return wrapped_file->pMethods->xFetch(wrapped_file, off, amt, pp);
166}
167
168int Unfetch(sqlite3_file *sqlite_file, sqlite3_int64 off, void *p) {
169 sqlite3_file* wrapped_file = GetWrappedFile(sqlite_file);
170 return wrapped_file->pMethods->xUnfetch(wrapped_file, off, p);
171}
172
shess5f2c3442017-01-24 02:15:10173int Open(sqlite3_vfs* vfs, const char* file_name, sqlite3_file* wrapper_file,
174 int desired_flags, int* used_flags) {
175 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
176
177 sqlite3_file* wrapped_file = static_cast<sqlite3_file*>(
178 sqlite3_malloc(wrapped_vfs->szOsFile));
179 if (!wrapped_file)
180 return SQLITE_NOMEM;
181
182 // NOTE(shess): SQLite's unixOpen() makes assumptions about the structure of
183 // |file_name|. Do not pass a local copy, here, only the passed-in value.
184 int rc = wrapped_vfs->xOpen(wrapped_vfs,
185 file_name, wrapped_file,
186 desired_flags, used_flags);
187 if (rc != SQLITE_OK) {
188 sqlite3_free(wrapped_file);
189 return rc;
190 }
191 // NOTE(shess): Any early exit from here needs to call xClose() on
192 // |wrapped_file|.
193
194#if defined(OS_MACOSX) && !defined(OS_IOS)
195 // When opening journal files, propagate time-machine exclusion from db.
196 static int kJournalFlags =
197 SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL |
198 SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL;
199 if (file_name && (desired_flags & kJournalFlags)) {
200 // https://www.sqlite.org/c3ref/vfs.html indicates that the journal path
Victor Costan7e74dce2019-01-28 20:15:25201 // will have a suffix separated by "-" from the main database file name.
202 base::StringPiece file_name_string_piece(file_name);
203 size_t dash_index = file_name_string_piece.rfind('-');
shess5f2c3442017-01-24 02:15:10204 if (dash_index != base::StringPiece::npos) {
Victor Costan7e74dce2019-01-28 20:15:25205 base::StringPiece db_name(file_name, dash_index);
206 if (base::mac::GetFileBackupExclusion(base::FilePath(db_name))) {
207 base::mac::SetFileBackupExclusion(
208 base::FilePath(file_name_string_piece));
shess5f2c3442017-01-24 02:15:10209 }
210 }
211 }
212#endif
213
Victor Costan86ca4312018-02-07 21:34:10214 // |iVersion| determines what methods SQLite may call on the instance.
215 // Having the methods which can't be proxied return an error may cause SQLite
216 // to operate differently than if it didn't call those methods at all. To be
217 // on the safe side, the wrapper sqlite3_io_methods version perfectly matches
218 // the version of the wrapped files.
shess5f2c3442017-01-24 02:15:10219 //
Victor Costan86ca4312018-02-07 21:34:10220 // At a first glance, it might be tempting to simplify the code by
Sergey Ulanov7de7e9f2019-03-09 00:41:02221 // restricting wrapping support to VFS version 3. However, this might fail on
222 // Mac.
Victor Costan86ca4312018-02-07 21:34:10223 //
224 // On Mac, SQLite built with SQLITE_ENABLE_LOCKING_STYLE ends up using a VFS
225 // that dynamically dispatches between a few variants of sqlite3_io_methods,
226 // based on whether the opened database is on a local or on a remote (AFS,
227 // NFS) filesystem. Some variants return a VFS version 1 structure.
shess5f2c3442017-01-24 02:15:10228 VfsFile* file = AsVfsFile(wrapper_file);
Sergey Ulanov7de7e9f2019-03-09 00:41:02229
230 // Call constructor explicitly since the memory is already allocated.
231 new (file) VfsFile();
232
shess5f2c3442017-01-24 02:15:10233 file->wrapped_file = wrapped_file;
Sergey Ulanov7de7e9f2019-03-09 00:41:02234
235#if defined(OS_FUCHSIA)
236 file->file_name = file_name;
237 file->lock_level = SQLITE_LOCK_NONE;
238#endif
239
shess5f2c3442017-01-24 02:15:10240 if (wrapped_file->pMethods->iVersion == 1) {
241 static const sqlite3_io_methods io_methods = {
242 1,
243 Close,
244 Read,
245 Write,
246 Truncate,
247 Sync,
248 FileSize,
Sergey Ulanov7de7e9f2019-03-09 00:41:02249#if !defined(OS_FUCHSIA)
shess5f2c3442017-01-24 02:15:10250 Lock,
251 Unlock,
252 CheckReservedLock,
Sergey Ulanov7de7e9f2019-03-09 00:41:02253#else
254 FuchsiaVfsLock,
255 FuchsiaVfsUnlock,
256 FuchsiaVfsCheckReservedLock,
257#endif
shess5f2c3442017-01-24 02:15:10258 FileControl,
259 SectorSize,
260 DeviceCharacteristics,
261 };
262 file->methods = &io_methods;
263 } else if (wrapped_file->pMethods->iVersion == 2) {
264 static const sqlite3_io_methods io_methods = {
265 2,
266 Close,
267 Read,
268 Write,
269 Truncate,
270 Sync,
271 FileSize,
Sergey Ulanov7de7e9f2019-03-09 00:41:02272#if !defined(OS_FUCHSIA)
shess5f2c3442017-01-24 02:15:10273 Lock,
274 Unlock,
275 CheckReservedLock,
Sergey Ulanov7de7e9f2019-03-09 00:41:02276#else
277 FuchsiaVfsLock,
278 FuchsiaVfsUnlock,
279 FuchsiaVfsCheckReservedLock,
280#endif
shess5f2c3442017-01-24 02:15:10281 FileControl,
282 SectorSize,
283 DeviceCharacteristics,
284 // Methods above are valid for version 1.
285 ShmMap,
286 ShmLock,
287 ShmBarrier,
288 ShmUnmap,
289 };
290 file->methods = &io_methods;
291 } else {
292 static const sqlite3_io_methods io_methods = {
293 3,
294 Close,
295 Read,
296 Write,
297 Truncate,
298 Sync,
299 FileSize,
Sergey Ulanov7de7e9f2019-03-09 00:41:02300#if !defined(OS_FUCHSIA)
shess5f2c3442017-01-24 02:15:10301 Lock,
302 Unlock,
303 CheckReservedLock,
Sergey Ulanov7de7e9f2019-03-09 00:41:02304#else
305 FuchsiaVfsLock,
306 FuchsiaVfsUnlock,
307 FuchsiaVfsCheckReservedLock,
308#endif
shess5f2c3442017-01-24 02:15:10309 FileControl,
310 SectorSize,
311 DeviceCharacteristics,
312 // Methods above are valid for version 1.
313 ShmMap,
314 ShmLock,
315 ShmBarrier,
316 ShmUnmap,
317 // Methods above are valid for version 2.
318 Fetch,
319 Unfetch,
320 };
321 file->methods = &io_methods;
322 }
323 return SQLITE_OK;
324}
325
326int Delete(sqlite3_vfs* vfs, const char* file_name, int sync_dir) {
327 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
328 return wrapped_vfs->xDelete(wrapped_vfs, file_name, sync_dir);
329}
330
331int Access(sqlite3_vfs* vfs, const char* file_name, int flag, int* res) {
332 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
333 return wrapped_vfs->xAccess(wrapped_vfs, file_name, flag, res);
334}
335
336int FullPathname(sqlite3_vfs* vfs, const char* relative_path,
337 int buf_size, char* absolute_path) {
shess5f2c3442017-01-24 02:15:10338 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
339 return wrapped_vfs->xFullPathname(
340 wrapped_vfs, relative_path, buf_size, absolute_path);
341}
342
shess5f2c3442017-01-24 02:15:10343int Randomness(sqlite3_vfs* vfs, int buf_size, char* buffer) {
344 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
345 return wrapped_vfs->xRandomness(wrapped_vfs, buf_size, buffer);
346}
347
348int Sleep(sqlite3_vfs* vfs, int microseconds) {
349 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
350 return wrapped_vfs->xSleep(wrapped_vfs, microseconds);
351}
352
shess5f2c3442017-01-24 02:15:10353int GetLastError(sqlite3_vfs* vfs, int e, char* s) {
354 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
355 return wrapped_vfs->xGetLastError(wrapped_vfs, e, s);
356}
357
358int CurrentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* now) {
359 sqlite3_vfs* wrapped_vfs = GetWrappedVfs(vfs);
360 return wrapped_vfs->xCurrentTimeInt64(wrapped_vfs, now);
361}
362
shess5f2c3442017-01-24 02:15:10363} // namespace
364
365sqlite3_vfs* VFSWrapper() {
366 const char* kVFSName = "VFSWrapper";
367
368 // Return existing version if already registered.
369 {
370 sqlite3_vfs* vfs = sqlite3_vfs_find(kVFSName);
Victor Costanbd623112018-07-18 04:17:27371 if (vfs)
shess5f2c3442017-01-24 02:15:10372 return vfs;
373 }
374
Sergey Ulanov7de7e9f2019-03-09 00:41:02375 // Get the default VFS on all platforms except Fuchsia.
376 const char* base_vfs_name = nullptr;
377#if defined(OS_FUCHSIA)
378 base_vfs_name = "unix-none";
379#endif
380 sqlite3_vfs* wrapped_vfs = sqlite3_vfs_find(base_vfs_name);
381
382 // Give up if there is no VFS implementation for the current platform.
383 if (!wrapped_vfs) {
384 NOTREACHED();
shess5f2c3442017-01-24 02:15:10385 return nullptr;
Sergey Ulanov7de7e9f2019-03-09 00:41:02386 }
shess5f2c3442017-01-24 02:15:10387
388 std::unique_ptr<sqlite3_vfs, std::function<void(sqlite3_vfs*)>> wrapper_vfs(
389 static_cast<sqlite3_vfs*>(sqlite3_malloc(sizeof(sqlite3_vfs))),
390 [](sqlite3_vfs* v) {
391 sqlite3_free(v);
392 });
393 memset(wrapper_vfs.get(), '\0', sizeof(sqlite3_vfs));
394
395 // VFS implementations should always work with a SQLite that only knows about
396 // earlier versions.
Victor Costanbe2623282019-04-11 21:32:21397 constexpr int kSqliteVfsApiVersion = 3;
398 wrapper_vfs->iVersion = kSqliteVfsApiVersion;
399
400 // All the SQLite VFS implementations used by Chrome should support the
401 // version proxied here.
402 DCHECK_GE(wrapped_vfs->iVersion, kSqliteVfsApiVersion);
shess5f2c3442017-01-24 02:15:10403
404 // Caller of xOpen() allocates this much space.
405 wrapper_vfs->szOsFile = sizeof(VfsFile);
406
407 wrapper_vfs->mxPathname = wrapped_vfs->mxPathname;
Victor Costanbe2623282019-04-11 21:32:21408 wrapper_vfs->pNext = nullptr; // Field used by SQLite.
shess5f2c3442017-01-24 02:15:10409 wrapper_vfs->zName = kVFSName;
410
411 // Keep a reference to the wrapped vfs for use in methods.
412 wrapper_vfs->pAppData = wrapped_vfs;
413
414 // VFS methods.
415 wrapper_vfs->xOpen = &Open;
416 wrapper_vfs->xDelete = &Delete;
417 wrapper_vfs->xAccess = &Access;
418 wrapper_vfs->xFullPathname = &FullPathname;
Victor Costanbe2623282019-04-11 21:32:21419
420 // SQLite's dynamic extension loading is disabled in Chrome. Not proxying
421 // these methods lets us ship less logic and provides a tiny bit of extra
422 // security, as we know for sure that SQLite will not dynamically load code.
423 wrapper_vfs->xDlOpen = nullptr;
424 wrapper_vfs->xDlError = nullptr;
425 wrapper_vfs->xDlSym = nullptr;
426 wrapper_vfs->xDlClose = nullptr;
427
shess5f2c3442017-01-24 02:15:10428 wrapper_vfs->xRandomness = &Randomness;
429 wrapper_vfs->xSleep = &Sleep;
Victor Costanbe2623282019-04-11 21:32:21430
431 // |xCurrentTime| is null when SQLite is built with SQLITE_OMIT_DEPRECATED, so
432 // it does not need to be proxied.
433 wrapper_vfs->xCurrentTime = nullptr;
434
shess5f2c3442017-01-24 02:15:10435 wrapper_vfs->xGetLastError = &GetLastError;
Victor Costanbe2623282019-04-11 21:32:21436
437 // The methods above are in version 1 of SQLite's VFS API.
438
439 DCHECK(wrapped_vfs->xCurrentTimeInt64 != nullptr);
Victor Costan86ca4312018-02-07 21:34:10440 wrapper_vfs->xCurrentTimeInt64 = &CurrentTimeInt64;
Victor Costanbe2623282019-04-11 21:32:21441
442 // The methods above are in version 2 of SQLite's VFS API.
443
444 // The VFS system call interception API is intended for very low-level SQLite
445 // testing and tweaks. Proxying these methods is not necessary because Chrome
446 // does not do very low-level SQLite testing, and the VFS wrapper supports all
447 // the needed tweaks.
448 wrapper_vfs->xSetSystemCall = nullptr;
449 wrapper_vfs->xGetSystemCall = nullptr;
450 wrapper_vfs->xNextSystemCall = nullptr;
451
shess5f2c3442017-01-24 02:15:10452 // The methods above are in version 3 of sqlite_vfs.
453
454 if (SQLITE_OK == sqlite3_vfs_register(wrapper_vfs.get(), 0)) {
455 ANNOTATE_LEAKING_OBJECT_PTR(wrapper_vfs.get());
456 wrapper_vfs.release();
457 }
458
459 return sqlite3_vfs_find(kVFSName);
460}
461
462} // namespace sql