File
src/fault.c
— part of check-in
[f360818737]
at
2016-12-07 15:49:02
on branch trunk
— Rename the SQLITE_OMIT_BUILTIN_TEST compile-time option to SQLITE_UNTESTABLE.
(user:
drh
size: 2693)
[more...]
0000: 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 32 /*.** 2008 Jan 2
0010: 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
0020: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0030: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0040: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0050: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0060: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0070: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0080: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
0090: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
00a0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
00b0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
00c0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
00d0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
00e0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
00f0: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0100: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0110: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0120: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0180: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
0190: 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f o support the co
01a0: 6e 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e ncept of "benign
01b0: 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 " .** malloc fai
01c0: 6c 75 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 lures (when the
01d0: 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 xMalloc() or xRe
01e0: 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f alloc() method o
01f0: 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 f the.** sqlite3
0200: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 _mem_methods str
0210: 75 63 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 ucture fails to
0220: 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b allocate a block
0230: 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e of memory.** an
0240: 64 20 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a d returns 0). .*
0250: 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 *.** Most malloc
0260: 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f failures are no
0270: 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 n-benign. After
0280: 74 68 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 they occur, SQLi
0290: 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 te.** abandons t
02a0: 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 he current opera
02b0: 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 tion and returns
02c0: 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 an error code (
02d0: 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 usually.** SQLIT
02e0: 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 E_NOMEM) to the
02f0: 75 73 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 user. However, s
0300: 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 ometimes a fault
0310: 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
0320: 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f ily.** fatal. Fo
0330: 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 r example, if a
0340: 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 malloc fails whi
0350: 6c 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 le resizing a ha
0360: 73 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a sh table, this .
0370: 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 ** is completely
0380: 20 72 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d recoverable sim
0390: 70 6c 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 ply by not carry
03a0: 69 6e 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 ing out the resi
03b0: 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 ze. The .** hash
03c0: 20 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 table will cont
03d0: 69 6e 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e inue to function
03e0: 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 normally. So a
03f0: 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
0400: 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 .** during a has
0410: 68 20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 h table resize i
0420: 73 20 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 s a benign fault
0430: 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 22 ..*/..#include "
0440: 73 71 6c 69 74 65 49 6e 74 2e 68 22 0a 0a 23 69 sqliteInt.h"..#i
0450: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 55 4e 54 fndef SQLITE_UNT
0460: 45 53 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 47 ESTABLE../*.** G
0470: 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e lobal variables.
0480: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
0490: 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 ct BenignMallocH
04a0: 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f ooks BenignMallo
04b0: 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 cHooks;.static S
04c0: 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
04d0: 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
04e0: 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 ks {. void (*xB
04f0: 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 enignBegin)(void
0500: 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e );. void (*xBen
0510: 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d ignEnd)(void);.}
0520: 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 sqlite3Hooks =
0530: 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 { 0, 0 };../* Th
0540: 65 20 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 e "wsdHooks" mac
0550: 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
0560: 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 to the appropria
0570: 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 te BenignMallocH
0580: 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 ooks.** structur
0590: 65 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 e. If writable
05a0: 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 static data is u
05b0: 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 nsupported on th
05c0: 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 e target,.** we
05d0: 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 have to locate t
05e0: 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 he state vector
05f0: 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e at run-time. In
0600: 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e the more common
0610: 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 .** case where w
0620: 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
0630: 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 ata is supported
0640: 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 , wsdHooks can r
0650: 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a efer directly.**
0660: 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 to the "sqlite3
0670: 48 6f 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 Hooks" state vec
0680: 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f tor declared abo
0690: 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ve..*/.#ifdef SQ
06a0: 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 LITE_OMIT_WSD.#
06b0: 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 define wsdHooksI
06c0: 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 nit \. BenignMa
06d0: 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 llocHooks *x = &
06e0: 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c GLOBAL(BenignMal
06f0: 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 locHooks,sqlite3
0700: 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 Hooks).# define
0710: 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 wsdHooks x[0].#e
0720: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 lse.# define wsd
0730: 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 HooksInit.# defi
0740: 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 ne wsdHooks sqli
0750: 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a te3Hooks.#endif.
0760: 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
0770: 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 hooks to call w
0780: 68 65 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e hen sqlite3Begin
0790: 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 BenignMalloc() a
07a0: 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 nd.** sqlite3End
07b0: 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 BenignMalloc() a
07c0: 72 65 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 re called, respe
07d0: 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 ctively..*/.void
07e0: 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 sqlite3BenignMa
07f0: 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 llocHooks(. voi
0800: 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e d (*xBenignBegin
0810: 29 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 )(void),. void
0820: 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f (*xBenignEnd)(vo
0830: 69 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b id).){. wsdHook
0840: 73 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b sInit;. wsdHook
0850: 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d s.xBenignBegin =
0860: 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 xBenignBegin;.
0870: 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 wsdHooks.xBenig
0880: 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e nEnd = xBenignEn
0890: 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 d;.}../*.** This
08a0: 20 28 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 (sqlite3EndBeni
08b0: 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 gnMalloc()) is c
08c0: 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 alled by SQLite
08d0: 63 6f 64 65 20 74 6f 20 69 6e 64 69 63 61 74 65 code to indicate
08e0: 20 74 68 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 that.** subsequ
08f0: 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 ent malloc failu
0900: 72 65 73 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 res are benign.
0910: 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 A call to sqlite
0920: 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
0930: 28 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 ().** indicates
0940: 74 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 that subsequent
0950: 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
0960: 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a are non-benign..
0970: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 */.void sqlite3B
0980: 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
0990: 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f (void){. wsdHoo
09a0: 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 ksInit;. if( ws
09b0: 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 dHooks.xBenignBe
09c0: 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 64 48 6f gin ){. wsdHo
09d0: 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e oks.xBenignBegin
09e0: 28 29 3b 0a 20 20 7d 0a 7d 0a 76 6f 69 64 20 73 ();. }.}.void s
09f0: 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
0a00: 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 alloc(void){. w
0a10: 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 sdHooksInit;. i
0a20: 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e f( wsdHooks.xBen
0a30: 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 77 73 ignEnd ){. ws
0a40: 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
0a50: 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 d();. }.}..#end
0a60: 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 66 20 if /* #ifndef
0a70: 53 51 4c 49 54 45 5f 55 4e 54 45 53 54 41 42 4c SQLITE_UNTESTABL
0a80: 45 20 2a 2f 0a E */.