Commit graph

503 commits

Author SHA1 Message Date
Ryan Liptak
f16eb18ce8 Use / as path separator when writing tar files
Some checks are pending
ci / aarch64-freebsd-debug (push) Waiting to run
ci / aarch64-freebsd-release (push) Waiting to run
ci / aarch64-linux-debug (push) Waiting to run
ci / aarch64-linux-release (push) Waiting to run
ci / aarch64-netbsd-debug (push) Waiting to run
ci / aarch64-netbsd-release (push) Waiting to run
ci / aarch64-macos-debug (push) Waiting to run
ci / aarch64-macos-release (push) Waiting to run
ci / loongarch64-linux-debug (push) Waiting to run
ci / loongarch64-linux-release (push) Waiting to run
ci / powerpc64le-linux-debug (push) Waiting to run
ci / powerpc64le-linux-release (push) Waiting to run
ci / riscv64-linux-debug (push) Waiting to run
ci / riscv64-linux-release (push) Waiting to run
ci / s390x-linux-debug (push) Waiting to run
ci / s390x-linux-release (push) Waiting to run
ci / x86_64-freebsd-debug (push) Waiting to run
ci / x86_64-freebsd-release (push) Waiting to run
ci / x86_64-linux-debug (push) Waiting to run
ci / x86_64-linux-debug-llvm (push) Waiting to run
ci / x86_64-linux-release (push) Waiting to run
ci / x86_64-netbsd-debug (push) Waiting to run
ci / x86_64-netbsd-release (push) Waiting to run
ci / x86_64-openbsd-debug (push) Waiting to run
ci / x86_64-openbsd-release (push) Waiting to run
ci / x86_64-windows-debug (push) Waiting to run
ci / x86_64-windows-release (push) Waiting to run
The tar format expects `/`, although some untar implementations do seem to handle Windows-style `\` path separators (7-Zip at least).

The tar.Writer API can't really enforce this, though, as doing so would effectively make `\` an illegal character when it's really not. So, it's up to the user to provide paths with the correct path separators.

`Build/WebServer.zig` will still output tars with `\` as a path separator on Windows, but that's currently only used during fuzzing which is not yet implemented on Windows.
2026-03-08 02:10:00 +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
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
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
Jacob Young
b5bd494606 std.Threaded: replace more kernel32 functions with ntdll 2026-02-07 00:02:50 -05:00
Andrew Kelley
e661e78256 store the Manifest in the fork set 2026-02-06 13:21:13 -08:00
Andrew Kelley
3d33735d73 zig build: add --fork CLI argument
closes #31124
2026-02-06 12:36:29 -08:00
Andrew Kelley
922ab8b8bc std: finish moving time to Io interface
Importantly, adds ability to get Clock resolution, which may be zero.
This allows error.Unexpected and error.ClockUnsupported to be removed
from timeout and clock reading error sets.
2026-02-02 23:02:31 -08:00
Andrew Kelley
550da1b676 std: migrate remaining sync primitives to Io
- delete std.Thread.Futex
- delete std.Thread.Mutex
- delete std.Thread.Semaphore
- delete std.Thread.Condition
- delete std.Thread.RwLock
- delete std.once

std.Thread.Mutex.Recursive remains... for now. it will be replaced with
a special purpose mechanism used only by panic logic.

std.Io.Threaded exposes mutexLock and mutexUnlock for the advanced case
when you need to call them directly.
2026-02-02 18:57:17 -08:00
Ryan Liptak
05346e123b Add process.Child.Cwd, use it for cwd and remove cwd_dir field
The user must now explicitly choose between inheriting the current CWD, passing a path for the CWD, or passing a Dir for the CWD.
2026-02-02 01:41:35 -08:00
Jake Greenfield
aacf8ce03d Use MultiReader in zig std 2026-02-01 22:22:47 -05:00
Andrew Kelley
b1d1806fef std.process: currentDir -> currentPath
In Zig standard library, Dir means an open directory handle. path
represents a file system identifier string. This function is better
named after "current path" than "current dir". "get" and "working" are
superfluous.
2026-01-29 18:47:58 -08:00
Andrew Kelley
649aaf4814 std: migrate getcwd to Io
progress towards #30150
2026-01-29 18:40:55 -08:00
murtaza
b5770541bd testing: ability to read environment variables from unit tests 2026-01-17 00:40:22 +01:00
Ryan Liptak
9d1a39c50f resinator: Sync with upstream
No functional differences, just some shuffling things around and the removal of the now-obsolete `utils.openFileNotDir`.
2026-01-14 01:04:24 +01:00
Sertonix
47cc233f22 aro: define arch macros for riscv
Fixes some test failure:

test
+- test-standalone
   +- standalone_test_cases
      +- standalone_test_cases.glibc_compat
         +- CheckObject
            +- compile exe native-linux-gnu.2.38 Debug native-linux-gnu.2.38 64 errors
test/standalone/glibc_compat/glibc_runtime_check.zig:11:18: error: C import failed
const c_malloc = @cImport(
                 ^~~~~~~~
referenced by:
    checkReallocarray: test/standalone/glibc_compat/glibc_runtime_check.zig:46:27
    main: test/standalone/glibc_compat/glibc_runtime_check.zig:112:26
    4 reference(s) hidden; use '-freference-trace=6' to see all references
error: translation failure
build/stage3/lib/zig/libc/include/riscv-linux-gnu/bits/wordsize.h:22:3: error: unsupported ABI
2026-01-13 20:01:52 +01:00
Matthew Lugg
aebd84b525 build runner: refactor step evaluation logic
The previous logic was made really messy by the fact that upon entry to
the step eval worker, the step may not be ready to run, we may be racing
with other workers doing the same check, and we had already acquired our
RSS requirement even though we might not run. It also required iterating
all dependencies each time we were called to check whether we were even
ready to run yet.

A much better strategy is for each step to have an atomic counter
representing how many of its dependencies are yet to complete. When a
step completes (successfully or otherwise), it decrements this value for
all of its dependants, and if it drops any to 0, it schedules that step
to run. This means each step is scheduled exactly once, and only when
all of its dependencies have finished, reducing redundant checks and
hence contention. If the step being scheduled needs to claim RSS which
isn't available, then it is instead added to `memory_blocked_steps`,
which is iterated by the step worker after a step with an RSS claim
finishes.

This logic is more concise than before, simpler to understand, generally
more efficient, and fixes a bug in the RSS tracking. Also, as a nice
side effect, it should also play a little bit nicer with `Io.Threaded`'s
scheduling strategy, because we no longer spawn extremely short-lived
tasks all the time as we previously did.

Resolves: https://codeberg.org/ziglang/zig/issues/30742
2026-01-09 03:16:39 +01:00
Andrew Kelley
7248b4a4e4 std.fs: deprecate base64 APIs
100% of std.fs is now deprecated.
2026-01-07 17:33:06 -08:00
Andrew Kelley
1f1381a866 update API usage of std.crypto.random to io.random 2026-01-07 11:03:36 -08:00
Andrew Kelley
816565dd07 std: move entropy to Io 2026-01-07 11:03:36 -08:00
Andrew Kelley
c8ecfad41a build runner: clean up tmp dirs 2026-01-04 17:46:44 -08:00
Andrew Kelley
b4dbe483a7 std.Build: adjust temp files API
Remove the RemoveDir step with no replacement. This step had no valid
purpose. Mutating source files? That should be done with
UpdateSourceFiles step. Deleting temporary directories? That required
creating the tmp directories in the configure phase which is broken.
Deleting cached artifacts? That's going to cause problems.

Similarly, remove the `Build.makeTempPath` function. This was used to
create a temporary path in the configure place which, again, is the
wrong place to do it.

Instead, the WriteFile step has been updated with more functionality:

tmp mode: In this mode, the directory will be placed inside "tmp" rather
than "o", and caching will be skipped. During the `make` phase, the step
will always do all the file system operations, and on successful build
completion, the dir will be deleted along with all other tmp
directories. The directory is therefore eligible to be used for
mutations by other steps. `Build.addTempFiles` is introduced to
initialize a WriteFile step with this mode.

mutate mode: The operations will not be performed against a freshly
created directory, but instead act against a temporary directory.
`Build.addMutateFiles` is introduced to initialize a WriteFile step with
this mode.

`Build.tmpPath` is introduced, which is a shortcut for
`Build.addTempFiles` followed by `WriteFile.getDirectory`.

* give Cache a gpa rather than arena because that's what it asks for
2026-01-04 17:23:45 -08:00
Andrew Kelley
e2c04a4651 fix some windows compilation errors 2026-01-04 00:27:09 -08:00
Andrew Kelley
b32a38ad27 build: fix file system watching compilation on macOS 2026-01-04 00:27:09 -08:00
Andrew Kelley
1070c2a71a rename env_map to environ_map
For naming consistency with `std.process.Environ.Map`.
2026-01-04 00:27:09 -08:00
Andrew Kelley
4afed3e9ef test-standalone: update the rest of the cases to new API 2026-01-04 00:27:08 -08:00
Andrew Kelley
bf74827ddb test-standalone: update more cases to new main API 2026-01-04 00:27:08 -08:00
Andrew Kelley
1b56686012 test-standalone: update cases to new main API 2026-01-04 00:27:08 -08:00
Andrew Kelley
f28802a9c6 zig libc: fix subcommand
This branch regressed the child process "run" mechanism because it
didn't pass the correct stdin, stdout, stderr values to process.spawn

Fixed now.
2026-01-04 00:27:08 -08:00
Andrew Kelley
960c512efd compiler: update std lib API usage 2026-01-04 00:27:08 -08:00
Andrew Kelley
08447ca47e std.fs.path: make relative a pure function
Instead of querying the operating system for current working directory
and environment variables, this function now accepts those things as
inputs.
2026-01-04 00:27:08 -08:00
Andrew Kelley
9009ab2495 std.Io.Threaded: make environ init non-optional
and argv0 on systems that need it too.

fixes surprising behavior for applications that forget to initialize the
environment field.
2026-01-04 00:27:08 -08:00
Andrew Kelley
3e6d6150d9 std.process.Environ: fix compile errors on POSIX 2026-01-04 00:27:08 -08: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
Andrew Kelley
d6a1e73142 std: start wrangling environment variables and process args
this commit is unfinished. It marks a spot where I wanted to start
moving child process stuff below the std.Io.VTable
2026-01-04 00:27:07 -08:00
Matthew Lugg
f306a9f84a
std: rebase fixups and cancelation changes
This commit includes some API changes which I agreed with Andrew as a
follow-up to the recent `Io.Group` changes:

* `Io.Group.await` *does* propagate cancelation to group tasks; it then
  waits for them to complete, and *also* returns `error.Canceled`. The
  assertion that group tasks handle `error.Canceled` "correctly" means
  this behavior is loosely analagous to how awaiting a future works. The
  important thing is that the semantics of `Group.await` and
  `Future.await` are similar, and `error.Canceled` will always be
  visible to the caller (assuming correct API usage).

* `Io.Group.awaitUncancelable` is removed.

* `Future.await` calls `recancel` only if the "child" task (the future
  being awaited) did not acknowledge cancelation. If it did, then it is
  assumed that the future will propagate `error.Canceled` through
  `await` as needed.
2026-01-03 15:45:11 +00:00
PeterMcKinnis
96ba0ab930 fix lockStderr API calls in test_runner fuzz code 2025-12-30 15:09:17 -05:00
Andrew Kelley
1eb8fe7c61 Merge pull request 'bsd-futex' (#30626) from mikdusan/zig:bsd-futex into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30626
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
2025-12-30 20:24:00 +01:00
Andrew Kelley
2adfd4d107 std.Io: fix and improve Group API
Rename `wait` to `await` to be consistent with Future API. The
convention here is that this set of functionality goes together:
* async/concurrent
* await/cancel

Also rename Select `wait` to `await` for the same reason.

`Group.await` now can return `error.Canceled`. Furthermore,
`Group.await` does not auto-propagate cancelation. Instead, users should
follow the pattern of `defer group.cancel(io);` after initialization,
and doing `try group.await(io);` at the end of the success path.
Advanced logic can choose to do something other than this pattern in the
event of cancelation.

Additionally, fixes a bug in `std.Io.Threaded` future await, in which it
swallowed an `error.Canceled`. Now if a task is canceled while awaiting
a future, after propagating the cancel request, it also recancels,
meaning that the awaiting task will properly detect its own cancelation
at the next cancelation point.

Furthermore, fixes a bug in the compiler where `error.Canceled` was
being swallowed in `dispatchPrelinkWork`.

Finally, fixes std.crypto code that inappropriately used
`catch unreachable` in response to cancelation without even so much as a
comment explaining why it was believed to be unreachable. Now, those
functions have `error.Canceled` in the error set and propagate
cancelation properly.

With this way of doing things, `Group.await` has a nice property: even if
all tasks in the group are CPU bound and without cancelation points, the
`Group.await` can still be canceled. In such case, the task that was
waiting for `await` wakes up with a chance to do some more resource
cleanup tasks, such as canceling more things, before entering the
deferred `Group.cancel` call at which point it has to suspend until the
canceled but uninterruptible CPU bound tasks complete.

closes #30601
2025-12-29 22:47:34 -08:00
Michael Dusan
1ea2d5692d
openbsd: init Io.Threaded.argv0 for tests
Some tests (eg. std.process.openExecutable) require argv0 for OpenBSD,
otherwise error.OperationUnsupported is encountered.
2025-12-29 21:06:08 -05:00
Andrew Kelley
744e22b18a build_runner: re-order the help menu
closes #30615
2025-12-28 19:53:42 -08:00
Ryan Liptak
4f8aa8213d Fix for compile error in resinator 2025-12-26 19:58:56 -08:00
Andrew Kelley
b0691df5ba test_runner: fix simple runner compilation 2025-12-26 19:58:56 -08:00
Andrew Kelley
0870f17501 fix aro and resinator compilation errors 2025-12-23 22:15:12 -08:00
Andrew Kelley
c05e2720a1 tools: fix compilation errors 2025-12-23 22:15:12 -08:00
Andrew Kelley
691afee786 langref: fix build failure 2025-12-23 22:15:12 -08:00
Andrew Kelley
33e302d67a update remaining calls to std.Io.Threaded.init 2025-12-23 22:15:12 -08:00
Andrew Kelley
9aee45be97 fix "zig std" compilation errors 2025-12-23 22:15:12 -08:00
Andrew Kelley
3c2f5adf41 std: integrate Io.Threaded with environment variables
* std.option allows overriding the debug Io instance
* if the default is used, start code initializes environ and argv0

also fix some places that needed recancel(), thanks mlugg!

See #30562
2025-12-23 22:15:12 -08:00