cosmopolitan/tool/net
Paul Kulchenko db16f0129a
Redbean sqlite fix multi close (#749)
* Update redbean SQLite config to handle more options

This requires moving sqlite3_initialize call to open, as configuration
should be allowed before initialization is done. This call is effective
only for the first time and then no-op after that.

* Fix redbean SQLite for closing db with already finalized statements

There is a loop in cleanupdb that finalizes all vms that are associated
with that db when it's being closed. Under some circumstances (detailed
below) that loop may contain references pointing to already collected
objects, thus leading to SIGSEGV when those references are used.

This may happen with the following sequence of events ("VM" is the name
used in lsqlite and describes the same thing as "statement"):
1. A finalized statement is created (for example, by preparing an empty
string or a string with no statement that is still grammatically valid).
2. This statement goes out of scope before the DB object it's associated
with does and is garbage collected.
3. When it's garbage collected, dbvm_gc method is called, which checks
for svm->vm being not NULL.
4. Since the VM is already finalized, cleanupvm method is not called,
so the VM reference is not removed from the table of VMs tracked for
that DB.
5. When the DB is finally closed or garbage collected, all the VMs
associated with it are accessed to be finalized, including the ones that
have been garbage collected and have invalid references (thus leading
to a memory access error).

Here is an example of a stacktrace from the resulting SIGSEGV:

70000003de20 5df71a getgeneric+26
70000003fac0 5dfc7f luaH_get+111
70000003faf0 5e06c8 luaH_set+40
70000003fb20 5c5bd7 aux_rawset+55
70000003fb50 5c70cb lua_rawset+27
70000003fb60 4fa8e7 cleanupvm+71
70000003fb80 4fa988 cleanupdb+88
70000003fbc0 4fe899 db_gc+41

One way to fix this is to use userdata references (which anchor their
targets) instead of lightuserdata references (which do not), but this
would prevent the targets (VMs) from being garbage collected until the
DB itself is garbage collected, so this needs to be combined with
weakening the keys in the DB table. The code in cleanupdb to remove the
VM references is no longer needed, as this is handled by having weak keys.

The patch also switches to using close_v2, as it is intended for use
with garbage collected languages where the order in which destructors
are called is arbitrary, as is the case here.

* Remove GC collection from redbean SQLite session

The behavior of sqlite3session_delete is undefined after the DB
connection is closed, so we need to avoid calling it from gc handler.
2023-02-23 20:10:03 -08:00
..
demo FIx link in gensvg demo (#723) 2023-01-28 09:12:13 -08:00
tiny Fix bugs with recent change 2022-04-16 10:40:23 -07:00
.init.lua Add SSL to redbean 2021-06-24 13:20:50 -07:00
counters.inc Make fixes and improvements 2022-10-19 07:19:19 -07:00
definitions.lua doc: remove "everything everything" from ParseUrl doc (#704) 2023-02-23 09:34:48 -08:00
dig.c Restart CI for New Technology and UBSAN hunting 2022-03-21 04:32:57 -07:00
echo.c Make improvements 2022-11-07 02:26:06 -08:00
favicon.ico Make major improvements to redbean and libraries 2021-04-18 12:34:15 -07:00
fetch.inc Fix bugs with Redbean Fetch() uploading 2023-01-05 09:52:36 -08:00
help.txt doc: remove "everything everything" from ParseUrl doc (#704) 2023-02-23 09:34:48 -08:00
largon2.c WIP: Correct all typos (#498) 2022-07-20 14:01:15 -07:00
lfinger.c Add tcp syn packet fingerprinting to redbean 2022-07-17 02:43:49 -07:00
lfinger.h Add tcp syn packet fingerprinting to redbean 2022-07-17 02:43:49 -07:00
lfuncs.c Greatly expand system() shell code features 2022-10-11 21:30:31 -07:00
lfuncs.h Do some work on redbean 2022-07-22 20:44:24 -07:00
ljson.c Increase stack size to 128k and guard size to 16k 2022-12-18 22:58:29 -08:00
ljson.h Further improve JSON serialization 2022-07-11 23:06:49 -07:00
lmaxmind.c Fix important bugs in redbean 2022-09-17 02:18:06 -07:00
lpath.c Make improvements 2022-07-21 03:36:42 -07:00
lpath.h Make improvements 2022-07-21 03:36:42 -07:00
lre.c Fix re.NOSUB memory error (#645) 2022-10-02 12:49:23 -07:00
lsqlite3.c Redbean sqlite fix multi close (#749) 2023-02-23 20:10:03 -08:00
luacheck.h Fix reporting of Lua stack items from various hook calls (#395) 2022-04-27 21:57:52 -07:00
net.mk Update assimilate to work with modern APE format 2023-01-04 06:34:48 -08:00
redbean-original.c Remove plenty of makefile misconfigurations 2022-07-21 09:20:59 -07:00
redbean-static.c Remove plenty of makefile misconfigurations 2022-07-21 09:20:59 -07:00
redbean-unsecure.c Remove plenty of makefile misconfigurations 2022-07-21 09:20:59 -07:00
redbean.c Remove slowloris handling from Redbean (#737) 2023-02-22 18:56:02 -08:00
redbean.png Make improvements 2020-09-14 00:02:34 -07:00
sandbox.h Strengthen the pledge() polyfill 2022-06-27 13:02:17 -07:00
wb.c Improve redbean method=get parameter handling 2022-09-19 20:04:39 -07:00