Skip to content

Commit

Permalink
Introduce cosmocc flags -mdbg -mtiny -moptlinux
Browse files Browse the repository at this point in the history
The cosmocc.zip toolchain will now include four builds of the libcosmo.a
runtime libraries. You can pass the -mdbg flag if you want to debug your
cosmopolitan runtime. You can pass the -moptlinux flag if you don't want
windows code lurking in your binary. See tool/cosmocc/README.md for more
details on how these flags may be used and their important implications.
  • Loading branch information
jart committed Jul 26, 2024
1 parent 59692b0 commit 642e9cb
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 56 deletions.
36 changes: 32 additions & 4 deletions build/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ CONFIG_CCFLAGS += -O3 -fmerge-all-constants
CONFIG_COPTS += -mred-zone
TARGET_ARCH ?= -march=native
endif
ifeq ($(MODE), x86_64-optlinux)
CONFIG_OFLAGS ?= -g -ggdb
CONFIG_CPPFLAGS += -DNDEBUG -DSYSDEBUG -DSUPPORT_VECTOR=1
CONFIG_CCFLAGS += -O3 -fmerge-all-constants
CONFIG_COPTS += -mred-zone
TARGET_ARCH ?= -march=native
endif
ifeq ($(MODE), aarch64-optlinux)
CONFIG_OFLAGS ?= -g -ggdb
CONFIG_CPPFLAGS += -DNDEBUG -DSYSDEBUG -DSUPPORT_VECTOR=1
CONFIG_CCFLAGS += -O3 -fmerge-all-constants
CONFIG_COPTS += -mred-zone
endif

# Release Mode
#
Expand Down Expand Up @@ -136,17 +149,32 @@ endif
ifeq ($(MODE), dbg)
ENABLE_FTRACE = 1
CONFIG_OFLAGS ?= -g -ggdb
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG -O0 -fno-inline
OVERRIDE_CFLAGS += -O0
OVERRIDE_CXXFLAGS += -O0
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
CONFIG_COPTS += -fsanitize=undefined
OVERRIDE_CCFLAGS += -fno-pie
QUOTA ?= -C64 -L300
endif
ifeq ($(MODE), x86_64-dbg)
ENABLE_FTRACE = 1
CONFIG_OFLAGS ?= -g -ggdb
OVERRIDE_CFLAGS += -O0
OVERRIDE_CXXFLAGS += -O0
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
CONFIG_COPTS += -fsanitize=undefined
OVERRIDE_CCFLAGS += -fno-pie
QUOTA ?= -C64 -L300
endif
ifeq ($(MODE), aarch64-dbg)
ENABLE_FTRACE = 1
CONFIG_OFLAGS ?= -g -ggdb
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG -O0 -fno-inline -fdce
OVERRIDE_CFLAGS += -O0 -fdce
OVERRIDE_CXXFLAGS += -O0 -fdce
CONFIG_CPPFLAGS += -DMODE_DBG -D__SANITIZE_UNDEFINED__ -Wno-unused-variable -Wno-unused-but-set-variable
CONFIG_CCFLAGS += $(BACKTRACES) -DSYSDEBUG
CONFIG_COPTS += -fsanitize=undefined
QUOTA ?= -C64 -L300
endif
Expand Down
6 changes: 5 additions & 1 deletion libc/calls/tcflush.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/strace.h"
#include "libc/mem/alloca.h"
#include "libc/nt/comms.h"
Expand Down Expand Up @@ -52,13 +52,17 @@ static const char *DescribeFlush(char buf[12], int action) {
}

static dontinline textwindows int sys_tcflush_nt(int fd, int queue) {
#ifdef __x86_64__
if (!sys_isatty(fd)) {
return -1; // ebadf, enotty
}
if (queue == TCOFLUSH) {
return 0; // windows console output is never buffered
}
return FlushConsoleInputBytes();
#else
return enosys();
#endif
}

/**
Expand Down
2 changes: 2 additions & 0 deletions libc/dlopen/dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ static void *foreign_thunk_nt(void *func) {
// movabs $tramp,%r10
code[14] = 0x49;
code[15] = 0xba;
#ifdef __x86_64__
WRITE64LE(code + 16, (uintptr_t)__sysv2nt14);
#endif
// jmp *%r10
code[24] = 0x41;
code[25] = 0xff;
Expand Down
9 changes: 8 additions & 1 deletion libc/intrin/cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/cp.internal.h"
#include "libc/intrin/describebacktrace.h"
#include "libc/intrin/kprintf.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/posixthread.internal.h"
Expand Down Expand Up @@ -46,7 +49,11 @@ void end_cancelation_point(int state) {
}
}

void report_cancelation_point(void) {
void report_cancelation_point(int sysv_ordinal, int xnu_ordinal) {
char bt[160];
struct StackFrame *bp = __builtin_frame_address(0);
kprintf("error: report_cancelation_point(%#x, %#x) %s\n", sysv_ordinal,
xnu_ordinal, (DescribeBacktrace)(bt, bp));
__builtin_trap();
}

Expand Down
3 changes: 2 additions & 1 deletion libc/log/oncrash_arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ static relegated void __oncrash_impl(int sig, siginfo_t *si, ucontext_t *ctx) {
if (j)
Append(b, " ");
Append(b, "%s%016lx%s x%d%s", ColorRegister(r),
ctx->uc_mcontext.regs[r], reset, r, r == 8 || r == 9 ? " " : "");
((uint64_t *)ctx->uc_mcontext.regs)[r], reset, r,
r == 8 || r == 9 ? " " : "");
}
Append(b, "\n");
}
Expand Down
4 changes: 4 additions & 0 deletions libc/nexgen32e/nt2sysv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*
* This macro should be used when specifying callbacks in the WIN32 API.
*/
#ifdef __x86_64__
#define NT2SYSV(FUNCTION) TRAMPOLINE(FUNCTION, __nt2sysv)
#else
#define NT2SYSV(FUNCTION) FUNCTION
#endif

#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_NT2SYSV_H_ */
12 changes: 9 additions & 3 deletions libc/proc/posix_spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/intrin/fds.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/calls/struct/rlimit.internal.h"
#include "libc/calls/struct/rusage.internal.h"
Expand All @@ -39,6 +38,7 @@
#include "libc/intrin/bsf.h"
#include "libc/intrin/describeflags.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/fds.h"
#include "libc/intrin/strace.h"
#include "libc/intrin/weaken.h"
#include "libc/mem/alloca.h"
Expand Down Expand Up @@ -95,6 +95,10 @@

#define CLOSER_CONTAINER(e) DLL_CONTAINER(struct Closer, elem, e)

static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd

#ifdef __x86_64__

struct Closer {
int64_t handle;
struct Dll elem;
Expand All @@ -106,8 +110,6 @@ struct SpawnFds {
struct Dll *closers;
};

static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd

static textwindows int64_t spawnfds_handle(struct SpawnFds *fds, int fd) {
if (__is_cloexec(fds->p + fd))
return -1;
Expand Down Expand Up @@ -429,6 +431,8 @@ static textwindows dontinline errno_t posix_spawn_nt(
return err;
}

#endif // __x86_64__

/**
* Spawns process, the POSIX way, e.g.
*
Expand Down Expand Up @@ -482,8 +486,10 @@ errno_t posix_spawn(int *pid, const char *path,
const posix_spawn_file_actions_t *file_actions,
const posix_spawnattr_t *attrp, char *const argv[],
char *const envp[]) {
#ifdef __x86_64__
if (IsWindows())
return posix_spawn_nt(pid, path, file_actions, attrp, argv, envp);
#endif
int pfds[2];
bool use_pipe;
volatile int status = 0;
Expand Down
2 changes: 2 additions & 0 deletions libc/runtime/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,9 @@ int sys_clone_linux(int flags, // rdi

static int LinuxThreadEntry(void *arg, int tid) {
struct LinuxCloneArgs *wt = arg;
#if defined(__x86_64__)
sys_set_tls(ARCH_SET_GS, wt->tls);
#endif
return wt->func(wt->arg, tid);
}

Expand Down
4 changes: 4 additions & 0 deletions libc/runtime/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
/**
* Returns preferred size and alignment of thread stack.
*/
#ifndef MODE_DBG
#define GetStackSize() 81920
#else
#define GetStackSize() 163840
#endif

/**
* Returns preferred stack guard size.
Expand Down
2 changes: 1 addition & 1 deletion libc/sysv/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ o/$(MODE)/libc/sysv/sysret.o: private \

ifeq ($(ARCH),aarch64)
o/$(MODE)/libc/sysv/sysv.o: private \
CFLAGS += \
OVERRIDE_CFLAGS += \
-ffixed-x0 \
-ffixed-x1 \
-ffixed-x2 \
Expand Down
6 changes: 3 additions & 3 deletions libc/sysv/sysv.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ register long freebsd_ordinal asm("x9");
register long xnu_ordinal asm("x16");
register long cosmo_tls_register asm("x28");

void report_cancelation_point(void);
void report_cancelation_point(int, int);

dontinline long systemfive_cancel(void) {
return _weaken(_pthread_cancel_ack)();
Expand All @@ -58,9 +58,9 @@ dontinline long systemfive_cancellable(void) {
return systemfive_cancel();
}
#if IsModeDbg()
if (!(pth->pt_flags & PT_INCANCEL)) {
if (!(pth->pt_flags & PT_INCANCEL) && !(pth->pt_flags & PT_NOCANCEL)) {
if (_weaken(report_cancelation_point)) {
_weaken(report_cancelation_point)();
_weaken(report_cancelation_point)(sysv_ordinal, xnu_ordinal);
}
__builtin_trap();
}
Expand Down
2 changes: 1 addition & 1 deletion libc/tinymath/powl.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ powl(long double x, long double y)
z = one - (r - z);
o.value = z;
j = o.parts32.mswhi;
j += (n << 16);
j += (int32_t)((uint32_t)n << 16); // TODO(jart): why ubsan
if ((j >> 16) <= 0)
z = scalbnl (z, n); /* subnormal output */
else
Expand Down
47 changes: 27 additions & 20 deletions test/ctl/string_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ctl/string.h"
#include "ctl/utility.h"
#include "libc/dce.h"
#include "libc/mem/leaks.h"

#include "libc/calls/struct/timespec.h"
Expand All @@ -43,103 +44,109 @@
const char* big_c = "aaaaaaaaaaaaaaaaaaaaaaaa";
const char* small_c = "aaaaaaaaaaaaaaaaaaaaaaa";

#if IsModeDbg()
#define ITERATIONS 1000 // because qemu in dbg mode is very slow
#else
#define ITERATIONS 1000000
#endif

int
main()
{
const ctl::string_view big(big_c), small(small_c);

BENCH(10000000, 1, {
BENCH(ITERATIONS * 10, 1, {
ctl::string s;
s.append("hello ");
s.append("world");
});

BENCH(1000000, 8, {
BENCH(ITERATIONS, 8, {
ctl::string s;
for (int i = 0; i < 8; ++i) {
s.append('a');
}
});

BENCH(1000000, 16, {
BENCH(ITERATIONS, 16, {
ctl::string s;
for (int i = 0; i < 16; ++i) {
s.append('a');
}
});

BENCH(1000000, 23, {
BENCH(ITERATIONS, 23, {
ctl::string s;
for (int i = 0; i < 23; ++i) {
s.append('a');
}
});

BENCH(1000000, 24, {
BENCH(ITERATIONS, 24, {
ctl::string s;
for (int i = 0; i < 24; ++i) {
s.append('a');
}
});

BENCH(1000000, 32, {
BENCH(ITERATIONS, 32, {
ctl::string s;
for (int i = 0; i < 32; ++i) {
s.append('a');
}
});

BENCH(1000000, 1, { ctl::string s(small_c); });
BENCH(ITERATIONS, 1, { ctl::string s(small_c); });

BENCH(1000000, 1, { ctl::string s(small); });
BENCH(ITERATIONS, 1, { ctl::string s(small); });

{
ctl::string small_copy("hello world");
BENCH(1000000, 1, { ctl::string s2(small_copy); });
BENCH(ITERATIONS, 1, { ctl::string s2(small_copy); });
}

BENCH(1000000, 1, {
BENCH(ITERATIONS, 1, {
ctl::string s(small);
ctl::string s2(ctl::move(s));
});

BENCH(1000000, 1, {
BENCH(ITERATIONS, 1, {
ctl::string s(small);
ctl::string s2(s);
});

BENCH(1000000, 1, { ctl::string s(big_c); });
BENCH(ITERATIONS, 1, { ctl::string s(big_c); });

BENCH(1000000, 1, { ctl::string s(big); });
BENCH(ITERATIONS, 1, { ctl::string s(big); });

{
ctl::string big_copy(big);
BENCH(1000000, 1, { ctl::string s2(big_copy); });
BENCH(ITERATIONS, 1, { ctl::string s2(big_copy); });
}

BENCH(1000000, 1, {
BENCH(ITERATIONS, 1, {
ctl::string s(big);
ctl::string s2(ctl::move(s));
});

BENCH(1000000, 1, {
BENCH(ITERATIONS, 1, {
ctl::string s(big);
ctl::string s2(s);
});

BENCH(1000000, 1, { ctl::string s(23, 'a'); });
BENCH(ITERATIONS, 1, { ctl::string s(23, 'a'); });

BENCH(1000000, 1, { ctl::string s(24, 'a'); });
BENCH(ITERATIONS, 1, { ctl::string s(24, 'a'); });

{
ctl::string s(5, 'a');
BENCH(1000000, 1, { ctl::string_view s2(s); });
BENCH(ITERATIONS, 1, { ctl::string_view s2(s); });
}

{
ctl::string big_trunc(48, 'a');
big_trunc.resize(4);
BENCH(1000000, 1, { ctl::string s(big_trunc); });
BENCH(ITERATIONS, 1, { ctl::string s(big_trunc); });
}

CheckForMemoryLeaks();
Expand Down
Loading

0 comments on commit 642e9cb

Please sign in to comment.