Make numerous improvements

- Python static hello world now 1.8mb
- Python static fully loaded now 10mb
- Python HTTPS client now uses MbedTLS
- Python REPL now completes import stmts
- Increase stack size for Python for now
- Begin synthesizing posixpath and ntpath
- Restore Python \N{UNICODE NAME} support
- Restore Python NFKD symbol normalization
- Add optimized code path for Intel SHA-NI
- Get more Python unit tests passing faster
- Get Python help() pagination working on NT
- Python hashlib now supports MbedTLS PBKDF2
- Make memcpy/memmove/memcmp/bcmp/etc. faster
- Add Mersenne Twister and Vigna to LIBC_RAND
- Provide privileged __printf() for error code
- Fix zipos opendir() so that it reports ENOTDIR
- Add basic chmod() implementation for Windows NT
- Add Cosmo's best functions to Python cosmo module
- Pin function trace indent depth to that of caller
- Show memory diagram on invalid access in MODE=dbg
- Differentiate stack overflow on crash in MODE=dbg
- Add stb_truetype and tools for analyzing font files
- Upgrade to UNICODE 13 and reduce its binary footprint
- COMPILE.COM now logs resource usage of build commands
- Start implementing basic poll() support on bare metal
- Set getauxval(AT_EXECFN) to GetModuleFileName() on NT
- Add descriptions to strerror() in non-TINY build modes
- Add COUNTBRANCH() macro to help with micro-optimizations
- Make error / backtrace / asan / memory code more unbreakable
- Add fast perfect C implementation of μ-Law and a-Law audio codecs
- Make strtol() functions consistent with other libc implementations
- Improve Linenoise implementation (see also github.com/jart/bestline)
- COMPILE.COM now suppresses stdout/stderr of successful build commands
This commit is contained in:
Justine Tunney 2021-09-27 22:58:51 -07:00
parent fa7b4f5bd1
commit 39bf41f4eb
806 changed files with 77494 additions and 63859 deletions

View file

@ -48,13 +48,9 @@ static bool have_getrandom;
* - RtlGenRandom() on Windows
* - getentropy() on XNU and OpenBSD
* - sysctl(KERN_ARND) on FreeBSD and NetBSD
* - RDSEED on Broadwell+ and Xen+ unless GRND_NORDRND
* - RDRAND on Ivybridge+ and Xen+ unless GRND_NORDRND
*
* The following flags may be specified:
*
* - GRND_NORDRND: Don't source rando from hardware.
* - GRND_NOSYSTEM: Don't source rando from operating system.
* - GRND_RANDOM: Halt the entire system while I tap an entropy pool
* so small that it's hard to use statistics to test if it's random
* - GRND_NONBLOCK: Do not wait for i/o events or me to jiggle my
@ -75,137 +71,44 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
int fd, cmd[2];
sigset_t neu, old;
if (n > 256) n = 256;
if (!IsTiny() &&
((f & ~(GRND_RANDOM | GRND_NONBLOCK | GRND_NORDRND | GRND_NOSYSTEM)) ||
(f & (GRND_NORDRND | GRND_NOSYSTEM)) ==
(GRND_NORDRND | GRND_NOSYSTEM))) {
return einval();
}
if (!(f & GRND_NOSYSTEM)) {
if (IsWindows()) {
if (RtlGenRandom(p, n)) {
rc = n;
} else {
return __winerr();
}
} else if (IsFreebsd() || IsNetbsd()) {
if (IsFreebsd()) {
cmd[0] = 1; /* CTL_KERN */
cmd[1] = 37; /* KERN_ARND */
} else {
cmd[0] = 1; /* CTL_KERN */
cmd[1] = 81; /* KERN_ARND */
}
m = n;
if ((rc = sysctl(cmd, 2, p, &m, 0, 0)) != -1) {
rc = m;
}
} else if (have_getrandom) {
if ((rc = sys_getrandom(p, n, f & (GRND_RANDOM | GRND_NONBLOCK))) != -1) {
if (!rc && (IsXnu() || IsOpenbsd())) {
rc = n;
}
}
} else if ((fd = __sys_openat(
AT_FDCWD,
(f & GRND_RANDOM) ? "/dev/random" : "/dev/urandom",
O_RDONLY | ((f & GRND_NONBLOCK) ? O_NONBLOCK : 0), 0)) !=
-1) {
rc = sys_read(fd, p, n);
sys_close(fd);
if ((f & ~(GRND_RANDOM | GRND_NONBLOCK))) return einval();
if (IsWindows()) {
if (RtlGenRandom(p, n)) {
rc = n;
} else {
return enosys();
return __winerr();
}
} else if (IsFreebsd() || IsNetbsd()) {
if (IsFreebsd()) {
cmd[0] = 1; /* CTL_KERN */
cmd[1] = 37; /* KERN_ARND */
} else {
cmd[0] = 1; /* CTL_KERN */
cmd[1] = 81; /* KERN_ARND */
}
m = n;
if ((rc = sysctl(cmd, 2, p, &m, 0, 0)) != -1) {
rc = m;
}
} else if (have_getrandom) {
if ((rc = sys_getrandom(p, n, f & (GRND_RANDOM | GRND_NONBLOCK))) != -1) {
if (!rc && (IsXnu() || IsOpenbsd())) {
rc = n;
}
}
} else if ((fd = __sys_openat(
AT_FDCWD, (f & GRND_RANDOM) ? "/dev/random" : "/dev/urandom",
O_RDONLY | ((f & GRND_NONBLOCK) ? O_NONBLOCK : 0), 0)) !=
-1) {
rc = sys_read(fd, p, n);
sys_close(fd);
} else {
memset(p, 0, n);
rc = n;
}
if (rc != -1) {
if (!IsTiny()) {
if (rc < 0 || rc > n) {
abort();
}
if (f & (GRND_RANDOM | GRND_NONBLOCK)) {
if (n && !rc) {
abort();
}
} else {
if (rc != n) {
abort();
}
}
}
if (!(f & GRND_NORDRND)) {
if (X86_HAVE(RDSEED)) {
for (i = j = 0; i < rc; ++j) {
/* CF=1: Destination register valid. Quoth Intel DRNG-SIG 4.1.3 */
asm volatile(CFLAG_ASM("rdseed\t%1")
: CFLAG_CONSTRAINT(cf), "=r"(x)
: /* no inputs */
: "cc");
if (cf) {
j = 0;
if (i + 8 <= rc) {
x ^= READ64LE((char *)p + i);
WRITE64LE((char *)p + i, x);
i += 8;
} else {
for (; i < rc; x >>= 8) {
((char *)p)[i++] ^= x;
}
}
} else if (j == 10) {
asm volatile("pause");
}
}
rc = i;
} else if (X86_HAVE(RDRND)) {
for (i = j = 0; i < rc; ++j) {
/* CF=1: Destination register valid. Quoth Intel DRNG-SIG 4.1.3 */
asm volatile(CFLAG_ASM("rdrand\t%1")
: CFLAG_CONSTRAINT(cf), "=r"(x)
: /* no inputs */
: "cc");
if (cf) {
j = 0;
if (i + 8 <= rc) {
x ^= READ64LE((char *)p + i);
WRITE64LE((char *)p + i, x);
i += 8;
} else {
for (; i < rc; x >>= 8) {
((char *)p)[i++] ^= x;
}
}
} else if (j == 10) {
asm volatile("pause");
}
}
rc = i;
} else if (f & GRND_NOSYSTEM) {
return enosys();
}
}
return enosys();
}
return rc;
}
static textstartup void getrandom_init(int argc, char **argv, char **envp,
intptr_t *auxv) {
extern unsigned kMutableCpuids[KCPUIDS_LEN][4] asm("kCpuids");
/*
* Clear RDRAND on AMD models before Zen and then some
* since it's not only slow but can freeze after sleep
* https://bugzilla.redhat.com/show_bug.cgi?id=1150286
*/
if ((X86_HAVE(RDRND) || X86_HAVE(RDSEED)) &&
(IsAuthenticAMD() &&
(kX86CpuFamily < 0x17 ||
(kX86CpuFamily == 0x17 &&
(0x70 <= kX86CpuModel && kX86CpuModel <= 0x7F))))) {
kMutableCpuids[KCPUIDS_1H][KCPUIDS_ECX] &= ~(1u << 30);
kMutableCpuids[KCPUIDS_7H][KCPUIDS_EBX] &= ~(1u << 18);
}
static textstartup void getrandom_init(void) {
if (sys_getrandom(0, 0, 0) == 0) {
have_getrandom = true;
}