blob: 731670492d9231d615592148624620e5fd21b929 [file] [log] [blame]
drhb3226e82007-08-13 12:39:03 +00001# 2007 Aug 13
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 tests aspects of the malloc failure while parsing
13# CREATE TABLE statements in auto_vacuum mode.
14#
drh66fd2162009-04-11 16:27:49 +000015# $Id: mallocC.test,v 1.10 2009/04/11 16:27:50 drh Exp $
drhb3226e82007-08-13 12:39:03 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
drheee4c8c2008-02-18 22:24:57 +000019source $testdir/malloc_common.tcl
drhb3226e82007-08-13 12:39:03 +000020
21# Only run these tests if memory debugging is turned on.
22#
drheee4c8c2008-02-18 22:24:57 +000023if {!$MEMDEBUG} {
drh5a3032b2007-09-03 16:12:09 +000024 puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed138fb2007-08-22 22:04:37 +000025 finish_test
26 return
drhb3226e82007-08-13 12:39:03 +000027}
28
drhb3226e82007-08-13 12:39:03 +000029proc do_mallocC_test {tn args} {
30 array set ::mallocopts $args
drh93aed5a2008-01-16 17:46:38 +000031 #set sum [allcksum db]
drhb3226e82007-08-13 12:39:03 +000032
33 for {set ::n 1} {true} {incr ::n} {
34
35 # Run the SQL. Malloc number $::n is set to fail. A malloc() failure
36 # may or may not be reported.
danielk1977a1644fd2007-08-29 12:31:25 +000037 sqlite3_memdebug_fail $::n -repeat 1
drhb4035742007-08-13 12:58:18 +000038 do_test mallocC-$tn.$::n.1 {
drhb3226e82007-08-13 12:39:03 +000039 set res [catchsql [string trim $::mallocopts(-sql)]]
40 set rc [expr {
41 0==[string compare $res {1 {out of memory}}] ||
danielk1977ae72d982007-10-03 08:46:44 +000042 [db errorcode] == 3082 ||
drhb3226e82007-08-13 12:39:03 +000043 0==[lindex $res 0]
44 }]
45 if {$rc!=1} {
46 puts "Error: $res"
47 }
48 set rc
49 } {1}
50
51 # If $::n is greater than the number of malloc() calls required to
52 # execute the SQL, then this test is finished. Break out of the loop.
danielk1977a1644fd2007-08-29 12:31:25 +000053 set nFail [sqlite3_memdebug_fail -1]
drhed138fb2007-08-22 22:04:37 +000054 if {$nFail==0} {
drhb3226e82007-08-13 12:39:03 +000055 break
56 }
57
58 # Recover from the malloc failure.
59 #
60 # Update: The new malloc() failure handling means that a transaction may
61 # still be active even if a malloc() has failed. But when these tests were
62 # written this was not the case. So do a manual ROLLBACK here so that the
63 # tests pass.
drhb4035742007-08-13 12:58:18 +000064 do_test mallocC-$tn.$::n.2 {
drhb3226e82007-08-13 12:39:03 +000065 catch {
66 execsql {
67 ROLLBACK;
68 }
69 }
70 expr 0
71 } {0}
72
73 # Checksum the database.
drhb4035742007-08-13 12:58:18 +000074 #do_test mallocC-$tn.$::n.3 {
drh93aed5a2008-01-16 17:46:38 +000075 # allcksum db
drhb3226e82007-08-13 12:39:03 +000076 #} $sum
77
drhb4035742007-08-13 12:58:18 +000078 #integrity_check mallocC-$tn.$::n.4
drhb3226e82007-08-13 12:39:03 +000079 if {$::nErr>1} return
80 }
81 unset ::mallocopts
82}
83
danielk1977ae72d982007-10-03 08:46:44 +000084sqlite3_extended_result_codes db 1
85
drhb3226e82007-08-13 12:39:03 +000086execsql {
87 PRAGMA auto_vacuum=1;
88 CREATE TABLE t0(a, b, c);
89}
drh66fd2162009-04-11 16:27:49 +000090
91# The number of memory allocation failures is different on 64-bit
92# and 32-bit systems due to larger structures on 64-bit systems
93# overflowing the lookaside more often. To debug problems, it is
94# sometimes helpful to reduce the size of the lookaside allocation
95# blocks. But this is normally disabled.
96#
97if {0} {
98 db close
99 sqlite3_shutdown
100 sqlite3_config_lookaside 50 500
101 sqlite3_initialize
102 autoinstall_test_functions
103 sqlite3 db test.db
104}
105
drhb3226e82007-08-13 12:39:03 +0000106do_mallocC_test 1 -sql {
107 BEGIN;
108 -- Allocate 32 new root pages. This will exercise the 'extract specific
109 -- page from the freelist' code when in auto-vacuum mode (see the
110 -- allocatePage() routine in btree.c).
111 CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE);
112 CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE);
113 CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE);
114 CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE);
115 CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE);
116 CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE);
117 CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE);
118 CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE);
119
120 ROLLBACK;
121}
122
123finish_test