LibCore: Don’t pass O_CLOEXEC to shm_open

Bug: Ladybird crashes on launch on macOS 26.4+ with “UNEXPECTED ERROR:
close: Bad file descriptor” in SharedVersion.cpp — because
AnonymousBuffer creation fails.

Cause: anon_create() passed O_CLOEXEC in the oflag argument to shm_open.
macOS 26.4+ rejects that with EINVAL. POSIX only specifies O_RDONLY,
O_RDWR, O_CREAT, O_EXCL, and O_TRUNC as valid shm_open flags; on earlier
macOS versions, O_CLOEXEC just happened to also be silently accepted.

Fix: Filter O_CLOEXEC from the oflag argument we pass to shm_open flags,
and instead set FD_CLOEXEC via fcntl — after opening.
This commit is contained in:
sideshowbarker
2026-03-26 00:38:53 +09:00
committed by Luke Wilde
parent 201e615aad
commit d7053a8eb8
Notes: github-actions[bot] 2026-03-31 16:22:31 +00:00

View File

@@ -155,13 +155,24 @@ ErrorOr<int> anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti
static size_t shared_memory_id = 0;
auto name = ByteString::formatted("/shm-{}-{}", getpid(), shared_memory_id++);
fd = shm_open(name.characters(), O_RDWR | O_CREAT | options, 0600);
// Passing O_CLOEXEC to shm_open in the oflag argument isn't POSIX-compliant and is known to be rejected
// in macOS 26.4+. So we filter it out here, and instead set FD_CLOEXEC via fcntl after opening.
fd = shm_open(name.characters(), O_RDWR | O_CREAT | (options & ~O_CLOEXEC), 0600);
if (shm_unlink(name.characters()) == -1) {
auto saved_errno = errno;
TRY(close(fd));
if (fd >= 0)
TRY(close(fd));
return Error::from_errno(saved_errno);
}
if (fd >= 0 && (options & O_CLOEXEC)) {
if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
auto saved_errno = errno;
TRY(close(fd));
return Error::from_errno(saved_errno);
}
}
#endif
if (fd < 0)
return Error::from_errno(errno);