stdlib: handle EEXIST in mmap with FIXED_NOREPLACE. Fixes #21475

This commit is contained in:
Alec Fessler 2025-02-22 15:07:44 -05:00 committed by Andrew Kelley
parent ecc76348e6
commit 1cc388d526
3 changed files with 10 additions and 2 deletions

View File

@ -1420,6 +1420,7 @@ const LinuxThreadImpl = struct {
error.PermissionDenied => unreachable,
error.ProcessFdQuotaExceeded => unreachable,
error.SystemFdQuotaExceeded => unreachable,
error.MappingAlreadyExists => unreachable,
else => |e| return e,
};
assert(mapped.len >= map_bytes);

View File

@ -2434,14 +2434,17 @@ pub const ElfModule = struct {
const end_pos = elf_file.getEndPos() catch return bad();
const file_len = cast(usize, end_pos) orelse return error.Overflow;
const mapped_mem = try std.posix.mmap(
const mapped_mem = std.posix.mmap(
null,
file_len,
std.posix.PROT.READ,
.{ .TYPE = .SHARED },
elf_file.handle,
0,
);
) catch |err| switch (err) {
error.MappingAlreadyExists => unreachable,
else => |e| return e,
};
errdefer std.posix.munmap(mapped_mem);
return load(

View File

@ -4754,6 +4754,9 @@ pub const MMapError = error{
ProcessFdQuotaExceeded,
SystemFdQuotaExceeded,
OutOfMemory,
/// Using FIXED_NOREPLACE flag and the process has already mapped memory at the given address
MappingAlreadyExists,
} || UnexpectedError;
/// Map files or devices into memory.
@ -4792,6 +4795,7 @@ pub fn mmap(
.MFILE => return error.ProcessFdQuotaExceeded,
.NFILE => return error.SystemFdQuotaExceeded,
.NOMEM => return error.OutOfMemory,
.EXIST => return error.MappingAlreadyExists,
else => return unexpectedErrno(err),
}
}