blob: 1eaf21393604957cc9afe88250a2c6926955dab4 [file] [log] [blame]
danielk197707cb5602006-01-20 10:55:05 +00001# 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#
danielk1977131c8bc2008-05-22 13:56:17 +000016# $Id: shared_err.test,v 1.19 2008/05/22 13:56:17 danielk1977 Exp $
danielk197707cb5602006-01-20 10:55:05 +000017
18proc skip {args} {}
19
20
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
danielk1977222a7572007-08-25 13:37:48 +000023source $testdir/malloc_common.tcl
danielk197707cb5602006-01-20 10:55:05 +000024db close
25
26ifcapable !shared_cache||!subquery {
27 finish_test
28 return
29}
danielk1977222a7572007-08-25 13:37:48 +000030
danielk197707cb5602006-01-20 10:55:05 +000031set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
32
danielk1977131c8bc2008-05-22 13:56:17 +000033
danielk197707cb5602006-01-20 10:55:05 +000034do_ioerr_test shared_ioerr-1 -tclprep {
35 sqlite3 db2 test.db
36 execsql {
37 PRAGMA read_uncommitted = 1;
38 CREATE TABLE t1(a,b,c);
39 BEGIN;
40 SELECT * FROM sqlite_master;
41 } db2
42} -sqlbody {
43 SELECT * FROM sqlite_master;
44 INSERT INTO t1 VALUES(1,2,3);
45 BEGIN TRANSACTION;
46 INSERT INTO t1 VALUES(1,2,3);
47 INSERT INTO t1 VALUES(4,5,6);
48 ROLLBACK;
49 SELECT * FROM t1;
50 BEGIN TRANSACTION;
51 INSERT INTO t1 VALUES(1,2,3);
52 INSERT INTO t1 VALUES(4,5,6);
53 COMMIT;
54 SELECT * FROM t1;
55 DELETE FROM t1 WHERE a<100;
56} -cleanup {
danielk197797a227c2006-01-20 16:32:04 +000057 do_test shared_ioerr-1.$n.cleanup.1 {
danielk197707cb5602006-01-20 10:55:05 +000058 set res [catchsql {
59 SELECT * FROM t1;
60 } db2]
61 set possible_results [list \
62 "1 {disk I/O error}" \
63 "0 {1 2 3}" \
64 "0 {1 2 3 1 2 3 4 5 6}" \
65 "0 {1 2 3 1 2 3 4 5 6 1 2 3 4 5 6}" \
66 "0 {}" \
67 ]
68 set rc [expr [lsearch -exact $possible_results $res] >= 0]
69 if {$rc != 1} {
70 puts ""
71 puts "Result: $res"
72 }
73 set rc
74 } {1}
75 db2 close
76}
77
78do_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
106 # When this transaction begins the table contains 512 entries. The
107 # two statements together add 512+146 more if it succeeds.
108 # (1024/7==146)
109 execsql {BEGIN;}
110 execsql {INSERT INTO t1 SELECT a+1, b FROM t1;}
111 execsql {INSERT INTO t1 SELECT 'string' || a, b FROM t1 WHERE 0 = (a%7);}
112 execsql {COMMIT;}
113
114 incr ::residx
115} -cleanup {
drh1527ff42008-01-18 17:03:32 +0000116 catchsql ROLLBACK
danielk197707cb5602006-01-20 10:55:05 +0000117 do_test shared_ioerr-2.$n.cleanup.1 {
118 set res [catchsql {
119 SELECT max(a), min(a), count(*) FROM (SELECT a FROM t1 order by a);
120 } db2]
121 set possible_results [list \
122 {0 {1024 1 1024}} \
123 {0 {1023 1 512}} \
124 {0 {string994 1 1170}} \
125 ]
126 set idx [lsearch -exact $possible_results $res]
127 set success [expr {$idx==$::residx || $res=="1 {disk I/O error}"}]
128 if {!$success} {
129 puts ""
130 puts "Result: \"$res\" ($::residx)"
131 }
132 set success
133 } {1}
134 db2 close
135}
136
danielk197797a227c2006-01-20 16:32:04 +0000137# This test is designed to provoke an IO error when a cursor position is
138# "saved" (because another cursor is going to modify the underlying table).
139#
140do_ioerr_test shared_ioerr-3 -tclprep {
141 sqlite3 db2 test.db
142 execsql {
143 PRAGMA read_uncommitted = 1;
danielk1977c4da5b92006-01-21 12:08:54 +0000144 PRAGMA cache_size = 10;
145 BEGIN;
146 CREATE TABLE t1(a, b, UNIQUE(a, b));
147 } db2
148 for {set i 0} {$i < 200} {incr i} {
149 set a [string range [string repeat "[format %03d $i]." 5] 0 end-1]
150
151 set b [string repeat $i 2000]
152 execsql {INSERT INTO t1 VALUES($a, $b)} db2
153 }
154 execsql {COMMIT} db2
155 set ::DB2 [sqlite3_connection_pointer db2]
156 set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
157 sqlite3_step $::STMT ;# Cursor points at 000.000.000.000
158 sqlite3_step $::STMT ;# Cursor points at 001.001.001.001
159
160} -tclbody {
161 execsql {
danielk197775bab7d2006-01-23 13:09:45 +0000162 BEGIN;
danielk1977c4da5b92006-01-21 12:08:54 +0000163 INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
danielk197775bab7d2006-01-23 13:09:45 +0000164 UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%';
165 COMMIT;
danielk1977c4da5b92006-01-21 12:08:54 +0000166 }
167} -cleanup {
danielk1977b94bf852007-03-19 13:53:37 +0000168 set ::steprc [sqlite3_step $::STMT]
169 set ::column [sqlite3_column_text $::STMT 0]
170 set ::finalrc [sqlite3_finalize $::STMT]
171
172 # There are three possible outcomes here (assuming persistent IO errors):
173 #
174 # 1. If the [sqlite3_step] did not require any IO (required pages in
175 # the cache), then the next row ("002...") may be retrieved
176 # successfully.
177 #
178 # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns
179 # SQLITE_ERROR and [sqlite3_finalize] returns IOERR.
180 #
181 # 3. If, after the initial IO error, SQLite tried to rollback the
182 # active transaction and a second IO error was encountered, then
183 # statement $::STMT will have been aborted. This means [sqlite3_stmt]
184 # returns SQLITE_ABORT, and the statement cursor does not move. i.e.
185 # [sqlite3_column] still returns the current row ("001...") and
186 # [sqlite3_finalize] returns SQLITE_OK.
187 #
188
danielk1977c4da5b92006-01-21 12:08:54 +0000189 do_test shared_ioerr-3.$n.cleanup.1 {
danielk1977b94bf852007-03-19 13:53:37 +0000190 expr {
191 $::steprc eq "SQLITE_ROW" ||
192 $::steprc eq "SQLITE_ERROR" ||
193 $::steprc eq "SQLITE_ABORT"
194 }
195 } {1}
danielk1977c4da5b92006-01-21 12:08:54 +0000196 do_test shared_ioerr-3.$n.cleanup.2 {
danielk1977b94bf852007-03-19 13:53:37 +0000197 expr {
198 ($::steprc eq "SQLITE_ROW" && $::column eq "002.002.002.002.002") ||
199 ($::steprc eq "SQLITE_ERROR" && $::column eq "") ||
200 ($::steprc eq "SQLITE_ABORT" && $::column eq "001.001.001.001.001")
201 }
202 } {1}
danielk1977c4da5b92006-01-21 12:08:54 +0000203 do_test shared_ioerr-3.$n.cleanup.3 {
danielk1977b94bf852007-03-19 13:53:37 +0000204 expr {
205 ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") ||
206 ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") ||
drhc1a7f942007-08-30 02:26:53 +0000207 ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
danielk1977b94bf852007-03-19 13:53:37 +0000208 }
209 } {1}
210
danielk1977c4da5b92006-01-21 12:08:54 +0000211# db2 eval {select * from sqlite_master}
212 db2 close
213}
214
drh77a2a5e2007-04-06 01:04:39 +0000215# This is a repeat of the previous test except that this time we
216# are doing a reverse-order scan of the table when the cursor is
217# "saved".
218#
219do_ioerr_test shared_ioerr-3rev -tclprep {
220 sqlite3 db2 test.db
221 execsql {
222 PRAGMA read_uncommitted = 1;
223 PRAGMA cache_size = 10;
224 BEGIN;
225 CREATE TABLE t1(a, b, UNIQUE(a, b));
226 } db2
227 for {set i 0} {$i < 200} {incr i} {
228 set a [string range [string repeat "[format %03d $i]." 5] 0 end-1]
229
230 set b [string repeat $i 2000]
231 execsql {INSERT INTO t1 VALUES($a, $b)} db2
232 }
233 execsql {COMMIT} db2
234 set ::DB2 [sqlite3_connection_pointer db2]
235 set ::STMT [sqlite3_prepare $::DB2 \
236 "SELECT a FROM t1 ORDER BY a DESC" -1 DUMMY]
237 sqlite3_step $::STMT ;# Cursor points at 199.199.199.199.199
238 sqlite3_step $::STMT ;# Cursor points at 198.198.198.198.198
239
240} -tclbody {
241 execsql {
242 BEGIN;
243 INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
244 UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%';
245 COMMIT;
246 }
247} -cleanup {
248 set ::steprc [sqlite3_step $::STMT]
249 set ::column [sqlite3_column_text $::STMT 0]
250 set ::finalrc [sqlite3_finalize $::STMT]
251
252 # There are three possible outcomes here (assuming persistent IO errors):
253 #
254 # 1. If the [sqlite3_step] did not require any IO (required pages in
255 # the cache), then the next row ("002...") may be retrieved
256 # successfully.
257 #
258 # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns
259 # SQLITE_ERROR and [sqlite3_finalize] returns IOERR.
260 #
261 # 3. If, after the initial IO error, SQLite tried to rollback the
262 # active transaction and a second IO error was encountered, then
263 # statement $::STMT will have been aborted. This means [sqlite3_stmt]
264 # returns SQLITE_ABORT, and the statement cursor does not move. i.e.
265 # [sqlite3_column] still returns the current row ("001...") and
266 # [sqlite3_finalize] returns SQLITE_OK.
267 #
268
269 do_test shared_ioerr-3rev.$n.cleanup.1 {
270 expr {
271 $::steprc eq "SQLITE_ROW" ||
272 $::steprc eq "SQLITE_ERROR" ||
273 $::steprc eq "SQLITE_ABORT"
274 }
275 } {1}
276 do_test shared_ioerr-3rev.$n.cleanup.2 {
277 expr {
278 ($::steprc eq "SQLITE_ROW" && $::column eq "197.197.197.197.197") ||
279 ($::steprc eq "SQLITE_ERROR" && $::column eq "") ||
280 ($::steprc eq "SQLITE_ABORT" && $::column eq "198.198.198.198.198")
281 }
282 } {1}
283 do_test shared_ioerr-3rev.$n.cleanup.3 {
284 expr {
285 ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") ||
286 ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") ||
drhc1a7f942007-08-30 02:26:53 +0000287 ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
drh77a2a5e2007-04-06 01:04:39 +0000288 }
289 } {1}
290
291# db2 eval {select * from sqlite_master}
292 db2 close
293}
294
drh7b3822b2006-01-23 23:49:34 +0000295# Only run these tests if memory debugging is turned on.
296#
drhc1a7f942007-08-30 02:26:53 +0000297ifcapable !memdebug {
drh5a3032b2007-09-03 16:12:09 +0000298 puts "Skipping tests shared_err-4 through -9:\
299 not compiled with -DSQLITE_MEMDEBUG..."
drh80d56822006-01-24 00:15:15 +0000300 db close
301 sqlite3_enable_shared_cache $::enable_shared_cache
drh7b3822b2006-01-23 23:49:34 +0000302 finish_test
303 return
304}
305
danielk1977c4da5b92006-01-21 12:08:54 +0000306# Provoke a malloc() failure when a cursor position is being saved. This
307# only happens with index cursors (because they malloc() space to save the
308# current key value). It does not happen with tables, because an integer
309# key does not require a malloc() to store.
310#
311# The library should return an SQLITE_NOMEM to the caller. The query that
312# owns the cursor (the one for which the position is not saved) should
313# continue unaffected.
314#
drhc1a7f942007-08-30 02:26:53 +0000315do_malloc_test shared_err-4 -tclprep {
danielk1977c4da5b92006-01-21 12:08:54 +0000316 sqlite3 db2 test.db
317 execsql {
318 PRAGMA read_uncommitted = 1;
danielk197797a227c2006-01-20 16:32:04 +0000319 BEGIN;
320 CREATE TABLE t1(a, b, UNIQUE(a, b));
321 } db2
322 for {set i 0} {$i < 5} {incr i} {
323 set a [string repeat $i 10]
324 set b [string repeat $i 2000]
325 execsql {INSERT INTO t1 VALUES($a, $b)} db2
326 }
327 execsql {COMMIT} db2
328 set ::DB2 [sqlite3_connection_pointer db2]
329 set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
330 sqlite3_step $::STMT ;# Cursor points at 0000000000
331 sqlite3_step $::STMT ;# Cursor points at 1111111111
332} -tclbody {
333 execsql {
334 INSERT INTO t1 VALUES(6, NULL);
335 }
336} -cleanup {
danielk1977c4da5b92006-01-21 12:08:54 +0000337 do_test shared_malloc-4.$::n.cleanup.1 {
danielk19778d34dfd2006-01-24 16:37:57 +0000338 set ::rc [sqlite3_step $::STMT]
drhc1a7f942007-08-30 02:26:53 +0000339 expr {$::rc=="SQLITE_ROW" || $::rc=="SQLITE_ERROR"}
danielk19778d34dfd2006-01-24 16:37:57 +0000340 } {1}
341 if {$::rc=="SQLITE_ROW"} {
342 do_test shared_malloc-4.$::n.cleanup.2 {
343 sqlite3_column_text $::STMT 0
344 } {2222222222}
345 }
danielk1977c4da5b92006-01-21 12:08:54 +0000346 do_test shared_malloc-4.$::n.cleanup.3 {
drhc1a7f942007-08-30 02:26:53 +0000347 set rc [sqlite3_finalize $::STMT]
drh1527ff42008-01-18 17:03:32 +0000348 expr {$rc=="SQLITE_OK" || $rc=="SQLITE_ABORT" ||
349 $rc=="SQLITE_NOMEM" || $rc=="SQLITE_IOERR"}
drhc1a7f942007-08-30 02:26:53 +0000350 } {1}
danielk197797a227c2006-01-20 16:32:04 +0000351# db2 eval {select * from sqlite_master}
352 db2 close
353}
354
drhc1a7f942007-08-30 02:26:53 +0000355do_malloc_test shared_err-5 -tclbody {
356 db close
danielk19774b202ae2006-01-23 05:50:58 +0000357 sqlite3 dbX test.db
358 sqlite3 dbY test.db
359 dbX close
360 dbY close
361} -cleanup {
362 catch {dbX close}
363 catch {dbY close}
364}
365
drhc1a7f942007-08-30 02:26:53 +0000366do_malloc_test shared_err-6 -tclbody {
danielk19777246f5b2006-01-24 11:30:27 +0000367 catch {db close}
368 sqlite3_thread_cleanup
369 sqlite3_enable_shared_cache 0
370} -cleanup {
371 sqlite3_enable_shared_cache 1
372}
373
drhc1a7f942007-08-30 02:26:53 +0000374# As of 3.5.0, sqlite3_enable_shared_cache can be called at
375# any time and from any thread
376#do_test shared_err-misuse-7.1 {
377# sqlite3 db test.db
378# catch {
379# sqlite3_enable_shared_cache 0
380# } msg
381# set msg
382#} {library routine called out of sequence}
danielk19777246f5b2006-01-24 11:30:27 +0000383
danielk19778d34dfd2006-01-24 16:37:57 +0000384# Again provoke a malloc() failure when a cursor position is being saved,
385# this time during a ROLLBACK operation by some other handle.
386#
387# The library should return an SQLITE_NOMEM to the caller. The query that
388# owns the cursor (the one for which the position is not saved) should
389# be aborted.
390#
391set ::aborted 0
drhc1a7f942007-08-30 02:26:53 +0000392do_malloc_test shared_err-8 -tclprep {
danielk19778d34dfd2006-01-24 16:37:57 +0000393 sqlite3 db2 test.db
394 execsql {
395 PRAGMA read_uncommitted = 1;
396 BEGIN;
397 CREATE TABLE t1(a, b, UNIQUE(a, b));
398 } db2
399 for {set i 0} {$i < 2} {incr i} {
400 set a [string repeat $i 10]
401 set b [string repeat $i 2000]
402 execsql {INSERT INTO t1 VALUES($a, $b)} db2
403 }
404 execsql {COMMIT} db2
405 set ::DB2 [sqlite3_connection_pointer db2]
406 set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
407 sqlite3_step $::STMT ;# Cursor points at 0000000000
408 sqlite3_step $::STMT ;# Cursor points at 1111111111
409} -tclbody {
410 execsql {
411 BEGIN;
412 INSERT INTO t1 VALUES(6, NULL);
413 ROLLBACK;
414 }
415} -cleanup {
416 do_test shared_malloc-8.$::n.cleanup.1 {
drh1527ff42008-01-18 17:03:32 +0000417 set res [catchsql {SELECT a FROM t1} db2]
418 set ans [lindex $res 1]
419 if {[lindex $res 0]} {
420 set r [expr {$ans=="disk I/O error"}]
421 } else {
422 set r [expr {[lrange $ans 0 1]=="0000000000 1111111111"}]
423 }
424 } {1}
danielk19778d34dfd2006-01-24 16:37:57 +0000425 do_test shared_malloc-8.$::n.cleanup.2 {
426 set rc1 [sqlite3_step $::STMT]
427 set rc2 [sqlite3_finalize $::STMT]
drhc1a7f942007-08-30 02:26:53 +0000428 if {$rc2=="SQLITE_ABORT"} {
danielk19778d34dfd2006-01-24 16:37:57 +0000429 incr ::aborted
430 }
431 expr {
432 ($rc1=="SQLITE_DONE" && $rc2=="SQLITE_OK") ||
drhc1a7f942007-08-30 02:26:53 +0000433 ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_ABORT") ||
drh1527ff42008-01-18 17:03:32 +0000434 ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_NOMEM") ||
435 ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_IOERR")
danielk19778d34dfd2006-01-24 16:37:57 +0000436 }
437 } {1}
438 db2 close
439}
440do_test shared_malloc-8.X {
441 # Test that one or more queries were aborted due to the malloc() failure.
442 expr $::aborted>=1
443} {1}
444
danielk1977131c8bc2008-05-22 13:56:17 +0000445
danielk1977992772c2007-08-30 10:07:38 +0000446# This test is designed to catch a specific bug that was present during
447# development of 3.5.0. If a malloc() failed while setting the page-size,
448# a buffer (Pager.pTmpSpace) was being freed. This could cause a seg-fault
449# later if another connection tried to use the pager.
450#
451# This test will crash 3.4.2.
452#
453do_malloc_test shared_err-9 -tclprep {
454 sqlite3 db2 test.db
455} -sqlbody {
456 PRAGMA page_size = 4096;
457 PRAGMA page_size = 1024;
458} -cleanup {
459 db2 eval {
460 CREATE TABLE abc(a, b, c);
461 BEGIN;
462 INSERT INTO abc VALUES(1, 2, 3);
463 ROLLBACK;
464 }
465 db2 close
466}
467
danielk1977131c8bc2008-05-22 13:56:17 +0000468catch {db close}
469catch {db2 close}
470do_malloc_test shared_err-10 -tclprep {
471 sqlite3 db test.db
472 sqlite3 db2 test.db
473
474 db eval { SELECT * FROM sqlite_master }
475 db2 eval {
476 BEGIN;
477 CREATE TABLE abc(a, b, c);
478 }
479} -tclbody {
480 catch {db eval {SELECT * FROM sqlite_master}}
481 error 1
482} -cleanup {
483 execsql { SELECT * FROM sqlite_master }
484}
485
486do_malloc_test shared_err-11 -tclprep {
487 sqlite3 db test.db
488 sqlite3 db2 test.db
489
490 db eval { SELECT * FROM sqlite_master }
491 db2 eval {
492 BEGIN;
493 CREATE TABLE abc(a, b, c);
494 }
495} -tclbody {
496 catch {db eval {SELECT * FROM sqlite_master}}
497 catch {sqlite3_errmsg16 db}
498 error 1
499} -cleanup {
500 execsql { SELECT * FROM sqlite_master }
501}
502
danielk1977992772c2007-08-30 10:07:38 +0000503
danielk197707cb5602006-01-20 10:55:05 +0000504catch {db close}
danielk1977992772c2007-08-30 10:07:38 +0000505catch {db2 close}
danielk197707cb5602006-01-20 10:55:05 +0000506sqlite3_enable_shared_cache $::enable_shared_cache
507finish_test