Commit graph

127 commits

Author SHA1 Message Date
mlugg
dae703d3c0
std.posix.abort: only trigger breakpoint on Windows if being debugged
Processes should reasonably be able to expect their children to abort
with typical exit codes, rather than a debugger breakpoint signal. This
flag in the PEB is what would be checked by `IsDebuggerPresent` in
kernel32, which is the function you would typically use for this
purpose.

This fixes `test-stack-trace` failures on Windows, as these tests were
expecting exit code 3 to indicate abort.
2025-09-30 13:44:55 +01:00
mlugg
a18fd41064
std: rework/remove ucontext_t
Our usage of `ucontext_t` in the standard library was kind of
problematic. We unnecessarily mimiced libc-specific structures, and our
`getcontext` implementation was overkill for our use case of stack
tracing.

This commit introduces a new namespace, `std.debug.cpu_context`, which
contains "context" types for various architectures (currently x86,
x86_64, ARM, and AARCH64) containing the general-purpose CPU registers;
the ones needed in practice for stack unwinding. Each implementation has
a function `current` which populates the structure using inline
assembly. The structure is user-overrideable, though that should only be
necessary if the standard library does not have an implementation for
the *architecture*: that is to say, none of this is OS-dependent.

Of course, in POSIX signal handlers, we get a `ucontext_t` from the
kernel. The function `std.debug.cpu_context.fromPosixSignalContext`
converts this to a `std.debug.cpu_context.Native` with a big ol' target
switch.

This functionality is not exposed from `std.c` or `std.posix`, and
neither are `ucontext_t`, `mcontext_t`, or `getcontext`. The rationale
is that these types and functions do not conform to a specific ABI, and
in fact tend to get updated over time based on CPU features and
extensions; in addition, different libcs use different structures which
are "partially compatible" with the kernel structure. Overall, it's a
mess, but all we need is the kernel context, so we can just define a
kernel-compatible structure as long as we don't claim C compatibility by
putting it in `std.c` or `std.posix`.

This change resulted in a few nice `std.debug` simplifications, but
nothing too noteworthy. However, the main benefit of this change is that
DWARF unwinding---sometimes necessary for collecting stack traces
reliably---now requires far less target-specific integration.

Also fix a bug I noticed in `PageAllocator` (I found this due to a bug
in my distro's QEMU distribution; thanks, broken QEMU patch!) and I
think a couple of minor bugs in `std.debug`.

Resolves: #23801
Resolves: #23802
2025-09-30 13:44:54 +01:00
mlugg
c2ada49354
replace usages of old std.debug APIs
src/crash_handler.zig is still TODO though, i am planning bigger changes there
2025-09-30 13:44:51 +01:00
mlugg
67fa5664b7
std.posix: mark getcontext as unsupported by default 2025-09-30 13:44:51 +01:00
rpkak
bc512648db use copy_file_range syscall on linux 2025-09-24 03:08:12 +02:00
John Benediktsson
37ecaae639
std.fmt: migrate bufPrintZ to bufPrintSentinel (#25260) 2025-09-19 05:02:22 +00:00
Andrew Kelley
d6b4e1918b
Merge pull request #25195 from blblack/netdefs
std: Add several sockopt-related constants and structs
2025-09-17 21:43:23 -07:00
Andrew Kelley
d2e4ad613b
Merge pull request #25217 from blblack/setsiderr
std.os.linux.setsid(): return raw syscall0 result
2025-09-17 21:14:08 -07:00
Justus Klausecker
9b4cae4750 std.posix.ptrace: support more platforms more correctly 2025-09-13 14:35:19 +02:00
Brandon Black
04071d64bb std.os.linux.setsid(): return raw syscall0 result
When not linking libc on 64-bit Linux and calling posix.setsid(),
we get a type error at compile time inside of posix.errno().  This
is because posix.errno()'s non-libc branch expects a usize-sized
value, which is what all the error-returning os.linux syscalls
return, and linux.setsid() instead returned a pid_t, which is only
32 bits wide.

This and the other 3 pid-related calls just below it (getpid(),
getppid(), and gettid()) are the only Linux syscall examples here
that are casting their return values to pid_t. For the other 3
this makes sense: those calls are documented to have no possible
errors and always return a valid pid_t value.

However, setsid() actually can return the error EPERM, and
therefore needs to return the raw value from syscall0 for
posix.errno() to process like normal.

Additionally, posix.setsid() needs an @intCast(rc) for the success
case as a result, like most other such cases.
2025-09-12 07:19:01 -05:00
Brandon Black
7995697527 std: add IP, IPV6, IPTOS sockopt constants
Because these lists are very long in several cases and quite
varied, I opted to place them in the existing c/foo.zig files.

There are many other sets of network-related constants like this
to add over time across all the OSes.  For now I picked these
because I needed a few constants from each of these namespaces for
my own project, so I tried to flesh out these namespaces
completely as best I could, at least for basic sockopt purposes.

Note windows has some of these already defined in ws2_32 as
individual constants rather than contained in a namespacing
struct.  I'm not sure what to do with that in the long run (break
it and namespace them?), but this doesn't change the status quo
for windows in any case.
2025-09-09 17:01:20 -05:00
Brandon Black
0e45b9d5db std: add linger struct for SO.LINGER 2025-09-09 16:59:50 -05:00
Brandon Black
3e372f1994 std: add in_pktinfo and in6_pktinfo structs defs
in_pktinfo is only used on a few targets for the IP_PKTINFO
sockopt, as many BSDs use an alternate mechanism (IP_RECVDSTADDR)
that doesn't require a special struct.  in6_pktinfo is more
universal.
2025-09-09 16:59:50 -05:00
Brandon Black
c70521e7b1 std: Add SCM constants for socket control messages 2025-09-09 16:59:50 -05:00
Brandon Black
c4c6c01cc5 recvmsg: posix wrapper, void on windows
Also, added EPIPE to recvfrom() error set (it's a documented error
for unix and tcp sockets, at least), which recvmsg() largely
shares.  Windows has an odd, callback-based form of recvmsg() that
doesn't fit the normal interface here.
2025-09-08 14:45:51 -05:00
Brandon Black
79313d844f socketpair: posix wrapper, void on windows
socketpair is something like a pipe2() for sockets, and generally
only works for AF_UNIX sockets for most platforms.  Winsock2
explicitly does not support this call, even though it does have
AF_UNIX sockets.
2025-09-08 14:45:51 -05:00
Brandon Black
048f9126b8 Return runtime err for EISCONN when connecting
Fixes #25063
2025-09-03 22:01:45 +02:00
Linus Groh
deb46e1dd3 std.posix: Fix getRandomBytesDevURandom()
This is not exercised in CI because most platforms have a dedicated
implementation in getrandom().
2025-09-03 02:23:19 +01:00
Andrew Kelley
79f267f6b9 std.Io: delete GenericReader
and delete deprecated alias std.io
2025-08-29 17:14:26 -07:00
Sardorbek Imomaliev
01b5023868
drop NameTooLong from sysctlbynameZ error set (#24909) 2025-08-21 12:36:57 +02:00
Brandon Black
8248597a27 Add mlock syscalls to std.c and std.posix
Linux already gained the relevant syscalls and consts in #24473

The basic mlock() and munlock() are fairly universal across the
*nix world with a consistent interface, but are missing on wasi
and windows.

The mlockall() and munlockall() calls are not as widely supported
as the basic ones.  Notable non-implementers include darwin,
haiku, and serenity (and of course wasi and windows again).

mlock2() is Linux-only, as are its MLOCK flags.
2025-08-20 23:38:45 +02:00
kcbanner
95f57c3369 net: Always set WSA_FLAG_OVERLAPPED when creating Windows sockets. Rework send and receive logic to use overlapped I/O.
build-web: Remove the now-redundant supports_recv logic
2025-08-09 18:48:15 -04:00
TibboddiT
2cf15bee03
Fix some libc version checks for Bionic 2025-08-08 01:26:00 +00:00
Linus Groh
ce776d3245 std: Add serenity to more OS checks 2025-07-30 23:28:58 +01:00
Linus Groh
813a0f125e std.posix: Default ACCMODE to NONE for serenity
Unlike all other platforms where RDONLY is 0 it does not work as a
default for the O flags on serenity - various syscalls other than
'open', e.g. 'pipe', return EINVAL if unexpected bits are set in the
flags.
2025-07-30 23:27:32 +01:00
Andrew Kelley
733b208256 std.posix.ftruncate: handle NonResizable properly 2025-07-25 14:15:33 -07:00
Linus Groh
26bd74e87f std.posix: Fix ACCMODE values for serenity 2025-07-24 00:54:40 +01:00
Andrew Kelley
96cbdd145d std.fs.File.Reader: fix sendFile logic
it wasn't accounting for both writer and reader buffering
2025-07-21 20:00:45 -07:00
Brandon Black
e8a4e47d38 Add setsid to std.(c|posix)
The interface and errors for this seem to be very universal and
generic. Note Linux already has this defined as a syscall as well.
2025-07-14 07:26:49 +02:00
Carmen
5b4e982169
std.os.uefi.tables: ziggify boot and runtime services (#23441)
* std.os.uefi.tables: ziggify boot and runtime services

* avoid T{} syntax

Co-authored-by: linusg <mail@linusgroh.de>

* misc fixes

* work

* self-review quickfixes

* dont make MemoryMapSlice generic

* more review fixes, work

* more work

* more work

* review fixes

* update boot/runtime services references throughout codebase

* self-review fixes

* couple of fixes i forgot to commit earlier

* fixes from integrating in my own project

* fixes from refAllDeclsRecursive

* Apply suggestions from code review

Co-authored-by: truemedian <truemedian@gmail.com>

* more fixes from review

* fixes from project integration

* make natural alignment of Guid align-8

* EventRegistration is a new opaque type

* fix getNextHighMonotonicCount

* fix locateProtocol

* fix exit

* partly revert 7372d65

* oops exit data_len is num of bytes

* fixes from project integration

* MapInfo consistency, MemoryType update per review

* turn EventRegistration back into a pointer

* forgot to finish updating MemoryType methods

* fix IntFittingRange calls

* set uefi.Page nat alignment

* Back out "set uefi.Page nat alignment"

This backs out commit cdd9bd6f7f5fb763f994b8fbe3e1a1c2996a2393.

* get rid of some error.NotFound-s

* fix .exit call in panic

* review comments, add format method

* fix resetSystem data alignment

* oops, didnt do a final refAllDeclsRecursive i guess

* review comments

* writergate update MemoryType.format

* fix rename

---------

Co-authored-by: linusg <mail@linusgroh.de>
Co-authored-by: truemedian <truemedian@gmail.com>
2025-07-12 17:18:53 +00:00
Atlas Yu
2cda4cfb39 std.posix.send: should expect ConnectionRefused
Closes: #20219
2025-07-11 13:06:13 +02:00
Andrew Kelley
0e37ff0d59 std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API

make std.testing.expectFmt work at compile-time

std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.

Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
  - anytype -> *std.io.Writer
  - inferred error set -> error{WriteFailed}
  - options -> (deleted)
* std.fmt.Formatted
  - now takes context type explicitly
  - no fmt string
2025-07-07 22:43:51 -07:00
jaune
ef8db03d52 std.posix.accept: add WSAENOTSOCK 2025-07-07 00:26:21 +02:00
Brandon Black
aa1556156e std.posix.getsockopt: set option length correctly
Fixes #24293
2025-07-03 09:41:26 +02:00
Jacob Young
1f98c98fff x86_64: increase passing test coverage on windows
Now that codegen has no references to linker state this is much easier.

Closes #24153
2025-06-19 18:41:12 -04:00
Pat Tullmann
6eb5e56306 std.posix: Add sigrtmin() and sigrtmax()
For C code the macros SIGRTMIN and SIGRTMAX provide these values.  In
practice what looks like a constant is actually provided by a libc call.
So the Zig implementations are explicitly function calls.

glibc (and Musl) export a run-time minimum "real-time" signal number,
based on how many signals are reserved for internal implementation details
(generally threading).  In practice, on Linux, sigrtmin() is 35 on glibc
with the older LinuxThread and 34 with the newer NPTL-based
implementation.  Musl always returns 35.  The maximum "real-time" signal
number is NSIG - 1 (64 on most Linux kernels, but 128 on MIPS).

When not linking a C Library, Zig can report the full range of "rt"
signals (none are reserved by Zig).

Fixes #21189
2025-05-09 15:10:25 +02:00
Pat Tullmann
120c4789c3 sigset_t: sigemptyset() and sigfillset() are functions that return sigset_t
By returning an initialized sigset (instead of taking the set as an output
parameter), these functions can be used to directly initialize the `mask`
parameter of a `Sigaction` instance.
2025-04-30 20:32:04 -07:00
Pat Tullmann
f0aefa625b posix: remove empty_sigset
When linking a libc, Zig should defer to the C library for sigset
operations.  The pre-filled constants signal sets (empty_sigset,
filled_sigset) are not compatible with C library initialization, so remove
them and use the runtime `sigemptyset` and `sigfillset` methods to
initialize any sigset.
2025-04-30 20:32:04 -07:00
Pat Tullmann
d16079d79a posix.zig: export sigset_t and matching operations from system
Unify the C library sigset_t and Linux native sigset_t and the accessor
operations.

Add tests that the various sigset_t operations are working.  And clean up
existing tests a bit.
2025-04-30 20:32:04 -07:00
Chris Boesch
206bd1ced8
Merge pull request #23268 from chrboesch/i19875
std.posix: Added 'error.ProcessNotFound' where necessary
2025-04-14 22:20:44 +02:00
g-logunov
326f254972
std.posix.getenv: early-return comparison (#23265)
Fixes std.posix.getenv() being slower than musl getenv() even when linking libc
2025-04-11 12:44:18 -07:00
Alex Rønne Petersen
9bbac42886
Merge pull request #23478 from alexrp/bsd-versions
`std.Target`: Bump some minimum OS versions for BSDs
2025-04-08 18:27:39 +02:00
Stefan Weigl-Bosker
bcb4ba9afd
std.os.linux: use heap.pageSize() instead of MMAP2_UNIT 2025-04-07 13:37:01 +02:00
Alex Rønne Petersen
88e8e9fb91
std: Remove some FreeBSD version checks and resulting dead code.
We now require FreeBSD 13.4+.
2025-04-06 08:05:04 +02:00
Alex Rønne Petersen
9dfdf35032
Merge pull request #22337 from ruihe774/fix-app-mask
* std.os.linux: remove app_mask
* std.posix: on libc-less linux, block all signals in raise(), not just app_mask
2025-04-02 17:36:59 +02:00
Misaki Kasumi
aa832d6a6d std.os.linux: block all signals in raise 2025-04-02 18:50:14 +08:00
blurrycat
fb188c3d18
std.posix: add getuid()/geteuid() 2025-03-27 07:58:27 +00:00
Alex Rønne Petersen
db7db48028
Merge pull request #23339 from Iced-Sun/master
std.posix: update LFS64 interfaces for android bionic C
2025-03-26 23:25:08 +01:00
孙冰
0118912e2d
std.posix: update LFS64 interfaces for android bionic C 2025-03-26 20:00:05 +08:00
Pat Tullmann
2210c4c360 lib/std/posix: test ftruncate via std.fs.File.setEndPos()
Add a test for std.fs.File's `setEndPos` (which is a simple wrapper around
`std.posix.ftruncate`) to exercise some success and failure paths.

Explicitly check that the `ftruncate` length isn't negative when
interpreted as a signed value.  This avoids having to decode overloaded
`EINVAL` errors.

Add errno handling to Windows path to map INVALID_PARAMETER to FileTooBig.

Fixes #22960
2025-03-26 02:57:23 +01:00