Release Cosmopolitan v3.3

This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.

This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.

Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.

OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().

This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.

We no longer use hex constants to define math.h symbols like M_PI.
This commit is contained in:
Justine Tunney 2024-02-20 11:12:09 -08:00
parent d3ff48c63f
commit 957c61cbbf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
736 changed files with 13726 additions and 9445 deletions

View file

@ -400,9 +400,7 @@ static wontreturn void FreebsdThreadMain(void *p) {
#ifdef __aarch64__
asm volatile("mov\tx28,%0" : /* no outputs */ : "r"(wt->tls));
#elif defined(__x86_64__)
if (__tls_morphed) {
sys_set_tls(AMD64_SET_GSBASE, wt->tls);
}
sys_set_tls(AMD64_SET_GSBASE, wt->tls);
#endif
*wt->ctid = wt->tid;
wt->func(wt->arg, wt->tid);
@ -575,7 +573,7 @@ static int CloneLinux(int (*func)(void *arg, int rc), char *stk, size_t stksz,
#endif
wt = (struct LinuxCloneArgs *)sp;
#ifdef __x86_64__
if ((flags & CLONE_SETTLS) && __tls_morphed) {
if (flags & CLONE_SETTLS) {
flags &= ~CLONE_SETTLS;
wt->arg = arg;
wt->tls = tls;

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/map.h"
#include "libc/intrin/strace.internal.h"
@ -85,19 +84,18 @@ cosmo: push %rbp
call _init
// call constructors
ezlea __init_array_end,ax // static ctors in forward order
.weak __init_array_end // could be called multiple times
ezlea __init_array_start,cx // idempotency recommended
.weak __init_array_start // @see ape/ape.lds
1: cmp %rax,%rcx
.weak __init_array_end
.weak __init_array_start
mov $__init_array_start,%eax
1: cmp $__init_array_end,%eax
je 2f
sub $8,%rax
push %rax
push %rcx
push %rax
call .Largs
call *(%rax)
pop %rcx
pop %rax
pop %rax
add $8,%eax
jmp 1b
// call main()

View file

@ -59,8 +59,6 @@ extern char syscon_openbsd[];
extern char syscon_netbsd[];
extern char syscon_windows[];
extern init_f __strace_init;
extern init_f *__preinit_array_start[] __attribute__((__weak__));
extern init_f *__preinit_array_end[] __attribute__((__weak__));
extern init_f *__init_array_start[] __attribute__((__weak__));
extern init_f *__init_array_end[] __attribute__((__weak__));
extern char ape_stack_prot[] __attribute__((__weak__));
@ -189,7 +187,7 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename,
#if SYSDEBUG
argc = __strace_init(argc, argv, envp, auxv);
#endif
for (init_f **fp = __init_array_end; fp-- > __init_array_start;) {
for (init_f **fp = __init_array_start; fp < __init_array_end; ++fp) {
(*fp)(argc, argv, envp, auxv);
}
#ifdef FTRACE

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/notice.inc"
.underrun
// Uniquely identifies each artifact linked in an address space.

View file

@ -251,7 +251,7 @@ textstartup void __enable_tls(void) {
#ifdef __x86_64__
// rewrite the executable tls opcodes in memory
if (IsWindows() || IsXnu()) {
if (IsWindows() || IsOpenbsd() || IsNetbsd()) {
__morph_tls();
}
#endif

View file

@ -59,7 +59,9 @@ typedef double fenv_t;
#endif /* __x86_64__ */
#ifdef __FLT_EVAL_METHOD__
#ifdef __STDC_WANT_IEC_60559_TYPES_EXT__
#define FLT_EVAL_METHOD __FLT_EVAL_METHOD_TS_18661_3__
#elif defined(__FLT_EVAL_METHOD__)
#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
#else
#define FLT_EVAL_METHOD 0

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "ape/sections.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/serialize.h"
@ -49,68 +50,105 @@ privileged void __morph_tls(void) {
unsigned char *p;
__morph_begin();
if (IsXnu()) {
// Apple is quite straightforward to patch. We basically
// just change the segment register, and the linear slot
// address 0x30 was promised to us, according to Go team
// https://github.com/golang/go/issues/23617
dis = 0x30;
} else if (IsWindows()) {
if (IsWindows()) {
// MSVC __declspec(thread) generates binary code for this
// %gs:0x1480 abi. So long as TlsAlloc() isn't called >64
// times we should be good.
dis = 0x1480 + __tls_index * 8;
} else {
dis = 0;
}
// iterate over modifiable code looking for 9 byte instruction
// this used to take 30ms with xed to enable tls on python.com
for (p = _ereal; p + 9 <= __privileged_start; p += n) {
// iterate over modifiable code looking for 9 byte instruction
// this used to take 30ms with xed to enable tls on python.com
for (p = _ereal; p + 9 <= __privileged_start; p += n) {
// use sse to zoom zoom to fs register prefixes
// that way it'll take 1 ms to morph python.com
// we recompiled a 13mb binary in 1 millisecond
while (p + 9 + 16 <= __privileged_start) {
if ((m = __builtin_ia32_pmovmskb128(
*(xmm_t *)p == (xmm_t){0144, 0144, 0144, 0144, 0144, 0144, 0144,
0144, 0144, 0144, 0144, 0144, 0144, 0144,
0144, 0144}))) {
m = __builtin_ctzll(m);
p += m;
break;
// use sse to zoom zoom to fs register prefixes
// that way it'll take 1 ms to morph python.com
// we recompiled a 13mb binary in 1 millisecond
while (p + 9 + 16 <= __privileged_start) {
if ((m = __builtin_ia32_pmovmskb128(
*(xmm_t *)p == (xmm_t){0145, 0145, 0145, 0145, 0145, 0145,
0145, 0145, 0145, 0145, 0145, 0145,
0145, 0145, 0145, 0145}))) {
m = __builtin_ctzll(m);
p += m;
break;
} else {
p += 16;
}
}
// we're checking for the following expression:
// 0145 == p[0] && // %gs
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
// 0x30 == p[4] && // sib (5,4,0) → (rbp,rsp,0) → disp32
// 0000 == p[5] && // displacement (von Neumann endian)
// 0000 == p[6] && // displacement
// 0000 == p[7] && // displacement
// 0000 == p[8] // displacement
w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
if ((w == READ64LE("\145\110\213\004\045\060\000\000") ||
w == READ64LE("\145\110\003\004\045\060\000\000")) &&
!p[8]) {
// now change the code
p[5] = (dis & 0x000000ff) >> 000; // displacement
p[6] = (dis & 0x0000ff00) >> 010; // displacement
p[7] = (dis & 0x00ff0000) >> 020; // displacement
p[8] = (dis & 0xff000000) >> 030; // displacement
// advance to the next instruction
n = 9;
} else {
p += 16;
n = 1;
}
}
} else {
// iterate over modifiable code looking for 9 byte instruction
// this used to take 30ms with xed to enable tls on python.com
for (p = _ereal; p + 9 <= __privileged_start; p += n) {
// we're checking for the following expression:
// 0144 == p[0] && // %fs
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
// 0045 == p[4] && // sib (5,4,0) → (rbp,rsp,0) → disp32
// 0000 == p[5] && // displacement (von Neumann endian)
// 0000 == p[6] && // displacement
// 0000 == p[7] && // displacement
// 0000 == p[8] // displacement
w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
if ((w == READ64LE("\144\110\213\004\045\000\000\000") ||
w == READ64LE("\144\110\003\004\045\000\000\000")) &&
!p[8]) {
// use sse to zoom zoom to fs register prefixes
// that way it'll take 1 ms to morph python.com
// we recompiled a 13mb binary in 1 millisecond
while (p + 9 + 16 <= __privileged_start) {
if ((m = __builtin_ia32_pmovmskb128(
*(xmm_t *)p == (xmm_t){0145, 0145, 0145, 0145, 0145, 0145,
0145, 0145, 0145, 0145, 0145, 0145,
0145, 0145, 0145, 0145}))) {
m = __builtin_ctzll(m);
p += m;
break;
} else {
p += 16;
}
}
// now change the code
p[0] = 0145; // change %fs to %gs
p[5] = (dis & 0x000000ff) >> 000; // displacement
p[6] = (dis & 0x0000ff00) >> 010; // displacement
p[7] = (dis & 0x00ff0000) >> 020; // displacement
p[8] = (dis & 0xff000000) >> 030; // displacement
// we're checking for the following expression:
// 0145 == p[0] && // %gs
// 0110 == (p[1] & 0373) && // rex.w (and ignore rex.r)
// (0213 == p[2] || // mov reg/mem → reg (word-sized)
// 0003 == p[2]) && // add reg/mem → reg (word-sized)
// 0004 == (p[3] & 0307) && // mod/rm (4,reg,0) means sib → reg
// 0x30 == p[4] && // sib (5,4,0) → (rbp,rsp,0) → disp32
// 0000 == p[5] && // displacement (von Neumann endian)
// 0000 == p[6] && // displacement
// 0000 == p[7] && // displacement
// 0000 == p[8] // displacement
w = READ64LE(p) & READ64LE("\377\373\377\307\377\377\377\377");
if ((w == READ64LE("\145\110\213\004\045\060\000\000") ||
w == READ64LE("\145\110\003\004\045\060\000\000")) &&
!p[8]) {
// advance to the next instruction
n = 9;
} else {
n = 1;
// now change the code
p[0] = 0144; // change %gs to %fs
// advance to the next instruction
n = 9;
} else {
n = 1;
}
}
}

View file

@ -39,10 +39,10 @@ dontinstrument textstartup void __set_tls(struct CosmoTib *tib) {
// ask the operating system to change the x86 segment register
if (IsWindows()) {
asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tib));
} else if (IsFreebsd()) {
sys_set_tls(__tls_morphed ? AMD64_SET_GSBASE : AMD64_SET_FSBASE, tib);
} else if (IsLinux()) {
sys_set_tls(__tls_morphed ? ARCH_SET_GS : ARCH_SET_FS, tib);
sys_set_tls(ARCH_SET_GS, tib);
} else if (IsFreebsd()) {
sys_set_tls(AMD64_SET_GSBASE, tib);
} else if (IsNetbsd()) {
// netbsd has sysarch(X86_SET_FSBASE) but we can't use that because
// signal handlers will cause it to be reset due to not setting the

View file

@ -302,7 +302,7 @@ int __zipos_open(struct ZiposUri *name, int flags) {
return rc;
}
__attribute__((__constructor__)) static void __zipos_ctor(void) {
__attribute__((__constructor__(60))) static textstartup void zipos_ctor(void) {
__zipos_wipe();
pthread_atfork(__zipos_lock, __zipos_unlock, __zipos_wipe);
}