malloc3.test 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. # 2005 November 30
  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 contains tests to ensure that the library handles malloc() failures
  13. # correctly. The emphasis of these tests are the _prepare(), _step() and
  14. # _finalize() calls.
  15. #
  16. # $Id: malloc3.test,v 1.24 2008/10/14 15:54:08 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. source $testdir/malloc_common.tcl
  20. # Only run these tests if memory debugging is turned on.
  21. #
  22. if {!$MEMDEBUG} {
  23. puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..."
  24. finish_test
  25. return
  26. }
  27. # Do not run these tests with an in-memory journal.
  28. #
  29. # In the pager layer, if an IO or OOM error occurs during a ROLLBACK, or
  30. # when flushing a page to disk due to cache-stress, the pager enters an
  31. # "error state". The only way out of the error state is to unlock the
  32. # database file and end the transaction, leaving whatever journal and
  33. # database files happen to be on disk in place. The next time the current
  34. # (or any other) connection opens a read transaction, hot-journal rollback
  35. # is performed if necessary.
  36. #
  37. # Of course, this doesn't work with an in-memory journal.
  38. #
  39. if {[permutation]=="inmemory_journal"} {
  40. finish_test
  41. return
  42. }
  43. #--------------------------------------------------------------------------
  44. # NOTES ON RECOVERING FROM A MALLOC FAILURE
  45. #
  46. # The tests in this file test the behaviours described in the following
  47. # paragraphs. These tests test the behaviour of the system when malloc() fails
  48. # inside of a call to _prepare(), _step(), _finalize() or _reset(). The
  49. # handling of malloc() failures within ancillary procedures is tested
  50. # elsewhere.
  51. #
  52. # Overview:
  53. #
  54. # Executing a statement is done in three stages (prepare, step and finalize). A
  55. # malloc() failure may occur within any stage. If a memory allocation fails
  56. # during statement preparation, no statement handle is returned. From the users
  57. # point of view the system state is as if _prepare() had never been called.
  58. #
  59. # If the memory allocation fails during the _step() or _finalize() calls, then
  60. # the database may be left in one of two states (after finalize() has been
  61. # called):
  62. #
  63. # * As if the neither _step() nor _finalize() had ever been called on
  64. # the statement handle (i.e. any changes made by the statement are
  65. # rolled back).
  66. # * The current transaction may be rolled back. In this case a hot-journal
  67. # may or may not actually be present in the filesystem.
  68. #
  69. # The caller can tell the difference between these two scenarios by invoking
  70. # _get_autocommit().
  71. #
  72. #
  73. # Handling of sqlite3_reset():
  74. #
  75. # If a malloc() fails while executing an sqlite3_reset() call, this is handled
  76. # in the same way as a failure within _finalize(). The statement handle
  77. # is not deleted and must be passed to _finalize() for resource deallocation.
  78. # Attempting to _step() or _reset() the statement after a failed _reset() will
  79. # always return SQLITE_NOMEM.
  80. #
  81. #
  82. # Other active SQL statements:
  83. #
  84. # The effect of a malloc failure on concurrently executing SQL statements,
  85. # particularly when the statement is executing with READ_UNCOMMITTED set and
  86. # the malloc() failure mandates statement rollback only. Currently, if
  87. # transaction rollback is required, all other vdbe's are aborted.
  88. #
  89. # Non-transient mallocs in btree.c:
  90. # * The Btree structure itself
  91. # * Each BtCursor structure
  92. #
  93. # Mallocs in pager.c:
  94. # readMasterJournal() - Space to read the master journal name
  95. # pager_delmaster() - Space for the entire master journal file
  96. #
  97. # sqlite3pager_open() - The pager structure itself
  98. # sqlite3_pagerget() - Space for a new page
  99. # pager_open_journal() - Pager.aInJournal[] bitmap
  100. # sqlite3pager_write() - For in-memory databases only: history page and
  101. # statement history page.
  102. # pager_stmt_begin() - Pager.aInStmt[] bitmap
  103. #
  104. # None of the above are a huge problem. The most troublesome failures are the
  105. # transient malloc() calls in btree.c, which can occur during the tree-balance
  106. # operation. This means the tree being balanced will be internally inconsistent
  107. # after the malloc() fails. To avoid the corrupt tree being read by a
  108. # READ_UNCOMMITTED query, we have to make sure the transaction or statement
  109. # rollback occurs before sqlite3_step() returns, not during a subsequent
  110. # sqlite3_finalize().
  111. #--------------------------------------------------------------------------
  112. #--------------------------------------------------------------------------
  113. # NOTES ON TEST IMPLEMENTATION
  114. #
  115. # The tests in this file are implemented differently from those in other
  116. # files. Instead, tests are specified using three primitives: SQL, PREP and
  117. # TEST. Each primitive has a single argument. Primitives are processed in
  118. # the order they are specified in the file.
  119. #
  120. # A TEST primitive specifies a TCL script as its argument. When a TEST
  121. # directive is encountered the Tcl script is evaluated. Usually, this Tcl
  122. # script contains one or more calls to [do_test].
  123. #
  124. # A PREP primitive specifies an SQL script as its argument. When a PREP
  125. # directive is encountered the SQL is evaluated using database connection
  126. # [db].
  127. #
  128. # The SQL primitives are where the action happens. An SQL primitive must
  129. # contain a single, valid SQL statement as its argument. When an SQL
  130. # primitive is encountered, it is evaluated one or more times to test the
  131. # behaviour of the system when malloc() fails during preparation or
  132. # execution of said statement. The Nth time the statement is executed,
  133. # the Nth malloc is said to fail. The statement is executed until it
  134. # succeeds, i.e. (M+1) times, where M is the number of mallocs() required
  135. # to prepare and execute the statement.
  136. #
  137. # Each time an SQL statement fails, the driver program (see proc [run_test]
  138. # below) figures out if a transaction has been automatically rolled back.
  139. # If not, it executes any TEST block immediately proceeding the SQL
  140. # statement, then reexecutes the SQL statement with the next value of N.
  141. #
  142. # If a transaction has been automatically rolled back, then the driver
  143. # program executes all the SQL specified as part of SQL or PREP primitives
  144. # between the current SQL statement and the most recent "BEGIN". Any
  145. # TEST block immediately proceeding the SQL statement is evaluated, and
  146. # then the SQL statement reexecuted with the incremented N value.
  147. #
  148. # That make any sense? If not, read the code in [run_test] and it might.
  149. #
  150. # Extra restriction imposed by the implementation:
  151. #
  152. # * If a PREP block starts a transaction, it must finish it.
  153. # * A PREP block may not close a transaction it did not start.
  154. #
  155. #--------------------------------------------------------------------------
  156. # These procs are used to build up a "program" in global variable
  157. # ::run_test_script. At the end of this file, the proc [run_test] is used
  158. # to execute the program (and all test cases contained therein).
  159. #
  160. set ::run_test_sql_id 0
  161. set ::run_test_script [list]
  162. proc TEST {id t} {lappend ::run_test_script -test [list $id $t]}
  163. proc PREP {p} {lappend ::run_test_script -prep [string trim $p]}
  164. proc DEBUG {s} {lappend ::run_test_script -debug $s}
  165. # SQL --
  166. #
  167. # SQL ?-norollback? <sql-text>
  168. #
  169. # Add an 'SQL' primitive to the program (see notes above). If the -norollback
  170. # switch is present, then the statement is not allowed to automatically roll
  171. # back any active transaction if malloc() fails. It must rollback the statement
  172. # transaction only.
  173. #
  174. proc SQL {a1 {a2 ""}} {
  175. # An SQL primitive parameter is a list of three elements, an id, a boolean
  176. # value indicating if the statement may cause transaction rollback when
  177. # malloc() fails, and the sql statement itself.
  178. set id [incr ::run_test_sql_id]
  179. if {$a2 == ""} {
  180. lappend ::run_test_script -sql [list $id true [string trim $a1]]
  181. } else {
  182. lappend ::run_test_script -sql [list $id false [string trim $a2]]
  183. }
  184. }
  185. # TEST_AUTOCOMMIT --
  186. #
  187. # A shorthand test to see if a transaction is active or not. The first
  188. # argument - $id - is the integer number of the test case. The second
  189. # argument is either 1 or 0, the expected value of the auto-commit flag.
  190. #
  191. proc TEST_AUTOCOMMIT {id a} {
  192. TEST $id "do_test \$testid { sqlite3_get_autocommit \$::DB } {$a}"
  193. }
  194. #--------------------------------------------------------------------------
  195. # Start of test program declaration
  196. #
  197. # Warm body test. A malloc() fails in the middle of a CREATE TABLE statement
  198. # in a single-statement transaction on an empty database. Not too much can go
  199. # wrong here.
  200. #
  201. TEST 1 {
  202. do_test $testid {
  203. execsql {SELECT tbl_name FROM sqlite_master;}
  204. } {}
  205. }
  206. SQL {
  207. CREATE TABLE IF NOT EXISTS abc(a, b, c);
  208. }
  209. TEST 2 {
  210. do_test $testid.1 {
  211. execsql {SELECT tbl_name FROM sqlite_master;}
  212. } {abc}
  213. }
  214. # Insert a couple of rows into the table. each insert is in its own
  215. # transaction. test that the table is unpopulated before running the inserts
  216. # (and hence after each failure of the first insert), and that it has been
  217. # populated correctly after the final insert succeeds.
  218. #
  219. TEST 3 {
  220. do_test $testid.2 {
  221. execsql {SELECT * FROM abc}
  222. } {}
  223. }
  224. SQL {INSERT INTO abc VALUES(1, 2, 3);}
  225. SQL {INSERT INTO abc VALUES(4, 5, 6);}
  226. SQL {INSERT INTO abc VALUES(7, 8, 9);}
  227. TEST 4 {
  228. do_test $testid {
  229. execsql {SELECT * FROM abc}
  230. } {1 2 3 4 5 6 7 8 9}
  231. }
  232. # Test a CREATE INDEX statement. Because the table 'abc' is so small, the index
  233. # will all fit on a single page, so this doesn't test too much that the CREATE
  234. # TABLE statement didn't test. A few of the transient malloc()s in btree.c
  235. # perhaps.
  236. #
  237. SQL {CREATE INDEX abc_i ON abc(a, b, c);}
  238. TEST 4 {
  239. do_test $testid {
  240. execsql {
  241. SELECT * FROM abc ORDER BY a DESC;
  242. }
  243. } {7 8 9 4 5 6 1 2 3}
  244. }
  245. # Test a DELETE statement. Also create a trigger and a view, just to make sure
  246. # these statements don't have any obvious malloc() related bugs in them. Note
  247. # that the test above will be executed each time the DELETE fails, so we're
  248. # also testing rollback of a DELETE from a table with an index on it.
  249. #
  250. SQL {DELETE FROM abc WHERE a > 2;}
  251. SQL {CREATE TRIGGER abc_t AFTER INSERT ON abc BEGIN SELECT 'trigger!'; END;}
  252. SQL {CREATE VIEW abc_v AS SELECT * FROM abc;}
  253. TEST 5 {
  254. do_test $testid {
  255. execsql {
  256. SELECT name, tbl_name FROM sqlite_master ORDER BY name;
  257. SELECT * FROM abc;
  258. }
  259. } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3}
  260. }
  261. set sql {
  262. BEGIN;DELETE FROM abc;
  263. }
  264. for {set i 1} {$i < 100} {incr i} {
  265. set a $i
  266. set b "String value $i"
  267. set c [string repeat X $i]
  268. append sql "INSERT INTO abc VALUES ($a, '$b', '$c');"
  269. }
  270. append sql {COMMIT;}
  271. PREP $sql
  272. SQL {
  273. DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
  274. }
  275. TEST 6 {
  276. do_test $testid.1 {
  277. execsql {SELECT count(*) FROM abc}
  278. } {94}
  279. do_test $testid.2 {
  280. execsql {
  281. SELECT min(
  282. (oid == a) AND 'String value ' || a == b AND a == length(c)
  283. ) FROM abc;
  284. }
  285. } {1}
  286. }
  287. SQL {
  288. DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
  289. }
  290. TEST 7 {
  291. do_test $testid {
  292. execsql {SELECT count(*) FROM abc}
  293. } {89}
  294. do_test $testid {
  295. execsql {
  296. SELECT min(
  297. (oid == a) AND 'String value ' || a == b AND a == length(c)
  298. ) FROM abc;
  299. }
  300. } {1}
  301. }
  302. SQL {
  303. DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5);
  304. }
  305. TEST 9 {
  306. do_test $testid {
  307. execsql {SELECT count(*) FROM abc}
  308. } {84}
  309. do_test $testid {
  310. execsql {
  311. SELECT min(
  312. (oid == a) AND 'String value ' || a == b AND a == length(c)
  313. ) FROM abc;
  314. }
  315. } {1}
  316. }
  317. set padding [string repeat X 500]
  318. PREP [subst {
  319. DROP TABLE abc;
  320. CREATE TABLE abc(a PRIMARY KEY, padding, b, c);
  321. INSERT INTO abc VALUES(0, '$padding', 2, 2);
  322. INSERT INTO abc VALUES(3, '$padding', 5, 5);
  323. INSERT INTO abc VALUES(6, '$padding', 8, 8);
  324. }]
  325. TEST 10 {
  326. do_test $testid {
  327. execsql {SELECT a, b, c FROM abc}
  328. } {0 2 2 3 5 5 6 8 8}
  329. }
  330. SQL {BEGIN;}
  331. SQL {INSERT INTO abc VALUES(9, 'XXXXX', 11, 12);}
  332. TEST_AUTOCOMMIT 11 0
  333. SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;}
  334. TEST_AUTOCOMMIT 12 0
  335. SQL {DELETE FROM abc WHERE a = 10;}
  336. TEST_AUTOCOMMIT 13 0
  337. SQL {COMMIT;}
  338. TEST 14 {
  339. do_test $testid.1 {
  340. sqlite3_get_autocommit $::DB
  341. } {1}
  342. do_test $testid.2 {
  343. execsql {SELECT a, b, c FROM abc}
  344. } {1 2 3 4 5 6 7 8 9}
  345. }
  346. PREP [subst {
  347. DROP TABLE abc;
  348. CREATE TABLE abc(a, padding, b, c);
  349. INSERT INTO abc VALUES(1, '$padding', 2, 3);
  350. INSERT INTO abc VALUES(4, '$padding', 5, 6);
  351. INSERT INTO abc VALUES(7, '$padding', 8, 9);
  352. CREATE INDEX abc_i ON abc(a, padding, b, c);
  353. }]
  354. TEST 15 {
  355. db eval {PRAGMA cache_size = 10}
  356. }
  357. SQL {BEGIN;}
  358. SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
  359. TEST 16 {
  360. do_test $testid {
  361. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  362. } {1 2 4 2 7 2}
  363. }
  364. SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
  365. TEST 17 {
  366. do_test $testid {
  367. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  368. } {1 4 4 4 7 4}
  369. }
  370. SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
  371. TEST 18 {
  372. do_test $testid {
  373. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  374. } {1 8 4 8 7 8}
  375. }
  376. SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}
  377. TEST 19 {
  378. do_test $testid {
  379. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  380. } {1 16 4 16 7 16}
  381. }
  382. SQL {COMMIT;}
  383. TEST 21 {
  384. do_test $testid {
  385. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  386. } {1 16 4 16 7 16}
  387. }
  388. SQL {BEGIN;}
  389. SQL {DELETE FROM abc WHERE oid %2}
  390. TEST 22 {
  391. do_test $testid {
  392. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  393. } {1 8 4 8 7 8}
  394. }
  395. SQL {DELETE FROM abc}
  396. TEST 23 {
  397. do_test $testid {
  398. execsql {SELECT * FROM abc}
  399. } {}
  400. }
  401. SQL {ROLLBACK;}
  402. TEST 24 {
  403. do_test $testid {
  404. execsql {SELECT a, count(*) FROM abc GROUP BY a;}
  405. } {1 16 4 16 7 16}
  406. }
  407. # Test some schema modifications inside of a transaction. These should all
  408. # cause transaction rollback if they fail. Also query a view, to cover a bit
  409. # more code.
  410. #
  411. PREP {DROP VIEW abc_v;}
  412. TEST 25 {
  413. do_test $testid {
  414. execsql {
  415. SELECT name, tbl_name FROM sqlite_master;
  416. }
  417. } {abc abc abc_i abc}
  418. }
  419. SQL {BEGIN;}
  420. SQL {CREATE TABLE def(d, e, f);}
  421. SQL {CREATE TABLE ghi(g, h, i);}
  422. TEST 26 {
  423. do_test $testid {
  424. execsql {
  425. SELECT name, tbl_name FROM sqlite_master;
  426. }
  427. } {abc abc abc_i abc def def ghi ghi}
  428. }
  429. SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi}
  430. SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);}
  431. TEST 27 {
  432. do_test $testid {
  433. execsql {
  434. SELECT name, tbl_name FROM sqlite_master;
  435. }
  436. } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi}
  437. }
  438. SQL {INSERT INTO def VALUES('a', 'b', 'c')}
  439. SQL {INSERT INTO def VALUES(1, 2, 3)}
  440. SQL -norollback {INSERT INTO ghi SELECT * FROM def}
  441. TEST 28 {
  442. do_test $testid {
  443. execsql {
  444. SELECT * FROM def, ghi WHERE d = g;
  445. }
  446. } {a b c a b c 1 2 3 1 2 3}
  447. }
  448. SQL {COMMIT}
  449. TEST 29 {
  450. do_test $testid {
  451. execsql {
  452. SELECT * FROM v1 WHERE d = g;
  453. }
  454. } {a b c a b c 1 2 3 1 2 3}
  455. }
  456. # Test a simple multi-file transaction
  457. #
  458. forcedelete test2.db
  459. ifcapable attach {
  460. SQL {ATTACH 'test2.db' AS aux;}
  461. SQL {BEGIN}
  462. SQL {CREATE TABLE aux.tbl2(x, y, z)}
  463. SQL {INSERT INTO tbl2 VALUES(1, 2, 3)}
  464. SQL {INSERT INTO def VALUES(4, 5, 6)}
  465. TEST 30 {
  466. do_test $testid {
  467. execsql {
  468. SELECT * FROM tbl2, def WHERE d = x;
  469. }
  470. } {1 2 3 1 2 3}
  471. }
  472. SQL {COMMIT}
  473. TEST 31 {
  474. do_test $testid {
  475. execsql {
  476. SELECT * FROM tbl2, def WHERE d = x;
  477. }
  478. } {1 2 3 1 2 3}
  479. }
  480. }
  481. # Test what happens when a malloc() fails while there are other active
  482. # statements. This changes the way sqlite3VdbeHalt() works.
  483. TEST 32 {
  484. if {![info exists ::STMT32]} {
  485. set sql "SELECT name FROM sqlite_master"
  486. set ::STMT32 [sqlite3_prepare $::DB $sql -1 DUMMY]
  487. do_test $testid {
  488. sqlite3_step $::STMT32
  489. } {SQLITE_ROW}
  490. }
  491. }
  492. SQL BEGIN
  493. TEST 33 {
  494. do_test $testid {
  495. execsql {SELECT * FROM ghi}
  496. } {a b c 1 2 3}
  497. }
  498. SQL -norollback {
  499. -- There is a unique index on ghi(g), so this statement may not cause
  500. -- an automatic ROLLBACK. Hence the "-norollback" switch.
  501. INSERT INTO ghi SELECT '2'||g, h, i FROM ghi;
  502. }
  503. TEST 34 {
  504. if {[info exists ::STMT32]} {
  505. do_test $testid {
  506. sqlite3_finalize $::STMT32
  507. } {SQLITE_OK}
  508. unset ::STMT32
  509. }
  510. }
  511. SQL COMMIT
  512. #
  513. # End of test program declaration
  514. #--------------------------------------------------------------------------
  515. proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} {
  516. if {[llength $arglist] %2} {
  517. error "Uneven number of arguments to TEST"
  518. }
  519. for {set i 0} {$i < $pcstart} {incr i} {
  520. set k2 [lindex $arglist [expr {2 * $i}]]
  521. set v2 [lindex $arglist [expr {2 * $i + 1}]]
  522. set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit
  523. switch -- $k2 {
  524. -sql {db eval [lindex $v2 2]}
  525. -prep {db eval $v2}
  526. -debug {eval $v2}
  527. }
  528. set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit
  529. if {$ac && !$nac} {set begin_pc $i}
  530. }
  531. db rollback_hook [list incr ::rollback_hook_count]
  532. set iFail $iFailStart
  533. set pc $pcstart
  534. while {$pc*2 < [llength $arglist]} {
  535. # Fetch the current instruction type and payload.
  536. set k [lindex $arglist [expr {2 * $pc}]]
  537. set v [lindex $arglist [expr {2 * $pc + 1}]]
  538. # Id of this iteration:
  539. set iterid "pc=$pc.iFail=$iFail$k"
  540. switch -- $k {
  541. -test {
  542. foreach {id script} $v {}
  543. set testid "malloc3-(test $id).$iterid"
  544. eval $script
  545. incr pc
  546. }
  547. -sql {
  548. set ::rollback_hook_count 0
  549. set id [lindex $v 0]
  550. set testid "malloc3-(integrity $id).$iterid"
  551. set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit
  552. sqlite3_memdebug_fail $iFail -repeat 0
  553. set rc [catch {db eval [lindex $v 2]} msg] ;# True error occurs
  554. set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit
  555. if {$rc != 0 && $nac && !$ac} {
  556. # Before [db eval] the auto-commit flag was clear. Now it
  557. # is set. Since an error occurred we assume this was not a
  558. # commit - therefore a rollback occurred. Check that the
  559. # rollback-hook was invoked.
  560. do_test malloc3-rollback_hook_count.$iterid {
  561. set ::rollback_hook_count
  562. } {1}
  563. }
  564. set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
  565. if {$rc == 0} {
  566. # Successful execution of sql. The number of failed malloc()
  567. # calls should be equal to the number of benign failures.
  568. # Otherwise a malloc() failed and the error was not reported.
  569. #
  570. set expr {$nFail!=$nBenign}
  571. if {[expr $expr]} {
  572. error "Unreported malloc() failure, test \"$testid\", $expr"
  573. }
  574. if {$ac && !$nac} {
  575. # Before the [db eval] the auto-commit flag was set, now it
  576. # is clear. We can deduce that a "BEGIN" statement has just
  577. # been successfully executed.
  578. set begin_pc $pc
  579. }
  580. incr pc
  581. set iFail 1
  582. integrity_check $testid
  583. } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} {
  584. # Out of memory error, as expected.
  585. #
  586. integrity_check $testid
  587. incr iFail
  588. if {$nac && !$ac} {
  589. if {![lindex $v 1] && [db errorcode] != 3082} {
  590. # error "Statement \"[lindex $v 2]\" caused a rollback"
  591. }
  592. for {set i $begin_pc} {$i < $pc} {incr i} {
  593. set k2 [lindex $arglist [expr {2 * $i}]]
  594. set v2 [lindex $arglist [expr {2 * $i + 1}]]
  595. set catchupsql ""
  596. switch -- $k2 {
  597. -sql {set catchupsql [lindex $v2 2]}
  598. -prep {set catchupsql $v2}
  599. }
  600. db eval $catchupsql
  601. }
  602. }
  603. } else {
  604. error $msg
  605. }
  606. # back up to the previous "-test" block.
  607. while {[lindex $arglist [expr {2 * ($pc - 1)}]] == "-test"} {
  608. incr pc -1
  609. }
  610. }
  611. -prep {
  612. db eval $v
  613. incr pc
  614. }
  615. -debug {
  616. eval $v
  617. incr pc
  618. }
  619. default { error "Unknown switch: $k" }
  620. }
  621. }
  622. }
  623. # Turn off the Tcl interface's prepared statement caching facility. Then
  624. # run the tests with "persistent" malloc failures.
  625. sqlite3_extended_result_codes db 1
  626. db cache size 0
  627. run_test $::run_test_script 1
  628. # Close and reopen the db.
  629. db close
  630. forcedelete test.db test.db-journal test2.db test2.db-journal
  631. sqlite3 db test.db
  632. sqlite3_extended_result_codes db 1
  633. set ::DB [sqlite3_connection_pointer db]
  634. # Turn off the Tcl interface's prepared statement caching facility in
  635. # the new connnection. Then run the tests with "transient" malloc failures.
  636. db cache size 0
  637. run_test $::run_test_script 0
  638. sqlite3_memdebug_fail -1
  639. finish_test