authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-09 19:49:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-13 15:02:53-07:00
logb5398180d6b362346522a6067d54b90b97e23dc2
tree227d414866a1052fd4197bb0b32e74e173300ef9
parent0b5ea2b902b5802786cac70740e93872d2a0973d

std.debug.Coverage.resolveAddressesDwarf: fix broken logic

The implementation assumed that compilation units did not overlap, which is not the case. The new implementation uses .debug_ranges to iterate over the requested PCs. This partially resolves #20990. The dump-cov tool is fixed but the same fix needs to be applied to `std.Build.Fuzz.WebServer` (sorting the PC list before passing it to be resolved by debug info). I am observing LLVM emit multiple 8-bit counters for the same PC addresses when enabling `-fsanitize-coverage=inline-8bit-counters`. This seems like a bug in LLVM. I can't fathom why that would be desireable.

5 files changed, 91 insertions(+), 76 deletions(-)

lib/std/debug/Coverage.zig+18-21
......@@ -151,46 +151,35 @@ pub fn resolveAddressesDwarf(
151151 d: *Dwarf,
152152) ResolveAddressesDwarfError!void {
153153 assert(sorted_pc_addrs.len == output.len);
154 assert(d.compile_units_sorted);
154 assert(d.ranges.items.len != 0); // call `populateRanges` first.
155155
156 var cu_i: usize = 0;
157 var line_table_i: usize = 0;
158 var cu: *Dwarf.CompileUnit = &d.compile_unit_list.items[0];
159 var range = cu.pc_range.?;
156 var range_i: usize = 0;
157 var range: *std.debug.Dwarf.Range = &d.ranges.items[0];
158 var line_table_i: usize = undefined;
159 var prev_cu: ?*std.debug.Dwarf.CompileUnit = null;
160160 // Protects directories and files tables from other threads.
161161 cov.mutex.lock();
162162 defer cov.mutex.unlock();
163163 next_pc: for (sorted_pc_addrs, output) |pc, *out| {
164164 while (pc >= range.end) {
165 cu_i += 1;
166 if (cu_i >= d.compile_unit_list.items.len) {
165 range_i += 1;
166 if (range_i >= d.ranges.items.len) {
167167 out.* = SourceLocation.invalid;
168168 continue :next_pc;
169169 }
170 cu = &d.compile_unit_list.items[cu_i];
171 line_table_i = 0;
172 range = cu.pc_range orelse {
173 out.* = SourceLocation.invalid;
174 continue :next_pc;
175 };
170 range = &d.ranges.items[range_i];
176171 }
177172 if (pc < range.start) {
178173 out.* = SourceLocation.invalid;
179174 continue :next_pc;
180175 }
181 if (line_table_i == 0) {
182 line_table_i = 1;
176 const cu = &d.compile_unit_list.items[range.compile_unit_index];
177 if (cu.src_loc_cache == null) {
183178 cov.mutex.unlock();
184179 defer cov.mutex.lock();
185180 d.populateSrcLocCache(gpa, cu) catch |err| switch (err) {
186181 error.MissingDebugInfo, error.InvalidDebugInfo => {
187182 out.* = SourceLocation.invalid;
188 cu_i += 1;
189 if (cu_i < d.compile_unit_list.items.len) {
190 cu = &d.compile_unit_list.items[cu_i];
191 line_table_i = 0;
192 if (cu.pc_range) |r| range = r;
193 }
194183 continue :next_pc;
195184 },
196185 else => |e| return e,
......@@ -198,6 +187,14 @@ pub fn resolveAddressesDwarf(
198187 }
199188 const slc = &cu.src_loc_cache.?;
200189 const table_addrs = slc.line_table.keys();
190 if (cu != prev_cu) {
191 prev_cu = cu;
192 line_table_i = std.sort.upperBound(u64, table_addrs, pc, struct {
193 fn order(context: u64, item: u64) std.math.Order {
194 return std.math.order(item, context);
195 }
196 }.order);
197 }
201198 while (line_table_i < table_addrs.len and table_addrs[line_table_i] < pc) line_table_i += 1;
202199
203200 const entry = slc.line_table.values()[line_table_i - 1];
lib/std/debug/Dwarf.zig+54-42
......@@ -38,19 +38,30 @@ pub const call_frame = @import("Dwarf/call_frame.zig");
3838endian: std.builtin.Endian,
3939sections: SectionArray = null_section_array,
4040is_macho: bool,
41compile_units_sorted: bool,
4241
43// Filled later by the initializer
42/// Filled later by the initializer
4443abbrev_table_list: std.ArrayListUnmanaged(Abbrev.Table) = .{},
44/// Filled later by the initializer
4545compile_unit_list: std.ArrayListUnmanaged(CompileUnit) = .{},
46/// Filled later by the initializer
4647func_list: std.ArrayListUnmanaged(Func) = .{},
4748
4849eh_frame_hdr: ?ExceptionFrameHeader = null,
49// These lookup tables are only used if `eh_frame_hdr` is null
50/// These lookup tables are only used if `eh_frame_hdr` is null
5051cie_map: std.AutoArrayHashMapUnmanaged(u64, CommonInformationEntry) = .{},
51// Sorted by start_pc
52/// Sorted by start_pc
5253fde_list: std.ArrayListUnmanaged(FrameDescriptionEntry) = .{},
5354
55/// Populated by `populateRanges`.
56ranges: std.ArrayListUnmanaged(Range) = .{},
57
58pub const Range = struct {
59 start: u64,
60 end: u64,
61 /// Index into `compile_unit_list`.
62 compile_unit_index: usize,
63};
64
5465pub const Section = struct {
5566 data: []const u8,
5667 // Module-relative virtual address.
......@@ -799,6 +810,7 @@ pub fn deinit(di: *Dwarf, gpa: Allocator) void {
799810 di.func_list.deinit(gpa);
800811 di.cie_map.deinit(gpa);
801812 di.fde_list.deinit(gpa);
813 di.ranges.deinit(gpa);
802814 di.* = undefined;
803815}
804816
......@@ -985,8 +997,8 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void {
985997 try di.func_list.append(allocator, .{
986998 .name = fn_name,
987999 .pc_range = .{
988 .start = range.start_addr,
989 .end = range.end_addr,
1000 .start = range.start,
1001 .end = range.end,
9901002 },
9911003 });
9921004 }
......@@ -1096,37 +1108,38 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator) ScanError!void {
10961108 }
10971109}
10981110
1099/// Populate missing PC ranges in compilation units, and then sort them by start address.
1100/// Does not guarantee pc_range to be non-null because there could be missing debug info.
1101pub fn sortCompileUnits(d: *Dwarf) ScanError!void {
1102 assert(!d.compile_units_sorted);
1111pub fn populateRanges(d: *Dwarf, gpa: Allocator) ScanError!void {
1112 assert(d.ranges.items.len == 0);
11031113
1104 for (d.compile_unit_list.items) |*cu| {
1105 if (cu.pc_range != null) continue;
1114 for (d.compile_unit_list.items, 0..) |*cu, cu_index| {
1115 if (cu.pc_range) |range| {
1116 try d.ranges.append(gpa, .{
1117 .start = range.start,
1118 .end = range.end,
1119 .compile_unit_index = cu_index,
1120 });
1121 continue;
1122 }
11061123 const ranges_value = cu.die.getAttr(AT.ranges) orelse continue;
11071124 var iter = DebugRangeIterator.init(ranges_value, d, cu) catch continue;
1108 var start: u64 = maxInt(u64);
1109 var end: u64 = 0;
11101125 while (try iter.next()) |range| {
1111 start = @min(start, range.start_addr);
1112 end = @max(end, range.end_addr);
1126 // Not sure why LLVM thinks it's OK to emit these...
1127 if (range.start == range.end) continue;
1128
1129 try d.ranges.append(gpa, .{
1130 .start = range.start,
1131 .end = range.end,
1132 .compile_unit_index = cu_index,
1133 });
11131134 }
1114 if (end != 0) cu.pc_range = .{
1115 .start = start,
1116 .end = end,
1117 };
11181135 }
11191136
1120 std.mem.sortUnstable(CompileUnit, d.compile_unit_list.items, {}, struct {
1121 pub fn lessThan(ctx: void, a: CompileUnit, b: CompileUnit) bool {
1137 std.mem.sortUnstable(Range, d.ranges.items, {}, struct {
1138 pub fn lessThan(ctx: void, a: Range, b: Range) bool {
11221139 _ = ctx;
1123 const a_range = a.pc_range orelse return false;
1124 const b_range = b.pc_range orelse return true;
1125 return a_range.start < b_range.start;
1140 return a.start < b.start;
11261141 }
11271142 }.lessThan);
1128
1129 d.compile_units_sorted = true;
11301143}
11311144
11321145const DebugRangeIterator = struct {
......@@ -1184,7 +1197,7 @@ const DebugRangeIterator = struct {
11841197 }
11851198
11861199 // Returns the next range in the list, or null if the end was reached.
1187 pub fn next(self: *@This()) !?struct { start_addr: u64, end_addr: u64 } {
1200 pub fn next(self: *@This()) !?PcRange {
11881201 switch (self.section_type) {
11891202 .debug_rnglists => {
11901203 const kind = try self.fbr.readByte();
......@@ -1203,8 +1216,8 @@ const DebugRangeIterator = struct {
12031216 const end_addr = try self.di.readDebugAddr(self.compile_unit.*, end_index);
12041217
12051218 return .{
1206 .start_addr = start_addr,
1207 .end_addr = end_addr,
1219 .start = start_addr,
1220 .end = end_addr,
12081221 };
12091222 },
12101223 RLE.startx_length => {
......@@ -1215,8 +1228,8 @@ const DebugRangeIterator = struct {
12151228 const end_addr = start_addr + len;
12161229
12171230 return .{
1218 .start_addr = start_addr,
1219 .end_addr = end_addr,
1231 .start = start_addr,
1232 .end = end_addr,
12201233 };
12211234 },
12221235 RLE.offset_pair => {
......@@ -1225,8 +1238,8 @@ const DebugRangeIterator = struct {
12251238
12261239 // This is the only kind that uses the base address
12271240 return .{
1228 .start_addr = self.base_address + start_addr,
1229 .end_addr = self.base_address + end_addr,
1241 .start = self.base_address + start_addr,
1242 .end = self.base_address + end_addr,
12301243 };
12311244 },
12321245 RLE.base_address => {
......@@ -1238,8 +1251,8 @@ const DebugRangeIterator = struct {
12381251 const end_addr = try self.fbr.readInt(usize);
12391252
12401253 return .{
1241 .start_addr = start_addr,
1242 .end_addr = end_addr,
1254 .start = start_addr,
1255 .end = end_addr,
12431256 };
12441257 },
12451258 RLE.start_length => {
......@@ -1248,8 +1261,8 @@ const DebugRangeIterator = struct {
12481261 const end_addr = start_addr + len;
12491262
12501263 return .{
1251 .start_addr = start_addr,
1252 .end_addr = end_addr,
1264 .start = start_addr,
1265 .end = end_addr,
12531266 };
12541267 },
12551268 else => return bad(),
......@@ -1267,8 +1280,8 @@ const DebugRangeIterator = struct {
12671280 }
12681281
12691282 return .{
1270 .start_addr = self.base_address + start_addr,
1271 .end_addr = self.base_address + end_addr,
1283 .start = self.base_address + start_addr,
1284 .end = self.base_address + end_addr,
12721285 };
12731286 },
12741287 else => unreachable,
......@@ -1286,7 +1299,7 @@ pub fn findCompileUnit(di: *const Dwarf, target_address: u64) !*CompileUnit {
12861299 const ranges_value = compile_unit.die.getAttr(AT.ranges) orelse continue;
12871300 var iter = DebugRangeIterator.init(ranges_value, di, compile_unit) catch continue;
12881301 while (try iter.next()) |range| {
1289 if (target_address >= range.start_addr and target_address < range.end_addr) return compile_unit;
1302 if (target_address >= range.start and target_address < range.end) return compile_unit;
12901303 }
12911304 }
12921305
......@@ -2345,7 +2358,6 @@ pub const ElfModule = struct {
23452358 .endian = endian,
23462359 .sections = sections,
23472360 .is_macho = false,
2348 .compile_units_sorted = false,
23492361 };
23502362
23512363 try Dwarf.open(&di, gpa);
lib/std/debug/Info.zig+1-1
......@@ -27,7 +27,7 @@ pub const LoadError = Dwarf.ElfModule.LoadError;
2727pub fn load(gpa: Allocator, path: Path, coverage: *Coverage) LoadError!Info {
2828 var sections: Dwarf.SectionArray = Dwarf.null_section_array;
2929 var elf_module = try Dwarf.ElfModule.loadPath(gpa, path, null, null, &sections, null);
30 try elf_module.dwarf.sortCompileUnits();
30 try elf_module.dwarf.populateRanges(gpa);
3131 var info: Info = .{
3232 .address_map = .{},
3333 .coverage = coverage,
lib/std/debug/SelfInfo.zig-3
......@@ -606,7 +606,6 @@ pub const Module = switch (native_os) {
606606 .endian = .little,
607607 .sections = sections,
608608 .is_macho = true,
609 .compile_units_sorted = false,
610609 };
611610
612611 try Dwarf.open(&di, allocator);
......@@ -996,7 +995,6 @@ fn readCoffDebugInfo(allocator: Allocator, coff_obj: *coff.Coff) !Module {
996995 .endian = native_endian,
997996 .sections = sections,
998997 .is_macho = false,
999 .compile_units_sorted = false,
1000998 };
1001999
10021000 try Dwarf.open(&dwarf, allocator);
......@@ -1810,7 +1808,6 @@ fn unwindFrameMachODwarf(
18101808 var di: Dwarf = .{
18111809 .endian = native_endian,
18121810 .is_macho = true,
1813 .compile_units_sorted = false,
18141811 };
18151812 defer di.deinit(context.allocator);
18161813
tools/dump-cov.zig+18-9
......@@ -54,21 +54,30 @@ pub fn main() !void {
5454 const header: *SeenPcsHeader = @ptrCast(cov_bytes);
5555 try stdout.print("{any}\n", .{header.*});
5656 const pcs = header.pcAddrs();
57 for (0.., pcs[0 .. pcs.len - 1], pcs[1..]) |i, a, b| {
58 if (a > b) std.log.err("{d}: 0x{x} > 0x{x}", .{ i, a, b });
59 }
60 assert(std.sort.isSorted(usize, pcs, {}, std.sort.asc(usize)));
6157
62 const seen_pcs = header.seenBits();
58 var indexed_pcs: std.AutoArrayHashMapUnmanaged(usize, void) = .{};
59 try indexed_pcs.entries.resize(arena, pcs.len);
60 @memcpy(indexed_pcs.entries.items(.key), pcs);
61 try indexed_pcs.reIndex(arena);
62
63 const sorted_pcs = try arena.dupe(usize, pcs);
64 std.mem.sortUnstable(usize, sorted_pcs, {}, std.sort.asc(usize));
6365
64 const source_locations = try arena.alloc(std.debug.Coverage.SourceLocation, pcs.len);
65 try debug_info.resolveAddresses(gpa, pcs, source_locations);
66 const source_locations = try arena.alloc(std.debug.Coverage.SourceLocation, sorted_pcs.len);
67 try debug_info.resolveAddresses(gpa, sorted_pcs, source_locations);
68
69 const seen_pcs = header.seenBits();
6670
67 for (pcs, source_locations, 0..) |pc, sl, i| {
71 for (sorted_pcs, source_locations) |pc, sl| {
72 if (sl.file == .invalid) {
73 try stdout.print(" {x}: invalid\n", .{pc});
74 continue;
75 }
6876 const file = debug_info.coverage.fileAt(sl.file);
6977 const dir_name = debug_info.coverage.directories.keys()[file.directory_index];
7078 const dir_name_slice = debug_info.coverage.stringAt(dir_name);
71 const hit: u1 = @truncate(seen_pcs[i / @bitSizeOf(usize)] >> @intCast(i % @bitSizeOf(usize)));
79 const seen_i = indexed_pcs.getIndex(pc).?;
80 const hit: u1 = @truncate(seen_pcs[seen_i / @bitSizeOf(usize)] >> @intCast(seen_i % @bitSizeOf(usize)));
7281 try stdout.print("{c}{x}: {s}/{s}:{d}:{d}\n", .{
7382 "-+"[hit], pc, dir_name_slice, debug_info.coverage.stringAt(file.basename), sl.line, sl.column,
7483 });