From c9a981fdbe50e6c755997a795bf3a7fc1a5518bf Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 28 Apr 2022 20:36:33 -0700 Subject: [PATCH] Fix some more reported issues --- libc/complex.h | 33 +++++ libc/isystem/complex.h | 2 + libc/tinymath/complex.h | 133 ------------------- libc/tinymath/complex.internal.h | 11 ++ libc/tinymath/complex_impl.internal.h | 22 ---- libc/tinymath/csqrt.c | 11 +- test/libc/tinymath/complex_test.c | 34 +++++ test/libc/tinymath/csqrt_test.c | 28 ++++ third_party/lz4cli/lz4cli.mk | 1 + third_party/lz4cli/platform.h | 8 -- tool/net/help.txt | 176 +++++++++++++------------- tool/net/lunix.c | 2 +- 12 files changed, 206 insertions(+), 255 deletions(-) delete mode 100644 libc/tinymath/complex.h create mode 100644 libc/tinymath/complex.internal.h delete mode 100644 libc/tinymath/complex_impl.internal.h create mode 100644 test/libc/tinymath/complex_test.c create mode 100644 test/libc/tinymath/csqrt_test.c diff --git a/libc/complex.h b/libc/complex.h index 51f7dd322..ebde8e5b6 100644 --- a/libc/complex.h +++ b/libc/complex.h @@ -82,6 +82,39 @@ complex long double clogl(complex long double); complex long double conjl(complex long double); complex long double cpowl(complex long double, complex long double); +#ifndef __cplusplus +#define __CIMAG(x, t) \ + (+(union { \ + _Complex t __z; \ + t __xy[2]; \ + }){(_Complex t)(x)} \ + .__xy[1]) +#define creal(x) ((double)(x)) +#define crealf(x) ((float)(x)) +#define creall(x) ((long double)(x)) +#define cimag(x) __CIMAG(x, double) +#define cimagf(x) __CIMAG(x, float) +#define cimagl(x) __CIMAG(x, long double) +#endif + +#ifdef __GNUC__ +#define _Complex_I (__extension__(0.0f + 1.0fi)) +#else +#define _Complex_I (0.0f + 1.0fi) +#endif + +#ifdef _Imaginary_I +#define __CMPLX(x, y, t) ((t)(x) + _Imaginary_I * (t)(y)) +#elif defined(__clang__) +#define __CMPLX(x, y, t) (+(_Complex t){(t)(x), (t)(y)}) +#else +#define __CMPLX(x, y, t) (__builtin_complex((t)(x), (t)(y))) +#endif + +#define CMPLX(x, y) __CMPLX(x, y, double) +#define CMPLXF(x, y) __CMPLX(x, y, float) +#define CMPLXL(x, y) __CMPLX(x, y, long double) + #endif /* C11 */ COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/isystem/complex.h b/libc/isystem/complex.h index 8267a4409..1da2fa62f 100644 --- a/libc/isystem/complex.h +++ b/libc/isystem/complex.h @@ -1,4 +1,6 @@ #ifndef LIBC_ISYSTEM_COMPLEX_H_ #define LIBC_ISYSTEM_COMPLEX_H_ +#include "libc/complex.h" #include "libc/math.h" +#define I _Complex_I #endif diff --git a/libc/tinymath/complex.h b/libc/tinymath/complex.h deleted file mode 100644 index 008b3c7e3..000000000 --- a/libc/tinymath/complex.h +++ /dev/null @@ -1,133 +0,0 @@ -#ifndef _COMPLEX_H -#define _COMPLEX_H - -#ifdef __cplusplus -extern "C" { -#endif - -#define complex _Complex -#ifdef __GNUC__ -#define _Complex_I (__extension__ (0.0f+1.0fi)) -#else -#define _Complex_I (0.0f+1.0fi) -#endif -#define I _Complex_I - -double complex cacos(double complex); -float complex cacosf(float complex); -long double complex cacosl(long double complex); - -double complex casin(double complex); -float complex casinf(float complex); -long double complex casinl(long double complex); - -double complex catan(double complex); -float complex catanf(float complex); -long double complex catanl(long double complex); - -double complex ccos(double complex); -float complex ccosf(float complex); -long double complex ccosl(long double complex); - -double complex csin(double complex); -float complex csinf(float complex); -long double complex csinl(long double complex); - -double complex ctan(double complex); -float complex ctanf(float complex); -long double complex ctanl(long double complex); - -double complex cacosh(double complex); -float complex cacoshf(float complex); -long double complex cacoshl(long double complex); - -double complex casinh(double complex); -float complex casinhf(float complex); -long double complex casinhl(long double complex); - -double complex catanh(double complex); -float complex catanhf(float complex); -long double complex catanhl(long double complex); - -double complex ccosh(double complex); -float complex ccoshf(float complex); -long double complex ccoshl(long double complex); - -double complex csinh(double complex); -float complex csinhf(float complex); -long double complex csinhl(long double complex); - -double complex ctanh(double complex); -float complex ctanhf(float complex); -long double complex ctanhl(long double complex); - -double complex cexp(double complex); -float complex cexpf(float complex); -long double complex cexpl(long double complex); - -double complex clog(double complex); -float complex clogf(float complex); -long double complex clogl(long double complex); - -double cabs(double complex); -float cabsf(float complex); -long double cabsl(long double complex); - -double complex cpow(double complex, double complex); -float complex cpowf(float complex, float complex); -long double complex cpowl(long double complex, long double complex); - -double complex csqrt(double complex); -float complex csqrtf(float complex); -long double complex csqrtl(long double complex); - -double carg(double complex); -float cargf(float complex); -long double cargl(long double complex); - -double cimag(double complex); -float cimagf(float complex); -long double cimagl(long double complex); - -double complex conj(double complex); -float complex conjf(float complex); -long double complex conjl(long double complex); - -double complex cproj(double complex); -float complex cprojf(float complex); -long double complex cprojl(long double complex); - -double creal(double complex); -float crealf(float complex); -long double creall(long double complex); - -#ifndef __cplusplus -#define __CIMAG(x, t) \ - (+(union { _Complex t __z; t __xy[2]; }){(_Complex t)(x)}.__xy[1]) - -#define creal(x) ((double)(x)) -#define crealf(x) ((float)(x)) -#define creall(x) ((long double)(x)) - -#define cimag(x) __CIMAG(x, double) -#define cimagf(x) __CIMAG(x, float) -#define cimagl(x) __CIMAG(x, long double) -#endif - -#if __STDC_VERSION__ >= 201112L -#if defined(_Imaginary_I) -#define __CMPLX(x, y, t) ((t)(x) + _Imaginary_I*(t)(y)) -#elif defined(__clang__) -#define __CMPLX(x, y, t) (+(_Complex t){ (t)(x), (t)(y) }) -#else -#define __CMPLX(x, y, t) (__builtin_complex((t)(x), (t)(y))) -#endif -#define CMPLX(x, y) __CMPLX(x, y, double) -#define CMPLXF(x, y) __CMPLX(x, y, float) -#define CMPLXL(x, y) __CMPLX(x, y, long double) -#endif - -#ifdef __cplusplus -} -#endif -#endif diff --git a/libc/tinymath/complex.internal.h b/libc/tinymath/complex.internal.h new file mode 100644 index 000000000..40b2130f4 --- /dev/null +++ b/libc/tinymath/complex.internal.h @@ -0,0 +1,11 @@ +#ifndef COSMOPOLITAN_LIBC_TINYMATH_COMPLEX_INTERNAL_H_ +#define COSMOPOLITAN_LIBC_TINYMATH_COMPLEX_INTERNAL_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +_Complex double __ldexp_cexp(_Complex double, int) hidden; +_Complex float __ldexp_cexpf(_Complex float, int) hidden; + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_LIBC_TINYMATH_COMPLEX_INTERNAL_H_ */ diff --git a/libc/tinymath/complex_impl.internal.h b/libc/tinymath/complex_impl.internal.h deleted file mode 100644 index b92cb6f3a..000000000 --- a/libc/tinymath/complex_impl.internal.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _COMPLEX_IMPL_H -#define _COMPLEX_IMPL_H - -#include -#include "libc/math.h" - -#undef __CMPLX -#undef CMPLX -#undef CMPLXF -#undef CMPLXL - -#define __CMPLX(x, y, t) \ - ((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z) - -#define CMPLX(x, y) __CMPLX(x, y, double) -#define CMPLXF(x, y) __CMPLX(x, y, float) -#define CMPLXL(x, y) __CMPLX(x, y, long double) - -hidden double complex __ldexp_cexp(double complex,int); -hidden float complex __ldexp_cexpf(float complex,int); - -#endif diff --git a/libc/tinymath/csqrt.c b/libc/tinymath/csqrt.c index d229d2684..d182ec42b 100644 --- a/libc/tinymath/csqrt.c +++ b/libc/tinymath/csqrt.c @@ -25,7 +25,9 @@ │ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/tinymath/complex_impl.internal.h" +#include "libc/complex.h" +#include "libc/math.h" +#include "libc/tinymath/complex.internal.h" asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ @@ -73,9 +75,12 @@ asm(".include \"libc/disclaimer.inc\""); /* We risk spurious overflow for components >= DBL_MAX / (1 + sqrt(2)). */ #define THRESH 0x1.a827999fcef32p+1022 -double complex csqrt(double complex z) +/** + * Returns complex square root. + */ +complex double csqrt(complex double z) { - double complex result; + complex double result; double a, b; double t; int scale; diff --git a/test/libc/tinymath/complex_test.c b/test/libc/tinymath/complex_test.c new file mode 100644 index 000000000..70e6f493e --- /dev/null +++ b/test/libc/tinymath/complex_test.c @@ -0,0 +1,34 @@ +/*-*- 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 "libc/complex.h" +#include "libc/fmt/fmt.h" +#include "libc/testlib/testlib.h" + +TEST(complex, test) { + char buf[128]; + complex double z, z1; + sprintf(buf, "I = %.1f%+.1fi", creal(_Complex_I), cimag(_Complex_I)); + EXPECT_STREQ("I = 0.0+1.0i", buf); + z1 = _Complex_I * _Complex_I; // imaginary unit squared + sprintf(buf, "I * I = %.1f%+.1fi", creal(z1), cimag(z1)); + EXPECT_STREQ("I * I = -1.0+0.0i", buf); + z = 1.0 + 2.0 * _Complex_I; // usual way to form a complex number pre-C11 + sprintf(buf, "z = %.1f%+.1fi", creal(z), cimag(z)); + EXPECT_STREQ("z = 1.0+2.0i", buf); +} diff --git a/test/libc/tinymath/csqrt_test.c b/test/libc/tinymath/csqrt_test.c new file mode 100644 index 000000000..d585a1b08 --- /dev/null +++ b/test/libc/tinymath/csqrt_test.c @@ -0,0 +1,28 @@ +/*-*- 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 "libc/complex.h" +#include "libc/fmt/fmt.h" +#include "libc/runtime/gc.internal.h" +#include "libc/testlib/testlib.h" +#include "libc/x/x.h" + +TEST(csqrt, test) { + complex double x = csqrt(-1); + EXPECT_STREQ("0 1", gc(xasprintf("%g %g", creal(x), cimag(x)))); +} diff --git a/third_party/lz4cli/lz4cli.mk b/third_party/lz4cli/lz4cli.mk index 48116f25e..568db66c6 100644 --- a/third_party/lz4cli/lz4cli.mk +++ b/third_party/lz4cli/lz4cli.mk @@ -41,6 +41,7 @@ o/$(MODE)/third_party/lz4cli/datagen.o: \ THIRD_PARTY_LZ4CLI_DIRECTDEPS = \ LIBC_INTRIN \ LIBC_STDIO \ + LIBC_LOG \ LIBC_TIME \ LIBC_UNICODE diff --git a/third_party/lz4cli/platform.h b/third_party/lz4cli/platform.h index 00701a22e..6da8d6fe6 100644 --- a/third_party/lz4cli/platform.h +++ b/third_party/lz4cli/platform.h @@ -107,14 +107,10 @@ extern "C" { * Detect if isatty() and fileno() are available ************************************************/ #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__) -# include /* isatty */ # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__) -# include /* _isatty */ # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) #elif defined(WIN32) || defined(_WIN32) -# include /* _isatty */ -# include /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ #include "libc/stdio/stdio.h" /* FILE */ static __inline int IS_CONSOLE(FILE* stdStream) { @@ -130,11 +126,7 @@ static __inline int IS_CONSOLE(FILE* stdStream) * OS-specific Includes ******************************/ #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) -# include /* _O_BINARY */ -# include /* _setmode, _fileno, _get_osfhandle */ # if !defined(__DJGPP__) -# include /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ -# include /* FSCTL_SET_SPARSE */ # define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; } # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); } # else diff --git a/tool/net/help.txt b/tool/net/help.txt index 7339f14b8..0fe55737f 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -2700,6 +2700,94 @@ UNIX MODULE See the unix.Rusage section below for details on returned fields. + unix.pledge([promises:str]) + ├─→ true + └─→ nil, unix.Errno + + Restrict system operations. + + This can be used to sandbox your redbean workers. It allows finer + customization compared to the `-S` flag. + + By default exit and exit_group are always allowed. This is useful + for processes that perform pure computation and interface with the + parent via shared memory. + + Currently only available on OpenBSD and Linux. On Linux, the default + action when your policy is violated is to return `EPERM`. On OpenBSD + the kernel will kill the process. + + `promises` is a string that may include any of the following groups + delimited by spaces. + + stdio + + Allows clock_getres, clock_gettime, close, dup, dup2, dup3, + fchdir, fstat, fsync, ftruncate, getdents, getegid, getrandom, + geteuid, getgid, getgroups, getitimer, getpgid, getpgrp, getpid, + getppid, getresgid, getresuid, getrlimit, getsid, gettimeofday, + getuid, lseek, madvise, brk, mmap, mprotect, munmap, nanosleep, + pipe, pipe2, poll, pread, preadv, pwrite, pwritev, read, readv, + recvfrom, recvmsg, select, sendmsg, sendto, setitimer, shutdown, + sigaction, sigprocmask, sigreturn, socketpair, umask, wait4, + write, writev. + + rpath + + Allows chdir, getcwd, openat, fstatat, faccessat, readlinkat, + lstat, chmod, fchmod, fchmodat, chown, fchown, fchownat, fstat. + + wpath + + Allows getcwd, openat, fstatat, faccessat, readlinkat, lstat, + chmod, fchmod, fchmodat, chown, fchown, fchownat, fstat. + + cpath + + Allows rename, renameat, link, linkat, symlink, symlinkat, unlink, + unlinkat, mkdir, mkdirat, rmdir. + + dpath + + Allows mknod + + tmppath + + Allows lstat, chmod, chown, unlink, fstat. + + inet + + Allows socket, listen, bind, connect, accept4, accept, + getpeername, getsockname, setsockopt, getsockopt. + + fattr + + Allows utimes, utimensat, chmod, fchmod, fchmodat, chown, + fchownat, lchown, fchown, utimes. + + unix + + Allows socket, listen, bind, connect, accept4, accept, + getpeername, getsockname, setsockopt, getsockopt. + + dns + + Allows sendto, recvfrom, socket, connect. + + proc + + Allows fork, vfork, kill, getpriority, setpriority, setrlimit, + setpgid, setsid. + + exec + + Allows execve. + + id + + Allows setuid, setreuid, setresuid, setgid, setregid, setresgid, + setgroups, setrlimit, getpriority, setpriority. + unix.gmtime(unixts:int) ├─→ year,mon,mday,hour,min,sec,gmtoffsec,wday,yday,dst:int,zone:str └─→ nil,unix.Errno @@ -3038,94 +3126,6 @@ UNIX MODULE higher priority process after failing to finish its work, within the allotted time slice. - sandbox.pledge([promises:str]) - ├─→ true - └─→ nil, unix.Errno - - Restrict system operations. - - This can be used to sandbox your redbean workers. It allows finer - customization compared to the `-S` flag. - - By default exit and exit_group are always allowed. This is useful - for processes that perform pure computation and interface with the - parent via shared memory. - - Currently only available on OpenBSD and Linux. On Linux, the default - action when your policy is violated is to return `EPERM`. On OpenBSD - the kernel will kill the process. - - `promises` is a string that may include any of the following groups - delimited by spaces. - - stdio - - Allows clock_getres, clock_gettime, close, dup, dup2, dup3, - fchdir, fstat, fsync, ftruncate, getdents, getegid, getrandom, - geteuid, getgid, getgroups, getitimer, getpgid, getpgrp, getpid, - getppid, getresgid, getresuid, getrlimit, getsid, gettimeofday, - getuid, lseek, madvise, brk, mmap, mprotect, munmap, nanosleep, - pipe, pipe2, poll, pread, preadv, pwrite, pwritev, read, readv, - recvfrom, recvmsg, select, sendmsg, sendto, setitimer, shutdown, - sigaction, sigprocmask, sigreturn, socketpair, umask, wait4, - write, writev. - - rpath - - Allows chdir, getcwd, openat, fstatat, faccessat, readlinkat, - lstat, chmod, fchmod, fchmodat, chown, fchown, fchownat, fstat. - - wpath - - Allows getcwd, openat, fstatat, faccessat, readlinkat, lstat, - chmod, fchmod, fchmodat, chown, fchown, fchownat, fstat. - - cpath - - Allows rename, renameat, link, linkat, symlink, symlinkat, unlink, - unlinkat, mkdir, mkdirat, rmdir. - - dpath - - Allows mknod - - tmppath - - Allows lstat, chmod, chown, unlink, fstat. - - inet - - Allows socket, listen, bind, connect, accept4, accept, - getpeername, getsockname, setsockopt, getsockopt. - - fattr - - Allows utimes, utimensat, chmod, fchmod, fchmodat, chown, - fchownat, lchown, fchown, utimes. - - unix - - Allows socket, listen, bind, connect, accept4, accept, - getpeername, getsockname, setsockopt, getsockopt. - - dns - - Allows sendto, recvfrom, socket, connect. - - proc - - Allows fork, vfork, kill, getpriority, setpriority, setrlimit, - setpgid, setsid. - - exec - - Allows execve. - - id - - Allows setuid, setreuid, setresuid, setgid, setregid, setresgid, - setgroups, setrlimit, getpriority, setpriority. - ──────────────────────────────────────────────────────────────────────────────── diff --git a/tool/net/lunix.c b/tool/net/lunix.c index 1fa9f6c00..f5c69240a 100644 --- a/tool/net/lunix.c +++ b/tool/net/lunix.c @@ -646,7 +646,7 @@ static int LuaUnixRaise(lua_State *L) { } // unix.wait([pid:int, options:int]) -// ├─→ pid:int, wstatus:int +// ├─→ pid:int, wstatus:int, unix.Rusage // └─→ nil, unix.Errno static int LuaUnixWait(lua_State *L) { struct rusage ru;