From b73e35c6fac2960b3a5fb082a712145318e2218b Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 8 Sep 2022 02:48:17 -0700 Subject: [PATCH] Improve open source compatibility This change tunes the default stack size for the outside world to 8mb while at the same time, reducing Cosmopolitan's default stack size to 64kb. You can override the stack size using STATIC_STACK_SIZE(). Your build scripts should point to o//ape/public/ape.lds This change also fixes the definition of SOMAXCONN and removes AF_RDS since it's not polyfilled and Python 3.11 complained. --- ape/ape.mk | 17 +++++++++++ ape/public/ape.lds | 1 + libc/integral/normalize.inc | 10 ++++++- libc/isystem/sys/socket.h | 1 + libc/sysv/consts.sh | 2 +- libc/sysv/consts/af.h | 2 -- libc/sysv/consts/ipproto.h | 2 +- libc/sysv/consts/limits.h | 2 ++ test/libc/release/test.mk | 33 +++++++++++----------- third_party/python/Lib/socket.py | 1 - third_party/python/Lib/test/test_socket.py | 1 - third_party/python/Modules/socketmodule.c | 2 -- tool/build/lib/loader.c | 2 +- 13 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 ape/public/ape.lds diff --git a/ape/ape.mk b/ape/ape.mk index e517365a4..179790eef 100644 --- a/ape/ape.mk +++ b/ape/ape.mk @@ -72,6 +72,22 @@ o/$(MODE)/ape/ape.lds: \ libc/str/str.h \ libc/zip.h +o/$(MODE)/ape/public/ape.lds: OVERRIDE_CPPFLAGS += -UCOSMO +o/$(MODE)/ape/public/ape.lds: \ + ape/public/ape.lds \ + ape/ape.lds \ + ape/macros.internal.h \ + ape/relocations.h \ + libc/intrin/bits.h \ + libc/calls/struct/timespec.h \ + libc/dce.h \ + libc/elf/def.h \ + libc/elf/pf2prot.internal.h \ + libc/macros.internal.h \ + libc/nt/pedef.internal.h \ + libc/str/str.h \ + libc/zip.h + o/ape/idata.inc: \ ape/idata.internal.h \ ape/relocations.h @@ -159,6 +175,7 @@ o/$(MODE)/ape/ape.macho.dbg: \ o/$(MODE)/ape: $(APE_CHECKS) \ o/$(MODE)/ape/ape.o \ o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/ape/ape.elf \ o/$(MODE)/ape/ape.macho \ o/$(MODE)/ape/ape-copy-self.o \ diff --git a/ape/public/ape.lds b/ape/public/ape.lds new file mode 100644 index 000000000..7f2579702 --- /dev/null +++ b/ape/public/ape.lds @@ -0,0 +1 @@ +#include "ape/ape.lds" diff --git a/libc/integral/normalize.inc b/libc/integral/normalize.inc index c675eff38..5618d7eb0 100644 --- a/libc/integral/normalize.inc +++ b/libc/integral/normalize.inc @@ -66,8 +66,16 @@ #define __BIGGEST_ALIGNMENT__ 16 #endif +/* TODO(jart): Remove this in favor of GetStackSize() */ +#if defined(COSMO) && defined(MODE_DBG) +#define STACKSIZE 0x20000 /* 128kb stack */ +#elif defined(COSMO) +#define STACKSIZE 0x10000 /* 64kb stack */ +#else +#define STACKSIZE 0x800000 /* 8mb stack */ +#endif + #define BIGPAGESIZE 0x200000 -#define STACKSIZE 0x20000 #define FRAMESIZE 0x10000 /* 8086 */ #define PAGESIZE 0x1000 /* i386+ */ #define BUFSIZ 0x1000 /* best stdio default */ diff --git a/libc/isystem/sys/socket.h b/libc/isystem/sys/socket.h index 432523617..948a7b939 100644 --- a/libc/isystem/sys/socket.h +++ b/libc/isystem/sys/socket.h @@ -5,6 +5,7 @@ #include "libc/sock/struct/msghdr.h" #include "libc/sock/struct/sockaddr.h" #include "libc/sysv/consts/af.h" +#include "libc/sysv/consts/limits.h" #include "libc/sysv/consts/msg.h" #include "libc/sysv/consts/pf.h" #include "libc/sysv/consts/scm.h" diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index 08a68bdfb..cb11bead7 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -1230,6 +1230,7 @@ syscon mount MNT_SNAPSHOT 0 0x40000000 0x01000000 0 0 0 # confusing # group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary syscon limits PIPE_BUF 4096 512 512 512 512 4096 # bsd consensus syscon limits NGROUPS_MAX 65536 16 1023 16 16 0 # +syscon limits SOMAXCONN 4096 128 128 128 128 2147483647 # maximum backlog for listen() syscon limits _ARG_MAX 128*1024 1024*1024 512*1024 512*1024 256*1024 32767*2 # bsd consensus syscon limits _NAME_MAX 255 255 255 255 511 255 # probably higher on windows? syscon limits _PATH_MAX 4096 1024 1024 1024 1024 512 # cosmopolitan libc imposes a lower 512 limit; nt theoretically goes up to 32767 @@ -2292,7 +2293,6 @@ syscon misc SET_WINDOW 36 0 0 0 0 0 syscon misc SFD_CLOEXEC 0x080000 0 0 0 0 0 syscon misc SFD_NONBLOCK 0x0800 0 0 0 0 0 -syscon misc SOMAXCONN 0x80 0x80 0x80 0x80 0x80 0x7fffffff # unix consensus syscon misc SUBCMDMASK 255 255 255 255 255 0 # unix consensus syscon misc SUBCMDSHIFT 8 8 8 8 8 0 # unix consensus syscon misc SYMTYPE 50 50 50 50 50 0 # unix consensus diff --git a/libc/sysv/consts/af.h b/libc/sysv/consts/af.h index 1792a2a02..13cfdc40a 100644 --- a/libc/sysv/consts/af.h +++ b/libc/sysv/consts/af.h @@ -38,7 +38,6 @@ extern const int AF_NFC; extern const int AF_PACKET; extern const int AF_PHONET; extern const int AF_PPPOX; -extern const int AF_RDS; extern const int AF_ROSE; extern const int AF_ROUTE; extern const int AF_RXRPC; @@ -88,7 +87,6 @@ COSMOPOLITAN_C_END_ #define AF_PACKET SYMBOLIC(AF_PACKET) #define AF_PHONET SYMBOLIC(AF_PHONET) #define AF_PPPOX SYMBOLIC(AF_PPPOX) -#define AF_RDS SYMBOLIC(AF_RDS) #define AF_ROSE SYMBOLIC(AF_ROSE) #define AF_ROUTE SYMBOLIC(AF_ROUTE) #define AF_RXRPC SYMBOLIC(AF_RXRPC) diff --git a/libc/sysv/consts/ipproto.h b/libc/sysv/consts/ipproto.h index 37a6fc862..489734f47 100644 --- a/libc/sysv/consts/ipproto.h +++ b/libc/sysv/consts/ipproto.h @@ -46,6 +46,7 @@ COSMOPOLITAN_C_END_ #define IPPROTO_TCP LITERALLY(6) #define IPPROTO_UDP LITERALLY(17) #define IPPROTO_RAW LITERALLY(255) +#define IPPROTO_MAX LITERALLY(263) /* xxx */ #define IPPROTO_AH SYMBOLIC(IPPROTO_AH) #define IPPROTO_BEETPH SYMBOLIC(IPPROTO_BEETPH) @@ -63,7 +64,6 @@ COSMOPOLITAN_C_END_ #define IPPROTO_IGMP SYMBOLIC(IPPROTO_IGMP) #define IPPROTO_IPIP SYMBOLIC(IPPROTO_IPIP) #define IPPROTO_IPV6 SYMBOLIC(IPPROTO_IPV6) -#define IPPROTO_MAX SYMBOLIC(IPPROTO_MAX) #define IPPROTO_MH SYMBOLIC(IPPROTO_MH) #define IPPROTO_MPLS SYMBOLIC(IPPROTO_MPLS) #define IPPROTO_MTP SYMBOLIC(IPPROTO_MTP) diff --git a/libc/sysv/consts/limits.h b/libc/sysv/consts/limits.h index 400bdbae9..338f42840 100644 --- a/libc/sysv/consts/limits.h +++ b/libc/sysv/consts/limits.h @@ -6,6 +6,7 @@ COSMOPOLITAN_C_START_ extern const int NGROUPS_MAX; extern const int PIPE_BUF; +extern const int SOMAXCONN; extern const int _ARG_MAX; extern const int _NAME_MAX; extern const int _NSIG; @@ -16,6 +17,7 @@ COSMOPOLITAN_C_END_ #define NGROUPS_MAX SYMBOLIC(NGROUPS_MAX) #define PIPE_BUF SYMBOLIC(PIPE_BUF) +#define SOMAXCONN SYMBOLIC(SOMAXCONN) #define _ARG_MAX SYMBOLIC(_ARG_MAX) #define _NAME_MAX SYMBOLIC(_NAME_MAX) #define _NSIG SYMBOLIC(_NSIG) diff --git a/test/libc/release/test.mk b/test/libc/release/test.mk index f33d01166..502f01fa2 100644 --- a/test/libc/release/test.mk +++ b/test/libc/release/test.mk @@ -1,19 +1,20 @@ #-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ -o/$(MODE)/test/libc/release/cosmopolitan.zip: .UNSANDBOXED = 1 o/$(MODE)/test/libc/release/cosmopolitan.zip: \ o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ o/$(MODE)/ape/ape-copy-self.o \ o/$(MODE)/ape/ape-no-modify-self.o \ - o/$(MODE)/cosmopolitan.a - mkdir -p $(@D) - zip -qj $@ \ + o/$(MODE)/cosmopolitan.a \ + o/$(MODE)/third_party/zip/zip.com + @$(COMPILE) -AZIP -T$@ \ + o/$(MODE)/third_party/zip/zip.com \ + -b$(TMPDIR) -qj $@ \ o/cosmopolitan.h \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ o/$(MODE)/ape/ape-copy-self.o \ @@ -35,7 +36,7 @@ o/$(MODE)/test/libc/release/smoke.o: \ o/$(MODE)/test/libc/release/smoke.com.dbg: \ o/$(MODE)/test/libc/release/smoke.o \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a @@ -43,7 +44,7 @@ o/$(MODE)/test/libc/release/smoke.com.dbg: \ -static \ -no-pie \ -nostdlib \ - -T o/$(MODE)/ape/ape.lds \ + -T o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/test/libc/release/smoke.o \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ @@ -52,7 +53,7 @@ o/$(MODE)/test/libc/release/smoke.com.dbg: \ o/$(MODE)/test/libc/release/smoke-nms.com.dbg: \ o/$(MODE)/test/libc/release/smoke.o \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape-no-modify-self.o \ o/$(MODE)/cosmopolitan.a @@ -60,7 +61,7 @@ o/$(MODE)/test/libc/release/smoke-nms.com.dbg: \ -static \ -no-pie \ -nostdlib \ - -T o/$(MODE)/ape/ape.lds \ + -T o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/test/libc/release/smoke.o \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape-no-modify-self.o \ @@ -69,7 +70,7 @@ o/$(MODE)/test/libc/release/smoke-nms.com.dbg: \ o/$(MODE)/test/libc/release/smoke-chibicc.com.dbg: \ o/$(MODE)/test/libc/release/smoke-chibicc.o \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape-no-modify-self.o \ o/$(MODE)/cosmopolitan.a \ @@ -78,7 +79,7 @@ o/$(MODE)/test/libc/release/smoke-chibicc.com.dbg: \ -static \ -no-pie \ -nostdlib \ - -T o/$(MODE)/ape/ape.lds \ + -T o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/test/libc/release/smoke-chibicc.o \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape-no-modify-self.o \ @@ -106,7 +107,7 @@ o/$(MODE)/test/libc/release/smoke-chibicc.o: \ o/$(MODE)/test/libc/release/smokecxx.com.dbg: \ o/$(MODE)/test/libc/release/smokecxx.o \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a @@ -114,7 +115,7 @@ o/$(MODE)/test/libc/release/smokecxx.com.dbg: \ -static \ -no-pie \ -nostdlib \ - -T o/$(MODE)/ape/ape.lds \ + -T o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/test/libc/release/smokecxx.o \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ @@ -136,7 +137,7 @@ o/$(MODE)/test/libc/release/smokecxx.o: \ o/$(MODE)/test/libc/release/smokeansi.com.dbg: \ o/$(MODE)/test/libc/release/smokeansi.o \ - o/$(MODE)/ape/ape.lds \ + o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ o/$(MODE)/cosmopolitan.a @@ -144,7 +145,7 @@ o/$(MODE)/test/libc/release/smokeansi.com.dbg: \ -static \ -no-pie \ -nostdlib \ - -T o/$(MODE)/ape/ape.lds \ + -T o/$(MODE)/ape/public/ape.lds \ o/$(MODE)/test/libc/release/smokeansi.o \ o/$(MODE)/libc/crt/crt.o \ o/$(MODE)/ape/ape.o \ diff --git a/third_party/python/Lib/socket.py b/third_party/python/Lib/socket.py index ace22f0a4..5ba8aef4f 100644 --- a/third_party/python/Lib/socket.py +++ b/third_party/python/Lib/socket.py @@ -769,7 +769,6 @@ if __name__ == 'PYOBJ.COM': AF_NETROM = 0 AF_PACKET = 0 AF_PPPOX = 0 - AF_RDS = 0 AF_ROSE = 0 AF_ROUTE = 0 AF_SECURITY = 0 diff --git a/third_party/python/Lib/test/test_socket.py b/third_party/python/Lib/test/test_socket.py index 95c3938ac..7896725f8 100644 --- a/third_party/python/Lib/test/test_socket.py +++ b/third_party/python/Lib/test/test_socket.py @@ -1696,7 +1696,6 @@ class CANTest(ThreadedCANSocketTest): class BasicRDSTest(unittest.TestCase): def testCrucialConstants(self): - socket.AF_RDS socket.PF_RDS def testCreateSocket(self): diff --git a/third_party/python/Modules/socketmodule.c b/third_party/python/Modules/socketmodule.c index f66f3e4d8..e95f5d79e 100644 --- a/third_party/python/Modules/socketmodule.c +++ b/third_party/python/Modules/socketmodule.c @@ -77,7 +77,6 @@ PYTHON_PROVIDE("_socket.AF_NETBEUI"); PYTHON_PROVIDE("_socket.AF_NETROM"); PYTHON_PROVIDE("_socket.AF_PACKET"); PYTHON_PROVIDE("_socket.AF_PPPOX"); -PYTHON_PROVIDE("_socket.AF_RDS"); PYTHON_PROVIDE("_socket.AF_ROSE"); PYTHON_PROVIDE("_socket.AF_ROUTE"); PYTHON_PROVIDE("_socket.AF_SECURITY"); @@ -6666,7 +6665,6 @@ PyInit__socket(void) if (AF_CAN) PyModule_AddIntMacro(m, AF_CAN); /* Controller Area Network */ if (PF_CAN) PyModule_AddIntMacro(m, PF_CAN); - if (AF_RDS) PyModule_AddIntMacro(m, AF_RDS); /* Reliable Datagram Sockets */ if (PF_RDS) PyModule_AddIntMacro(m, PF_RDS); if (AF_PACKET) PyModule_AddIntMacro(m, AF_PACKET); if (PF_PACKET) PyModule_AddIntMacro(m, PF_PACKET); diff --git a/tool/build/lib/loader.c b/tool/build/lib/loader.c index 96ebabef0..2f6e643b0 100644 --- a/tool/build/lib/loader.c +++ b/tool/build/lib/loader.c @@ -204,7 +204,7 @@ void LoadProgram(struct Machine *m, const char *prog, char **args, char **vars, sp = 0x800000000000; Write64(m->sp, sp); m->cr3 = AllocateLinearPage(m); - CHECK_NE(-1, ReserveVirtual(m, sp - STACKSIZE, STACKSIZE, + CHECK_NE(-1, ReserveVirtual(m, sp - 0x800000, 0x800000, PAGE_V | PAGE_RW | PAGE_U | PAGE_RSRV)); LoadArgv(m, prog, args, vars); if (memcmp(elf->map, "\177ELF", 4) == 0) {