blob: 928e8f9051df4102c3521ba47c77cb51e5ff7633 [file] [log] [blame]
drh5a3032b2007-09-03 16:12:09 +00001# 2007 May 05
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# This file contains common code used by many different malloc tests
13# within the test suite.
14#
15# $Id: malloc_common.tcl,v 1.8 2007/09/03 16:12:10 drh Exp $
danielk1977c9cf9012007-05-30 10:36:47 +000016
drh5a3032b2007-09-03 16:12:09 +000017# If we did not compile with malloc testing enabled, then do nothing.
18#
danielk1977cdc3a6b2007-08-25 13:09:26 +000019ifcapable !memdebug {
danielk1977369ff422007-09-03 07:31:09 +000020 return 0
danielk1977cdc3a6b2007-08-25 13:09:26 +000021}
22
danielk1977c9cf9012007-05-30 10:36:47 +000023# Usage: do_malloc_test <test number> <options...>
24#
25# The first argument, <test number>, is an integer used to name the
26# tests executed by this proc. Options are as follows:
27#
28# -tclprep TCL script to run to prepare test.
29# -sqlprep SQL script to run to prepare test.
30# -tclbody TCL script to run with malloc failure simulation.
31# -sqlbody TCL script to run with malloc failure simulation.
32# -cleanup TCL script to run after the test.
33#
34# This command runs a series of tests to verify SQLite's ability
35# to handle an out-of-memory condition gracefully. It is assumed
36# that if this condition occurs a malloc() call will return a
37# NULL pointer. Linux, for example, doesn't do that by default. See
38# the "BUGS" section of malloc(3).
39#
40# Each iteration of a loop, the TCL commands in any argument passed
41# to the -tclbody switch, followed by the SQL commands in any argument
42# passed to the -sqlbody switch are executed. Each iteration the
43# Nth call to sqliteMalloc() is made to fail, where N is increased
44# each time the loop runs starting from 1. When all commands execute
45# successfully, the loop ends.
46#
47proc do_malloc_test {tn args} {
48 array unset ::mallocopts
49 array set ::mallocopts $args
50
51 if {[string is integer $tn]} {
52 set tn malloc-$tn
53 }
drhf3a65f72007-08-22 20:18:21 +000054 if {[info exists ::mallocopts(-start)]} {
55 set start $::mallocopts(-start)
56 } else {
57 set start 1
58 }
danielk1977c9cf9012007-05-30 10:36:47 +000059
danielk1977a1644fd2007-08-29 12:31:25 +000060 foreach ::iRepeat {0 1} {
61 set ::go 1
62 for {set ::n $start} {$::go && $::n < 50000} {incr ::n} {
danielk1977c9cf9012007-05-30 10:36:47 +000063
danielk1977a1644fd2007-08-29 12:31:25 +000064 # If $::iRepeat is 0, then the malloc() failure is transient - it
65 # fails and then subsequent calls succeed. If $::iRepeat is 1,
66 # then the failure is persistent - once malloc() fails it keeps
67 # failing.
danielk1977c9cf9012007-05-30 10:36:47 +000068 #
danielk1977a1644fd2007-08-29 12:31:25 +000069 set zRepeat "transient"
70 if {$::iRepeat} {set zRepeat "persistent"}
danielk1977c9cf9012007-05-30 10:36:47 +000071
danielk1977a1644fd2007-08-29 12:31:25 +000072 do_test ${tn}.${zRepeat}.${::n} {
73
74 # Remove all traces of database files test.db and test2.db
75 # from the file-system. Then open (empty database) "test.db"
76 # with the handle [db].
77 #
78 catch {db close}
79 catch {file delete -force test.db}
80 catch {file delete -force test.db-journal}
81 catch {file delete -force test2.db}
82 catch {file delete -force test2.db-journal}
83 if {[info exists ::mallocopts(-testdb)]} {
84 file copy $::mallocopts(-testdb) test.db
danielk1977c9cf9012007-05-30 10:36:47 +000085 }
danielk1977a1644fd2007-08-29 12:31:25 +000086 catch {sqlite3 db test.db}
87
88 # Execute any -tclprep and -sqlprep scripts.
89 #
90 if {[info exists ::mallocopts(-tclprep)]} {
91 eval $::mallocopts(-tclprep)
92 }
93 if {[info exists ::mallocopts(-sqlprep)]} {
94 execsql $::mallocopts(-sqlprep)
95 }
96
97 # Now set the ${::n}th malloc() to fail and execute the -tclbody
98 # and -sqlbody scripts.
99 #
100 sqlite3_memdebug_fail $::n -repeat $::iRepeat
101 set ::mallocbody {}
102 if {[info exists ::mallocopts(-tclbody)]} {
103 append ::mallocbody "$::mallocopts(-tclbody)\n"
104 }
105 if {[info exists ::mallocopts(-sqlbody)]} {
106 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
107 }
danielk1977c9cf9012007-05-30 10:36:47 +0000108
danielk1977a1644fd2007-08-29 12:31:25 +0000109 # The following block sets local variables as follows:
110 #
111 # isFail - True if an error (any error) was reported by sqlite.
112 # nFail - The total number of simulated malloc() failures.
113 # nBenign - The number of benign simulated malloc() failures.
114 #
115 set isFail [catch $::mallocbody msg]
116 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
danielk1977a1644fd2007-08-29 12:31:25 +0000117
118 # If one or more mallocs failed, run this loop body again.
119 #
120 set go [expr {$nFail>0}]
121
122 if {($nFail-$nBenign)==0} {
123 if {$isFail} {
124 set v2 $msg
125 } else {
126 set isFail 1
127 set v2 1
128 }
129 } elseif {!$isFail} {
130 set v2 $msg
131 } elseif {[info command db]=="" || [db errorcode]==7
132 || $msg=="out of memory"} {
133 set v2 1
134 } else {
135 set v2 $msg
136 }
137 lappend isFail $v2
138 } {1 1}
139
140 if {[info exists ::mallocopts(-cleanup)]} {
141 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
142 }
danielk1977c9cf9012007-05-30 10:36:47 +0000143 }
144 }
145 unset ::mallocopts
danielk197777519402007-08-30 11:48:31 +0000146 sqlite3_memdebug_fail -1
danielk1977c9cf9012007-05-30 10:36:47 +0000147}