| ... | ... | @@ -4,7 +4,8 @@ |
| 4 | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | 5 | // and substantial portions of the software. |
| 6 | 6 | const std = @import("std.zig"); |
| 7 | | const Blake3 = std.crypto.hash.Blake3; |
| 7 | const crypto = std.crypto; |
| 8 | const Hasher = crypto.auth.siphash.SipHash128(1, 3); // provides enough collision resistance for the CacheHash use cases, while being one of our fastest options right now |
| 8 | 9 | const fs = std.fs; |
| 9 | 10 | const base64 = std.base64; |
| 10 | 11 | const ArrayList = std.ArrayList; |
| ... | ... | @@ -16,9 +17,8 @@ const Allocator = std.mem.Allocator; |
| 16 | 17 | |
| 17 | 18 | const base64_encoder = fs.base64_encoder; |
| 18 | 19 | const base64_decoder = fs.base64_decoder; |
| 19 | | /// This is 70 more bits than UUIDs. For an analysis of probability of collisions, see: |
| 20 | | /// https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions |
| 21 | | const BIN_DIGEST_LEN = 24; |
| 20 | /// This is 128 bits - Even with 2^54 cache entries, the probably of a collision would be under 10^-6 |
| 21 | const BIN_DIGEST_LEN = 16; |
| 22 | 22 | const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); |
| 23 | 23 | |
| 24 | 24 | const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024; |
| ... | ... | @@ -43,9 +43,13 @@ pub const File = struct { |
| 43 | 43 | } |
| 44 | 44 | }; |
| 45 | 45 | |
| 46 | /// CacheHash manages project-local `zig-cache` directories. |
| 47 | /// This is not a general-purpose cache. |
| 48 | /// It was designed to be fast and simple, not to withstand attacks using specially-crafted input. |
| 46 | 49 | pub const CacheHash = struct { |
| 47 | 50 | allocator: *Allocator, |
| 48 | | blake3: Blake3, |
| 51 | hasher_init: Hasher, // initial state, that can be copied |
| 52 | hasher: Hasher, // current state for incremental hashing |
| 49 | 53 | manifest_dir: fs.Dir, |
| 50 | 54 | manifest_file: ?fs.File, |
| 51 | 55 | manifest_dirty: bool, |
| ... | ... | @@ -54,9 +58,11 @@ pub const CacheHash = struct { |
| 54 | 58 | |
| 55 | 59 | /// Be sure to call release after successful initialization. |
| 56 | 60 | pub fn init(allocator: *Allocator, dir: fs.Dir, manifest_dir_path: []const u8) !CacheHash { |
| 61 | const hasher_init = Hasher.init(&[_]u8{0} ** Hasher.minimum_key_length); |
| 57 | 62 | return CacheHash{ |
| 58 | 63 | .allocator = allocator, |
| 59 | | .blake3 = Blake3.init(.{}), |
| 64 | .hasher_init = hasher_init, |
| 65 | .hasher = hasher_init, |
| 60 | 66 | .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}), |
| 61 | 67 | .manifest_file = null, |
| 62 | 68 | .manifest_dirty = false, |
| ... | ... | @@ -69,8 +75,8 @@ pub const CacheHash = struct { |
| 69 | 75 | pub fn addSlice(self: *CacheHash, val: []const u8) void { |
| 70 | 76 | assert(self.manifest_file == null); |
| 71 | 77 | |
| 72 | | self.blake3.update(val); |
| 73 | | self.blake3.update(&[_]u8{0}); |
| 78 | self.hasher.update(val); |
| 79 | self.hasher.update(&[_]u8{0}); |
| 74 | 80 | } |
| 75 | 81 | |
| 76 | 82 | /// Convert the input value into bytes and record it as a dependency of the |
| ... | ... | @@ -133,12 +139,12 @@ pub const CacheHash = struct { |
| 133 | 139 | assert(self.manifest_file == null); |
| 134 | 140 | |
| 135 | 141 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 136 | | self.blake3.final(&bin_digest); |
| 142 | self.hasher.final(&bin_digest); |
| 137 | 143 | |
| 138 | 144 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); |
| 139 | 145 | |
| 140 | | self.blake3 = Blake3.init(.{}); |
| 141 | | self.blake3.update(&bin_digest); |
| 146 | self.hasher = self.hasher_init; |
| 147 | self.hasher.update(&bin_digest); |
| 142 | 148 | |
| 143 | 149 | const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest}); |
| 144 | 150 | defer self.allocator.free(manifest_file_path); |
| ... | ... | @@ -238,7 +244,7 @@ pub const CacheHash = struct { |
| 238 | 244 | } |
| 239 | 245 | |
| 240 | 246 | var actual_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 241 | | try hashFile(this_file, &actual_digest); |
| 247 | try hashFile(this_file, &actual_digest, self.hasher_init); |
| 242 | 248 | |
| 243 | 249 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| 244 | 250 | cache_hash_file.bin_digest = actual_digest; |
| ... | ... | @@ -248,7 +254,7 @@ pub const CacheHash = struct { |
| 248 | 254 | } |
| 249 | 255 | |
| 250 | 256 | if (!any_file_changed) { |
| 251 | | self.blake3.update(&cache_hash_file.bin_digest); |
| 257 | self.hasher.update(&cache_hash_file.bin_digest); |
| 252 | 258 | } |
| 253 | 259 | } |
| 254 | 260 | |
| ... | ... | @@ -256,8 +262,8 @@ pub const CacheHash = struct { |
| 256 | 262 | // cache miss |
| 257 | 263 | // keep the manifest file open |
| 258 | 264 | // reset the hash |
| 259 | | self.blake3 = Blake3.init(.{}); |
| 260 | | self.blake3.update(&bin_digest); |
| 265 | self.hasher = self.hasher_init; |
| 266 | self.hasher.update(&bin_digest); |
| 261 | 267 | |
| 262 | 268 | // Remove files not in the initial hash |
| 263 | 269 | for (self.files.items[input_file_count..]) |*file| { |
| ... | ... | @@ -266,7 +272,7 @@ pub const CacheHash = struct { |
| 266 | 272 | self.files.shrink(input_file_count); |
| 267 | 273 | |
| 268 | 274 | for (self.files.items) |file| { |
| 269 | | self.blake3.update(&file.bin_digest); |
| 275 | self.hasher.update(&file.bin_digest); |
| 270 | 276 | } |
| 271 | 277 | return null; |
| 272 | 278 | } |
| ... | ... | @@ -304,23 +310,23 @@ pub const CacheHash = struct { |
| 304 | 310 | |
| 305 | 311 | // Hash while reading from disk, to keep the contents in the cpu cache while |
| 306 | 312 | // doing hashing. |
| 307 | | var blake3 = Blake3.init(.{}); |
| 313 | var hasher = self.hasher_init; |
| 308 | 314 | var off: usize = 0; |
| 309 | 315 | while (true) { |
| 310 | 316 | // give me everything you've got, captain |
| 311 | 317 | const bytes_read = try file.read(contents[off..]); |
| 312 | 318 | if (bytes_read == 0) break; |
| 313 | | blake3.update(contents[off..][0..bytes_read]); |
| 319 | hasher.update(contents[off..][0..bytes_read]); |
| 314 | 320 | off += bytes_read; |
| 315 | 321 | } |
| 316 | | blake3.final(&ch_file.bin_digest); |
| 322 | hasher.final(&ch_file.bin_digest); |
| 317 | 323 | |
| 318 | 324 | ch_file.contents = contents; |
| 319 | 325 | } else { |
| 320 | | try hashFile(file, &ch_file.bin_digest); |
| 326 | try hashFile(file, &ch_file.bin_digest, self.hasher_init); |
| 321 | 327 | } |
| 322 | 328 | |
| 323 | | self.blake3.update(&ch_file.bin_digest); |
| 329 | self.hasher.update(&ch_file.bin_digest); |
| 324 | 330 | } |
| 325 | 331 | |
| 326 | 332 | /// Add a file as a dependency of process being cached, after the initial hash has been |
| ... | ... | @@ -382,7 +388,7 @@ pub const CacheHash = struct { |
| 382 | 388 | // the artifacts to cache. |
| 383 | 389 | |
| 384 | 390 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 385 | | self.blake3.final(&bin_digest); |
| 391 | self.hasher.final(&bin_digest); |
| 386 | 392 | |
| 387 | 393 | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 388 | 394 | base64_encoder.encode(&out_digest, &bin_digest); |
| ... | ... | @@ -433,17 +439,17 @@ pub const CacheHash = struct { |
| 433 | 439 | } |
| 434 | 440 | }; |
| 435 | 441 | |
| 436 | | fn hashFile(file: fs.File, bin_digest: []u8) !void { |
| 437 | | var blake3 = Blake3.init(.{}); |
| 442 | fn hashFile(file: fs.File, bin_digest: []u8, hasher_init: anytype) !void { |
| 438 | 443 | var buf: [1024]u8 = undefined; |
| 439 | 444 | |
| 445 | var hasher = hasher_init; |
| 440 | 446 | while (true) { |
| 441 | 447 | const bytes_read = try file.read(&buf); |
| 442 | 448 | if (bytes_read == 0) break; |
| 443 | | blake3.update(buf[0..bytes_read]); |
| 449 | hasher.update(buf[0..bytes_read]); |
| 444 | 450 | } |
| 445 | 451 | |
| 446 | | blake3.final(bin_digest); |
| 452 | hasher.final(bin_digest); |
| 447 | 453 | } |
| 448 | 454 | |
| 449 | 455 | /// If the wall clock time, rounded to the same precision as the |
| ... | ... | @@ -507,7 +513,7 @@ test "cache file and then recall it" { |
| 507 | 513 | _ = try ch.addFile(temp_file, null); |
| 508 | 514 | |
| 509 | 515 | // There should be nothing in the cache |
| 510 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 516 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 511 | 517 | |
| 512 | 518 | digest1 = ch.final(); |
| 513 | 519 | } |
| ... | ... | @@ -575,7 +581,7 @@ test "check that changing a file makes cache fail" { |
| 575 | 581 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 576 | 582 | |
| 577 | 583 | // There should be nothing in the cache |
| 578 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 584 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 579 | 585 | |
| 580 | 586 | testing.expect(mem.eql(u8, original_temp_file_contents, ch.files.items[temp_file_idx].contents.?)); |
| 581 | 587 | |
| ... | ... | @@ -592,7 +598,7 @@ test "check that changing a file makes cache fail" { |
| 592 | 598 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 593 | 599 | |
| 594 | 600 | // A file that we depend on has been updated, so the cache should not contain an entry for it |
| 595 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 601 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 596 | 602 | |
| 597 | 603 | // The cache system does not keep the contents of re-hashed input files. |
| 598 | 604 | testing.expect(ch.files.items[temp_file_idx].contents == null); |
| ... | ... | @@ -625,7 +631,7 @@ test "no file inputs" { |
| 625 | 631 | ch.add("1234"); |
| 626 | 632 | |
| 627 | 633 | // There should be nothing in the cache |
| 628 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 634 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 629 | 635 | |
| 630 | 636 | digest1 = ch.final(); |
| 631 | 637 | } |
| ... | ... | @@ -672,7 +678,7 @@ test "CacheHashes with files added after initial hash work" { |
| 672 | 678 | _ = try ch.addFile(temp_file1, null); |
| 673 | 679 | |
| 674 | 680 | // There should be nothing in the cache |
| 675 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 681 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 676 | 682 | |
| 677 | 683 | _ = try ch.addFilePost(temp_file2); |
| 678 | 684 | |
| ... | ... | @@ -705,7 +711,7 @@ test "CacheHashes with files added after initial hash work" { |
| 705 | 711 | _ = try ch.addFile(temp_file1, null); |
| 706 | 712 | |
| 707 | 713 | // A file that we depend on has been updated, so the cache should not contain an entry for it |
| 708 | | testing.expectEqual(@as(?[32]u8, null), try ch.hit()); |
| 714 | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 709 | 715 | |
| 710 | 716 | _ = try ch.addFilePost(temp_file2); |
| 711 | 717 | |