Make improvements

This change progresses our AARCH64 support:

- The AARCH64 build and tests are now passing
- Add 128-bit floating-point support to printf()
- Fix clone() so it initializes cosmo's x28 TLS register
- Fix TLS memory layout issue with aarch64 _Alignas vars
- Revamp microbenchmarking tools so they work on aarch64
- Make some subtle improvements to aarch64 crash reporting
- Make kisdangerous() memory checks more accurate on aarch64
- Remove sys_open() since it's not available on Linux AARCH64

This change makes general improvements to Cosmo and Redbean:

- Introduce GetHostIsa() function in Redbean
- You can now feature check using pledge(0, 0)
- You can now feature check using unveil("",0)
- Refactor some more x86-specific asm comments
- Refactor and write docs for some libm functions
- Make the mmap() API behave more similar to Linux
- Fix WIFSIGNALED() which wrongly returned true for zero
- Rename some obscure cosmo keywords from noFOO to dontFOO
This commit is contained in:
Justine Tunney 2023-06-03 08:12:13 -07:00
parent 5655c9a4e7
commit 8f522cb702
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
116 changed files with 1194 additions and 1025 deletions

View file

@ -25,6 +25,7 @@
#include "libc/intrin/promises.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/sig.h"
@ -32,10 +33,11 @@
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#ifdef __x86_64__
void SetUp(void) {
if (!__is_linux_2_6_23() && !IsOpenbsd()) exit(0);
if (pledge(0, 0) == -1) {
fprintf(stderr, "warning: pledge() not supported on this system\n");
exit(0);
}
}
TEST(pledge, testSoftError) {
@ -116,5 +118,3 @@ TEST(pledge, testEmptyPledge_doesntUseTrapping) {
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
TERMS(IsOpenbsd() ? SIGABRT : SIGSYS);
}
#endif /* __x86_64__ */

View file

@ -62,9 +62,6 @@
#include "libc/time/time.h"
#include "libc/x/x.h"
// TODO(jart): Get pledge truly working on AARCH64
#ifdef __x86_64__
char testlib_enable_tmp_setup_teardown;
void OnSig(int sig) {
@ -75,7 +72,10 @@ int sys_memfd_secret(unsigned int); // our ENOSYS threshold
void SetUp(void) {
__enable_threads();
if (!__is_linux_2_6_23() && !IsOpenbsd()) exit(0);
if (pledge(0, 0) == -1) {
fprintf(stderr, "warning: pledge() not supported on this system\n");
exit(0);
}
testlib_extract("/zip/life.elf", "life.elf", 0755);
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
__pledge_mode = PLEDGE_PENALTY_RETURN_EPERM;
@ -659,5 +659,3 @@ BENCH(pledge, bench) {
}
wait(0);
}
#endif /* __x86_64__ */

View file

@ -45,28 +45,6 @@ TEST(read, eof) {
////////////////////////////////////////////////////////////////////////////////
static long Read(long fd, void *buf, unsigned long size) {
#ifdef __x86_64__
long ax, di, si, dx;
asm volatile("syscall"
: "=a"(ax), "=D"(di), "=S"(si), "=d"(dx)
: "0"(__NR_read), "1"(fd), "2"(buf), "3"(size)
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
return ax;
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)fd;
register long r1 asm("x1") = (long)buf;
register long r2 asm("x2") = (long)size;
register long r8 asm("x8") = (long)__NR_read;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r1), "r"(r2), "r"(r8)
: "memory");
return res_x0;
#endif
}
BENCH(read, bench) {
char buf[16];
ASSERT_SYS(0, 3, open("/dev/zero", O_RDONLY));
@ -80,7 +58,5 @@ BENCH(read, bench) {
preadv(3, (struct iovec[]){{buf, 1}, {buf + 1, 4}}, 2, 0));
EZBENCH2("sys_read", donothing, sys_read(3, buf, 5));
EZBENCH2("sys_readv", donothing, sys_readv(3, &(struct iovec){buf, 5}, 1));
EZBENCH2("Read", donothing, Read(3, buf, 5));
EZBENCH2("Read", donothing, Read(3, buf, 5));
ASSERT_SYS(0, 0, close(3));
}

View file

@ -118,25 +118,20 @@ o/$(MODE)/test/libc/calls/ioctl_siocgifconf_test.com.runs: \
o/$(MODE)/test/libc/calls/poll_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc inet
o/$(MODE)/test/libc/calls/fcntl_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc flock
o/$(MODE)/test/libc/calls/lock_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc flock
o/$(MODE)/test/libc/calls/lock2_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc flock
o/$(MODE)/test/libc/calls/fcntl_test.com.runs \
o/$(MODE)/test/libc/calls/lock_test.com.runs \
o/$(MODE)/test/libc/calls/lock2_test.com.runs \
o/$(MODE)/test/libc/calls/lock_ofd_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc flock
o/$(MODE)/test/libc/calls/unveil_test.com.runs \
o/$(MODE)/test/libc/calls/openbsd_test.com.runs: \
private .PLEDGE = stdio rpath wpath cpath fattr proc unveil
o/$(MODE)/test/libc/calls/fexecve_test.com.runs: \
private .UNSANDBOXED = 1 # for memfd_create()
o/$(MODE)/test/libc/calls/execve_test.com.runs: \
o/$(MODE)/test/libc/calls/execve_test.com.runs: \
private .UNSANDBOXED = 1 # for memfd_create()
o/$(MODE)/test/libc/calls/read_test.com.runs: \

View file

@ -51,16 +51,21 @@ char testlib_enable_tmp_setup_teardown;
struct stat st;
static bool SupportsLandlock(void) {
int e = errno;
bool r = landlock_create_ruleset(0, 0, LANDLOCK_CREATE_RULESET_VERSION) >= 0;
errno = e;
return r;
bool HasUnveilSupport(void) {
return unveil("", 0) >= 0;
}
bool UnveilCanSecureTruncate(void) {
int abi = unveil("", 0);
return abi == 0 || abi >= 3;
}
void SetUpOnce(void) {
__enable_threads();
if (!(IsLinux() && SupportsLandlock()) && !IsOpenbsd()) exit(0);
if (!HasUnveilSupport()) {
fprintf(stderr, "warning: unveil() not supported on this system: %m\n");
exit(0);
}
}
void SetUp(void) {
@ -68,10 +73,6 @@ void SetUp(void) {
ASSERT_SYS(0, 0, stat("/zip/life.elf", &st));
}
bool HasTruncateSupport(void) {
return IsOpenbsd() || landlock_create_ruleset(0, 0, LANDLOCK_CREATE_RULESET_VERSION) >= 3;
}
TEST(unveil, api_differences) {
SPAWN(fork);
ASSERT_SYS(0, 0, mkdir("foo", 0755));
@ -249,7 +250,7 @@ TEST(unveil, truncate_isForbiddenBySeccomp) {
ASSERT_SYS(0, 0, xbarf("garden/secret.txt", "hello", 5));
ASSERT_SYS(0, 0, unveil("jail", "rw"));
ASSERT_SYS(0, 0, unveil(0, 0));
ASSERT_SYS(!HasTruncateSupport() ? EPERM : EACCES_OR_ENOENT, -1,
ASSERT_SYS(!UnveilCanSecureTruncate() ? EPERM : EACCES_OR_ENOENT, -1,
truncate("garden/secret.txt", 0));
if (IsLinux()) {
ASSERT_SYS(0, 0, stat("garden/secret.txt", &st));

View file

@ -96,30 +96,6 @@ TEST(write, rlimitFsizeExceeded_raisesEfbig) {
EXITS(0);
}
static long Write(long fd, const void *data, unsigned long size) {
#ifdef __x86_64__
long ax, di, si, dx;
asm volatile("syscall"
: "=a"(ax), "=D"(di), "=S"(si), "=d"(dx)
: "0"(__NR_write), "1"(fd), "2"(data), "3"(size)
: "rcx", "r8", "r9", "r10", "r11", "memory", "cc");
return ax;
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)fd;
register long r1 asm("x1") = (long)data;
register long r2 asm("x2") = (long)size;
register long r8 asm("x8") = (long)__NR_write;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)
: "r"(r0), "r"(r1), "r"(r2), "r"(r8)
: "memory");
return res_x0;
#else
#error "unsupported architecture"
#endif
}
BENCH(write, bench) {
ASSERT_SYS(0, 3, open("/dev/null", O_WRONLY));
EZBENCH2("write", donothing, write(3, "hello", 5));
@ -127,7 +103,5 @@ BENCH(write, bench) {
EZBENCH2("sys_write", donothing, sys_write(3, "hello", 5));
EZBENCH2("sys_writev", donothing,
sys_writev(3, &(struct iovec){"hello", 5}, 1));
EZBENCH2("Write", donothing, Write(3, "hello", 5));
EZBENCH2("Write", donothing, Write(3, "hello", 5));
ASSERT_SYS(0, 0, close(3));
}

View file

@ -26,6 +26,7 @@
#include "libc/testlib/testlib.h"
TEST(__dos2errno, test) {
#ifdef __x86__
EXPECT_EQ(0, __dos2errno(0));
EXPECT_EQ(EACCES, __dos2errno(kNtErrorSectorNotFound));
EXPECT_EQ(EADDRNOTAVAIL, __dos2errno(kNtErrorInvalidNetname));
@ -33,4 +34,5 @@ TEST(__dos2errno, test) {
if (IsWindows()) {
EXPECT_EQ(ENOLCK, __dos2errno(kNtErrorNotLocked));
}
#endif
}

View file

@ -221,14 +221,13 @@ TEST(ksnprintf, testSymbols) {
}
}
#ifdef __x86_64__
TEST(ksnprintf, fuzzTheUnbreakable) {
int e;
size_t i;
uint64_t x;
char *f, b[32];
_Alignas(FRAMESIZE) static const char weasel[FRAMESIZE];
asm("mov\t%1,%0" : "=r"(f) : "g"(weasel));
f = VEIL("r", weasel);
EXPECT_SYS(0, 0, mprotect(f, FRAMESIZE, PROT_READ | PROT_WRITE));
strcpy(f, "hello %s\n");
EXPECT_EQ(12, ksnprintf(b, sizeof(b), f, "world"));
@ -243,7 +242,6 @@ TEST(ksnprintf, fuzzTheUnbreakable) {
}
EXPECT_SYS(0, 0, mprotect(f, FRAMESIZE, PROT_READ));
}
#endif /* __x86_64__ */
TEST(kprintf, testFailure_wontClobberErrnoAndBypassesSystemCallSupport) {
int n;

View file

@ -220,7 +220,7 @@ int main(int argc, char *argv[]) {
TestContendedLock("PTHREAD_MUTEX_ERRORCHECK RAW TLS",
PTHREAD_MUTEX_ERRORCHECK);
__tls_enabled = 0;
__tls_enabled_set(false);
TestUncontendedLock("PTHREAD_MUTEX_NORMAL RAW", PTHREAD_MUTEX_NORMAL);
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW", PTHREAD_MUTEX_RECURSIVE);

View file

@ -17,10 +17,10 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/stdio/rand.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
@ -83,7 +83,7 @@ TEST(memmove, bighug) {
BENCH(memmove, bench) {
volatile char *r;
int n, max = 8 * 1024 * 1024;
int n, max = 128 * 1024 * 1024;
char *volatile p = gc(calloc(max, 1));
char *volatile q = gc(calloc(max, 1));
EZBENCH_N("memmove", 0, memmove(p, q, 0));

View file

@ -108,8 +108,8 @@ TEST(strncmp, testInequality) {
char *s1 = strcpy(malloc(2), "1");
char *s2 = strcpy(malloc(1), "");
ASSERT_EQ(0, strncmp(s1, s2, 0));
ASSERT_EQ('1', strncmp(s1, s2, 1));
ASSERT_EQ(-'1', strncmp(s2, s1, 1));
ASSERT_GT(strncmp(s1, s2, 1), 0);
ASSERT_LT(strncmp(s2, s1, 1), 0);
free(s2);
free(s1);
}

View file

@ -32,7 +32,7 @@
#ifdef __x86_64__
void SetUpOnce(void) {
__tls_enabled = false;
__tls_enabled_set(false);
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
}

View file

@ -1,96 +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 2022 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 "ape/sections.internal.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/rlimit.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
void SetUp(void) {
if (IsWindows()) {
ASSERT_SYS(ENOSYS, -1, brk(0));
ASSERT_SYS(ENOSYS, MAP_FAILED, sbrk(0));
exit(0);
}
}
TEST(sbrk, testReportCurrentBreak) {
ASSERT_SYS(0, _end, sbrk(0));
}
TEST(sbrk, hugeDelta_returnsEoverflow) {
ASSERT_SYS(EOVERFLOW, MAP_FAILED, sbrk(INTPTR_MAX));
}
TEST(brk, underflowsEnd_returnsEinval) {
ASSERT_SYS(EINVAL, -1, brk(0));
}
TEST(sbrk, underflowsEnd_returnsEinval) {
ASSERT_SYS(EINVAL, MAP_FAILED, sbrk(-GUARDSIZE));
}
#ifndef __aarch64__
// not sure if qemu-aarch64 supports this
TEST(sbrk, giantDelta_returnsEnomem) {
if (IsXnu()) return; // mmap polyfills this but brk doesn't right now
if (IsWsl1()) return; // WSL1 setrlimit() is busted
SPAWN(fork);
struct rlimit rl = {1024 * 1024, 1024 * 1024};
ASSERT_SYS(0, 0, setrlimit(RLIMIT_AS, &rl));
ASSERT_SYS(ENOMEM, MAP_FAILED, sbrk(1024 * 1024 * 4));
EXITS(0);
}
#endif
TEST(sbrk, overlapsExistingMapping_failsWithEexist) {
char *p = (char *)ROUNDUP((intptr_t)_end, FRAMESIZE);
ASSERT_EQ(p, mmap(p, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0));
ASSERT_SYS(EEXIST, MAP_FAILED, sbrk(FRAMESIZE));
ASSERT_SYS(0, 0, munmap(p, FRAMESIZE));
}
TEST(sbrk, testGrowAndShrink) {
SPAWN(fork);
ASSERT_FALSE(testlib_memoryexists(_end));
ASSERT_SYS(0, _end, sbrk(GUARDSIZE));
ASSERT_SYS(0, _end + GUARDSIZE, sbrk(0));
ASSERT_TRUE(testlib_memoryexists(_end));
ASSERT_FALSE(testlib_memoryexists(_end + GUARDSIZE));
ASSERT_SYS(0, _end + GUARDSIZE, sbrk(-GUARDSIZE));
ASSERT_FALSE(testlib_memoryexists(_end));
EXITS(0);
}
TEST(brk, testGrowAndShrink) {
SPAWN(fork);
ASSERT_FALSE(testlib_memoryexists(_end));
ASSERT_EQ(0, brk(_end + GUARDSIZE));
ASSERT_TRUE(testlib_memoryexists(_end));
ASSERT_FALSE(testlib_memoryexists(_end + GUARDSIZE));
ASSERT_EQ(0, brk(_end));
EXITS(0);
}

View file

@ -101,6 +101,8 @@ TEST(printf, double) {
}
}
#if LDBL_MANT_DIG == 64
static const struct {
const char *s;
const char *f;
@ -155,3 +157,5 @@ TEST(printf, longdouble) {
}
}
}
#endif // LDBL_MANT_DIG == 64

View file

@ -18,6 +18,7 @@
#include "libc/str/highwayhash64.h"
#include "libc/inttypes.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"

View file

@ -18,6 +18,8 @@
*/
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
TEST(ilogb, yolo) {