blob: 98e8b48922deb7a7c27be0c1c2fa705c813f4d1d [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}
51 catch {sqlite3 db test.db}
danielk1977c9cf9012007-05-30 10:36:47 +000052
53 # Execute any -tclprep and -sqlprep scripts.
54 #
55 if {[info exists ::mallocopts(-tclprep)]} {
56 eval $::mallocopts(-tclprep)
57 }
58 if {[info exists ::mallocopts(-sqlprep)]} {
59 execsql $::mallocopts(-sqlprep)
60 }
61
62 # Now set the ${::n}th malloc() to fail and execute the -tclbody and
63 # -sqlbody scripts.
64 #
drhf3a65f72007-08-22 20:18:21 +000065 sqlite3_memdebug_fail $::n 1
danielk1977c9cf9012007-05-30 10:36:47 +000066 set ::mallocbody {}
67 if {[info exists ::mallocopts(-tclbody)]} {
68 append ::mallocbody "$::mallocopts(-tclbody)\n"
69 }
70 if {[info exists ::mallocopts(-sqlbody)]} {
71 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
72 }
73 set v [catch $::mallocbody msg]
drhf3a65f72007-08-22 20:18:21 +000074 set failFlag [sqlite3_memdebug_fail -1 0]
75 set go [expr {$failFlag>0}]
danielk1977c9cf9012007-05-30 10:36:47 +000076
danielk1977c9cf9012007-05-30 10:36:47 +000077
drhf3a65f72007-08-22 20:18:21 +000078 if {$failFlag==0} {
danielk1977c9cf9012007-05-30 10:36:47 +000079 if {$v} {
drhf3a65f72007-08-22 20:18:21 +000080 set v2 $msg
danielk1977c9cf9012007-05-30 10:36:47 +000081 } else {
drhf3a65f72007-08-22 20:18:21 +000082 set v 1
83 set v2 1
danielk1977c9cf9012007-05-30 10:36:47 +000084 }
drhf3a65f72007-08-22 20:18:21 +000085 } elseif {!$v} {
86 set v2 $msg
87 } elseif {[info command db]=="" || [db errorcode]==7
88 || $msg=="out of memory"} {
89 set v2 1
danielk1977c9cf9012007-05-30 10:36:47 +000090 } else {
drhf3a65f72007-08-22 20:18:21 +000091 set v2 $msg
danielk1977c9cf9012007-05-30 10:36:47 +000092 }
drhf3a65f72007-08-22 20:18:21 +000093 lappend v $v2
danielk1977c9cf9012007-05-30 10:36:47 +000094 } {1 1}
95
96 if {[info exists ::mallocopts(-cleanup)]} {
97 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
98 }
99 }
100 unset ::mallocopts
101}