drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 1 | # 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 | # |
drh | 66fd216 | 2009-04-11 16:27:49 +0000 | [diff] [blame^] | 15 | # $Id: mallocC.test,v 1.10 2009/04/11 16:27:50 drh Exp $ |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 19 | source $testdir/malloc_common.tcl |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 20 | |
| 21 | # Only run these tests if memory debugging is turned on. |
| 22 | # |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 23 | if {!$MEMDEBUG} { |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 24 | puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..." |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 25 | finish_test |
| 26 | return |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 27 | } |
| 28 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 29 | proc do_mallocC_test {tn args} { |
| 30 | array set ::mallocopts $args |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 31 | #set sum [allcksum db] |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 32 | |
| 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. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 37 | sqlite3_memdebug_fail $::n -repeat 1 |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 38 | do_test mallocC-$tn.$::n.1 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 39 | set res [catchsql [string trim $::mallocopts(-sql)]] |
| 40 | set rc [expr { |
| 41 | 0==[string compare $res {1 {out of memory}}] || |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 42 | [db errorcode] == 3082 || |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 43 | 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. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 53 | set nFail [sqlite3_memdebug_fail -1] |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 54 | if {$nFail==0} { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 55 | 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. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 64 | do_test mallocC-$tn.$::n.2 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 65 | catch { |
| 66 | execsql { |
| 67 | ROLLBACK; |
| 68 | } |
| 69 | } |
| 70 | expr 0 |
| 71 | } {0} |
| 72 | |
| 73 | # Checksum the database. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 74 | #do_test mallocC-$tn.$::n.3 { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 75 | # allcksum db |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 76 | #} $sum |
| 77 | |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 78 | #integrity_check mallocC-$tn.$::n.4 |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 79 | if {$::nErr>1} return |
| 80 | } |
| 81 | unset ::mallocopts |
| 82 | } |
| 83 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 84 | sqlite3_extended_result_codes db 1 |
| 85 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 86 | execsql { |
| 87 | PRAGMA auto_vacuum=1; |
| 88 | CREATE TABLE t0(a, b, c); |
| 89 | } |
drh | 66fd216 | 2009-04-11 16:27:49 +0000 | [diff] [blame^] | 90 | |
| 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 | # |
| 97 | if {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 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 106 | do_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 | |
| 123 | finish_test |