Incorporate more small improvements

This commit is contained in:
Justine Tunney 2023-07-23 10:57:18 -07:00
parent 1d4eb08fa1
commit f83eb440f7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
20 changed files with 121 additions and 256 deletions

View file

@ -91,6 +91,6 @@ int close(int fd) {
__releasefd(fd);
}
}
STRACE("%s(%d) → %d% m", "close", fd, rc);
STRACE("close(%d) → %d% m", fd, rc);
return rc;
}

View file

@ -34,14 +34,6 @@ forceinline bool __isfdkind(int fd, int kind) {
return 0 <= fd && fd < g_fds.n && g_fds.p[fd].kind == kind;
}
forceinline size_t _clampio(size_t size) {
if (!IsTrustworthy()) {
return MIN(size, 0x7ffff000);
} else {
return size;
}
}
int sys_close_nt(struct Fd *, int) _Hide;
int _check_interrupts(bool, struct Fd *) _Hide;
int sys_openat_metal(int, const char *, int, unsigned);

View file

@ -1,29 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nt/struct/overlapped.h"
#include "libc/str/str.h"
textwindows struct NtOverlapped *_offset2overlap(int64_t handle,
int64_t opt_offset,
struct NtOverlapped *mem) {
if (opt_offset == -1) return NULL;
bzero(mem, sizeof(struct NtOverlapped));
mem->Pointer = (void *)(uintptr_t)opt_offset;
return mem;
}

View file

@ -26,6 +26,7 @@
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/calls/wincrash.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
@ -37,13 +38,10 @@
#include "libc/sysv/errfuns.h"
static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
size_t size, ssize_t offset) {
bool32 ok;
int64_t p;
uint32_t got, avail;
struct NtOverlapped overlap;
size_t size, int64_t offset) {
// our terrible polling mechanism
// try to poll rather than block
uint32_t avail;
if (GetFileType(fd->handle) == kNtFileTypePipe) {
for (;;) {
if (!PeekNamedPipe(fd->handle, 0, 0, 0, &avail, 0)) break;
@ -63,19 +61,28 @@ static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
POLLTRACE("sys_read_nt ready to read");
}
if (offset != -1) {
// windows changes the file pointer even if overlapped is passed
_npassert(SetFilePointerEx(fd->handle, 0, &p, SEEK_CUR));
// perform the read i/o operation
bool32 ok;
uint32_t got;
size = MIN(size, 0x7ffff000);
if (offset == -1) {
// perform simple blocking read
ok = ReadFile(fd->handle, data, size, &got, 0);
} else {
// perform pread()-style read at particular file offset
int64_t position;
// save file pointer which windows clobbers, even for overlapped i/o
if (!SetFilePointerEx(fd->handle, 0, &position, SEEK_CUR)) {
return __winerr(); // fd probably isn't seekable?
}
struct NtOverlapped overlap = {0};
overlap.Pointer = (void *)(uintptr_t)offset;
ok = ReadFile(fd->handle, data, size, 0, &overlap);
if (!ok && GetLastError() == kNtErrorIoPending) ok = true;
if (ok) ok = GetOverlappedResult(fd->handle, &overlap, &got, true);
// restore file pointer which windows clobbers, even on error
_unassert(SetFilePointerEx(fd->handle, position, 0, SEEK_SET));
}
ok = ReadFile(fd->handle, data, _clampio(size), &got,
_offset2overlap(fd->handle, offset, &overlap));
if (offset != -1) {
// windows clobbers file pointer even on error
_npassert(SetFilePointerEx(fd->handle, p, 0, SEEK_SET));
}
if (ok) {
return got;
}
@ -93,7 +100,7 @@ static textwindows ssize_t sys_read_nt_impl(struct Fd *fd, void *data,
}
textwindows ssize_t sys_read_nt(struct Fd *fd, const struct iovec *iov,
size_t iovlen, ssize_t opt_offset) {
size_t iovlen, int64_t opt_offset) {
ssize_t rc;
uint32_t size;
size_t i, total;

View file

@ -13,7 +13,7 @@ int64_t sys_vmsplice(int, const struct iovec *, int64_t, uint32_t) _Hide;
int64_t sys_writev(int32_t, const struct iovec *, int32_t) _Hide;
size_t __iovec_size(const struct iovec *, size_t) _Hide;
ssize_t WritevUninterruptible(int, struct iovec *, int);
ssize_t sys_read_nt(struct Fd *, const struct iovec *, size_t, ssize_t) _Hide;
ssize_t sys_read_nt(struct Fd *, const struct iovec *, size_t, int64_t) _Hide;
ssize_t sys_readv_metal(struct Fd *, const struct iovec *, int) _Hide;
ssize_t sys_readv_nt(struct Fd *, const struct iovec *, int) _Hide;
ssize_t sys_readv_serial(struct Fd *, const struct iovec *, int) _Hide;

View file

@ -1,13 +1,10 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_WINCRASH_INTERNAL_H_
#define COSMOPOLITAN_LIBC_CALLS_WINCRASH_INTERNAL_H_
#include "libc/nt/struct/ntexceptionpointers.h"
#include "libc/nt/struct/overlapped.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
unsigned __wincrash_nt(struct NtExceptionPointers *);
struct NtOverlapped *_offset2overlap(int64_t, int64_t,
struct NtOverlapped *) _Hide;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -16,20 +16,20 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/iovec.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/calls/wincrash.internal.h"
#include "libc/errno.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/internal.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/sig.h"
@ -37,26 +37,31 @@
static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size,
ssize_t offset) {
// perform the write i/o operation
bool32 ok;
int64_t h, p;
uint32_t err, sent;
struct NtOverlapped overlap;
h = g_fds.p[fd].handle;
if (offset != -1) {
// windows changes the file pointer even if overlapped is passed
SetFilePointerEx(h, 0, &p, SEEK_CUR);
uint32_t sent;
int64_t handle;
handle = g_fds.p[fd].handle;
size = MIN(size, 0x7ffff000);
if (offset == -1) {
// perform simple blocking write
ok = WriteFile(handle, data, size, &sent, 0);
} else {
// perform pwrite()-style write at particular file offset
int64_t position;
// save file pointer which windows clobbers, even for overlapped i/o
if (!SetFilePointerEx(handle, 0, &position, SEEK_CUR)) {
return __winerr(); // fd probably isn't seekable?
}
struct NtOverlapped overlap = {0};
overlap.Pointer = (void *)(uintptr_t)offset;
ok = WriteFile(handle, data, size, 0, &overlap);
if (!ok && GetLastError() == kNtErrorIoPending) ok = true;
if (ok) ok = GetOverlappedResult(handle, &overlap, &sent, true);
// restore file pointer which windows clobbers, even on error
_unassert(SetFilePointerEx(handle, position, 0, SEEK_SET));
}
ok = WriteFile(h, data, _clampio(size), &sent,
_offset2overlap(h, offset, &overlap));
if (offset != -1) {
// windows clobbers file pointer even on error
SetFilePointerEx(h, p, 0, SEEK_SET);
}
if (ok) {
return sent;
}
@ -86,8 +91,6 @@ textwindows ssize_t sys_write_nt(int fd, const struct iovec *iov, size_t iovlen,
ssize_t opt_offset) {
ssize_t rc;
size_t i, total;
uint32_t size, wrote;
struct NtOverlapped overlap;
if (opt_offset < -1) return einval();
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
if (iovlen) {