cosmopolitan/tool
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
..
args Pay off more technical debt 2022-09-12 23:36:56 -07:00
build Make mkdeps more helpful when files are missing 2023-02-23 08:11:08 -08:00
curl Add support for the -i (include headers) and -d (POST data) options in curl.com (#708) 2022-12-23 17:06:48 -08:00
decode Rewrite brk() and sbrk() 2022-10-01 23:11:56 -07:00
emacs Make some system call fixes 2023-02-12 22:16:34 -08:00
hash Make important improvements 2022-09-14 22:39:08 -07:00
lambda Clean up some code 2022-08-20 12:32:51 -07:00
net Redbean sqlite fix multi close (#749) 2023-02-23 20:10:03 -08:00
plinko Make more threading improvements 2022-11-01 23:28:26 -07:00
scripts Implement program for finding start of zip content 2023-01-06 19:55:00 -08:00
viz Increase stack size to 128k and guard size to 16k 2022-12-18 22:58:29 -08:00
tool.mk Make curl example into a complete tool (#706) 2022-12-21 16:12:02 -08:00