blob: fc8b3abbde656b783e114a6206bd4bf7bbfc35dd [file] [log] [blame]
drh66f4a062006-07-26 14:57:30 +00001# 2006 July 26
drhaa0fe802006-07-26 14:49:19 +00002#
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#***********************************************************************
drh66f4a062006-07-26 14:57:30 +000011# This file contains additional out-of-memory checks (see malloc.tcl)
12# added to expose a bug in out-of-memory handling for sqlite3_prepare16().
drhaa0fe802006-07-26 14:49:19 +000013#
drh66f4a062006-07-26 14:57:30 +000014# $Id: malloc7.test,v 1.2 2006/07/26 14:57:30 drh Exp $
drhaa0fe802006-07-26 14:49:19 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Only run these tests if memory debugging is turned on.
20#
21if {[info command sqlite_malloc_stat]==""} {
22 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
23 finish_test
24 return
25}
26
27# Usage: do_malloc_test <test number> <options...>
28#
29# The first argument, <test number>, is an integer used to name the
30# tests executed by this proc. Options are as follows:
31#
32# -tclprep TCL script to run to prepare test.
33# -sqlprep SQL script to run to prepare test.
34# -tclbody TCL script to run with malloc failure simulation.
35# -sqlbody TCL script to run with malloc failure simulation.
36# -cleanup TCL script to run after the test.
37#
38# This command runs a series of tests to verify SQLite's ability
39# to handle an out-of-memory condition gracefully. It is assumed
40# that if this condition occurs a malloc() call will return a
41# NULL pointer. Linux, for example, doesn't do that by default. See
42# the "BUGS" section of malloc(3).
43#
44# Each iteration of a loop, the TCL commands in any argument passed
45# to the -tclbody switch, followed by the SQL commands in any argument
46# passed to the -sqlbody switch are executed. Each iteration the
47# Nth call to sqliteMalloc() is made to fail, where N is increased
48# each time the loop runs starting from 1. When all commands execute
49# successfully, the loop ends.
50#
51proc do_malloc_test {tn args} {
52 array unset ::mallocopts
53 array set ::mallocopts $args
54
55 set ::go 1
56 for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
57 do_test malloc7-$tn.$::n {
58
59 # Remove all traces of database files test.db and test2.db from the files
60 # system. Then open (empty database) "test.db" with the handle [db].
61 #
62 sqlite_malloc_fail 0
63 catch {db close}
64 catch {file delete -force test.db}
65 catch {file delete -force test.db-journal}
66 catch {file delete -force test2.db}
67 catch {file delete -force test2.db-journal}
68 catch {sqlite3 db test.db}
69 set ::DB [sqlite3_connection_pointer db]
70
71 # Execute any -tclprep and -sqlprep scripts.
72 #
73 if {[info exists ::mallocopts(-tclprep)]} {
74 eval $::mallocopts(-tclprep)
75 }
76 if {[info exists ::mallocopts(-sqlprep)]} {
77 execsql $::mallocopts(-sqlprep)
78 }
79
80 # Now set the ${::n}th malloc() to fail and execute the -tclbody and
81 # -sqlbody scripts.
82 #
83 sqlite_malloc_fail $::n
84 set ::mallocbody {}
85 if {[info exists ::mallocopts(-tclbody)]} {
86 append ::mallocbody "$::mallocopts(-tclbody)\n"
87 }
88 if {[info exists ::mallocopts(-sqlbody)]} {
89 append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
90 }
91 set v [catch $::mallocbody msg]
92
93 # If the test fails (if $v!=0) and the database connection actually
94 # exists, make sure the failure code is SQLITE_NOMEM.
95 if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)]
96 && [db errorcode]!=7} {
97 set v 999
98 }
99
100 set leftover [lindex [sqlite_malloc_stat] 2]
101 if {$leftover>0} {
102 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
103 set ::go 0
104 if {$v} {
105 puts "\nError message returned: $msg"
106 } else {
107 set v {1 1}
108 }
109 } else {
110 set v2 [expr {$msg=="" || $msg=="out of memory"}]
111 if {!$v2} {puts "\nError message returned: $msg"}
112 lappend v $v2
113 }
114 } {1 1}
115
116 if {[info exists ::mallocopts(-cleanup)]} {
117 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
118 }
119 }
120 unset ::mallocopts
121}
122
123db eval {
124 CREATE TABLE t1(a,b,c,d);
125 CREATE INDEX i1 ON t1(b,c);
126}
127
128do_malloc_test 1 -tclbody {
129 set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
130 append sql16 "\00\00"
131 set nbyte [string length $sql16]
132 set ::STMT [sqlite3_prepare16 $::DB $sql16 $nbyte DUMMY]
133 sqlite3_finalize $::STMT
134}
135
136
137
138# Ensure that no file descriptors were leaked.
139do_test malloc-99.X {
140 catch {db close}
141 set sqlite_open_file_count
142} {0}
143
144puts open-file-count=$sqlite_open_file_count
145sqlite_malloc_fail 0
146finish_test