authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-13 19:30:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-13 19:30:22-07:00
log72768bddcd815f84ac6d40ffd80258ceaa511cb9
treed1d18e964bec1eb0689817b7943918b19393ee3c
parenta726e09389aceff39f4478f215f4991a5f148e8d

std.Build.Fuzz.WebServer: sort pcs before source location lookup

Unfortunately, the PCs do not get sorted during linking.

1 files changed, 19 insertions(+), 1 deletions(-)

lib/std/Build/Fuzz/WebServer.zig+19-1
...@@ -634,10 +634,28 @@ fn prepareTables(...@@ -634,10 +634,28 @@ fn prepareTables(
634 const pcs = header.pcAddrs();634 const pcs = header.pcAddrs();
635 const source_locations = try gpa.alloc(Coverage.SourceLocation, pcs.len);635 const source_locations = try gpa.alloc(Coverage.SourceLocation, pcs.len);
636 errdefer gpa.free(source_locations);636 errdefer gpa.free(source_locations);
637 debug_info.resolveAddresses(gpa, pcs, source_locations) catch |err| {637
638 // Unfortunately the PCs array that LLVM gives us from the 8-bit PC
639 // counters feature is not sorted.
640 var sorted_pcs: std.MultiArrayList(struct { pc: u64, index: u32, sl: Coverage.SourceLocation }) = .{};
641 defer sorted_pcs.deinit(gpa);
642 try sorted_pcs.resize(gpa, pcs.len);
643 @memcpy(sorted_pcs.items(.pc), pcs);
644 for (sorted_pcs.items(.index), 0..) |*v, i| v.* = @intCast(i);
645 sorted_pcs.sortUnstable(struct {
646 addrs: []const u64,
647
648 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
649 return ctx.addrs[a_index] < ctx.addrs[b_index];
650 }
651 }{ .addrs = sorted_pcs.items(.pc) });
652
653 debug_info.resolveAddresses(gpa, sorted_pcs.items(.pc), sorted_pcs.items(.sl)) catch |err| {
638 log.err("failed to resolve addresses to source locations: {s}", .{@errorName(err)});654 log.err("failed to resolve addresses to source locations: {s}", .{@errorName(err)});
639 return error.AlreadyReported;655 return error.AlreadyReported;
640 };656 };
657
658 for (sorted_pcs.items(.index), sorted_pcs.items(.sl)) |i, sl| source_locations[i] = sl;
641 gop.value_ptr.source_locations = source_locations;659 gop.value_ptr.source_locations = source_locations;
642660
643 ws.coverage_condition.broadcast();661 ws.coverage_condition.broadcast();