Skip to content

Commit db6d02b

Browse files
AlecFesslera-khabarov
authored andcommitted
stdlib: handle EEXIST in mmap with FIXED_NOREPLACE. Fixes #21475
1 parent 5f22c3b commit db6d02b

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

lib/std/Thread.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,7 @@ const LinuxThreadImpl = struct {
14201420
error.PermissionDenied => unreachable,
14211421
error.ProcessFdQuotaExceeded => unreachable,
14221422
error.SystemFdQuotaExceeded => unreachable,
1423+
error.MappingAlreadyExists => unreachable,
14231424
else => |e| return e,
14241425
};
14251426
assert(mapped.len >= map_bytes);

lib/std/debug/Dwarf.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,14 +2434,17 @@ pub const ElfModule = struct {
24342434
const end_pos = elf_file.getEndPos() catch return bad();
24352435
const file_len = cast(usize, end_pos) orelse return error.Overflow;
24362436

2437-
const mapped_mem = try std.posix.mmap(
2437+
const mapped_mem = std.posix.mmap(
24382438
null,
24392439
file_len,
24402440
std.posix.PROT.READ,
24412441
.{ .TYPE = .SHARED },
24422442
elf_file.handle,
24432443
0,
2444-
);
2444+
) catch |err| switch (err) {
2445+
error.MappingAlreadyExists => unreachable,
2446+
else => |e| return e,
2447+
};
24452448
errdefer std.posix.munmap(mapped_mem);
24462449

24472450
return load(

lib/std/posix.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4754,6 +4754,9 @@ pub const MMapError = error{
47544754
ProcessFdQuotaExceeded,
47554755
SystemFdQuotaExceeded,
47564756
OutOfMemory,
4757+
4758+
/// Using FIXED_NOREPLACE flag and the process has already mapped memory at the given address
4759+
MappingAlreadyExists,
47574760
} || UnexpectedError;
47584761

47594762
/// Map files or devices into memory.
@@ -4792,6 +4795,7 @@ pub fn mmap(
47924795
.MFILE => return error.ProcessFdQuotaExceeded,
47934796
.NFILE => return error.SystemFdQuotaExceeded,
47944797
.NOMEM => return error.OutOfMemory,
4798+
.EXIST => return error.MappingAlreadyExists,
47954799
else => return unexpectedErrno(err),
47964800
}
47974801
}

0 commit comments

Comments
 (0)