| ... | @@ -7,11 +7,10 @@ const log = std.log.scoped(.link); | ... | @@ -7,11 +7,10 @@ const log = std.log.scoped(.link); |
| 7 | const macho = std.macho; | 7 | const macho = std.macho; |
| 8 | const mem = std.mem; | 8 | const mem = std.mem; |
| 9 | const testing = std.testing; | 9 | const testing = std.testing; |
| 10 | const ThreadPool = std.Thread.Pool; | | |
| 11 | const WaitGroup = std.Thread.WaitGroup; | | |
| 12 | | 10 | |
| 13 | const Allocator = mem.Allocator; | 11 | const Allocator = mem.Allocator; |
| 14 | const Compilation = @import("../../Compilation.zig"); | 12 | const Compilation = @import("../../Compilation.zig"); |
| | 13 | const Hasher = @import("hasher.zig").ParallelHasher; |
| 15 | const Sha256 = std.crypto.hash.sha2.Sha256; | 14 | const Sha256 = std.crypto.hash.sha2.Sha256; |
| 16 | | 15 | |
| 17 | const hash_size = Sha256.digest_length; | 16 | const hash_size = Sha256.digest_length; |
| ... | @@ -289,7 +288,11 @@ pub fn writeAdhocSignature( | ... | @@ -289,7 +288,11 @@ pub fn writeAdhocSignature( |
| 289 | self.code_directory.inner.nCodeSlots = total_pages; | 288 | self.code_directory.inner.nCodeSlots = total_pages; |
| 290 | | 289 | |
| 291 | // Calculate hash for each page (in file) and write it to the buffer | 290 | // Calculate hash for each page (in file) and write it to the buffer |
| 292 | try self.parallelHash(gpa, comp.thread_pool, opts.file, opts.file_size); | 291 | var hasher = Hasher(Sha256){}; |
| | 292 | try hasher.hash(gpa, comp.thread_pool, opts.file, self.code_directory.code_slots.items, .{ |
| | 293 | .chunk_size = self.page_size, |
| | 294 | .max_file_size = opts.file_size, |
| | 295 | }); |
| 293 | | 296 | |
| 294 | try blobs.append(.{ .code_directory = &self.code_directory }); | 297 | try blobs.append(.{ .code_directory = &self.code_directory }); |
| 295 | header.length += @sizeOf(macho.BlobIndex); | 298 | header.length += @sizeOf(macho.BlobIndex); |
| ... | @@ -348,62 +351,6 @@ pub fn writeAdhocSignature( | ... | @@ -348,62 +351,6 @@ pub fn writeAdhocSignature( |
| 348 | } | 351 | } |
| 349 | } | 352 | } |
| 350 | | 353 | |
| 351 | fn parallelHash( | | |
| 352 | self: *CodeSignature, | | |
| 353 | gpa: Allocator, | | |
| 354 | pool: *ThreadPool, | | |
| 355 | file: fs.File, | | |
| 356 | file_size: u32, | | |
| 357 | ) !void { | | |
| 358 | var wg: WaitGroup = .{}; | | |
| 359 | | | |
| 360 | const total_num_chunks = mem.alignForward(usize, file_size, self.page_size) / self.page_size; | | |
| 361 | assert(self.code_directory.code_slots.items.len >= total_num_chunks); | | |
| 362 | | | |
| 363 | const buffer = try gpa.alloc(u8, self.page_size * total_num_chunks); | | |
| 364 | defer gpa.free(buffer); | | |
| 365 | | | |
| 366 | const results = try gpa.alloc(fs.File.PReadError!usize, total_num_chunks); | | |
| 367 | defer gpa.free(results); | | |
| 368 | | | |
| 369 | { | | |
| 370 | wg.reset(); | | |
| 371 | defer wg.wait(); | | |
| 372 | | | |
| 373 | var i: usize = 0; | | |
| 374 | while (i < total_num_chunks) : (i += 1) { | | |
| 375 | const fstart = i * self.page_size; | | |
| 376 | const fsize = if (fstart + self.page_size > file_size) | | |
| 377 | file_size - fstart | | |
| 378 | else | | |
| 379 | self.page_size; | | |
| 380 | wg.start(); | | |
| 381 | try pool.spawn(worker, .{ | | |
| 382 | file, | | |
| 383 | fstart, | | |
| 384 | buffer[fstart..][0..fsize], | | |
| 385 | &self.code_directory.code_slots.items[i], | | |
| 386 | &results[i], | | |
| 387 | &wg, | | |
| 388 | }); | | |
| 389 | } | | |
| 390 | } | | |
| 391 | for (results) |result| _ = try result; | | |
| 392 | } | | |
| 393 | | | |
| 394 | fn worker( | | |
| 395 | file: fs.File, | | |
| 396 | fstart: usize, | | |
| 397 | buffer: []u8, | | |
| 398 | out: *[hash_size]u8, | | |
| 399 | err: *fs.File.PReadError!usize, | | |
| 400 | wg: *WaitGroup, | | |
| 401 | ) void { | | |
| 402 | defer wg.finish(); | | |
| 403 | err.* = file.preadAll(buffer, fstart); | | |
| 404 | Sha256.hash(buffer, out, .{}); | | |
| 405 | } | | |
| 406 | | | |
| 407 | pub fn size(self: CodeSignature) u32 { | 354 | pub fn size(self: CodeSignature) u32 { |
| 408 | var ssize: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex) + self.code_directory.size(); | 355 | var ssize: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex) + self.code_directory.size(); |
| 409 | if (self.requirements) |req| { | 356 | if (self.requirements) |req| { |