Perform some code cleanup

This commit is contained in:
Justine Tunney 2022-10-04 23:32:16 -07:00
parent 174d288e66
commit fe3216e961
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
14 changed files with 803 additions and 312 deletions

View file

@ -41,7 +41,6 @@
* @param size in range [1..0x7ffff000] is reasonable
* @return [1..size] bytes on success, 0 on EOF, or -1 w/ errno; with
* exception of size==0, in which case return zero means no error
* @see write(), pread(), readv()
* @asyncsignalsafe
* @restartable
* @vforksafe
@ -49,7 +48,7 @@
ssize_t read(int fd, void *buf, size_t size) {
ssize_t rc;
if (fd >= 0) {
if (IsAsan() && !__asan_is_valid(buf, size)) {
if ((!buf && size) || (IsAsan() && !__asan_is_valid(buf, size))) {
rc = efault();
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
rc = _weaken(__zipos_read)(
@ -65,7 +64,7 @@ ssize_t read(int fd, void *buf, size_t size) {
rc = sys_readv_nt(g_fds.p + fd, &(struct iovec){buf, size}, 1);
}
} else {
rc = einval();
rc = ebadf();
}
DATATRACE("read(%d, [%#.*hhs%s], %'zu) → %'zd% m", fd, MAX(0, MIN(40, rc)),
buf, rc > 40 ? "..." : "", size, rc);

View file

@ -46,8 +46,8 @@
* @restartable
*/
ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
int i;
ssize_t rc;
if (fd >= 0 && iovlen >= 0) {
if (IsAsan() && !__asan_is_valid_iov(iov, iovlen)) {
rc = efault();
@ -67,9 +67,12 @@ ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
} else {
rc = sys_readv_nt(g_fds.p + fd, iov, iovlen);
}
} else if (fd < 0) {
rc = ebadf();
} else {
rc = einval();
}
#if defined(SYSDEBUG) && _DATATRACE
if (UNLIKELY(__strace > 0)) {
if (rc == -1 && errno == EFAULT) {
@ -81,5 +84,6 @@ ssize_t readv(int fd, const struct iovec *iov, int iovlen) {
}
}
#endif
return rc;
}

View file

@ -18,11 +18,11 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/struct/rlimit.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/sysv/errfuns.h"
@ -43,6 +43,8 @@
* - `RLIMIT_FSIZE` causes `SIGXFSZ` to sent to the process when the
* soft limit on file size is exceeded and the process is destroyed
* when the hard limit is exceeded. It works everywhere but Windows
* and it also causes `EFBIG` to be returned by i/o functions after
* the `SIGXFSZ` signal is delivered or ignored
*
* - `RLIMIT_NPROC` limits the number of simultaneous processes and it
* should work on all platforms except Windows. Please be advised it

View file

@ -17,10 +17,10 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/enum/threadaccess.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thread.h"
@ -48,6 +48,7 @@ static textwindows int sys_tkill_nt(int tid, int sig) {
* @param tid is thread id
* @param sig does nothing on xnu
* @return 0 on success, or -1 w/ errno
* @asyncsignalsafe
*/
int tkill(int tid, int sig) {
int rc;

View file

@ -34,12 +34,27 @@
* This function changes the current file position. For documentation
* on file position behaviors and gotchas, see the lseek() function.
*
* @param fd is something open()'d earlier
* @param fd is open file descriptor
* @param buf is copied from, cf. copy_file_range(), sendfile(), etc.
* @param size in range [1..0x7ffff000] is reasonable
* @return [1..size] bytes on success, or -1 w/ errno; noting zero is
* impossible unless size was passed as zero to do an error check
* @see read(), pwrite(), writev(), SIGPIPE
* @raise EBADF if `fd` is negative or not an open file descriptor
* @raise EBADF if `fd` wasn't opened with `O_WRONLY` or `O_RDWR`
* @raise EPIPE if `fd` is a pipe whose other reader end was closed,
* after the `SIGPIPE` signal was delivered, blocked, or ignored
* @raise EFBIG if `RLIMIT_FSIZE` soft limit was exceeded, after the
* `SIGXFSZ` signal was either delivered, blocked, or ignored
* @raise EFAULT if `size` is nonzero and `buf` points to bad memory
* @raise EPERM if pledge() is in play without the stdio promise
* @raise ENOSPC if device containing `fd` is full
* @raise EIO if low-level i/o error happened
* @raise EINTR if signal was delivered instead
* @raise EAGAIN if `O_NONBLOCK` is in play and write needs to block
* @raise ENOBUFS if kernel lacked internal resources; which FreeBSD
* and XNU say could happen with sockets, and OpenBSD documents it
* as a general possibility; whereas other system don't specify it
* @raise ENXIO is specified only by POSIX and XNU when a request is
* made of a nonexistent device or outside device capabilities
* @asyncsignalsafe
* @restartable
* @vforksafe
@ -47,7 +62,7 @@
ssize_t write(int fd, const void *buf, size_t size) {
ssize_t rc;
if (fd >= 0) {
if (IsAsan() && !__asan_is_valid(buf, size)) {
if ((!buf && size) || (IsAsan() && !__asan_is_valid(buf, size))) {
rc = efault();
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
rc = _weaken(__zipos_write)(
@ -63,7 +78,7 @@ ssize_t write(int fd, const void *buf, size_t size) {
rc = sys_writev_nt(fd, &(struct iovec){buf, size}, 1);
}
} else {
rc = einval();
rc = ebadf();
}
DATATRACE("write(%d, %#.*hhs%s, %'zu) → %'zd% m", fd, MAX(0, MIN(40, rc)),
buf, rc > 40 ? "..." : "", size, rc);

View file

@ -50,7 +50,6 @@
* @restartable
*/
ssize_t writev(int fd, const struct iovec *iov, int iovlen) {
int i;
ssize_t rc;
if (fd >= 0 && iovlen >= 0) {
@ -72,6 +71,8 @@ ssize_t writev(int fd, const struct iovec *iov, int iovlen) {
} else {
rc = sys_writev_nt(fd, iov, iovlen);
}
} else if (fd < 0) {
rc = ebadf();
} else {
rc = einval();
}

View file

@ -488,13 +488,11 @@ static int CloneLinux(int (*func)(void *arg, int tid), char *stk, size_t stksz,
// COSMOPOLITAN
/**
* Creates thread without malloc being linked, e.g.
* Creates thread without malloc being linked.
*
* int worker(void *arg) {
* return 0;
* }
* If you use clone() you're on you're own, e.g.
*
* // NOTE: See _mktls() for _Thread_local support.
* int worker(void *arg) { return 0; }
* struct CosmoTib tib = {.tib_self = &tib, .tib_tid = -1};
* char *stk = _mapstack();
* tid = clone(worker, stk, GetStackSize() - 16,

View file

@ -100,4 +100,7 @@ void _wait0(const atomic_int *ctid) {
_wait0_poll(&ts);
}
}
if (IsOpenbsd()) {
sched_yield(); // TODO(jart): whhhy?
}
}