Avi Drissman | 69b874f | 2022-09-15 19:11:14 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 5 | #ifndef SQL_SANDBOXED_VFS_FILE_H_ |
| 6 | #define SQL_SANDBOXED_VFS_FILE_H_ |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 7 | |
| 8 | #include "base/files/file.h" |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 10 | #include "base/memory/raw_ptr.h" |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 11 | #include "base/memory/raw_ptr_exclusion.h" |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 12 | #include "third_party/sqlite/sqlite3.h" |
| 13 | |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 14 | namespace sql { |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 15 | |
Victor Costan | 862ec72 | 2022-02-07 23:08:53 | [diff] [blame] | 16 | // The file types associated with a SQLite database. |
| 17 | enum class SandboxedVfsFileType { |
| 18 | // The main file, which stores the database pages. |
| 19 | kDatabase, |
| 20 | // The transaction rollback journal file. Used when WAL is off. |
| 21 | // This file has the same path as the database, plus the "-journal" suffix. |
| 22 | kJournal, |
| 23 | // The Write-Ahead Log (WAL) file. |
| 24 | // This file has the same path as the database, plus the "-wal" suffix. |
| 25 | kWal, |
| 26 | }; |
| 27 | |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 28 | class SandboxedVfs; |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 29 | |
Victor Costan | 959eab0 | 2020-08-17 18:33:56 | [diff] [blame] | 30 | // SQLite VFS file implementation that works in a sandboxed process. |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 31 | // |
| 32 | // An instance is created when SQLite calls into SandboxedVfs::Open(). The |
| 33 | // instance is deleted by a call to SandboxedVfsFile::Close(). |
| 34 | // |
| 35 | // The SQLite VFS API includes a complex locking strategy documented in |
| 36 | // https://www.sqlite.org/lockingv3.html |
| 37 | // |
| 38 | // This implementation uses a simplified locking strategy, where we grab an |
| 39 | // exclusive lock when entering any of the modes that prepare for a transition |
| 40 | // to EXCLUSIVE. (These modes are RESERVED and PENDING). This approach is easy |
| 41 | // to implement on top of base::File's locking primitives, at the cost of some |
| 42 | // false contention, which makes us slower under high concurrency. |
| 43 | // |
| 44 | // SQLite's built-in VFSes use the OS support for locking a range of bytes in |
| 45 | // the file, rather locking than the whole file. |
| 46 | class SandboxedVfsFile { |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 47 | public: |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 48 | // Creates an instance in the given buffer. Note that `vfs` MUST outlive the |
| 49 | // returned sqlite3_file object. |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 50 | static void Create(base::File file, |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 51 | base::FilePath file_path, |
Victor Costan | 862ec72 | 2022-02-07 23:08:53 | [diff] [blame] | 52 | #if DCHECK_IS_ON() |
| 53 | SandboxedVfsFileType file_type, |
| 54 | #endif // DCHECK_IS_ON() |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 55 | SandboxedVfs* vfs, |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 56 | sqlite3_file& buffer); |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 57 | |
| 58 | // Extracts the instance bridged to the given SQLite VFS file. |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 59 | static SandboxedVfsFile& FromSqliteFile(sqlite3_file& sqlite_file); |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 60 | |
| 61 | // sqlite3_file implementation. |
| 62 | int Close(); |
| 63 | int Read(void* buffer, int size, sqlite3_int64 offset); |
| 64 | int Write(const void* buffer, int size, sqlite3_int64 offset); |
| 65 | int Truncate(sqlite3_int64 size); |
| 66 | int Sync(int flags); |
| 67 | int FileSize(sqlite3_int64* result_size); |
| 68 | int Lock(int mode); |
| 69 | int Unlock(int mode); |
| 70 | int CheckReservedLock(int* has_reserved_lock); |
| 71 | int FileControl(int opcode, void* data); |
| 72 | int SectorSize(); |
| 73 | int DeviceCharacteristics(); |
| 74 | int ShmMap(int page_index, |
| 75 | int page_size, |
| 76 | int extend_file_if_needed, |
| 77 | void volatile** result); |
| 78 | int ShmLock(int offset, int size, int flags); |
| 79 | void ShmBarrier(); |
| 80 | int ShmUnmap(int also_delete_file); |
| 81 | int Fetch(sqlite3_int64 offset, int size, void** result); |
| 82 | int Unfetch(sqlite3_int64 offset, void* fetch_result); |
| 83 | |
| 84 | private: |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 85 | SandboxedVfsFile(base::File file, |
| 86 | base::FilePath file_path, |
Victor Costan | 862ec72 | 2022-02-07 23:08:53 | [diff] [blame] | 87 | #if DCHECK_IS_ON() |
| 88 | SandboxedVfsFileType file_type, |
| 89 | #endif // DCHECK_IS_ON() |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 90 | SandboxedVfs* vfs); |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 91 | ~SandboxedVfsFile(); |
| 92 | |
| 93 | // Constructed from a file handle passed from the browser process. |
| 94 | base::File file_; |
| 95 | // One of the SQLite locking mode constants. |
| 96 | int sqlite_lock_mode_; |
| 97 | // The SandboxedVfs that created this instance. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 98 | const raw_ptr<SandboxedVfs> vfs_; |
Victor Costan | 862ec72 | 2022-02-07 23:08:53 | [diff] [blame] | 99 | #if DCHECK_IS_ON() |
| 100 | // Tracked to check assumptions about SQLite's locking protocol. |
| 101 | const SandboxedVfsFileType file_type_; |
| 102 | #endif // DCHECK_IS_ON() |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 103 | // Used to identify the file in IPCs to the browser process. |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 104 | const base::FilePath file_path_; |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // sqlite3_file "subclass" that bridges to a SandboxedVfsFile instance. |
| 108 | struct SandboxedVfsFileSqliteBridge { |
| 109 | sqlite3_file sqlite_file; |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 110 | // `sandboxed_vfs_file` is not a raw_ptr<SandboxedVfsFile>, because |
| 111 | // reinterpret_cast of uninitialized memory to raw_ptr can cause ref-counting |
| 112 | // mismatch. |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 113 | RAW_PTR_EXCLUSION SandboxedVfsFile* sandboxed_vfs_file; |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 114 | |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 115 | static SandboxedVfsFileSqliteBridge& FromSqliteFile( |
| 116 | sqlite3_file& sqlite_file); |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 117 | }; |
| 118 | |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 119 | } // namespace sql |
Victor Costan | 451bb9cf | 2019-03-16 04:48:07 | [diff] [blame] | 120 | |
Ken Rockot | 7655fcb6 | 2020-08-12 19:22:40 | [diff] [blame] | 121 | #endif // SQL_SANDBOXED_VFS_FILE_H_ |