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, EIO, 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 ** This function is called exactly once from within each invocation of a 157 ** system call wrapper in this file. It returns 1 if the function should 158 ** fail, or 0 if it should succeed. 159 */ 160 static int tsIsFail(void){ 161 gSyscall.nCount--; 162 if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){ 163 gSyscall.nFail++; 164 return 1; 165 } 166 return 0; 167 } 168 169 /* 170 ** Return the current error-number value for function zFunc. zFunc must be 171 ** the name of a system call in the aSyscall[] table. 172 ** 173 ** Usually, the current error-number is the value that errno should be set 174 ** to if the named system call fails. The exception is "fallocate". See 175 ** comments above the implementation of ts_fallocate() for details. 176 */ 177 static int tsErrno(const char *zFunc){ 178 int i; 179 int nFunc = strlen(zFunc); 180 for(i=0; aSyscall[i].zName; i++){ 181 if( strlen(aSyscall[i].zName)!=nFunc ) continue; 182 if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue; 183 return aSyscall[i].custom_errno; 184 } 185 186 assert(0); 187 return 0; 188 } 189 190 /* 191 ** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the 192 ** value of errno before returning. 193 */ 194 static int tsIsFailErrno(const char *zFunc){ 195 if( tsIsFail() ){ 196 errno = tsErrno(zFunc); 197 return 1; 198 } 199 return 0; 200 } 201 202 /* 203 ** A wrapper around open(). 204 */ 205 static int ts_open(const char *zFile, int flags, int mode){ 206 if( tsIsFailErrno("open") ){ 207 return -1; 208 } 209 return orig_open(zFile, flags, mode); 210 } 211 212 /* 213 ** A wrapper around close(). 214 */ 215 static int ts_close(int fd){ 216 if( tsIsFail() ){ 217 return -1; 218 } 219 return orig_close(fd); 220 } 221 222 /* 223 ** A wrapper around access(). 224 */ 225 static int ts_access(const char *zPath, int mode){ 226 if( tsIsFail() ){ 227 return -1; 228 } 229 return orig_access(zPath, mode); 230 } 231 232 /* 233 ** A wrapper around getcwd(). 234 */ 235 static char *ts_getcwd(char *zPath, size_t nPath){ 236 if( tsIsFail() ){ 237 return NULL; 238 } 239 return orig_getcwd(zPath, nPath); 240 } 241 242 /* 243 ** A wrapper around stat(). 244 */ 245 static int ts_stat(const char *zPath, struct stat *p){ 246 if( tsIsFail() ){ 247 return -1; 248 } 249 return orig_stat(zPath, p); 250 } 251 252 /* 253 ** A wrapper around fstat(). 254 */ 255 static int ts_fstat(int fd, struct stat *p){ 256 if( tsIsFailErrno("fstat") ){ 257 return -1; 258 } 259 return orig_fstat(fd, p); 260 } 261 262 /* 263 ** A wrapper around ftruncate(). 264 */ 265 static int ts_ftruncate(int fd, off_t n){ 266 if( tsIsFailErrno("ftruncate") ){ 267 return -1; 268 } 269 return orig_ftruncate(fd, n); 270 } 271 272 /* 273 ** A wrapper around fcntl(). 274 */ 275 static int ts_fcntl(int fd, int cmd, ... ){ 276 va_list ap; 277 void *pArg; 278 if( tsIsFail() ){ 279 return -1; 280 } 281 va_start(ap, cmd); 282 pArg = va_arg(ap, void *); 283 return orig_fcntl(fd, cmd, pArg); 284 } 285 286 /* 287 ** A wrapper around read(). 288 */ 289 static int ts_read(int fd, void *aBuf, size_t nBuf){ 290 if( tsIsFail() ){ 291 return -1; 292 } 293 return orig_read(fd, aBuf, nBuf); 294 } 295 296 /* 297 ** A wrapper around pread(). 298 */ 299 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){ 300 if( tsIsFail() ){ 301 return -1; 302 } 303 return orig_pread(fd, aBuf, nBuf, off); 304 } 305 306 /* 307 ** A wrapper around pread64(). 308 */ 309 static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){ 310 if( tsIsFail() ){ 311 return -1; 312 } 313 return orig_pread64(fd, aBuf, nBuf, off); 314 } 315 316 /* 317 ** A wrapper around write(). 318 */ 319 static int ts_write(int fd, const void *aBuf, size_t nBuf){ 320 if( tsIsFail() ){ 321 return -1; 322 } 323 return orig_write(fd, aBuf, nBuf); 324 } 325 326 /* 327 ** A wrapper around pwrite(). 328 */ 329 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){ 330 if( tsIsFail() ){ 331 return -1; 332 } 333 return orig_pwrite(fd, aBuf, nBuf, off); 334 } 335 336 /* 337 ** A wrapper around pwrite64(). 338 */ 339 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){ 340 if( tsIsFail() ){ 341 return -1; 342 } 343 return orig_pwrite64(fd, aBuf, nBuf, off); 344 } 345 346 /* 347 ** A wrapper around fchmod(). 348 */ 349 static int ts_fchmod(int fd, mode_t mode){ 350 if( tsIsFail() ){ 351 return -1; 352 } 353 return orig_fchmod(fd, mode); 354 } 355 356 /* 357 ** A wrapper around fallocate(). 358 ** 359 ** SQLite assumes that the fallocate() function is compatible with 360 ** posix_fallocate(). According to the Linux man page (2009-09-30): 361 ** 362 ** posix_fallocate() returns zero on success, or an error number on 363 ** failure. Note that errno is not set. 364 */ 365 static int ts_fallocate(int fd, off_t off, off_t len){ 366 if( tsIsFail() ){ 367 return tsErrno("fallocate"); 368 } 369 return orig_fallocate(fd, off, len); 370 } 371 372 static int test_syscall_install( 373 void * clientData, 374 Tcl_Interp *interp, 375 int objc, 376 Tcl_Obj *CONST objv[] 377 ){ 378 sqlite3_vfs *pVfs; 379 int nElem; 380 int i; 381 Tcl_Obj **apElem; 382 383 if( objc!=3 ){ 384 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST"); 385 return TCL_ERROR; 386 } 387 if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){ 388 return TCL_ERROR; 389 } 390 pVfs = sqlite3_vfs_find(0); 391 392 for(i=0; i<nElem; i++){ 393 int iCall; 394 int rc = Tcl_GetIndexFromObjStruct(interp, 395 apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall 396 ); 397 if( rc ) return rc; 398 if( aSyscall[iCall].xOrig==0 ){ 399 aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName); 400 pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest); 401 } 402 aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno; 403 } 404 405 return TCL_OK; 406 } 407 408 static int test_syscall_uninstall( 409 void * clientData, 410 Tcl_Interp *interp, 411 int objc, 412 Tcl_Obj *CONST objv[] 413 ){ 414 sqlite3_vfs *pVfs; 415 int i; 416 417 if( objc!=2 ){ 418 Tcl_WrongNumArgs(interp, 2, objv, ""); 419 return TCL_ERROR; 420 } 421 422 pVfs = sqlite3_vfs_find(0); 423 for(i=0; aSyscall[i].zName; i++){ 424 if( aSyscall[i].xOrig ){ 425 pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0); 426 aSyscall[i].xOrig = 0; 427 } 428 } 429 return TCL_OK; 430 } 431 432 static int test_syscall_reset( 433 void * clientData, 434 Tcl_Interp *interp, 435 int objc, 436 Tcl_Obj *CONST objv[] 437 ){ 438 sqlite3_vfs *pVfs; 439 int i; 440 int rc; 441 442 if( objc!=2 && objc!=3 ){ 443 Tcl_WrongNumArgs(interp, 2, objv, ""); 444 return TCL_ERROR; 445 } 446 447 pVfs = sqlite3_vfs_find(0); 448 if( objc==2 ){ 449 rc = pVfs->xSetSystemCall(pVfs, 0, 0); 450 for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0; 451 }else{ 452 int nFunc; 453 char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc); 454 rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0); 455 for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){ 456 if( strlen(aSyscall[i].zName)!=nFunc ) continue; 457 if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue; 458 aSyscall[i].xOrig = 0; 459 } 460 } 461 if( rc!=SQLITE_OK ){ 462 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1)); 463 return TCL_ERROR; 464 } 465 466 Tcl_ResetResult(interp); 467 return TCL_OK; 468 } 469 470 static int test_syscall_exists( 471 void * clientData, 472 Tcl_Interp *interp, 473 int objc, 474 Tcl_Obj *CONST objv[] 475 ){ 476 sqlite3_vfs *pVfs; 477 sqlite3_syscall_ptr x; 478 479 if( objc!=3 ){ 480 Tcl_WrongNumArgs(interp, 2, objv, ""); 481 return TCL_ERROR; 482 } 483 484 pVfs = sqlite3_vfs_find(0); 485 x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2])); 486 487 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0)); 488 return TCL_OK; 489 } 490 491 static int test_syscall_fault( 492 void * clientData, 493 Tcl_Interp *interp, 494 int objc, 495 Tcl_Obj *CONST objv[] 496 ){ 497 int nCount = 0; 498 int bPersist = 0; 499 500 if( objc!=2 && objc!=4 ){ 501 Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?"); 502 return TCL_ERROR; 503 } 504 505 if( objc==4 ){ 506 if( Tcl_GetIntFromObj(interp, objv[2], &nCount) 507 || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist) 508 ){ 509 return TCL_ERROR; 510 } 511 } 512 513 Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail)); 514 gSyscall.nCount = nCount; 515 gSyscall.bPersist = bPersist; 516 gSyscall.nFail = 0; 517 return TCL_OK; 518 } 519 520 static int test_syscall_errno( 521 void * clientData, 522 Tcl_Interp *interp, 523 int objc, 524 Tcl_Obj *CONST objv[] 525 ){ 526 int iCall; 527 int iErrno; 528 int rc; 529 530 struct Errno { 531 const char *z; 532 int i; 533 } aErrno[] = { 534 { "EACCES", EACCES }, 535 { "EINTR", EINTR }, 536 { "EIO", EIO }, 537 { "EOVERFLOW", EOVERFLOW }, 538 { "ENOMEM", ENOMEM }, 539 { 0, 0 } 540 }; 541 542 if( objc!=4 ){ 543 Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO"); 544 return TCL_ERROR; 545 } 546 547 rc = Tcl_GetIndexFromObjStruct(interp, 548 objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall 549 ); 550 if( rc!=TCL_OK ) return rc; 551 rc = Tcl_GetIndexFromObjStruct(interp, 552 objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno 553 ); 554 if( rc!=TCL_OK ) return rc; 555 556 aSyscall[iCall].custom_errno = aErrno[iErrno].i; 557 return TCL_OK; 558 } 559 560 static int test_syscall_list( 561 void * clientData, 562 Tcl_Interp *interp, 563 int objc, 564 Tcl_Obj *CONST objv[] 565 ){ 566 const char *zSys; 567 sqlite3_vfs *pVfs; 568 Tcl_Obj *pList; 569 570 if( objc!=2 ){ 571 Tcl_WrongNumArgs(interp, 2, objv, ""); 572 return TCL_ERROR; 573 } 574 575 pVfs = sqlite3_vfs_find(0); 576 pList = Tcl_NewObj(); 577 Tcl_IncrRefCount(pList); 578 for(zSys = pVfs->xNextSystemCall(pVfs, 0); 579 zSys!=0; 580 zSys = pVfs->xNextSystemCall(pVfs, zSys) 581 ){ 582 Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1)); 583 } 584 585 Tcl_SetObjResult(interp, pList); 586 Tcl_DecrRefCount(pList); 587 return TCL_OK; 588 } 589 590 static int test_syscall( 591 void * clientData, 592 Tcl_Interp *interp, 593 int objc, 594 Tcl_Obj *CONST objv[] 595 ){ 596 struct SyscallCmd { 597 const char *zName; 598 Tcl_ObjCmdProc *xCmd; 599 } aCmd[] = { 600 { "fault", test_syscall_fault }, 601 { "install", test_syscall_install }, 602 { "uninstall", test_syscall_uninstall }, 603 { "reset", test_syscall_reset }, 604 { "errno", test_syscall_errno }, 605 { "exists", test_syscall_exists }, 606 { "list", test_syscall_list }, 607 { 0, 0 } 608 }; 609 int iCmd; 610 int rc; 611 612 if( objc<2 ){ 613 Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); 614 return TCL_ERROR; 615 } 616 rc = Tcl_GetIndexFromObjStruct(interp, 617 objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd 618 ); 619 if( rc!=TCL_OK ) return rc; 620 return aCmd[iCmd].xCmd(clientData, interp, objc, objv); 621 } 622 623 int SqlitetestSyscall_Init(Tcl_Interp *interp){ 624 struct SyscallCmd { 625 const char *zName; 626 Tcl_ObjCmdProc *xCmd; 627 } aCmd[] = { 628 { "test_syscall", test_syscall}, 629 }; 630 int i; 631 632 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 633 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0); 634 } 635 return TCL_OK; 636 } 637 #else 638 int SqlitetestSyscall_Init(Tcl_Interp *interp){ 639 return TCL_OK; 640 } 641 #endif 642 643