Merge pull request #19312 from mikdusan/bsd-debitrot

bsd: debitrot
This commit is contained in:
Andrew Kelley 2024-03-15 16:59:05 -07:00 committed by GitHub
commit ce4245f873
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 108 additions and 73 deletions

View File

@ -246,7 +246,7 @@ const emutls_control = extern struct {
// Two threads could race against the same emutls_control.
// Use atomic for reading coherent value lockless.
const index_lockless = @atomicLoad(usize, &self.object.index, .Acquire);
const index_lockless = @atomicLoad(usize, &self.object.index, .acquire);
if (index_lockless != 0) {
// index is already initialized, return it.
@ -264,7 +264,7 @@ const emutls_control = extern struct {
}
// Store a new index atomically (for having coherent index_lockless reading).
@atomicStore(usize, &self.object.index, emutls_control.next_index, .Release);
@atomicStore(usize, &self.object.index, emutls_control.next_index, .release);
// Increment the next available index
emutls_control.next_index += 1;

View File

@ -801,7 +801,7 @@ const PosixImpl = struct {
assert(std.c.pthread_mutex_lock(&bucket.mutex) == .SUCCESS);
defer assert(std.c.pthread_mutex_unlock(&bucket.mutex) == .SUCCESS);
cancelled = ptr.load(.Monotonic) != expect;
cancelled = ptr.load(.monotonic) != expect;
if (cancelled) {
return;
}
@ -855,14 +855,14 @@ const PosixImpl = struct {
// The pending count increment in wait() must also now use seq_cst for the update + this pending load
// to be in the same modification order as our load isn't using release/acquire to guarantee it.
bucket.pending.fence(.seq_cst);
if (bucket.pending.load(.Monotonic) == 0) {
if (bucket.pending.load(.monotonic) == 0) {
return;
}
// Keep a list of all the waiters notified and wake then up outside the mutex critical section.
var notified = WaitList{};
defer if (notified.len > 0) {
const pending = bucket.pending.fetchSub(notified.len, .Monotonic);
const pending = bucket.pending.fetchSub(notified.len, .monotonic);
assert(pending >= notified.len);
while (notified.pop()) |waiter| {
@ -875,7 +875,7 @@ const PosixImpl = struct {
defer assert(std.c.pthread_mutex_unlock(&bucket.mutex) == .SUCCESS);
// Another pending check again to avoid the WaitQueue lookup if not necessary.
if (bucket.pending.load(.Monotonic) > 0) {
if (bucket.pending.load(.monotonic) > 0) {
notified = WaitQueue.remove(&bucket.treap, address, max_waiters);
}
}

View File

@ -375,5 +375,5 @@ test "concurrent access" {
try testing.expectEqual(num_writes, runner.writes);
//std.debug.print("reads={}\n", .{ runner.reads.load(.Unordered)});
//std.debug.print("reads={}\n", .{ runner.reads.load(.unordered)});
}

View File

@ -159,7 +159,7 @@ test Value {
// acquire ensures count decrement and code before
// previous unrefs()s happens-before we call dropFn
// below.
// Another alternative is to use .AcqRel on the
// Another alternative is to use .acq_rel on the
// fetchSub count decrement but it's extra barrier in
// possibly hot path.
rc.count.fence(.acquire);

View File

@ -1502,25 +1502,19 @@ pub extern "c" fn closedir(dp: *DIR) c_int;
pub extern "c" fn telldir(dp: *DIR) c_long;
pub extern "c" fn seekdir(dp: *DIR, loc: c_long) void;
pub extern "c" fn clock_gettime(clk_id: c_int, tp: *c.timespec) c_int;
pub extern "c" fn clock_getres(clk_id: c_int, tp: *c.timespec) c_int;
pub extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
pub extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
pub extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
pub extern "c" fn sched_yield() c_int;
pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;
pub extern "c" fn sigfillset(set: ?*c.sigset_t) void;
pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;
pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
pub extern "c" fn alarm(seconds: c_uint) c_uint;
pub extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
pub const clock_getres = switch (native_os) {
.netbsd => private.__clock_getres50,
else => private.clock_getres,
};
pub const clock_gettime = switch (native_os) {
.netbsd => private.__clock_gettime50,
else => private.clock_gettime,
};
pub const fstat = switch (native_os) {
.macos => switch (native_arch) {
@ -1539,6 +1533,31 @@ pub const fstatat = switch (native_os) {
else => private.fstatat,
};
pub const getdirentries = switch (native_os) {
.macos, .ios, .tvos, .watchos => private.__getdirentries64,
else => private.getdirentries,
};
pub const getrusage = switch (native_os) {
.netbsd => private.__getrusage50,
else => private.getrusage,
};
pub const gettimeofday = switch (native_os) {
.netbsd => private.__gettimeofday50,
else => private.gettimeofday,
};
pub const msync = switch (native_os) {
.netbsd => private.__msync13,
else => private.msync,
};
pub const nanosleep = switch (native_os) {
.netbsd => private.__nanosleep50,
else => private.nanosleep,
};
pub const readdir = switch (native_os) {
.macos => switch (native_arch) {
.x86_64 => private.@"readdir$INODE64",
@ -1549,10 +1568,35 @@ pub const readdir = switch (native_os) {
};
pub const realpath = switch (native_os) {
.macos, .ios, .watchos, .tvos => private.@"realpath$DARWIN_EXTSN",
.macos, .ios, .tvos, .watchos => private.@"realpath$DARWIN_EXTSN",
else => private.realpath,
};
pub const sched_yield = switch (native_os) {
.netbsd => private.__libc_thr_yield,
else => private.sched_yield,
};
pub const sigaction = switch (native_os) {
.netbsd => private.__sigaction14,
else => private.sigaction,
};
pub const sigfillset = switch (native_os) {
.netbsd => private.__sigfillset14,
else => private.sigfillset,
};
pub const sigprocmask = switch (native_os) {
.netbsd => private.__sigprocmask14,
else => private.sigprocmask,
};
pub const socket = switch (native_os) {
.netbsd => private.__socket30,
else => private.socket,
};
pub const stat = switch (native_os) {
.macos => switch (native_arch) {
.x86_64 => private.@"stat$INODE64",
@ -1680,7 +1724,6 @@ pub extern "c" fn recvfrom(
pub extern "c" fn recvmsg(sockfd: c.fd_t, msg: *c.msghdr, flags: u32) isize;
pub extern "c" fn kill(pid: c.pid_t, sig: c_int) c_int;
pub extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
pub extern "c" fn setuid(uid: c.uid_t) c_int;
pub extern "c" fn setgid(gid: c.gid_t) c_int;
@ -1878,10 +1921,22 @@ else
};
const private = struct {
extern "c" fn clock_getres(clk_id: c_int, tp: *c.timespec) c_int;
extern "c" fn clock_gettime(clk_id: c_int, tp: *c.timespec) c_int;
extern "c" fn fstat(fd: c.fd_t, buf: *c.Stat) c_int;
extern "c" fn fstatat(dirfd: c.fd_t, path: [*:0]const u8, buf: *c.Stat, flag: u32) c_int;
extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
extern "c" fn readdir(dir: *c.DIR) ?*c.dirent;
extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
extern "c" fn sched_yield() c_int;
extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
extern "c" fn sigfillset(set: ?*c.sigset_t) void;
extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;
extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *c.Stat) c_int;
/// macos modernized symbols.
@ -1894,7 +1949,20 @@ const private = struct {
/// macos modernized symbols.
extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
extern "c" fn __getdirentries64(fd: c.fd_t, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize;
/// netbsd modernized symbols.
extern "c" fn __clock_getres50(clk_id: c_int, tp: *c.timespec) c_int;
extern "c" fn __clock_gettime50(clk_id: c_int, tp: *c.timespec) c_int;
extern "c" fn __fstat50(fd: c.fd_t, buf: *c.Stat) c_int;
extern "c" fn __getrusage50(who: c_int, usage: *c.rusage) c_int;
extern "c" fn __gettimeofday50(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
extern "c" fn __libc_thr_yield() c_int;
extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn __nanosleep50(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
extern "c" fn __sigfillset14(set: ?*c.sigset_t) void;
extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;
extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
extern "c" fn __stat50(path: [*:0]const u8, buf: *c.Stat) c_int;
};

View File

@ -924,9 +924,6 @@ pub extern "c" fn os_unfair_lock_trylock(o: os_unfair_lock_t) bool;
pub extern "c" fn os_unfair_lock_assert_owner(o: os_unfair_lock_t) void;
pub extern "c" fn os_unfair_lock_assert_not_owner(o: os_unfair_lock_t) void;
// XXX: close -> close$NOCANCEL
// XXX: getdirentries -> _getdirentries64
// See: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/sys/_types.h.auto.html
// TODO: audit mode_t/pid_t, should likely be u16/i32
pub const blkcnt_t = i64;

View File

@ -494,6 +494,12 @@ pub const sockaddr = extern struct {
addr: [16]u8,
scope_id: u32,
};
pub const un = extern struct {
len: u8 = @sizeOf(un),
family: sa_family_t = AF.UNIX,
path: [104]u8,
};
};
pub const Kevent = extern struct {

View File

@ -18,47 +18,14 @@ pub extern "c" fn _lwp_self() lwpid_t;
pub extern "c" fn pipe2(fds: *[2]fd_t, flags: std.c.O) c_int;
pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int;
pub const stat = __stat50;
pub extern "c" fn __clock_gettime50(clk_id: c_int, tp: *timespec) c_int;
pub const clock_gettime = __clock_gettime50;
pub extern "c" fn __clock_getres50(clk_id: c_int, tp: *timespec) c_int;
pub const clock_getres = __clock_getres50;
pub extern "c" fn __getdents30(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
pub const getdents = __getdents30;
pub extern "c" fn __sigaltstack14(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
pub const sigaltstack = __sigaltstack14;
pub extern "c" fn __nanosleep50(rqtp: *const timespec, rmtp: ?*timespec) c_int;
pub const nanosleep = __nanosleep50;
pub extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int;
pub const sigaction = __sigaction14;
pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
pub const sigprocmask = __sigaction14;
pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
pub const socket = __socket30;
pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
pub const gettimeofday = __gettimeofday50;
pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int;
pub const getrusage = __getrusage50;
pub extern "c" fn __libc_thr_yield() c_int;
pub const sched_yield = __libc_thr_yield;
pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
pub extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
pub const msync = __msync13;
pub const pthread_spin_t = switch (builtin.cpu.arch) {
.aarch64, .aarch64_be, .aarch64_32 => u8,
.mips, .mipsel, .mips64, .mips64el => u32,

View File

@ -49,7 +49,7 @@ pub const Iterator = switch (builtin.os.tag) {
posix.lseek_SET(self.dir.fd, 0) catch unreachable; // EBADF here likely means that the Dir was not opened with iteration permissions
self.first_iter = false;
}
const rc = posix.system.__getdirentries64(
const rc = posix.system.getdirentries(
self.dir.fd,
&self.buf,
self.buf.len,
@ -161,10 +161,7 @@ pub const Iterator = switch (builtin.os.tag) {
posix.lseek_SET(self.dir.fd, 0) catch unreachable; // EBADF here likely means that the Dir was not opened with iteration permissions
self.first_iter = false;
}
const rc = if (builtin.os.tag == .netbsd)
posix.system.__getdents30(self.dir.fd, &self.buf, self.buf.len)
else
posix.system.getdents(self.dir.fd, &self.buf, self.buf.len);
const rc = posix.system.getdents(self.dir.fd, &self.buf, self.buf.len);
switch (posix.errno(rc)) {
.SUCCESS => {},
.BADF => unreachable, // Dir is invalid or was opened without iteration ability

View File

@ -727,11 +727,11 @@ pub const Inst = struct {
/// Result type is always `void`.
/// Uses the `bin_op` field. LHS is pointer, RHS is element.
atomic_store_unordered,
/// Same as `atomic_store_unordered` but with `AtomicOrder.Monotonic`.
/// Same as `atomic_store_unordered` but with `AtomicOrder.monotonic`.
atomic_store_monotonic,
/// Same as `atomic_store_unordered` but with `AtomicOrder.Release`.
/// Same as `atomic_store_unordered` but with `AtomicOrder.release`.
atomic_store_release,
/// Same as `atomic_store_unordered` but with `AtomicOrder.SeqCst`.
/// Same as `atomic_store_unordered` but with `AtomicOrder.seq_cst`.
atomic_store_seq_cst,
/// Atomically read-modify-write via a pointer.
/// Result type is the element type of the pointer.

View File

@ -647,10 +647,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.atomic_store_unordered => @panic("TODO try self.airAtomicStore(inst, .Unordered)"),
.atomic_store_monotonic => @panic("TODO try self.airAtomicStore(inst, .Monotonic)"),
.atomic_store_release => @panic("TODO try self.airAtomicStore(inst, .Release)"),
.atomic_store_seq_cst => @panic("TODO try self.airAtomicStore(inst, .SeqCst)"),
.atomic_store_unordered => @panic("TODO try self.airAtomicStore(inst, .unordered)"),
.atomic_store_monotonic => @panic("TODO try self.airAtomicStore(inst, .monotonic)"),
.atomic_store_release => @panic("TODO try self.airAtomicStore(inst, .release)"),
.atomic_store_seq_cst => @panic("TODO try self.airAtomicStore(inst, .seq_cst)"),
.struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0),
.struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1),