[bun dev] Fix segfaults

This commit is contained in:
Jarred Sumner
2022-04-05 06:38:36 -07:00
parent 70f9294e15
commit daeede28db
4 changed files with 116 additions and 43 deletions

View File

@@ -17,7 +17,7 @@ const C = @import("../../../global.zig").C;
const linux = os.linux;
const Maybe = JSC.Node.Maybe;
pub const system = if (Environment.isLinux) linux else darwin;
pub const system = if (Environment.isLinux) linux else @import("io").darwin;
pub const S = struct {
pub usingnamespace if (Environment.isLinux) linux.S else std.os.S;
};
@@ -88,6 +88,8 @@ pub const Tag = enum(u8) {
getcwd,
chdir,
fcopyfile,
recv,
send,
pub var strings = std.EnumMap(Tag, JSC.C.JSStringRef).initFull(null);
};
@@ -273,6 +275,48 @@ pub fn read(fd: os.fd_t, buf: []u8) Maybe(usize) {
unreachable;
}
pub fn recv(fd: os.fd_t, buf: []u8, flag: u32) Maybe(usize) {
if (comptime Environment.isMac) {
const rc = system.@"recvfrom$NOCANCEL"(fd, buf.ptr, bun.len, flag, null, null);
if (Maybe(usize).errnoSys(rc, .recv)) |err| {
return err;
}
return Maybe(usize){ .result = @intCast(usize, rc) };
} else {
while (true) {
const rc = linux.recvfrom(fd, buf.ptr, bun.len, flag | os.SOCK.CLOEXEC | os.MSG.NOSIGNAL, null, null);
if (Maybe(usize).errnoSys(rc, .recv)) |err| {
if (err.getErrno() == .INTR) continue;
return err;
}
return Maybe(usize){ .result = @intCast(usize, rc) };
}
}
unreachable;
}
pub fn send(fd: os.fd_t, buf: []const u8, flag: u32) Maybe(usize) {
if (comptime Environment.isMac) {
const rc = system.@"sendto$NOCANCEL"(fd, buf.ptr, buf.len, flag, null, 0);
if (Maybe(usize).errnoSys(rc, .send)) |err| {
return err;
}
return Maybe(usize){ .result = @intCast(usize, rc) };
} else {
while (true) {
const rc = linux.sendto(fd, buf.ptr, buf.len, flag | os.SOCK.CLOEXEC | os.MSG.NOSIGNAL, null, 0);
if (Maybe(usize).errnoSys(rc, .send)) |err| {
if (err.getErrno() == .INTR) continue;
return err;
}
return Maybe(usize){ .result = @intCast(usize, rc) };
}
}
unreachable;
}
pub fn readlink(in: [:0]const u8, buf: []u8) Maybe(usize) {
while (true) {
const rc = sys.readlink(in, buf.ptr, buf.len);