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 ** This file contains code used to implement test interfaces to the
14 ** memory allocation subsystem.
16 #include "sqliteInt.h"
23 ** This structure is used to encapsulate the global state variables used
24 ** by malloc() fault simulation.
26 static struct MemFault
{
27 int iCountdown
; /* Number of pending successes before a failure */
28 int nRepeat
; /* Number of times to repeat the failure */
29 int nBenign
; /* Number of benign failures seen since last config */
30 int nFail
; /* Number of failures seen since last config */
31 u8 enable
; /* True if enabled */
32 int isInstalled
; /* True if the fault simulation layer is installed */
33 int isBenignMode
; /* True if malloc failures are considered benign */
34 sqlite3_mem_methods m
; /* 'Real' malloc implementation */
38 ** This routine exists as a place to set a breakpoint that will
39 ** fire on any simulated malloc() failure.
41 static void sqlite3Fault(void){
47 ** Check to see if a fault should be simulated. Return true to simulate
48 ** the fault. Return false if the fault should not be simulated.
50 static int faultsimStep(void){
51 if( likely(!memfault
.enable
) ){
54 if( memfault
.iCountdown
>0 ){
55 memfault
.iCountdown
--;
60 if( memfault
.isBenignMode
>0 ){
64 if( memfault
.nRepeat
<=0 ){
71 ** A version of sqlite3_mem_methods.xMalloc() that includes fault simulation
74 static void *faultsimMalloc(int n
){
76 if( !faultsimStep() ){
77 p
= memfault
.m
.xMalloc(n
);
84 ** A version of sqlite3_mem_methods.xRealloc() that includes fault simulation
87 static void *faultsimRealloc(void *pOld
, int n
){
89 if( !faultsimStep() ){
90 p
= memfault
.m
.xRealloc(pOld
, n
);
96 ** The following method calls are passed directly through to the underlying
105 static void faultsimFree(void *p
){
108 static int faultsimSize(void *p
){
109 return memfault
.m
.xSize(p
);
111 static int faultsimRoundup(int n
){
112 return memfault
.m
.xRoundup(n
);
114 static int faultsimInit(void *p
){
115 return memfault
.m
.xInit(memfault
.m
.pAppData
);
117 static void faultsimShutdown(void *p
){
118 memfault
.m
.xShutdown(memfault
.m
.pAppData
);
122 ** This routine configures the malloc failure simulation. After
123 ** calling this routine, the next nDelay mallocs will succeed, followed
124 ** by a block of nRepeat failures, after which malloc() calls will begin
127 static void faultsimConfig(int nDelay
, int nRepeat
){
128 memfault
.iCountdown
= nDelay
;
129 memfault
.nRepeat
= nRepeat
;
130 memfault
.nBenign
= 0;
132 memfault
.enable
= nDelay
>=0;
134 /* Sometimes, when running multi-threaded tests, the isBenignMode
135 ** variable is not properly incremented/decremented so that it is
136 ** 0 when not inside a benign malloc block. This doesn't affect
137 ** the multi-threaded tests, as they do not use this system. But
138 ** it does affect OOM tests run later in the same process. So
139 ** zero the variable here, just to be sure.
141 memfault
.isBenignMode
= 0;
145 ** Return the number of faults (both hard and benign faults) that have
146 ** occurred since the injector was last configured.
148 static int faultsimFailures(void){
149 return memfault
.nFail
;
153 ** Return the number of benign faults that have occurred since the
154 ** injector was last configured.
156 static int faultsimBenignFailures(void){
157 return memfault
.nBenign
;
161 ** Return the number of successes that will occur before the next failure.
162 ** If no failures are scheduled, return -1.
164 static int faultsimPending(void){
165 if( memfault
.enable
){
166 return memfault
.iCountdown
;
173 static void faultsimBeginBenign(void){
174 memfault
.isBenignMode
++;
176 static void faultsimEndBenign(void){
177 memfault
.isBenignMode
--;
181 ** Add or remove the fault-simulation layer using sqlite3_config(). If
182 ** the argument is non-zero, the
184 static int faultsimInstall(int install
){
185 static struct sqlite3_mem_methods m
= {
186 faultsimMalloc
, /* xMalloc */
187 faultsimFree
, /* xFree */
188 faultsimRealloc
, /* xRealloc */
189 faultsimSize
, /* xSize */
190 faultsimRoundup
, /* xRoundup */
191 faultsimInit
, /* xInit */
192 faultsimShutdown
, /* xShutdown */
197 install
= (install
? 1 : 0);
198 assert(memfault
.isInstalled
==1 || memfault
.isInstalled
==0);
200 if( install
==memfault
.isInstalled
){
205 rc
= sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &memfault
.m
);
206 assert(memfault
.m
.xMalloc
);
208 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &m
);
210 sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS
,
211 faultsimBeginBenign
, faultsimEndBenign
214 sqlite3_mem_methods m
;
215 assert(memfault
.m
.xMalloc
);
217 /* One should be able to reset the default memory allocator by storing
218 ** a zeroed allocator then calling GETMALLOC. */
219 memset(&m
, 0, sizeof(m
));
220 sqlite3_config(SQLITE_CONFIG_MALLOC
, &m
);
221 sqlite3_config(SQLITE_CONFIG_GETMALLOC
, &m
);
222 assert( memcmp(&m
, &memfault
.m
, sizeof(m
))==0 );
224 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, &memfault
.m
);
225 sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS
, 0, 0);
229 memfault
.isInstalled
= 1;
237 ** This function is implemented in main.c. Returns a pointer to a static
238 ** buffer containing the symbolic SQLite error code that corresponds to
239 ** the least-significant 8-bits of the integer passed as an argument.
242 ** sqlite3ErrName(1) -> "SQLITE_ERROR"
244 extern const char *sqlite3ErrName(int);
247 ** Transform pointers to text and back again
249 static void pointerToText(void *p
, char *z
){
250 static const char zHex
[] = "0123456789abcdef";
258 if( sizeof(n
)==sizeof(p
) ){
259 memcpy(&n
, &p
, sizeof(p
));
260 }else if( sizeof(u
)==sizeof(p
) ){
261 memcpy(&u
, &p
, sizeof(u
));
266 for(i
=0, k
=sizeof(p
)*2-1; i
<sizeof(p
)*2; i
++, k
--){
272 static int hexToInt(int h
){
273 if( h
>='0' && h
<='9' ){
275 }else if( h
>='a' && h
<='f' ){
281 static int textToPointer(const char *z
, void **pp
){
282 sqlite3_uint64 n
= 0;
285 for(i
=0; i
<sizeof(void*)*2 && z
[0]; i
++){
288 if( v
<0 ) return TCL_ERROR
;
291 if( *z
!=0 ) return TCL_ERROR
;
292 if( sizeof(n
)==sizeof(*pp
) ){
293 memcpy(pp
, &n
, sizeof(n
));
294 }else if( sizeof(u
)==sizeof(*pp
) ){
296 memcpy(pp
, &u
, sizeof(u
));
304 ** Usage: sqlite3_malloc NBYTES
306 ** Raw test interface for sqlite3_malloc().
308 static int test_malloc(
312 Tcl_Obj
*CONST objv
[]
318 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTES");
321 if( Tcl_GetIntFromObj(interp
, objv
[1], &nByte
) ) return TCL_ERROR
;
322 p
= sqlite3_malloc((unsigned)nByte
);
323 pointerToText(p
, zOut
);
324 Tcl_AppendResult(interp
, zOut
, NULL
);
329 ** Usage: sqlite3_realloc PRIOR NBYTES
331 ** Raw test interface for sqlite3_realloc().
333 static int test_realloc(
337 Tcl_Obj
*CONST objv
[]
343 Tcl_WrongNumArgs(interp
, 1, objv
, "PRIOR NBYTES");
346 if( Tcl_GetIntFromObj(interp
, objv
[2], &nByte
) ) return TCL_ERROR
;
347 if( textToPointer(Tcl_GetString(objv
[1]), &pPrior
) ){
348 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
351 p
= sqlite3_realloc(pPrior
, (unsigned)nByte
);
352 pointerToText(p
, zOut
);
353 Tcl_AppendResult(interp
, zOut
, NULL
);
358 ** Usage: sqlite3_free PRIOR
360 ** Raw test interface for sqlite3_free().
362 static int test_free(
366 Tcl_Obj
*CONST objv
[]
370 Tcl_WrongNumArgs(interp
, 1, objv
, "PRIOR");
373 if( textToPointer(Tcl_GetString(objv
[1]), &pPrior
) ){
374 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
377 sqlite3_free(pPrior
);
382 ** These routines are in test_hexio.c
384 int sqlite3TestHexToBin(const char *, int, char *);
385 int sqlite3TestBinToHex(char*,int);
388 ** Usage: memset ADDRESS SIZE HEX
390 ** Set a chunk of memory (obtained from malloc, probably) to a
391 ** specified hex pattern.
393 static int test_memset(
397 Tcl_Obj
*CONST objv
[]
406 Tcl_WrongNumArgs(interp
, 1, objv
, "ADDRESS SIZE HEX");
409 if( textToPointer(Tcl_GetString(objv
[1]), &p
) ){
410 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
413 if( Tcl_GetIntFromObj(interp
, objv
[2], &size
) ){
417 Tcl_AppendResult(interp
, "size must be positive", (char*)0);
420 zHex
= Tcl_GetStringFromObj(objv
[3], &n
);
421 if( n
>sizeof(zBin
)*2 ) n
= sizeof(zBin
)*2;
422 n
= sqlite3TestHexToBin(zHex
, n
, zBin
);
424 Tcl_AppendResult(interp
, "no data", (char*)0);
428 for(i
=0; i
<size
; i
++){
435 ** Usage: memget ADDRESS SIZE
437 ** Return memory as hexadecimal text.
439 static int test_memget(
443 Tcl_Obj
*CONST objv
[]
451 Tcl_WrongNumArgs(interp
, 1, objv
, "ADDRESS SIZE");
454 if( textToPointer(Tcl_GetString(objv
[1]), &p
) ){
455 Tcl_AppendResult(interp
, "bad pointer: ", Tcl_GetString(objv
[1]), (char*)0);
458 if( Tcl_GetIntFromObj(interp
, objv
[2], &size
) ){
462 Tcl_AppendResult(interp
, "size must be positive", (char*)0);
467 if( size
>(sizeof(zHex
)-1)/2 ){
468 n
= (sizeof(zHex
)-1)/2;
472 memcpy(zHex
, zBin
, n
);
475 sqlite3TestBinToHex(zHex
, n
);
476 Tcl_AppendResult(interp
, zHex
, (char*)0);
482 ** Usage: sqlite3_memory_used
484 ** Raw test interface for sqlite3_memory_used().
486 static int test_memory_used(
490 Tcl_Obj
*CONST objv
[]
492 Tcl_SetObjResult(interp
, Tcl_NewWideIntObj(sqlite3_memory_used()));
497 ** Usage: sqlite3_memory_highwater ?RESETFLAG?
499 ** Raw test interface for sqlite3_memory_highwater().
501 static int test_memory_highwater(
505 Tcl_Obj
*CONST objv
[]
508 if( objc
!=1 && objc
!=2 ){
509 Tcl_WrongNumArgs(interp
, 1, objv
, "?RESET?");
513 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &resetFlag
) ) return TCL_ERROR
;
515 Tcl_SetObjResult(interp
,
516 Tcl_NewWideIntObj(sqlite3_memory_highwater(resetFlag
)));
521 ** Usage: sqlite3_memdebug_backtrace DEPTH
523 ** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined
524 ** then this routine is a no-op.
526 static int test_memdebug_backtrace(
530 Tcl_Obj
*CONST objv
[]
534 Tcl_WrongNumArgs(interp
, 1, objv
, "DEPT");
537 if( Tcl_GetIntFromObj(interp
, objv
[1], &depth
) ) return TCL_ERROR
;
538 #ifdef SQLITE_MEMDEBUG
540 extern void sqlite3MemdebugBacktrace(int);
541 sqlite3MemdebugBacktrace(depth
);
548 ** Usage: sqlite3_memdebug_dump FILENAME
550 ** Write a summary of unfreed memory to FILENAME.
552 static int test_memdebug_dump(
556 Tcl_Obj
*CONST objv
[]
559 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME");
562 #if defined(SQLITE_MEMDEBUG) || defined(SQLITE_MEMORY_SIZE) \
563 || defined(SQLITE_POW2_MEMORY_SIZE)
565 extern void sqlite3MemdebugDump(const char*);
566 sqlite3MemdebugDump(Tcl_GetString(objv
[1]));
573 ** Usage: sqlite3_memdebug_malloc_count
575 ** Return the total number of times malloc() has been called.
577 static int test_memdebug_malloc_count(
581 Tcl_Obj
*CONST objv
[]
585 Tcl_WrongNumArgs(interp
, 1, objv
, "");
588 #if defined(SQLITE_MEMDEBUG)
590 extern int sqlite3MemdebugMallocCount();
591 nMalloc
= sqlite3MemdebugMallocCount();
594 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nMalloc
));
600 ** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS?
602 ** where options are:
605 ** -benigncnt <varname>
607 ** Arrange for a simulated malloc() failure after COUNTER successes.
608 ** If a repeat count is specified, the fault is repeated that many
611 ** Each call to this routine overrides the prior counter value.
612 ** This routine returns the number of simulated failures that have
613 ** happened since the previous call to this routine.
615 ** To disable simulated failures, use a COUNTER of -1.
617 static int test_memdebug_fail(
621 Tcl_Obj
*CONST objv
[]
626 Tcl_Obj
*pBenignCnt
= 0;
631 Tcl_WrongNumArgs(interp
, 1, objv
, "COUNTER ?OPTIONS?");
634 if( Tcl_GetIntFromObj(interp
, objv
[1], &iFail
) ) return TCL_ERROR
;
636 for(ii
=2; ii
<objc
; ii
+=2){
638 char *zOption
= Tcl_GetStringFromObj(objv
[ii
], &nOption
);
641 if( nOption
>1 && strncmp(zOption
, "-repeat", nOption
)==0 ){
643 zErr
= "option requires an argument: ";
645 if( Tcl_GetIntFromObj(interp
, objv
[ii
+1], &nRepeat
) ){
649 }else if( nOption
>1 && strncmp(zOption
, "-benigncnt", nOption
)==0 ){
651 zErr
= "option requires an argument: ";
653 pBenignCnt
= objv
[ii
+1];
656 zErr
= "unknown option: ";
660 Tcl_AppendResult(interp
, zErr
, zOption
, 0);
665 nBenign
= faultsimBenignFailures();
666 nFail
= faultsimFailures();
667 faultsimConfig(iFail
, nRepeat
);
670 Tcl_ObjSetVar2(interp
, pBenignCnt
, 0, Tcl_NewIntObj(nBenign
), 0);
672 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nFail
));
677 ** Usage: sqlite3_memdebug_pending
679 ** Return the number of malloc() calls that will succeed before a
680 ** simulated failure occurs. A negative return value indicates that
681 ** no malloc() failure is scheduled.
683 static int test_memdebug_pending(
687 Tcl_Obj
*CONST objv
[]
691 Tcl_WrongNumArgs(interp
, 1, objv
, "");
694 nPending
= faultsimPending();
695 Tcl_SetObjResult(interp
, Tcl_NewIntObj(nPending
));
700 ** The following global variable keeps track of the number of tests
701 ** that have run. This variable is only useful when running in the
704 static int sqlite3_memdebug_title_count
= 0;
707 ** Usage: sqlite3_memdebug_settitle TITLE
709 ** Set a title string stored with each allocation. The TITLE is
710 ** typically the name of the test that was running when the
711 ** allocation occurred. The TITLE is stored with the allocation
712 ** and can be used to figure out which tests are leaking memory.
714 ** Each title overwrite the previous.
716 static int test_memdebug_settitle(
720 Tcl_Obj
*CONST objv
[]
722 sqlite3_memdebug_title_count
++;
724 Tcl_WrongNumArgs(interp
, 1, objv
, "TITLE");
727 #ifdef SQLITE_MEMDEBUG
730 extern int sqlite3MemdebugSettitle(const char*);
731 zTitle
= Tcl_GetString(objv
[1]);
732 sqlite3MemdebugSettitle(zTitle
);
738 #define MALLOC_LOG_FRAMES 10
739 #define MALLOC_LOG_KEYINTS ( \
740 10 * ((sizeof(int)>=sizeof(void*)) ? 1 : sizeof(void*)/sizeof(int)) \
742 static Tcl_HashTable aMallocLog
;
743 static int mallocLogEnabled
= 0;
745 typedef struct MallocLog MallocLog
;
751 #ifdef SQLITE_MEMDEBUG
752 static void test_memdebug_callback(int nByte
, int nFrame
, void **aFrame
){
753 if( mallocLogEnabled
){
755 Tcl_HashEntry
*pEntry
;
758 int aKey
[MALLOC_LOG_KEYINTS
];
759 unsigned int nKey
= sizeof(int)*MALLOC_LOG_KEYINTS
;
761 memset(aKey
, 0, nKey
);
762 if( (sizeof(void*)*nFrame
)<nKey
){
763 nKey
= nFrame
*sizeof(void*);
765 memcpy(aKey
, aFrame
, nKey
);
767 pEntry
= Tcl_CreateHashEntry(&aMallocLog
, (const char *)aKey
, &isNew
);
769 pLog
= (MallocLog
*)Tcl_Alloc(sizeof(MallocLog
));
770 memset(pLog
, 0, sizeof(MallocLog
));
771 Tcl_SetHashValue(pEntry
, (ClientData
)pLog
);
773 pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
777 pLog
->nByte
+= nByte
;
780 #endif /* SQLITE_MEMDEBUG */
782 static void test_memdebug_log_clear(void){
783 Tcl_HashSearch search
;
784 Tcl_HashEntry
*pEntry
;
786 pEntry
=Tcl_FirstHashEntry(&aMallocLog
, &search
);
788 pEntry
=Tcl_NextHashEntry(&search
)
790 MallocLog
*pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
791 Tcl_Free((char *)pLog
);
793 Tcl_DeleteHashTable(&aMallocLog
);
794 Tcl_InitHashTable(&aMallocLog
, MALLOC_LOG_KEYINTS
);
797 static int test_memdebug_log(
801 Tcl_Obj
*CONST objv
[]
803 static int isInit
= 0;
806 static const char *MB_strs
[] = { "start", "stop", "dump", "clear", "sync" };
808 MB_LOG_START
, MB_LOG_STOP
, MB_LOG_DUMP
, MB_LOG_CLEAR
, MB_LOG_SYNC
812 #ifdef SQLITE_MEMDEBUG
813 extern void sqlite3MemdebugBacktraceCallback(
814 void (*xBacktrace
)(int, int, void **));
815 sqlite3MemdebugBacktraceCallback(test_memdebug_callback
);
817 Tcl_InitHashTable(&aMallocLog
, MALLOC_LOG_KEYINTS
);
822 Tcl_WrongNumArgs(interp
, 1, objv
, "SUB-COMMAND ...");
824 if( Tcl_GetIndexFromObj(interp
, objv
[1], MB_strs
, "sub-command", 0, &iSub
) ){
828 switch( (enum MB_enum
)iSub
){
830 mallocLogEnabled
= 1;
833 mallocLogEnabled
= 0;
836 Tcl_HashSearch search
;
837 Tcl_HashEntry
*pEntry
;
838 Tcl_Obj
*pRet
= Tcl_NewObj();
840 assert(sizeof(Tcl_WideInt
)>=sizeof(void*));
843 pEntry
=Tcl_FirstHashEntry(&aMallocLog
, &search
);
845 pEntry
=Tcl_NextHashEntry(&search
)
847 Tcl_Obj
*apElem
[MALLOC_LOG_FRAMES
+2];
848 MallocLog
*pLog
= (MallocLog
*)Tcl_GetHashValue(pEntry
);
849 Tcl_WideInt
*aKey
= (Tcl_WideInt
*)Tcl_GetHashKey(&aMallocLog
, pEntry
);
852 apElem
[0] = Tcl_NewIntObj(pLog
->nCall
);
853 apElem
[1] = Tcl_NewIntObj(pLog
->nByte
);
854 for(ii
=0; ii
<MALLOC_LOG_FRAMES
; ii
++){
855 apElem
[ii
+2] = Tcl_NewWideIntObj(aKey
[ii
]);
858 Tcl_ListObjAppendElement(interp
, pRet
,
859 Tcl_NewListObj(MALLOC_LOG_FRAMES
+2, apElem
)
863 Tcl_SetObjResult(interp
, pRet
);
867 test_memdebug_log_clear();
872 #ifdef SQLITE_MEMDEBUG
873 extern void sqlite3MemdebugSync();
874 test_memdebug_log_clear();
875 mallocLogEnabled
= 1;
876 sqlite3MemdebugSync();
886 ** Usage: sqlite3_config_scratch SIZE N
888 ** Set the scratch memory buffer using SQLITE_CONFIG_SCRATCH.
889 ** The buffer is static and is of limited size. N might be
890 ** adjusted downward as needed to accommodate the requested size.
891 ** The revised value of N is returned.
893 ** A negative SIZE causes the buffer pointer to be NULL.
895 static int test_config_scratch(
899 Tcl_Obj
*CONST objv
[]
903 static char *buf
= 0;
905 Tcl_WrongNumArgs(interp
, 1, objv
, "SIZE N");
908 if( Tcl_GetIntFromObj(interp
, objv
[1], &sz
) ) return TCL_ERROR
;
909 if( Tcl_GetIntFromObj(interp
, objv
[2], &N
) ) return TCL_ERROR
;
913 rc
= sqlite3_config(SQLITE_CONFIG_SCRATCH
, 0, 0, 0);
915 buf
= malloc( sz
*N
+ 1 );
916 rc
= sqlite3_config(SQLITE_CONFIG_SCRATCH
, buf
, sz
, N
);
918 pResult
= Tcl_NewObj();
919 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
920 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(N
));
921 Tcl_SetObjResult(interp
, pResult
);
926 ** Usage: sqlite3_config_pagecache SIZE N
928 ** Set the page-cache memory buffer using SQLITE_CONFIG_PAGECACHE.
929 ** The buffer is static and is of limited size. N might be
930 ** adjusted downward as needed to accommodate the requested size.
931 ** The revised value of N is returned.
933 ** A negative SIZE causes the buffer pointer to be NULL.
935 static int test_config_pagecache(
939 Tcl_Obj
*CONST objv
[]
943 static char *buf
= 0;
945 Tcl_WrongNumArgs(interp
, 1, objv
, "SIZE N");
948 if( Tcl_GetIntFromObj(interp
, objv
[1], &sz
) ) return TCL_ERROR
;
949 if( Tcl_GetIntFromObj(interp
, objv
[2], &N
) ) return TCL_ERROR
;
953 rc
= sqlite3_config(SQLITE_CONFIG_PAGECACHE
, 0, 0, 0);
955 buf
= malloc( sz
*N
);
956 rc
= sqlite3_config(SQLITE_CONFIG_PAGECACHE
, buf
, sz
, N
);
958 pResult
= Tcl_NewObj();
959 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
960 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(N
));
961 Tcl_SetObjResult(interp
, pResult
);
966 ** Usage: sqlite3_config_alt_pcache INSTALL_FLAG DISCARD_CHANCE PRNG_SEED
968 ** Set up the alternative test page cache. Install if INSTALL_FLAG is
969 ** true and uninstall (reverting to the default page cache) if INSTALL_FLAG
970 ** is false. DISCARD_CHANGE is an integer between 0 and 100 inclusive
971 ** which determines the chance of discarding a page when unpinned. 100
972 ** is certainty. 0 is never. PRNG_SEED is the pseudo-random number generator
975 static int test_alt_pcache(
979 Tcl_Obj
*CONST objv
[]
982 int discardChance
= 0;
985 extern void installTestPCache(int,unsigned,unsigned,unsigned);
986 if( objc
<2 || objc
>5 ){
987 Tcl_WrongNumArgs(interp
, 1, objv
,
988 "INSTALLFLAG DISCARDCHANCE PRNGSEEED HIGHSTRESS");
991 if( Tcl_GetIntFromObj(interp
, objv
[1], &installFlag
) ) return TCL_ERROR
;
992 if( objc
>=3 && Tcl_GetIntFromObj(interp
, objv
[2], &discardChance
) ){
995 if( objc
>=4 && Tcl_GetIntFromObj(interp
, objv
[3], &prngSeed
) ){
998 if( objc
>=5 && Tcl_GetIntFromObj(interp
, objv
[4], &highStress
) ){
1001 if( discardChance
<0 || discardChance
>100 ){
1002 Tcl_AppendResult(interp
, "discard-chance should be between 0 and 100",
1006 installTestPCache(installFlag
, (unsigned)discardChance
, (unsigned)prngSeed
,
1007 (unsigned)highStress
);
1012 ** Usage: sqlite3_config_memstatus BOOLEAN
1014 ** Enable or disable memory status reporting using SQLITE_CONFIG_MEMSTATUS.
1016 static int test_config_memstatus(
1020 Tcl_Obj
*CONST objv
[]
1024 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOLEAN");
1027 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &enable
) ) return TCL_ERROR
;
1028 rc
= sqlite3_config(SQLITE_CONFIG_MEMSTATUS
, enable
);
1029 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
1034 ** Usage: sqlite3_config_lookaside SIZE COUNT
1037 static int test_config_lookaside(
1041 Tcl_Obj
*CONST objv
[]
1046 Tcl_WrongNumArgs(interp
, 1, objv
, "SIZE COUNT");
1049 if( Tcl_GetIntFromObj(interp
, objv
[1], &sz
) ) return TCL_ERROR
;
1050 if( Tcl_GetIntFromObj(interp
, objv
[2], &cnt
) ) return TCL_ERROR
;
1051 pRet
= Tcl_NewObj();
1052 Tcl_ListObjAppendElement(
1053 interp
, pRet
, Tcl_NewIntObj(sqlite3GlobalConfig
.szLookaside
)
1055 Tcl_ListObjAppendElement(
1056 interp
, pRet
, Tcl_NewIntObj(sqlite3GlobalConfig
.nLookaside
)
1058 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, cnt
);
1059 Tcl_SetObjResult(interp
, pRet
);
1065 ** Usage: sqlite3_db_config_lookaside CONNECTION BUFID SIZE COUNT
1067 ** There are two static buffers with BUFID 1 and 2. Each static buffer
1068 ** is 10KB in size. A BUFID of 0 indicates that the buffer should be NULL
1069 ** which will cause sqlite3_db_config() to allocate space on its own.
1071 static int test_db_config_lookaside(
1075 Tcl_Obj
*CONST objv
[]
1081 static char azBuf
[2][10000];
1082 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1084 Tcl_WrongNumArgs(interp
, 1, objv
, "BUFID SIZE COUNT");
1087 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1088 if( Tcl_GetIntFromObj(interp
, objv
[2], &bufid
) ) return TCL_ERROR
;
1089 if( Tcl_GetIntFromObj(interp
, objv
[3], &sz
) ) return TCL_ERROR
;
1090 if( Tcl_GetIntFromObj(interp
, objv
[4], &cnt
) ) return TCL_ERROR
;
1092 rc
= sqlite3_db_config(db
, SQLITE_DBCONFIG_LOOKASIDE
, 0, sz
, cnt
);
1093 }else if( bufid
>=1 && bufid
<=2 && sz
*cnt
<=sizeof(azBuf
[0]) ){
1094 rc
= sqlite3_db_config(db
, SQLITE_DBCONFIG_LOOKASIDE
, azBuf
[bufid
], sz
,cnt
);
1096 Tcl_AppendResult(interp
, "illegal arguments - see documentation", (char*)0);
1099 Tcl_SetObjResult(interp
, Tcl_NewIntObj(rc
));
1104 ** Usage: sqlite3_config_heap NBYTE NMINALLOC
1106 static int test_config_heap(
1110 Tcl_Obj
*CONST objv
[]
1112 static char *zBuf
; /* Use this memory */
1113 int nByte
; /* Size of buffer to pass to sqlite3_config() */
1114 int nMinAlloc
; /* Size of minimum allocation */
1115 int rc
; /* Return code of sqlite3_config() */
1117 Tcl_Obj
* CONST
*aArg
= &objv
[1];
1121 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTE NMINALLOC");
1124 if( Tcl_GetIntFromObj(interp
, aArg
[0], &nByte
) ) return TCL_ERROR
;
1125 if( Tcl_GetIntFromObj(interp
, aArg
[1], &nMinAlloc
) ) return TCL_ERROR
;
1130 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, (void*)0, 0, 0);
1132 zBuf
= realloc(zBuf
, nByte
);
1133 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, zBuf
, nByte
, nMinAlloc
);
1136 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1141 ** Usage: sqlite3_config_heap_size NBYTE
1143 static int test_config_heap_size(
1147 Tcl_Obj
*CONST objv
[]
1149 int nByte
; /* Size to pass to sqlite3_config() */
1150 int rc
; /* Return code of sqlite3_config() */
1152 Tcl_Obj
* CONST
*aArg
= &objv
[1];
1156 Tcl_WrongNumArgs(interp
, 1, objv
, "NBYTE");
1159 if( Tcl_GetIntFromObj(interp
, aArg
[0], &nByte
) ) return TCL_ERROR
;
1161 rc
= sqlite3_config(SQLITE_CONFIG_WIN32_HEAPSIZE
, nByte
);
1163 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1168 ** Usage: sqlite3_config_error [DB]
1170 ** Invoke sqlite3_config() or sqlite3_db_config() with invalid
1171 ** opcodes and verify that they return errors.
1173 static int test_config_error(
1177 Tcl_Obj
*CONST objv
[]
1180 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1182 if( objc
!=2 && objc
!=1 ){
1183 Tcl_WrongNumArgs(interp
, 1, objv
, "[DB]");
1187 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1188 if( sqlite3_db_config(db
, 99999)!=SQLITE_ERROR
){
1189 Tcl_AppendResult(interp
,
1190 "sqlite3_db_config(db, 99999) does not return SQLITE_ERROR",
1195 if( sqlite3_config(99999)!=SQLITE_ERROR
){
1196 Tcl_AppendResult(interp
,
1197 "sqlite3_config(99999) does not return SQLITE_ERROR",
1206 ** Usage: sqlite3_config_uri BOOLEAN
1208 ** Enables or disables interpretation of URI parameters by default using
1209 ** SQLITE_CONFIG_URI.
1211 static int test_config_uri(
1215 Tcl_Obj
*CONST objv
[]
1221 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
1224 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &bOpenUri
) ){
1228 rc
= sqlite3_config(SQLITE_CONFIG_URI
, bOpenUri
);
1229 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1235 ** Usage: sqlite3_config_cis BOOLEAN
1237 ** Enables or disables the use of the covering-index scan optimization.
1238 ** SQLITE_CONFIG_COVERING_INDEX_SCAN.
1240 static int test_config_cis(
1244 Tcl_Obj
*CONST objv
[]
1250 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOL");
1253 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &bUseCis
) ){
1257 rc
= sqlite3_config(SQLITE_CONFIG_COVERING_INDEX_SCAN
, bUseCis
);
1258 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1265 ** Usage: sqlite3_dump_memsys3 FILENAME
1266 ** sqlite3_dump_memsys5 FILENAME
1268 ** Write a summary of unfreed memsys3 allocations to FILENAME.
1270 static int test_dump_memsys3(
1274 Tcl_Obj
*CONST objv
[]
1277 Tcl_WrongNumArgs(interp
, 1, objv
, "FILENAME");
1281 switch( SQLITE_PTR_TO_INT(clientData
) ){
1283 #ifdef SQLITE_ENABLE_MEMSYS3
1284 extern void sqlite3Memsys3Dump(const char*);
1285 sqlite3Memsys3Dump(Tcl_GetString(objv
[1]));
1290 #ifdef SQLITE_ENABLE_MEMSYS5
1291 extern void sqlite3Memsys5Dump(const char*);
1292 sqlite3Memsys5Dump(Tcl_GetString(objv
[1]));
1301 ** Usage: sqlite3_status OPCODE RESETFLAG
1303 ** Return a list of three elements which are the sqlite3_status() return
1304 ** code, the current value, and the high-water mark value.
1306 static int test_status(
1310 Tcl_Obj
*CONST objv
[]
1312 int rc
, iValue
, mxValue
;
1313 int i
, op
, resetFlag
;
1314 const char *zOpName
;
1315 static const struct {
1319 { "SQLITE_STATUS_MEMORY_USED", SQLITE_STATUS_MEMORY_USED
},
1320 { "SQLITE_STATUS_MALLOC_SIZE", SQLITE_STATUS_MALLOC_SIZE
},
1321 { "SQLITE_STATUS_PAGECACHE_USED", SQLITE_STATUS_PAGECACHE_USED
},
1322 { "SQLITE_STATUS_PAGECACHE_OVERFLOW", SQLITE_STATUS_PAGECACHE_OVERFLOW
},
1323 { "SQLITE_STATUS_PAGECACHE_SIZE", SQLITE_STATUS_PAGECACHE_SIZE
},
1324 { "SQLITE_STATUS_SCRATCH_USED", SQLITE_STATUS_SCRATCH_USED
},
1325 { "SQLITE_STATUS_SCRATCH_OVERFLOW", SQLITE_STATUS_SCRATCH_OVERFLOW
},
1326 { "SQLITE_STATUS_SCRATCH_SIZE", SQLITE_STATUS_SCRATCH_SIZE
},
1327 { "SQLITE_STATUS_PARSER_STACK", SQLITE_STATUS_PARSER_STACK
},
1328 { "SQLITE_STATUS_MALLOC_COUNT", SQLITE_STATUS_MALLOC_COUNT
},
1332 Tcl_WrongNumArgs(interp
, 1, objv
, "PARAMETER RESETFLAG");
1335 zOpName
= Tcl_GetString(objv
[1]);
1336 for(i
=0; i
<ArraySize(aOp
); i
++){
1337 if( strcmp(aOp
[i
].zName
, zOpName
)==0 ){
1342 if( i
>=ArraySize(aOp
) ){
1343 if( Tcl_GetIntFromObj(interp
, objv
[1], &op
) ) return TCL_ERROR
;
1345 if( Tcl_GetBooleanFromObj(interp
, objv
[2], &resetFlag
) ) return TCL_ERROR
;
1348 rc
= sqlite3_status(op
, &iValue
, &mxValue
, resetFlag
);
1349 pResult
= Tcl_NewObj();
1350 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
1351 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(iValue
));
1352 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(mxValue
));
1353 Tcl_SetObjResult(interp
, pResult
);
1358 ** Usage: sqlite3_db_status DATABASE OPCODE RESETFLAG
1360 ** Return a list of three elements which are the sqlite3_db_status() return
1361 ** code, the current value, and the high-water mark value.
1363 static int test_db_status(
1367 Tcl_Obj
*CONST objv
[]
1369 int rc
, iValue
, mxValue
;
1370 int i
, op
, resetFlag
;
1371 const char *zOpName
;
1373 extern int getDbPointer(Tcl_Interp
*, const char*, sqlite3
**);
1374 static const struct {
1378 { "LOOKASIDE_USED", SQLITE_DBSTATUS_LOOKASIDE_USED
},
1379 { "CACHE_USED", SQLITE_DBSTATUS_CACHE_USED
},
1380 { "SCHEMA_USED", SQLITE_DBSTATUS_SCHEMA_USED
},
1381 { "STMT_USED", SQLITE_DBSTATUS_STMT_USED
},
1382 { "LOOKASIDE_HIT", SQLITE_DBSTATUS_LOOKASIDE_HIT
},
1383 { "LOOKASIDE_MISS_SIZE", SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
},
1384 { "LOOKASIDE_MISS_FULL", SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
},
1385 { "CACHE_HIT", SQLITE_DBSTATUS_CACHE_HIT
},
1386 { "CACHE_MISS", SQLITE_DBSTATUS_CACHE_MISS
},
1387 { "CACHE_WRITE", SQLITE_DBSTATUS_CACHE_WRITE
},
1388 { "DEFERRED_FKS", SQLITE_DBSTATUS_DEFERRED_FKS
}
1392 Tcl_WrongNumArgs(interp
, 1, objv
, "DB PARAMETER RESETFLAG");
1395 if( getDbPointer(interp
, Tcl_GetString(objv
[1]), &db
) ) return TCL_ERROR
;
1396 zOpName
= Tcl_GetString(objv
[2]);
1397 if( memcmp(zOpName
, "SQLITE_", 7)==0 ) zOpName
+= 7;
1398 if( memcmp(zOpName
, "DBSTATUS_", 9)==0 ) zOpName
+= 9;
1399 for(i
=0; i
<ArraySize(aOp
); i
++){
1400 if( strcmp(aOp
[i
].zName
, zOpName
)==0 ){
1405 if( i
>=ArraySize(aOp
) ){
1406 if( Tcl_GetIntFromObj(interp
, objv
[2], &op
) ) return TCL_ERROR
;
1408 if( Tcl_GetBooleanFromObj(interp
, objv
[3], &resetFlag
) ) return TCL_ERROR
;
1411 rc
= sqlite3_db_status(db
, op
, &iValue
, &mxValue
, resetFlag
);
1412 pResult
= Tcl_NewObj();
1413 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(rc
));
1414 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(iValue
));
1415 Tcl_ListObjAppendElement(0, pResult
, Tcl_NewIntObj(mxValue
));
1416 Tcl_SetObjResult(interp
, pResult
);
1421 ** install_malloc_faultsim BOOLEAN
1423 static int test_install_malloc_faultsim(
1427 Tcl_Obj
*CONST objv
[]
1433 Tcl_WrongNumArgs(interp
, 1, objv
, "BOOLEAN");
1436 if( TCL_OK
!=Tcl_GetBooleanFromObj(interp
, objv
[1], &isInstall
) ){
1439 rc
= faultsimInstall(isInstall
);
1440 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1445 ** sqlite3_install_memsys3
1447 static int test_install_memsys3(
1451 Tcl_Obj
*CONST objv
[]
1453 int rc
= SQLITE_MISUSE
;
1454 #ifdef SQLITE_ENABLE_MEMSYS3
1455 const sqlite3_mem_methods
*sqlite3MemGetMemsys3(void);
1456 rc
= sqlite3_config(SQLITE_CONFIG_MALLOC
, sqlite3MemGetMemsys3());
1458 Tcl_SetResult(interp
, (char *)sqlite3ErrName(rc
), TCL_VOLATILE
);
1462 static int test_vfs_oom_test(
1466 Tcl_Obj
*CONST objv
[]
1468 extern int sqlite3_memdebug_vfs_oom_test
;
1470 Tcl_WrongNumArgs(interp
, 1, objv
, "?INTEGER?");
1472 }else if( objc
==2 ){
1474 if( Tcl_GetIntFromObj(interp
, objv
[1], &iNew
) ) return TCL_ERROR
;
1475 sqlite3_memdebug_vfs_oom_test
= iNew
;
1477 Tcl_SetObjResult(interp
, Tcl_NewIntObj(sqlite3_memdebug_vfs_oom_test
));
1482 ** Register commands with the TCL interpreter.
1484 int Sqlitetest_malloc_Init(Tcl_Interp
*interp
){
1487 Tcl_ObjCmdProc
*xProc
;
1490 { "sqlite3_malloc", test_malloc
,0 },
1491 { "sqlite3_realloc", test_realloc
,0 },
1492 { "sqlite3_free", test_free
,0 },
1493 { "memset", test_memset
,0 },
1494 { "memget", test_memget
,0 },
1495 { "sqlite3_memory_used", test_memory_used
,0 },
1496 { "sqlite3_memory_highwater", test_memory_highwater
,0 },
1497 { "sqlite3_memdebug_backtrace", test_memdebug_backtrace
,0 },
1498 { "sqlite3_memdebug_dump", test_memdebug_dump
,0 },
1499 { "sqlite3_memdebug_fail", test_memdebug_fail
,0 },
1500 { "sqlite3_memdebug_pending", test_memdebug_pending
,0 },
1501 { "sqlite3_memdebug_settitle", test_memdebug_settitle
,0 },
1502 { "sqlite3_memdebug_malloc_count", test_memdebug_malloc_count
,0 },
1503 { "sqlite3_memdebug_log", test_memdebug_log
,0 },
1504 { "sqlite3_config_scratch", test_config_scratch
,0 },
1505 { "sqlite3_config_pagecache", test_config_pagecache
,0 },
1506 { "sqlite3_config_alt_pcache", test_alt_pcache
,0 },
1507 { "sqlite3_status", test_status
,0 },
1508 { "sqlite3_db_status", test_db_status
,0 },
1509 { "install_malloc_faultsim", test_install_malloc_faultsim
,0 },
1510 { "sqlite3_config_heap", test_config_heap
,0 },
1511 { "sqlite3_config_heap_size", test_config_heap_size
,0 },
1512 { "sqlite3_config_memstatus", test_config_memstatus
,0 },
1513 { "sqlite3_config_lookaside", test_config_lookaside
,0 },
1514 { "sqlite3_config_error", test_config_error
,0 },
1515 { "sqlite3_config_uri", test_config_uri
,0 },
1516 { "sqlite3_config_cis", test_config_cis
,0 },
1517 { "sqlite3_db_config_lookaside",test_db_config_lookaside
,0 },
1518 { "sqlite3_dump_memsys3", test_dump_memsys3
,3 },
1519 { "sqlite3_dump_memsys5", test_dump_memsys3
,5 },
1520 { "sqlite3_install_memsys3", test_install_memsys3
,0 },
1521 { "sqlite3_memdebug_vfs_oom_test", test_vfs_oom_test
,0 },
1524 for(i
=0; i
<sizeof(aObjCmd
)/sizeof(aObjCmd
[0]); i
++){
1525 ClientData c
= (ClientData
)SQLITE_INT_TO_PTR(aObjCmd
[i
].clientData
);
1526 Tcl_CreateObjCommand(interp
, aObjCmd
[i
].zName
, aObjCmd
[i
].xProc
, c
, 0);