4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
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:
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
24 ** open close access getcwd stat fstat
25 ** ftruncate fcntl read pread pread64 write
26 ** pwrite pwrite64 fchmod fallocate
28 ** test_syscall uninstall
29 ** Uninstall all wrapper functions.
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
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.
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
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:
54 ** xSetSystemCall(pVfs, 0, 0);
56 ** To restore the default system calls. The [uninstall] command restores
57 ** each system call individually by calling (i.e.):
59 ** xSetSystemCall(pVfs, "open", 0);
61 ** With an argument, this command attempts to reset the system call named
62 ** by the parameter using the same method as [uninstall].
64 ** test_syscall exists SYSTEM-CALL
65 ** Return true if the named system call exists. Or false otherwise.
68 ** Return a list of all system calls. The list is constructed using
69 ** the xNextSystemCall() VFS method.
78 #include "sqliteInt.h"
82 extern const char *sqlite3TestErrorName(int);
84 #include <sys/types.h>
87 static struct TestSyscallGlobal {
88 int bPersist; /* 1 for persistent errors, 0 for transient */
89 int nCount; /* Fail after this many more calls */
90 int nFail; /* Number of failures that have occurred */
91 } gSyscall = { 0, 0 };
93 static int ts_open(const char *, int, int);
94 static int ts_close(int fd);
95 static int ts_access(const char *zPath, int mode);
96 static char *ts_getcwd(char *zPath, size_t nPath);
97 static int ts_stat(const char *zPath, struct stat *p);
98 static int ts_fstat(int fd, struct stat *p);
99 static int ts_ftruncate(int fd, off_t n);
100 static int ts_fcntl(int fd, int cmd, ... );
101 static int ts_read(int fd, void *aBuf, size_t nBuf);
102 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
103 static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off);
104 static int ts_write(int fd, const void *aBuf, size_t nBuf);
105 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
106 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
107 static int ts_fchmod(int fd, mode_t mode);
108 static int ts_fallocate(int fd, off_t off, off_t len);
111 struct TestSyscallArray {
113 sqlite3_syscall_ptr xTest;
114 sqlite3_syscall_ptr xOrig;
115 int default_errno; /* Default value for errno following errors */
116 int custom_errno; /* Current value for errno if error */
118 /* 0 */ { "open", (sqlite3_syscall_ptr)ts_open, 0, EACCES, 0 },
119 /* 1 */ { "close", (sqlite3_syscall_ptr)ts_close, 0, 0, 0 },
120 /* 2 */ { "access", (sqlite3_syscall_ptr)ts_access, 0, 0, 0 },
121 /* 3 */ { "getcwd", (sqlite3_syscall_ptr)ts_getcwd, 0, 0, 0 },
122 /* 4 */ { "stat", (sqlite3_syscall_ptr)ts_stat, 0, 0, 0 },
123 /* 5 */ { "fstat", (sqlite3_syscall_ptr)ts_fstat, 0, 0, 0 },
124 /* 6 */ { "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, EIO, 0 },
125 /* 7 */ { "fcntl", (sqlite3_syscall_ptr)ts_fcntl, 0, EACCES, 0 },
126 /* 8 */ { "read", (sqlite3_syscall_ptr)ts_read, 0, 0, 0 },
127 /* 9 */ { "pread", (sqlite3_syscall_ptr)ts_pread, 0, 0, 0 },
128 /* 10 */ { "pread64", (sqlite3_syscall_ptr)ts_pread64, 0, 0, 0 },
129 /* 11 */ { "write", (sqlite3_syscall_ptr)ts_write, 0, 0, 0 },
130 /* 12 */ { "pwrite", (sqlite3_syscall_ptr)ts_pwrite, 0, 0, 0 },
131 /* 13 */ { "pwrite64", (sqlite3_syscall_ptr)ts_pwrite64, 0, 0, 0 },
132 /* 14 */ { "fchmod", (sqlite3_syscall_ptr)ts_fchmod, 0, 0, 0 },
133 /* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
137 #define orig_open ((int(*)(const char *, int, int))aSyscall[0].xOrig)
138 #define orig_close ((int(*)(int))aSyscall[1].xOrig)
139 #define orig_access ((int(*)(const char*,int))aSyscall[2].xOrig)
140 #define orig_getcwd ((char*(*)(char*,size_t))aSyscall[3].xOrig)
141 #define orig_stat ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
142 #define orig_fstat ((int(*)(int,struct stat*))aSyscall[5].xOrig)
143 #define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
144 #define orig_fcntl ((int(*)(int,int,...))aSyscall[7].xOrig)
145 #define orig_read ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
146 #define orig_pread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
147 #define orig_pread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].xOrig)
148 #define orig_write ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
149 #define orig_pwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
151 #define orig_pwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
153 #define orig_fchmod ((int(*)(int,mode_t))aSyscall[14].xOrig)
154 #define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
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.
161 static int tsIsFail(void){
163 if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
171 ** Return the current error-number value for function zFunc. zFunc must be
172 ** the name of a system call in the aSyscall[] table.
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.
178 static int tsErrno(const char *zFunc){
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;
192 ** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the
193 ** value of errno before returning.
195 static int tsIsFailErrno(const char *zFunc){
197 errno = tsErrno(zFunc);
204 ** A wrapper around open().
206 static int ts_open(const char *zFile, int flags, int mode){
207 if( tsIsFailErrno("open") ){
210 return orig_open(zFile, flags, mode);
214 ** A wrapper around close().
216 static int ts_close(int fd){
218 /* Even if simulating an error, close the original file-descriptor.
219 ** This is to stop the test process from running out of file-descriptors
220 ** when running a long test. If a call to close() appears to fail, SQLite
221 ** never attempts to use the file-descriptor afterwards (or even to close
222 ** it a second time). */
226 return orig_close(fd);
230 ** A wrapper around access().
232 static int ts_access(const char *zPath, int mode){
236 return orig_access(zPath, mode);
240 ** A wrapper around getcwd().
242 static char *ts_getcwd(char *zPath, size_t nPath){
246 return orig_getcwd(zPath, nPath);
250 ** A wrapper around stat().
252 static int ts_stat(const char *zPath, struct stat *p){
256 return orig_stat(zPath, p);
260 ** A wrapper around fstat().
262 static int ts_fstat(int fd, struct stat *p){
263 if( tsIsFailErrno("fstat") ){
266 return orig_fstat(fd, p);
270 ** A wrapper around ftruncate().
272 static int ts_ftruncate(int fd, off_t n){
273 if( tsIsFailErrno("ftruncate") ){
276 return orig_ftruncate(fd, n);
280 ** A wrapper around fcntl().
282 static int ts_fcntl(int fd, int cmd, ... ){
285 if( tsIsFailErrno("fcntl") ){
289 pArg = va_arg(ap, void *);
290 return orig_fcntl(fd, cmd, pArg);
294 ** A wrapper around read().
296 static int ts_read(int fd, void *aBuf, size_t nBuf){
297 if( tsIsFailErrno("read") ){
300 return orig_read(fd, aBuf, nBuf);
304 ** A wrapper around pread().
306 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
307 if( tsIsFailErrno("pread") ){
310 return orig_pread(fd, aBuf, nBuf, off);
314 ** A wrapper around pread64().
316 static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){
317 if( tsIsFailErrno("pread64") ){
320 return orig_pread64(fd, aBuf, nBuf, off);
324 ** A wrapper around write().
326 static int ts_write(int fd, const void *aBuf, size_t nBuf){
327 if( tsIsFailErrno("write") ){
330 return orig_write(fd, aBuf, nBuf);
334 ** A wrapper around pwrite().
336 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
337 if( tsIsFailErrno("pwrite") ){
340 return orig_pwrite(fd, aBuf, nBuf, off);
344 ** A wrapper around pwrite64().
346 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){
347 if( tsIsFailErrno("pwrite64") ){
350 return orig_pwrite64(fd, aBuf, nBuf, off);
354 ** A wrapper around fchmod().
356 static int ts_fchmod(int fd, mode_t mode){
360 return orig_fchmod(fd, mode);
364 ** A wrapper around fallocate().
366 ** SQLite assumes that the fallocate() function is compatible with
367 ** posix_fallocate(). According to the Linux man page (2009-09-30):
369 ** posix_fallocate() returns zero on success, or an error number on
370 ** failure. Note that errno is not set.
372 static int ts_fallocate(int fd, off_t off, off_t len){
374 return tsErrno("fallocate");
376 return orig_fallocate(fd, off, len);
379 static int test_syscall_install(
383 Tcl_Obj *CONST objv[]
391 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
394 if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
397 pVfs = sqlite3_vfs_find(0);
399 for(i=0; i<nElem; i++){
401 int rc = Tcl_GetIndexFromObjStruct(interp,
402 apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
405 if( aSyscall[iCall].xOrig==0 ){
406 aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
407 pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
409 aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
415 static int test_syscall_uninstall(
419 Tcl_Obj *CONST objv[]
425 Tcl_WrongNumArgs(interp, 2, objv, "");
429 pVfs = sqlite3_vfs_find(0);
430 for(i=0; aSyscall[i].zName; i++){
431 if( aSyscall[i].xOrig ){
432 pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0);
433 aSyscall[i].xOrig = 0;
439 static int test_syscall_reset(
443 Tcl_Obj *CONST objv[]
449 if( objc!=2 && objc!=3 ){
450 Tcl_WrongNumArgs(interp, 2, objv, "");
454 pVfs = sqlite3_vfs_find(0);
456 rc = pVfs->xSetSystemCall(pVfs, 0, 0);
457 for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
460 char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc);
461 rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0);
462 for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){
463 if( strlen(aSyscall[i].zName)!=nFunc ) continue;
464 if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
465 aSyscall[i].xOrig = 0;
469 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
473 Tcl_ResetResult(interp);
477 static int test_syscall_exists(
481 Tcl_Obj *CONST objv[]
484 sqlite3_syscall_ptr x;
487 Tcl_WrongNumArgs(interp, 2, objv, "");
491 pVfs = sqlite3_vfs_find(0);
492 x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
494 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
498 static int test_syscall_fault(
502 Tcl_Obj *CONST objv[]
507 if( objc!=2 && objc!=4 ){
508 Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
513 if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
514 || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
520 Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
521 gSyscall.nCount = nCount;
522 gSyscall.bPersist = bPersist;
527 static int test_syscall_errno(
531 Tcl_Obj *CONST objv[]
541 { "EACCES", EACCES },
544 { "EOVERFLOW", EOVERFLOW },
545 { "ENOMEM", ENOMEM },
546 { "EAGAIN", EAGAIN },
547 { "ETIMEDOUT", ETIMEDOUT },
550 { "EDEADLK", EDEADLK },
551 { "ENOLCK", ENOLCK },
556 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
560 rc = Tcl_GetIndexFromObjStruct(interp,
561 objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
563 if( rc!=TCL_OK ) return rc;
564 rc = Tcl_GetIndexFromObjStruct(interp,
565 objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
567 if( rc!=TCL_OK ) return rc;
569 aSyscall[iCall].custom_errno = aErrno[iErrno].i;
573 static int test_syscall_list(
577 Tcl_Obj *CONST objv[]
584 Tcl_WrongNumArgs(interp, 2, objv, "");
588 pVfs = sqlite3_vfs_find(0);
589 pList = Tcl_NewObj();
590 Tcl_IncrRefCount(pList);
591 for(zSys = pVfs->xNextSystemCall(pVfs, 0);
593 zSys = pVfs->xNextSystemCall(pVfs, zSys)
595 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
598 Tcl_SetObjResult(interp, pList);
599 Tcl_DecrRefCount(pList);
603 static int test_syscall_defaultvfs(
607 Tcl_Obj *CONST objv[]
612 Tcl_WrongNumArgs(interp, 2, objv, "");
616 pVfs = sqlite3_vfs_find(0);
617 Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
621 static int test_syscall(
625 Tcl_Obj *CONST objv[]
629 Tcl_ObjCmdProc *xCmd;
631 { "fault", test_syscall_fault },
632 { "install", test_syscall_install },
633 { "uninstall", test_syscall_uninstall },
634 { "reset", test_syscall_reset },
635 { "errno", test_syscall_errno },
636 { "exists", test_syscall_exists },
637 { "list", test_syscall_list },
638 { "defaultvfs", test_syscall_defaultvfs },
645 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
648 rc = Tcl_GetIndexFromObjStruct(interp,
649 objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
651 if( rc!=TCL_OK ) return rc;
652 return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
655 int SqlitetestSyscall_Init(Tcl_Interp *interp){
658 Tcl_ObjCmdProc *xCmd;
660 { "test_syscall", test_syscall},
664 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
665 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
670 int SqlitetestSyscall_Init(Tcl_Interp *interp){