xref: /sqlite-3.40.0/test/malloc_common.tcl (revision f703b42d)
15a3032b3Sdrh# 2007 May 05
25a3032b3Sdrh#
35a3032b3Sdrh# The author disclaims copyright to this source code.  In place of
45a3032b3Sdrh# a legal notice, here is a blessing:
55a3032b3Sdrh#
65a3032b3Sdrh#    May you do good and not evil.
75a3032b3Sdrh#    May you find forgiveness for yourself and forgive others.
85a3032b3Sdrh#    May you share freely, never taking more than you give.
95a3032b3Sdrh#
105a3032b3Sdrh#***********************************************************************
115a3032b3Sdrh#
125a3032b3Sdrh# This file contains common code used by many different malloc tests
135a3032b3Sdrh# within the test suite.
145a3032b3Sdrh#
1598c21903Sdanielk1977# $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $
16c9cf901dSdanielk1977
175a3032b3Sdrh# If we did not compile with malloc testing enabled, then do nothing.
185a3032b3Sdrh#
193088d59eSdrhifcapable builtin_test {
205efaf070Sdrh  set MEMDEBUG 1
215efaf070Sdrh} else {
22eee4c8caSdrh  set MEMDEBUG 0
23369ff42eSdanielk1977  return 0
24cdc3a6bcSdanielk1977}
25cdc3a6bcSdanielk1977
261f55e28dSdan# Transient and persistent OOM errors:
271f55e28dSdan#
281f55e28dSdanset FAULTSIM(oom-transient) [list          \
291f55e28dSdan  -injectstart   {oom_injectstart 0}       \
301f55e28dSdan  -injectstop    oom_injectstop            \
311f55e28dSdan  -injecterrlist {{1 {out of memory}}}     \
321f55e28dSdan]
331f55e28dSdanset FAULTSIM(oom-persistent) [list         \
341f55e28dSdan  -injectstart {oom_injectstart 1000000}   \
351f55e28dSdan  -injectstop oom_injectstop               \
361f55e28dSdan  -injecterrlist {{1 {out of memory}}}     \
371f55e28dSdan]
381f55e28dSdan
391f55e28dSdan# Transient and persistent IO errors:
401f55e28dSdan#
411f55e28dSdanset FAULTSIM(ioerr-transient) [list        \
421f55e28dSdan  -injectstart   {ioerr_injectstart 0}     \
431f55e28dSdan  -injectstop    ioerr_injectstop          \
441f55e28dSdan  -injecterrlist {{1 {disk I/O error}}}    \
451f55e28dSdan]
461f55e28dSdanset FAULTSIM(ioerr-persistent) [list       \
471f55e28dSdan  -injectstart   {ioerr_injectstart 1}     \
481f55e28dSdan  -injectstop    ioerr_injectstop          \
491f55e28dSdan  -injecterrlist {{1 {disk I/O error}}}    \
501f55e28dSdan]
511f55e28dSdan
52146ed78bSdan# SQLITE_FULL errors (always persistent):
53146ed78bSdan#
54146ed78bSdanset FAULTSIM(full) [list                   \
55146ed78bSdan  -injectinstall   fullerr_injectinstall   \
56146ed78bSdan  -injectstart     fullerr_injectstart     \
57146ed78bSdan  -injectstop      fullerr_injectstop      \
58146ed78bSdan  -injecterrlist   {{1 {database or disk is full}}} \
59146ed78bSdan  -injectuninstall fullerr_injectuninstall \
60146ed78bSdan]
61146ed78bSdan
621f55e28dSdan# Transient and persistent SHM errors:
631f55e28dSdan#
641f55e28dSdanset FAULTSIM(shmerr-transient) [list       \
651f55e28dSdan  -injectinstall   shmerr_injectinstall    \
661f55e28dSdan  -injectstart     {shmerr_injectstart 0}  \
671f55e28dSdan  -injectstop      shmerr_injectstop       \
681f55e28dSdan  -injecterrlist   {{1 {disk I/O error}}}  \
691f55e28dSdan  -injectuninstall shmerr_injectuninstall  \
701f55e28dSdan]
711f55e28dSdanset FAULTSIM(shmerr-persistent) [list      \
721f55e28dSdan  -injectinstall   shmerr_injectinstall    \
731f55e28dSdan  -injectstart     {shmerr_injectstart 1}  \
741f55e28dSdan  -injectstop      shmerr_injectstop       \
751f55e28dSdan  -injecterrlist   {{1 {disk I/O error}}}  \
761f55e28dSdan  -injectuninstall shmerr_injectuninstall  \
771f55e28dSdan]
781f55e28dSdan
79f9b4419dSdan# Transient and persistent CANTOPEN errors:
80f9b4419dSdan#
81f9b4419dSdanset FAULTSIM(cantopen-transient) [list       \
82f9b4419dSdan  -injectinstall   cantopen_injectinstall    \
83f9b4419dSdan  -injectstart     {cantopen_injectstart 0}  \
84f9b4419dSdan  -injectstop      cantopen_injectstop       \
85f9b4419dSdan  -injecterrlist   {{1 {unable to open database file}}}  \
86f9b4419dSdan  -injectuninstall cantopen_injectuninstall  \
87f9b4419dSdan]
88f9b4419dSdanset FAULTSIM(cantopen-persistent) [list      \
89f9b4419dSdan  -injectinstall   cantopen_injectinstall    \
90f9b4419dSdan  -injectstart     {cantopen_injectstart 1}  \
91f9b4419dSdan  -injectstop      cantopen_injectstop       \
92f9b4419dSdan  -injecterrlist   {{1 {unable to open database file}}}  \
93f9b4419dSdan  -injectuninstall cantopen_injectuninstall  \
94f9b4419dSdan]
95f9b4419dSdan
9640449062Sdanset FAULTSIM(interrupt) [list                 \
9740449062Sdan  -injectinstall   interrupt_injectinstall    \
9840449062Sdan  -injectstart     interrupt_injectstart      \
9940449062Sdan  -injectstop      interrupt_injectstop       \
10040449062Sdan  -injecterrlist   {{1 interrupted} {1 interrupt}}        \
10140449062Sdan  -injectuninstall interrupt_injectuninstall  \
10240449062Sdan]
10340449062Sdan
1041f55e28dSdan
105a4bc123fSdan
106a4bc123fSdan#--------------------------------------------------------------------------
107a4bc123fSdan# Usage do_faultsim_test NAME ?OPTIONS...?
108a4bc123fSdan#
109a4bc123fSdan#     -faults           List of fault types to simulate.
110a4bc123fSdan#
111a4bc123fSdan#     -prep             Script to execute before -body.
112a4bc123fSdan#
113a4bc123fSdan#     -body             Script to execute (with fault injection).
114a4bc123fSdan#
115a4bc123fSdan#     -test             Script to execute after -body.
116a4bc123fSdan#
117f3aef49cSdan#     -install          Script to execute after faultsim -injectinstall
118f3aef49cSdan#
119f3aef49cSdan#     -uninstall        Script to execute after faultsim -uninjectinstall
120f3aef49cSdan#
121a4bc123fSdanproc do_faultsim_test {name args} {
1221f55e28dSdan  global FAULTSIM
1231f55e28dSdan
12422c745a9Sdan  foreach n [array names FAULTSIM] {
12522c745a9Sdan    if {$n != "interrupt"} {lappend DEFAULT(-faults) $n}
12622c745a9Sdan  }
127a4bc123fSdan  set DEFAULT(-prep)          ""
128a4bc123fSdan  set DEFAULT(-body)          ""
129a4bc123fSdan  set DEFAULT(-test)          ""
130f3aef49cSdan  set DEFAULT(-install)       ""
131f3aef49cSdan  set DEFAULT(-uninstall)     ""
132e4bec379Sdan  set DEFAULT(-start)          1
133e4bec379Sdan  set DEFAULT(-end)            0
134a4bc123fSdan
135ddf80eb8Sdan  fix_testname name
136ddf80eb8Sdan
137a4bc123fSdan  array set O [array get DEFAULT]
138a4bc123fSdan  array set O $args
139a4bc123fSdan  foreach o [array names O] {
140a4bc123fSdan    if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
141a4bc123fSdan  }
142a4bc123fSdan
1431f55e28dSdan  set faultlist [list]
144a4bc123fSdan  foreach f $O(-faults) {
1451f55e28dSdan    set flist [array names FAULTSIM $f]
1461f55e28dSdan    if {[llength $flist]==0} { error "unknown fault: $f" }
1471f55e28dSdan    set faultlist [concat $faultlist $flist]
148a4bc123fSdan  }
1491f55e28dSdan
150f3aef49cSdan  set testspec [list -prep $O(-prep) -body $O(-body) \
151e4bec379Sdan      -test $O(-test) -install $O(-install) -uninstall $O(-uninstall) \
152e4bec379Sdan      -start $O(-start) -end $O(-end)
153f3aef49cSdan  ]
1541f55e28dSdan  foreach f [lsort -unique $faultlist] {
1551f55e28dSdan    eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
156a4bc123fSdan  }
157a4bc123fSdan}
158a4bc123fSdan
159ccc7ff4dSdan
160a4bc123fSdan#-------------------------------------------------------------------------
161a4bc123fSdan# Procedures to save and restore the current file-system state:
162a4bc123fSdan#
163b0ac3e3aSdan#   faultsim_save
16450833e32Sdan#   faultsim_restore
165a4bc123fSdan#   faultsim_save_and_close
166a4bc123fSdan#   faultsim_restore_and_reopen
167b0ac3e3aSdan#   faultsim_delete_and_reopen
168a4bc123fSdan#
169ccc7ff4dSdanproc faultsim_save {args} { uplevel db_save $args }
170ccc7ff4dSdanproc faultsim_save_and_close {args} { uplevel db_save_and_close $args }
171ccc7ff4dSdanproc faultsim_restore {args} { uplevel db_restore $args }
172ccc7ff4dSdanproc faultsim_restore_and_reopen {args} {
173ccc7ff4dSdan  uplevel db_restore_and_reopen $args
174ccc7ff4dSdan  sqlite3_extended_result_codes db 1
175ccc7ff4dSdan  sqlite3_db_config_lookaside db 0 0 0
176b0ac3e3aSdan}
177ccc7ff4dSdanproc faultsim_delete_and_reopen {args} {
178ccc7ff4dSdan  uplevel db_delete_and_reopen $args
179a4bc123fSdan  sqlite3_extended_result_codes db 1
180a4bc123fSdan  sqlite3_db_config_lookaside db 0 0 0
181a4bc123fSdan}
182a4bc123fSdan
1831f55e28dSdanproc faultsim_integrity_check {{db db}} {
1841f55e28dSdan  set ic [$db eval { PRAGMA integrity_check }]
1851f55e28dSdan  if {$ic != "ok"} { error "Integrity check: $ic" }
1861f55e28dSdan}
187a4bc123fSdan
1881f55e28dSdan
1891f55e28dSdan# The following procs are used as [do_one_faultsim_test] callbacks when
1901f55e28dSdan# injecting OOM faults into test cases.
1918521abfdSdan#
1928521abfdSdanproc oom_injectstart {nRepeat iFail} {
1937bddb755Sdan  sqlite3_memdebug_fail [expr $iFail-1] -repeat $nRepeat
1948521abfdSdan}
1958521abfdSdanproc oom_injectstop {} {
1968521abfdSdan  sqlite3_memdebug_fail -1
1978521abfdSdan}
1988521abfdSdan
1991f55e28dSdan# The following procs are used as [do_one_faultsim_test] callbacks when
2001f55e28dSdan# injecting IO error faults into test cases.
2011f55e28dSdan#
202a4bc123fSdanproc ioerr_injectstart {persist iFail} {
203a4bc123fSdan  set ::sqlite_io_error_persist $persist
204a4bc123fSdan  set ::sqlite_io_error_pending $iFail
205a4bc123fSdan}
206a4bc123fSdanproc ioerr_injectstop {} {
207a4bc123fSdan  set sv $::sqlite_io_error_hit
208a4bc123fSdan  set ::sqlite_io_error_persist 0
209a4bc123fSdan  set ::sqlite_io_error_pending 0
210a4bc123fSdan  set ::sqlite_io_error_hardhit 0
211a4bc123fSdan  set ::sqlite_io_error_hit     0
212a4bc123fSdan  set ::sqlite_io_error_pending 0
213a4bc123fSdan  return $sv
214a4bc123fSdan}
215a4bc123fSdan
2161f55e28dSdan# The following procs are used as [do_one_faultsim_test] callbacks when
2171f55e28dSdan# injecting shared-memory related error faults into test cases.
2181f55e28dSdan#
2191f55e28dSdanproc shmerr_injectinstall {} {
2201f55e28dSdan  testvfs shmfault -default true
221346e4267Sdan  shmfault filter {xShmOpen xShmMap xShmLock}
2221f55e28dSdan}
2231f55e28dSdanproc shmerr_injectuninstall {} {
2241f55e28dSdan  catch {db  close}
2251f55e28dSdan  catch {db2 close}
2261f55e28dSdan  shmfault delete
2271f55e28dSdan}
2281f55e28dSdanproc shmerr_injectstart {persist iFail} {
2291f55e28dSdan  shmfault ioerr $iFail $persist
2301f55e28dSdan}
2311f55e28dSdanproc shmerr_injectstop {} {
232f9b4419dSdan  shmfault ioerr
2331f55e28dSdan}
2341f55e28dSdan
235dca321aeSdan# The following procs are used as [do_one_faultsim_test] callbacks when
236dca321aeSdan# injecting SQLITE_FULL error faults into test cases.
237dca321aeSdan#
238146ed78bSdanproc fullerr_injectinstall {} {
239146ed78bSdan  testvfs shmfault -default true
240146ed78bSdan}
241146ed78bSdanproc fullerr_injectuninstall {} {
242146ed78bSdan  catch {db  close}
243146ed78bSdan  catch {db2 close}
244146ed78bSdan  shmfault delete
245146ed78bSdan}
246146ed78bSdanproc fullerr_injectstart {iFail} {
247f9b4419dSdan  shmfault full $iFail 1
248146ed78bSdan}
249146ed78bSdanproc fullerr_injectstop {} {
250f9b4419dSdan  shmfault full
251f9b4419dSdan}
252f9b4419dSdan
253f9b4419dSdan# The following procs are used as [do_one_faultsim_test] callbacks when
254f9b4419dSdan# injecting SQLITE_CANTOPEN error faults into test cases.
255f9b4419dSdan#
256f9b4419dSdanproc cantopen_injectinstall {} {
257f9b4419dSdan  testvfs shmfault -default true
258f9b4419dSdan}
259f9b4419dSdanproc cantopen_injectuninstall {} {
260f9b4419dSdan  catch {db  close}
261f9b4419dSdan  catch {db2 close}
262f9b4419dSdan  shmfault delete
263f9b4419dSdan}
264f9b4419dSdanproc cantopen_injectstart {persist iFail} {
265f9b4419dSdan  shmfault cantopen $iFail $persist
266f9b4419dSdan}
267f9b4419dSdanproc cantopen_injectstop {} {
268f9b4419dSdan  shmfault cantopen
269146ed78bSdan}
270146ed78bSdan
27140449062Sdan# The following procs are used as [do_one_faultsim_test] callbacks
27240449062Sdan# when injecting SQLITE_INTERRUPT error faults into test cases.
27340449062Sdan#
27440449062Sdanproc interrupt_injectinstall {} {
27540449062Sdan}
27640449062Sdanproc interrupt_injectuninstall {} {
27740449062Sdan}
27840449062Sdanproc interrupt_injectstart {iFail} {
27940449062Sdan  set ::sqlite_interrupt_count $iFail
28040449062Sdan}
28140449062Sdanproc interrupt_injectstop {} {
28240449062Sdan  set res [expr $::sqlite_interrupt_count<=0]
28340449062Sdan  set ::sqlite_interrupt_count 0
28440449062Sdan  set res
28540449062Sdan}
28640449062Sdan
287a4bc123fSdan# This command is not called directly. It is used by the
288a4bc123fSdan# [faultsim_test_result] command created by [do_faultsim_test] and used
289a4bc123fSdan# by -test scripts.
2908521abfdSdan#
291a4bc123fSdanproc faultsim_test_result_int {args} {
2928521abfdSdan  upvar testrc testrc testresult testresult testnfail testnfail
2938521abfdSdan  set t [list $testrc $testresult]
294a4bc123fSdan  set r $args
295e0569a4aSdan  if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch -exact $r $t]<0 } {
296893c0ffcSdan    error "nfail=$testnfail rc=$testrc result=$testresult list=$r"
2978521abfdSdan  }
2988521abfdSdan}
2998521abfdSdan
300a4bc123fSdan#--------------------------------------------------------------------------
301a4bc123fSdan# Usage do_one_faultsim_test NAME ?OPTIONS...?
3028521abfdSdan#
3038521abfdSdan# The first argument, <test number>, is used as a prefix of the test names
3048521abfdSdan# taken by tests executed by this command. Options are as follows. All
3058521abfdSdan# options take a single argument.
3068521abfdSdan#
3078521abfdSdan#     -injectstart      Script to enable fault-injection.
3088521abfdSdan#
3098521abfdSdan#     -injectstop       Script to disable fault-injection.
3108521abfdSdan#
311a4bc123fSdan#     -injecterrlist    List of generally acceptable test results (i.e. error
312a4bc123fSdan#                       messages). Example: [list {1 {out of memory}}]
313a4bc123fSdan#
3141f55e28dSdan#     -injectinstall
3151f55e28dSdan#
3161f55e28dSdan#     -injectuninstall
3171f55e28dSdan#
3188521abfdSdan#     -prep             Script to execute before -body.
3198521abfdSdan#
3208521abfdSdan#     -body             Script to execute (with fault injection).
3218521abfdSdan#
3228521abfdSdan#     -test             Script to execute after -body.
3238521abfdSdan#
324e4bec379Sdan#     -start            Index of first fault to inject (default 1)
325e4bec379Sdan#
326a4bc123fSdanproc do_one_faultsim_test {testname args} {
3278521abfdSdan
3281f55e28dSdan  set DEFAULT(-injectstart)     "expr"
3291f55e28dSdan  set DEFAULT(-injectstop)      "expr 0"
3301f55e28dSdan  set DEFAULT(-injecterrlist)   [list]
3311f55e28dSdan  set DEFAULT(-injectinstall)   ""
3321f55e28dSdan  set DEFAULT(-injectuninstall) ""
3338521abfdSdan  set DEFAULT(-prep)            ""
3348521abfdSdan  set DEFAULT(-body)            ""
3358521abfdSdan  set DEFAULT(-test)            ""
336f3aef49cSdan  set DEFAULT(-install)         ""
337f3aef49cSdan  set DEFAULT(-uninstall)       ""
338e4bec379Sdan  set DEFAULT(-start)           1
339e4bec379Sdan  set DEFAULT(-end)             0
3408521abfdSdan
3418521abfdSdan  array set O [array get DEFAULT]
3428521abfdSdan  array set O $args
3438521abfdSdan  foreach o [array names O] {
3448521abfdSdan    if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
3458521abfdSdan  }
3468521abfdSdan
3478521abfdSdan  proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
348a4bc123fSdan  proc faultsim_test_result {args} "
349a4bc123fSdan    uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
350a4bc123fSdan  "
3518521abfdSdan
3521f55e28dSdan  eval $O(-injectinstall)
353f3aef49cSdan  eval $O(-install)
3541f55e28dSdan
3558521abfdSdan  set stop 0
356e4bec379Sdan  for {set iFail $O(-start)}                        \
357e4bec379Sdan      {!$stop && ($O(-end)==0 || $iFail<=$O(-end))} \
358e4bec379Sdan      {incr iFail}                                  \
359e4bec379Sdan  {
3608521abfdSdan
3618521abfdSdan    # Evaluate the -prep script.
3628521abfdSdan    #
3638521abfdSdan    eval $O(-prep)
3648521abfdSdan
3658521abfdSdan    # Start the fault-injection. Run the -body script. Stop the fault
3668521abfdSdan    # injection. Local var $nfail is set to the total number of faults
3678521abfdSdan    # injected into the system this trial.
3688521abfdSdan    #
3698521abfdSdan    eval $O(-injectstart) $iFail
3708521abfdSdan    set rc [catch $O(-body) res]
3718521abfdSdan    set nfail [eval $O(-injectstop)]
3728521abfdSdan
3738521abfdSdan    # Run the -test script. If it throws no error, consider this trial
3748521abfdSdan    # sucessful. If it does throw an error, cause a [do_test] test to
3758521abfdSdan    # fail (and print out the unexpected exception thrown by the -test
3768521abfdSdan    # script at the same time).
3778521abfdSdan    #
3788521abfdSdan    set rc [catch [list faultsim_test_proc $rc $res $nfail] res]
3798521abfdSdan    if {$rc == 0} {set res ok}
3808521abfdSdan    do_test $testname.$iFail [list list $rc $res] {0 ok}
3818521abfdSdan
3828521abfdSdan    # If no faults where injected this trial, don't bother running
3838521abfdSdan    # any more. This test is finished.
3848521abfdSdan    #
3858521abfdSdan    if {$nfail==0} { set stop 1 }
3868521abfdSdan  }
3871f55e28dSdan
388f3aef49cSdan  eval $O(-uninstall)
3891f55e28dSdan  eval $O(-injectuninstall)
3908521abfdSdan}
3918521abfdSdan
392c9cf901dSdanielk1977# Usage: do_malloc_test <test number> <options...>
393c9cf901dSdanielk1977#
394c9cf901dSdanielk1977# The first argument, <test number>, is an integer used to name the
395c9cf901dSdanielk1977# tests executed by this proc. Options are as follows:
396c9cf901dSdanielk1977#
397c9cf901dSdanielk1977#     -tclprep          TCL script to run to prepare test.
398c9cf901dSdanielk1977#     -sqlprep          SQL script to run to prepare test.
399c9cf901dSdanielk1977#     -tclbody          TCL script to run with malloc failure simulation.
400c9cf901dSdanielk1977#     -sqlbody          TCL script to run with malloc failure simulation.
401c9cf901dSdanielk1977#     -cleanup          TCL script to run after the test.
402c9cf901dSdanielk1977#
403c9cf901dSdanielk1977# This command runs a series of tests to verify SQLite's ability
404c9cf901dSdanielk1977# to handle an out-of-memory condition gracefully. It is assumed
405c9cf901dSdanielk1977# that if this condition occurs a malloc() call will return a
406c9cf901dSdanielk1977# NULL pointer. Linux, for example, doesn't do that by default. See
407c9cf901dSdanielk1977# the "BUGS" section of malloc(3).
408c9cf901dSdanielk1977#
409c9cf901dSdanielk1977# Each iteration of a loop, the TCL commands in any argument passed
410c9cf901dSdanielk1977# to the -tclbody switch, followed by the SQL commands in any argument
411c9cf901dSdanielk1977# passed to the -sqlbody switch are executed. Each iteration the
412c9cf901dSdanielk1977# Nth call to sqliteMalloc() is made to fail, where N is increased
413c9cf901dSdanielk1977# each time the loop runs starting from 1. When all commands execute
414c9cf901dSdanielk1977# successfully, the loop ends.
415c9cf901dSdanielk1977#
416c9cf901dSdanielk1977proc do_malloc_test {tn args} {
417c9cf901dSdanielk1977  array unset ::mallocopts
418c9cf901dSdanielk1977  array set ::mallocopts $args
419c9cf901dSdanielk1977
420c9cf901dSdanielk1977  if {[string is integer $tn]} {
421c9cf901dSdanielk1977    set tn malloc-$tn
42276ccd89dSdan    catch { set tn $::testprefix-$tn }
423c9cf901dSdanielk1977  }
424f3a65f7eSdrh  if {[info exists ::mallocopts(-start)]} {
425f3a65f7eSdrh    set start $::mallocopts(-start)
426f3a65f7eSdrh  } else {
427ae72d982Sdanielk1977    set start 0
428f3a65f7eSdrh  }
4291527ff4eSdrh  if {[info exists ::mallocopts(-end)]} {
4301527ff4eSdrh    set end $::mallocopts(-end)
4311527ff4eSdrh  } else {
4321527ff4eSdrh    set end 50000
4331527ff4eSdrh  }
43493aed5a1Sdrh  save_prng_state
435c9cf901dSdanielk1977
436643167ffSdrh  foreach ::iRepeat {0 10000000} {
437c9cf901dSdanielk1977    set ::go 1
4381527ff4eSdrh    for {set ::n $start} {$::go && $::n <= $end} {incr ::n} {
439c9cf901dSdanielk1977
440a1644fd8Sdanielk1977      # If $::iRepeat is 0, then the malloc() failure is transient - it
441a1644fd8Sdanielk1977      # fails and then subsequent calls succeed. If $::iRepeat is 1,
442a1644fd8Sdanielk1977      # then the failure is persistent - once malloc() fails it keeps
443a1644fd8Sdanielk1977      # failing.
444a1644fd8Sdanielk1977      #
445a1644fd8Sdanielk1977      set zRepeat "transient"
446a1644fd8Sdanielk1977      if {$::iRepeat} {set zRepeat "persistent"}
44793aed5a1Sdrh      restore_prng_state
448fda06befSmistachkin      foreach file [glob -nocomplain test.db-mj*] {forcedelete $file}
449a1644fd8Sdanielk1977
450a1644fd8Sdanielk1977      do_test ${tn}.${zRepeat}.${::n} {
451a1644fd8Sdanielk1977
452a1644fd8Sdanielk1977        # Remove all traces of database files test.db and test2.db
453a1644fd8Sdanielk1977        # from the file-system. Then open (empty database) "test.db"
454a1644fd8Sdanielk1977        # with the handle [db].
455c9cf901dSdanielk1977        #
456c9cf901dSdanielk1977        catch {db close}
457304feffcSdan        catch {db2 close}
458d78fe8e3Sdan        forcedelete test.db
459d78fe8e3Sdan        forcedelete test.db-journal
460d78fe8e3Sdan        forcedelete test.db-wal
461d78fe8e3Sdan        forcedelete test2.db
462d78fe8e3Sdan        forcedelete test2.db-journal
463d78fe8e3Sdan        forcedelete test2.db-wal
464ed138fb3Sdrh        if {[info exists ::mallocopts(-testdb)]} {
4655ea4298cSmistachkin          copy_file $::mallocopts(-testdb) test.db
466ed138fb3Sdrh        }
467c9cf901dSdanielk1977        catch { sqlite3 db test.db }
468ae72d982Sdanielk1977        if {[info commands db] ne ""} {
469ae72d982Sdanielk1977          sqlite3_extended_result_codes db 1
470ae72d982Sdanielk1977        }
471e9d1c720Sdrh        sqlite3_db_config_lookaside db 0 0 0
472c9cf901dSdanielk1977
473c9cf901dSdanielk1977        # Execute any -tclprep and -sqlprep scripts.
474c9cf901dSdanielk1977        #
475c9cf901dSdanielk1977        if {[info exists ::mallocopts(-tclprep)]} {
476c9cf901dSdanielk1977          eval $::mallocopts(-tclprep)
477c9cf901dSdanielk1977        }
478c9cf901dSdanielk1977        if {[info exists ::mallocopts(-sqlprep)]} {
479c9cf901dSdanielk1977          execsql $::mallocopts(-sqlprep)
480c9cf901dSdanielk1977        }
481c9cf901dSdanielk1977
482a1644fd8Sdanielk1977        # Now set the ${::n}th malloc() to fail and execute the -tclbody
483a1644fd8Sdanielk1977        # and -sqlbody scripts.
484c9cf901dSdanielk1977        #
485a1644fd8Sdanielk1977        sqlite3_memdebug_fail $::n -repeat $::iRepeat
486c9cf901dSdanielk1977        set ::mallocbody {}
487c9cf901dSdanielk1977        if {[info exists ::mallocopts(-tclbody)]} {
488c9cf901dSdanielk1977          append ::mallocbody "$::mallocopts(-tclbody)\n"
489c9cf901dSdanielk1977        }
490c9cf901dSdanielk1977        if {[info exists ::mallocopts(-sqlbody)]} {
491c9cf901dSdanielk1977          append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
492c9cf901dSdanielk1977        }
493c9cf901dSdanielk1977
494a1644fd8Sdanielk1977        # The following block sets local variables as follows:
495a1644fd8Sdanielk1977        #
496a1644fd8Sdanielk1977        #     isFail  - True if an error (any error) was reported by sqlite.
497a1644fd8Sdanielk1977        #     nFail   - The total number of simulated malloc() failures.
498a1644fd8Sdanielk1977        #     nBenign - The number of benign simulated malloc() failures.
499a1644fd8Sdanielk1977        #
500a1644fd8Sdanielk1977        set isFail [catch $::mallocbody msg]
501a1644fd8Sdanielk1977        set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
502643167ffSdrh        # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) "
503c9cf901dSdanielk1977
504a1644fd8Sdanielk1977        # If one or more mallocs failed, run this loop body again.
505a1644fd8Sdanielk1977        #
506a1644fd8Sdanielk1977        set go [expr {$nFail>0}]
507a1644fd8Sdanielk1977
508a1644fd8Sdanielk1977        if {($nFail-$nBenign)==0} {
509a1644fd8Sdanielk1977          if {$isFail} {
510f3a65f7eSdrh            set v2 $msg
511c9cf901dSdanielk1977          } else {
512a1644fd8Sdanielk1977            set isFail 1
513f3a65f7eSdrh            set v2 1
514c9cf901dSdanielk1977          }
515a1644fd8Sdanielk1977        } elseif {!$isFail} {
516f3a65f7eSdrh          set v2 $msg
517ae72d982Sdanielk1977        } elseif {
518ae72d982Sdanielk1977          [info command db]=="" ||
519ae72d982Sdanielk1977          [db errorcode]==7 ||
520ae72d982Sdanielk1977          $msg=="out of memory"
521ae72d982Sdanielk1977        } {
522f3a65f7eSdrh          set v2 1
523c9cf901dSdanielk1977        } else {
524f3a65f7eSdrh          set v2 $msg
525ae72d982Sdanielk1977          puts [db errorcode]
526f3a65f7eSdrh        }
527a1644fd8Sdanielk1977        lappend isFail $v2
528c9cf901dSdanielk1977      } {1 1}
529c9cf901dSdanielk1977
530c9cf901dSdanielk1977      if {[info exists ::mallocopts(-cleanup)]} {
531c9cf901dSdanielk1977        catch [list uplevel #0 $::mallocopts(-cleanup)] msg
532c9cf901dSdanielk1977      }
533c9cf901dSdanielk1977    }
534a1644fd8Sdanielk1977  }
535c9cf901dSdanielk1977  unset ::mallocopts
5367751940dSdanielk1977  sqlite3_memdebug_fail -1
537c9cf901dSdanielk1977}
538ef378025Sdan
539ef378025Sdan
540ef378025Sdan#-------------------------------------------------------------------------
541ef378025Sdan# This proc is used to test a single SELECT statement. Parameter $name is
542ef378025Sdan# passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter
543ef378025Sdan# $sql is passed the text of the SELECT statement. Parameter $result is
544ef378025Sdan# set to the expected output if the SELECT statement is successfully
545ef378025Sdan# executed using [db eval].
546ef378025Sdan#
547ef378025Sdan# Example:
548ef378025Sdan#
549ef378025Sdan#   do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2}
550ef378025Sdan#
551ef378025Sdan# If global variable DO_MALLOC_TEST is set to a non-zero value, or if
552ef378025Sdan# it is not defined at all, then OOM testing is performed on the SELECT
553ef378025Sdan# statement. Each OOM test case is said to pass if either (a) executing
554ef378025Sdan# the SELECT statement succeeds and the results match those specified
555ef378025Sdan# by parameter $result, or (b) TCL throws an "out of memory" error.
556ef378025Sdan#
557ef378025Sdan# If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement
558ef378025Sdan# is executed just once. In this case the test case passes if the results
559ef378025Sdan# match the expected results passed via parameter $result.
560ef378025Sdan#
561ef378025Sdanproc do_select_test {name sql result} {
5624f7c5e68Sdan  uplevel [list doPassiveTest 0 $name $sql [list 0 [list {*}$result]]]
563ef378025Sdan}
564ef378025Sdan
565ef378025Sdanproc do_restart_select_test {name sql result} {
566ef378025Sdan  uplevel [list doPassiveTest 1 $name $sql [list 0 $result]]
567ef378025Sdan}
568ef378025Sdan
569ef378025Sdanproc do_error_test {name sql error} {
570ef378025Sdan  uplevel [list doPassiveTest 0 $name $sql [list 1 $error]]
571ef378025Sdan}
572ef378025Sdan
573ef378025Sdanproc doPassiveTest {isRestart name sql catchres} {
574ef378025Sdan  if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
575ef378025Sdan
5764f7c5e68Sdan  if {[info exists ::testprefix]
5774f7c5e68Sdan   && [string is integer [string range $name 0 0]]
5784f7c5e68Sdan  } {
5794f7c5e68Sdan    set name $::testprefix.$name
5804f7c5e68Sdan  }
5814f7c5e68Sdan
582ef378025Sdan  switch $::DO_MALLOC_TEST {
583ef378025Sdan    0 { # No malloc failures.
584ef378025Sdan      do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres
585ef378025Sdan      return
586ef378025Sdan    }
587ef378025Sdan    1 { # Simulate transient failures.
588ef378025Sdan      set nRepeat 1
589ef378025Sdan      set zName "transient"
590ef378025Sdan      set nStartLimit 100000
591ef378025Sdan      set nBackup 1
592ef378025Sdan    }
593ef378025Sdan    2 { # Simulate persistent failures.
594ef378025Sdan      set nRepeat 1
595ef378025Sdan      set zName "persistent"
596ef378025Sdan      set nStartLimit 100000
597ef378025Sdan      set nBackup 1
598ef378025Sdan    }
599ef378025Sdan    3 { # Simulate transient failures with extra brute force.
600ef378025Sdan      set nRepeat 100000
601ef378025Sdan      set zName "ridiculous"
602ef378025Sdan      set nStartLimit 1
603ef378025Sdan      set nBackup 10
604ef378025Sdan    }
605ef378025Sdan  }
606ef378025Sdan
607ef378025Sdan  # The set of acceptable results from running [catchsql $sql].
608ef378025Sdan  #
609ef378025Sdan  set answers [list {1 {out of memory}} $catchres]
610ef378025Sdan  set str [join $answers " OR "]
611ef378025Sdan
612ef378025Sdan  set nFail 1
613ef378025Sdan  for {set iLimit $nStartLimit} {$nFail} {incr iLimit} {
614ef378025Sdan    for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} {
615ef378025Sdan      for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} {
616ef378025Sdan
617ef378025Sdan        if {$isRestart} { sqlite3 db test.db }
618ef378025Sdan
619ef378025Sdan        sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat
620ef378025Sdan        set res [uplevel [list catchsql $sql]]
621ef378025Sdan        if {[lsearch -exact $answers $res]>=0} { set res $str }
622ef378025Sdan        set testname "$name.$zName.$iFail"
623ef378025Sdan        do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str
624ef378025Sdan
625ef378025Sdan        set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
626ef378025Sdan      }
627ef378025Sdan    }
628ef378025Sdan  }
629ef378025Sdan}
630ef378025Sdan
631ef378025Sdan
632ef378025Sdan#-------------------------------------------------------------------------
633ef378025Sdan# Test a single write to the database. In this case a  "write" is a
634ef378025Sdan# DELETE, UPDATE or INSERT statement.
635ef378025Sdan#
636ef378025Sdan# If OOM testing is performed, there are several acceptable outcomes:
637ef378025Sdan#
638ef378025Sdan#   1) The write succeeds. No error is returned.
639ef378025Sdan#
640ef378025Sdan#   2) An "out of memory" exception is thrown and:
641ef378025Sdan#
642ef378025Sdan#     a) The statement has no effect, OR
643ef378025Sdan#     b) The current transaction is rolled back, OR
644ef378025Sdan#     c) The statement succeeds. This can only happen if the connection
645ef378025Sdan#        is in auto-commit mode (after the statement is executed, so this
646ef378025Sdan#        includes COMMIT statements).
647ef378025Sdan#
648ef378025Sdan# If the write operation eventually succeeds, zero is returned. If a
649ef378025Sdan# transaction is rolled back, non-zero is returned.
650ef378025Sdan#
651ef378025Sdan# Parameter $name is the name to use for the test case (or test cases).
652ef378025Sdan# The second parameter, $tbl, should be the name of the database table
653ef378025Sdan# being modified. Parameter $sql contains the SQL statement to test.
654ef378025Sdan#
655ef378025Sdanproc do_write_test {name tbl sql} {
656ef378025Sdan  if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
657ef378025Sdan
658ef378025Sdan  # Figure out an statement to get a checksum for table $tbl.
659ef378025Sdan  db eval "SELECT * FROM $tbl" V break
660ef378025Sdan  set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl"
661ef378025Sdan
662ef378025Sdan  # Calculate the initial table checksum.
663ef378025Sdan  set cksum1 [db one $cksumsql]
664ef378025Sdan
665ef378025Sdan  if {$::DO_MALLOC_TEST } {
666ef378025Sdan    set answers [list {1 {out of memory}} {0 {}}]
667*f703b42dSdan    lappend answers [list 1 {unable to open a temporary database file for storing temporary tables}]
668ef378025Sdan    if {$::DO_MALLOC_TEST==1} {
669decd09cbSshaneh      set modes {100000 persistent}
670ef378025Sdan    } else {
671decd09cbSshaneh      set modes {1 transient}
672ef378025Sdan    }
673ef378025Sdan  } else {
674ef378025Sdan    set answers [list {0 {}}]
675ef378025Sdan    set modes [list 0 nofail]
676ef378025Sdan  }
677ef378025Sdan  set str [join $answers " OR "]
678ef378025Sdan
679ef378025Sdan  foreach {nRepeat zName} $modes {
680ef378025Sdan    for {set iFail 1} 1 {incr iFail} {
681ef378025Sdan      if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat}
682ef378025Sdan
683ef378025Sdan      set res [uplevel [list catchsql $sql]]
684ef378025Sdan      set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
685ef378025Sdan      if {$nFail==0} {
686ef378025Sdan        do_test $name.$zName.$iFail [list set {} $res] {0 {}}
687ef378025Sdan        return
688ef378025Sdan      } else {
689ef378025Sdan        if {[lsearch $answers $res]>=0} {
690ef378025Sdan          set res $str
691ef378025Sdan        }
692ef378025Sdan        do_test $name.$zName.$iFail [list set {} $res] $str
693ef378025Sdan        set cksum2 [db one $cksumsql]
694ef378025Sdan        if {$cksum1 != $cksum2} return
695ef378025Sdan      }
696ef378025Sdan    }
697ef378025Sdan  }
698ef378025Sdan}
699