xref: /sqlite-3.40.0/src/test_syscall.c (revision 0fd7d860)
1 /*
2 ** 2011 March 28
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 ** The code in this file implements a Tcl interface used to test error
14 ** handling in the os_unix.c module. Wrapper functions that support fault
15 ** injection are registered as the low-level OS functions using the
16 ** xSetSystemCall() method of the VFS. The Tcl interface is as follows:
17 **
18 **
19 **   test_syscall install LIST
20 **     Install wrapper functions for all system calls in argument LIST.
21 **     LIST must be a list consisting of zero or more of the following
22 **     literal values:
23 **
24 **         open        close      access   getcwd   stat      fstat
25 **         ftruncate   fcntl      read     pread    pread64   write
26 **         pwrite      pwrite64   fchmod   fallocate
27 **
28 **   test_syscall uninstall
29 **     Uninstall all wrapper functions.
30 **
31 **   test_syscall fault ?COUNT PERSIST?
32 **     If [test_syscall fault] is invoked without the two arguments, fault
33 **     injection is disabled. Otherwise, fault injection is configured to
34 **     cause a failure on the COUNT'th next call to a system call with a
35 **     wrapper function installed. A COUNT value of 1 means fail the next
36 **     system call.
37 **
38 **     Argument PERSIST is interpreted as a boolean. If true, the all
39 **     system calls following the initial failure also fail. Otherwise, only
40 **     the single transient failure is injected.
41 **
42 **   test_syscall errno CALL ERRNO
43 **     Set the value that the global "errno" is set to following a fault
44 **     in call CALL. Argument CALL must be one of the system call names
45 **     listed above (under [test_syscall install]). ERRNO is a symbolic
46 **     name (i.e. "EACCES"). Not all errno codes are supported. Add extra
47 **     to the aErrno table in function test_syscall_errno() below as
48 **     required.
49 **
50 **   test_syscall reset ?SYSTEM-CALL?
51 **     With no argument, this is an alias for the [uninstall] command. However,
52 **     this command uses a VFS call of the form:
53 **
54 **       xSetSystemCall(pVfs, 0, 0);
55 **
56 **     To restore the default system calls. The [uninstall] command restores
57 **     each system call individually by calling (i.e.):
58 **
59 **       xSetSystemCall(pVfs, "open", 0);
60 **
61 **     With an argument, this command attempts to reset the system call named
62 **     by the parameter using the same method as [uninstall].
63 **
64 **   test_syscall exists SYSTEM-CALL
65 **     Return true if the named system call exists. Or false otherwise.
66 **
67 **   test_syscall list
68 **     Return a list of all system calls. The list is constructed using
69 **     the xNextSystemCall() VFS method.
70 */
71 
72 #include "sqlite3.h"
73 #include "tcl.h"
74 #include <stdlib.h>
75 #include <string.h>
76 #include <assert.h>
77 
78 #ifdef SQLITE_OS_UNIX
79 
80 /* From test1.c */
81 extern const char *sqlite3TestErrorName(int);
82 
83 #include <sys/types.h>
84 #include <errno.h>
85 
86 static struct TestSyscallGlobal {
87   int bPersist;                   /* 1 for persistent errors, 0 for transient */
88   int nCount;                     /* Fail after this many more calls */
89   int nFail;                      /* Number of failures that have occurred */
90 } gSyscall = { 0, 0 };
91 
92 static int ts_open(const char *, int, int);
93 static int ts_close(int fd);
94 static int ts_access(const char *zPath, int mode);
95 static char *ts_getcwd(char *zPath, size_t nPath);
96 static int ts_stat(const char *zPath, struct stat *p);
97 static int ts_fstat(int fd, struct stat *p);
98 static int ts_ftruncate(int fd, off_t n);
99 static int ts_fcntl(int fd, int cmd, ... );
100 static int ts_read(int fd, void *aBuf, size_t nBuf);
101 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
102 static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off);
103 static int ts_write(int fd, const void *aBuf, size_t nBuf);
104 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
105 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
106 static int ts_fchmod(int fd, mode_t mode);
107 static int ts_fallocate(int fd, off_t off, off_t len);
108 
109 
110 struct TestSyscallArray {
111   const char *zName;
112   sqlite3_syscall_ptr xTest;
113   sqlite3_syscall_ptr xOrig;
114   int default_errno;              /* Default value for errno following errors */
115   int custom_errno;               /* Current value for errno if error */
116 } aSyscall[] = {
117   /*  0 */ { "open",      (sqlite3_syscall_ptr)ts_open,      0, EACCES, 0 },
118   /*  1 */ { "close",     (sqlite3_syscall_ptr)ts_close,     0, 0, 0 },
119   /*  2 */ { "access",    (sqlite3_syscall_ptr)ts_access,    0, 0, 0 },
120   /*  3 */ { "getcwd",    (sqlite3_syscall_ptr)ts_getcwd,    0, 0, 0 },
121   /*  4 */ { "stat",      (sqlite3_syscall_ptr)ts_stat,      0, 0, 0 },
122   /*  5 */ { "fstat",     (sqlite3_syscall_ptr)ts_fstat,     0, 0, 0 },
123   /*  6 */ { "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, 0, 0 },
124   /*  7 */ { "fcntl",     (sqlite3_syscall_ptr)ts_fcntl,     0, 0, 0 },
125   /*  8 */ { "read",      (sqlite3_syscall_ptr)ts_read,      0, 0, 0 },
126   /*  9 */ { "pread",     (sqlite3_syscall_ptr)ts_pread,     0, 0, 0 },
127   /* 10 */ { "pread64",   (sqlite3_syscall_ptr)ts_pread64,   0, 0, 0 },
128   /* 11 */ { "write",     (sqlite3_syscall_ptr)ts_write,     0, 0, 0 },
129   /* 12 */ { "pwrite",    (sqlite3_syscall_ptr)ts_pwrite,    0, 0, 0 },
130   /* 13 */ { "pwrite64",  (sqlite3_syscall_ptr)ts_pwrite64,  0, 0, 0 },
131   /* 14 */ { "fchmod",    (sqlite3_syscall_ptr)ts_fchmod,    0, 0, 0 },
132   /* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
133            { 0, 0, 0, 0, 0 }
134 };
135 
136 #define orig_open      ((int(*)(const char *, int, int))aSyscall[0].xOrig)
137 #define orig_close     ((int(*)(int))aSyscall[1].xOrig)
138 #define orig_access    ((int(*)(const char*,int))aSyscall[2].xOrig)
139 #define orig_getcwd    ((char*(*)(char*,size_t))aSyscall[3].xOrig)
140 #define orig_stat      ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
141 #define orig_fstat     ((int(*)(int,struct stat*))aSyscall[5].xOrig)
142 #define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
143 #define orig_fcntl     ((int(*)(int,int,...))aSyscall[7].xOrig)
144 #define orig_read      ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
145 #define orig_pread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
146 #define orig_pread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].xOrig)
147 #define orig_write     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
148 #define orig_pwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
149                        aSyscall[12].xOrig)
150 #define orig_pwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
151                        aSyscall[13].xOrig)
152 #define orig_fchmod    ((int(*)(int,mode_t))aSyscall[14].xOrig)
153 #define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
154 
155 
156 /*
157 ** This function is called exactly once from within each invocation of a
158 ** system call wrapper in this file. It returns 1 if the function should
159 ** fail, or 0 if it should succeed.
160 */
161 static int tsIsFail(void){
162   gSyscall.nCount--;
163   if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
164     gSyscall.nFail++;
165     return 1;
166   }
167   return 0;
168 }
169 
170 /*
171 ** Return the current error-number value for function zFunc. zFunc must be
172 ** the name of a system call in the aSyscall[] table.
173 **
174 ** Usually, the current error-number is the value that errno should be set
175 ** to if the named system call fails. The exception is "fallocate". See
176 ** comments above the implementation of ts_fallocate() for details.
177 */
178 static int tsErrno(const char *zFunc){
179   int i;
180   int nFunc = strlen(zFunc);
181   for(i=0; aSyscall[i].zName; i++){
182     if( strlen(aSyscall[i].zName)!=nFunc ) continue;
183     if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
184     return aSyscall[i].custom_errno;
185   }
186 
187   assert(0);
188   return 0;
189 }
190 
191 /*
192 ** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the
193 ** value of errno before returning.
194 */
195 static int tsIsFailErrno(const char *zFunc){
196   if( tsIsFail() ){
197     errno = tsErrno(zFunc);
198     return 1;
199   }
200   return 0;
201 }
202 
203 /*
204 ** A wrapper around open().
205 */
206 static int ts_open(const char *zFile, int flags, int mode){
207   if( tsIsFailErrno("open") ){
208     return -1;
209   }
210   return orig_open(zFile, flags, mode);
211 }
212 
213 /*
214 ** A wrapper around close().
215 */
216 static int ts_close(int fd){
217   if( tsIsFail() ){
218     return -1;
219   }
220   return orig_close(fd);
221 }
222 
223 /*
224 ** A wrapper around access().
225 */
226 static int ts_access(const char *zPath, int mode){
227   if( tsIsFail() ){
228     return -1;
229   }
230   return orig_access(zPath, mode);
231 }
232 
233 /*
234 ** A wrapper around getcwd().
235 */
236 static char *ts_getcwd(char *zPath, size_t nPath){
237   if( tsIsFail() ){
238     return NULL;
239   }
240   return orig_getcwd(zPath, nPath);
241 }
242 
243 /*
244 ** A wrapper around stat().
245 */
246 static int ts_stat(const char *zPath, struct stat *p){
247   if( tsIsFail() ){
248     return -1;
249   }
250   return orig_stat(zPath, p);
251 }
252 
253 /*
254 ** A wrapper around fstat().
255 */
256 static int ts_fstat(int fd, struct stat *p){
257   if( tsIsFail() ){
258     return -1;
259   }
260   return orig_fstat(fd, p);
261 }
262 
263 /*
264 ** A wrapper around ftruncate().
265 */
266 static int ts_ftruncate(int fd, off_t n){
267   if( tsIsFail() ){
268     return -1;
269   }
270   return orig_ftruncate(fd, n);
271 }
272 
273 /*
274 ** A wrapper around fcntl().
275 */
276 static int ts_fcntl(int fd, int cmd, ... ){
277   va_list ap;
278   void *pArg;
279   if( tsIsFail() ){
280     return -1;
281   }
282   va_start(ap, cmd);
283   pArg = va_arg(ap, void *);
284   return orig_fcntl(fd, cmd, pArg);
285 }
286 
287 /*
288 ** A wrapper around read().
289 */
290 static int ts_read(int fd, void *aBuf, size_t nBuf){
291   if( tsIsFail() ){
292     return -1;
293   }
294   return orig_read(fd, aBuf, nBuf);
295 }
296 
297 /*
298 ** A wrapper around pread().
299 */
300 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
301   if( tsIsFail() ){
302     return -1;
303   }
304   return orig_pread(fd, aBuf, nBuf, off);
305 }
306 
307 /*
308 ** A wrapper around pread64().
309 */
310 static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){
311   if( tsIsFail() ){
312     return -1;
313   }
314   return orig_pread64(fd, aBuf, nBuf, off);
315 }
316 
317 /*
318 ** A wrapper around write().
319 */
320 static int ts_write(int fd, const void *aBuf, size_t nBuf){
321   if( tsIsFail() ){
322     return -1;
323   }
324   return orig_write(fd, aBuf, nBuf);
325 }
326 
327 /*
328 ** A wrapper around pwrite().
329 */
330 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
331   if( tsIsFail() ){
332     return -1;
333   }
334   return orig_pwrite(fd, aBuf, nBuf, off);
335 }
336 
337 /*
338 ** A wrapper around pwrite64().
339 */
340 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){
341   if( tsIsFail() ){
342     return -1;
343   }
344   return orig_pwrite64(fd, aBuf, nBuf, off);
345 }
346 
347 /*
348 ** A wrapper around fchmod().
349 */
350 static int ts_fchmod(int fd, mode_t mode){
351   if( tsIsFail() ){
352     return -1;
353   }
354   return orig_fchmod(fd, mode);
355 }
356 
357 /*
358 ** A wrapper around fallocate().
359 **
360 ** SQLite assumes that the fallocate() function is compatible with
361 ** posix_fallocate(). According to the Linux man page (2009-09-30):
362 **
363 **   posix_fallocate() returns  zero on success, or an error number on
364 **   failure. Note that errno is not set.
365 */
366 static int ts_fallocate(int fd, off_t off, off_t len){
367   if( tsIsFail() ){
368     return tsErrno("fallocate");
369   }
370   return orig_fallocate(fd, off, len);
371 }
372 
373 static int test_syscall_install(
374   void * clientData,
375   Tcl_Interp *interp,
376   int objc,
377   Tcl_Obj *CONST objv[]
378 ){
379   sqlite3_vfs *pVfs;
380   int nElem;
381   int i;
382   Tcl_Obj **apElem;
383 
384   if( objc!=3 ){
385     Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
386     return TCL_ERROR;
387   }
388   if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
389     return TCL_ERROR;
390   }
391   pVfs = sqlite3_vfs_find(0);
392 
393   for(i=0; i<nElem; i++){
394     int iCall;
395     int rc = Tcl_GetIndexFromObjStruct(interp,
396         apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
397     );
398     if( rc ) return rc;
399     if( aSyscall[iCall].xOrig==0 ){
400       aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
401       pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
402     }
403     aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
404   }
405 
406   return TCL_OK;
407 }
408 
409 static int test_syscall_uninstall(
410   void * clientData,
411   Tcl_Interp *interp,
412   int objc,
413   Tcl_Obj *CONST objv[]
414 ){
415   sqlite3_vfs *pVfs;
416   int i;
417 
418   if( objc!=2 ){
419     Tcl_WrongNumArgs(interp, 2, objv, "");
420     return TCL_ERROR;
421   }
422 
423   pVfs = sqlite3_vfs_find(0);
424   for(i=0; aSyscall[i].zName; i++){
425     if( aSyscall[i].xOrig ){
426       pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0);
427       aSyscall[i].xOrig = 0;
428     }
429   }
430   return TCL_OK;
431 }
432 
433 static int test_syscall_reset(
434   void * clientData,
435   Tcl_Interp *interp,
436   int objc,
437   Tcl_Obj *CONST objv[]
438 ){
439   sqlite3_vfs *pVfs;
440   int i;
441   int rc;
442 
443   if( objc!=2 && objc!=3 ){
444     Tcl_WrongNumArgs(interp, 2, objv, "");
445     return TCL_ERROR;
446   }
447 
448   pVfs = sqlite3_vfs_find(0);
449   if( objc==2 ){
450     rc = pVfs->xSetSystemCall(pVfs, 0, 0);
451     for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
452   }else{
453     int nFunc;
454     char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc);
455     rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0);
456     for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){
457       if( strlen(aSyscall[i].zName)!=nFunc ) continue;
458       if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
459       aSyscall[i].xOrig = 0;
460     }
461   }
462   if( rc!=SQLITE_OK ){
463     Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
464     return TCL_ERROR;
465   }
466 
467   Tcl_ResetResult(interp);
468   return TCL_OK;
469 }
470 
471 static int test_syscall_exists(
472   void * clientData,
473   Tcl_Interp *interp,
474   int objc,
475   Tcl_Obj *CONST objv[]
476 ){
477   sqlite3_vfs *pVfs;
478   sqlite3_syscall_ptr x;
479 
480   if( objc!=3 ){
481     Tcl_WrongNumArgs(interp, 2, objv, "");
482     return TCL_ERROR;
483   }
484 
485   pVfs = sqlite3_vfs_find(0);
486   x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
487 
488   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
489   return TCL_OK;
490 }
491 
492 static int test_syscall_fault(
493   void * clientData,
494   Tcl_Interp *interp,
495   int objc,
496   Tcl_Obj *CONST objv[]
497 ){
498   int nCount = 0;
499   int bPersist = 0;
500 
501   if( objc!=2 && objc!=4 ){
502     Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
503     return TCL_ERROR;
504   }
505 
506   if( objc==4 ){
507     if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
508      || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
509     ){
510       return TCL_ERROR;
511     }
512   }
513 
514   Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
515   gSyscall.nCount = nCount;
516   gSyscall.bPersist = bPersist;
517   gSyscall.nFail = 0;
518   return TCL_OK;
519 }
520 
521 static int test_syscall_errno(
522   void * clientData,
523   Tcl_Interp *interp,
524   int objc,
525   Tcl_Obj *CONST objv[]
526 ){
527   int iCall;
528   int iErrno;
529   int rc;
530 
531   struct Errno {
532     const char *z;
533     int i;
534   } aErrno[] = {
535     { "EACCES", EACCES },
536     { 0, 0 }
537   };
538 
539   if( objc!=4 ){
540     Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
541     return TCL_ERROR;
542   }
543 
544   rc = Tcl_GetIndexFromObjStruct(interp,
545       objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
546   );
547   if( rc!=TCL_OK ) return rc;
548   rc = Tcl_GetIndexFromObjStruct(interp,
549       objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
550   );
551   if( rc!=TCL_OK ) return rc;
552 
553   aSyscall[iCall].custom_errno = aErrno[iErrno].i;
554   return TCL_OK;
555 }
556 
557 static int test_syscall_list(
558   void * clientData,
559   Tcl_Interp *interp,
560   int objc,
561   Tcl_Obj *CONST objv[]
562 ){
563   const char *zSys;
564   sqlite3_vfs *pVfs;
565   Tcl_Obj *pList;
566 
567   if( objc!=2 ){
568     Tcl_WrongNumArgs(interp, 2, objv, "");
569     return TCL_ERROR;
570   }
571 
572   pVfs = sqlite3_vfs_find(0);
573   pList = Tcl_NewObj();
574   Tcl_IncrRefCount(pList);
575   for(zSys = pVfs->xNextSystemCall(pVfs, 0);
576       zSys!=0;
577       zSys = pVfs->xNextSystemCall(pVfs, zSys)
578   ){
579     Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
580   }
581 
582   Tcl_SetObjResult(interp, pList);
583   Tcl_DecrRefCount(pList);
584   return TCL_OK;
585 }
586 
587 static int test_syscall(
588   void * clientData,
589   Tcl_Interp *interp,
590   int objc,
591   Tcl_Obj *CONST objv[]
592 ){
593   struct SyscallCmd {
594     const char *zName;
595     Tcl_ObjCmdProc *xCmd;
596   } aCmd[] = {
597     { "fault",     test_syscall_fault },
598     { "install",   test_syscall_install },
599     { "uninstall", test_syscall_uninstall },
600     { "reset",     test_syscall_reset },
601     { "errno",     test_syscall_errno },
602     { "exists",    test_syscall_exists },
603     { "list",      test_syscall_list },
604     { 0, 0 }
605   };
606   int iCmd;
607   int rc;
608 
609   if( objc<2 ){
610     Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
611     return TCL_ERROR;
612   }
613   rc = Tcl_GetIndexFromObjStruct(interp,
614       objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
615   );
616   if( rc!=TCL_OK ) return rc;
617   return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
618 }
619 
620 int SqlitetestSyscall_Init(Tcl_Interp *interp){
621   struct SyscallCmd {
622     const char *zName;
623     Tcl_ObjCmdProc *xCmd;
624   } aCmd[] = {
625     { "test_syscall",     test_syscall},
626   };
627   int i;
628 
629   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
630     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
631   }
632   return TCL_OK;
633 }
634 #else
635 int SqlitetestSyscall_Init(Tcl_Interp *interp){
636   return TCL_OK;
637 }
638 #endif
639 
640