| ... | @@ -4,7 +4,8 @@ | ... | @@ -4,7 +4,8 @@ |
| 4 | // The MIT license requires this copyright notice to be included in all copies | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | // and substantial portions of the software. | 5 | // and substantial portions of the software. |
| 6 | const std = @import("std.zig"); | 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 | const fs = std.fs; | 9 | const fs = std.fs; |
| 9 | const base64 = std.base64; | 10 | const base64 = std.base64; |
| 10 | const ArrayList = std.ArrayList; | 11 | const ArrayList = std.ArrayList; |
| ... | @@ -16,9 +17,8 @@ const Allocator = std.mem.Allocator; | ... | @@ -16,9 +17,8 @@ const Allocator = std.mem.Allocator; |
| 16 | | 17 | |
| 17 | const base64_encoder = fs.base64_encoder; | 18 | const base64_encoder = fs.base64_encoder; |
| 18 | const base64_decoder = fs.base64_decoder; | 19 | const base64_decoder = fs.base64_decoder; |
| 19 | /// This is 70 more bits than UUIDs. For an analysis of probability of collisions, see: | 20 | /// This is 128 bits - Even with 2^54 cache entries, the probably of a collision would be under 10^-6 |
| 20 | /// https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions | 21 | const BIN_DIGEST_LEN = 16; |
| 21 | const BIN_DIGEST_LEN = 24; | | |
| 22 | const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); | 22 | const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); |
| 23 | | 23 | |
| 24 | const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024; | 24 | const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024; |
| ... | @@ -43,9 +43,13 @@ pub const File = struct { | ... | @@ -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 | pub const CacheHash = struct { | 49 | pub const CacheHash = struct { |
| 47 | allocator: *Allocator, | 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 | manifest_dir: fs.Dir, | 53 | manifest_dir: fs.Dir, |
| 50 | manifest_file: ?fs.File, | 54 | manifest_file: ?fs.File, |
| 51 | manifest_dirty: bool, | 55 | manifest_dirty: bool, |
| ... | @@ -54,9 +58,11 @@ pub const CacheHash = struct { | ... | @@ -54,9 +58,11 @@ pub const CacheHash = struct { |
| 54 | | 58 | |
| 55 | /// Be sure to call release after successful initialization. | 59 | /// Be sure to call release after successful initialization. |
| 56 | pub fn init(allocator: *Allocator, dir: fs.Dir, manifest_dir_path: []const u8) !CacheHash { | 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 | return CacheHash{ | 62 | return CacheHash{ |
| 58 | .allocator = allocator, | 63 | .allocator = allocator, |
| 59 | .blake3 = Blake3.init(.{}), | 64 | .hasher_init = hasher_init, |
| | 65 | .hasher = hasher_init, |
| 60 | .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}), | 66 | .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}), |
| 61 | .manifest_file = null, | 67 | .manifest_file = null, |
| 62 | .manifest_dirty = false, | 68 | .manifest_dirty = false, |
| ... | @@ -69,8 +75,8 @@ pub const CacheHash = struct { | ... | @@ -69,8 +75,8 @@ pub const CacheHash = struct { |
| 69 | pub fn addSlice(self: *CacheHash, val: []const u8) void { | 75 | pub fn addSlice(self: *CacheHash, val: []const u8) void { |
| 70 | assert(self.manifest_file == null); | 76 | assert(self.manifest_file == null); |
| 71 | | 77 | |
| 72 | self.blake3.update(val); | 78 | self.hasher.update(val); |
| 73 | self.blake3.update(&[_]u8{0}); | 79 | self.hasher.update(&[_]u8{0}); |
| 74 | } | 80 | } |
| 75 | | 81 | |
| 76 | /// Convert the input value into bytes and record it as a dependency of the | 82 | /// Convert the input value into bytes and record it as a dependency of the |
| ... | @@ -133,12 +139,12 @@ pub const CacheHash = struct { | ... | @@ -133,12 +139,12 @@ pub const CacheHash = struct { |
| 133 | assert(self.manifest_file == null); | 139 | assert(self.manifest_file == null); |
| 134 | | 140 | |
| 135 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; | 141 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 136 | self.blake3.final(&bin_digest); | 142 | self.hasher.final(&bin_digest); |
| 137 | | 143 | |
| 138 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); | 144 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); |
| 139 | | 145 | |
| 140 | self.blake3 = Blake3.init(.{}); | 146 | self.hasher = self.hasher_init; |
| 141 | self.blake3.update(&bin_digest); | 147 | self.hasher.update(&bin_digest); |
| 142 | | 148 | |
| 143 | const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest}); | 149 | const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest}); |
| 144 | defer self.allocator.free(manifest_file_path); | 150 | defer self.allocator.free(manifest_file_path); |
| ... | @@ -238,7 +244,7 @@ pub const CacheHash = struct { | ... | @@ -238,7 +244,7 @@ pub const CacheHash = struct { |
| 238 | } | 244 | } |
| 239 | | 245 | |
| 240 | var actual_digest: [BIN_DIGEST_LEN]u8 = undefined; | 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 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { | 249 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| 244 | cache_hash_file.bin_digest = actual_digest; | 250 | cache_hash_file.bin_digest = actual_digest; |
| ... | @@ -248,7 +254,7 @@ pub const CacheHash = struct { | ... | @@ -248,7 +254,7 @@ pub const CacheHash = struct { |
| 248 | } | 254 | } |
| 249 | | 255 | |
| 250 | if (!any_file_changed) { | 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,8 +262,8 @@ pub const CacheHash = struct { |
| 256 | // cache miss | 262 | // cache miss |
| 257 | // keep the manifest file open | 263 | // keep the manifest file open |
| 258 | // reset the hash | 264 | // reset the hash |
| 259 | self.blake3 = Blake3.init(.{}); | 265 | self.hasher = self.hasher_init; |
| 260 | self.blake3.update(&bin_digest); | 266 | self.hasher.update(&bin_digest); |
| 261 | | 267 | |
| 262 | // Remove files not in the initial hash | 268 | // Remove files not in the initial hash |
| 263 | for (self.files.items[input_file_count..]) |*file| { | 269 | for (self.files.items[input_file_count..]) |*file| { |
| ... | @@ -266,7 +272,7 @@ pub const CacheHash = struct { | ... | @@ -266,7 +272,7 @@ pub const CacheHash = struct { |
| 266 | self.files.shrink(input_file_count); | 272 | self.files.shrink(input_file_count); |
| 267 | | 273 | |
| 268 | for (self.files.items) |file| { | 274 | for (self.files.items) |file| { |
| 269 | self.blake3.update(&file.bin_digest); | 275 | self.hasher.update(&file.bin_digest); |
| 270 | } | 276 | } |
| 271 | return null; | 277 | return null; |
| 272 | } | 278 | } |
| ... | @@ -304,23 +310,23 @@ pub const CacheHash = struct { | ... | @@ -304,23 +310,23 @@ pub const CacheHash = struct { |
| 304 | | 310 | |
| 305 | // Hash while reading from disk, to keep the contents in the cpu cache while | 311 | // Hash while reading from disk, to keep the contents in the cpu cache while |
| 306 | // doing hashing. | 312 | // doing hashing. |
| 307 | var blake3 = Blake3.init(.{}); | 313 | var hasher = self.hasher_init; |
| 308 | var off: usize = 0; | 314 | var off: usize = 0; |
| 309 | while (true) { | 315 | while (true) { |
| 310 | // give me everything you've got, captain | 316 | // give me everything you've got, captain |
| 311 | const bytes_read = try file.read(contents[off..]); | 317 | const bytes_read = try file.read(contents[off..]); |
| 312 | if (bytes_read == 0) break; | 318 | if (bytes_read == 0) break; |
| 313 | blake3.update(contents[off..][0..bytes_read]); | 319 | hasher.update(contents[off..][0..bytes_read]); |
| 314 | off += bytes_read; | 320 | off += bytes_read; |
| 315 | } | 321 | } |
| 316 | blake3.final(&ch_file.bin_digest); | 322 | hasher.final(&ch_file.bin_digest); |
| 317 | | 323 | |
| 318 | ch_file.contents = contents; | 324 | ch_file.contents = contents; |
| 319 | } else { | 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 | /// Add a file as a dependency of process being cached, after the initial hash has been | 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,7 +388,7 @@ pub const CacheHash = struct { |
| 382 | // the artifacts to cache. | 388 | // the artifacts to cache. |
| 383 | | 389 | |
| 384 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; | 390 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 385 | self.blake3.final(&bin_digest); | 391 | self.hasher.final(&bin_digest); |
| 386 | | 392 | |
| 387 | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; | 393 | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 388 | base64_encoder.encode(&out_digest, &bin_digest); | 394 | base64_encoder.encode(&out_digest, &bin_digest); |
| ... | @@ -433,17 +439,17 @@ pub const CacheHash = struct { | ... | @@ -433,17 +439,17 @@ pub const CacheHash = struct { |
| 433 | } | 439 | } |
| 434 | }; | 440 | }; |
| 435 | | 441 | |
| 436 | fn hashFile(file: fs.File, bin_digest: []u8) !void { | 442 | fn hashFile(file: fs.File, bin_digest: []u8, hasher_init: anytype) !void { |
| 437 | var blake3 = Blake3.init(.{}); | | |
| 438 | var buf: [1024]u8 = undefined; | 443 | var buf: [1024]u8 = undefined; |
| 439 | | 444 | |
| | 445 | var hasher = hasher_init; |
| 440 | while (true) { | 446 | while (true) { |
| 441 | const bytes_read = try file.read(&buf); | 447 | const bytes_read = try file.read(&buf); |
| 442 | if (bytes_read == 0) break; | 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 | /// If the wall clock time, rounded to the same precision as the | 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,7 +513,7 @@ test "cache file and then recall it" { |
| 507 | _ = try ch.addFile(temp_file, null); | 513 | _ = try ch.addFile(temp_file, null); |
| 508 | | 514 | |
| 509 | // There should be nothing in the cache | 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 | digest1 = ch.final(); | 518 | digest1 = ch.final(); |
| 513 | } | 519 | } |
| ... | @@ -575,7 +581,7 @@ test "check that changing a file makes cache fail" { | ... | @@ -575,7 +581,7 @@ test "check that changing a file makes cache fail" { |
| 575 | const temp_file_idx = try ch.addFile(temp_file, 100); | 581 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 576 | | 582 | |
| 577 | // There should be nothing in the cache | 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 | testing.expect(mem.eql(u8, original_temp_file_contents, ch.files.items[temp_file_idx].contents.?)); | 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,7 +598,7 @@ test "check that changing a file makes cache fail" { |
| 592 | const temp_file_idx = try ch.addFile(temp_file, 100); | 598 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 593 | | 599 | |
| 594 | // A file that we depend on has been updated, so the cache should not contain an entry for it | 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 | // The cache system does not keep the contents of re-hashed input files. | 603 | // The cache system does not keep the contents of re-hashed input files. |
| 598 | testing.expect(ch.files.items[temp_file_idx].contents == null); | 604 | testing.expect(ch.files.items[temp_file_idx].contents == null); |
| ... | @@ -625,7 +631,7 @@ test "no file inputs" { | ... | @@ -625,7 +631,7 @@ test "no file inputs" { |
| 625 | ch.add("1234"); | 631 | ch.add("1234"); |
| 626 | | 632 | |
| 627 | // There should be nothing in the cache | 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 | digest1 = ch.final(); | 636 | digest1 = ch.final(); |
| 631 | } | 637 | } |
| ... | @@ -672,7 +678,7 @@ test "CacheHashes with files added after initial hash work" { | ... | @@ -672,7 +678,7 @@ test "CacheHashes with files added after initial hash work" { |
| 672 | _ = try ch.addFile(temp_file1, null); | 678 | _ = try ch.addFile(temp_file1, null); |
| 673 | | 679 | |
| 674 | // There should be nothing in the cache | 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 | _ = try ch.addFilePost(temp_file2); | 683 | _ = try ch.addFilePost(temp_file2); |
| 678 | | 684 | |
| ... | @@ -705,7 +711,7 @@ test "CacheHashes with files added after initial hash work" { | ... | @@ -705,7 +711,7 @@ test "CacheHashes with files added after initial hash work" { |
| 705 | _ = try ch.addFile(temp_file1, null); | 711 | _ = try ch.addFile(temp_file1, null); |
| 706 | | 712 | |
| 707 | // A file that we depend on has been updated, so the cache should not contain an entry for it | 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 | _ = try ch.addFilePost(temp_file2); | 716 | _ = try ch.addFilePost(temp_file2); |
| 711 | | 717 | |