Open kak-0 opened 2 years ago
The SIGBUS also happens to kak file -e 'set global autoreload yes'
when running while true; do echo $RANDOM > file; done
, so I don't think this is specific to SSHFS.
mmap(2)
says:
SIGBUS: Attempted access to a page of the buffer that lies beyond the end of the mapped file.
The parse_file()
function pointed to is, I think, this one:
Note that we get the file size on line 122, and then access the file data on line 137, a classic time-of-check-to-time-of-use (TOCTOU) bug. It's possible (although unlikely) for the file-size to change between those two moments (as in krobelus' test-case) and cause Kakoune to crash.
It's possible that SSHFS makes the problem worse - maybe it caches the expected file-size locally (so that the 'time of check' is not when Kakoune checks, but when the cache was last refreshed), or maybe it's just network latency that pushes "time-of-check" and "time-of-use" far enough apart to cause problems.
Unfortunately, unlike other APIs there's no good way to mmap() the entire file in one step and prevent TOCTOU bugs entirely. Alternatives include:
mmap(2)
and only use read(2)
/write(2)
for file I/O.
Version of Kakoune
v2021.11.08-59-g9acd4e62
Reproducer
Kakoune occasionally crashes when reloading a file that's mounted with SSHFS after the file is modified outside of Kakoune (e.g. after running
git checkout
on the remote machine).Outcome
Killed by signal SIGBUS.
Backtrace:
Expectations
No response
Additional information
I got a SIGBUS signal in
parse_file
on this line:This line tries to read a memory-mapped file mapped in
MappedFile::MappedFile
:mmaps(2) says "It is unspecified whether changes made to the file after the mmap() call are visible in the mapped region" when using
MAP_PRIVATE
. Maybe the crash is due to a race condition where the file contents or size changes while Kakoune is reading it.This could also be a bug in how SSHFS implements mmap.
In either case, I guess the only workaround is to read the file with
read(2)
instead ofmmap(2)
.Does anyone else experience crashes when using SSHFS? Is there another workaround?