diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-08-14 04:55:32 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-14 04:55:32 -0700 |
| commit | b7a1ef3e19039690b182a295dff7cffe5fcc521f (patch) | |
| tree | d62b8bdee5b8f332941cd846435bddf667853341 /lib/std/debug/Coverage.zig | |
| parent | b470d2a7de20f8036c8976ec18ed1fb16a15c662 (diff) | |
| parent | 72768bddcd815f84ac6d40ffd80258ceaa511cb9 (diff) | |
| download | zig-b7a1ef3e19039690b182a295dff7cffe5fcc521f.tar.gz zig-b7a1ef3e19039690b182a295dff7cffe5fcc521f.zip | |
Merge pull request #21075 from ziglang/fuzz
fix several debug info bugs
Diffstat (limited to 'lib/std/debug/Coverage.zig')
| -rw-r--r-- | lib/std/debug/Coverage.zig | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/lib/std/debug/Coverage.zig b/lib/std/debug/Coverage.zig index f341efaffb..2d0e054673 100644 --- a/lib/std/debug/Coverage.zig +++ b/lib/std/debug/Coverage.zig @@ -145,60 +145,64 @@ pub const ResolveAddressesDwarfError = Dwarf.ScanError; pub fn resolveAddressesDwarf( cov: *Coverage, gpa: Allocator, + /// Asserts the addresses are in ascending order. sorted_pc_addrs: []const u64, /// Asserts its length equals length of `sorted_pc_addrs`. output: []SourceLocation, d: *Dwarf, ) ResolveAddressesDwarfError!void { assert(sorted_pc_addrs.len == output.len); - assert(d.compile_units_sorted); + assert(d.ranges.items.len != 0); // call `populateRanges` first. - var cu_i: usize = 0; - var line_table_i: usize = 0; - var cu: *Dwarf.CompileUnit = &d.compile_unit_list.items[0]; - var range = cu.pc_range.?; + var range_i: usize = 0; + var range: *std.debug.Dwarf.Range = &d.ranges.items[0]; + var line_table_i: usize = undefined; + var prev_pc: u64 = 0; + var prev_cu: ?*std.debug.Dwarf.CompileUnit = null; // Protects directories and files tables from other threads. cov.mutex.lock(); defer cov.mutex.unlock(); next_pc: for (sorted_pc_addrs, output) |pc, *out| { + assert(pc >= prev_pc); + prev_pc = pc; + while (pc >= range.end) { - cu_i += 1; - if (cu_i >= d.compile_unit_list.items.len) { + range_i += 1; + if (range_i >= d.ranges.items.len) { out.* = SourceLocation.invalid; continue :next_pc; } - cu = &d.compile_unit_list.items[cu_i]; - line_table_i = 0; - range = cu.pc_range orelse { - out.* = SourceLocation.invalid; - continue :next_pc; - }; + range = &d.ranges.items[range_i]; } if (pc < range.start) { out.* = SourceLocation.invalid; continue :next_pc; } - if (line_table_i == 0) { - line_table_i = 1; - cov.mutex.unlock(); - defer cov.mutex.lock(); - d.populateSrcLocCache(gpa, cu) catch |err| switch (err) { - error.MissingDebugInfo, error.InvalidDebugInfo => { - out.* = SourceLocation.invalid; - cu_i += 1; - if (cu_i < d.compile_unit_list.items.len) { - cu = &d.compile_unit_list.items[cu_i]; - line_table_i = 0; - if (cu.pc_range) |r| range = r; - } - continue :next_pc; - }, - else => |e| return e, - }; + const cu = &d.compile_unit_list.items[range.compile_unit_index]; + if (cu != prev_cu) { + prev_cu = cu; + if (cu.src_loc_cache == null) { + cov.mutex.unlock(); + defer cov.mutex.lock(); + d.populateSrcLocCache(gpa, cu) catch |err| switch (err) { + error.MissingDebugInfo, error.InvalidDebugInfo => { + out.* = SourceLocation.invalid; + continue :next_pc; + }, + else => |e| return e, + }; + } + const slc = &cu.src_loc_cache.?; + const table_addrs = slc.line_table.keys(); + line_table_i = std.sort.upperBound(u64, table_addrs, pc, struct { + fn order(context: u64, item: u64) std.math.Order { + return std.math.order(item, context); + } + }.order); } const slc = &cu.src_loc_cache.?; const table_addrs = slc.line_table.keys(); - while (line_table_i < table_addrs.len and table_addrs[line_table_i] < pc) line_table_i += 1; + while (line_table_i < table_addrs.len and table_addrs[line_table_i] <= pc) line_table_i += 1; const entry = slc.line_table.values()[line_table_i - 1]; const corrected_file_index = entry.file - @intFromBool(slc.version < 5); |
