Fix some bugs

This commit is contained in:
Justine Tunney 2022-05-11 02:50:03 -07:00
parent 363d2ec436
commit 2aebda7718
7 changed files with 63 additions and 60 deletions

View file

@ -112,6 +112,7 @@ char *GetProgramExecutableName(void) {
program_executable_name, program_executable_name,
program_executable_name + sizeof(program_executable_name)); program_executable_name + sizeof(program_executable_name));
errno = e; errno = e;
once = true;
} }
return program_executable_name; return program_executable_name;
} }

View file

@ -16,11 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/dce.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
.init.start 300,_init_wincrash .init.start 300,_init_wincrash
#if !IsTiny()
mov __wincrashearly(%rip),%rcx mov __wincrashearly(%rip),%rcx
ntcall __imp_RemoveVectoredExceptionHandler ntcall __imp_RemoveVectoredExceptionHandler
#endif
pushpop 1,%rcx pushpop 1,%rcx
ezlea __wincrash_nt,dx ezlea __wincrash_nt,dx
ntcall __imp_AddVectoredExceptionHandler ntcall __imp_AddVectoredExceptionHandler

View file

@ -227,7 +227,9 @@ textwindows void WinMainForked(void) {
// restore the crash reporting stuff // restore the crash reporting stuff
if (weaken(__wincrash_nt)) { if (weaken(__wincrash_nt)) {
RemoveVectoredExceptionHandler(__wincrashearly); if (!IsTiny()) {
RemoveVectoredExceptionHandler(__wincrashearly);
}
AddVectoredExceptionHandler(1, (void *)weaken(__wincrash_nt)); AddVectoredExceptionHandler(1, (void *)weaken(__wincrash_nt));
} }
if (weaken(__onntconsoleevent_nt)) { if (weaken(__onntconsoleevent_nt)) {

View file

@ -268,7 +268,9 @@ __msabi textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
os = WINDOWS; /* madness https://news.ycombinator.com/item?id=21019722 */ os = WINDOWS; /* madness https://news.ycombinator.com/item?id=21019722 */
ts = rdtsc(); ts = rdtsc();
__pid = GetCurrentProcessId(); __pid = GetCurrentProcessId();
#if !IsTiny()
__wincrashearly = AddVectoredExceptionHandler(1, (void *)OnEarlyWinCrash); __wincrashearly = AddVectoredExceptionHandler(1, (void *)OnEarlyWinCrash);
#endif
cmdline = GetCommandLine(); cmdline = GetCommandLine();
#ifdef SYSDEBUG #ifdef SYSDEBUG
/* sloppy flag-only check for early initialization */ /* sloppy flag-only check for early initialization */

View file

@ -75,8 +75,9 @@ TEST(rand64, testThreadSafety_doesntProduceIdenticalValues) {
sigset_t ss, oldss; sigset_t ss, oldss;
int i, j, rc, ws, tid[THREADS]; int i, j, rc, ws, tid[THREADS];
if (IsXnu()) return; if (IsXnu()) return;
if (IsNetbsd()) return; // still flaky :'( if (IsNetbsd()) return; // still flaky :'(
if (IsOpenbsd()) return; // still flaky :'( if (IsOpenbsd()) return; // still flaky :'(
if (IsTiny() && IsWindows()) return; // todo(jart): wut
struct sigaction oldsa; struct sigaction oldsa;
struct sigaction sa = {.sa_handler = OnChld, .sa_flags = SA_RESTART}; struct sigaction sa = {.sa_handler = OnChld, .sa_flags = SA_RESTART};
EXPECT_NE(-1, sigaction(SIGCHLD, &sa, &oldsa)); EXPECT_NE(-1, sigaction(SIGCHLD, &sa, &oldsa));

View file

@ -2289,6 +2289,11 @@ UNIX MODULE
- `SOCK_RDM` - `SOCK_RDM`
- `SOCK_SEQPACKET` - `SOCK_SEQPACKET`
You may bitwise or any of the following into `type`:
- `SOCK_CLOEXEC`
- `SOCK_NONBLOCK`
`protocol` defaults to `IPPROTO_TCP` and can be: `protocol` defaults to `IPPROTO_TCP` and can be:
- `IPPROTO_IP` - `IPPROTO_IP`
@ -2297,16 +2302,26 @@ UNIX MODULE
- `IPPROTO_UDP` - `IPPROTO_UDP`
- `IPPROTO_RAW` - `IPPROTO_RAW`
`SOCK_CLOEXEC` may be bitwise or'd into `type`.
unix.socketpair([family:int[, type:int[, protocol:int]]]) unix.socketpair([family:int[, type:int[, protocol:int]]])
├─→ fd1:int, fd2:int ├─→ fd1:int, fd2:int
└─→ nil, unix.Errno └─→ nil, unix.Errno
`SOCK_CLOEXEC` may be or'd into type Creates bidirectional pipe.
`family` defaults to `AF_INET`
`type` defaults to `SOCK_STREAM` `family` defaults to `AF_UNIX`.
`protocol` defaults to `IPPROTO_TCP`
`type` defaults to `SOCK_STREAM` and can be:
- `SOCK_STREAM`
- `SOCK_DGRAM`
- `SOCK_SEQPACKET`
You may bitwise or any of the following into `type`:
- `SOCK_CLOEXEC`
- `SOCK_NONBLOCK`
`protocol` defaults to `0`.
unix.bind(fd:int[, ip:uint32, port:uint16]) unix.bind(fd:int[, ip:uint32, port:uint16])
├─→ true ├─→ true

View file

@ -436,11 +436,13 @@ static int LuaUnixChmod(lua_State *L) {
static int LuaUnixReadlink(lua_State *L) { static int LuaUnixReadlink(lua_State *L) {
char *buf; char *buf;
ssize_t rc; ssize_t rc;
int olderr = errno; const char *path;
int dirfd, olderr = errno;
size_t got, bufsiz = 8192; size_t got, bufsiz = 8192;
path = luaL_checkstring(L, 1);
dirfd = luaL_optinteger(L, 2, AT_FDCWD);
if ((buf = LuaUnixAllocRaw(L, bufsiz))) { if ((buf = LuaUnixAllocRaw(L, bufsiz))) {
if ((rc = readlinkat(luaL_optinteger(L, 2, AT_FDCWD), if ((rc = readlinkat(dirfd, path, buf, bufsiz)) != -1) {
luaL_checkstring(L, 1), buf, bufsiz)) != -1) {
got = rc; got = rc;
if (got < bufsiz) { if (got < bufsiz) {
lua_pushlstring(L, buf, got); lua_pushlstring(L, buf, got);
@ -459,9 +461,8 @@ static int LuaUnixReadlink(lua_State *L) {
// ├─→ path:str // ├─→ path:str
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixGetcwd(lua_State *L) { static int LuaUnixGetcwd(lua_State *L) {
int olderr;
char *path; char *path;
olderr = errno; int olderr = errno;
if ((path = getcwd(0, 0))) { if ((path = getcwd(0, 0))) {
lua_pushstring(L, path); lua_pushstring(L, path);
free(path); free(path);
@ -944,7 +945,6 @@ static int LuaUnixWrite(lua_State *L) {
fd = luaL_checkinteger(L, 1); fd = luaL_checkinteger(L, 1);
data = luaL_checklstring(L, 2, &size); data = luaL_checklstring(L, 2, &size);
offset = luaL_optinteger(L, 3, -1); offset = luaL_optinteger(L, 3, -1);
size = MIN(size, 0x7ffff000);
if (offset == -1) { if (offset == -1) {
rc = write(fd, data, size); rc = write(fd, data, size);
} else { } else {
@ -1147,9 +1147,9 @@ static int LuaUnixSocket(lua_State *L) {
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixSocketpair(lua_State *L) { static int LuaUnixSocketpair(lua_State *L) {
int sv[2], olderr = errno; int sv[2], olderr = errno;
if (!socketpair(luaL_optinteger(L, 1, AF_INET), if (!socketpair(luaL_optinteger(L, 1, AF_UNIX),
luaL_optinteger(L, 2, SOCK_STREAM), luaL_optinteger(L, 2, SOCK_STREAM), luaL_optinteger(L, 3, 0),
luaL_optinteger(L, 3, IPPROTO_TCP), sv)) { sv)) {
lua_pushinteger(L, sv[0]); lua_pushinteger(L, sv[0]);
lua_pushinteger(L, sv[1]); lua_pushinteger(L, sv[1]);
return 2; return 2;
@ -1338,8 +1338,8 @@ static int LuaUnixAccept(lua_State *L) {
static int LuaUnixPoll(lua_State *L) { static int LuaUnixPoll(lua_State *L) {
size_t nfds; size_t nfds;
struct pollfd *fds, *fds2; struct pollfd *fds, *fds2;
int i, fd, olderr, events, timeoutms; int i, fd, events, timeoutms, olderr = errno;
olderr = errno; timeoutms = luaL_optinteger(L, 2, -1);
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnil(L); lua_pushnil(L);
for (fds = 0, nfds = 0; lua_next(L, 1);) { for (fds = 0, nfds = 0; lua_next(L, 1);) {
@ -1358,7 +1358,6 @@ static int LuaUnixPoll(lua_State *L) {
} }
lua_pop(L, 1); lua_pop(L, 1);
} }
timeoutms = luaL_optinteger(L, 2, -1);
olderr = errno; olderr = errno;
if ((events = poll(fds, nfds, timeoutms)) != -1) { if ((events = poll(fds, nfds, timeoutms)) != -1) {
lua_createtable(L, events, 0); lua_createtable(L, events, 0);
@ -1386,13 +1385,12 @@ static int LuaUnixRecvfrom(lua_State *L) {
uint32_t addrsize; uint32_t addrsize;
lua_Integer bufsiz; lua_Integer bufsiz;
struct sockaddr_in sa; struct sockaddr_in sa;
int fd, flags, olderr; int fd, flags, olderr = errno;
olderr = errno;
addrsize = sizeof(sa); addrsize = sizeof(sa);
fd = luaL_checkinteger(L, 1); fd = luaL_checkinteger(L, 1);
bufsiz = luaL_optinteger(L, 2, 1500); bufsiz = luaL_optinteger(L, 2, 1500);
flags = luaL_optinteger(L, 3, 0);
bufsiz = MIN(bufsiz, 0x7ffff000); bufsiz = MIN(bufsiz, 0x7ffff000);
flags = luaL_optinteger(L, 3, 0);
if ((buf = LuaUnixAllocRaw(L, bufsiz))) { if ((buf = LuaUnixAllocRaw(L, bufsiz))) {
rc = recvfrom(fd, buf, bufsiz, flags, &sa, &addrsize); rc = recvfrom(fd, buf, bufsiz, flags, &sa, &addrsize);
if (rc != -1) { if (rc != -1) {
@ -1419,12 +1417,11 @@ static int LuaUnixRecv(lua_State *L) {
size_t got; size_t got;
ssize_t rc; ssize_t rc;
lua_Integer bufsiz; lua_Integer bufsiz;
int fd, flags, olderr, pushed; int fd, flags, pushed, olderr = errno;
olderr = errno;
fd = luaL_checkinteger(L, 1); fd = luaL_checkinteger(L, 1);
bufsiz = luaL_optinteger(L, 2, 1500); bufsiz = luaL_optinteger(L, 2, 1500);
flags = luaL_optinteger(L, 3, 0);
bufsiz = MIN(bufsiz, 0x7ffff000); bufsiz = MIN(bufsiz, 0x7ffff000);
flags = luaL_optinteger(L, 3, 0);
if ((buf = LuaUnixAllocRaw(L, bufsiz))) { if ((buf = LuaUnixAllocRaw(L, bufsiz))) {
rc = recv(fd, buf, bufsiz, flags); rc = recv(fd, buf, bufsiz, flags);
if (rc != -1) { if (rc != -1) {
@ -1448,13 +1445,10 @@ static int LuaUnixSend(lua_State *L) {
char *data; char *data;
ssize_t rc; ssize_t rc;
size_t sent, size; size_t sent, size;
lua_Integer bufsiz; int fd, flags, olderr = errno;
int fd, flags, olderr;
olderr = errno;
fd = luaL_checkinteger(L, 1); fd = luaL_checkinteger(L, 1);
data = luaL_checklstring(L, 2, &size); data = luaL_checklstring(L, 2, &size);
size = MIN(size, 0x7ffff000); flags = luaL_optinteger(L, 3, 0);
flags = luaL_optinteger(L, 5, 0);
return SysretInteger(L, "send", olderr, send(fd, data, size, flags)); return SysretInteger(L, "send", olderr, send(fd, data, size, flags));
} }
@ -1464,14 +1458,11 @@ static int LuaUnixSend(lua_State *L) {
static int LuaUnixSendto(lua_State *L) { static int LuaUnixSendto(lua_State *L) {
char *data; char *data;
size_t sent, size; size_t sent, size;
lua_Integer bufsiz;
int fd, flags, olderr;
struct sockaddr_in sa; struct sockaddr_in sa;
olderr = errno; int fd, flags, olderr = errno;
bzero(&sa, sizeof(sa));
fd = luaL_checkinteger(L, 1); fd = luaL_checkinteger(L, 1);
data = luaL_checklstring(L, 2, &size); data = luaL_checklstring(L, 2, &size);
size = MIN(size, 0x7ffff000); bzero(&sa, sizeof(sa));
sa.sin_addr.s_addr = htonl(luaL_checkinteger(L, 3)); sa.sin_addr.s_addr = htonl(luaL_checkinteger(L, 3));
sa.sin_port = htons(luaL_checkinteger(L, 4)); sa.sin_port = htons(luaL_checkinteger(L, 4));
flags = luaL_optinteger(L, 5, 0); flags = luaL_optinteger(L, 5, 0);
@ -1493,12 +1484,10 @@ static int LuaUnixShutdown(lua_State *L) {
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixSigprocmask(lua_State *L) { static int LuaUnixSigprocmask(lua_State *L) {
uint64_t imask; uint64_t imask;
int i, n, how, olderr; int olderr = errno;
struct sigset *newmask, oldmask; struct sigset oldmask;
olderr = errno; if (!sigprocmask(luaL_checkinteger(L, 1),
how = luaL_checkinteger(L, 1); luaL_checkudata(L, 2, "unix.Sigset"), &oldmask)) {
newmask = luaL_checkudata(L, 2, "unix.Sigset");
if (!sigprocmask(how, newmask, &oldmask)) {
LuaPushSigset(L, oldmask); LuaPushSigset(L, oldmask);
return 1; return 1;
} else { } else {
@ -1533,10 +1522,8 @@ static void LuaUnixOnSignal(int sig, siginfo_t *si, ucontext_t *ctx) {
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixSigaction(lua_State *L) { static int LuaUnixSigaction(lua_State *L) {
struct sigset *mask; struct sigset *mask;
int i, n, sig, olderr; int i, n, sig, olderr = errno;
struct sigaction sa, oldsa, *saptr; struct sigaction sa, oldsa, *saptr = &sa;
saptr = &sa;
olderr = errno;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sig = luaL_checkinteger(L, 1); sig = luaL_checkinteger(L, 1);
if (!(1 <= sig && sig <= NSIG)) { if (!(1 <= sig && sig <= NSIG)) {
@ -1611,13 +1598,7 @@ static int LuaUnixSigaction(lua_State *L) {
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixSigsuspend(lua_State *L) { static int LuaUnixSigsuspend(lua_State *L) {
int olderr = errno; int olderr = errno;
struct sigset *set; sigsuspend(!lua_isnoneornil(L, 1) ? luaL_checkudata(L, 1, "unix.Sigset") : 0);
if (!lua_isnoneornil(L, 1)) {
set = luaL_checkudata(L, 1, "unix.Sigset");
} else {
set = 0;
}
sigsuspend(set);
return SysretErrno(L, "sigsuspend", olderr); return SysretErrno(L, "sigsuspend", olderr);
} }
@ -1625,9 +1606,8 @@ static int LuaUnixSigsuspend(lua_State *L) {
// ├─→ intervalsec:int, intervalns:int, valuesec:int, valuens:int // ├─→ intervalsec:int, intervalns:int, valuesec:int, valuens:int
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixSetitimer(lua_State *L) { static int LuaUnixSetitimer(lua_State *L) {
int which, olderr; int which, olderr = errno;
struct itimerval it, oldit, *itptr; struct itimerval it, oldit, *itptr;
olderr = errno;
which = luaL_checkinteger(L, 1); which = luaL_checkinteger(L, 1);
if (!lua_isnoneornil(L, 2)) { if (!lua_isnoneornil(L, 2)) {
itptr = &it; itptr = &it;
@ -2266,7 +2246,7 @@ static int LuaUnixDirClose(lua_State *L) {
int rc, olderr; int rc, olderr;
dirp = GetUnixDirSelf(L); dirp = GetUnixDirSelf(L);
if (*dirp) { if (*dirp) {
olderr = 0; olderr = errno;
rc = closedir(*dirp); rc = closedir(*dirp);
*dirp = 0; *dirp = 0;
return SysretBool(L, "closedir", olderr, rc); return SysretBool(L, "closedir", olderr, rc);
@ -2299,8 +2279,7 @@ static int LuaUnixDirRead(lua_State *L) {
// ├─→ fd:int // ├─→ fd:int
// └─→ nil, unix.Errno // └─→ nil, unix.Errno
static int LuaUnixDirFd(lua_State *L) { static int LuaUnixDirFd(lua_State *L) {
int fd, olderr; int fd, olderr = errno;
olderr = errno;
fd = dirfd(GetDirOrDie(L)); fd = dirfd(GetDirOrDie(L));
if (fd != -1) { if (fd != -1) {
lua_pushinteger(L, fd); lua_pushinteger(L, fd);
@ -2547,7 +2526,6 @@ int LuaUnix(lua_State *L) {
// wait() options // wait() options
LuaSetIntField(L, "WNOHANG", WNOHANG); LuaSetIntField(L, "WNOHANG", WNOHANG);
LuaSetIntField(L, "WNOHANG", WNOHANG);
// socket() family // socket() family
LuaSetIntField(L, "AF_UNSPEC", AF_UNSPEC); LuaSetIntField(L, "AF_UNSPEC", AF_UNSPEC);
@ -2561,6 +2539,7 @@ int LuaUnix(lua_State *L) {
LuaSetIntField(L, "SOCK_RDM", SOCK_RDM); LuaSetIntField(L, "SOCK_RDM", SOCK_RDM);
LuaSetIntField(L, "SOCK_SEQPACKET", SOCK_SEQPACKET); LuaSetIntField(L, "SOCK_SEQPACKET", SOCK_SEQPACKET);
LuaSetIntField(L, "SOCK_CLOEXEC", SOCK_CLOEXEC); LuaSetIntField(L, "SOCK_CLOEXEC", SOCK_CLOEXEC);
LuaSetIntField(L, "SOCK_NONBLOCK", SOCK_NONBLOCK);
// socket() protocol // socket() protocol
LuaSetIntField(L, "IPPROTO_IP", IPPROTO_IP); LuaSetIntField(L, "IPPROTO_IP", IPPROTO_IP);