xref: /sqlite-3.40.0/test/malloc9.test (revision d230f648)
1# 2007 April 30
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# This file contains additional out-of-memory checks (see malloc.tcl)
12# added to expose a bug in out-of-memory handling for sqlite3_prepare().
13#
14# $Id: malloc9.test,v 1.1 2007/04/30 21:39:16 drh Exp $
15
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 malloc9-$tn.$::n {
58
59      sqlite_malloc_fail 0
60      catch {db close}
61      catch {file delete -force test.db}
62      catch {file delete -force test.db-journal}
63      sqlite3 db test.db
64      set ::DB [sqlite3_connection_pointer db]
65
66      # Execute any -tclprep and -sqlprep scripts.
67      #
68      if {[info exists ::mallocopts(-tclprep)]} {
69        eval $::mallocopts(-tclprep)
70      }
71      if {[info exists ::mallocopts(-sqlprep)]} {
72        execsql $::mallocopts(-sqlprep)
73      }
74
75      # Now set the ${::n}th malloc() to fail and execute the -tclbody and
76      # -sqlbody scripts.
77      #
78      sqlite_malloc_fail $::n
79      set ::mallocbody {}
80      if {[info exists ::mallocopts(-tclbody)]} {
81        append ::mallocbody "$::mallocopts(-tclbody)\n"
82      }
83      if {[info exists ::mallocopts(-sqlbody)]} {
84        append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
85      }
86      set v [catch $::mallocbody msg]
87
88      # If the test fails (if $v!=0) and the database connection actually
89      # exists, make sure the failure code is SQLITE_NOMEM.
90      if {$v && [info command db]=="db" && [info exists ::mallocopts(-sqlbody)]
91              && [db errorcode]!=7} {
92        set v 999
93      }
94
95      set leftover [lindex [sqlite_malloc_stat] 2]
96      if {$leftover>0} {
97        if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
98        set ::go 0
99        if {$v} {
100          puts "\nError message returned: $msg"
101        } else {
102          set v {1 1}
103        }
104      } else {
105        set v2 [expr {$msg=="" || [regexp {out of memory} $msg]}]
106        if {!$v2} {puts "\nError message returned: $msg"}
107        lappend v $v2
108      }
109    } {1 1}
110
111    if {[info exists ::mallocopts(-cleanup)]} {
112      catch [list uplevel #0 $::mallocopts(-cleanup)] msg
113    }
114  }
115  unset ::mallocopts
116}
117
118
119do_malloc_test 1 -tclprep {
120  set sql {CREATE TABLE t1(x)}
121  set sqlbytes [string length $sql]
122  append sql {; INSERT INTO t1 VALUES(1)}
123} -tclbody {
124  if {[catch {sqlite3_prepare db $sql $sqlbytes TAIL} STMT]} {
125    set msg $STMT
126    set STMT {}
127    error $msg
128  }
129} -cleanup {
130  if {$STMT!=""} {
131    sqlite3_finalize $STMT
132  }
133}
134
135# Ensure that no file descriptors were leaked.
136do_test malloc-99.X {
137  catch {db close}
138  set sqlite_open_file_count
139} {0}
140
141sqlite_malloc_fail 0
142finish_test
143