Commit graph

1971 commits

Author SHA1 Message Date
Jōshin
ed8fadea37
Keep argv[0], add COSMOPOLITAN_PROGRAM_EXECUTABLE (#980)
* Introduce env.com

Handy tool for debugging environment issues.

* Inject path as COSMOPOLITAN_PROGRAM_EXECUTABLE

`argv[0]` was previously being used as a communication channel between
the loader and the binary, giving the binary its full path for use e.g.
in `GetProgramExecutableName`. But `argv[0]` is not a good channel for
this; much of what made 2a3813c6 so gross is due to that.

This change fixes the issue by preserving `argv[0]` and establishing a
new communication channel: `COSMOPOLITAN_PROGRAM_EXECUTABLE`.

The M1 loader will always set this as the first variable. Linux should
soon follow. On the other side, `GetProgramExecutableName` checks that
variable first. If it sees it, it trusts it as-is.

A lot of the churn in `ape/ape-m1.c` in this change is actually backing
out hacks introduced in 2a3813c6; the best comparison is:

    git diff 2a3813c6^..
2023-12-04 12:45:46 -08:00
Jōshin
2a3813c6cf
$prog.ape support (#977)
* ape loader: $prog.ape + login shell support

If the ape loader is invoked with `$0 = $prog.ape`, then it searches for
a `$prog` in the same directory as it and loads that. In particular, the
loader searches the `PATH` for an executable named `$prog.ape`, then for
an executable named `$prog` in the same directory. If the former but not
the latter is found, the search terminates with an error.

It also handles the special case of getting started as `-$SHELL`, which
getty uses to indicate that the shell is a login shell. The path is not
searched in this case, and the program location is read straight out of
the `SHELL` variable.

It is now possible to have `/usr/local/bin/zsh.ape` act as a login shell
for a `/usr/local/bin/zsh` αpε, insofar as the program will get started
with the 'correct' args. Unfortunately, many things break if `$0` is not
the actual full path of the executable being run; for example, backspace
does not update the display properly.

To work around the brokenness introduced by not having `$0` be the full
path of the binary, we cut the leading `-` out of `argv[0]` if present.
This gets the loader's behavior with `$prog.ape` up to par, but doesn't
tell login shells that they are login shells.

So we introduce a hack to accomplish that: if ape is run as `-$prog.ape`
and the shell is `$prog`, the binary that is loaded has a `-l` flag put
into its first argument.

As of this commit, αpε binaries can be used as login shells on OSX.

* if islogin, execfn = shell

Prior to this, execfn was not being properly set for login shells that
did not receive `$_`, which was the case for iTerm2 on Mac. There were
no observable consequences of this, but fixing it seems good anyway.

* Fix auxv location calculation

In the non-login-shell case, it was leaving a word of uninitialized
memory at `envp[i] + 1`. This reuses the previous calculation based
on `envp`.
2023-12-03 19:39:32 -08:00
Jōshin
8dd348067b
refs is atomic_size_t (#976)
The unassert covers all invalid values rather than a third of them.
2023-12-02 16:48:58 -08:00
Justine Tunney
450d9b1d43
Release Cosmopolitan v3.1.3 2023-12-01 10:29:59 -08:00
Justine Tunney
69ba516466
Revert "Re-enable apelink size optimization"
This reverts commit 7b3d7ee07b.

It broke llava-v1.5-7b-q4-server.llamafile on Windows.
2023-12-01 10:26:17 -08:00
Justine Tunney
9e9d2c0006
Release Cosmopolitan v3.1.2 2023-12-01 06:47:57 -08:00
Jōshin
a0f599ce12
Fix memory_order_release in __zipos_seek (#975) 2023-12-01 02:28:15 -08:00
Jōshin
d95d61b1af
Better zipos refcounts and atomic reads/seeks (#973)
* Better refcounting

Cribbed from [Rust Arc][1] and the [Boost docs][2]:

"""
Increasing the reference counter can always be done with
memory_order_relaxed: New references to an object can only be formed
from an existing reference, and passing an existing reference from one
thread to another must already provide any required synchronization.

It is important to enforce any possible access to the object in one
thread (through an existing reference) to happen before deleting the
object in a different thread. This is achieved by a "release" operation
after dropping a reference (any access to the object through this
reference must obviously happened before), and an "acquire" operation
before deleting the object.

It would be possible to use memory_order_acq_rel for the fetch_sub
operation, but this results in unneeded "acquire" operations when the
reference counter does not yet reach zero and may impose a performance
penalty.
"""

[1] https://moshg.github.io/rust-std-ja/src/alloc/arc.rs.html
[2] https://www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html

* Make ZiposHandle's pos atomic

Implements a somewhat stronger guarantee than POSIX specifies: reads and
seeks are atomic. They may be arbitrarily reordered between threads, but
each one happens all the way and leaves the fd in a consistent state.

This is achieved by "locking" pos in __zipos_read by storing SIZE_MAX to
pos during the operation, so only one can be in-flight at a time. Seeks,
on the other hand, just update pos in one go, and rerun if it changed in
the meantime.

I used `LIKELY` / `UNLIKELY` to pessimize the concurrent case; hopefully
that buys back some performance.
2023-12-01 01:01:03 -08:00
Jōshin
f0bfabba07
Revert "Remove noop __zipos_postdup" (#974)
`close` does not modify the fd table, so `rc` can potentially have a
stale zipos object sitting on it. As such, we need `__zipos_postdup`
there.
2023-12-01 00:51:01 -08:00
Jōshin
7845495a93
Get tool/cosmocc/package.sh working on M1 (#970)
On aarch64 hosts, MODE= is changed to MODE=aarch64, so o// targets don't
work. So On aarch64, get apelink.com out of o/aarch64/. Also prepend ape
when calling it. And finally, fetch with curl when wget isn't installed.
2023-12-01 00:08:55 -08:00
Jōshin
d1a745c17c
Implement __zipos_dup (#972)
* Implement __zipos_dup

Makes ZiposHandle reference-counted by an `rc` field in a union with its
freelist `next` pointer. The functions `__zipos_free` and `__zipos_keep`
function as incref/decref for it. Adds `__zipos_postdup` to fix metadata
on file descriptors after dup-like operations, and adds zipos support to
`sys_dup_nt` + `sys_close_nt`.

* Remove noop __zipos_postdup

rc is never a zipos file because it is always a previously unused file
descriptor. fd is never a zipos file because that case has been handled
above by __zipos_fcntl.
2023-12-01 00:08:30 -08:00
Justine Tunney
6556dd2673
Add $(uniq token...) native function to Make
This reduces mono repo `make` latency from 336ms to 226ms.
2023-11-30 21:02:16 -08:00
Justine Tunney
14bf57180f
Import GNU Make 4.4.1
Landlock Make hasn't been working well on AARCH64 systems. Let's do this
over the right way, using our new build tools.
2023-11-30 20:54:52 -08:00
Justine Tunney
9315ebbfd9
Fix MODE=tinylinux build 2023-11-30 15:38:08 -08:00
mataha
879bb84244
Update quickjs (#890)
Includes additional fixes from main repo's unmerged PRs:

 - quickjs#132: Fix undefined behavior: shift 32 bits for uint32_t in bf_set_ui
 - quickjs#171: Fix locale-aware representation of hours in Date class
 - quickjs#182: Fix stack overflow in CVE-2023-31922
2023-11-30 10:51:16 -08:00
Justine Tunney
4b7ba9a4c5
Fix some bugs with dup2() and ZipOS
On UNIX if dup2(newfd) was a ZipOS file descriptor, then its resources
weren't being released, and the newly created file descriptor would be
mistaken for ZipOS due to its memory not being cleared. On Windows, an
issue also existed relating to newfd resources not being released.
2023-11-30 10:10:02 -08:00
Justine Tunney
26c70e08bf
Make it possible to package.sh cosmocc w/ modes 2023-11-30 07:12:15 -08:00
Justine Tunney
20938a45e6
Add appropriate chmod to README instructions
See Mozilla-Ocho/llamafile#13
2023-11-30 03:35:17 -08:00
Justine Tunney
7b3d7ee07b
Re-enable apelink size optimization
I'm no longer able to reproduce the PE import table corruption that was
previously observed. Since this optimization shaves up to 64kb off each
fat APE binary we build, it's worth turning this back on again, to wait
and see if something breaks, and if so, fix it. At least until the next
release is shipped.

See #965
2023-11-29 05:41:23 -08:00
Justine Tunney
ff955aaa01
Make memcmp() and memchr() go fast again
Readahead within the specified size is legal, even if it overlaps a page
boundary; it's the fault of the caller if that causes a segfault.
2023-11-29 05:17:21 -08:00
Justine Tunney
70155df7a9
Avoid linking win32 signals impl
This shaves ~4kb off o/tiny/examples/hello2.com by avoiding linking the
WIN32 signals polyfill unless sigaction() is being used.

See #965
2023-11-29 04:09:31 -08:00
Justine Tunney
2b960bb249
Exclude strace from MODE=tiny builds
This change gets o/tinylinux/examples/hello2.com back down to 8kb in
size which had been unintentionally bloated to 40kb in recent months

See #965
2023-11-29 03:45:54 -08:00
Justine Tunney
b7e1dc81c2
Release Cosmopolitan v3.1.1 2023-11-29 02:19:56 -08:00
Justine Tunney
41c06c6325
Make cosmocc 100x faster on MacOS
This change upgrades to superconfigure z0.0.23 which fixes an issue
where the compiler had harmless /home/... paths baked-in, which are
normally only present in the build environment, and usually skipped
over. Sadly on MacOS calling fstatat() on these paths would lead to
cloud file system ops that caused system calls to take a long time.
That's problematic, since cosmocc needs to be a 100% local command.
2023-11-29 01:48:17 -08:00
Justine Tunney
4427581a05
Cleanup inline locking code 2023-11-29 00:36:22 -08:00
Justine Tunney
96185e1ac0
Fix MODE=aarch64 cross-compiling
This change fixes a regression that happened some time ago when building
for AARCH64 using the vendored toolchain rather than cosmocc. The errors
that would show up `Relocations in generic ELF (EM: 62)` have been fixed
2023-11-28 22:40:06 -08:00
Justine Tunney
ca5e361919
Don't show (win32 error 0) 2023-11-28 21:30:20 -08:00
Justine Tunney
9cad66aa11
Freshen bootstrap binaries for MacOS + SIP 2023-11-28 21:18:31 -08:00
Justine Tunney
03de761890
Update cosmocc to use superconfigure z0.0.23 2023-11-28 20:04:15 -08:00
Justine Tunney
43de12db55
Introduce forkpty() 2023-11-28 19:59:57 -08:00
Justine Tunney
fa20edc44d
Reduce header complexity
- Remove most __ASSEMBLER__ __LINKER__ ifdefs
- Rename libc/intrin/bits.h to libc/serialize.h
- Block pthread cancelation in fchmodat() polyfill
- Remove `clang-format off` statements in third_party
2023-11-28 14:39:42 -08:00
Justine Tunney
96f979dfc5
Rename makefiles BUILD.mk
This way they appear at the top of directory listings.
2023-11-28 11:21:08 -08:00
Paul Kulchenko
0bffd09433
Allow redbean to accept --assimilate option without complaining (#916)
This allows using --assimilate on Windows or already assimilated binary.
Ref #356.
2023-11-19 19:02:30 -08:00
Stephen Gregoratto
cc5c5319bf
Linux: Add cachestat, fchmodat2 syscalls (#958) 2023-11-19 19:01:20 -08:00
Justine Tunney
69faf1b403
Release Cosmpolitan v3.1 2023-11-18 20:40:35 -08:00
Justine Tunney
e4dea37b8e
Make clock_nanosleep() cancel faster 2023-11-18 18:12:09 -08:00
Justine Tunney
0c89516ac5
Fix unzip warning with apelink generated symtabs 2023-11-18 17:15:22 -08:00
Justine Tunney
3e6d536822
Correct misunderstanding with zip64 extra records 2023-11-18 14:35:57 -08:00
Justine Tunney
dbd8176ea8
Add CPU_COUNT_S() 2023-11-18 12:38:30 -08:00
Justine Tunney
545a8f4cb0
Add more sched.h content 2023-11-18 08:08:15 -08:00
Justine Tunney
f7cfe03888
Fix dlopen() for FreeBSD and NetBSD 2023-11-18 04:35:48 -08:00
Justine Tunney
2c1efd3d78
Fix Python build break 2023-11-18 02:32:01 -08:00
Matheus Moreira
3ac473df3b
Floating point parsing support for scanf family (#924) 2023-11-18 02:25:36 -08:00
Justine Tunney
8caf1b48a9
Improve time/sleep accuracy on Windows
It's now almost as good as Linux thanks to a Windows 8+ API.
2023-11-18 01:57:44 -08:00
Justine Tunney
72ac5f18d9
Fix dash compatible syntax issue 2023-11-17 16:47:47 -08:00
Justine Tunney
2c9d2943d6
Introduce AddDllDirectory() 2023-11-17 10:35:34 -08:00
Justine Tunney
32b97f2d25
Improve execve() path argument munging
Munging of paths passed inside the system() interpreter command is no
longer supported. You have to pass your paths to posix_spawn() or the
execve() family of functions if you want them to be munged. The first
three characters must match `^/[a-z]/` in which case, it'll be turned
into a DOS-style drive path with backslashes.
2023-11-17 09:59:03 -08:00
Justine Tunney
529cb4817c
Improve dlopen() on Apple Silicon
- Introduce MAP_JIT which is zero on other platforms
- Invent __jit_begin() and __jit_end() which wrap Apple's APIs
- Runtime dispatch to sys_icache_invalidate() in __clear_cache()
2023-11-17 02:33:14 -08:00
Justine Tunney
7a9e176ecf
Improve debug binary location detection 2023-11-17 00:07:39 -08:00
Justine Tunney
68c7c9c1e0
Clean up some code
- Use good ELF technique in cosmo_dlopen()
- Make strerror() conform more to other libc impls
- Introduce __clear_cache() and use it in cosmo_dlopen()
- Remove libc/fmt/fmt.h header (trying to kill off LIBC_FMT)
2023-11-16 17:31:07 -08:00