From fa67c3c584b9d0d66ac40e13a50bb32446f41575 Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 11 Jul 2008 02:21:40 +0000 Subject: [PATCH] Additional test coverage in btree.c. Added corruption tests for the ptrmap pages of an autovacuumed database (corrupt8.test). (CVS 5391) FossilOrigin-Name: 620b472133438607c412e0c21d2a27605a89a414 --- manifest | 13 +++--- manifest.uuid | 2 +- src/btree.c | 32 ++++++++------- test/corrupt8.test | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 test/corrupt8.test diff --git a/manifest b/manifest index d827463dbc..aaf4425c8b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sbug\sin\sthe\smutex-testing\slogic\sintroduced\sby\scheck-in\s(5389).\s(CVS\s5390) -D 2008-07-10T20:41:50 +C Additional\stest\scoverage\sin\sbtree.c.\s\sAdded\scorruption\stests\sfor\nthe\sptrmap\spages\sof\san\sautovacuumed\sdatabase\s(corrupt8.test).\s(CVS\s5391) +D 2008-07-11T02:21:41 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a03f7cb4f7ad50bc53a788c6c544430e81f95de4 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -95,7 +95,7 @@ F src/attach.c b18ba42c77f7d3941f5d23d2ca20fa1d841a4e91 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d F src/btmutex.c 483ced3c52205b04b97df69161fadbf87f4f1ea2 -F src/btree.c 61c4752f4e05a5905d95170b0b5ddb24fb143b02 +F src/btree.c 71ba242014031cc6b30761094adad35ad4896a19 F src/btree.h 9373128fbd6509a281e0d356cb15f9cffbfa876c F src/btreeInt.h d59e58d39950a17c0fb7e004c90ab7696d3e7df5 F src/build.c bac7233d984be3805aaa41cf500f7ee12dc97249 @@ -248,6 +248,7 @@ F test/corrupt4.test acdb01afaedf529004b70e55de1a6f5a05ae7fff F test/corrupt5.test 7796d5bdfe155ed824cee9dff371f49da237cfe0 F test/corrupt6.test e69b877d478224deab7b66844566258cecacd25e F test/corrupt7.test f0ff354eb2f0a23035fbd06724b87cac95b55cc1 +F test/corrupt8.test c8ebf7cfe9fca7818a71907a2e433c4a38dbf838 F test/crash.test 1b6ac8410689ff78028887f445062dc897c9ac89 F test/crash2.test 26d7a4c5520201e5de2c696ea51ab946b59dc0e9 F test/crash3.test 0b09687ae1a3ccbcefdfaeb4b963e26e36255d76 @@ -601,7 +602,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P ed8b2525006ae7f8cacd01b291760513fdbdff57 -R efb7a3b7b52866a48ca989ffc22b1e05 +P 8fc462b6b7afe390463ea7b010fd3230d9acc358 +R 4385bebdc0676f5f0e502792343cbd11 U drh -Z 90b53c53d9eef880479c1542b15e3289 +Z f5f817655b5188ade4dd50e6a7a1f252 diff --git a/manifest.uuid b/manifest.uuid index 97bf57c80f..27ca667f5c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8fc462b6b7afe390463ea7b010fd3230d9acc358 \ No newline at end of file +620b472133438607c412e0c21d2a27605a89a414 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index a23cb7f250..940bfb64bc 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.475 2008/07/10 00:32:42 drh Exp $ +** $Id: btree.c,v 1.476 2008/07/11 02:21:41 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -97,6 +97,8 @@ static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ BtLock *pIter; assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); /* This is a no-op if the shared-cache is not enabled */ if( !p->sharable ){ @@ -125,7 +127,6 @@ static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ ** write-cursor does not change. */ if( - !p->db || 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK || iTab==MASTER_ROOT @@ -156,6 +157,8 @@ static int lockTable(Btree *p, Pgno iTable, u8 eLock){ BtLock *pIter; assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); /* This is a no-op if the shared-cache is not enabled */ if( !p->sharable ){ @@ -170,7 +173,6 @@ static int lockTable(Btree *p, Pgno iTable, u8 eLock){ ** the ReadUncommitted flag. */ if( - (p->db) && (p->db->flags&SQLITE_ReadUncommitted) && (eLock==READ_LOCK) && iTable!=MASTER_ROOT @@ -629,14 +631,13 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ ** for the overflow page. */ static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ - if( pCell ){ - CellInfo info; - sqlite3BtreeParseCellPtr(pPage, pCell, &info); - assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); - if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ - Pgno ovfl = get4byte(&pCell[info.iOverflow]); - return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); - } + CellInfo info; + assert( pCell!=0 ); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); + assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); + if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ + Pgno ovfl = get4byte(&pCell[info.iOverflow]); + return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); } return SQLITE_OK; } @@ -711,8 +712,8 @@ static void defragmentPage(MemPage *pPage){ ** Allocate nByte bytes of space on a page. ** ** Return the index into pPage->aData[] of the first byte of -** the new allocation. Or return 0 if there is not enough free -** space on the page to satisfy the allocation request. +** the new allocation. The caller guarantees that there is enough +** space. This routine will never fail. ** ** If the page contains nBytes of free space but does not contain ** nBytes of contiguous free space, then this routine automatically @@ -732,8 +733,9 @@ static int allocateSpace(MemPage *pPage, int nByte){ assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( pPage->pBt ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - if( nByte<4 ) nByte = 4; - if( pPage->nFreenOverflow>0 ) return 0; + assert( nByte>=0 ); /* Minimum cell size is 4 */ + assert( pPage->nFree>=nByte ); + assert( pPage->nOverflow==0 ); pPage->nFree -= nByte; hdr = pPage->hdrOffset; diff --git a/test/corrupt8.test b/test/corrupt8.test new file mode 100644 index 0000000000..f615781cca --- /dev/null +++ b/test/corrupt8.test @@ -0,0 +1,99 @@ +# 2008 June 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests to make sure SQLite does not crash or +# segfault if it sees a corrupt database file. It specifically focuses +# on corrupt pointer map pages. +# +# $Id: corrupt8.test,v 1.1 2008/07/11 02:21:41 drh Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# We must have the page_size pragma for these tests to work. +# +ifcapable !pager_pragmas||!autovacuum { + finish_test + return +} + +# Create a database to work with. +# +do_test corrupt8-1.1 { + execsql { + PRAGMA auto_vacuum=1; + PRAGMA page_size=1024; + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(1); + INSERT INTO t1(x) VALUES(2); + INSERT INTO t1(x) SELECT x+2 FROM t1; + INSERT INTO t1(x) SELECT x+4 FROM t1; + INSERT INTO t1(x) SELECT x+8 FROM t1; + INSERT INTO t1(x) SELECT x+16 FROM t1; + INSERT INTO t1(x) SELECT x+32 FROM t1; + INSERT INTO t1(x) SELECT x+64 FROM t1; + INSERT INTO t1(x) SELECT x+128 FROM t1; + INSERT INTO t1(x) SELECT x+256 FROM t1; + CREATE TABLE t2(a,b); + INSERT INTO t2 SELECT x, x*x FROM t1; + } + expr {[file size test.db]>1024*12} +} {1} +integrity_check corrupt8-1.2 + +# Loop through each ptrmap entry. Corrupt the entry and make sure the +# corruption is detected by the integrity_check. +# +for {set i 1024} {$i<2048} {incr i 5} { + set oldval [hexio_read test.db $i 1] + if {$oldval==0} break + hexio_write test.db $i 00 + do_test corrupt8-2.$i.0 { + db close + sqlite3 db test.db + set x [db eval {PRAGMA integrity_check}] + expr {$x!="ok"} + } {1} + for {set k 1} {$k<=5} {incr k} { + if {$k==$oldval} continue + hexio_write test.db $i 0$k + do_test corrupt8-2.$i.$k { + db close + sqlite3 db test.db + set x [db eval {PRAGMA integrity_check}] + expr {$x!="ok"} + } {1} + } + hexio_write test.db $i 06 + do_test corrupt8-2.$i.6 { + db close + sqlite3 db test.db + set x [db eval {PRAGMA integrity_check}] + expr {$x!="ok"} + } {1} + hexio_write test.db $i $oldval + if {$oldval>2} { + set i2 [expr {$i+1+$i%4}] + set oldval [hexio_read test.db $i2 1] + hexio_write test.db $i2 [format %02x [expr {($oldval+1)&0xff}]] + do_test corrupt8-2.$i.7 { + db close + sqlite3 db test.db + set x [db eval {PRAGMA integrity_check}] + expr {$x!="ok"} + } {1} + hexio_write test.db $i2 $oldval + } +} + + +finish_test