1 /*
2 ** 2005 November 29
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This file contains OS interface code that is common to all
14 ** architectures.
15 */
16 #include "sqliteInt.h"
17 
18 /*
19 ** If we compile with the SQLITE_TEST macro set, then the following block
20 ** of code will give us the ability to simulate a disk I/O error.  This
21 ** is used for testing the I/O recovery logic.
22 */
23 #if defined(SQLITE_TEST)
24 int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
25 int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
26 int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
27 int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
28 int sqlite3_io_error_benign = 0;         /* True if errors are benign */
29 int sqlite3_diskfull_pending = 0;
30 int sqlite3_diskfull = 0;
31 #endif /* defined(SQLITE_TEST) */
32 
33 /*
34 ** When testing, also keep a count of the number of open files.
35 */
36 #if defined(SQLITE_TEST)
37 int sqlite3_open_file_count = 0;
38 #endif /* defined(SQLITE_TEST) */
39 
40 /*
41 ** The default SQLite sqlite3_vfs implementations do not allocate
42 ** memory (actually, os_unix.c allocates a small amount of memory
43 ** from within OsOpen()), but some third-party implementations may.
44 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
45 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
46 **
47 ** The following functions are instrumented for malloc() failure
48 ** testing:
49 **
50 **     sqlite3OsRead()
51 **     sqlite3OsWrite()
52 **     sqlite3OsSync()
53 **     sqlite3OsFileSize()
54 **     sqlite3OsLock()
55 **     sqlite3OsCheckReservedLock()
56 **     sqlite3OsFileControl()
57 **     sqlite3OsShmMap()
58 **     sqlite3OsOpen()
59 **     sqlite3OsDelete()
60 **     sqlite3OsAccess()
61 **     sqlite3OsFullPathname()
62 **
63 */
64 #if defined(SQLITE_TEST)
65 int sqlite3_memdebug_vfs_oom_test = 1;
66   #define DO_OS_MALLOC_TEST(x)                                       \
67   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
68     void *pTstAlloc = sqlite3Malloc(10);                             \
69     if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
70     sqlite3_free(pTstAlloc);                                         \
71   }
72 #else
73   #define DO_OS_MALLOC_TEST(x)
74 #endif
75 
76 /*
77 ** The following routines are convenience wrappers around methods
78 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
79 ** of this would be completely automatic if SQLite were coded using
80 ** C++ instead of plain old C.
81 */
sqlite3OsClose(sqlite3_file * pId)82 void sqlite3OsClose(sqlite3_file *pId){
83   if( pId->pMethods ){
84     pId->pMethods->xClose(pId);
85     pId->pMethods = 0;
86   }
87 }
sqlite3OsRead(sqlite3_file * id,void * pBuf,int amt,i64 offset)88 int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
89   DO_OS_MALLOC_TEST(id);
90   return id->pMethods->xRead(id, pBuf, amt, offset);
91 }
sqlite3OsWrite(sqlite3_file * id,const void * pBuf,int amt,i64 offset)92 int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
93   DO_OS_MALLOC_TEST(id);
94   return id->pMethods->xWrite(id, pBuf, amt, offset);
95 }
sqlite3OsTruncate(sqlite3_file * id,i64 size)96 int sqlite3OsTruncate(sqlite3_file *id, i64 size){
97   return id->pMethods->xTruncate(id, size);
98 }
sqlite3OsSync(sqlite3_file * id,int flags)99 int sqlite3OsSync(sqlite3_file *id, int flags){
100   DO_OS_MALLOC_TEST(id);
101   return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
102 }
sqlite3OsFileSize(sqlite3_file * id,i64 * pSize)103 int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
104   DO_OS_MALLOC_TEST(id);
105   return id->pMethods->xFileSize(id, pSize);
106 }
sqlite3OsLock(sqlite3_file * id,int lockType)107 int sqlite3OsLock(sqlite3_file *id, int lockType){
108   DO_OS_MALLOC_TEST(id);
109   return id->pMethods->xLock(id, lockType);
110 }
sqlite3OsUnlock(sqlite3_file * id,int lockType)111 int sqlite3OsUnlock(sqlite3_file *id, int lockType){
112   return id->pMethods->xUnlock(id, lockType);
113 }
sqlite3OsCheckReservedLock(sqlite3_file * id,int * pResOut)114 int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
115   DO_OS_MALLOC_TEST(id);
116   return id->pMethods->xCheckReservedLock(id, pResOut);
117 }
118 
119 /*
120 ** Use sqlite3OsFileControl() when we are doing something that might fail
121 ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
122 ** when simply tossing information over the wall to the VFS and we do not
123 ** really care if the VFS receives and understands the information since it
124 ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
125 ** routine has no return value since the return value would be meaningless.
126 */
sqlite3OsFileControl(sqlite3_file * id,int op,void * pArg)127 int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
128   if( id->pMethods==0 ) return SQLITE_NOTFOUND;
129 #ifdef SQLITE_TEST
130   if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
131    && op!=SQLITE_FCNTL_LOCK_TIMEOUT
132    && op!=SQLITE_FCNTL_CKPT_DONE
133    && op!=SQLITE_FCNTL_CKPT_START
134   ){
135     /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
136     ** is using a regular VFS, it is called after the corresponding
137     ** transaction has been committed. Injecting a fault at this point
138     ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
139     ** but the transaction is committed anyway.
140     **
141     ** The core must call OsFileControl() though, not OsFileControlHint(),
142     ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
143     ** means the commit really has failed and an error should be returned
144     ** to the user.
145     **
146     ** The CKPT_DONE and CKPT_START file-controls are write-only signals
147     ** to the cksumvfs.  Their return code is meaningless and is ignored
148     ** by the SQLite core, so there is no point in simulating OOMs for them.
149     */
150     DO_OS_MALLOC_TEST(id);
151   }
152 #endif
153   return id->pMethods->xFileControl(id, op, pArg);
154 }
sqlite3OsFileControlHint(sqlite3_file * id,int op,void * pArg)155 void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
156   if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
157 }
158 
sqlite3OsSectorSize(sqlite3_file * id)159 int sqlite3OsSectorSize(sqlite3_file *id){
160   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
161   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
162 }
sqlite3OsDeviceCharacteristics(sqlite3_file * id)163 int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
164   return id->pMethods->xDeviceCharacteristics(id);
165 }
166 #ifndef SQLITE_OMIT_WAL
sqlite3OsShmLock(sqlite3_file * id,int offset,int n,int flags)167 int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
168   return id->pMethods->xShmLock(id, offset, n, flags);
169 }
sqlite3OsShmBarrier(sqlite3_file * id)170 void sqlite3OsShmBarrier(sqlite3_file *id){
171   id->pMethods->xShmBarrier(id);
172 }
sqlite3OsShmUnmap(sqlite3_file * id,int deleteFlag)173 int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
174   return id->pMethods->xShmUnmap(id, deleteFlag);
175 }
sqlite3OsShmMap(sqlite3_file * id,int iPage,int pgsz,int bExtend,void volatile ** pp)176 int sqlite3OsShmMap(
177   sqlite3_file *id,               /* Database file handle */
178   int iPage,
179   int pgsz,
180   int bExtend,                    /* True to extend file if necessary */
181   void volatile **pp              /* OUT: Pointer to mapping */
182 ){
183   DO_OS_MALLOC_TEST(id);
184   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
185 }
186 #endif /* SQLITE_OMIT_WAL */
187 
188 #if SQLITE_MAX_MMAP_SIZE>0
189 /* The real implementation of xFetch and xUnfetch */
sqlite3OsFetch(sqlite3_file * id,i64 iOff,int iAmt,void ** pp)190 int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
191   DO_OS_MALLOC_TEST(id);
192   return id->pMethods->xFetch(id, iOff, iAmt, pp);
193 }
sqlite3OsUnfetch(sqlite3_file * id,i64 iOff,void * p)194 int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
195   return id->pMethods->xUnfetch(id, iOff, p);
196 }
197 #else
198 /* No-op stubs to use when memory-mapped I/O is disabled */
sqlite3OsFetch(sqlite3_file * id,i64 iOff,int iAmt,void ** pp)199 int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
200   *pp = 0;
201   return SQLITE_OK;
202 }
sqlite3OsUnfetch(sqlite3_file * id,i64 iOff,void * p)203 int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
204   return SQLITE_OK;
205 }
206 #endif
207 
208 /*
209 ** The next group of routines are convenience wrappers around the
210 ** VFS methods.
211 */
sqlite3OsOpen(sqlite3_vfs * pVfs,const char * zPath,sqlite3_file * pFile,int flags,int * pFlagsOut)212 int sqlite3OsOpen(
213   sqlite3_vfs *pVfs,
214   const char *zPath,
215   sqlite3_file *pFile,
216   int flags,
217   int *pFlagsOut
218 ){
219   int rc;
220   DO_OS_MALLOC_TEST(0);
221   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
222   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
223   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
224   ** reaching the VFS. */
225   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
226   assert( rc==SQLITE_OK || pFile->pMethods==0 );
227   return rc;
228 }
sqlite3OsDelete(sqlite3_vfs * pVfs,const char * zPath,int dirSync)229 int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
230   DO_OS_MALLOC_TEST(0);
231   assert( dirSync==0 || dirSync==1 );
232   return pVfs->xDelete(pVfs, zPath, dirSync);
233 }
sqlite3OsAccess(sqlite3_vfs * pVfs,const char * zPath,int flags,int * pResOut)234 int sqlite3OsAccess(
235   sqlite3_vfs *pVfs,
236   const char *zPath,
237   int flags,
238   int *pResOut
239 ){
240   DO_OS_MALLOC_TEST(0);
241   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
242 }
sqlite3OsFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nPathOut,char * zPathOut)243 int sqlite3OsFullPathname(
244   sqlite3_vfs *pVfs,
245   const char *zPath,
246   int nPathOut,
247   char *zPathOut
248 ){
249   DO_OS_MALLOC_TEST(0);
250   zPathOut[0] = 0;
251   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
252 }
253 #ifndef SQLITE_OMIT_LOAD_EXTENSION
sqlite3OsDlOpen(sqlite3_vfs * pVfs,const char * zPath)254 void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
255   return pVfs->xDlOpen(pVfs, zPath);
256 }
sqlite3OsDlError(sqlite3_vfs * pVfs,int nByte,char * zBufOut)257 void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
258   pVfs->xDlError(pVfs, nByte, zBufOut);
259 }
sqlite3OsDlSym(sqlite3_vfs * pVfs,void * pHdle,const char * zSym)260 void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
261   return pVfs->xDlSym(pVfs, pHdle, zSym);
262 }
sqlite3OsDlClose(sqlite3_vfs * pVfs,void * pHandle)263 void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
264   pVfs->xDlClose(pVfs, pHandle);
265 }
266 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
sqlite3OsRandomness(sqlite3_vfs * pVfs,int nByte,char * zBufOut)267 int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
268   if( sqlite3Config.iPrngSeed ){
269     memset(zBufOut, 0, nByte);
270     if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
271     memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
272     return SQLITE_OK;
273   }else{
274     return pVfs->xRandomness(pVfs, nByte, zBufOut);
275   }
276 
277 }
sqlite3OsSleep(sqlite3_vfs * pVfs,int nMicro)278 int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
279   return pVfs->xSleep(pVfs, nMicro);
280 }
sqlite3OsGetLastError(sqlite3_vfs * pVfs)281 int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
282   return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
283 }
sqlite3OsCurrentTimeInt64(sqlite3_vfs * pVfs,sqlite3_int64 * pTimeOut)284 int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
285   int rc;
286   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
287   ** method to get the current date and time if that method is available
288   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
289   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
290   ** unavailable.
291   */
292   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
293     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
294   }else{
295     double r;
296     rc = pVfs->xCurrentTime(pVfs, &r);
297     *pTimeOut = (sqlite3_int64)(r*86400000.0);
298   }
299   return rc;
300 }
301 
sqlite3OsOpenMalloc(sqlite3_vfs * pVfs,const char * zFile,sqlite3_file ** ppFile,int flags,int * pOutFlags)302 int sqlite3OsOpenMalloc(
303   sqlite3_vfs *pVfs,
304   const char *zFile,
305   sqlite3_file **ppFile,
306   int flags,
307   int *pOutFlags
308 ){
309   int rc;
310   sqlite3_file *pFile;
311   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
312   if( pFile ){
313     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
314     if( rc!=SQLITE_OK ){
315       sqlite3_free(pFile);
316     }else{
317       *ppFile = pFile;
318     }
319   }else{
320     rc = SQLITE_NOMEM_BKPT;
321   }
322   return rc;
323 }
sqlite3OsCloseFree(sqlite3_file * pFile)324 void sqlite3OsCloseFree(sqlite3_file *pFile){
325   assert( pFile );
326   sqlite3OsClose(pFile);
327   sqlite3_free(pFile);
328 }
329 
330 /*
331 ** This function is a wrapper around the OS specific implementation of
332 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
333 ** ability to simulate a malloc failure, so that the handling of an
334 ** error in sqlite3_os_init() by the upper layers can be tested.
335 */
sqlite3OsInit(void)336 int sqlite3OsInit(void){
337   void *p = sqlite3_malloc(10);
338   if( p==0 ) return SQLITE_NOMEM_BKPT;
339   sqlite3_free(p);
340   return sqlite3_os_init();
341 }
342 
343 /*
344 ** The list of all registered VFS implementations.
345 */
346 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
347 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
348 
349 /*
350 ** Locate a VFS by name.  If no name is given, simply return the
351 ** first VFS on the list.
352 */
sqlite3_vfs_find(const char * zVfs)353 sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
354   sqlite3_vfs *pVfs = 0;
355 #if SQLITE_THREADSAFE
356   sqlite3_mutex *mutex;
357 #endif
358 #ifndef SQLITE_OMIT_AUTOINIT
359   int rc = sqlite3_initialize();
360   if( rc ) return 0;
361 #endif
362 #if SQLITE_THREADSAFE
363   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
364 #endif
365   sqlite3_mutex_enter(mutex);
366   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
367     if( zVfs==0 ) break;
368     if( strcmp(zVfs, pVfs->zName)==0 ) break;
369   }
370   sqlite3_mutex_leave(mutex);
371   return pVfs;
372 }
373 
374 /*
375 ** Unlink a VFS from the linked list
376 */
vfsUnlink(sqlite3_vfs * pVfs)377 static void vfsUnlink(sqlite3_vfs *pVfs){
378   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) );
379   if( pVfs==0 ){
380     /* No-op */
381   }else if( vfsList==pVfs ){
382     vfsList = pVfs->pNext;
383   }else if( vfsList ){
384     sqlite3_vfs *p = vfsList;
385     while( p->pNext && p->pNext!=pVfs ){
386       p = p->pNext;
387     }
388     if( p->pNext==pVfs ){
389       p->pNext = pVfs->pNext;
390     }
391   }
392 }
393 
394 /*
395 ** Register a VFS with the system.  It is harmless to register the same
396 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
397 ** true.
398 */
sqlite3_vfs_register(sqlite3_vfs * pVfs,int makeDflt)399 int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
400   MUTEX_LOGIC(sqlite3_mutex *mutex;)
401 #ifndef SQLITE_OMIT_AUTOINIT
402   int rc = sqlite3_initialize();
403   if( rc ) return rc;
404 #endif
405 #ifdef SQLITE_ENABLE_API_ARMOR
406   if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
407 #endif
408 
409   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
410   sqlite3_mutex_enter(mutex);
411   vfsUnlink(pVfs);
412   if( makeDflt || vfsList==0 ){
413     pVfs->pNext = vfsList;
414     vfsList = pVfs;
415   }else{
416     pVfs->pNext = vfsList->pNext;
417     vfsList->pNext = pVfs;
418   }
419   assert(vfsList);
420   sqlite3_mutex_leave(mutex);
421   return SQLITE_OK;
422 }
423 
424 /*
425 ** Unregister a VFS so that it is no longer accessible.
426 */
sqlite3_vfs_unregister(sqlite3_vfs * pVfs)427 int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
428   MUTEX_LOGIC(sqlite3_mutex *mutex;)
429 #ifndef SQLITE_OMIT_AUTOINIT
430   int rc = sqlite3_initialize();
431   if( rc ) return rc;
432 #endif
433   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
434   sqlite3_mutex_enter(mutex);
435   vfsUnlink(pVfs);
436   sqlite3_mutex_leave(mutex);
437   return SQLITE_OK;
438 }
439