Improve system call support

This commit is contained in:
Justine Tunney 2021-08-25 21:35:58 -07:00
parent 63b867bd2f
commit 3085ac7837
65 changed files with 900 additions and 544 deletions

View file

@ -193,6 +193,13 @@
#include "libc/sysv/consts/fileno.h"
#include "libc/x/x.h"
#include "third_party/linenoise/linenoise.h"
asm(".ident\t\"\\n\\n\
linenoise (BSD-2)\\n\
Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>\n\
Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n\
Copyright (c) 2018-2020, Justine Tunney <jtunney at gmail dot com>\"");
asm(".include \"libc/disclaimer.inc\"");
/* clang-format off */
#define LINENOISE_MAX_LINE 4096

View file

@ -17,10 +17,14 @@
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/vendor.internal.h"
#include "libc/nt/runtime.h"
#include "libc/rand/rand.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/symbols.internal.h"
@ -28,6 +32,8 @@
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/nr.h"
#include "libc/x/x.h"
#include "third_party/mbedtls/config.h"
#include "third_party/mbedtls/endian.h"
#include "third_party/mbedtls/error.h"
@ -93,7 +99,7 @@ void mbedtls_test_platform_teardown(void) {
}
wontreturn void exit(int rc) {
if (rc) fwrite(output, 1, appendz(output).i, stderr);
if (rc) xwrite(1, output, appendz(output).i);
free(output);
__cxa_finalize(0);
_Exit(rc);

View file

@ -101,6 +101,7 @@ THIRD_PARTY_MBEDTLS_TEST_DIRECTDEPS = \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_STDIO \
@ -153,6 +154,10 @@ o/$(MODE)/third_party/mbedtls/test/lib.o: \
o/$(MODE)/third_party/mbedtls/test: \
$(THIRD_PARTY_MBEDTLS_TEST_CHECKS)
.PHONY: o/$(MODE)/third_party/mbedtls/test/TESTS
o/$(MODE)/third_party/mbedtls/test/TESTS: \
$(THIRD_PARTY_MBEDTLS_TEST_TESTS)
o/$(MODE)/third_party/mbedtls/test/test_suite_aes.cbc.com: o/$(MODE)/third_party/mbedtls/test/test_suite_aes.cbc.com.dbg
o/$(MODE)/third_party/mbedtls/test/test_suite_aes.cbc.com.dbg: \
$(THIRD_PARTY_MBEDTLS_TEST_DEPS) \

View file

@ -811,7 +811,7 @@ _ssl__SSLSocket_do_handshake_impl(PySSLSocket *self)
if (has_timeout)
timeout = deadline - _PyTime_GetMonotonicClock();
if (err.ssl == SSL_ERROR_WANT_READ) {
if (err.ssl == SSL_ERROR_WANT_READ) {
sockstate = PySSL_select(sock, 0, timeout);
} else if (err.ssl == SSL_ERROR_WANT_WRITE) {
sockstate = PySSL_select(sock, 1, timeout);

View file

@ -1,3 +1,16 @@
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timeval.h"
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/rand/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/grnd.h"
#include "libc/time/time.h"
#include "third_party/python/Modules/expat/expat.h"
#include "third_party/python/Modules/expat/expat_config.h"
/* clang-format off */
/* f2d0ab6d1d4422a08cf1cf3bbdfba96b49dea42fb5ff4615e03a2a25c306e769 (2.2.8+)
__ __ _
@ -39,24 +52,7 @@ asm(".include \"libc/disclaimer.inc\"");
#define XML_BUILDING_EXPAT 1
#ifdef HAVE_EXPAT_CONFIG_H
#include "libc/sysv/consts/grnd.h"
#include "libc/rand/rand.h"
#include "libc/errno.h"
#include "libc/calls/struct/timeval.h"
#include "libc/time/time.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/stdio.h"
#include "libc/calls/calls.h"
#include "libc/assert.h"
#include "libc/limits.h"
#include "third_party/python/Modules/expat/expat_config.h"
#endif
#include "third_party/python/Modules/expat/ascii.inc"
#include "third_party/python/Modules/expat/expat.h"
#include "third_party/python/Modules/expat/siphash.inc"
#if defined(_WIN32) && !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
@ -442,7 +438,6 @@ static ELEMENT_TYPE *getElementType(XML_Parser parser, const ENCODING *enc,
static XML_Char *copyString(const XML_Char *s,
const XML_Memory_Handling_Suite *memsuite);
static unsigned long generate_hash_secret_salt(XML_Parser parser);
static XML_Bool startParsing(XML_Parser parser);
static XML_Parser parserCreate(const XML_Char *encodingName,
@ -592,193 +587,6 @@ static const XML_Char implicitContext[] = {
ASCII_s, ASCII_p, ASCII_a, ASCII_c, ASCII_e,
'\0'};
/* To avoid warnings about unused functions: */
#if !defined(HAVE_ARC4RANDOM_BUF) && !defined(HAVE_ARC4RANDOM)
#if defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM)
/* Obtain entropy on Linux 3.17+ */
static int writeRandomBytes_getrandom_nonblock(void *target, size_t count) {
int success = 0; /* full count bytes written? */
size_t bytesWrittenTotal = 0;
const unsigned int getrandomFlags = GRND_NONBLOCK;
do {
void *const currentTarget = (void *)((char *)target + bytesWrittenTotal);
const size_t bytesToWrite = count - bytesWrittenTotal;
const int bytesWrittenMore =
#if defined(HAVE_GETRANDOM)
getrandom(currentTarget, bytesToWrite, getrandomFlags);
#else
syscall(SYS_getrandom, currentTarget, bytesToWrite, getrandomFlags);
#endif
if (bytesWrittenMore > 0) {
bytesWrittenTotal += bytesWrittenMore;
if (bytesWrittenTotal >= count) success = 1;
}
} while (!success && (errno == EINTR));
return success;
}
#endif /* defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM) */
#if !defined(_WIN32) && defined(XML_DEV_URANDOM)
/* Extract entropy from /dev/urandom */
static int writeRandomBytes_dev_urandom(void *target, size_t count) {
int success = 0; /* full count bytes written? */
size_t bytesWrittenTotal = 0;
const int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
return 0;
}
do {
void *const currentTarget = (void *)((char *)target + bytesWrittenTotal);
const size_t bytesToWrite = count - bytesWrittenTotal;
const ssize_t bytesWrittenMore = read(fd, currentTarget, bytesToWrite);
if (bytesWrittenMore > 0) {
bytesWrittenTotal += bytesWrittenMore;
if (bytesWrittenTotal >= count) success = 1;
}
} while (!success && (errno == EINTR));
close(fd);
return success;
}
#endif /* ! defined(_WIN32) && defined(XML_DEV_URANDOM) */
#endif /* ! defined(HAVE_ARC4RANDOM_BUF) && ! defined(HAVE_ARC4RANDOM) */
#if defined(HAVE_ARC4RANDOM) && !defined(HAVE_ARC4RANDOM_BUF)
static void writeRandomBytes_arc4random(void *target, size_t count) {
size_t bytesWrittenTotal = 0;
while (bytesWrittenTotal < count) {
const uint32_t random32 = arc4random();
size_t i = 0;
for (; (i < sizeof(random32)) && (bytesWrittenTotal < count);
i++, bytesWrittenTotal++) {
const uint8_t random8 = (uint8_t)(random32 >> (i * 8));
((uint8_t *)target)[bytesWrittenTotal] = random8;
}
}
}
#endif /* defined(HAVE_ARC4RANDOM) && ! defined(HAVE_ARC4RANDOM_BUF) */
#ifdef _WIN32
/* Obtain entropy on Windows using the rand_s() function which
* generates cryptographically secure random numbers. Internally it
* uses RtlGenRandom API which is present in Windows XP and later.
*/
static int writeRandomBytes_rand_s(void *target, size_t count) {
size_t bytesWrittenTotal = 0;
while (bytesWrittenTotal < count) {
unsigned int random32 = 0;
size_t i = 0;
if (rand_s(&random32)) return 0; /* failure */
for (; (i < sizeof(random32)) && (bytesWrittenTotal < count);
i++, bytesWrittenTotal++) {
const uint8_t random8 = (uint8_t)(random32 >> (i * 8));
((uint8_t *)target)[bytesWrittenTotal] = random8;
}
}
return 1; /* success */
}
#endif /* _WIN32 */
#if !defined(HAVE_ARC4RANDOM_BUF) && !defined(HAVE_ARC4RANDOM)
static unsigned long gather_time_entropy(void) {
#ifdef _WIN32
FILETIME ft;
GetSystemTimeAsFileTime(&ft); /* never fails */
return ft.dwHighDateTime ^ ft.dwLowDateTime;
#else
struct timeval tv;
int gettimeofday_res;
gettimeofday_res = gettimeofday(&tv, NULL);
#if defined(NDEBUG)
(void)gettimeofday_res;
#else
assert(gettimeofday_res == 0);
#endif /* defined(NDEBUG) */
/* Microseconds time is <20 bits entropy */
return tv.tv_usec;
#endif
}
#endif /* ! defined(HAVE_ARC4RANDOM_BUF) && ! defined(HAVE_ARC4RANDOM) */
static unsigned long ENTROPY_DEBUG(const char *label, unsigned long entropy) {
const char *const EXPAT_ENTROPY_DEBUG = getenv("EXPAT_ENTROPY_DEBUG");
if (EXPAT_ENTROPY_DEBUG && !strcmp(EXPAT_ENTROPY_DEBUG, "1")) {
fprintf(stderr, "Entropy: %s --> 0x%0*lx (%lu bytes)\n", label,
(int)sizeof(entropy) * 2, entropy, (unsigned long)sizeof(entropy));
}
return entropy;
}
static unsigned long generate_hash_secret_salt(XML_Parser parser) {
unsigned long entropy;
(void)parser;
/* "Failproof" high quality providers: */
#if defined(HAVE_ARC4RANDOM_BUF)
arc4random_buf(&entropy, sizeof(entropy));
return ENTROPY_DEBUG("arc4random_buf", entropy);
#elif defined(HAVE_ARC4RANDOM)
writeRandomBytes_arc4random((void *)&entropy, sizeof(entropy));
return ENTROPY_DEBUG("arc4random", entropy);
#else
/* Try high quality providers first .. */
#ifdef _WIN32
if (writeRandomBytes_rand_s((void *)&entropy, sizeof(entropy))) {
return ENTROPY_DEBUG("rand_s", entropy);
}
#elif defined(HAVE_GETRANDOM) || defined(HAVE_SYSCALL_GETRANDOM)
if (writeRandomBytes_getrandom_nonblock((void *)&entropy, sizeof(entropy))) {
return ENTROPY_DEBUG("getrandom", entropy);
}
#endif
#if !defined(_WIN32) && defined(XML_DEV_URANDOM)
if (writeRandomBytes_dev_urandom((void *)&entropy, sizeof(entropy))) {
return ENTROPY_DEBUG("/dev/urandom", entropy);
}
#endif /* ! defined(_WIN32) && defined(XML_DEV_URANDOM) */
/* .. and self-made low quality for backup: */
/* Process ID is 0 bits entropy if attacker has local access */
entropy = gather_time_entropy() ^ getpid();
/* Factors are 2^31-1 and 2^61-1 (Mersenne primes M31 and M61) */
if (sizeof(unsigned long) == 4) {
return ENTROPY_DEBUG("fallback(4)", entropy * 2147483647);
} else {
return ENTROPY_DEBUG("fallback(8)",
entropy * (unsigned long)2305843009213693951ULL);
}
#endif
}
static unsigned long get_hash_secret_salt(XML_Parser parser) {
if (parser->m_parentParser != NULL)
return get_hash_secret_salt(parser->m_parentParser);
@ -789,7 +597,7 @@ static XML_Bool /* only valid for root parser */
startParsing(XML_Parser parser) {
/* hash functions must be initialized before setContext() is called */
if (parser->m_hash_secret_salt == 0)
parser->m_hash_secret_salt = generate_hash_secret_salt(parser);
parser->m_hash_secret_salt = rand64();
if (parser->m_ns) {
/* implicit context only set for root parser, since child
parsers (i.e. external entity parsers) will inherit it

View file

@ -11,6 +11,7 @@
#include "libc/dns/dns.h"
#include "libc/dns/ent.h"
#include "libc/errno.h"
#include "libc/nt/enum/version.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/sysv/consts/af.h"
@ -6803,8 +6804,7 @@ PyInit__socket(void)
PyModule_AddIntMacro(m, INADDR_BROADCAST);
PyModule_AddIntMacro(m, INADDR_LOOPBACK);
PyModule_AddIntMacro(m, INADDR_UNSPEC_GROUP);
PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
INADDR_ALLHOSTS_GROUP);
PyModule_AddIntMacro(m, INADDR_ALLHOSTS_GROUP);
PyModule_AddIntMacro(m, INADDR_MAX_LOCAL_GROUP);
PyModule_AddIntMacro(m, INADDR_NONE);
@ -6930,9 +6930,9 @@ PyInit__socket(void)
if (TCP_USER_TIMEOUT) PyModule_AddIntMacro(m, TCP_USER_TIMEOUT);
if (TCP_SAVE_SYN) PyModule_AddIntMacro(m, TCP_SAVE_SYN);
if (TCP_SAVED_SYN) PyModule_AddIntMacro(m, TCP_SAVED_SYN);
if (TCP_KEEPCNT && (!IsWindows() || NtGetVersion() >= 10))
if (TCP_KEEPCNT && (!IsWindows() || NtGetVersion() >= kNtVersionWindows10))
PyModule_AddIntMacro(m, TCP_KEEPCNT);
if (TCP_FASTOPEN && (!IsWindows() || NtGetVersion() >= 10))
if (TCP_FASTOPEN && (!IsWindows() || NtGetVersion() >= kNtVersionWindows10))
PyModule_AddIntMacro(m, TCP_FASTOPEN);
if (TCP_FASTOPEN_CONNECT)
PyModule_AddIntMacro(m, TCP_FASTOPEN_CONNECT);

View file

@ -27,7 +27,7 @@
\n\
/*\n\
* Auto-generated by\n\
* %s \\\n\
* o/third_party/python/freeze \\\n\
* %s \\\n\
* %s\n\
*/\n\
@ -46,6 +46,8 @@ main(int argc, char *argv[])
PyObject *code = NULL, *marshalled = NULL;
int is_bootstrap = 1;
if (argc == 2 && !strcmp(argv[1], "-n")) return 0;
if (argc != 3) {
fprintf(stderr, "need to specify input and output paths\n");
return 2;
@ -115,7 +117,7 @@ main(int argc, char *argv[])
fprintf(stderr, "cannot open '%s' for writing\n", outpath);
goto error;
}
fprintf(outfile, HEADER, argv[0], argv[1], argv[2]);
fprintf(outfile, HEADER, argv[1], argv[2]);
if (is_bootstrap)
fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
else

View file

@ -8,7 +8,7 @@
/*
* Auto-generated by
* o/third_party/python/freeze.com \
* o/third_party/python/freeze \
* third_party/python/Lib/importlib/_bootstrap.py \
* third_party/python/Python/importlib.inc
*/

View file

@ -8,7 +8,7 @@
/*
* Auto-generated by
* o/third_party/python/freeze.com \
* o/third_party/python/freeze \
* third_party/python/Lib/importlib/_bootstrap_external.py \
* third_party/python/Python/importlib_external.inc
*/

View file

@ -235,8 +235,6 @@ THIRD_PARTY_PYTHON_INCS = \
third_party/python/Python/wordcode_helpers.inc \
third_party/python/Python/ceval_gil.inc \
third_party/python/Python/thread_nt.inc \
third_party/python/Python/importlib_external.inc \
third_party/python/Python/importlib.inc \
third_party/python/Python/clinic/import.inc \
third_party/python/Python/clinic/bltinmodule.inc \
third_party/python/Modules/expat/asciitab.inc \
@ -2266,6 +2264,16 @@ o/$(MODE)/third_party/python/freeze: \
@cp -f $< $@
@$@ -n
third_party/python/Python/importlib.inc: \
o/$(MODE)/third_party/python/freeze \
third_party/python/Lib/importlib/_bootstrap.py
@$(COMPILE) -AFREEZE $^ $@
third_party/python/Python/importlib_external.inc: \
o/$(MODE)/third_party/python/freeze \
third_party/python/Lib/importlib/_bootstrap_external.py
@$(COMPILE) -AFREEZE $^ $@
$(THIRD_PARTY_PYTHON_STAGE1_A): \
third_party/python \
$(THIRD_PARTY_PYTHON_STAGE1_A).pkg \
@ -2306,41 +2314,27 @@ $(THIRD_PARTY_PYTHON_STAGE2_A_OBJS): \
-DMULTIARCH='"x86_64-cosmo"'
o/$(MODE)/third_party/python/Modules/getbuildinfo.o: \
OVERRIDE_CFLAGS += \
-DGITVERSION='"3.6"' \
-DGITTAG='"3.6"' \
-DGITBRANCH='"cosmo"'
o/$(MODE)/third_party/python/Modules/_elementtree.o: \
OVERRIDE_CFLAGS += \
-DUSE_PYEXPAT_CAPI \
-DHAVE_EXPAT_CONFIG_H
o/$(MODE)/third_party/python/Modules/expat/xmlparse.o \
o/$(MODE)/third_party/python/Modules/expat/xmlrole.o \
o/$(MODE)/third_party/python/Modules/expat/xmltok.o \
o/$(MODE)/third_party/python/Modules/pyexpat.o: \
OVERRIDE_CFLAGS += \
-DXML_POOR_ENTROPY \
-DHAVE_EXPAT_CONFIG_H \
-DUSE_PYEXPAT_CAPI
-DGITVERSION='"3.6"' \
-DGITTAG='"3.6"' \
-DGITBRANCH='"cosmo"'
o/$(MODE)/third_party/python/Modules/_decimal/libmpdec/transpose.o: \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED
# Issue #23654: Turn off ICC's tail call optimization for the
# stack_overflow generator. ICC turns the recursive tail
# call into a loop. [Let's do GCC too, just to be safe.]
o/$(MODE)/third_party/python/Modules/faulthandler.o: \
OVERRIDE_CFLAGS += \
-fno-optimize-sibling-calls
OVERRIDE_CFLAGS += \
-fno-optimize-sibling-calls
$(THIRD_PARTY_PYTHON_STDLIB_PYS_OBJS): ZIPOBJ_FLAGS += -P.python -C3
$(THIRD_PARTY_PYTHON_STDLIB_DIRS_OBJS): ZIPOBJ_FLAGS += -P.python -C3
$(THIRD_PARTY_PYTHON_STDLIB_DATA_OBJS): ZIPOBJ_FLAGS += -P.python -C3
$(THIRD_PARTY_PYTHON_STDLIB_PYCS_OBJS): ZIPOBJ_FLAGS += -P.python -C5
.PRECIOUS: $(THIRD_PARTY_PYTHON_STDLIB_PYCS_OBJS)
.PRECIOUS: $(THIRD_PARTY_PYTHON_STDLIB_PYCS)
o/$(MODE)/third_party/python/Python/ceval.o: QUOTA = -M512m
o/$(MODE)/third_party/python/Objects/unicodeobject.o: QUOTA += -C16