Commit graph

14627 commits

Author SHA1 Message Date
Justus Klausecker
bbc77df3eb std.heap: delete ThreadSafeAllocator
We can keep ourselves safe from those threads perfectly well without you, thanks!
2026-02-26 21:20:34 +01:00
Saurabh Mishra
4e2cec265d Make std.PriorityQueue an unmanaged container (#31299)
## Summary of changes

+ Make adjustments to the `allocator` field and ensure the below tests pass:

  ```sh
  zig test lib/std/std.zig --zig-lib-dir lib
  zig build test-std -Dno-matrix --summary all
  ```

+ Rename `add` to `push` and `remove` to `pop` in methods and tests

+ Incorporate the functionality of `pop` in `popOrNull`, then rename the `popOrNull` to `pop` and update tests

+ Use `.empty` to set default field values and rename the `init` method to `initContext`

+ Improve variable types in tests: min heap uses the less than context function and max heap uses greater than context function

+ Remove the `dump` method as its not being used anywhere

+ Document methods `clearRetainingCapacity`, `clearAndFree`, `update`, and `ensureTotalCapacityPrecise`

Closes https://codeberg.org/ziglang/zig/issues/31298

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31299
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Saurabh Mishra <saurabh.m@proton.me>
Co-committed-by: Saurabh Mishra <saurabh.m@proton.me>
2026-02-26 21:09:52 +01:00
estevesnp
0b6b65b387 fix: Allow async and concurrent from Io.Select to accept non-void error unions 2026-02-26 20:49:05 +01:00
Justus Klausecker
de41123957 std.heap.ArenaAllocator: fix reset creating undersized nodes
Previously resetting with `retain_capacity < @sizeOf(Node)` would create
an invalid node. This is now fixed, plus `Node.size` now has its own `Size`
type that provides additional safety via assertions to prevent bugs like
this in the future.
2026-02-26 15:40:48 +01:00
Justus Klausecker
2fa2300ba4 std.heap.ArenaAllocator: Get rid of cmpxchg loop in hot path
This is achieved by bumping `end_index` by a large enough amount so that
a suitably aligned region of memory can always be provided. The potential
wasted space this creates is then recovered by a single cmpxchg. This is
always successful for single-threaded arenas which means that this version
still behaves exactly the same as the old single-threaded implementation
when only being accessed by one thread at a time. It can however fail when
another thread bumps `end_index` in the meantime. The observerd failure
rates under extreme load are:

2 Threads: 4-5%
3 Threads: 13-15%
4 Threads: 15-17%
5 Threads: 17-18%
6 Threads: 19-20%
7 Threads: 18-21%

This version offers ~25% faster performance under extreme load from 7 threads,
with diminishing speedups for less threads. The performance for 1 and 2
threads is nearly identical.
2026-02-26 15:30:55 +01:00
Andrew Kelley
e0173c2ce0 Merge pull request 'rework fuzz testing to be smith based' (#31205) from gooncreeper/zig:integrated-smith into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31205
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-25 20:23:36 +01:00
Kendall Condon
bb304796f4 optimize flate decompression
Matches now use memcpy and memset when possible.

Block loops have been rewritten to be more optimizer friendly.

Reworks Symbol and HuffmanDecoder
* Symbol now only includes the value and number of code bits.
  decodeSymbol returns only the value.
* HuffmanDecoder now takes the regular bits instead of the reversed.
* Code table construction now uses buckets instead of sorting.
* For linked codes, the value field of Symbol is now used as the next
  index. The actual value is the element index.
* InvalidCode is now detected only once with a special linked index.

Performance is 39.7% faster than before and 1.1% faster than gzip using
a sample created from compressing a tar of the src directory.
2026-02-25 20:05:48 +01:00
Justus Klausecker
a3a9dc111d std.heap.ArenaAllocator: make it threadsafe
Modifies the `Allocator` implementation provided by `ArenaAllocator` to be
threadsafe using only atomics and no synchronization primitives locked
behind an `Io` implementation.

At its core this is a lock-free singly linked list which uses CAS loops to
exchange the head node. A nice property of `ArenaAllocator` is that the
only functions that can ever remove nodes from its linked list are `reset`
and `deinit`, both of which are not part of the `Allocator` interface and
thus aren't threadsafe, so node-related ABA problems are impossible.

There *are* some trade-offs: end index tracking is now per node instead of
per allocator instance. It's not possible to publish a head node and its
end index at the same time if the latter isn't part of the former.

Another compromise had to be made in regards to resizing existing nodes.
Annoyingly, `rawResize` of an arbitrary thread-safe child allocator can
of course never be guaranteed to be an atomic operation, so only one
`alloc` call can ever resize at the same time, other threads have to
consider any resizes they attempt during that time failed. This causes
slightly less optimal behavior than what could be achieved with a mutex.
The LSB of `Node.size` is used to signal that a node is being resized.
This means that all nodes have to have an even size.

Calls to `alloc` have to allocate new nodes optimistically as they can
only know whether any CAS on a head node will succeed after attempting it,
and to attempt the CAS they of course already need to know the address of
the freshly allocated node they are trying to make the new head.
The simplest solution to this would be to just free the new node again if
a CAS fails, however this can be expensive and would mean that in practice
arenas could only really be used with a GPA as their child allocator. To
work around this, this implementation keeps its own free list of nodes
which didn't make their CAS to be reused by a later `alloc` invocation.
To keep things simple and avoid ABA problems the free list is only ever
be accessed beyond its head by 'stealing' the head node (and thus the
entire list) with an atomic swap. This makes iteration and removal trivial
since there's only ever one thread doing it at a time which also owns all
nodes it's holding. When the thread is done it can just push its list onto
the free list again.

This implementation offers comparable performance to the previous one when
only being accessed by a single thread and a slight speedup compared to
the previous implementation wrapped into a `ThreadSafeAllocator` up to ~7
threads performing operations on it concurrently.
(measured on a base model MacBook Pro M1)
2026-02-25 19:12:35 +01:00
hemisputnik
2f8e660805 std.math.log10: handle comptime_int inputs correctly
also add a few tests for comptime types

fixes #31333
2026-02-25 18:57:58 +01:00
Andrew Kelley
608b07a3d7 Merge pull request 'fix std.heap.PageAllocator to not intrude on stacks + re-enable LoongArch CI' (#31271) from alexrp/zig:page-allocator-fixes into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31271
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-24 21:24:50 +01:00
Toufiq Shishir
784e89fd4b fix: remove null argument from std.debug.lockStderr call inside std.json.Value.dump 2026-02-24 04:23:25 +01:00
Frank Denis
e8ca9229c8 Expose the elligator map for Curve25519
This is the same as for Edwards25519 without the y coordinate,
since it returns Montgomery coordinates, but it can be confusing
to call the Edwards25519 function while working on the
Curve25519 representation.

New protocols such as CPACE requires the map over Curve25519.
2026-02-23 13:04:58 +01:00
George Huebner
27ef6e9a9b bpf: add missing helpers 2026-02-23 02:27:13 +01:00
Yusuf Bham
de6c0d500f uefi(guid): don't use @byteSwap in fmt, adjust fmt test
@byteSwap shouldn't be called since we're formatting
ints again, and UEFI only officially supports LE.
2026-02-22 23:13:33 +01:00
inge4pres
29e52aa040 lib: add cgroup BPF helpers
Add bpf_cgrp_storage_get and bpf_cgrp_storage_delete from
libbpf helpers.

Signed-off-by: inge4pres <fgualazzi@gmail.com>
2026-02-22 20:24:51 +01:00
Alex Rønne Petersen
b5bcbf2a62
std.heap.DebugAllocator: make BucketHeader.fromPage() use wrapping arithmetic
If we've allocated the very last page in the address space then these operations
will overflow and underflow respectively - which is fine.
2026-02-21 23:39:34 +01:00
Matthew Lugg
a9d18c4a0c
std.heap.PageAllocator: avoid mremaps which may reserve potential stack space
Linux's approach to mapping the main thread's stack is quite odd: it essentially
tries to select an mmap address (assuming unhinted mmap calls) which do not
cover the region of virtual address space into which the stack *would* grow
(based on the stack rlimit), but it doesn't actually *prevent* those pages from
being mapped. It also doesn't try particularly hard: it's been observed that the
first (unhinted) mmap call in a simple application is usually put at an address
which is within a gigabyte or two of the stack, which is close enough to make
issues somewhat likely. In particular, if we get an address which is close-ish
to the stack, and then `mremap` it without the MAY_MOVE flag, we are *very*
likely to map pages in this "theoretical stack region". This is particularly a
problem on loongarch64, where the initial mmap address is empirically only
around 200 megabytes from the stack (whereas on most other 64-bit targets it's
closer to a gigabyte).

To work around this, we just need to avoid mremap in some cases. Unfortunately,
this system call isn't used too heavily by musl or glibc, so design issues like
this can and do exist without being caught. So, when `PageAllocator.resize` is
called, let's not try to `mremap` to grow the pages. We can still call `mremap`
in the `PageAllocator.remap` path, because in that case we can set the
`MAY_MOVE` flag, which empirically appears to make the Linux kernel avoid the
problematic "theoretical stack region".
2026-02-21 23:39:34 +01:00
Alex Rønne Petersen
c8dd050305
std.heap.PageAllocator: hint mmaps in the same direction as stack growth
The old logic was fine for targets where the stack grows up (so, literally just
hppa), but problematic on targets where it grows down, because we could hint
that we wanted an allocation to happen in an area of the address space that the
kernel expects to be able to expand the stack into. The kernel is happy to
satisfy such a hint despite the obvious problems this leads to later down the
road.

Co-authored-by: rpkak <rpkak@noreply.codeberg.org>
2026-02-21 23:39:20 +01:00
Andrew Kelley
311bba4af0 std.Io.Select: add awaitMany function and unit test
and fix documentation. these functions are in fact threadsafe.
2026-02-20 17:28:33 -08:00
Andrew Kelley
f9053f38e5 std.Io.Select: add documentation 2026-02-20 17:08:13 -08:00
Andrew Kelley
54eb03cbf6 std.Io.Select: remove "outstanding" field
it is not fundamentally part of this abstraction
2026-02-20 16:42:37 -08:00
Andrew Kelley
5ac6ff43d4 Merge pull request 'Io.Select: cancelation and concurrent' (#30836) from blblack/zig:select-stuff into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30836
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-21 01:29:06 +01:00
Andrew Kelley
6a9510c0eb Merge pull request 'Make std.PriorityDequeue an unmanaged container' (#31273) from saurabh/zig:unmanaged-priority-dequeue into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31273
2026-02-20 07:11:57 +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
David Rubin
8259d8d631 minimal 2026-02-20 05:34:54 +01:00
Alex Rønne Petersen
e95132476d std.os.linux: fix the mmap2 unit for various architectures
closes https://codeberg.org/ziglang/zig/issues/31033
2026-02-20 04:56:07 +01:00
Lukas Lalinsky
7dddcd043f Add lr and sp clobbers for arm/thumb 2026-02-20 01:16:33 +01:00
Yusuf Bham
1e26d885d3 uefi(guid): don't use std.mem.asBytes in format
Using std.mem.asBytes causes a panic when format
is called as slices can no longer have hex options
when formatting, so instead just print the ints.
2026-02-18 20:19:50 -05:00
Saurabh Mishra
5140455264 min heap is formed with less than comparison 2026-02-18 13:24:03 -08:00
Saurabh Mishra
293710c1cd use .empty for default field values intead of init() and introduce initContext() 2026-02-18 12:38:26 -08:00
Saurabh Mishra
23252c27be merge popMinOrNull into popMin and popMaxOrNull into popMax, and update tests 2026-02-18 11:58:13 -08:00
Saurabh Mishra
535722b374 rename add to push and remove to pop in methods and tests 2026-02-18 11:43:57 -08:00
Saurabh Mishra
d297f7b8f2 make priority dequeue an unmanaged container and ensure tests pass 2026-02-18 11:20:32 -08:00
Marcel W. Wysocki
0e3c6514a4 link: recognize thin archives in ld script detection
Needed for linking the Linux kernel.
2026-02-17 23:15:32 +01:00
Andrew Kelley
27eec9bd62 Revert "Allow overriding std.Io at a namespace level."
This reverts commit e314dadb01.

This idea requires more consideration before committing to it.
At the very least let's not regress autodocs.

Closes #31230
2026-02-15 20:26:12 -08:00
vitalii
1838134200 fix segfault when /etc/hosts missing last newline 2026-02-15 23:12:13 +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
5763f7dbcc std.Io.Evented: clean up supporting code for select 2026-02-15 05:52:07 -05:00
Andrew Kelley
d9fc7fa04d std.Io: remove select function
This function works with a slice of futures and returns the index of a
completed one. This doesn't work very well in practice because it's
either too high level or too low level.

At the lower level we have Io.Batch for doing this kind of thing at the
Operation API layer.

At the higher level we have Io.Select which is a convenience wrapper
around an Io.Group and an Io.Queue.
2026-02-14 18:31:48 -08:00
Andrew Kelley
c6eeae8a8c Merge pull request 'std.math.acos/asin: Add @Vector(?,f32) and @Vector(?,f64) support' (#31209) from lzm-build/zig:acos_asin_simd into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31209
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-14 23:35:03 +01:00
Jacob Young
771047ae93 Io.Dispatch.sleep: add cancelation support 2026-02-14 16:26:49 -05:00
Jacob Young
b7f93695f9 Io.Dispatch.Mutex: fix deadlock conditions 2026-02-14 05:52:59 -05:00
lzm-build
51509fe63b std.math.acos/asin: Add @Vector(?,f32) and @Vector(?,f64) support 2026-02-14 16:57:05 +08:00
Andrew Kelley
017228de89 libc malloc: introduce a canary
Instead of padding, use static entropy to detect corrupted header.

* 64-bit, safe modes: 10 canary bits
* 64-bit, unsafe modes: 0 canary bits
* 32-bit: 27 canary bits

A further enhancement, not done here, would be to upgrade from canary to
parity.
2026-02-14 09:25:41 +01:00
lzm-build
ef13a373bc fix assertion argument order in acos/asin/atan 2026-02-14 13:33:55 +08:00
Kendall Condon
5d58306162 rework fuzz testing to be smith based
-- On the standard library side:

The `input: []const u8` parameter of functions passed to `testing.fuzz`
has changed to `smith: *testing.Smith`. `Smith` is used to generate
values from libfuzzer or input bytes generated by libfuzzer.

`Smith` contains the following base methods:
* `value` as a generic method for generating any type
* `eos` for generating end-of-stream markers. Provides the additional
  guarantee `true` will eventually by provided.
* `bytes` for filling a byte array.
* `slice` for filling part of a buffer and providing the length.

`Smith.Weight` is used for giving value ranges a higher probability of
being selected. By default, every value has a weight of zero (i.e. they
will not be selected). Weights can only apply to values that fit within
a u64. The above functions have corresponding ones that accept weights.
Additionally, the following functions are provided:
* `baselineWeights` which provides a set of weights containing every
  possible value of a type.
* `eosSimpleWeighted` for unique weights for `true` and `false`
* `valueRangeAtMost` and `valueRangeLessThan` for weighing only a range
  of values.

-- On the libfuzzer and abi side:

--- Uids

These are u32s which are used to classify requested values. This solves
the problem of a mutation causing a new value to be requested and
shifting all future values; for example:

1. An initial input contains the values 1, 2, 3 which are interpreted
as a, b, and c respectively by the test.

2. The 1 is mutated to a 4 which causes the test to request an extra
value interpreted as d. The input is now 4, 2, 3, 5 (new value) which
the test corresponds to a, d, b, c; however, b and c no longer
correspond to their original values.

Uids contain a hash component and type component. The hash component
is currently determined in `Smith` by taking a hash of the calling
`@returnAddress()` or via an argument in the corresponding `WithHash`
functions. The type component is used extensively in libfuzzer with its
hashmaps.

--- Mutations

At the start of a cycle (a run), a random number of values to mutate is
selected with less being exponentially more likely. The indexes of the
values are selected from a selected uid with a logarithmic bias to uids
with more values.

Mutations may change a single values, several consecutive values in a
uid, or several consecutive values in the uid-independent order they
were requested. They may generate random values, mutate from previous
ones, or copy from other values in the same uid from the same input or
spliced from another.

For integers, mutations from previous ones currently only generates
random values. For bytes, mutations from previous mix new random data
and previous bytes with a set number of mutations.

--- Passive Minimization

A different approach has been taken for minimizing inputs: instead of
trying a fixed set of mutations when a fresh input is found, the input
is instead simply added to the corpus and removed when it is no longer
valuable.

The quality of an input is measured based off how many unique pcs it
hit and how many values it needed from the fuzzer. It is tracked which
inputs hold the best qualities for each pc for hitting the minimum and
maximum unique pcs while needing the least values.

Once all an input's qualities have been superseded for the pcs it hit,
it is removed from the corpus.

-- Comparison to byte-based smith

A byte-based smith would be much more inefficient and complex than this
solution. It would be unable to solve the shifting problem that Uids
do. It is unable to provide values from the fuzzer past end-of-stream.
Even with feedback, it would be unable to act on dynamic weights which
have proven essential with the updated tests (e.g. to constrain values
to a range).

-- Test updates

All the standard library tests have been updated to use the new smith
interface. For `Deque`, an ad hoc allocator was written to improve
performance and remove reliance on heap allocation. `TokenSmith` has
been added to aid in testing Ast and help inform decisions on the smith
interface.
2026-02-13 22:12:19 -05:00
Kendall Condon
e40557b1f7 allow specifying mode in --debug-rt
The motivation is that libfuzzer is slow in Debug mode and bugs usually
manifest late into fuzzing, which makes testing it in ReleaseSafe
useful.
2026-02-13 17:58:09 -05:00
Kendall Condon
1a9d75ebe0 fix fuzzing speed with prior runs 2026-02-13 17:58:09 -05:00
Kendall Condon
af1e196db3 align end of elf archives
The end of the archive needs to also be aligned to a two-byte boundary,
not just the start of records. This was causing lld to reject archives.

Notably, this was happening with compiler_rt when rebuilding in fuzz
mode, which is why this commit is included in this patchset.
2026-02-13 17:58:09 -05:00
Kendall Condon
3c9024be08 update fuzzing for build system changes 2026-02-13 17:58:09 -05:00