From 5efaf070991267cf9c8b4b7e5b3ed1e512d50d41 Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 18 Mar 2008 00:07:10 +0000 Subject: [PATCH] Add the ability to simulate out-of-memory errors when using the default memory allocator, mem1.c. Fix a bug that this enhancement revealed. (CVS 4875) FossilOrigin-Name: d55a5e1c11ef90534abd2e5f18d06dd4739ade70 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/mem1.c | 26 +++++++++++++++++--------- src/test_malloc.c | 14 +++++--------- src/vdbemem.c | 3 ++- test/malloc_common.tcl | 8 ++++---- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/manifest b/manifest index 1d8ca0f4d8..18b0ffb355 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Version\s3.5.7\s(CVS\s4874) -D 2008-03-17T19:03:04 +C Add\sthe\sability\sto\ssimulate\sout-of-memory\serrors\swhen\susing\sthe\sdefault\nmemory\sallocator,\smem1.c.\s\sFix\sa\sbug\sthat\sthis\senhancement\srevealed.\s(CVS\s4875) +D 2008-03-18T00:07:11 F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7 F Makefile.in 5be94fea84f1599672e5041de03b97990baca593 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -108,7 +108,7 @@ F src/loadext.c eceaebcff9807519ed9494056648d0d879f93ed3 F src/main.c 15db7dda6703f9a41a70337466215bf1a0e1fdcf F src/malloc.c 60e392a4c12c839517f9b0db7b995f825444fb35 F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217 -F src/mem1.c 62a821702d3292809ca78e7c55c3ca04b05a3757 +F src/mem1.c fc716ff521b6dd3e43eaa211967383308800e70a F src/mem2.c a17fd71f39e593f46445282c36077231e70a31eb F src/mem3.c 52547678a2ae50c203d54be1a5bf51eb02438a3f F src/mem4.c 45c328ec6dcb7e8d319cb383615b5fe547ca5409 @@ -161,7 +161,7 @@ F src/test_config.c 9223ff4a7b8b97c9d12965b0123db1cbd6757efb F src/test_devsym.c cee1aecaa90c895030399ca4ae38f84a08038f8a F src/test_hexio.c 1a1cd8324d57585ea86b922f609fa1fbaaf9662d F src/test_loadext.c 22065d601a18878e5542191001f0eaa5d77c0ed8 -F src/test_malloc.c 85e702341c752810017c94c40f2a1ac92128abc0 +F src/test_malloc.c cdad54dc6a4152ba2681073cb21f74e76da95551 F src/test_md5.c c107c96637123239c3518b5fbe97a79130f4d32e F src/test_onefile.c 2fea6d22f13f5f286356c80c77ffd41f995f2b7a F src/test_schema.c 12c9de7661d6294eec2d57afbb52e2af1128084f @@ -181,7 +181,7 @@ F src/vdbeapi.c cf9fc963efae3cdf5de08e2a9718b487059c7fc5 F src/vdbeaux.c 83e34e38706889a16fbd77fce3777390d425af79 F src/vdbeblob.c 63c750acc7b5012479f508c0e9627372a82cb65d F src/vdbefifo.c a30c237b2a3577e1415fb6e288cbb6b8ed1e5736 -F src/vdbemem.c 7a39ad5579e0a9e7305a8fe9a2095840f0ab8454 +F src/vdbemem.c eca1f7ae77f5de66cd6809800d98f6a9186e0f47 F src/vtab.c 00cd16317b29495c185ff40e4b227917d5a371b2 F src/where.c 78d6689d7154d8d41c1a913e17bce9a320981653 F tclinstaller.tcl 4356d9d94d2b5ed5e68f9f0c80c4df3048dd7617 @@ -389,7 +389,7 @@ F test/mallocD.test f78c295e8e18ea3029e65ca08278690e00c22100 F test/mallocE.test db1ed69d7eded1b080952e2a7c37f364ad241b08 F test/mallocF.test 2d5c590ebc2fc7f0dcebdf5aa8498b9aed69107e F test/mallocG.test b295dc03b6d8d705ce425ff4d1ce6bbeb1c5ab33 -F test/malloc_common.tcl bb48df8df8c3ae75a9cb2d3626d54202b2ba2401 +F test/malloc_common.tcl 5f7b853b2203bd61c9e3c504d461bc3202d13972 F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8 F test/memdb.test a67bda4ff90a38f2b19f6c7f95aa7289e051d893 F test/memleak.test d2d2a1ff7105d32dc3fdf691458cf6cba58c7217 @@ -623,7 +623,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 -P fcf3d0a3d5d3a71155ab0aa5f533da72063d54f0 -R 1dda9680613a1b8479a4668dc28f14c0 +P 9a6583d375a1b922e88eed888e54e51cbc85f400 +R 31d912fc5305f78d6110f0f1f2963749 U drh -Z fbb5cb4a11e45c4a408698a49a0c8107 +Z 80b52909a8b7e1801c400c7055de8bb6 diff --git a/manifest.uuid b/manifest.uuid index 273fc4ee5e..c124686b40 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9a6583d375a1b922e88eed888e54e51cbc85f400 \ No newline at end of file +d55a5e1c11ef90534abd2e5f18d06dd4739ade70 \ No newline at end of file diff --git a/src/mem1.c b/src/mem1.c index 2b28a8cdb8..87dd41afc2 100644 --- a/src/mem1.c +++ b/src/mem1.c @@ -12,7 +12,7 @@ ** This file contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** -** $Id: mem1.c,v 1.16 2008/02/14 23:26:56 drh Exp $ +** $Id: mem1.c,v 1.17 2008/03/18 00:07:11 drh Exp $ */ #include "sqliteInt.h" @@ -137,10 +137,14 @@ void *sqlite3_malloc(int nBytes){ if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){ sqlite3MemsysAlarm(nBytes); } - p = malloc(nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ + p = 0; + }else{ p = malloc(nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = malloc(nBytes+8); + } } if( p ){ p[0] = nBytes; @@ -205,12 +209,16 @@ void *sqlite3_realloc(void *pPrior, int nBytes){ if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){ sqlite3MemsysAlarm(nBytes-nOld); } - p = realloc(p, nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); - p = pPrior; - p--; + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ + p = 0; + }else{ p = realloc(p, nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = pPrior; + p--; + p = realloc(p, nBytes+8); + } } if( p ){ p[0] = nBytes; diff --git a/src/test_malloc.c b/src/test_malloc.c index 86a32059fd..38708009af 100644 --- a/src/test_malloc.c +++ b/src/test_malloc.c @@ -13,7 +13,7 @@ ** This file contains code used to implement test interfaces to the ** memory allocation subsystem. ** -** $Id: test_malloc.c,v 1.15 2008/02/19 15:15:16 drh Exp $ +** $Id: test_malloc.c,v 1.16 2008/03/18 00:07:11 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -463,18 +463,14 @@ static int test_memdebug_pending( int objc, Tcl_Obj *CONST objv[] ){ + int nPending; if( objc!=1 ){ Tcl_WrongNumArgs(interp, 1, objv, ""); return TCL_ERROR; } - -#if defined(SQLITE_MEMDEBUG) || defined(SQLITE_POW2_MEMORY_SIZE) - { - int nPending = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_PENDING, - SQLITE_FAULTINJECTOR_MALLOC); - Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); - } -#endif + nPending = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_PENDING, + SQLITE_FAULTINJECTOR_MALLOC); + Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); return TCL_OK; } diff --git a/src/vdbemem.c b/src/vdbemem.c index 632151c19e..1c72cb37a0 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -81,7 +81,7 @@ int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ || (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==MEM_Static ); - if( ((f&MEM_Dyn)==0 || pMem->xDel || sqlite3MallocSize(pMem->z)xDel || sqlite3MallocSize(pMem->z)db, (n>32?n:32)); } if( !z ){ + pMem->flags = MEM_Null; return SQLITE_NOMEM; } } diff --git a/test/malloc_common.tcl b/test/malloc_common.tcl index fafe643a84..534241ab95 100644 --- a/test/malloc_common.tcl +++ b/test/malloc_common.tcl @@ -12,15 +12,15 @@ # This file contains common code used by many different malloc tests # within the test suite. # -# $Id: malloc_common.tcl,v 1.13 2008/02/18 22:24:58 drh Exp $ +# $Id: malloc_common.tcl,v 1.14 2008/03/18 00:07:11 drh Exp $ # If we did not compile with malloc testing enabled, then do nothing. # -ifcapable !memdebug&&!mem5 { +ifcapable faultinjector { + set MEMDEBUG 1 +} else { set MEMDEBUG 0 return 0 -} else { - set MEMDEBUG 1 } # Usage: do_malloc_test