1b3226e85Sdrh# 2007 Aug 13 2b3226e85Sdrh# 3b3226e85Sdrh# The author disclaims copyright to this source code. In place of 4b3226e85Sdrh# a legal notice, here is a blessing: 5b3226e85Sdrh# 6b3226e85Sdrh# May you do good and not evil. 7b3226e85Sdrh# May you find forgiveness for yourself and forgive others. 8b3226e85Sdrh# May you share freely, never taking more than you give. 9b3226e85Sdrh# 10b3226e85Sdrh#*********************************************************************** 11b3226e85Sdrh# 12b3226e85Sdrh# This file tests aspects of the malloc failure while parsing 13b3226e85Sdrh# CREATE TABLE statements in auto_vacuum mode. 14b3226e85Sdrh# 15*66fd2160Sdrh# $Id: mallocC.test,v 1.10 2009/04/11 16:27:50 drh Exp $ 16b3226e85Sdrh 17b3226e85Sdrhset testdir [file dirname $argv0] 18b3226e85Sdrhsource $testdir/tester.tcl 19eee4c8caSdrhsource $testdir/malloc_common.tcl 20b3226e85Sdrh 21b3226e85Sdrh# Only run these tests if memory debugging is turned on. 22b3226e85Sdrh# 23eee4c8caSdrhif {!$MEMDEBUG} { 245a3032b3Sdrh puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..." 25b3226e85Sdrh finish_test 26b3226e85Sdrh return 27b3226e85Sdrh} 28b3226e85Sdrh 29b3226e85Sdrhproc do_mallocC_test {tn args} { 30b3226e85Sdrh array set ::mallocopts $args 3193aed5a1Sdrh #set sum [allcksum db] 32b3226e85Sdrh 33b3226e85Sdrh for {set ::n 1} {true} {incr ::n} { 34b3226e85Sdrh 35b3226e85Sdrh # Run the SQL. Malloc number $::n is set to fail. A malloc() failure 36b3226e85Sdrh # may or may not be reported. 37a1644fd8Sdanielk1977 sqlite3_memdebug_fail $::n -repeat 1 38b403574aSdrh do_test mallocC-$tn.$::n.1 { 39b3226e85Sdrh set res [catchsql [string trim $::mallocopts(-sql)]] 40b3226e85Sdrh set rc [expr { 41b3226e85Sdrh 0==[string compare $res {1 {out of memory}}] || 42ae72d982Sdanielk1977 [db errorcode] == 3082 || 43b3226e85Sdrh 0==[lindex $res 0] 44b3226e85Sdrh }] 45b3226e85Sdrh if {$rc!=1} { 46b3226e85Sdrh puts "Error: $res" 47b3226e85Sdrh } 48b3226e85Sdrh set rc 49b3226e85Sdrh } {1} 50b3226e85Sdrh 51b3226e85Sdrh # If $::n is greater than the number of malloc() calls required to 52b3226e85Sdrh # execute the SQL, then this test is finished. Break out of the loop. 53a1644fd8Sdanielk1977 set nFail [sqlite3_memdebug_fail -1] 54ed138fb3Sdrh if {$nFail==0} { 55b3226e85Sdrh break 56b3226e85Sdrh } 57b3226e85Sdrh 58b3226e85Sdrh # Recover from the malloc failure. 59b3226e85Sdrh # 60b3226e85Sdrh # Update: The new malloc() failure handling means that a transaction may 61b3226e85Sdrh # still be active even if a malloc() has failed. But when these tests were 62b3226e85Sdrh # written this was not the case. So do a manual ROLLBACK here so that the 63b3226e85Sdrh # tests pass. 64b403574aSdrh do_test mallocC-$tn.$::n.2 { 65b3226e85Sdrh catch { 66b3226e85Sdrh execsql { 67b3226e85Sdrh ROLLBACK; 68b3226e85Sdrh } 69b3226e85Sdrh } 70b3226e85Sdrh expr 0 71b3226e85Sdrh } {0} 72b3226e85Sdrh 73b3226e85Sdrh # Checksum the database. 74b403574aSdrh #do_test mallocC-$tn.$::n.3 { 7593aed5a1Sdrh # allcksum db 76b3226e85Sdrh #} $sum 77b3226e85Sdrh 78b403574aSdrh #integrity_check mallocC-$tn.$::n.4 79b3226e85Sdrh } 80b3226e85Sdrh unset ::mallocopts 81b3226e85Sdrh} 82b3226e85Sdrh 83ae72d982Sdanielk1977sqlite3_extended_result_codes db 1 84ae72d982Sdanielk1977 85b3226e85Sdrhexecsql { 86b3226e85Sdrh PRAGMA auto_vacuum=1; 87b3226e85Sdrh CREATE TABLE t0(a, b, c); 88b3226e85Sdrh} 89*66fd2160Sdrh 90*66fd2160Sdrh# The number of memory allocation failures is different on 64-bit 91*66fd2160Sdrh# and 32-bit systems due to larger structures on 64-bit systems 92*66fd2160Sdrh# overflowing the lookaside more often. To debug problems, it is 93*66fd2160Sdrh# sometimes helpful to reduce the size of the lookaside allocation 94*66fd2160Sdrh# blocks. But this is normally disabled. 95*66fd2160Sdrh# 96*66fd2160Sdrhif {0} { 97*66fd2160Sdrh db close 98*66fd2160Sdrh sqlite3_shutdown 99*66fd2160Sdrh sqlite3_config_lookaside 50 500 100*66fd2160Sdrh sqlite3_initialize 101*66fd2160Sdrh autoinstall_test_functions 102*66fd2160Sdrh sqlite3 db test.db 103*66fd2160Sdrh} 104*66fd2160Sdrh 105b3226e85Sdrhdo_mallocC_test 1 -sql { 106b3226e85Sdrh BEGIN; 107b3226e85Sdrh -- Allocate 32 new root pages. This will exercise the 'extract specific 108b3226e85Sdrh -- page from the freelist' code when in auto-vacuum mode (see the 109b3226e85Sdrh -- allocatePage() routine in btree.c). 110b3226e85Sdrh CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE); 111b3226e85Sdrh CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE); 112b3226e85Sdrh CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE); 113b3226e85Sdrh CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE); 114b3226e85Sdrh CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE); 115b3226e85Sdrh CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE); 116b3226e85Sdrh CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE); 117b3226e85Sdrh CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE); 118b3226e85Sdrh 119b3226e85Sdrh ROLLBACK; 120b3226e85Sdrh} 121b3226e85Sdrh 122b3226e85Sdrhfinish_test 123