malloc.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. # 2001 September 15
  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 attempts to check the behavior of the SQLite library in
  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)
  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
  17. # due to an out-of-memory situation.
  18. #
  19. # $Id: malloc.test,v 1.81 2009/06/24 13:13:45 drh Exp $
  20. set testdir [file dirname $argv0]
  21. source $testdir/tester.tcl
  22. # Only run these tests if memory debugging is turned on.
  23. #
  24. source $testdir/malloc_common.tcl
  25. if {!$MEMDEBUG} {
  26. puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
  27. finish_test
  28. return
  29. }
  30. # Do a couple of memory dumps just to exercise the memory dump logic
  31. # that that we can say that we have.
  32. #
  33. puts stderr "This is a test. Ignore the error that follows:"
  34. sqlite3_memdebug_dump $testdir
  35. puts "Memory dump to file memdump.txt..."
  36. sqlite3_memdebug_dump memdump.txt
  37. ifcapable bloblit&&subquery {
  38. do_malloc_test 1 -tclprep {
  39. db close
  40. } -tclbody {
  41. if {[catch {sqlite3 db test.db}]} {
  42. error "out of memory"
  43. }
  44. sqlite3_extended_result_codes db 1
  45. } -sqlbody {
  46. DROP TABLE IF EXISTS t1;
  47. CREATE TABLE t1(
  48. a int, b float, c double, d text, e varchar(20),
  49. primary key(a,b,c)
  50. );
  51. CREATE INDEX i1 ON t1(a,b);
  52. INSERT INTO t1 VALUES(1,2.3,4.5,'hi',x'746865726500');
  53. INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
  54. SELECT * FROM t1;
  55. SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
  56. DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
  57. SELECT count(*), group_concat(e) FROM t1;
  58. SELECT b FROM t1 ORDER BY 1 COLLATE nocase;
  59. }
  60. }
  61. # Ensure that no file descriptors were leaked.
  62. do_test malloc-1.X {
  63. catch {db close}
  64. set sqlite_open_file_count
  65. } {0}
  66. ifcapable subquery {
  67. do_malloc_test 2 -sqlbody {
  68. CREATE TABLE t1(a int, b int default 'abc', c int default 1);
  69. CREATE INDEX i1 ON t1(a,b);
  70. INSERT INTO t1 VALUES(1,1,'99 abcdefghijklmnopqrstuvwxyz');
  71. INSERT INTO t1 VALUES(2,4,'98 abcdefghijklmnopqrstuvwxyz');
  72. INSERT INTO t1 VALUES(3,9,'97 abcdefghijklmnopqrstuvwxyz');
  73. INSERT INTO t1 VALUES(4,16,'96 abcdefghijklmnopqrstuvwxyz');
  74. INSERT INTO t1 VALUES(5,25,'95 abcdefghijklmnopqrstuvwxyz');
  75. INSERT INTO t1 VALUES(6,36,'94 abcdefghijklmnopqrstuvwxyz');
  76. SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
  77. UPDATE t1 SET b=b||b||b||b;
  78. UPDATE t1 SET b=a WHERE a in (10,12,22);
  79. INSERT INTO t1(c,b,a) VALUES(20,10,5);
  80. INSERT INTO t1 SELECT * FROM t1
  81. WHERE a IN (SELECT a FROM t1 WHERE a<10);
  82. DELETE FROM t1 WHERE a>=10;
  83. DROP INDEX i1;
  84. DELETE FROM t1;
  85. }
  86. }
  87. # Ensure that no file descriptors were leaked.
  88. do_test malloc-2.X {
  89. catch {db close}
  90. set sqlite_open_file_count
  91. } {0}
  92. do_malloc_test 3 -sqlbody {
  93. BEGIN TRANSACTION;
  94. CREATE TABLE t1(a int, b int, c int);
  95. CREATE INDEX i1 ON t1(a,b);
  96. INSERT INTO t1 VALUES(1,1,99);
  97. INSERT INTO t1 VALUES(2,4,98);
  98. INSERT INTO t1 VALUES(3,9,97);
  99. INSERT INTO t1 VALUES(4,16,96);
  100. INSERT INTO t1 VALUES(5,25,95);
  101. INSERT INTO t1 VALUES(6,36,94);
  102. INSERT INTO t1(c,b,a) VALUES(20,10,5);
  103. DELETE FROM t1 WHERE a>=10;
  104. DROP INDEX i1;
  105. DELETE FROM t1;
  106. ROLLBACK;
  107. }
  108. # Ensure that no file descriptors were leaked.
  109. do_test malloc-3.X {
  110. catch {db close}
  111. set sqlite_open_file_count
  112. } {0}
  113. ifcapable subquery {
  114. do_malloc_test 4 -sqlbody {
  115. BEGIN TRANSACTION;
  116. CREATE TABLE t1(a int, b int, c int);
  117. CREATE INDEX i1 ON t1(a,b);
  118. INSERT INTO t1 VALUES(1,1,99);
  119. INSERT INTO t1 VALUES(2,4,98);
  120. INSERT INTO t1 VALUES(3,9,97);
  121. INSERT INTO t1 VALUES(4,16,96);
  122. INSERT INTO t1 VALUES(5,25,95);
  123. INSERT INTO t1 VALUES(6,36,94);
  124. UPDATE t1 SET b=a WHERE a in (10,12,22);
  125. INSERT INTO t1 SELECT * FROM t1
  126. WHERE a IN (SELECT a FROM t1 WHERE a<10);
  127. DROP INDEX i1;
  128. DELETE FROM t1;
  129. COMMIT;
  130. }
  131. }
  132. # Ensure that no file descriptors were leaked.
  133. do_test malloc-4.X {
  134. catch {db close}
  135. set sqlite_open_file_count
  136. } {0}
  137. ifcapable trigger {
  138. do_malloc_test 5 -sqlbody {
  139. BEGIN TRANSACTION;
  140. CREATE TABLE t1(a,b);
  141. CREATE TABLE t2(x,y);
  142. CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a = 2 BEGIN
  143. INSERT INTO t2(x,y) VALUES(new.rowid,1);
  144. INSERT INTO t2(x,y) SELECT * FROM t2;
  145. INSERT INTO t2 SELECT * FROM t2;
  146. UPDATE t2 SET y=y+1 WHERE x=new.rowid;
  147. SELECT 123;
  148. DELETE FROM t2 WHERE x=new.rowid;
  149. END;
  150. INSERT INTO t1(a,b) VALUES(2,3);
  151. COMMIT;
  152. }
  153. }
  154. # Ensure that no file descriptors were leaked.
  155. do_test malloc-5.X {
  156. catch {db close}
  157. set sqlite_open_file_count
  158. } {0}
  159. ifcapable vacuum {
  160. do_malloc_test 6 -sqlprep {
  161. BEGIN TRANSACTION;
  162. CREATE TABLE t1(a);
  163. INSERT INTO t1 VALUES(1);
  164. INSERT INTO t1 SELECT a*2 FROM t1;
  165. INSERT INTO t1 SELECT a*2 FROM t1;
  166. INSERT INTO t1 SELECT a*2 FROM t1;
  167. INSERT INTO t1 SELECT a*2 FROM t1;
  168. INSERT INTO t1 SELECT a*2 FROM t1;
  169. INSERT INTO t1 SELECT a*2 FROM t1;
  170. INSERT INTO t1 SELECT a*2 FROM t1;
  171. INSERT INTO t1 SELECT a*2 FROM t1;
  172. INSERT INTO t1 SELECT a*2 FROM t1;
  173. INSERT INTO t1 SELECT a*2 FROM t1;
  174. DELETE FROM t1 where rowid%5 = 0;
  175. COMMIT;
  176. } -sqlbody {
  177. VACUUM;
  178. }
  179. }
  180. autoinstall_test_functions
  181. do_malloc_test 7 -sqlprep {
  182. CREATE TABLE t1(a, b);
  183. INSERT INTO t1 VALUES(1, 2);
  184. INSERT INTO t1 VALUES(3, 4);
  185. INSERT INTO t1 VALUES(5, 6);
  186. INSERT INTO t1 VALUES(7, randstr(1200,1200));
  187. } -sqlbody {
  188. SELECT min(a) FROM t1 WHERE a<6 GROUP BY b;
  189. SELECT a FROM t1 WHERE a<6 ORDER BY a;
  190. SELECT b FROM t1 WHERE a>6;
  191. }
  192. # This block is designed to test that some malloc failures that may
  193. # occur in vdbeapi.c. Specifically, if a malloc failure that occurs
  194. # when converting UTF-16 text to integers and real numbers is handled
  195. # correctly.
  196. #
  197. # This is done by retrieving a string from the database engine and
  198. # manipulating it using the sqlite3_column_*** APIs. This doesn't
  199. # actually return an error to the user when a malloc() fails.. That
  200. # could be viewed as a bug.
  201. #
  202. # These tests only run if UTF-16 support is compiled in.
  203. #
  204. ifcapable utf16 {
  205. set ::STMT {}
  206. do_malloc_test 8 -tclprep {
  207. set sql "SELECT '[string repeat abc 20]', '[string repeat def 20]', ?"
  208. set ::STMT [sqlite3_prepare db $sql -1 X]
  209. sqlite3_step $::STMT
  210. if { $::tcl_platform(byteOrder)=="littleEndian" } {
  211. set ::bomstr "\xFF\xFE"
  212. } else {
  213. set ::bomstr "\xFE\xFF"
  214. }
  215. append ::bomstr [encoding convertto unicode "123456789_123456789_123456789"]
  216. } -tclbody {
  217. sqlite3_column_text16 $::STMT 0
  218. sqlite3_column_int $::STMT 0
  219. sqlite3_column_text16 $::STMT 1
  220. sqlite3_column_double $::STMT 1
  221. set rc [sqlite3_reset $::STMT]
  222. if {$rc eq "SQLITE_NOMEM"} {error "out of memory"}
  223. sqlite3_bind_text16 $::STMT 1 $::bomstr 60
  224. #catch {sqlite3_finalize $::STMT}
  225. #if {[lindex [sqlite_malloc_stat] 2]<=0} {
  226. # error "out of memory"
  227. #}
  228. } -cleanup {
  229. if {$::STMT!=""} {
  230. sqlite3_finalize $::STMT
  231. set ::STMT {}
  232. }
  233. }
  234. }
  235. # This block tests that malloc() failures that occur whilst commiting
  236. # a multi-file transaction are handled correctly.
  237. #
  238. do_malloc_test 9 -sqlprep {
  239. ATTACH 'test2.db' as test2;
  240. CREATE TABLE abc1(a, b, c);
  241. CREATE TABLE test2.abc2(a, b, c);
  242. } -sqlbody {
  243. BEGIN;
  244. INSERT INTO abc1 VALUES(1, 2, 3);
  245. INSERT INTO abc2 VALUES(1, 2, 3);
  246. COMMIT;
  247. }
  248. # This block tests malloc() failures that occur while opening a
  249. # connection to a database.
  250. do_malloc_test 10 -tclprep {
  251. catch {db2 close}
  252. db close
  253. forcedelete test.db test.db-journal
  254. sqlite3 db test.db
  255. sqlite3_extended_result_codes db 1
  256. db eval {CREATE TABLE abc(a, b, c)}
  257. } -tclbody {
  258. db close
  259. sqlite3 db2 test.db
  260. sqlite3_extended_result_codes db2 1
  261. db2 eval {SELECT * FROM sqlite_master}
  262. db2 close
  263. }
  264. # This block tests malloc() failures that occur within calls to
  265. # sqlite3_create_function().
  266. do_malloc_test 11 -tclbody {
  267. set rc [sqlite3_create_function db]
  268. if {[string match $rc SQLITE_OK]} {
  269. set rc [sqlite3_create_aggregate db]
  270. }
  271. if {[string match $rc SQLITE_NOMEM]} {
  272. error "out of memory"
  273. }
  274. }
  275. do_malloc_test 12 -tclbody {
  276. set sql16 [encoding convertto unicode "SELECT * FROM sqlite_master"]
  277. append sql16 "\00\00"
  278. set ::STMT [sqlite3_prepare16 db $sql16 -1 DUMMY]
  279. sqlite3_finalize $::STMT
  280. }
  281. # Test malloc errors when replaying two hot journals from a 2-file
  282. # transaction.
  283. ifcapable crashtest&&attach {
  284. do_malloc_test 13 -tclprep {
  285. set rc [crashsql -delay 1 -file test2.db {
  286. ATTACH 'test2.db' as aux;
  287. PRAGMA cache_size = 10;
  288. BEGIN;
  289. CREATE TABLE aux.t2(a, b, c);
  290. CREATE TABLE t1(a, b, c);
  291. COMMIT;
  292. }]
  293. if {$rc!="1 {child process exited abnormally}"} {
  294. error "Wrong error message: $rc"
  295. }
  296. } -tclbody {
  297. db eval {ATTACH 'test2.db' as aux;}
  298. set rc [catch {db eval {
  299. SELECT * FROM t1;
  300. SELECT * FROM t2;
  301. }} err]
  302. if {$rc && $err!="no such table: t1"} {
  303. error $err
  304. }
  305. }
  306. }
  307. if {$tcl_platform(platform)!="windows"} {
  308. do_malloc_test 14 -tclprep {
  309. catch {db close}
  310. sqlite3 db2 test2.db
  311. sqlite3_extended_result_codes db2 1
  312. db2 eval {
  313. PRAGMA journal_mode = DELETE; /* For inmemory_journal permutation */
  314. PRAGMA synchronous = 0;
  315. CREATE TABLE t1(a, b);
  316. INSERT INTO t1 VALUES(1, 2);
  317. BEGIN;
  318. INSERT INTO t1 VALUES(3, 4);
  319. }
  320. forcecopy test2.db test.db
  321. forcecopy test2.db-journal test.db-journal
  322. db2 close
  323. } -tclbody {
  324. sqlite3 db test.db
  325. sqlite3_extended_result_codes db 1
  326. # If an out-of-memory occurs within a call to a VFS layer function during
  327. # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
  328. # [5668] for details.
  329. set rc [catch {db eval { SELECT * FROM t1 }} msg]
  330. if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
  331. if {$rc} { error $msg }
  332. set msg
  333. }
  334. }
  335. proc string_compare {a b} {
  336. return [string compare $a $b]
  337. }
  338. # Test for malloc() failures in sqlite3_create_collation() and
  339. # sqlite3_create_collation16().
  340. #
  341. ifcapable utf16 {
  342. do_malloc_test 15 -start 4 -tclbody {
  343. db collate string_compare string_compare
  344. if {[catch {add_test_collate db 1 1 1} msg]} {
  345. if {$msg=="SQLITE_NOMEM"} {set msg "out of memory"}
  346. error $msg
  347. }
  348. db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
  349. db complete {-- Useful comment}
  350. execsql {
  351. CREATE TABLE t1(a, b COLLATE string_compare);
  352. INSERT INTO t1 VALUES(10, 'string');
  353. INSERT INTO t1 VALUES(10, 'string2');
  354. }
  355. }
  356. }
  357. # Also test sqlite3_complete(). There are (currently) no malloc()
  358. # calls in this function, but test anyway against future changes.
  359. #
  360. do_malloc_test 16 -tclbody {
  361. db complete {SELECT "hello """||'world"' [microsoft], * FROM anicetable;}
  362. db complete {-- Useful comment}
  363. db eval {
  364. SELECT * FROM sqlite_master;
  365. }
  366. }
  367. # Test handling of malloc() failures in sqlite3_open16().
  368. #
  369. ifcapable utf16 {
  370. do_malloc_test 17 -tclbody {
  371. set DB2 0
  372. set STMT 0
  373. # open database using sqlite3_open16()
  374. set filename [encoding convertto unicode test.db]
  375. append filename "\x00\x00"
  376. set DB2 [sqlite3_open16 $filename -unused]
  377. if {0==$DB2} {
  378. error "out of memory"
  379. }
  380. sqlite3_extended_result_codes $DB2 1
  381. # Prepare statement
  382. set rc [catch {sqlite3_prepare $DB2 {SELECT * FROM sqlite_master} -1 X} msg]
  383. if {[sqlite3_errcode $DB2] eq "SQLITE_IOERR+12"} {
  384. error "out of memory"
  385. }
  386. if {[regexp ".*automatic extension loading.*" [sqlite3_errmsg $DB2]]} {
  387. error "out of memory"
  388. }
  389. if {$rc} {
  390. error [string range $msg 4 end]
  391. }
  392. set STMT $msg
  393. # Finalize statement
  394. set rc [sqlite3_finalize $STMT]
  395. if {$rc!="SQLITE_OK"} {
  396. error [sqlite3_errmsg $DB2]
  397. }
  398. set STMT 0
  399. # Close database
  400. set rc [sqlite3_close $DB2]
  401. if {$rc!="SQLITE_OK"} {
  402. error [sqlite3_errmsg $DB2]
  403. }
  404. set DB2 0
  405. } -cleanup {
  406. if {$STMT!="0"} {
  407. sqlite3_finalize $STMT
  408. }
  409. if {$DB2!="0"} {
  410. set rc [sqlite3_close $DB2]
  411. }
  412. }
  413. }
  414. # Test handling of malloc() failures in sqlite3_errmsg16().
  415. #
  416. ifcapable utf16 {
  417. do_malloc_test 18 -tclprep {
  418. catch {
  419. db eval "SELECT [string repeat longcolumnname 10] FROM sqlite_master"
  420. }
  421. } -tclbody {
  422. set utf16 [sqlite3_errmsg16 [sqlite3_connection_pointer db]]
  423. binary scan $utf16 c* bytes
  424. if {[llength $bytes]==0} {
  425. error "out of memory"
  426. }
  427. }
  428. }
  429. # This test is aimed at coverage testing. Specificly, it is supposed to
  430. # cause a malloc() only used when converting between the two utf-16
  431. # encodings to fail (i.e. little-endian->big-endian). It only actually
  432. # hits this malloc() on little-endian hosts.
  433. #
  434. set static_string "\x00h\x00e\x00l\x00l\x00o"
  435. for {set l 0} {$l<10} {incr l} {
  436. append static_string $static_string
  437. }
  438. append static_string "\x00\x00"
  439. do_malloc_test 19 -tclprep {
  440. execsql {
  441. PRAGMA encoding = "UTF16be";
  442. CREATE TABLE abc(a, b, c);
  443. }
  444. } -tclbody {
  445. unset -nocomplain ::STMT
  446. set r [catch {
  447. set ::STMT [sqlite3_prepare db {SELECT ?} -1 DUMMY]
  448. sqlite3_bind_text16 -static $::STMT 1 $static_string 112
  449. } msg]
  450. if {$r} {error [string range $msg 4 end]}
  451. set msg
  452. } -cleanup {
  453. if {[info exists ::STMT]} {
  454. sqlite3_finalize $::STMT
  455. }
  456. }
  457. unset static_string
  458. # Make sure SQLITE_NOMEM is reported out on an ATTACH failure even
  459. # when the malloc failure occurs within the nested parse.
  460. #
  461. ifcapable attach {
  462. do_malloc_test 20 -tclprep {
  463. db close
  464. forcedelete test2.db test2.db-journal
  465. sqlite3 db test2.db
  466. sqlite3_extended_result_codes db 1
  467. db eval {CREATE TABLE t1(x);}
  468. db close
  469. } -tclbody {
  470. if {[catch {sqlite3 db test.db}]} {
  471. error "out of memory"
  472. }
  473. sqlite3_extended_result_codes db 1
  474. } -sqlbody {
  475. ATTACH DATABASE 'test2.db' AS t2;
  476. SELECT * FROM t1;
  477. DETACH DATABASE t2;
  478. }
  479. }
  480. # Test malloc failure whilst installing a foreign key.
  481. #
  482. ifcapable foreignkey {
  483. do_malloc_test 21 -sqlbody {
  484. CREATE TABLE abc(a, b, c, FOREIGN KEY(a) REFERENCES abc(b))
  485. }
  486. }
  487. # Test malloc failure in an sqlite3_prepare_v2() call.
  488. #
  489. do_malloc_test 22 -tclbody {
  490. set ::STMT ""
  491. set r [catch {
  492. set ::STMT [
  493. sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 DUMMY
  494. ]
  495. } msg]
  496. if {$r} {error [string range $msg 4 end]}
  497. } -cleanup {
  498. if {$::STMT ne ""} {
  499. sqlite3_finalize $::STMT
  500. set ::STMT ""
  501. }
  502. }
  503. ifcapable {pager_pragmas} {
  504. # This tests a special case - that an error that occurs while the pager
  505. # is trying to recover from error-state in exclusive-access mode works.
  506. #
  507. do_malloc_test 23 -tclprep {
  508. db eval {
  509. PRAGMA cache_size = 10;
  510. PRAGMA locking_mode = exclusive;
  511. BEGIN;
  512. CREATE TABLE abc(a, b, c);
  513. CREATE INDEX abc_i ON abc(a, b, c);
  514. INSERT INTO abc
  515. VALUES(randstr(100,100), randstr(100,100), randstr(100,100));
  516. INSERT INTO abc
  517. SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
  518. INSERT INTO abc
  519. SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
  520. INSERT INTO abc
  521. SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
  522. INSERT INTO abc
  523. SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
  524. INSERT INTO abc
  525. SELECT randstr(100,100), randstr(100,100), randstr(100,100) FROM abc;
  526. COMMIT;
  527. }
  528. # This puts the pager into error state.
  529. #
  530. db eval BEGIN
  531. db eval {UPDATE abc SET a = 0 WHERE oid%2}
  532. set ::sqlite_io_error_pending 10
  533. catch {db eval {ROLLBACK}} msg
  534. } -tclbody {
  535. # If an out-of-memory occurs within a call to a VFS layer function during
  536. # hot-journal rollback, sqlite will report SQLITE_CORRUPT. See commit
  537. # [5668] for details.
  538. set rc [catch {db eval { SELECT * FROM abc LIMIT 10 }} msg]
  539. if {$msg eq "database disk image is malformed"} { set msg "out of memory" }
  540. if {$rc} { error $msg }
  541. set msg
  542. } -cleanup {
  543. set e [db eval {PRAGMA integrity_check}]
  544. if {$e ne "ok"} {error $e}
  545. }
  546. }
  547. ifcapable compound {
  548. do_malloc_test 24 -sqlprep {
  549. CREATE TABLE t1(a, b, c)
  550. } -sqlbody {
  551. SELECT 1 FROM t1 UNION SELECT 2 FROM t1 ORDER BY 1
  552. }
  553. }
  554. ifcapable view&&trigger {
  555. do_malloc_test 25 -sqlprep {
  556. CREATE TABLE t1(a, b, c);
  557. CREATE VIEW v1 AS SELECT * FROM t1;
  558. CREATE TRIGGER v1t1 INSTEAD OF DELETE ON v1 BEGIN SELECT 1; END;
  559. CREATE TRIGGER v1t2 INSTEAD OF INSERT ON v1 BEGIN SELECT 1; END;
  560. CREATE TRIGGER v1t3 INSTEAD OF UPDATE ON v1 BEGIN SELECT 1; END;
  561. } -sqlbody {
  562. DELETE FROM v1 WHERE a = 1;
  563. INSERT INTO v1 VALUES(1, 2, 3);
  564. UPDATE v1 SET a = 1 WHERE b = 2;
  565. }
  566. }
  567. do_malloc_test 25 -sqlprep {
  568. CREATE TABLE abc(a, b, c);
  569. CREATE INDEX i1 ON abc(a, b);
  570. INSERT INTO abc VALUES(1, 2, 3);
  571. INSERT INTO abc VALUES(4, 5, 6);
  572. } -tclbody {
  573. # For each UPDATE executed, the cursor used for the SELECT statement
  574. # must be "saved". Because the cursor is open on an index, this requires
  575. # a malloc() to allocate space to save the index key. This test case is
  576. # aimed at testing the response of the library to a failure in that
  577. # particular malloc() call.
  578. db eval {SELECT a FROM abc ORDER BY a} {
  579. db eval {UPDATE abc SET b = b - 1 WHERE a = $a}
  580. }
  581. }
  582. # This test is designed to test a specific juncture in the sqlite code.
  583. # The database set up by -sqlprep script contains a single table B-Tree
  584. # of height 2. In the -tclbody script, the existing database connection
  585. # is closed and a new one opened and used to insert a new row into the
  586. # table B-Tree. By using a new connection, the outcome of a malloc()
  587. # failure while seeking to the right-hand side of the B-Tree to insert
  588. # a new record can be tested.
  589. #
  590. do_malloc_test 26 -sqlprep {
  591. BEGIN;
  592. CREATE TABLE t1(a, b);
  593. INSERT INTO t1 VALUES(1, randomblob(210));
  594. INSERT INTO t1 VALUES(1, randomblob(210));
  595. INSERT INTO t1 VALUES(1, randomblob(210));
  596. INSERT INTO t1 VALUES(1, randomblob(210));
  597. INSERT INTO t1 VALUES(1, randomblob(210));
  598. COMMIT;
  599. } -tclbody {
  600. db close
  601. sqlite3 db test.db
  602. db eval { INSERT INTO t1 VALUES(1, randomblob(210)) }
  603. }
  604. # Test that no memory is leaked following a malloc() failure in
  605. # sqlite3_initialize().
  606. #
  607. do_malloc_test 27 -tclprep {
  608. db close
  609. sqlite3_shutdown
  610. } -tclbody {
  611. set rc [sqlite3_initialize]
  612. if {$rc == "SQLITE_NOMEM"} {
  613. error "out of memory"
  614. }
  615. }
  616. autoinstall_test_functions
  617. # Test that malloc failures that occur while processing INDEXED BY
  618. # clauses are handled correctly.
  619. do_malloc_test 28 -sqlprep {
  620. CREATE TABLE t1(a, b);
  621. CREATE INDEX i1 ON t1(a);
  622. CREATE VIEW v1 AS SELECT * FROM t1 INDEXED BY i1 WHERE a = 10;
  623. } -sqlbody {
  624. SELECT * FROM t1 INDEXED BY i1 ORDER BY a;
  625. SELECT * FROM v1;
  626. }
  627. do_malloc_test 29 -sqlprep {
  628. CREATE TABLE t1(a TEXT, b TEXT);
  629. } -sqlbody {
  630. INSERT INTO t1 VALUES(1, -234);
  631. INSERT INTO t1 SELECT * FROM t1 UNION ALL SELECT * FROM t1;
  632. }
  633. do_malloc_test 30 -tclprep {
  634. db eval {
  635. CREATE TABLE t1(x PRIMARY KEY);
  636. INSERT INTO t1 VALUES(randstr(500,500));
  637. INSERT INTO t1 VALUES(randstr(500,500));
  638. INSERT INTO t1 VALUES(randstr(500,500));
  639. }
  640. db close
  641. sqlite3 db test.db
  642. # The DELETE command in the following block moves the overflow pages that
  643. # are part of the primary key index to the free-list. But it does not
  644. # actually load the content of the pages. This leads to the peculiar
  645. # situation where cache entries exist, but are not populated with data.
  646. # They are populated next time they are requested by the b-tree layer.
  647. #
  648. db eval {
  649. BEGIN;
  650. DELETE FROM t1;
  651. ROLLBACK;
  652. }
  653. } -sqlbody {
  654. -- This statement requires the 'no-content' pages loaded by the DELETE
  655. -- statement above. When requesting the pages, the content is loaded
  656. -- from the database file. The point of this test case is to test handling
  657. -- of malloc errors (including SQLITE_IOERR_NOMEM errors) when loading
  658. -- the content.
  659. SELECT * FROM t1 ORDER BY x;
  660. }
  661. # After committing a transaction in persistent-journal mode, if a journal
  662. # size limit is configured SQLite may attempt to truncate the journal file.
  663. # This test verifies the libraries response to a malloc() failure during
  664. # this operation.
  665. #
  666. do_malloc_test 31 -sqlprep {
  667. PRAGMA journal_mode = persist;
  668. PRAGMA journal_size_limit = 1024;
  669. CREATE TABLE t1(a PRIMARY KEY, b);
  670. } -sqlbody {
  671. INSERT INTO t1 VALUES(1, 2);
  672. }
  673. # When written, this test provoked an obscure change-counter bug.
  674. #
  675. # If, when running in exclusive mode, a malloc() failure occurs
  676. # after the database file change-counter has been written but
  677. # before the transaction has been committed, then the transaction
  678. # is automatically rolled back. However, internally the
  679. # Pager.changeCounterDone flag was being left set. This means
  680. # that if the same connection attempts another transaction following
  681. # the malloc failure and rollback, the change counter will not
  682. # be updated. This could corrupt another processes cache.
  683. #
  684. do_malloc_test 32 -tclprep {
  685. # Build a small database containing an indexed table.
  686. #
  687. db eval {
  688. PRAGMA locking_mode = normal;
  689. BEGIN;
  690. CREATE TABLE t1(a PRIMARY KEY, b);
  691. INSERT INTO t1 VALUES(1, 'one');
  692. INSERT INTO t1 VALUES(2, 'two');
  693. INSERT INTO t1 VALUES(3, 'three');
  694. COMMIT;
  695. PRAGMA locking_mode = exclusive;
  696. }
  697. # Open a second database connection. Load the table (but not index)
  698. # into the second connections pager cache.
  699. #
  700. sqlite3 db2 test.db
  701. db2 eval {
  702. PRAGMA locking_mode = normal;
  703. SELECT b FROM t1;
  704. }
  705. } -tclbody {
  706. # Running in exclusive mode, perform a database transaction that
  707. # modifies both the database table and index. For iterations where
  708. # the malloc failure occurs after updating the change counter but
  709. # before committing the transaction, this should result in the
  710. # transaction being rolled back but the changeCounterDone flag
  711. # left set.
  712. #
  713. db eval { UPDATE t1 SET a = a + 3 }
  714. } -cleanup {
  715. # Perform another transaction using the first connection. Unlock
  716. # the database after doing so. If this is one of the right iterations,
  717. # then this should result in the database contents being updated but
  718. # the change-counter left as it is.
  719. #
  720. db eval {
  721. PRAGMA locking_mode = normal;
  722. UPDATE t1 SET a = a + 3;
  723. }
  724. # Now do an integrity check with the second connection. The second
  725. # connection still has the database table in its cache. If this is
  726. # one of the magic iterations and the change counter was not modified,
  727. # then it won't realize that the cached data is out of date. Since
  728. # the cached data won't match the up to date index data read from
  729. # the database file, the integrity check should fail.
  730. #
  731. set zRepeat "transient"
  732. if {$::iRepeat} {set zRepeat "persistent"}
  733. do_test malloc-32.$zRepeat.${::n}.integrity {
  734. execsql {PRAGMA integrity_check} db2
  735. } {ok}
  736. db2 close
  737. }
  738. # The following two OOM tests verify that OOM handling works in the
  739. # code used to optimize "SELECT count(*) FROM <tbl>".
  740. #
  741. do_malloc_test 33 -tclprep {
  742. db eval { PRAGMA cache_size = 10 }
  743. db transaction {
  744. db eval { CREATE TABLE abc(a, b) }
  745. for {set i 0} {$i<500} {incr i} {
  746. db eval {INSERT INTO abc VALUES(randstr(100,100), randstr(1000,1000))}
  747. }
  748. }
  749. } -sqlbody {
  750. SELECT count(*) FROM abc;
  751. }
  752. do_malloc_test 34 -tclprep {
  753. db eval { PRAGMA cache_size = 10 }
  754. db transaction {
  755. db eval { CREATE TABLE abc(a PRIMARY KEY, b) }
  756. for {set i 0} {$i<500} {incr i} {
  757. db eval {INSERT INTO abc VALUES(randstr(100,100), randstr(1000,1000))}
  758. }
  759. }
  760. } -sqlbody {
  761. SELECT count(*) FROM abc;
  762. }
  763. proc f {args} { error "Quite a long error!" }
  764. do_malloc_test 35 -tclprep {
  765. db func f f
  766. set ::STMT [sqlite3_prepare db "SELECT f()" -1 DUMMY]
  767. sqlite3_step $::STMT
  768. } -tclbody {
  769. sqlite3_finalize $::STMT
  770. } -cleanup {
  771. # At one point an assert( !db->mallocFailed ) could fail in the following
  772. # call to sqlite3_errmsg(). Because sqlite3_finalize() had failed to clear
  773. # the flag before returning.
  774. sqlite3_errmsg16 db
  775. }
  776. do_malloc_test 36 -sqlprep {
  777. CREATE TABLE t1(a, b);
  778. INSERT INTO t1 VALUES(1, 2);
  779. INSERT INTO t1 VALUES(3, 4);
  780. } -sqlbody {
  781. SELECT test_agg_errmsg16(), group_concat(a) FROM t1
  782. }
  783. # At one point, if an OOM occurred immediately after obtaining a shared lock
  784. # on the database file, the file remained locked. This test case ensures
  785. # that bug has been fixed.i
  786. if {[db eval {PRAGMA locking_mode}]!="exclusive"} {
  787. do_malloc_test 37 -tclprep {
  788. sqlite3 db2 test.db
  789. execsql {
  790. CREATE TABLE t1(a, b);
  791. INSERT INTO t1 VALUES(1, 2);
  792. } db2
  793. } -sqlbody {
  794. SELECT * FROM t1;
  795. } -cleanup {
  796. # Try to write to the database using connection [db2]. If connection [db]
  797. # has correctly released the shared lock, this write attempt should
  798. # succeed. If [db] has not released the lock, this should hit an
  799. # SQLITE_BUSY error.
  800. do_test malloc-36.$zRepeat.${::n}.unlocked {
  801. execsql {INSERT INTO t1 VALUES(3, 4)} db2
  802. } {}
  803. db2 close
  804. }
  805. catch { db2 close }
  806. }
  807. # Test that if an OOM error occurs, aux-data is still correctly destroyed.
  808. # This test case was causing either a memory-leak or an assert() failure
  809. # at one point, depending on the configuration.
  810. #
  811. do_malloc_test 39 -tclprep {
  812. sqlite3 db test.db
  813. } -sqlbody {
  814. SELECT test_auxdata('abc', 'def');
  815. } -cleanup {
  816. db close
  817. }
  818. # Ensure that no file descriptors were leaked.
  819. do_test malloc-99.X {
  820. catch {db close}
  821. set sqlite_open_file_count
  822. } {0}
  823. puts open-file-count=$sqlite_open_file_count
  824. finish_test