zig/lib/std
mlugg 160f2dabed
std.Build.Cache: fix several bugs
Aside from adding comments to document the logic in `Cache.Manifest.hit`
better, this commit fixes two serious bugs.

The first, spotted by Andrew, is that when upgrading from a shared to an
exclusive lock on the manifest file, we do not seek it back to the
start. This is a simple fix.

The second is more subtle, and has to do with the computation of file
digests. Broadly speaking, the goal of the main loop in `hit` is to
iterate the files listed in the manifest file, and check if they've
changed, based on stat and a file hash. While doing this, the
`bin_digest` field of `std.Build.Cache.File`, which is initially
`undefined`, is populated for all files, either straight from the
manifest (if the stat matches) or recomputed from the file on-disk. This
file digest is then used to update `man.hash.hasher`, which is building
the final hash used as, for instance, the output directory name when the
compiler emits into the cache directory. When `hit` returns a cache
miss, it is expected that `man.hash.hasher` includes the digests of all
"initial files"; that is, those which have been already added with e.g.
`addFilePath`, but not those which will later be added with
`addFilePost` (even though the manifest file has told us about some such
files). Previously, `hit` was using the `unhit` function to do this in a
few cases. However, this is incorrect, because `hit` assumes that all
files already have their `bin_digest` field populated; this function is
only valid to call *after* `hit` returns. Instead, we need to actually
compute the hashes which haven't yet been populated. Even if this logic
has been working, there was still a bug here, because we called `unhit`
when upgrading from a shared to an exclusive lock, writing the
(potentially `undefined`) file digests, but the loop itself writes the
file digests *again*! All in all, the hashing logic here was actually
incredibly broken.

I've taken the opportunity to restructure this section of the code into
what I think is a more readable format. A new function,
`hitWithCurrentLock`, uses the open manifest file to try and find a
cache hit. It returns a tagged union which, in the miss case, tells the
caller (`hit`) how many files already have their hash populated. This
avoids redundant work recomputing the same hash multiple times in
situations where the lock needs upgrading. This also eliminates the
outer loop from `hit`, which was a little confusing because it iterated
no more than twice!

The bugs fixed here could manifest in several different ways depending
on how contended file locks were satisfied. Most notably, on a cache
miss, the Zig compiler might have written the compilation output to the
incorrect directory (because it incorrectly constructed a hash using
`undefined` or repeated file digests), resulting in all future hits on
this manifest causing `error.FileNotFound`. This is #23110. I have been
able to reproduce #23110 on `master`, and have not been able to after
this commit, so I am relatively sure this commit resolves that issue.

Resolves: #23110
2025-04-27 14:08:21 +02:00
..
Build std.Build.Cache: fix several bugs 2025-04-27 14:08:21 +02:00
c Merge pull request #23447 from alexrp/cpuid-updates 2025-04-03 19:32:59 +02:00
compress Merge pull request #23417 from dweiller/zstd-fixes 2025-03-31 17:52:23 +02:00
crypto typo: was issues -> was issued 2025-03-18 04:58:08 +01:00
debug stdlib: handle EEXIST in mmap with FIXED_NOREPLACE. Fixes #21475 2025-02-24 04:36:14 -05:00
dwarf
fmt
fs Calculate WTF-8 length before converting instead of converting into an intermediate buffer on the stack 2025-04-26 15:07:26 +02:00
hash Add rapidhash (#22085) 2025-02-22 14:32:48 -08:00
heap DebugAllocator: Fix bucket removal logic causing segfault/leak (#23390) 2025-04-02 14:22:15 +02:00
http lib/std/http/Client.zig: Ignore empty proxy environment variables (#23223) 2025-03-14 21:20:55 +01:00
io
json Merge pull request #23220 from samy-00007/bytesAsSlice-fix 2025-03-25 15:23:08 +01:00
math Merge pull request #23310 from Rexicon226/fix-23309 2025-03-25 18:44:58 +01:00
mem Allocator.create: properly handle alignment for zero-sized types (#21864) 2025-03-26 21:52:12 +01:00
meta
net std.net: Implement if_nametoindex for windows (#22555) 2025-04-05 20:41:50 +02:00
os Return FileNotFound when CreateProcessW is called with a missing path (#23567) 2025-04-16 04:25:02 +02:00
posix lib/std/posix/test.zig: enable disabled tests using CWD 2025-02-20 08:44:09 +01:00
process
Random
sort
tar
Target std.Target.Query: Don't append glibc version in zigTriple() if ABI isn't GNU. 2025-03-09 12:07:34 -07:00
testing FailingAllocator: remove outdated doc comments, move doc comment example to decltest 2025-04-25 19:58:11 +02:00
Thread std.Target: Remove functions that just wrap component functions. 2025-02-17 19:18:19 +01:00
time
tz
unicode
valgrind
zig std: add nvidia as a known arm implementer 2025-04-09 15:03:45 +02:00
zip
zon zon: normalize negative zeroes 2025-04-02 08:43:21 +02:00
array_hash_map.zig std.meta.FieldType -> @FieldType 2025-03-27 12:19:07 +01:00
array_list.zig std.ArrayList: delete unit test 2025-02-26 11:42:03 -08:00
ascii.zig
atomic.zig std.atomic: make cache_line const a comptime_int 2025-02-22 03:48:09 +01:00
base64.zig docs(std.base64): Add references to RFC 4648 2025-03-24 15:31:58 +01:00
bit_set.zig
BitStack.zig
bounded_array.zig std.BoundedArray: popOrNull() -> pop() [v2] (#22723) 2025-02-09 11:46:15 +00:00
buf_map.zig
buf_set.zig
Build.zig Revert "Merge pull request #22898 from kristoff-it/deprecated-proposal" 2025-02-28 01:37:10 -08:00
builtin.zig compiler: Support more GCC code models and fix the mapping to LLVM code models. 2025-03-20 13:34:04 -07:00
c.zig std.c: android bionic C supports arc4random_buf and getentropy 2025-03-25 15:24:07 +01:00
coff.zig
compress.zig
crypto.zig Merge pull request #19976 from clickingbuttons/asn1 2025-02-22 20:21:09 -05:00
debug.zig Fix std.debug.dumpHex address offsets 2025-03-26 15:40:56 +01:00
dwarf.zig
dynamic_library.zig std.DynLib: fix proper type of chain_ptr on GnuHashSection32 2025-03-03 20:01:05 +01:00
elf.zig std.DynLib: add support for DT_GNU_HASH sections in elf files 2025-02-22 17:33:49 -05:00
enums.zig
fifo.zig runtime page size detection 2025-02-06 14:23:23 -08:00
fmt.zig fix(std/fmt.zig): fix overflow in fmtDurationSigned 2025-03-24 15:31:53 +01:00
fs.zig
gpu.zig std.gpu: stop using comptimePrint 2025-02-18 18:07:31 +03:30
hash.zig Add rapidhash (#22085) 2025-02-22 14:32:48 -08:00
hash_map.zig std.hash_map: placeholder for doc comments 2025-02-06 14:23:23 -08:00
heap.zig Allocator.create: properly handle alignment for zero-sized types (#21864) 2025-03-26 21:52:12 +01:00
http.zig
io.zig
json.zig
leb128.zig
linked_list.zig
log.zig
macho.zig
math.zig std: remove special cases for stage2_x86_64 that are no longer needed 2025-02-12 09:48:17 -05:00
mem.zig Merge pull request #23220 from samy-00007/bytesAsSlice-fix 2025-03-25 15:23:08 +01:00
meta.zig docs: update old reference from builtin.Type.Struct to builtin.Type.@"struct" 2025-02-18 23:35:16 +01:00
multi_array_list.zig Add quota for comptime sort, add test 2025-03-31 17:52:16 +02:00
net.zig std.net: Implement if_nametoindex for windows (#22555) 2025-04-05 20:41:50 +02:00
once.zig
os.zig
pdb.zig
posix.zig std.os.linux: block all signals in raise 2025-04-02 23:57:32 +02:00
priority_dequeue.zig
priority_queue.zig
process.zig Merge pull request #23373 from alexrp/get-base-address 2025-03-27 21:08:04 +01:00
Progress.zig
Random.zig
RingBuffer.zig
segmented_list.zig
SemanticVersion.zig
simd.zig
sort.zig
start.zig start: fix pc register syntax for m68k 2025-04-08 12:10:22 +02:00
static_string_map.zig
std.zig move libubsan to lib/ and integrate it into -fubsan-rt 2025-02-25 11:22:33 -08:00
tar.zig
Target.zig std.Target: Update known max OS versions. 2025-02-25 20:30:43 +01:00
testing.zig std.testing: improve compile error on untagged union equality 2025-02-16 15:51:40 +01:00
Thread.zig stdlib: handle EEXIST in mmap with FIXED_NOREPLACE. Fixes #21475 2025-02-24 04:36:14 -05:00
time.zig std.time: Make Instant.since() work on UEFI 2025-03-02 23:16:34 +00:00
treap.zig
tz.zig
unicode.zig
Uri.zig
valgrind.zig
wasm.zig
zig.zig Move the compiler's LLVM bitcode builder to std.zig.llvm. 2025-02-27 01:32:49 -05:00
zip.zig runtime page size detection 2025-02-06 14:23:23 -08:00
zon.zig