Translate fcntl flock l_type between Linux and macOS#80
Merged
Conversation
The F_GETLK/F_SETLK/F_SETLKW handler copied the guest's l_type field straight into the host struct flock, on the assumption (stated in a comment) that the lock-type constants match. They do not: Linux aarch64: F_RDLCK=0, F_WRLCK=1, F_UNLCK=2 macOS/BSD: F_RDLCK=1, F_UNLCK=2, F_WRLCK=3 A Linux F_RDLCK (0) is not a valid type on macOS, so the host fcntl() rejected it with EINVAL. POSIX-locking databases take a shared read lock first -- SQLite, for example, fails to open any on-disk database with "disk I/O error" because its initial F_RDLCK on the pending byte never succeeds. Map l_type Linux->macOS on input for all three commands, and map it back macOS->Linux when writing the F_GETLK result, rejecting unknown types with EINVAL. Add test-flock, which locks around SQLite's 1GiB pending-byte offsets; it fails with errno 22 before this change and passes after.
Contributor
|
Thank @Max042004 for contributing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
F_GETLK/F_SETLK/F_SETLKWhandler insrc/syscall/fs.ccopied theguest's
l_typefield straight into the hoststruct flock, on theassumption (stated in a comment) that the lock-type constants match. They do
not:
F_RDLCKF_WRLCKF_UNLCKA Linux
F_RDLCK(0) is not a valid lock type on macOS, so the hostfcntl()rejects it withEINVAL. POSIX-locking databases take a sharedread lock first, so this breaks them. Concretely, SQLite fails to open any
on-disk database with
disk I/O error (10): its initialF_RDLCKon the1 GiB "pending byte" never succeeds. In-memory databases are unaffected,
which is what made this easy to miss.
Trace from a failing
sqlite3 file.db "CREATE TABLE ..."under--verbose:Fix
Map
l_typeLinux→macOS on input for all three commands, and map it backmacOS→Linux when writing the
F_GETLKresult. Unknown types are rejectedwith
EINVALrather than passed through.Test
Adds
tests/test-flock.c(registered inmanifest.txtunder "Syscallcoverage tests"), which exercises
F_SETLK/F_SETLKW/F_GETLKaround thesame 1 GiB pending-byte offsets SQLite uses. It fails with
errno 22before this change and passes after — verified by reverting the fix.
Validation
make check: all 97 tests pass (including the newtest-flock).check-newline,check-format(clang-format 22),check-security,cppcheck, syscall-dispatch consistency, andmake lintall clean;make elfusebuilds and codesigns.CRUD, transactions, JSON1 function indexes, FTS5, WAL +
wal_checkpoint,a 50k-row transaction, and
.dump/.readround-trips all succeed; theresulting file is readable by the host
sqlite3.Summary by cubic
Fixes
fcntllock-type translation between Linux guests and macOS hosts to prevent EINVAL on valid read locks. Restores on-disk SQLite and other POSIX-locking apps by correctly mappingl_typeforF_GETLK,F_SETLK, andF_SETLKW.l_typeLinux→macOS on input and macOS→Linux onF_GETLKoutput; reject unknown types withEINVAL.tests/test-flock.c(registered intests/manifest.txt) to cover SQLite’s pending-byte lock pattern; failed before, passes now.Written for commit 63e7a93. Summary will update on new commits.