Commit graph

2675 commits

Author SHA1 Message Date
Justine Tunney
c8e10eef30
Make bulk_free() go faster 2024-12-23 20:31:57 -08:00
Justine Tunney
624573207e
Make threads faster and more reliable
This change doubles the performance of thread spawning. That's thanks to
our new stack manager, which allows us to avoid zeroing stacks. It gives
us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is
faster now, since it doesn't need to acquire the pthread GIL. On NetBSD,
that helps us avoid allocating too many semaphores. Even if that happens
we're now able to survive semaphores running out and even memory running
out, when allocating *NSYNC waiter objects. I found a lot more rare bugs
in the POSIX threads runtime that could cause things to crash, if you've
got dozens of threads all spawning and joining dozens of threads. I want
cosmo to be world class production worthy for 2025 so happy holidays all
2024-12-21 22:13:00 -08:00
Justine Tunney
906bd06a5a
Fix MODE=tiny build 2024-12-17 01:36:29 -08:00
Justine Tunney
c8c81af0c7
Remove distracting code from dlmalloc 2024-12-16 22:54:30 -08:00
Justine Tunney
af7bd80430
Eliminate cyclic locks in runtime
This change introduces a new deadlock detector for Cosmo's POSIX threads
implementation. Error check mutexes will now track a DAG of nested locks
and report EDEADLK when a deadlock is theoretically possible. These will
occur rarely, but it's important for production hardening your code. You
don't even need to change your mutexes to use the POSIX error check mode
because `cosmocc -mdbg` will enable error checking on mutexes by default
globally. When cycles are found, an error message showing your demangled
symbols describing the strongly connected component are printed and then
the SIGTRAP is raised, which means you'll also get a backtrace if you're
using ShowCrashReports() too. This new error checker is so low-level and
so pure that it's able to verify the relationships of every libc runtime
lock, including those locks upon which the mutex implementation depends.
2024-12-16 22:25:12 -08:00
Justine Tunney
26c051c297
Spoof PID across execve() on Windows
It's now possible with cosmo and redbean, to deliver a signal to a child
process after it has called execve(). However the executed program needs
to be compiled using cosmocc. The cosmo runtime WinMain() implementation
now intercepts a _COSMO_PID environment variable that's set by execve().
It ensures the child process will use the same C:\ProgramData\cosmo\sigs
file, which is where kill() will place the delivered signal. We are able
to do this on Windows even better than NetBSD, which has a bug with this

Fixes #1334
2024-12-14 13:13:08 -08:00
Justine Tunney
9cc1bd04b2
Test rwlock more 2024-12-14 09:40:13 -08:00
Justine Tunney
69402f4d78
Support building ltests.c in MODE=dbg
Fixes #1226
2024-12-13 08:19:42 -08:00
Justine Tunney
838b54f906
Fix C++ math.h include order issue
Fixes #1257
2024-12-13 07:49:59 -08:00
Justine Tunney
2d43d400c6
Support process shared pthread_rwlock
Cosmo now has a non-nsync implementation of POSIX read-write locks. It's
possible to call pthread_rwlockattr_setpshared in PTHREAD_PROCESS_SHARED
mode. Furthermore, if cosmo is built with PTHREAD_USE_NSYNC set to zero,
then Cosmo shouldn't use nsync at all. That's helpful if you want to not
link any Apache 2.0 licensed code.
2024-12-13 03:00:06 -08:00
Justine Tunney
c22b413ac4
Make strcasestr() faster 2024-12-12 22:50:20 -08:00
Justine Tunney
22094ae9ca
Change language in leak detector 2024-12-10 11:04:35 -08:00
Justine Tunney
bda2a4d55e
Fix jtckdint version number 2024-12-07 03:19:11 -08:00
Justine Tunney
b490e23d63
Improve Windows sleep accuracy from 15ms to 15µs 2024-12-06 23:03:57 -08:00
Steven Dee (Jōshin)
b40140e6c5
Improve redbean concurrency (#1332)
In the course of playing with redbean I was confused about how the state
was behaving and then noticed that some stuff is maybe getting edited by
multiple processes. I tried to improve things by changing the definition
of the counter variables to be explicitly atomic. Claude assures me that
most modern Unixes support cross-process atomics, so I just went with it
on that front.

I also added some mutexes to the shared state to try to synchronize some
other things that might get written or read from workers but couldn't be
made atomic, mainly the rusage and time values. I could've probably been
less granular and just had a global shared-state lock, but I opted to be
fairly granular as a starting point.

This also reorders the resetting of the lastmeltdown timespec before the
SIGUSR2 signal is sent; hopefully this is okay.
2024-12-02 14:05:38 -08:00
Steven Dee (Jōshin)
3142758675
Fix atomic_fetch_sub on workers (#1331)
clangd was showing a diagnostic for this line.
2024-11-29 16:57:43 -08:00
Justine Tunney
cf9252f429
Correct redbean unix.commandv() docs
Fixes #1330
2024-11-29 12:15:03 -08:00
Justine Tunney
5fae582e82
Protect privileged demangler from stack overflow 2024-11-24 06:43:17 -08:00
Justine Tunney
ef00a7d0c2
Fix AFL crashes in C++ demangler
American Fuzzy Lop didn't need to try very hard, to crash our privileged
__demangle() implementation. This change helps ensure our barebones impl
will fail rather than crash when given adversarial input data.
2024-11-23 14:25:09 -08:00
Justine Tunney
746660066f
Release Cosmopolitan v3.9.7 2024-11-22 21:38:09 -08:00
Justine Tunney
fd15b2d7a3
Ensure ^C gets printed to Windows console 2024-11-22 14:56:53 -08:00
Justine Tunney
e228aa3e14
Save rax register in getcontext 2024-11-22 13:32:52 -08:00
Justine Tunney
9ddbfd921e
Introduce cosmo_futex_wait and cosmo_futex_wake
Cosmopolitan Futexes are now exposed as a public API.
2024-11-22 11:25:15 -08:00
Justine Tunney
729f7045e3
Cleanup terminal on ^C in asteroids game 2024-11-22 08:52:49 -08:00
Justine Tunney
e47d67ba9b
Add asteroids game
Source code is the same as upstream, aside from a header added.
2024-11-22 08:46:33 -08:00
Justine Tunney
2477677c85
Delete superfluous definition 2024-11-22 08:27:42 -08:00
Justine Tunney
abdf6c9c26
Sync with jtckdint 2024-11-20 15:56:56 -08:00
Justine Tunney
5c3f854acb
Fix strcasestr()
Fixes #1323
2024-11-20 14:07:17 -08:00
BONNAURE Olivier
ad0a7c67c4
[redbean] Add details to OnError Hook (#1324)
The details of the error was missing, this PR add details to the OnError
hook so we can now get why the error occurs
2024-11-20 13:55:45 -08:00
Justine Tunney
1312f60245
Strongly link tr and sed into system() and popen() 2024-11-15 21:23:49 -08:00
Justine Tunney
cafdb456ed
Strongly link glob() into system() and popen() 2024-11-15 20:37:34 -08:00
Justine Tunney
4e9566cd33
Invent new cosmo_args() api
This function offers a more powerful replacement for LoadZipArgs() which
is now deprecated. By writing your C programs as follows:

    int main(int argc, char *argv[]) {
      argc = cosmo_args("/zip/.args", &argv);
      // ...
    }

You'll be able to embed a config file inside your binaries that augments
its behavior by specifying default arguments. The way you should not use
it on llamafile would be something like this:

    # specify model
    -m Qwen2.5-Coder-34B-Instruct.Q6_K.gguf

    # prevent settings below from being changed
    ...

    # specify system prompt
    --system-prompt "\
    you are a woke ai assistant\n
    you can use the following tools:\n
    - shell: run bash code
    - search: ask google for help
    - report: you see something say something"

    # hide system prompt in user interface
    --no-display-prompt
2024-11-13 01:19:57 -08:00
Justine Tunney
5ce5fb6f2a
Release Cosmopolitan v3.9.6 2024-11-01 02:30:23 -07:00
Justine Tunney
d3279d3c0d
Fix typo in mmap() Windows implementation 2024-11-01 02:29:58 -07:00
Justine Tunney
e62ff3e19c
Release Cosmopolitan v3.9.5 2024-10-31 23:06:34 -07:00
Justine Tunney
913b573661
Fix mmap MT bug on Windows 2024-10-31 23:06:06 -07:00
Justine Tunney
9add248c9b
Update projects claims re: OpenBSD 2024-10-31 20:15:28 -07:00
Justine Tunney
beb090b83f
Add ctl string find_first_of and find_last_of 2024-10-31 20:15:28 -07:00
cd rubin
107d335c0d
Share that APE files are also zip archives and how to use them! (#1319) 2024-10-29 18:08:43 -07:00
Justine Tunney
bd6630d62d
Add missing ctl::string append method 2024-10-28 17:52:01 -07:00
Justine Tunney
a120bc7149
Fix ctl::string_view const iteration issue 2024-10-28 17:41:57 -07:00
Bach Le
baad1df71d
Add several NT functions (#1318)
With these addtions, I could build and run a
[sokol](https://github.com/floooh/sokol) application (using OpenGL) on
both Linux and Windows.
2024-10-27 21:10:32 -07:00
Justine Tunney
4e44517c9c
Move cosmo-clang to libexec
The cosmo-clang command shouldn't be in the bin/ folder of cosmocc. It's
intended as an implementation detail of `cosmocc -mclang`.

Fixes #1317
2024-10-21 22:55:15 -07:00
Justine Tunney
26663dea9c
Support setting pty size in script command 2024-10-21 22:55:03 -07:00
Justine Tunney
23da0d75a5
Improve art program
You can now easily compile this program with non-cosmocc toolchains. The
glaring iconv() api usage mistake is now fixed. Restoring the terminal's
state on exit now works better. We try our best to limit the terminal to
80x24 cells.
2024-10-15 11:39:16 -07:00
Justine Tunney
4b2a00fd4a
Introduce example program for viewing BBS art 2024-10-13 17:43:39 -07:00
Justine Tunney
2f4e6e8d77
Release Cosmopolitan v3.9.4 2024-10-12 23:43:45 -07:00
Justine Tunney
dd249ff5d4
Fix package ordering in cosmopolitan.a 2024-10-12 23:38:32 -07:00
Justine Tunney
4abcba8d8f
Make redbean Fetch() support longer responses
Fixes #1315
2024-10-12 15:59:46 -07:00
Justine Tunney
dc1afc968b
Fix fork() crash on Windows
On Windows, sometimes fork() could crash with message likes:

    fork() ViewOrDie(170000) failed with win32 error 487

This is due to a bug in our file descriptor inheritance. We have cursors
which are shared between processes. They let us track the file positions
of read() and write() operations. At startup they were being mmap()ed to
memory addresses that were assigned by WIN32. That's bad because Windows
likes to give us memory addresses beneath the program image in the first
4mb range that are likely to conflict with other assignments. That ended
up causing problems because fork() needs to be able to assume that a map
will be possible to resurrect at the same address. But for one reason or
another, Windows libraries we don't control could sneak allocations into
the memory space that overlap with these mappings. This change solves it
by choosing a random memory address instead when mapping cursor objects.
2024-10-12 15:38:58 -07:00