diff --git a/libc/dns/freeaddrinfo.c b/libc/dns/freeaddrinfo.c index 782b20afe..147323056 100644 --- a/libc/dns/freeaddrinfo.c +++ b/libc/dns/freeaddrinfo.c @@ -21,6 +21,7 @@ /** * Frees addresses returned by getaddrinfo(). + * @threadsafe */ int freeaddrinfo(struct addrinfo *ai) { struct addrinfo *next; diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c index b30ce2e32..f07b10bc6 100644 --- a/libc/dns/getaddrinfo.c +++ b/libc/dns/getaddrinfo.c @@ -41,6 +41,7 @@ * @param res receives a pointer that must be freed with freeaddrinfo(), * and won't be modified if -1 is returned * @return 0 on success or EAI_xxx value + * @threadsafe */ int getaddrinfo(const char *name, const char *service, const struct addrinfo *hints, struct addrinfo **res) { diff --git a/libc/dns/gethoststxt.c b/libc/dns/gethoststxt.c index 16598bdde..95cd09bec 100644 --- a/libc/dns/gethoststxt.c +++ b/libc/dns/gethoststxt.c @@ -23,6 +23,7 @@ #include "libc/dce.h" #include "libc/dns/hoststxt.h" #include "libc/fmt/fmt.h" +#include "libc/intrin/pthread.h" #include "libc/macros.internal.h" #include "libc/nt/systeminfo.h" #include "libc/runtime/runtime.h" @@ -32,6 +33,7 @@ static struct HostsTxt *g_hoststxt; static struct HostsTxtInitialStaticMemory { struct HostsTxt ht; + pthread_mutex_t lock; struct HostsTxtEntry entries[8]; char strings[64]; } g_hoststxt_init; @@ -53,6 +55,7 @@ static textwindows dontinline char *GetNtHostsTxtPath(char *pathbuf, * Returns hosts.txt map. * * @note yoinking realloc() ensures there's no size limits + * @threadsafe */ const struct HostsTxt *GetHostsTxt(void) { FILE *f; @@ -60,6 +63,7 @@ const struct HostsTxt *GetHostsTxt(void) { char pathbuf[PATH_MAX]; struct HostsTxtInitialStaticMemory *init; init = &g_hoststxt_init; + pthread_mutex_lock(&init->lock); if (!g_hoststxt) { g_hoststxt = &init->ht; init->ht.entries.n = pushpop(ARRAYLEN(init->entries)); @@ -78,5 +82,6 @@ const struct HostsTxt *GetHostsTxt(void) { fclose(f); } } + pthread_mutex_unlock(&init->lock); return g_hoststxt; } diff --git a/libc/dns/getresolvconf.c b/libc/dns/getresolvconf.c index 3681d9d12..df1d00537 100644 --- a/libc/dns/getresolvconf.c +++ b/libc/dns/getresolvconf.c @@ -20,6 +20,7 @@ #include "libc/dce.h" #include "libc/dns/resolvconf.h" #include "libc/fmt/fmt.h" +#include "libc/intrin/pthread.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" @@ -28,17 +29,20 @@ static struct ResolvConf *g_resolvconf; static struct ResolvConfInitialStaticMemory { struct ResolvConf rv; + pthread_mutex_t lock; struct sockaddr_in nameservers[3]; } g_resolvconf_init; /** * Returns singleton with DNS server address. + * @threadsafe */ const struct ResolvConf *GetResolvConf(void) { int rc; FILE *f; struct ResolvConfInitialStaticMemory *init; init = &g_resolvconf_init; + pthread_mutex_lock(&init->lock); if (!g_resolvconf) { g_resolvconf = &init->rv; pushmov(&init->rv.nameservers.n, ARRAYLEN(init->nameservers)); @@ -58,5 +62,6 @@ const struct ResolvConf *GetResolvConf(void) { /* TODO(jart): Elevate robustness. */ } } + pthread_mutex_unlock(&init->lock); return g_resolvconf; } diff --git a/libc/dns/lookupservicesbyname.c b/libc/dns/lookupservicesbyname.c index 14458531e..d6046fabf 100644 --- a/libc/dns/lookupservicesbyname.c +++ b/libc/dns/lookupservicesbyname.c @@ -47,6 +47,7 @@ * @return -1 on error, or positive port number * @note aliases are read from file for comparison, but not returned. * @see LookupServicesByPort + * @threadsafe */ int LookupServicesByName(const char *servname, char *servproto, size_t servprotolen, char *buf, size_t bufsize, diff --git a/libc/log/cxaprintexits.c b/libc/log/cxaprintexits.c index a02d77490..54bae4ee2 100644 --- a/libc/log/cxaprintexits.c +++ b/libc/log/cxaprintexits.c @@ -37,6 +37,7 @@ void __cxa_printexits(FILE *f, void *pred) { fprintf(f, " GLOBAL DESTRUCTORS \n"); fprintf(f, " callback arg pred \n"); fprintf(f, "---------------------- ------------------ ------------------\n"); + __cxa_lock(); if ((b = __cxa_blocks.p)) { do { mask = b->mask; @@ -56,5 +57,6 @@ void __cxa_printexits(FILE *f, void *pred) { } } while ((b = b->next)); } + __cxa_unlock(); fprintf(f, "\n"); } diff --git a/libc/macros.internal.inc b/libc/macros.internal.inc index 15772f719..29cf280fd 100644 --- a/libc/macros.internal.inc +++ b/libc/macros.internal.inc @@ -127,10 +127,6 @@ // Loads address of errno into %rcx .macro .errno call __errno_location -// cs -// cs -// cs -// mov $__errno,%eax .endm // Post-Initialization Read-Only (PIRO) BSS section. diff --git a/libc/runtime/cosmo.S b/libc/runtime/cosmo.S index 97f228013..adc19feaa 100644 --- a/libc/runtime/cosmo.S +++ b/libc/runtime/cosmo.S @@ -124,6 +124,16 @@ cosmo: push %rbp .weak ape_stack_align #endif +#if IsAsan() + .init.start 305,_init_symbols + push %rdi + push %rsi + call __init_symbols + pop %rsi + pop %rdi + .init.end 305,_init_symbols +#endif + #ifdef __PG__ .init.start 306,_init_ftrace push %rdi @@ -135,16 +145,6 @@ cosmo: push %rbp .init.end 306,_init_ftrace #endif -#if IsAsan() - .init.start 306,_init_symbols - push %rdi - push %rsi - call __init_symbols - pop %rsi - pop %rdi - .init.end 306,_init_symbols -#endif - #if IsModeDbg() #ifdef SYSDEBUG .init.start 307,_init_printargs diff --git a/libc/sock/firewall.c b/libc/sock/firewall.c index 10bdfb4b7..890c75454 100644 --- a/libc/sock/firewall.c +++ b/libc/sock/firewall.c @@ -20,6 +20,8 @@ #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/log/log.h" +#include "libc/runtime/internal.h" +#include "libc/runtime/runtime.h" #include "libc/sock/internal.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/af.h" @@ -42,6 +44,7 @@ void _firewall(const void *addr, uint32_t addrsize) { *p++ = '\n'; write(2, b, p - b); if (weaken(__die)) weaken(__die)(); - abort(); + __restorewintty(); + _Exit(66); } }