| OLD | NEW |
| 1 # 2001 September 15 | 1 # 2001 September 15 |
| 2 # | 2 # |
| 3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
| 5 # | 5 # |
| 6 # May you do good and not evil. | 6 # May you do good and not evil. |
| 7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
| 8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
| 9 # | 9 # |
| 10 #*********************************************************************** | 10 #*********************************************************************** |
| 11 # | 11 # |
| 12 # This file attempts to check the behavior of the SQLite library in | 12 # This file attempts to check the behavior of the SQLite library in |
| 13 # an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, | 13 # an out-of-memory situation. When compiled with -DSQLITE_DEBUG=1, |
| 14 # the SQLite library accepts a special command (sqlite3_memdebug_fail N C) | 14 # the SQLite library accepts a special command (sqlite3_memdebug_fail N C) |
| 15 # which causes the N-th malloc to fail. This special feature is used | 15 # which causes the N-th malloc to fail. This special feature is used |
| 16 # to see what happens in the library if a malloc were to really fail | 16 # to see what happens in the library if a malloc were to really fail |
| 17 # due to an out-of-memory situation. | 17 # due to an out-of-memory situation. |
| 18 # | 18 # |
| 19 # $Id: malloc.test,v 1.81 2009/06/24 13:13:45 drh Exp $ | 19 # $Id: malloc.test,v 1.81 2009/06/24 13:13:45 drh Exp $ |
| 20 | 20 |
| 21 set testdir [file dirname $argv0] | 21 set testdir [file dirname $argv0] |
| 22 source $testdir/tester.tcl | 22 source $testdir/tester.tcl |
| 23 set ::testprefix malloc |
| 23 | 24 |
| 24 | 25 |
| 25 # Only run these tests if memory debugging is turned on. | 26 # Only run these tests if memory debugging is turned on. |
| 26 # | 27 # |
| 27 source $testdir/malloc_common.tcl | 28 source $testdir/malloc_common.tcl |
| 28 if {!$MEMDEBUG} { | 29 if {!$MEMDEBUG} { |
| 29 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." | 30 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." |
| 30 finish_test | 31 finish_test |
| 31 return | 32 return |
| 32 } | 33 } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 INSERT INTO abc1 VALUES(1, 2, 3); | 264 INSERT INTO abc1 VALUES(1, 2, 3); |
| 264 INSERT INTO abc2 VALUES(1, 2, 3); | 265 INSERT INTO abc2 VALUES(1, 2, 3); |
| 265 COMMIT; | 266 COMMIT; |
| 266 } | 267 } |
| 267 | 268 |
| 268 # This block tests malloc() failures that occur while opening a | 269 # This block tests malloc() failures that occur while opening a |
| 269 # connection to a database. | 270 # connection to a database. |
| 270 do_malloc_test 10 -tclprep { | 271 do_malloc_test 10 -tclprep { |
| 271 catch {db2 close} | 272 catch {db2 close} |
| 272 db close | 273 db close |
| 273 file delete -force test.db test.db-journal | 274 forcedelete test.db test.db-journal |
| 274 sqlite3 db test.db | 275 sqlite3 db test.db |
| 275 sqlite3_extended_result_codes db 1 | 276 sqlite3_extended_result_codes db 1 |
| 276 db eval {CREATE TABLE abc(a, b, c)} | 277 db eval {CREATE TABLE abc(a, b, c)} |
| 277 } -tclbody { | 278 } -tclbody { |
| 278 db close | 279 db close |
| 279 sqlite3 db2 test.db | 280 sqlite3 db2 test.db |
| 280 sqlite3_extended_result_codes db2 1 | 281 sqlite3_extended_result_codes db2 1 |
| 281 db2 eval {SELECT * FROM sqlite_master} | 282 db2 eval {SELECT * FROM sqlite_master} |
| 282 db2 close | 283 db2 close |
| 283 } | 284 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 sqlite3 db2 test2.db | 335 sqlite3 db2 test2.db |
| 335 sqlite3_extended_result_codes db2 1 | 336 sqlite3_extended_result_codes db2 1 |
| 336 db2 eval { | 337 db2 eval { |
| 337 PRAGMA journal_mode = DELETE; /* For inmemory_journal permutation */ | 338 PRAGMA journal_mode = DELETE; /* For inmemory_journal permutation */ |
| 338 PRAGMA synchronous = 0; | 339 PRAGMA synchronous = 0; |
| 339 CREATE TABLE t1(a, b); | 340 CREATE TABLE t1(a, b); |
| 340 INSERT INTO t1 VALUES(1, 2); | 341 INSERT INTO t1 VALUES(1, 2); |
| 341 BEGIN; | 342 BEGIN; |
| 342 INSERT INTO t1 VALUES(3, 4); | 343 INSERT INTO t1 VALUES(3, 4); |
| 343 } | 344 } |
| 344 copy_file test2.db test.db | 345 forcecopy test2.db test.db |
| 345 copy_file test2.db-journal test.db-journal | 346 forcecopy test2.db-journal test.db-journal |
| 346 db2 close | 347 db2 close |
| 347 } -tclbody { | 348 } -tclbody { |
| 348 sqlite3 db test.db | 349 sqlite3 db test.db |
| 349 sqlite3_extended_result_codes db 1 | 350 sqlite3_extended_result_codes db 1 |
| 350 | 351 |
| 351 # If an out-of-memory occurs within a call to a VFS layer function during | 352 # If an out-of-memory occurs within a call to a VFS layer function during |
| 352 # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit | 353 # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit |
| 353 # [5668] for details. | 354 # [5668] for details. |
| 354 set rc [catch {db eval { SELECT * FROM t1 }} msg] | 355 set rc [catch {db eval { SELECT * FROM t1 }} msg] |
| 355 if {$msg eq "database disk image is malformed"} { set msg "out of memory" } | 356 if {$msg eq "database disk image is malformed"} { set msg "out of memory" } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 } | 493 } |
| 493 } | 494 } |
| 494 unset static_string | 495 unset static_string |
| 495 | 496 |
| 496 # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even | 497 # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even |
| 497 # when the malloc failure occurs within the nested parse. | 498 # when the malloc failure occurs within the nested parse. |
| 498 # | 499 # |
| 499 ifcapable attach { | 500 ifcapable attach { |
| 500 do_malloc_test 20 -tclprep { | 501 do_malloc_test 20 -tclprep { |
| 501 db close | 502 db close |
| 502 file delete -force test2.db test2.db-journal | 503 forcedelete test2.db test2.db-journal |
| 503 sqlite3 db test2.db | 504 sqlite3 db test2.db |
| 504 sqlite3_extended_result_codes db 1 | 505 sqlite3_extended_result_codes db 1 |
| 505 db eval {CREATE TABLE t1(x);} | 506 db eval {CREATE TABLE t1(x);} |
| 506 db close | 507 db close |
| 507 } -tclbody { | 508 } -tclbody { |
| 508 if {[catch {sqlite3 db test.db}]} { | 509 if {[catch {sqlite3 db test.db}]} { |
| 509 error "out of memory" | 510 error "out of memory" |
| 510 } | 511 } |
| 511 sqlite3_extended_result_codes db 1 | 512 sqlite3_extended_result_codes db 1 |
| 512 } -sqlbody { | 513 } -sqlbody { |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 835 } | 836 } |
| 836 | 837 |
| 837 do_malloc_test 36 -sqlprep { | 838 do_malloc_test 36 -sqlprep { |
| 838 CREATE TABLE t1(a, b); | 839 CREATE TABLE t1(a, b); |
| 839 INSERT INTO t1 VALUES(1, 2); | 840 INSERT INTO t1 VALUES(1, 2); |
| 840 INSERT INTO t1 VALUES(3, 4); | 841 INSERT INTO t1 VALUES(3, 4); |
| 841 } -sqlbody { | 842 } -sqlbody { |
| 842 SELECT test_agg_errmsg16(), group_concat(a) FROM t1 | 843 SELECT test_agg_errmsg16(), group_concat(a) FROM t1 |
| 843 } | 844 } |
| 844 | 845 |
| 845 # At one point, if an OOM occured immediately after obtaining a shared lock | 846 # At one point, if an OOM occurred immediately after obtaining a shared lock |
| 846 # on the database file, the file remained locked. This test case ensures | 847 # on the database file, the file remained locked. This test case ensures |
| 847 # that bug has been fixed.i | 848 # that bug has been fixed.i |
| 848 if {[db eval {PRAGMA locking_mode}]!="exclusive"} { | 849 if {[db eval {PRAGMA locking_mode}]!="exclusive"} { |
| 849 do_malloc_test 37 -tclprep { | 850 do_malloc_test 37 -tclprep { |
| 850 sqlite3 db2 test.db | 851 sqlite3 db2 test.db |
| 851 execsql { | 852 execsql { |
| 852 CREATE TABLE t1(a, b); | 853 CREATE TABLE t1(a, b); |
| 853 INSERT INTO t1 VALUES(1, 2); | 854 INSERT INTO t1 VALUES(1, 2); |
| 854 } db2 | 855 } db2 |
| 855 } -sqlbody { | 856 } -sqlbody { |
| 856 SELECT * FROM t1; | 857 SELECT * FROM t1; |
| 857 } -cleanup { | 858 } -cleanup { |
| 858 # Try to write to the database using connection [db2]. If connection [db] | 859 # Try to write to the database using connection [db2]. If connection [db] |
| 859 # has correctly released the shared lock, this write attempt should | 860 # has correctly released the shared lock, this write attempt should |
| 860 # succeed. If [db] has not released the lock, this should hit an | 861 # succeed. If [db] has not released the lock, this should hit an |
| 861 # SQLITE_BUSY error. | 862 # SQLITE_BUSY error. |
| 862 do_test malloc-36.$zRepeat.${::n}.unlocked { | 863 do_test malloc-36.$zRepeat.${::n}.unlocked { |
| 863 execsql {INSERT INTO t1 VALUES(3, 4)} db2 | 864 execsql {INSERT INTO t1 VALUES(3, 4)} db2 |
| 864 } {} | 865 } {} |
| 865 db2 close | 866 db2 close |
| 866 } | 867 } |
| 867 catch { db2 close } | 868 catch { db2 close } |
| 868 } | 869 } |
| 869 | 870 |
| 870 ifcapable stat2&&utf16 { | 871 |
| 871 do_malloc_test 38 -tclprep { | 872 # Test that if an OOM error occurs, aux-data is still correctly destroyed. |
| 872 add_test_collate db 0 0 1 | 873 # This test case was causing either a memory-leak or an assert() failure |
| 873 execsql { | 874 # at one point, depending on the configuration. |
| 874 ANALYZE; | 875 # |
| 875 CREATE TABLE t4(x COLLATE test_collate); | 876 do_malloc_test 39 -tclprep { |
| 876 CREATE INDEX t4x ON t4(x); | 877 sqlite3 db test.db |
| 877 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 0, 'aaa'); | 878 } -sqlbody { |
| 878 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 1, 'aaa'); | 879 SELECT test_auxdata('abc', 'def'); |
| 879 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 2, 'aaa'); | 880 } -cleanup { |
| 880 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 3, 'aaa'); | 881 db close |
| 881 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 4, 'aaa'); | 882 } |
| 882 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 5, 'aaa'); | 883 |
| 883 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 6, 'aaa'); | 884 reset_db |
| 884 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 7, 'aaa'); | 885 add_test_utf16bin_collate db |
| 885 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 8, 'aaa'); | 886 do_execsql_test 40.1 { |
| 886 INSERT INTO sqlite_stat2 VALUES('t4', 't4x', 9, 'aaa'); | 887 CREATE TABLE t1(a); |
| 887 } | 888 INSERT INTO t1 VALUES('fghij'); |
| 888 db close | 889 INSERT INTO t1 VALUES('pqrst'); |
| 889 sqlite3 db test.db | 890 INSERT INTO t1 VALUES('abcde'); |
| 890 sqlite3_db_config_lookaside db 0 0 0 | 891 INSERT INTO t1 VALUES('uvwxy'); |
| 891 add_test_collate db 0 0 1 | 892 INSERT INTO t1 VALUES('klmno'); |
| 892 } -sqlbody { | 893 } |
| 893 SELECT * FROM t4 AS t41, t4 AS t42 WHERE t41.x>'ddd' AND t42.x>'ccc' | 894 do_execsql_test 40.2 { |
| 895 SELECT * FROM t1 ORDER BY 1 COLLATE utf16bin; |
| 896 } {abcde fghij klmno pqrst uvwxy} |
| 897 do_faultsim_test 40.3 -faults oom-trans* -body { |
| 898 execsql { |
| 899 SELECT * FROM t1 ORDER BY 1 COLLATE utf16bin; |
| 894 } | 900 } |
| 901 } -test { |
| 902 faultsim_test_result {0 {abcde fghij klmno pqrst uvwxy}} |
| 903 faultsim_integrity_check |
| 904 } |
| 905 |
| 906 reset_db |
| 907 add_test_utf16bin_collate db |
| 908 set big [string repeat x 200] |
| 909 do_execsql_test 41.1 { |
| 910 DROP TABLE IF EXISTS t1; |
| 911 CREATE TABLE t1(a COLLATE utf16bin); |
| 912 INSERT INTO t1 VALUES('fghij' || $::big); |
| 913 INSERT INTO t1 VALUES('pqrst' || $::big); |
| 914 INSERT INTO t1 VALUES('abcde' || $::big); |
| 915 INSERT INTO t1 VALUES('uvwxy' || $::big); |
| 916 INSERT INTO t1 VALUES('klmno' || $::big); |
| 917 CREATE INDEX i1 ON t1(a); |
| 918 } |
| 919 do_faultsim_test 41.2 -faults oom* -body { |
| 920 execsql { SELECT * FROM t1 WHERE a = ('abcde' || $::big)} |
| 921 } -test { |
| 922 faultsim_test_result [list 0 "abcde$::big"] |
| 923 faultsim_integrity_check |
| 895 } | 924 } |
| 896 | 925 |
| 897 # Ensure that no file descriptors were leaked. | 926 # Ensure that no file descriptors were leaked. |
| 898 do_test malloc-99.X { | 927 do_test malloc-99.X { |
| 899 catch {db close} | 928 catch {db close} |
| 900 set sqlite_open_file_count | 929 set sqlite_open_file_count |
| 901 } {0} | 930 } {0} |
| 902 | 931 |
| 903 puts open-file-count=$sqlite_open_file_count | 932 puts open-file-count=$sqlite_open_file_count |
| 904 finish_test | 933 finish_test |
| OLD | NEW |