xref: /sqlite-3.40.0/src/test_syscall.c (revision f9986d90)
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 mmap
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 **   test_syscall pagesize PGSZ
72 **     If PGSZ is a power of two greater than 256, install a wrapper around
73 **     OS function getpagesize() that reports the system page size as PGSZ.
74 **     Or, if PGSZ is less than zero, remove any wrapper already installed.
75 */
76 
77 #include "sqliteInt.h"
78 #include "sqlite3.h"
79 #include "tcl.h"
80 #include <stdlib.h>
81 #include <string.h>
82 #include <assert.h>
83 
84 #if SQLITE_OS_UNIX
85 
86 /* From main.c */
87 extern const char *sqlite3ErrName(int);
88 
89 #include <sys/mman.h>
90 #include <sys/types.h>
91 #include <errno.h>
92 
93 static struct TestSyscallGlobal {
94   int bPersist;                   /* 1 for persistent errors, 0 for transient */
95   int nCount;                     /* Fail after this many more calls */
96   int nFail;                      /* Number of failures that have occurred */
97   int pgsz;
98   sqlite3_syscall_ptr orig_getpagesize;
99 } gSyscall = { 0, 0, 0, 0, 0 };
100 
101 static int ts_open(const char *, int, int);
102 static int ts_close(int fd);
103 static int ts_access(const char *zPath, int mode);
104 static char *ts_getcwd(char *zPath, size_t nPath);
105 static int ts_stat(const char *zPath, struct stat *p);
106 static int ts_fstat(int fd, struct stat *p);
107 static int ts_ftruncate(int fd, off_t n);
108 static int ts_fcntl(int fd, int cmd, ... );
109 static int ts_read(int fd, void *aBuf, size_t nBuf);
110 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
111 /* Note:  pread64() and pwrite64() actually use off64_t as the type on their
112 ** last parameter.  But that datatype is not defined on many systems
113 ** (ex: Mac, OpenBSD).  So substitute a likely equivalent: sqlite3_uint64 */
114 static int ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off);
115 static int ts_write(int fd, const void *aBuf, size_t nBuf);
116 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
117 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off);
118 static int ts_fchmod(int fd, mode_t mode);
119 static int ts_fallocate(int fd, off_t off, off_t len);
120 static void *ts_mmap(void *, size_t, int, int, int, off_t);
121 static void *ts_mremap(void*, size_t, size_t, int, ...);
122 
123 struct TestSyscallArray {
124   const char *zName;
125   sqlite3_syscall_ptr xTest;
126   sqlite3_syscall_ptr xOrig;
127   int default_errno;              /* Default value for errno following errors */
128   int custom_errno;               /* Current value for errno if error */
129 } aSyscall[] = {
130   /*  0 */ { "open",      (sqlite3_syscall_ptr)ts_open,      0, EACCES, 0 },
131   /*  1 */ { "close",     (sqlite3_syscall_ptr)ts_close,     0, 0, 0 },
132   /*  2 */ { "access",    (sqlite3_syscall_ptr)ts_access,    0, 0, 0 },
133   /*  3 */ { "getcwd",    (sqlite3_syscall_ptr)ts_getcwd,    0, 0, 0 },
134   /*  4 */ { "stat",      (sqlite3_syscall_ptr)ts_stat,      0, 0, 0 },
135   /*  5 */ { "fstat",     (sqlite3_syscall_ptr)ts_fstat,     0, 0, 0 },
136   /*  6 */ { "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, EIO, 0 },
137   /*  7 */ { "fcntl",     (sqlite3_syscall_ptr)ts_fcntl,     0, EACCES, 0 },
138   /*  8 */ { "read",      (sqlite3_syscall_ptr)ts_read,      0, 0, 0 },
139   /*  9 */ { "pread",     (sqlite3_syscall_ptr)ts_pread,     0, 0, 0 },
140   /* 10 */ { "pread64",   (sqlite3_syscall_ptr)ts_pread64,   0, 0, 0 },
141   /* 11 */ { "write",     (sqlite3_syscall_ptr)ts_write,     0, 0, 0 },
142   /* 12 */ { "pwrite",    (sqlite3_syscall_ptr)ts_pwrite,    0, 0, 0 },
143   /* 13 */ { "pwrite64",  (sqlite3_syscall_ptr)ts_pwrite64,  0, 0, 0 },
144   /* 14 */ { "fchmod",    (sqlite3_syscall_ptr)ts_fchmod,    0, 0, 0 },
145   /* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
146   /* 16 */ { "mmap",      (sqlite3_syscall_ptr)ts_mmap,      0, 0, 0 },
147   /* 17 */ { "mremap",    (sqlite3_syscall_ptr)ts_mremap,    0, 0, 0 },
148            { 0, 0, 0, 0, 0 }
149 };
150 
151 #define orig_open      ((int(*)(const char *, int, int))aSyscall[0].xOrig)
152 #define orig_close     ((int(*)(int))aSyscall[1].xOrig)
153 #define orig_access    ((int(*)(const char*,int))aSyscall[2].xOrig)
154 #define orig_getcwd    ((char*(*)(char*,size_t))aSyscall[3].xOrig)
155 #define orig_stat      ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
156 #define orig_fstat     ((int(*)(int,struct stat*))aSyscall[5].xOrig)
157 #define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
158 #define orig_fcntl     ((int(*)(int,int,...))aSyscall[7].xOrig)
159 #define orig_read      ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
160 #define orig_pread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
161 #define orig_pread64   ((ssize_t(*)(int,void*,size_t,sqlite3_uint64))aSyscall[10].xOrig)
162 #define orig_write     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
163 #define orig_pwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
164                        aSyscall[12].xOrig)
165 #define orig_pwrite64  ((ssize_t(*)(int,const void*,size_t,sqlite3_uint64))\
166                        aSyscall[13].xOrig)
167 #define orig_fchmod    ((int(*)(int,mode_t))aSyscall[14].xOrig)
168 #define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
169 #define orig_mmap      ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[16].xOrig)
170 #define orig_mremap    ((void*(*)(void*,size_t,size_t,int,...))aSyscall[17].xOrig)
171 
172 /*
173 ** This function is called exactly once from within each invocation of a
174 ** system call wrapper in this file. It returns 1 if the function should
175 ** fail, or 0 if it should succeed.
176 */
177 static int tsIsFail(void){
178   gSyscall.nCount--;
179   if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
180     gSyscall.nFail++;
181     return 1;
182   }
183   return 0;
184 }
185 
186 /*
187 ** Return the current error-number value for function zFunc. zFunc must be
188 ** the name of a system call in the aSyscall[] table.
189 **
190 ** Usually, the current error-number is the value that errno should be set
191 ** to if the named system call fails. The exception is "fallocate". See
192 ** comments above the implementation of ts_fallocate() for details.
193 */
194 static int tsErrno(const char *zFunc){
195   int i;
196   int nFunc = strlen(zFunc);
197   for(i=0; aSyscall[i].zName; i++){
198     if( strlen(aSyscall[i].zName)!=nFunc ) continue;
199     if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
200     return aSyscall[i].custom_errno;
201   }
202 
203   assert(0);
204   return 0;
205 }
206 
207 /*
208 ** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the
209 ** value of errno before returning.
210 */
211 static int tsIsFailErrno(const char *zFunc){
212   if( tsIsFail() ){
213     errno = tsErrno(zFunc);
214     return 1;
215   }
216   return 0;
217 }
218 
219 /*
220 ** A wrapper around open().
221 */
222 static int ts_open(const char *zFile, int flags, int mode){
223   if( tsIsFailErrno("open") ){
224     return -1;
225   }
226   return orig_open(zFile, flags, mode);
227 }
228 
229 /*
230 ** A wrapper around close().
231 */
232 static int ts_close(int fd){
233   if( tsIsFail() ){
234     /* Even if simulating an error, close the original file-descriptor.
235     ** This is to stop the test process from running out of file-descriptors
236     ** when running a long test. If a call to close() appears to fail, SQLite
237     ** never attempts to use the file-descriptor afterwards (or even to close
238     ** it a second time).  */
239     orig_close(fd);
240     return -1;
241   }
242   return orig_close(fd);
243 }
244 
245 /*
246 ** A wrapper around access().
247 */
248 static int ts_access(const char *zPath, int mode){
249   if( tsIsFail() ){
250     return -1;
251   }
252   return orig_access(zPath, mode);
253 }
254 
255 /*
256 ** A wrapper around getcwd().
257 */
258 static char *ts_getcwd(char *zPath, size_t nPath){
259   if( tsIsFail() ){
260     return NULL;
261   }
262   return orig_getcwd(zPath, nPath);
263 }
264 
265 /*
266 ** A wrapper around stat().
267 */
268 static int ts_stat(const char *zPath, struct stat *p){
269   if( tsIsFail() ){
270     return -1;
271   }
272   return orig_stat(zPath, p);
273 }
274 
275 /*
276 ** A wrapper around fstat().
277 */
278 static int ts_fstat(int fd, struct stat *p){
279   if( tsIsFailErrno("fstat") ){
280     return -1;
281   }
282   return orig_fstat(fd, p);
283 }
284 
285 /*
286 ** A wrapper around ftruncate().
287 */
288 static int ts_ftruncate(int fd, off_t n){
289   if( tsIsFailErrno("ftruncate") ){
290     return -1;
291   }
292   return orig_ftruncate(fd, n);
293 }
294 
295 /*
296 ** A wrapper around fcntl().
297 */
298 static int ts_fcntl(int fd, int cmd, ... ){
299   va_list ap;
300   void *pArg;
301   if( tsIsFailErrno("fcntl") ){
302     return -1;
303   }
304   va_start(ap, cmd);
305   pArg = va_arg(ap, void *);
306   return orig_fcntl(fd, cmd, pArg);
307 }
308 
309 /*
310 ** A wrapper around read().
311 */
312 static int ts_read(int fd, void *aBuf, size_t nBuf){
313   if( tsIsFailErrno("read") ){
314     return -1;
315   }
316   return orig_read(fd, aBuf, nBuf);
317 }
318 
319 /*
320 ** A wrapper around pread().
321 */
322 static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
323   if( tsIsFailErrno("pread") ){
324     return -1;
325   }
326   return orig_pread(fd, aBuf, nBuf, off);
327 }
328 
329 /*
330 ** A wrapper around pread64().
331 */
332 static int ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off){
333   if( tsIsFailErrno("pread64") ){
334     return -1;
335   }
336   return orig_pread64(fd, aBuf, nBuf, off);
337 }
338 
339 /*
340 ** A wrapper around write().
341 */
342 static int ts_write(int fd, const void *aBuf, size_t nBuf){
343   if( tsIsFailErrno("write") ){
344     if( tsErrno("write")==EINTR ) orig_write(fd, aBuf, nBuf/2);
345     return -1;
346   }
347   return orig_write(fd, aBuf, nBuf);
348 }
349 
350 /*
351 ** A wrapper around pwrite().
352 */
353 static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
354   if( tsIsFailErrno("pwrite") ){
355     return -1;
356   }
357   return orig_pwrite(fd, aBuf, nBuf, off);
358 }
359 
360 /*
361 ** A wrapper around pwrite64().
362 */
363 static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off){
364   if( tsIsFailErrno("pwrite64") ){
365     return -1;
366   }
367   return orig_pwrite64(fd, aBuf, nBuf, off);
368 }
369 
370 /*
371 ** A wrapper around fchmod().
372 */
373 static int ts_fchmod(int fd, mode_t mode){
374   if( tsIsFail() ){
375     return -1;
376   }
377   return orig_fchmod(fd, mode);
378 }
379 
380 /*
381 ** A wrapper around fallocate().
382 **
383 ** SQLite assumes that the fallocate() function is compatible with
384 ** posix_fallocate(). According to the Linux man page (2009-09-30):
385 **
386 **   posix_fallocate() returns  zero on success, or an error number on
387 **   failure. Note that errno is not set.
388 */
389 static int ts_fallocate(int fd, off_t off, off_t len){
390   if( tsIsFail() ){
391     return tsErrno("fallocate");
392   }
393   return orig_fallocate(fd, off, len);
394 }
395 
396 static void *ts_mmap(
397   void *pAddr,
398   size_t nByte,
399   int prot,
400   int flags,
401   int fd,
402   off_t iOff
403 ){
404   if( tsIsFailErrno("mmap") ){
405     return MAP_FAILED;
406   }
407   return orig_mmap(pAddr, nByte, prot, flags, fd, iOff);
408 }
409 
410 static void *ts_mremap(void *a, size_t b, size_t c, int d, ...){
411   va_list ap;
412   void *pArg;
413   if( tsIsFailErrno("mremap") ){
414     return MAP_FAILED;
415   }
416   va_start(ap, d);
417   pArg = va_arg(ap, void *);
418   return orig_mremap(a, b, c, d, pArg);
419 }
420 
421 static int test_syscall_install(
422   void * clientData,
423   Tcl_Interp *interp,
424   int objc,
425   Tcl_Obj *CONST objv[]
426 ){
427   sqlite3_vfs *pVfs;
428   int nElem;
429   int i;
430   Tcl_Obj **apElem;
431 
432   if( objc!=3 ){
433     Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
434     return TCL_ERROR;
435   }
436   if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
437     return TCL_ERROR;
438   }
439   pVfs = sqlite3_vfs_find(0);
440 
441   for(i=0; i<nElem; i++){
442     int iCall;
443     int rc = Tcl_GetIndexFromObjStruct(interp,
444         apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
445     );
446     if( rc ) return rc;
447     if( aSyscall[iCall].xOrig==0 ){
448       aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
449       pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
450     }
451     aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
452   }
453 
454   return TCL_OK;
455 }
456 
457 static int test_syscall_uninstall(
458   void * clientData,
459   Tcl_Interp *interp,
460   int objc,
461   Tcl_Obj *CONST objv[]
462 ){
463   sqlite3_vfs *pVfs;
464   int i;
465 
466   if( objc!=2 ){
467     Tcl_WrongNumArgs(interp, 2, objv, "");
468     return TCL_ERROR;
469   }
470 
471   pVfs = sqlite3_vfs_find(0);
472   for(i=0; aSyscall[i].zName; i++){
473     if( aSyscall[i].xOrig ){
474       pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, 0);
475       aSyscall[i].xOrig = 0;
476     }
477   }
478   return TCL_OK;
479 }
480 
481 static int test_syscall_reset(
482   void * clientData,
483   Tcl_Interp *interp,
484   int objc,
485   Tcl_Obj *CONST objv[]
486 ){
487   sqlite3_vfs *pVfs;
488   int i;
489   int rc;
490 
491   if( objc!=2 && objc!=3 ){
492     Tcl_WrongNumArgs(interp, 2, objv, "");
493     return TCL_ERROR;
494   }
495 
496   pVfs = sqlite3_vfs_find(0);
497   if( objc==2 ){
498     rc = pVfs->xSetSystemCall(pVfs, 0, 0);
499     for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0;
500   }else{
501     int nFunc;
502     char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc);
503     rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0);
504     for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){
505       if( strlen(aSyscall[i].zName)!=nFunc ) continue;
506       if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
507       aSyscall[i].xOrig = 0;
508     }
509   }
510   if( rc!=SQLITE_OK ){
511     Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
512     return TCL_ERROR;
513   }
514 
515   Tcl_ResetResult(interp);
516   return TCL_OK;
517 }
518 
519 static int test_syscall_exists(
520   void * clientData,
521   Tcl_Interp *interp,
522   int objc,
523   Tcl_Obj *CONST objv[]
524 ){
525   sqlite3_vfs *pVfs;
526   sqlite3_syscall_ptr x;
527 
528   if( objc!=3 ){
529     Tcl_WrongNumArgs(interp, 2, objv, "");
530     return TCL_ERROR;
531   }
532 
533   pVfs = sqlite3_vfs_find(0);
534   x = pVfs->xGetSystemCall(pVfs, Tcl_GetString(objv[2]));
535 
536   Tcl_SetObjResult(interp, Tcl_NewBooleanObj(x!=0));
537   return TCL_OK;
538 }
539 
540 static int test_syscall_fault(
541   void * clientData,
542   Tcl_Interp *interp,
543   int objc,
544   Tcl_Obj *CONST objv[]
545 ){
546   int nCount = 0;
547   int bPersist = 0;
548 
549   if( objc!=2 && objc!=4 ){
550     Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
551     return TCL_ERROR;
552   }
553 
554   if( objc==4 ){
555     if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
556      || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
557     ){
558       return TCL_ERROR;
559     }
560   }
561 
562   Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
563   gSyscall.nCount = nCount;
564   gSyscall.bPersist = bPersist;
565   gSyscall.nFail = 0;
566   return TCL_OK;
567 }
568 
569 static int test_syscall_errno(
570   void * clientData,
571   Tcl_Interp *interp,
572   int objc,
573   Tcl_Obj *CONST objv[]
574 ){
575   int iCall;
576   int iErrno;
577   int rc;
578 
579   struct Errno {
580     const char *z;
581     int i;
582   } aErrno[] = {
583     { "EACCES",    EACCES },
584     { "EINTR",     EINTR },
585     { "EIO",       EIO },
586     { "EOVERFLOW", EOVERFLOW },
587     { "ENOMEM",    ENOMEM },
588     { "EAGAIN",    EAGAIN },
589     { "ETIMEDOUT", ETIMEDOUT },
590     { "EBUSY",     EBUSY },
591     { "EPERM",     EPERM },
592     { "EDEADLK",   EDEADLK },
593     { "ENOLCK",    ENOLCK },
594     { 0, 0 }
595   };
596 
597   if( objc!=4 ){
598     Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
599     return TCL_ERROR;
600   }
601 
602   rc = Tcl_GetIndexFromObjStruct(interp,
603       objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
604   );
605   if( rc!=TCL_OK ) return rc;
606   rc = Tcl_GetIndexFromObjStruct(interp,
607       objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
608   );
609   if( rc!=TCL_OK ) return rc;
610 
611   aSyscall[iCall].custom_errno = aErrno[iErrno].i;
612   return TCL_OK;
613 }
614 
615 static int test_syscall_list(
616   void * clientData,
617   Tcl_Interp *interp,
618   int objc,
619   Tcl_Obj *CONST objv[]
620 ){
621   const char *zSys;
622   sqlite3_vfs *pVfs;
623   Tcl_Obj *pList;
624 
625   if( objc!=2 ){
626     Tcl_WrongNumArgs(interp, 2, objv, "");
627     return TCL_ERROR;
628   }
629 
630   pVfs = sqlite3_vfs_find(0);
631   pList = Tcl_NewObj();
632   Tcl_IncrRefCount(pList);
633   for(zSys = pVfs->xNextSystemCall(pVfs, 0);
634       zSys!=0;
635       zSys = pVfs->xNextSystemCall(pVfs, zSys)
636   ){
637     Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
638   }
639 
640   Tcl_SetObjResult(interp, pList);
641   Tcl_DecrRefCount(pList);
642   return TCL_OK;
643 }
644 
645 static int test_syscall_defaultvfs(
646   void * clientData,
647   Tcl_Interp *interp,
648   int objc,
649   Tcl_Obj *CONST objv[]
650 ){
651   sqlite3_vfs *pVfs;
652 
653   if( objc!=2 ){
654     Tcl_WrongNumArgs(interp, 2, objv, "");
655     return TCL_ERROR;
656   }
657 
658   pVfs = sqlite3_vfs_find(0);
659   Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
660   return TCL_OK;
661 }
662 
663 static int ts_getpagesize(void){
664   return gSyscall.pgsz;
665 }
666 
667 static int test_syscall_pagesize(
668   void * clientData,
669   Tcl_Interp *interp,
670   int objc,
671   Tcl_Obj *CONST objv[]
672 ){
673   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
674   int pgsz;
675   if( objc!=3 ){
676     Tcl_WrongNumArgs(interp, 2, objv, "PGSZ");
677     return TCL_ERROR;
678   }
679   if( Tcl_GetIntFromObj(interp, objv[2], &pgsz) ){
680     return TCL_ERROR;
681   }
682 
683   if( pgsz<0 ){
684     if( gSyscall.orig_getpagesize ){
685       pVfs->xSetSystemCall(pVfs, "getpagesize", gSyscall.orig_getpagesize);
686     }
687   }else{
688     if( pgsz<512 || (pgsz & (pgsz-1)) ){
689       Tcl_AppendResult(interp, "pgsz out of range", 0);
690       return TCL_ERROR;
691     }
692     gSyscall.orig_getpagesize = pVfs->xGetSystemCall(pVfs, "getpagesize");
693     gSyscall.pgsz = pgsz;
694     pVfs->xSetSystemCall(
695         pVfs, "getpagesize", (sqlite3_syscall_ptr)ts_getpagesize
696     );
697   }
698 
699   return TCL_OK;
700 }
701 
702 static int test_syscall(
703   void * clientData,
704   Tcl_Interp *interp,
705   int objc,
706   Tcl_Obj *CONST objv[]
707 ){
708   struct SyscallCmd {
709     const char *zName;
710     Tcl_ObjCmdProc *xCmd;
711   } aCmd[] = {
712     { "fault",      test_syscall_fault },
713     { "install",    test_syscall_install },
714     { "uninstall",  test_syscall_uninstall },
715     { "reset",      test_syscall_reset },
716     { "errno",      test_syscall_errno },
717     { "exists",     test_syscall_exists },
718     { "list",       test_syscall_list },
719     { "defaultvfs", test_syscall_defaultvfs },
720     { "pagesize",   test_syscall_pagesize },
721     { 0, 0 }
722   };
723   int iCmd;
724   int rc;
725 
726   if( objc<2 ){
727     Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
728     return TCL_ERROR;
729   }
730   rc = Tcl_GetIndexFromObjStruct(interp,
731       objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd
732   );
733   if( rc!=TCL_OK ) return rc;
734   return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
735 }
736 
737 int SqlitetestSyscall_Init(Tcl_Interp *interp){
738   struct SyscallCmd {
739     const char *zName;
740     Tcl_ObjCmdProc *xCmd;
741   } aCmd[] = {
742     { "test_syscall",     test_syscall},
743   };
744   int i;
745 
746   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
747     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
748   }
749   return TCL_OK;
750 }
751 #else
752 int SqlitetestSyscall_Init(Tcl_Interp *interp){
753   return TCL_OK;
754 }
755 #endif
756