blob: c49f5ac61bcd6f8f7ead2408cba308458eac7898 [file] [log] [blame]
danielk1977c9cf9012007-05-30 10:36:47 +00001
2# Usage: do_malloc_test <test number> <options...>
3#
4# The first argument, <test number>, is an integer used to name the
5# tests executed by this proc. Options are as follows:
6#
7# -tclprep TCL script to run to prepare test.
8# -sqlprep SQL script to run to prepare test.
9# -tclbody TCL script to run with malloc failure simulation.
10# -sqlbody TCL script to run with malloc failure simulation.
11# -cleanup TCL script to run after the test.
12#
13# This command runs a series of tests to verify SQLite's ability
14# to handle an out-of-memory condition gracefully. It is assumed
15# that if this condition occurs a malloc() call will return a
16# NULL pointer. Linux, for example, doesn't do that by default. See
17# the "BUGS" section of malloc(3).
18#
19# Each iteration of a loop, the TCL commands in any argument passed
20# to the -tclbody switch, followed by the SQL commands in any argument
21# passed to the -sqlbody switch are executed. Each iteration the
22# Nth call to sqliteMalloc() is made to fail, where N is increased
23# each time the loop runs starting from 1. When all commands execute
24# successfully, the loop ends.
25#
26proc do_malloc_test {tn args} {
27 array unset ::mallocopts
28 array set ::mallocopts $args
29
30 if {[string is integer $tn]} {
31 set tn malloc-$tn
32 }
drhf3a65f72007-08-22 20:18:21 +000033 if {[info exists ::mallocopts(-start)]} {
34 set start $::mallocopts(-start)
35 } else {
36 set start 1
37 }
danielk1977c9cf9012007-05-30 10:36:47 +000038
39 set ::go 1
drhf3a65f72007-08-22 20:18:21 +000040 for {set ::n $start} {$::go && $::n < 50000} {incr ::n} {
danielk1977c9cf9012007-05-30 10:36:47 +000041 do_test $tn.$::n {
42
43 # Remove all traces of database files test.db and test2.db from the files
44 # system. Then open (empty database) "test.db" with the handle [db].
45 #
danielk1977c9cf9012007-05-30 10:36:47 +000046 catch {db close}
47 catch {file delete -force test.db}
48 catch {file delete -force test.db-journal}
49 catch {file delete -force test2.db}
50 catch {file delete -force test2.db-journal}
drhed138fb2007-08-22 22:04:37 +000051 if {[info exists ::mallocopts(-testdb)]} {
52 file copy $::mallocopts(-testdb) test.db
53 }
danielk1977c9cf9012007-05-30 10:36:47 +000054 catch {sqlite3 db test.db}
danielk1977c9cf9012007-05-30 10:36:47 +000055
56 # Execute any -tclprep and -sqlprep scripts.
57 #
58 if {[info exists ::mallocopts(-tclprep)]} {
59 eval $::mallocopts(-tclprep)
60 }
61 if {[info exists ::mallocopts(-sqlprep)]} {
62 execsql $::mallocopts(-sqlprep)
63 }
64
65 # Now set the ${::n}th malloc() to fail and execute the -tclbody and
66 # -sqlbody scripts.
67 #
drhf3a65f72007-08-22 20:18:21 +000068 sqlite3_memdebug_fail $::n 1
danielk1977c9cf9012007-05-30 10:36:47 +000069 set ::mallocbody {}
70 if {[info exists ::mallocopts(-tclbody)]} {
71 append ::mallocbody "$::mallocopts(-tclbody)\n"
72 }
73 if {[info exists ::mallocopts(-sqlbody)]} {
74 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
75 }
76 set v [catch $::mallocbody msg]
drhf3a65f72007-08-22 20:18:21 +000077 set failFlag [sqlite3_memdebug_fail -1 0]
78 set go [expr {$failFlag>0}]
danielk1977c9cf9012007-05-30 10:36:47 +000079
danielk1977c9cf9012007-05-30 10:36:47 +000080
drhf3a65f72007-08-22 20:18:21 +000081 if {$failFlag==0} {
danielk1977c9cf9012007-05-30 10:36:47 +000082 if {$v} {
drhf3a65f72007-08-22 20:18:21 +000083 set v2 $msg
danielk1977c9cf9012007-05-30 10:36:47 +000084 } else {
drhf3a65f72007-08-22 20:18:21 +000085 set v 1
86 set v2 1
danielk1977c9cf9012007-05-30 10:36:47 +000087 }
drhf3a65f72007-08-22 20:18:21 +000088 } elseif {!$v} {
89 set v2 $msg
90 } elseif {[info command db]=="" || [db errorcode]==7
91 || $msg=="out of memory"} {
92 set v2 1
danielk1977c9cf9012007-05-30 10:36:47 +000093 } else {
drhf3a65f72007-08-22 20:18:21 +000094 set v2 $msg
danielk1977c9cf9012007-05-30 10:36:47 +000095 }
drhf3a65f72007-08-22 20:18:21 +000096 lappend v $v2
danielk1977c9cf9012007-05-30 10:36:47 +000097 } {1 1}
98
99 if {[info exists ::mallocopts(-cleanup)]} {
100 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
101 }
102 }
103 unset ::mallocopts
104}