Improve cancellations, randomness, and time

- Exhaustively document cancellation points
- Rename SIGCANCEL to SIGTHR just like BSDs
- Further improve POSIX thread cancellations
- Ensure asynchronous cancellations work correctly
- Elevate the quality of getrandom() and getentropy()
- Make futexes cancel correctly on OpenBSD 6.x and 7.x
- Add reboot.com and shutdown.com to examples directory
- Remove underscore prefix from awesome timespec_*() APIs
- Create assertions that help verify our cancellation points
- Remove bad timespec APIs (cmp generalizes eq/ne/gt/gte/lt/lte)
This commit is contained in:
Justine Tunney 2022-11-05 19:49:41 -07:00
parent 0d7c265392
commit 3f0bcdc3ef
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
173 changed files with 1599 additions and 782 deletions

View file

@ -309,10 +309,10 @@ static int Usleep(void) {
struct timespec t;
if (n > 1) {
f = 0;
t = _timespec_frommicros(atoi(args[1]));
t = timespec_frommicros(atoi(args[1]));
} else {
f = TIMER_ABSTIME;
t = _timespec_max;
t = timespec_max;
}
return clock_nanosleep(0, f, &t, 0);
}

View file

@ -148,8 +148,10 @@ cosmo: push %rbp
or $-1,%r8
xor %r9d,%r9d
push %rsi
push %rsi
call mmap
pop %r8
pop %r8
pop %rsi
pop %rdi
cmp $-1,%rax

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/log/libfatal.internal.h"
#include "libc/runtime/ezmap.internal.h"
@ -26,6 +26,8 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
// TODO(jart): DELETE
hidden int MapFileRead(const char *filename, struct MappedFile *mf) {
mf->addr = MAP_FAILED;
if ((mf->fd = open(filename, O_RDONLY)) != -1 &&

View file

@ -1,41 +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 2021 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/runtime/fenv.h"
/* TODO: implement these functions */
int feclearexcept(int mask) {
return 0;
}
int fegetenv(fenv_t *envp) {
return 0;
}
int feraiseexcept(int mask) {
return 0;
}
int fetestexcept(int mask) {
return 0;
}
int fesetenv(const fenv_t *envp) {
return 0;
}

View file

@ -16,7 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bits.h"
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/elf/def.h"
@ -24,6 +24,7 @@
#include "libc/elf/struct/ehdr.h"
#include "libc/elf/struct/phdr.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
@ -38,6 +39,7 @@ bool IsDynamicExecutable(const char *prog) {
Elf64_Phdr *p;
struct stat st;
int i, fd, err;
BLOCK_CANCELLATIONS;
fd = -1;
err = errno;
e = MAP_FAILED;
@ -74,5 +76,6 @@ Finish:
if (e != MAP_FAILED) munmap(e, st.st_size);
if (fd != -1) close(fd);
errno = err;
ALLOW_CANCELLATIONS;
return res;
}

View file

@ -18,6 +18,7 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/cp.internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
@ -34,16 +35,22 @@
* @param addr needs to be 4096-byte page aligned
* @param flags needs MS_ASYNC or MS_SYNC and can have MS_INVALIDATE
* @return 0 on success or -1 w/ errno
* @raise ECANCELED if thread was cancelled in masked mode
* @raise EINTR if we needed to block and a signal was delivered instead
* @cancellationpoint
*/
int msync(void *addr, size_t size, int flags) {
int rc;
BEGIN_CANCELLATION_POINT;
_unassert(((flags & MS_SYNC) ^ (flags & MS_ASYNC)) || !(MS_SYNC && MS_ASYNC));
if (!IsWindows()) {
rc = sys_msync(addr, size, flags);
} else {
rc = sys_msync_nt(addr, size, flags);
}
END_CANCELLATION_POINT;
STRACE("msync(%p, %'zu, %#x) → %d% m", addr, size, flags, rc);
return rc;
}

View file

@ -17,8 +17,8 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/intrin/strace.internal.h"
#include "libc/dce.h"
#include "libc/elf/def.h"
#include "libc/elf/scalar.h"
@ -27,6 +27,7 @@
#include "libc/elf/struct/sym.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/strace.internal.h"
#include "libc/limits.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
@ -100,12 +101,7 @@ static void GetImageRange(Elf64_Ehdr *elf, intptr_t *x, intptr_t *y) {
if (y) *y = end;
}
/**
* Maps debuggable binary into memory and indexes symbol addresses.
*
* @return object freeable with CloseSymbolTable(), or NULL w/ errno
*/
struct SymbolTable *OpenSymbolTable(const char *filename) {
static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
int fd;
void *map;
long *stp;
@ -200,3 +196,16 @@ SystemError:
close(fd);
return 0;
}
/**
* Maps debuggable binary into memory and indexes symbol addresses.
*
* @return object freeable with CloseSymbolTable(), or NULL w/ errno
*/
struct SymbolTable *OpenSymbolTable(const char *filename) {
struct SymbolTable *st;
BLOCK_CANCELLATIONS;
st = OpenSymbolTableImpl(filename);
ALLOW_CANCELLATIONS;
return st;
}