xref: /sqlite-3.40.0/test/shared_err.test (revision 131c8bc0)
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.19 2008/05/22 13:56:17 danielk1977 Exp $
17
18proc skip {args} {}
19
20
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
23source $testdir/malloc_common.tcl
24db close
25
26ifcapable !shared_cache||!subquery {
27  finish_test
28  return
29}
30
31set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
32
33
34do_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 {
57  do_test shared_ioerr-1.$n.cleanup.1 {
58    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 {
116  catchsql ROLLBACK
117  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
137# 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;
144    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 {
162    BEGIN;
163    INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
164    UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%';
165    COMMIT;
166  }
167} -cleanup {
168  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
189  do_test shared_ioerr-3.$n.cleanup.1 {
190    expr {
191      $::steprc eq "SQLITE_ROW" ||
192      $::steprc eq "SQLITE_ERROR" ||
193      $::steprc eq "SQLITE_ABORT"
194    }
195  } {1}
196  do_test shared_ioerr-3.$n.cleanup.2 {
197    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}
203  do_test shared_ioerr-3.$n.cleanup.3 {
204    expr {
205      ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") ||
206      ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") ||
207      ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
208    }
209  } {1}
210
211# db2 eval {select * from sqlite_master}
212  db2 close
213}
214
215# 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") ||
287      ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT")
288    }
289  } {1}
290
291# db2 eval {select * from sqlite_master}
292  db2 close
293}
294
295# Only run these tests if memory debugging is turned on.
296#
297ifcapable !memdebug {
298   puts "Skipping tests shared_err-4 through -9:\
299         not compiled with -DSQLITE_MEMDEBUG..."
300   db close
301   sqlite3_enable_shared_cache $::enable_shared_cache
302   finish_test
303   return
304}
305
306# 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#
315do_malloc_test shared_err-4 -tclprep {
316  sqlite3 db2 test.db
317  execsql {
318    PRAGMA read_uncommitted = 1;
319    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 {
337  do_test shared_malloc-4.$::n.cleanup.1 {
338    set ::rc [sqlite3_step $::STMT]
339    expr {$::rc=="SQLITE_ROW" || $::rc=="SQLITE_ERROR"}
340  } {1}
341  if {$::rc=="SQLITE_ROW"} {
342    do_test shared_malloc-4.$::n.cleanup.2 {
343      sqlite3_column_text $::STMT 0
344    } {2222222222}
345  }
346  do_test shared_malloc-4.$::n.cleanup.3 {
347   set rc [sqlite3_finalize $::STMT]
348   expr {$rc=="SQLITE_OK" || $rc=="SQLITE_ABORT" ||
349         $rc=="SQLITE_NOMEM" || $rc=="SQLITE_IOERR"}
350  } {1}
351# db2 eval {select * from sqlite_master}
352  db2 close
353}
354
355do_malloc_test shared_err-5 -tclbody {
356  db close
357  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
366do_malloc_test shared_err-6 -tclbody {
367  catch {db close}
368  sqlite3_thread_cleanup
369  sqlite3_enable_shared_cache 0
370} -cleanup {
371  sqlite3_enable_shared_cache 1
372}
373
374# 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}
383
384# 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
392do_malloc_test shared_err-8 -tclprep {
393  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 {
417    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}
425  do_test shared_malloc-8.$::n.cleanup.2 {
426    set rc1 [sqlite3_step $::STMT]
427    set rc2 [sqlite3_finalize $::STMT]
428    if {$rc2=="SQLITE_ABORT"} {
429      incr ::aborted
430    }
431    expr {
432      ($rc1=="SQLITE_DONE" && $rc2=="SQLITE_OK") ||
433      ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_ABORT") ||
434      ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_NOMEM") ||
435      ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_IOERR")
436    }
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
445
446# 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
468catch {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
503
504catch {db close}
505catch {db2 close}
506sqlite3_enable_shared_cache $::enable_shared_cache
507finish_test
508