sl@0: # 2007 Aug 13 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file tests aspects of the malloc failure while parsing sl@0: # CREATE TABLE statements in auto_vacuum mode. sl@0: # sl@0: # $Id: mallocC.test,v 1.9 2008/02/18 22:24:58 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: source $testdir/malloc_common.tcl sl@0: sl@0: # Only run these tests if memory debugging is turned on. sl@0: # sl@0: if {!$MEMDEBUG} { sl@0: puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..." sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: proc do_mallocC_test {tn args} { sl@0: array set ::mallocopts $args sl@0: #set sum [allcksum db] sl@0: sl@0: for {set ::n 1} {true} {incr ::n} { sl@0: sl@0: # Run the SQL. Malloc number $::n is set to fail. A malloc() failure sl@0: # may or may not be reported. sl@0: sqlite3_memdebug_fail $::n -repeat 1 sl@0: do_test mallocC-$tn.$::n.1 { sl@0: set res [catchsql [string trim $::mallocopts(-sql)]] sl@0: set rc [expr { sl@0: 0==[string compare $res {1 {out of memory}}] || sl@0: [db errorcode] == 3082 || sl@0: 0==[lindex $res 0] sl@0: }] sl@0: if {$rc!=1} { sl@0: puts "Error: $res" sl@0: } sl@0: set rc sl@0: } {1} sl@0: sl@0: # If $::n is greater than the number of malloc() calls required to sl@0: # execute the SQL, then this test is finished. Break out of the loop. sl@0: set nFail [sqlite3_memdebug_fail -1] sl@0: if {$nFail==0} { sl@0: break sl@0: } sl@0: sl@0: # Recover from the malloc failure. sl@0: # sl@0: # Update: The new malloc() failure handling means that a transaction may sl@0: # still be active even if a malloc() has failed. But when these tests were sl@0: # written this was not the case. So do a manual ROLLBACK here so that the sl@0: # tests pass. sl@0: do_test mallocC-$tn.$::n.2 { sl@0: catch { sl@0: execsql { sl@0: ROLLBACK; sl@0: } sl@0: } sl@0: expr 0 sl@0: } {0} sl@0: sl@0: # Checksum the database. sl@0: #do_test mallocC-$tn.$::n.3 { sl@0: # allcksum db sl@0: #} $sum sl@0: sl@0: #integrity_check mallocC-$tn.$::n.4 sl@0: if {$::nErr>1} return sl@0: } sl@0: unset ::mallocopts sl@0: } sl@0: sl@0: sqlite3_extended_result_codes db 1 sl@0: sl@0: execsql { sl@0: PRAGMA auto_vacuum=1; sl@0: CREATE TABLE t0(a, b, c); sl@0: } sl@0: do_mallocC_test 1 -sql { sl@0: BEGIN; sl@0: -- Allocate 32 new root pages. This will exercise the 'extract specific sl@0: -- page from the freelist' code when in auto-vacuum mode (see the sl@0: -- allocatePage() routine in btree.c). sl@0: CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE); sl@0: sl@0: ROLLBACK; sl@0: } sl@0: sl@0: finish_test