Commit graph

386 commits

Author SHA1 Message Date
invlpg
0a2f6a048b fix msghdr and cmsghdr on non-Linux targets, document musl behaviour
The following assertions fail on non-Linux platforms after c0c2010535
which inserted padding based on musl definitions. This padding only
exists on musl to workaround a discrepancy betweeen the POSIX API and
Linux ABI, and is incorrect on other POSIX operating systems.

This change makes the padding musl-only, and documents the reason it
exists. With this change, the assertions pass on Linux and FreeBSD
targets. The corresponding definitions on other targets line up with the
POSIX and FreeBSD ones, so they should work there too.

```zig
const std = @import("std");
const assert = std.debug.assert;
const msghdr = std.c.msghdr;
const cmsghdr = std.c.cmsghdr;

const c = @cImport({
    @cInclude("sys/socket.h");
});

comptime {
    assert(@offsetOf(msghdr, "iovlen") == @offsetOf(c.msghdr, "msg_iovlen"));
    assert(@offsetOf(msghdr, "controllen") == @offsetOf(c.msghdr, "msg_controllen"));
    assert(@offsetOf(msghdr, "control") == @offsetOf(c.msghdr, "msg_control"));
    assert(@offsetOf(msghdr, "flags") == @offsetOf(c.msghdr, "msg_flags"));

    assert(@sizeOf(msghdr) == @sizeOf(c.msghdr));

    assert(@offsetOf(cmsghdr, "len") == @offsetOf(c.cmsghdr, "cmsg_len"));
    assert(@offsetOf(cmsghdr, "level") == @offsetOf(c.cmsghdr, "cmsg_level"));

    assert(@sizeOf(cmsghdr) == @sizeOf(c.cmsghdr));
}
```
2026-03-02 17:46:26 +01:00
John Benediktsson
14d06330e0 std.c: fix SO_LINGER on darwin and define SO_LINGER_SEC 2026-02-20 05:36:46 +01:00
Iku Iwasa
8c83acd3d4 Add missing EV flags for NetBSD
- Add FLAG1, ERROR and EOF.
2026-02-15 23:08:11 +01:00
Jacob Young
2fa1a78491 Io.Dispatch: introduce grand central dispatch io impl 2026-02-13 12:29:40 -05:00
Meghan Denny
13f31bb3c3 std/c: glibc has supported arc4random_buf since 2.36 2026-02-11 15:29:51 -08:00
Andrew Kelley
59073484ba std.Io: add ioctl / DeviceIoControlFile API 2026-02-01 01:08:01 -08:00
GasInfinity
d5c3bf25dc
feat(std.c): add _Exit 2026-01-24 20:41:15 +01:00
Lukas Lalinsky
5f950884a1 std.c: add IPPROTO_RAW for Darwin platforms
IPPROTO_RAW (255) was missing from the Darwin/macOS IPPROTO struct,
even though it is defined in system headers and supported by the platform.
This is a commonly used protocol for raw IP sockets.
2026-01-24 12:26:00 +01:00
GasInfinity
9d3e9054a7
feat(std.c): add wasi definition for utsname 2026-01-21 00:06:42 +01:00
Alex Rønne Petersen
fb66d7712c
std.c: fix W functions for NetBSD
I'm not sure where the old logic came from but it certainly didn't match NetBSD
10.1 system headers, and was causing the build system to see incorrect exit
status information for processes that were expected to crash (e.g. SIGABRT).
2026-01-19 13:15:57 +01:00
Alex Rønne Petersen
27685bcf20 std.c: fix O constants for FreeBSD
closes https://codeberg.org/ziglang/zig/issues/30861
2026-01-17 12:13:20 +01:00
Andrew Kelley
d417441f4f Merge pull request 'libc: use common implementations for wchar.h' (#30850) from GasInfinity/zig:libc-wchar into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30850
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-17 00:58:01 +01:00
Andrew Kelley
c917f619f0 std: fix compilation failures on various targets 2026-01-15 14:18:20 -08:00
Andrew Kelley
bed7bc37c4 std.File.MemoryMap updates
- change offset to u64
- make len non-optional
- make write take a file_size parameter
- std.Io.Threaded: introduce disable_memory_mapping flag to force it to
  take the fallback path.

Additionally:

- introduce BlockSize to File.Stat. On Windows, based on cached call to
  NtQuerySystemInformation. On unsupported OS's, set to 1.
- support File.NLink on Windows. this was available the whole time, we
  just didn't see the field at first.
- remove EBADF / INVALID_HANDLE from reading/writing file error sets
2026-01-15 14:18:20 -08:00
GasInfinity
e22bf480aa
feat(libzigc): use common implementation for wchar.h
* but does not fully implement it (missing locale/unicode tables)
* also removes their musl/mingw implementation
2026-01-15 18:01:20 +01:00
Matthew Lugg
85cac9e5b6 std: use sigaltstack for default segfault handler
This allows stack overflows to print stack traces. The size of the
sigaltstack (and whether it is actually set) can be configured by
setting `std.Options.signal_stack_size`.

The default value for the signal stack size was chosen experimentally by
doubling the value required to get stack traces on stack overflow with
the self-hosted x86_64 backend. While some targets may typically use
more stack space than x86_64-linux, the self-hosted x86_64 backend is
quite wasteful with stack at the moment, making it a fair benchmark.
Executables produced by the LLVM backend should have lower stack usage.
2026-01-13 07:24:49 +01:00
Carl Åstholm
0a8e8e67d7 Misc. std.Io fixes for WASI and Emscripten
- Corrects WASI `UTIME_*` definitions now that the libc build has been
  fixed (see previous commit), and adds the corresponding definitions
  for Emscripten which were missing.
- Fixes `dirReadUnimplemented()`, which didn't compile.
- Prevents a dependency on `pthread_kill` from being pulled in in
  single-threaded Emscripten builds, where it isn't defined.

With these changes, Emscripten can now participate in juicy main.
2026-01-12 22:38:21 +01:00
Andrew Kelley
4d6d2922b8 std: move memory locking and memory protection to process
and introduce type safety for posix.PROT (mmap, mprotect)

progress towards #6600
2026-01-09 13:52:00 -08:00
Linus Groh
27039a014c std.c: Make clock_nanosleep available on serenity
Follow-up to https://codeberg.org/ziglang/zig/pulls/30746.

The TIMER_ABSTIME value was adjusted to match other systems in
https://github.com/SerenityOS/serenity/pull/26543.
2026-01-09 09:37:49 +01:00
Andrew Kelley
b0570b807f Merge pull request 'std.Io.Threaded: clock_nanosleep is not linux-only' (#30746) from clock_nanosleep into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30746
2026-01-08 21:00:36 +01:00
Andrew Kelley
1face9ad78 std.c.SIG: make it non-exhaustive
54a84964f8 did this for std.os.linux.SIG
but I neglected to also do it for std.c.SIG
2026-01-08 20:52:42 +01:00
Andrew Kelley
130fc7ef75 std.c: use {} rather than void for absent functions 2026-01-07 21:13:16 -08:00
Andrew Kelley
4319c8924e std.Io.Threaded: clock_nanosleep is not linux-only
clock_nanosleep is specified by POSIX but not implemented on these
hereby shamed operating systems:
* macOS
* OpenBSD (which defines TIMER_ABSTIME for some reason...?)
2026-01-07 21:13:16 -08:00
GasInfinity
335c0fcba1 feat(libzigc): add div, ldiv, lldiv and imaxdiv
* also remove musl implementation
2026-01-07 21:53:40 +01:00
Andrew Kelley
06130c5e61 std.Io.Threaded: set O_DIRECTORY along with O_TMPFILE 2026-01-05 20:31:31 -08:00
Michael Dusan
6ab1159e81 netbsd: use correct symbol for lwp_park 2026-01-06 05:27:54 +01:00
Andrew Kelley
32af0f6154 std: move child process APIs to std.Io
this gets the build runner compiling again on linux

this work is incomplete; it only moves code around so that environment
variables can be wrangled properly. a future commit will need to audit
the cancelation and error handling of this moved logic.
2026-01-04 00:27:08 -08:00
Matthew Lugg
e57c557ad4
std.Io.Threaded: hugely improve Windows and NetBSD support
The most interesting thing here is the replacement of the pthread futex
implementation with an implementation based on thread park/unpark APIs.
Thread parking tends to be the primitive provided by systems which do
not have a futex primitive, such as NetBSD, so this implementation is
far more efficient than the pthread one. It is also useful on Windows,
where `RtlWaitOnAddress` is itself a userland implementation based on
thread park/unpark; we can implement it ourselves including support for
features which Windows' implementation lacks, such as cancelation and
waking a number of waiters with 1<n<infinity.

Compared to the pthread implementation, this thread-parking-based one
also supports full robust cancelation. Thread parking also turns out to
be useful for implementing `sleep`, so is now used for that on Windows
and NetBSD.

This commit also introduces proper cancelation support for most Windows
operations. The most notable omission right now is DNS lookups through
`GetAddrInfoEx`, just because they're a little more work due to having
a unique cancelation mechanism---but the machinery is all there, so I'll
finish gluing it together soon.

As of this commit, there are very few parts of `Io.Threaded` which do
not support full robust cancelation. The only ones which actually really
matter (because they could block for a prolonged period of time) are DNS
lookups on Windows (as discussed above) and futex waits on WASM.
2026-01-03 15:45:10 +00:00
Matthew Lugg
04226193cc
Revert "Use mmap std.heap.page_allocator impl when compiling for Wasm + libc"
This reverts commit c9fa8e46df.

This commit was failing CI checks. This failure was unfortunately not
noticed before merge, due in part to the build runner bug fixed in the
last commit.
2026-01-03 14:52:17 +00:00
Carl Åstholm
c9fa8e46df Use mmap std.heap.page_allocator impl when compiling for Wasm + libc
When linking libc, it should be the libc that manages the heap. The main
Wasm memory might have been configured as non-growable, which makes
`WasmAllocator` a poor default and causes the common `DebugAllocator`
use case fail with OOM errors unless the user uses `std_options` to
override the default page allocator. Additionally, on Emscripten,
growing Wasm memory without notifying the JS glue code will cause array
buffers to get detached and lead to spurious crashes.
2026-01-02 16:55:11 -08:00
Michael Dusan
4b26c49076
dragonfly: make test lib/std/std.zig pass 2025-12-29 22:29:56 -05:00
Michael Dusan
09f06082f0
openbsd: make test lib/std/std.zig pass
* According to OpenBSD's getdents docs indicate the buffer must be
  greater or or equal to the block size associated with the file and to
  refer to stat(2).
* Use S_BLKSIZE, which is 512, instead of @sizeOf(std.c.dirent), which is 280.
* Oddly the other BSDs are not this picky.
2025-12-29 21:40:02 -05:00
Michael Dusan
51728f1053
openbsd: add timespec OMIT and NOW definitions 2025-12-29 21:06:08 -05:00
Michael Dusan
0ae629ee22 netbsd: use correct symbol for wait4 2025-12-28 20:20:43 +01:00
Andrew Kelley
c0809c9b68 std: add more timespec OMIT and NOW definitions 2025-12-27 11:08:56 -08:00
Michael Dusan
486ed907f7 dragonfly: define std.c.nlink_t 2025-12-23 22:15:11 -08:00
Andrew Kelley
3e624e17a4 std: fix compilation errors on FreeBSD 2025-12-23 22:15:11 -08:00
Andrew Kelley
405db921dc std: fix compilation targeting WASI 2025-12-23 22:15:11 -08:00
Andrew Kelley
f3723b42e1 std.Io: add unimplemented hard link API to File and Dir 2025-12-23 22:15:10 -08:00
Andrew Kelley
9407ad8516 std.c.utimensat: add const 2025-12-23 22:15:09 -08:00
Alex Rønne Petersen
18db762bb8
std.c: openbsd does not have SIGRTMIN/SIGRTMAX 2025-12-16 06:24:58 +01:00
Alex Rønne Petersen
56add8374c
std.c: remove comptime asserts of siginfo_t size
These serve no purpose other than to verify that the compiler is doing layout
correctly, and this is clearly not the place for that.
2025-12-16 06:24:54 +01:00
Stephen Gregoratto
6216922a9d Linux: Nuke Stat bits in favour of statx
Maintaining the POSIX `stat` bits for Zig is a pain. The order and
bit-length of members differ between all architectures, and int types
can be signed or unsigned. The libcs deal with this by introducing the
own version of `struct stat` and copying the kernel structure members to
it. In the case of glibc, they did it twice thanks to the largefile
transition!

In practice, the project needs to maintain three versions of `struct
stat`:
- What the kernel defines.
- What musl wants for `struct stat`.
- What glibc wants for `struct stat64`. Make sure to use `fstatat64`!

This isn't as simple as running `zig translate-c`. In #21440 I had to:
- Compile toolchains for each arch+glibc/musl combo.
- Create a test `fstat` program with/without `FILE_OFFSET_BITS=64`.
- Dump the value for `struct stat`.
- Stare at `std.os.linux`/`std.c` and cry.
- Add some missing padding.

The fact that so many target checks in the `linux` and `posix` tests
exist is most likely due to writing to padding bits and failing later.

The solution to this madness is `statx(2)`:
- It takes a single structure that is the same for all arches AND libcs.
- It uses a custom timestamp format, but it is 64-bit ready.
- It gives the same info as `fstatat(2)` and more!
- Unlike `fstatat(2)`, you can request a subset of the info required
  based on passing a mask.

It's so good that modern Linux arches (e.g. riscv) don't even implement
`stat`, with the libcs using a generic `struct stat` and copying from
`struct statx`.

Therefore, this commit rips out all the `stat` bits from `std.os.linux`
and `std.c`. `std.posix.Stat` is now `void`, and calling
`std.posix.*stat` is an compile-time error. A wrapper around `statx` has
been added to `std.os.linux`, and callers have been upgraded to use it.
Tests have also been updated to use `statx` where possible.

While I was here, I converted the mask and file attributes to be packed
struct bitfields. A nice side effect is checking that you actually
recieved the members you asked for via `Statx.mask`, which I have used
by adding `assert`s at specific callsites.
2025-12-14 01:41:47 +01:00
Matthew Lugg
65922a2d43
std: make stack unwinding faster on macOS
https://github.com/ziglang/zig/issues/26027#issuecomment-3571227050
tracked some bad performance in `DebugAllocator` on macOS down to a
function in dyld which `std.debug.SelfInfo` was calling into. It turns
out `dladdr`'s symbol lookup logic is horrendously slow (looking at its
source code, it appears to be doing a *linear scan* over all symbols in
the image?!). However, we don't actually need the symbol, so we want to
try and avoid this logic.

Luckily, dyld has more precise APIs for what we need! Unluckily, Apple,
in their infinite wisdom, decided they should be deprecated in favour of
`dladdr`, despite the latter being several times slower (and by "several
times", I have measured a 50x slowdown on repeated calls to `dladdr`
compared to the other API). But luckily again, the deprecated APIs are
still exposed.

So, after a careful analysis of the situation (reading dyld code and
cursing Apple engineers), I think it makes sense to just use these
deprecated APIs for now. If they ever go away, we can write our own
cache for this data to bypass Apple's awfully slow code, but I suspect
these functions will stick around for the foreseeable future.

Uh, and if `_dyld_get_image_header_containing_address` goes away,
there's also `dyld_image_header_containing_address`, which is a
seemingly identical function, exported by dyld just the same, but with a
separate (functionally identical) implementation, and not documented in
the public header file. Apple work in mysterious ways, I guess.
2025-12-06 10:41:42 +00:00
Alex Rønne Petersen
2659fadb95
std.c: add rusage for dragonfly, netbsd, openbsd 2025-12-04 03:46:36 +01:00
Andrew Kelley
39ac40209b std.Io.Threaded: use musl's beautiful pthread_cancel semantics 2025-12-01 19:17:52 -08:00
rpkak
6b4f45f782 system specific errno 2025-11-20 15:03:23 -08:00
Lukas Lalinsky
73f863a6fb Fix AI/NI flag definitions for BSD systems
Add missing AI flags for NetBSD and OpenBSD:
- NetBSD: Add AI.SRV flag at bit 11 (0x00000800)
- OpenBSD: Add AI.EXT flag at bit 3 and AI.FQDN flag at bit 5

Add missing NI (getnameinfo) flag definitions for all BSDs and Darwin:
- FreeBSD/Haiku: NOFQDN, NUMERICHOST, NAMEREQD, NUMERICSERV, DGRAM, NUMERICSCOPE
- DragonFly/NetBSD: Same flags with NUMERICSCOPE at bit 6 (deprecated WITHSCOPEID at bit 5 is skipped)
- OpenBSD: NUMERICHOST, NUMERICSERV, NOFQDN, NAMEREQD, DGRAM (no NUMERICSCOPE)
- macOS/Darwin: NOFQDN, NUMERICHOST, NAMEREQD, NUMERICSERV, DGRAM, NUMERICSCOPE at bit 8 (deprecated WITHSCOPEID at bit 5 is skipped)

References:
- NetBSD: https://github.com/NetBSD/src/blob/trunk/include/netdb.h
- OpenBSD: https://github.com/openbsd/src/blob/master/include/netdb.h
- FreeBSD: https://github.com/freebsd/freebsd-src/blob/master/include/netdb.h
- DragonFly BSD: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/include/netdb.h
- Haiku: https://github.com/haiku/haiku/blob/master/headers/posix/netdb.h
- macOS/Darwin: https://github.com/apple-oss-distributions/Libinfo/blob/main/lookup.subproj/netdb.h
2025-11-17 19:01:39 +01:00
Meghan Denny
d07360f999 std.c: implement rusage for freebsd
Reference: e0c41af925/sys/sys/resource.h (L70)
2025-11-16 06:15:54 +01:00
Lukas Lalinsky
6bdea35ce5 Fix std.c.MSF.SYNC for freebsd, openbsd, dragonfly 2025-11-14 18:02:53 +01:00