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#include "sqliteInt.h"
79#if SQLITE_OS_UNIX
80
81/* From test1.c */
82extern const char *sqlite3TestErrorName(int);
83
84#include <sys/types.h>
85#include <errno.h>
86
87static 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 };
92
93static int ts_open(const char *, int, int);
94static int ts_close(int fd);
95static int ts_access(const char *zPath, int mode);
96static char *ts_getcwd(char *zPath, size_t nPath);
97static int ts_stat(const char *zPath, struct stat *p);
98static int ts_fstat(int fd, struct stat *p);
99static int ts_ftruncate(int fd, off_t n);
100static int ts_fcntl(int fd, int cmd, ... );
101static int ts_read(int fd, void *aBuf, size_t nBuf);
102static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
103static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off);
104static int ts_write(int fd, const void *aBuf, size_t nBuf);
105static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
106static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
107static int ts_fchmod(int fd, mode_t mode);
108static int ts_fallocate(int fd, off_t off, off_t len);
109
110
111struct TestSyscallArray {
112  const char *zName;
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 */
117} aSyscall[] = {
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 },
134           { 0, 0, 0, 0, 0 }
135};
136
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))\
150                       aSyscall[12].xOrig)
151#define orig_pwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
152                       aSyscall[13].xOrig)
153#define orig_fchmod    ((int(*)(int,mode_t))aSyscall[14].xOrig)
154#define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
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*/
161static 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*/
178static 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*/
195static 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*/
206static 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*/
216static int ts_close(int fd){
217  if( tsIsFail() ){
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).  */
223    orig_close(fd);
224    return -1;
225  }
226  return orig_close(fd);
227}
228
229/*
230** A wrapper around access().
231*/
232static int ts_access(const char *zPath, int mode){
233  if( tsIsFail() ){
234    return -1;
235  }
236  return orig_access(zPath, mode);
237}
238
239/*
240** A wrapper around getcwd().
241*/
242static char *ts_getcwd(char *zPath, size_t nPath){
243  if( tsIsFail() ){
244    return NULL;
245  }
246  return orig_getcwd(zPath, nPath);
247}
248
249/*
250** A wrapper around stat().
251*/
252static int ts_stat(const char *zPath, struct stat *p){
253  if( tsIsFail() ){
254    return -1;
255  }
256  return orig_stat(zPath, p);
257}
258
259/*
260** A wrapper around fstat().
261*/
262static int ts_fstat(int fd, struct stat *p){
263  if( tsIsFailErrno("fstat") ){
264    return -1;
265  }
266  return orig_fstat(fd, p);
267}
268
269/*
270** A wrapper around ftruncate().
271*/
272static int ts_ftruncate(int fd, off_t n){
273  if( tsIsFailErrno("ftruncate") ){
274    return -1;
275  }
276  return orig_ftruncate(fd, n);
277}
278
279/*
280** A wrapper around fcntl().
281*/
282static int ts_fcntl(int fd, int cmd, ... ){
283  va_list ap;
284  void *pArg;
285  if( tsIsFailErrno("fcntl") ){
286    return -1;
287  }
288  va_start(ap, cmd);
289  pArg = va_arg(ap, void *);
290  return orig_fcntl(fd, cmd, pArg);
291}
292
293/*
294** A wrapper around read().
295*/
296static int ts_read(int fd, void *aBuf, size_t nBuf){
297  if( tsIsFailErrno("read") ){
298    return -1;
299  }
300  return orig_read(fd, aBuf, nBuf);
301}
302
303/*
304** A wrapper around pread().
305*/
306static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
307  if( tsIsFailErrno("pread") ){
308    return -1;
309  }
310  return orig_pread(fd, aBuf, nBuf, off);
311}
312
313/*
314** A wrapper around pread64().
315*/
316static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){
317  if( tsIsFailErrno("pread64") ){
318    return -1;
319  }
320  return orig_pread64(fd, aBuf, nBuf, off);
321}
322
323/*
324** A wrapper around write().
325*/
326static int ts_write(int fd, const void *aBuf, size_t nBuf){
327  if( tsIsFailErrno("write") ){
328    return -1;
329  }
330  return orig_write(fd, aBuf, nBuf);
331}
332
333/*
334** A wrapper around pwrite().
335*/
336static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
337  if( tsIsFailErrno("pwrite") ){
338    return -1;
339  }
340  return orig_pwrite(fd, aBuf, nBuf, off);
341}
342
343/*
344** A wrapper around pwrite64().
345*/
346static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){
347  if( tsIsFailErrno("pwrite64") ){
348    return -1;
349  }
350  return orig_pwrite64(fd, aBuf, nBuf, off);
351}
352
353/*
354** A wrapper around fchmod().
355*/
356static int ts_fchmod(int fd, mode_t mode){
357  if( tsIsFail() ){
358    return -1;
359  }
360  return orig_fchmod(fd, mode);
361}
362
363/*
364** A wrapper around fallocate().
365**
366** SQLite assumes that the fallocate() function is compatible with
367** posix_fallocate(). According to the Linux man page (2009-09-30):
368**
369**   posix_fallocate() returns  zero on success, or an error number on
370**   failure. Note that errno is not set.
371*/
372static int ts_fallocate(int fd, off_t off, off_t len){
373  if( tsIsFail() ){
374    return tsErrno("fallocate");
375  }
376  return orig_fallocate(fd, off, len);
377}
378
379static int test_syscall_install(
380  void * clientData,
381  Tcl_Interp *interp,
382  int objc,
383  Tcl_Obj *CONST objv[]
384){
385  sqlite3_vfs *pVfs;
386  int nElem;
387  int i;
388  Tcl_Obj **apElem;
389
390  if( objc!=3 ){
391    Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
392    return TCL_ERROR;
393  }
394  if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
395    return TCL_ERROR;
396  }
397  pVfs = sqlite3_vfs_find(0);
398
399  for(i=0; i<nElem; i++){
400    int iCall;
401    int rc = Tcl_GetIndexFromObjStruct(interp,
402        apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
403    );
404    if( rc ) return rc;
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);
408    }
409    aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
410  }
411
412  return TCL_OK;
413}
414
415static int test_syscall_uninstall(
416  void * clientData,
417  Tcl_Interp *interp,
418  int objc,
419  Tcl_Obj *CONST objv[]
420){
421  sqlite3_vfs *pVfs;
422  int i;
423
424  if( objc!=2 ){
425    Tcl_WrongNumArgs(interp, 2, objv, "");
426    return TCL_ERROR;
427  }
428
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;
434    }
435  }
436  return TCL_OK;
437}
438
439static int test_syscall_reset(
440  void * clientData,
441  Tcl_Interp *interp,
442  int objc,
443  Tcl_Obj *CONST objv[]
444){
445  sqlite3_vfs *pVfs;
446  int i;
447  int rc;
448
449  if( objc!=2 && objc!=3 ){
450    Tcl_WrongNumArgs(interp, 2, objv, "");
451    return TCL_ERROR;
452  }
453
454  pVfs = sqlite3_vfs_find(0);
455  if( objc==2 ){
456    rc = pVfs->xSetSystemCall(pVfs, 0, 0);
457    for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
458  }else{
459    int nFunc;
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;
466    }
467  }
468  if( rc!=SQLITE_OK ){
469    Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3TestErrorName(rc), -1));
470    return TCL_ERROR;
471  }
472
473  Tcl_ResetResult(interp);
474  return TCL_OK;
475}
476
477static int test_syscall_exists(
478  void * clientData,
479  Tcl_Interp *interp,
480  int objc,
481  Tcl_Obj *CONST objv[]
482){
483  sqlite3_vfs *pVfs;
484  sqlite3_syscall_ptr x;
485
486  if( objc!=3 ){
487    Tcl_WrongNumArgs(interp, 2, objv, "");
488    return TCL_ERROR;
489  }
490
491  pVfs = sqlite3_vfs_find(0);
492  x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
493
494  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
495  return TCL_OK;
496}
497
498static int test_syscall_fault(
499  void * clientData,
500  Tcl_Interp *interp,
501  int objc,
502  Tcl_Obj *CONST objv[]
503){
504  int nCount = 0;
505  int bPersist = 0;
506
507  if( objc!=2 && objc!=4 ){
508    Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
509    return TCL_ERROR;
510  }
511
512  if( objc==4 ){
513    if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
514     || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
515    ){
516      return TCL_ERROR;
517    }
518  }
519
520  Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
521  gSyscall.nCount = nCount;
522  gSyscall.bPersist = bPersist;
523  gSyscall.nFail = 0;
524  return TCL_OK;
525}
526
527static int test_syscall_errno(
528  void * clientData,
529  Tcl_Interp *interp,
530  int objc,
531  Tcl_Obj *CONST objv[]
532){
533  int iCall;
534  int iErrno;
535  int rc;
536
537  struct Errno {
538    const char *z;
539    int i;
540  } aErrno[] = {
541    { "EACCES",    EACCES },
542    { "EINTR",     EINTR },
543    { "EIO",       EIO },
544    { "EOVERFLOW", EOVERFLOW },
545    { "ENOMEM",    ENOMEM },
546    { "EAGAIN",    EAGAIN },
547    { "ETIMEDOUT", ETIMEDOUT },
548    { "EBUSY",     EBUSY },
549    { "EPERM",     EPERM },
550    { "EDEADLK",   EDEADLK },
551    { "ENOLCK",    ENOLCK },
552    { 0, 0 }
553  };
554
555  if( objc!=4 ){
556    Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
557    return TCL_ERROR;
558  }
559
560  rc = Tcl_GetIndexFromObjStruct(interp,
561      objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
562  );
563  if( rc!=TCL_OK ) return rc;
564  rc = Tcl_GetIndexFromObjStruct(interp,
565      objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
566  );
567  if( rc!=TCL_OK ) return rc;
568
569  aSyscall[iCall].custom_errno = aErrno[iErrno].i;
570  return TCL_OK;
571}
572
573static int test_syscall_list(
574  void * clientData,
575  Tcl_Interp *interp,
576  int objc,
577  Tcl_Obj *CONST objv[]
578){
579  const char *zSys;
580  sqlite3_vfs *pVfs;
581  Tcl_Obj *pList;
582
583  if( objc!=2 ){
584    Tcl_WrongNumArgs(interp, 2, objv, "");
585    return TCL_ERROR;
586  }
587
588  pVfs = sqlite3_vfs_find(0);
589  pList = Tcl_NewObj();
590  Tcl_IncrRefCount(pList);
591  for(zSys = pVfs->xNextSystemCall(pVfs, 0);
592      zSys!=0;
593      zSys = pVfs->xNextSystemCall(pVfs, zSys)
594  ){
595    Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
596  }
597
598  Tcl_SetObjResult(interp, pList);
599  Tcl_DecrRefCount(pList);
600  return TCL_OK;
601}
602
603static int test_syscall_defaultvfs(
604  void * clientData,
605  Tcl_Interp *interp,
606  int objc,
607  Tcl_Obj *CONST objv[]
608){
609  sqlite3_vfs *pVfs;
610
611  if( objc!=2 ){
612    Tcl_WrongNumArgs(interp, 2, objv, "");
613    return TCL_ERROR;
614  }
615
616  pVfs = sqlite3_vfs_find(0);
617  Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
618  return TCL_OK;
619}
620
621static int test_syscall(
622  void * clientData,
623  Tcl_Interp *interp,
624  int objc,
625  Tcl_Obj *CONST objv[]
626){
627  struct SyscallCmd {
628    const char *zName;
629    Tcl_ObjCmdProc *xCmd;
630  } aCmd[] = {
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 },
639    { 0, 0 }
640  };
641  int iCmd;
642  int rc;
643
644  if( objc<2 ){
645    Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
646    return TCL_ERROR;
647  }
648  rc = Tcl_GetIndexFromObjStruct(interp,
649      objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
650  );
651  if( rc!=TCL_OK ) return rc;
652  return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
653}
654
655int SqlitetestSyscall_Init(Tcl_Interp *interp){
656  struct SyscallCmd {
657    const char *zName;
658    Tcl_ObjCmdProc *xCmd;
659  } aCmd[] = {
660    { "test_syscall",     test_syscall},
661  };
662  int i;
663
664  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
665    Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
666  }
667  return TCL_OK;
668}
669#else
670int SqlitetestSyscall_Init(Tcl_Interp *interp){
671  return TCL_OK;
672}
673#endif
674
675