shared_err.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. # 2005 December 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. # The focus of the tests in this file are IO errors that occur in a shared
  13. # cache context. What happens to connection B if one connection A encounters
  14. # an IO-error whilst reading or writing the file-system?
  15. #
  16. # $Id: shared_err.test,v 1.24 2008/10/12 00:27:54 shane Exp $
  17. proc skip {args} {}
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. source $testdir/malloc_common.tcl
  21. db close
  22. ifcapable !shared_cache||!subquery {
  23. finish_test
  24. return
  25. }
  26. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  27. do_ioerr_test shared_ioerr-1 -tclprep {
  28. sqlite3 db2 test.db
  29. execsql {
  30. PRAGMA read_uncommitted = 1;
  31. CREATE TABLE t1(a,b,c);
  32. BEGIN;
  33. SELECT * FROM sqlite_master;
  34. } db2
  35. } -sqlbody {
  36. SELECT * FROM sqlite_master;
  37. INSERT INTO t1 VALUES(1,2,3);
  38. BEGIN TRANSACTION;
  39. INSERT INTO t1 VALUES(1,2,3);
  40. INSERT INTO t1 VALUES(4,5,6);
  41. ROLLBACK;
  42. SELECT * FROM t1;
  43. BEGIN TRANSACTION;
  44. INSERT INTO t1 VALUES(1,2,3);
  45. INSERT INTO t1 VALUES(4,5,6);
  46. COMMIT;
  47. SELECT * FROM t1;
  48. DELETE FROM t1 WHERE a<100;
  49. } -cleanup {
  50. do_test shared_ioerr-1.$n.cleanup.1 {
  51. set res [catchsql {
  52. SELECT * FROM t1;
  53. } db2]
  54. set possible_results [list \
  55. "1 {disk I/O error}" \
  56. "0 {1 2 3}" \
  57. "0 {1 2 3 1 2 3 4 5 6}" \
  58. "0 {1 2 3 1 2 3 4 5 6 1 2 3 4 5 6}" \
  59. "0 {}" \
  60. "1 {database disk image is malformed}" \
  61. ]
  62. set rc [expr [lsearch -exact $possible_results $res] >= 0]
  63. if {$rc != 1} {
  64. puts ""
  65. puts "Result: $res"
  66. }
  67. set rc
  68. } {1}
  69. # The "database disk image is malformed" is a special case that can
  70. # occur if an IO error occurs during a rollback in the {SELECT * FROM t1}
  71. # statement above. This test is to make sure there is no real database
  72. # corruption.
  73. db2 close
  74. do_test shared_ioerr-1.$n.cleanup.2 {
  75. execsql {pragma integrity_check} db
  76. } {ok}
  77. }
  78. do_ioerr_test shared_ioerr-2 -tclprep {
  79. sqlite3 db2 test.db
  80. execsql {
  81. PRAGMA read_uncommitted = 1;
  82. BEGIN;
  83. CREATE TABLE t1(a, b);
  84. INSERT INTO t1(oid) VALUES(NULL);
  85. INSERT INTO t1(oid) SELECT NULL FROM t1;
  86. INSERT INTO t1(oid) SELECT NULL FROM t1;
  87. INSERT INTO t1(oid) SELECT NULL FROM t1;
  88. INSERT INTO t1(oid) SELECT NULL FROM t1;
  89. INSERT INTO t1(oid) SELECT NULL FROM t1;
  90. INSERT INTO t1(oid) SELECT NULL FROM t1;
  91. INSERT INTO t1(oid) SELECT NULL FROM t1;
  92. INSERT INTO t1(oid) SELECT NULL FROM t1;
  93. INSERT INTO t1(oid) SELECT NULL FROM t1;
  94. INSERT INTO t1(oid) SELECT NULL FROM t1;
  95. UPDATE t1 set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789';
  96. CREATE INDEX i1 ON t1(a);
  97. COMMIT;
  98. BEGIN;
  99. SELECT * FROM sqlite_master;
  100. } db2
  101. } -tclbody {
  102. set ::residx 0
  103. execsql {DELETE FROM t1 WHERE 0 = (a % 2);}
  104. incr ::residx
  105. # When this transaction begins the table contains 512 entries. The
  106. # two statements together add 512+146 more if it succeeds.
  107. # (1024/7==146)
  108. execsql {BEGIN;}
  109. execsql {INSERT INTO t1 SELECT a+1, b FROM t1;}
  110. execsql {INSERT INTO t1 SELECT 'string' || a, b FROM t1 WHERE 0 = (a%7);}
  111. execsql {COMMIT;}
  112. incr ::residx
  113. } -cleanup {
  114. catchsql ROLLBACK
  115. do_test shared_ioerr-2.$n.cleanup.1 {
  116. set res [catchsql {
  117. SELECT max(a), min(a), count(*) FROM (SELECT a FROM t1 order by a);
  118. } db2]
  119. set possible_results [list \
  120. {0 {1024 1 1024}} \
  121. {0 {1023 1 512}} \
  122. {0 {string994 1 1170}} \
  123. ]
  124. set idx [lsearch -exact $possible_results $res]
  125. set success [expr {$idx==$::residx || $res=="1 {disk I/O error}"}]
  126. if {!$success} {
  127. puts ""
  128. puts "Result: \"$res\" ($::residx)"
  129. }
  130. set success
  131. } {1}
  132. db2 close
  133. }
  134. # This test is designed to provoke an IO error when a cursor position is
  135. # "saved" (because another cursor is going to modify the underlying table).
  136. #
  137. do_ioerr_test shared_ioerr-3 -tclprep {
  138. sqlite3 db2 test.db
  139. execsql {
  140. PRAGMA read_uncommitted = 1;
  141. PRAGMA cache_size = 10;
  142. BEGIN;
  143. CREATE TABLE t1(a, b, UNIQUE(a, b));
  144. } db2
  145. for {set i 0} {$i < 200} {incr i} {
  146. set a [string range [string repeat "[format %03d $i]." 5] 0 end-1]
  147. set b [string repeat $i 2000]
  148. execsql {INSERT INTO t1 VALUES($a, $b)} db2
  149. }
  150. execsql {COMMIT} db2
  151. set ::DB2 [sqlite3_connection_pointer db2]
  152. set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
  153. sqlite3_step $::STMT ;# Cursor points at 000.000.000.000
  154. sqlite3_step $::STMT ;# Cursor points at 001.001.001.001
  155. } -tclbody {
  156. execsql {
  157. BEGIN;
  158. INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
  159. UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%';
  160. COMMIT;
  161. }
  162. } -cleanup {
  163. set ::steprc [sqlite3_step $::STMT]
  164. set ::column [sqlite3_column_text $::STMT 0]
  165. set ::finalrc [sqlite3_finalize $::STMT]
  166. # There are three possible outcomes here (assuming persistent IO errors):
  167. #
  168. # 1. If the [sqlite3_step] did not require any IO (required pages in
  169. # the cache), then the next row ("002...") may be retrieved
  170. # successfully.
  171. #
  172. # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns
  173. # SQLITE_ERROR and [sqlite3_finalize] returns IOERR.
  174. #
  175. # 3. If, after the initial IO error, SQLite tried to rollback the
  176. # active transaction and a second IO error was encountered, then
  177. # statement $::STMT will have been aborted. This means [sqlite3_stmt]
  178. # returns SQLITE_ABORT, and the statement cursor does not move. i.e.
  179. # [sqlite3_column] still returns the current row ("001...") and
  180. # [sqlite3_finalize] returns SQLITE_OK.
  181. #
  182. do_test shared_ioerr-3.$n.cleanup.1 {
  183. expr {
  184. $::steprc eq "SQLITE_ROW" ||
  185. $::steprc eq "SQLITE_ERROR" ||
  186. $::steprc eq "SQLITE_ABORT"
  187. }
  188. } {1}
  189. do_test shared_ioerr-3.$n.cleanup.2 {
  190. expr {
  191. ($::steprc eq "SQLITE_ROW" && $::column eq "002.002.002.002.002") ||
  192. ($::steprc eq "SQLITE_ERROR" && $::column eq "") ||
  193. ($::steprc eq "SQLITE_ABORT" && $::column eq "001.001.001.001.001")
  194. }
  195. } {1}
  196. do_test shared_ioerr-3.$n.cleanup.3 {
  197. expr {
  198. ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") ||
  199. ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") ||
  200. ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
  201. }
  202. } {1}
  203. # db2 eval {select * from sqlite_master}
  204. db2 close
  205. }
  206. # This is a repeat of the previous test except that this time we
  207. # are doing a reverse-order scan of the table when the cursor is
  208. # "saved".
  209. #
  210. do_ioerr_test shared_ioerr-3rev -tclprep {
  211. sqlite3 db2 test.db
  212. execsql {
  213. PRAGMA read_uncommitted = 1;
  214. PRAGMA cache_size = 10;
  215. BEGIN;
  216. CREATE TABLE t1(a, b, UNIQUE(a, b));
  217. } db2
  218. for {set i 0} {$i < 200} {incr i} {
  219. set a [string range [string repeat "[format %03d $i]." 5] 0 end-1]
  220. set b [string repeat $i 2000]
  221. execsql {INSERT INTO t1 VALUES($a, $b)} db2
  222. }
  223. execsql {COMMIT} db2
  224. set ::DB2 [sqlite3_connection_pointer db2]
  225. set ::STMT [sqlite3_prepare $::DB2 \
  226. "SELECT a FROM t1 ORDER BY a DESC" -1 DUMMY]
  227. sqlite3_step $::STMT ;# Cursor points at 199.199.199.199.199
  228. sqlite3_step $::STMT ;# Cursor points at 198.198.198.198.198
  229. } -tclbody {
  230. execsql {
  231. BEGIN;
  232. INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
  233. UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%';
  234. COMMIT;
  235. }
  236. } -cleanup {
  237. set ::steprc [sqlite3_step $::STMT]
  238. set ::column [sqlite3_column_text $::STMT 0]
  239. set ::finalrc [sqlite3_finalize $::STMT]
  240. # There are three possible outcomes here (assuming persistent IO errors):
  241. #
  242. # 1. If the [sqlite3_step] did not require any IO (required pages in
  243. # the cache), then the next row ("002...") may be retrieved
  244. # successfully.
  245. #
  246. # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns
  247. # SQLITE_ERROR and [sqlite3_finalize] returns IOERR.
  248. #
  249. # 3. If, after the initial IO error, SQLite tried to rollback the
  250. # active transaction and a second IO error was encountered, then
  251. # statement $::STMT will have been aborted. This means [sqlite3_stmt]
  252. # returns SQLITE_ABORT, and the statement cursor does not move. i.e.
  253. # [sqlite3_column] still returns the current row ("001...") and
  254. # [sqlite3_finalize] returns SQLITE_OK.
  255. #
  256. do_test shared_ioerr-3rev.$n.cleanup.1 {
  257. expr {
  258. $::steprc eq "SQLITE_ROW" ||
  259. $::steprc eq "SQLITE_ERROR" ||
  260. $::steprc eq "SQLITE_ABORT"
  261. }
  262. } {1}
  263. do_test shared_ioerr-3rev.$n.cleanup.2 {
  264. expr {
  265. ($::steprc eq "SQLITE_ROW" && $::column eq "197.197.197.197.197") ||
  266. ($::steprc eq "SQLITE_ERROR" && $::column eq "") ||
  267. ($::steprc eq "SQLITE_ABORT" && $::column eq "198.198.198.198.198")
  268. }
  269. } {1}
  270. do_test shared_ioerr-3rev.$n.cleanup.3 {
  271. expr {
  272. ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") ||
  273. ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") ||
  274. ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
  275. }
  276. } {1}
  277. # db2 eval {select * from sqlite_master}
  278. db2 close
  279. }
  280. # Provoke a malloc() failure when a cursor position is being saved. This
  281. # only happens with index cursors (because they malloc() space to save the
  282. # current key value). It does not happen with tables, because an integer
  283. # key does not require a malloc() to store.
  284. #
  285. # The library should return an SQLITE_NOMEM to the caller. The query that
  286. # owns the cursor (the one for which the position is not saved) should
  287. # continue unaffected.
  288. #
  289. do_malloc_test shared_err-4 -tclprep {
  290. sqlite3 db2 test.db
  291. execsql {
  292. PRAGMA read_uncommitted = 1;
  293. BEGIN;
  294. CREATE TABLE t1(a, b, UNIQUE(a, b));
  295. } db2
  296. for {set i 0} {$i < 5} {incr i} {
  297. set a [string repeat $i 10]
  298. set b [string repeat $i 2000]
  299. execsql {INSERT INTO t1 VALUES($a, $b)} db2
  300. }
  301. execsql {COMMIT} db2
  302. set ::DB2 [sqlite3_connection_pointer db2]
  303. set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
  304. sqlite3_step $::STMT ;# Cursor points at 0000000000
  305. sqlite3_step $::STMT ;# Cursor points at 1111111111
  306. } -tclbody {
  307. execsql {
  308. INSERT INTO t1 VALUES(6, NULL);
  309. }
  310. } -cleanup {
  311. do_test shared_malloc-4.$::n.cleanup.1 {
  312. set ::rc [sqlite3_step $::STMT]
  313. expr {$::rc=="SQLITE_ROW" || $::rc=="SQLITE_ERROR"}
  314. } {1}
  315. if {$::rc=="SQLITE_ROW"} {
  316. do_test shared_malloc-4.$::n.cleanup.2 {
  317. sqlite3_column_text $::STMT 0
  318. } {2222222222}
  319. }
  320. do_test shared_malloc-4.$::n.cleanup.3 {
  321. set rc [sqlite3_finalize $::STMT]
  322. expr {$rc=="SQLITE_OK" || $rc=="SQLITE_ABORT" ||
  323. $rc=="SQLITE_NOMEM" || $rc=="SQLITE_IOERR"}
  324. } {1}
  325. # db2 eval {select * from sqlite_master}
  326. db2 close
  327. }
  328. do_malloc_test shared_err-5 -tclbody {
  329. db close
  330. sqlite3 dbX test.db
  331. sqlite3 dbY test.db
  332. dbX close
  333. dbY close
  334. } -cleanup {
  335. catch {dbX close}
  336. catch {dbY close}
  337. }
  338. do_malloc_test shared_err-6 -tclbody {
  339. catch {db close}
  340. ifcapable deprecated {
  341. sqlite3_thread_cleanup
  342. }
  343. sqlite3_enable_shared_cache 0
  344. } -cleanup {
  345. sqlite3_enable_shared_cache 1
  346. }
  347. # As of 3.5.0, sqlite3_enable_shared_cache can be called at
  348. # any time and from any thread
  349. #do_test shared_err-misuse-7.1 {
  350. # sqlite3 db test.db
  351. # catch {
  352. # sqlite3_enable_shared_cache 0
  353. # } msg
  354. # set msg
  355. #} {library routine called out of sequence}
  356. # Again provoke a malloc() failure when a cursor position is being saved,
  357. # this time during a ROLLBACK operation by some other handle.
  358. #
  359. # The library should return an SQLITE_NOMEM to the caller. The query that
  360. # owns the cursor (the one for which the position is not saved) should
  361. # be aborted.
  362. #
  363. set ::aborted 0
  364. do_malloc_test shared_err-8 -tclprep {
  365. sqlite3 db2 test.db
  366. execsql {
  367. PRAGMA read_uncommitted = 1;
  368. BEGIN;
  369. CREATE TABLE t1(a, b, UNIQUE(a, b));
  370. } db2
  371. for {set i 0} {$i < 2} {incr i} {
  372. set a [string repeat $i 10]
  373. set b [string repeat $i 2000]
  374. execsql {INSERT INTO t1 VALUES($a, $b)} db2
  375. }
  376. execsql {COMMIT} db2
  377. execsql BEGIN
  378. execsql ROLLBACK
  379. set ::DB2 [sqlite3_connection_pointer db2]
  380. set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
  381. sqlite3_step $::STMT ;# Cursor points at 0000000000
  382. sqlite3_step $::STMT ;# Cursor points at 1111111111
  383. } -tclbody {
  384. execsql {
  385. BEGIN;
  386. INSERT INTO t1 VALUES(6, NULL);
  387. ROLLBACK}
  388. } -cleanup {
  389. # UPDATE: As of [5668], if the rollback fails SQLITE_CORRUPT is returned.
  390. # So these tests have been updated to expect SQLITE_CORRUPT and its
  391. # associated English language error message.
  392. #
  393. do_test shared_malloc-8.$::n.cleanup.1 {
  394. set res [catchsql {SELECT a FROM t1} db2]
  395. set ans [lindex $res 1]
  396. if {[lindex $res 0]} {
  397. set r [expr {
  398. $ans=="disk I/O error" ||
  399. $ans=="out of memory" ||
  400. $ans=="database disk image is malformed"
  401. }]
  402. } else {
  403. set r [expr {[lrange $ans 0 1]=="0000000000 1111111111"}]
  404. }
  405. } {1}
  406. do_test shared_malloc-8.$::n.cleanup.2 {
  407. set rc1 [sqlite3_step $::STMT]
  408. set rc2 [sqlite3_finalize $::STMT]
  409. if {$rc2=="SQLITE_ABORT"} {
  410. incr ::aborted
  411. }
  412. expr {
  413. ($rc1=="SQLITE_DONE" && $rc2=="SQLITE_OK") ||
  414. ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_ABORT") ||
  415. ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_NOMEM") ||
  416. ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_IOERR") ||
  417. ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_CORRUPT")
  418. }
  419. } {1}
  420. db2 close
  421. }
  422. do_test shared_malloc-8.X {
  423. # Test that one or more queries were aborted due to the malloc() failure.
  424. expr $::aborted>=1
  425. } {1}
  426. # This test is designed to catch a specific bug that was present during
  427. # development of 3.5.0. If a malloc() failed while setting the page-size,
  428. # a buffer (Pager.pTmpSpace) was being freed. This could cause a seg-fault
  429. # later if another connection tried to use the pager.
  430. #
  431. # This test will crash 3.4.2.
  432. #
  433. do_malloc_test shared_err-9 -tclprep {
  434. sqlite3 db2 test.db
  435. } -sqlbody {
  436. PRAGMA page_size = 4096;
  437. PRAGMA page_size = 1024;
  438. } -cleanup {
  439. db2 eval {
  440. CREATE TABLE abc(a, b, c);
  441. BEGIN;
  442. INSERT INTO abc VALUES(1, 2, 3);
  443. ROLLBACK;
  444. }
  445. db2 close
  446. }
  447. catch {db close}
  448. catch {db2 close}
  449. do_malloc_test shared_err-10 -tclprep {
  450. sqlite3 db test.db
  451. sqlite3 db2 test.db
  452. db eval { SELECT * FROM sqlite_master }
  453. db2 eval {
  454. BEGIN;
  455. CREATE TABLE abc(a, b, c);
  456. }
  457. } -tclbody {
  458. catch {db eval {SELECT * FROM sqlite_master}}
  459. error 1
  460. } -cleanup {
  461. execsql { SELECT * FROM sqlite_master }
  462. }
  463. do_malloc_test shared_err-11 -tclprep {
  464. sqlite3 db test.db
  465. sqlite3 db2 test.db
  466. db eval { SELECT * FROM sqlite_master }
  467. db2 eval {
  468. BEGIN;
  469. CREATE TABLE abc(a, b, c);
  470. }
  471. } -tclbody {
  472. catch {db eval {SELECT * FROM sqlite_master}}
  473. catch {sqlite3_errmsg16 db}
  474. error 1
  475. } -cleanup {
  476. execsql { SELECT * FROM sqlite_master }
  477. }
  478. catch {db close}
  479. catch {db2 close}
  480. do_malloc_test shared_err-12 -sqlbody {
  481. CREATE TABLE abc(a, b, c);
  482. INSERT INTO abc VALUES(1, 2, 3);
  483. }
  484. catch {db close}
  485. catch {db2 close}
  486. sqlite3_enable_shared_cache $::enable_shared_cache
  487. finish_test