diff options
Diffstat (limited to 'src/trace/heap.zig')
| -rw-r--r-- | src/trace/heap.zig | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/src/trace/heap.zig b/src/trace/heap.zig index 8f0dc68..d74e07a 100644 --- a/src/trace/heap.zig +++ b/src/trace/heap.zig @@ -180,6 +180,13 @@ pub const Tcache = struct { slot: u64, /// The bins that hold anything. bins: []Chain, + /// How many chunks a bin holds when full, where a bin's count is the + /// slots it has left (`Layout.tcache_slots`): the count of a bin that + /// is empty, every small bin having the same (`glibc.malloc.tcache_count`, + /// 16 by default). A bin holds `capacity - count`. Null otherwise: the + /// count is what a bin holds, and glibc keeps the capacity elsewhere (7 + /// by default). + capacity: ?u16 = null, }; pub const Heap = struct { @@ -192,8 +199,11 @@ pub const Heap = struct { cut: End = .end, cut_at: u64 = 0, /// Null: the thread has none yet (glibc 2.42 on makes it at the first - /// free), or its pointer is not known. + /// free), or its pointer is not known; `tcache_dummy` says which. tcache: ?Tcache = null, + /// No tcache yet: the thread's pointer aims at glibc's dummy, zeros in + /// the C library's read-only data, until its first free. + tcache_dummy: bool = false, /// The fastbins and the bins that hold anything. fastbins: []Chain = &.{}, bins: []Chain = &.{}, @@ -257,11 +267,13 @@ pub fn walk(arena: std.mem.Allocator, mem: anytype, g: Glibc, slot: ?u64) !?Heap .max_system_mem = field(ab, l.system_mem + 8), } }; - // The tcache, when the thread's pointer leads to a chunk of its size. - if (slot) |at| if (word(mem, at)) |t| if (t % 16 == 0) if (word(mem, t -% 8)) |size| if (size & ~@as(u64, 7) == l.tcache_chunk) { + // The tcache, when the thread's pointer leads to a chunk of its size; + // else, all zeros where it leads, the dummy it aims at until there is one. + if (slot) |at| if (word(mem, at)) |t| if (t != 0) { var tcache_buf: [max_struct]u8 = undefined; const tb = tcache_buf[0 .. l.tcache_entries + 8 * @as(u64, l.tcache_bins)]; - if (mem.read(t, tb) == tb.len) { + const chunk = t % 16 == 0 and if (word(mem, t -% 8)) |size| size & ~@as(u64, 7) == l.tcache_chunk else false; + if (!chunk) h.tcache_dummy = mem.read(t, tb) == tb.len and std.mem.allEqual(u8, tb, 0) else if (mem.read(t, tb) == tb.len) { var bins: std.ArrayList(Chain) = .empty; // shortcut: 2.44's large bins (64 on, used only when the tunable glibc.malloc.tcache_max is raised) are walked as the small ones are, untested; upgrade with a target that raises it. for (0..l.tcache_bins) |i| { @@ -272,7 +284,12 @@ pub fn walk(arena: std.mem.Allocator, mem: anytype, g: Glibc, slot: ?u64) !?Heap try follow(arena, mem, &c, head, l, 0); try bins.append(arena, c); } - h.tcache = .{ .addr = t, .slot = at, .bins = bins.items }; + var capacity: ?u16 = null; + if (l.tcache_slots) for (0..@min(64, l.tcache_bins)) |i| if (field(tb, l.tcache_entries + 8 * i) == 0) { + capacity = std.mem.readInt(u16, tb[2 * i ..][0..2], .little); + break; + }; + h.tcache = .{ .addr = t, .slot = at, .bins = bins.items, .capacity = capacity }; } }; @@ -404,8 +421,11 @@ pub fn print(h: Heap, w: *std.Io.Writer) !void { i += n; } if (h.cut != .end) try w.print(" ... {t} at {x}: no further\n", .{ h.cut, h.cut_at }); + if (h.tcache_dummy) try w.writeAll("tcache: none yet, the thread's pointer on glibc's dummy\n"); if (h.tcache) |t| { - try w.print("tcache {x}\n", .{t.addr}); + try w.print("tcache {x}", .{t.addr}); + if (t.capacity) |n| try w.print(", {d} a bin", .{n}); + try w.writeAll("\n"); for (t.bins) |c| try printChain(w, c, h.layout); } for (h.fastbins) |c| try printChain(w, c, h.layout); @@ -554,23 +574,32 @@ fn banner(bytes: []const u8) ?[2]u16 { /// Where the stopped thread keeps its tcache pointer, from its thread /// pointer: in the static TLS below it (64 KiB of it at most: a library /// preloaded has its block between the program's and libc's), the word -/// that points at a chunk of the tcache's size in the main heap. Null until -/// the tcache is made. +/// that points at a chunk of the tcache's size in the main heap, or, before +/// there is one (glibc 2.42 on), at its dummy: zeros of the tcache's size in +/// the read-only data of the C library. Null until either is there. fn findTcache(d: *Debugger, g: Glibc, l: Layout) !?i64 { const tp = d.regs.fs_base; var w: [8]u8 = undefined; const top = if (d.read(g.arena + l.top, &w) == 8) std.mem.readInt(u64, &w, .little) else return null; const system_mem = if (d.read(g.arena + l.system_mem, &w) == 8) std.mem.readInt(u64, &w, .little) else return null; - if (system_mem == 0 or top < system_mem) return null; + const heap = system_mem != 0 and top >= system_mem; + const lib = d.syms.moduleAt(g.arena); const m = d.syms.mappingAt(tp -% 8) orelse return null; const from = @max(m.start, tp -| (64 << 10)) & ~@as(u64, 7); const tls = try d.gpa.alloc(u64, (tp - from) / 8); defer d.gpa.free(tls); const n = d.read(from, std.mem.sliceAsBytes(tls)) / 8; + var zeros: [max_struct]u8 = undefined; + const size = l.tcache_entries + 8 * @as(u64, l.tcache_bins); for (tls[0..n], 0..) |p, i| { - if (p % 16 != 0 or p < top -| system_mem or p >= top) continue; - if (d.read(p - 8, &w) != 8 or std.mem.readInt(u64, &w, .little) & ~@as(u64, 7) != l.tcache_chunk) continue; - return @as(i64, @intCast(from + 8 * i)) - @as(i64, @intCast(tp)); + const found = if (heap and p % 16 == 0 and p >= top -| system_mem and p < top) + d.read(p - 8, &w) == 8 and std.mem.readInt(u64, &w, .little) & ~@as(u64, 7) == l.tcache_chunk + else dummy: { + const in = d.syms.mappingAt(p) orelse break :dummy false; + if (in.prot & 2 != 0 or lib == null or !std.mem.eql(u8, in.name, lib.?.path)) break :dummy false; + break :dummy d.read(p, zeros[0..size]) == size and std.mem.allEqual(u8, zeros[0..size], 0); + }; + if (found) return @as(i64, @intCast(from + 8 * i)) - @as(i64, @intCast(tp)); } return null; } |
