Skip to content

Commit 83f773f

Browse files
authored
Merge pull request #24960 from ziglang/MemoryAccessor
std.debug: delete MemoryAccessor
2 parents 77c09d1 + e4a7b15 commit 83f773f

File tree

6 files changed

+33
-267
lines changed

6 files changed

+33
-267
lines changed

lib/std/debug.zig

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const native_os = builtin.os.tag;
1414
const native_endian = native_arch.endian();
1515
const Writer = std.io.Writer;
1616

17-
pub const MemoryAccessor = @import("debug/MemoryAccessor.zig");
1817
pub const FixedBufferReader = @import("debug/FixedBufferReader.zig");
1918
pub const Dwarf = @import("debug/Dwarf.zig");
2019
pub const Pdb = @import("debug/Pdb.zig");
@@ -501,6 +500,11 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
501500
// TODO: This should use the DWARF unwinder if .eh_frame_hdr is available (so that full debug info parsing isn't required).
502501
// A new path for loading SelfInfo needs to be created which will only attempt to parse in-memory sections, because
503502
// stopping to load other debug info (ie. source line info) from disk here is not required for unwinding.
503+
if (builtin.cpu.arch == .powerpc64) {
504+
// https://github.com/ziglang/zig/issues/24970
505+
stack_trace.index = 0;
506+
return;
507+
}
504508
var it = StackIterator.init(first_address, null);
505509
defer it.deinit();
506510
for (stack_trace.instruction_addresses, 0..) |*addr, i| {
@@ -773,7 +777,6 @@ pub const StackIterator = struct {
773777
first_address: ?usize,
774778
// Last known value of the frame pointer register.
775779
fp: usize,
776-
ma: MemoryAccessor = MemoryAccessor.init,
777780

778781
// When SelfInfo and a register context is available, this iterator can unwind
779782
// stacks with frames that don't use a frame pointer (ie. -fomit-frame-pointer),
@@ -795,7 +798,7 @@ pub const StackIterator = struct {
795798
::: .{ .memory = true });
796799
}
797800

798-
return StackIterator{
801+
return .{
799802
.first_address = first_address,
800803
// TODO: this is a workaround for #16876
801804
//.fp = fp orelse @frameAddress(),
@@ -825,7 +828,6 @@ pub const StackIterator = struct {
825828
}
826829

827830
pub fn deinit(it: *StackIterator) void {
828-
it.ma.deinit();
829831
if (have_ucontext and it.unwind_state != null) it.unwind_state.?.dwarf_context.deinit();
830832
}
831833

@@ -896,7 +898,6 @@ pub const StackIterator = struct {
896898
unwind_state.debug_info.allocator,
897899
module.base_address,
898900
&unwind_state.dwarf_context,
899-
&it.ma,
900901
unwind_info,
901902
module.eh_frame,
902903
)) |return_address| {
@@ -915,7 +916,6 @@ pub const StackIterator = struct {
915916
di,
916917
module.base_address,
917918
&unwind_state.dwarf_context,
918-
&it.ma,
919919
null,
920920
);
921921
} else return error.MissingDebugInfo;
@@ -951,16 +951,15 @@ pub const StackIterator = struct {
951951

952952
// Sanity check.
953953
if (fp == 0 or !mem.isAligned(fp, @alignOf(usize))) return null;
954-
const new_fp = math.add(usize, it.ma.load(usize, fp) orelse return null, fp_bias) catch
954+
const new_fp = math.add(usize, @as(*usize, @ptrFromInt(fp)).*, fp_bias) catch
955955
return null;
956956

957957
// Sanity check: the stack grows down thus all the parent frames must be
958958
// be at addresses that are greater (or equal) than the previous one.
959959
// A zero frame pointer often signals this is the last frame, that case
960960
// is gracefully handled by the next call to next_internal.
961961
if (new_fp != 0 and new_fp < it.fp) return null;
962-
const new_pc = it.ma.load(usize, math.add(usize, fp, pc_offset) catch return null) orelse
963-
return null;
962+
const new_pc = @as(*usize, @ptrFromInt(math.add(usize, fp, pc_offset) catch return null)).*;
964963

965964
it.fp = new_fp;
966965

@@ -1774,7 +1773,6 @@ pub inline fn inValgrind() bool {
17741773

17751774
test {
17761775
_ = &Dwarf;
1777-
_ = &MemoryAccessor;
17781776
_ = &FixedBufferReader;
17791777
_ = &Pdb;
17801778
_ = &SelfInfo;

lib/std/debug/Dwarf.zig

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const UT = DW.UT;
2424
const assert = std.debug.assert;
2525
const cast = std.math.cast;
2626
const maxInt = std.math.maxInt;
27-
const MemoryAccessor = std.debug.MemoryAccessor;
2827
const Path = std.Build.Cache.Path;
2928
const FixedBufferReader = std.debug.FixedBufferReader;
3029
const ArrayList = std.ArrayList;
@@ -349,29 +348,9 @@ pub const ExceptionFrameHeader = struct {
349348
};
350349
}
351350

352-
fn isValidPtr(
353-
self: ExceptionFrameHeader,
354-
comptime T: type,
355-
ptr: usize,
356-
ma: *MemoryAccessor,
357-
eh_frame_len: ?usize,
358-
) bool {
359-
if (eh_frame_len) |len| {
360-
return ptr >= self.eh_frame_ptr and ptr <= self.eh_frame_ptr + len - @sizeOf(T);
361-
} else {
362-
return ma.load(T, ptr) != null;
363-
}
364-
}
365-
366-
/// Find an entry by binary searching the eh_frame_hdr section.
367-
///
368-
/// Since the length of the eh_frame section (`eh_frame_len`) may not be known by the caller,
369-
/// MemoryAccessor will be used to verify readability of the header entries.
370-
/// If `eh_frame_len` is provided, then these checks can be skipped.
371351
pub fn findEntry(
372352
self: ExceptionFrameHeader,
373-
ma: *MemoryAccessor,
374-
eh_frame_len: ?usize,
353+
eh_frame_len: usize,
375354
eh_frame_hdr_ptr: usize,
376355
pc: usize,
377356
cie: *CommonInformationEntry,
@@ -421,8 +400,7 @@ pub const ExceptionFrameHeader = struct {
421400

422401
if (fde_ptr < self.eh_frame_ptr) return bad();
423402

424-
// Even if eh_frame_len is not specified, all ranges accssed are checked via MemoryAccessor
425-
const eh_frame = @as([*]const u8, @ptrFromInt(self.eh_frame_ptr))[0 .. eh_frame_len orelse maxInt(u32)];
403+
const eh_frame = @as([*]const u8, @ptrFromInt(self.eh_frame_ptr))[0..eh_frame_len];
426404

427405
const fde_offset = fde_ptr - self.eh_frame_ptr;
428406
var eh_frame_fbr: FixedBufferReader = .{
@@ -431,15 +409,13 @@ pub const ExceptionFrameHeader = struct {
431409
.endian = native_endian,
432410
};
433411

434-
const fde_entry_header = try EntryHeader.read(&eh_frame_fbr, if (eh_frame_len == null) ma else null, .eh_frame);
435-
if (fde_entry_header.entry_bytes.len > 0 and !self.isValidPtr(u8, @intFromPtr(&fde_entry_header.entry_bytes[fde_entry_header.entry_bytes.len - 1]), ma, eh_frame_len)) return bad();
412+
const fde_entry_header = try EntryHeader.read(&eh_frame_fbr, .eh_frame);
436413
if (fde_entry_header.type != .fde) return bad();
437414

438415
// CIEs always come before FDEs (the offset is a subtraction), so we can assume this memory is readable
439416
const cie_offset = fde_entry_header.type.fde;
440417
try eh_frame_fbr.seekTo(cie_offset);
441-
const cie_entry_header = try EntryHeader.read(&eh_frame_fbr, if (eh_frame_len == null) ma else null, .eh_frame);
442-
if (cie_entry_header.entry_bytes.len > 0 and !self.isValidPtr(u8, @intFromPtr(&cie_entry_header.entry_bytes[cie_entry_header.entry_bytes.len - 1]), ma, eh_frame_len)) return bad();
418+
const cie_entry_header = try EntryHeader.read(&eh_frame_fbr, .eh_frame);
443419
if (cie_entry_header.type != .cie) return bad();
444420

445421
cie.* = try CommonInformationEntry.parse(
@@ -486,15 +462,11 @@ pub const EntryHeader = struct {
486462

487463
/// Reads a header for either an FDE or a CIE, then advances the fbr to the position after the trailing structure.
488464
/// `fbr` must be a FixedBufferReader backed by either the .eh_frame or .debug_frame sections.
489-
pub fn read(
490-
fbr: *FixedBufferReader,
491-
opt_ma: ?*MemoryAccessor,
492-
dwarf_section: Section.Id,
493-
) !EntryHeader {
465+
pub fn read(fbr: *FixedBufferReader, dwarf_section: Section.Id) !EntryHeader {
494466
assert(dwarf_section == .eh_frame or dwarf_section == .debug_frame);
495467

496468
const length_offset = fbr.pos;
497-
const unit_header = try readUnitHeader(fbr, opt_ma);
469+
const unit_header = try readUnitHeader(fbr);
498470
const unit_length = cast(usize, unit_header.unit_length) orelse return bad();
499471
if (unit_length == 0) return .{
500472
.length_offset = length_offset,
@@ -506,10 +478,7 @@ pub const EntryHeader = struct {
506478
const end_offset = start_offset + unit_length;
507479
defer fbr.pos = end_offset;
508480

509-
const id = try if (opt_ma) |ma|
510-
fbr.readAddressChecked(unit_header.format, ma)
511-
else
512-
fbr.readAddress(unit_header.format);
481+
const id = try fbr.readAddress(unit_header.format);
513482
const entry_bytes = fbr.buf[fbr.pos..end_offset];
514483
const cie_id: u64 = switch (dwarf_section) {
515484
.eh_frame => CommonInformationEntry.eh_id,
@@ -856,7 +825,7 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void {
856825
while (this_unit_offset < fbr.buf.len) {
857826
try fbr.seekTo(this_unit_offset);
858827

859-
const unit_header = try readUnitHeader(&fbr, null);
828+
const unit_header = try readUnitHeader(&fbr);
860829
if (unit_header.unit_length == 0) return;
861830
const next_offset = unit_header.header_length + unit_header.unit_length;
862831

@@ -1045,7 +1014,7 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator) ScanError!void {
10451014
while (this_unit_offset < fbr.buf.len) {
10461015
try fbr.seekTo(this_unit_offset);
10471016

1048-
const unit_header = try readUnitHeader(&fbr, null);
1017+
const unit_header = try readUnitHeader(&fbr);
10491018
if (unit_header.unit_length == 0) return;
10501019
const next_offset = unit_header.header_length + unit_header.unit_length;
10511020

@@ -1427,7 +1396,7 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) !
14271396
};
14281397
try fbr.seekTo(line_info_offset);
14291398

1430-
const unit_header = try readUnitHeader(&fbr, null);
1399+
const unit_header = try readUnitHeader(&fbr);
14311400
if (unit_header.unit_length == 0) return missing();
14321401

14331402
const next_offset = unit_header.header_length + unit_header.unit_length;
@@ -1815,7 +1784,7 @@ pub fn scanCieFdeInfo(di: *Dwarf, allocator: Allocator, base_address: usize) !vo
18151784
if (di.section(frame_section)) |section_data| {
18161785
var fbr: FixedBufferReader = .{ .buf = section_data, .endian = di.endian };
18171786
while (fbr.pos < fbr.buf.len) {
1818-
const entry_header = try EntryHeader.read(&fbr, null, frame_section);
1787+
const entry_header = try EntryHeader.read(&fbr, frame_section);
18191788
switch (entry_header.type) {
18201789
.cie => {
18211790
const cie = try CommonInformationEntry.parse(
@@ -1988,8 +1957,8 @@ const UnitHeader = struct {
19881957
unit_length: u64,
19891958
};
19901959

1991-
fn readUnitHeader(fbr: *FixedBufferReader, opt_ma: ?*MemoryAccessor) ScanError!UnitHeader {
1992-
return switch (try if (opt_ma) |ma| fbr.readIntChecked(u32, ma) else fbr.readInt(u32)) {
1960+
fn readUnitHeader(fbr: *FixedBufferReader) ScanError!UnitHeader {
1961+
return switch (try fbr.readInt(u32)) {
19931962
0...0xfffffff0 - 1 => |unit_length| .{
19941963
.format = .@"32",
19951964
.header_length = 4,
@@ -1999,7 +1968,7 @@ fn readUnitHeader(fbr: *FixedBufferReader, opt_ma: ?*MemoryAccessor) ScanError!U
19991968
0xffffffff => .{
20001969
.format = .@"64",
20011970
.header_length = 12,
2002-
.unit_length = try if (opt_ma) |ma| fbr.readIntChecked(u64, ma) else fbr.readInt(u64),
1971+
.unit_length = try fbr.readInt(u64),
20031972
},
20041973
};
20051974
}

lib/std/debug/Dwarf/expression.zig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const assert = std.debug.assert;
1515
pub const Context = struct {
1616
/// The dwarf format of the section this expression is in
1717
format: std.dwarf.Format = .@"32",
18-
/// If specified, any addresses will pass through before being accessed
19-
memory_accessor: ?*std.debug.MemoryAccessor = null,
2018
/// The compilation unit this expression relates to, if any
2119
compile_unit: ?*const std.debug.Dwarf.CompileUnit = null,
2220
/// When evaluating a user-presented expression, this is the address of the object being evaluated
@@ -465,16 +463,6 @@ pub fn StackMachine(comptime options: Options) type {
465463
else => unreachable,
466464
};
467465

468-
if (context.memory_accessor) |memory_accessor| {
469-
if (!switch (size) {
470-
1 => memory_accessor.load(u8, addr) != null,
471-
2 => memory_accessor.load(u16, addr) != null,
472-
4 => memory_accessor.load(u32, addr) != null,
473-
8 => memory_accessor.load(u64, addr) != null,
474-
else => return error.InvalidExpression,
475-
}) return error.InvalidExpression;
476-
}
477-
478466
const value: addr_type = std.math.cast(addr_type, @as(u64, switch (size) {
479467
1 => @as(*const u8, @ptrFromInt(addr)).*,
480468
2 => @as(*const u16, @ptrFromInt(addr)).*,

lib/std/debug/FixedBufferReader.zig

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Optimized for performance in debug builds.
22

33
const std = @import("../std.zig");
4-
const MemoryAccessor = std.debug.MemoryAccessor;
54

65
const FixedBufferReader = @This();
76

@@ -38,17 +37,6 @@ pub fn readInt(fbr: *FixedBufferReader, comptime T: type) Error!T {
3837
return std.mem.readInt(T, fbr.buf[fbr.pos..][0..size], fbr.endian);
3938
}
4039

41-
pub fn readIntChecked(
42-
fbr: *FixedBufferReader,
43-
comptime T: type,
44-
ma: *MemoryAccessor,
45-
) Error!T {
46-
if (ma.load(T, @intFromPtr(fbr.buf[fbr.pos..].ptr)) == null)
47-
return error.InvalidBuffer;
48-
49-
return fbr.readInt(T);
50-
}
51-
5240
pub fn readUleb128(fbr: *FixedBufferReader, comptime T: type) Error!T {
5341
return std.leb.readUleb128(T, fbr);
5442
}
@@ -64,17 +52,6 @@ pub fn readAddress(fbr: *FixedBufferReader, format: std.dwarf.Format) Error!u64
6452
};
6553
}
6654

67-
pub fn readAddressChecked(
68-
fbr: *FixedBufferReader,
69-
format: std.dwarf.Format,
70-
ma: *MemoryAccessor,
71-
) Error!u64 {
72-
return switch (format) {
73-
.@"32" => try fbr.readIntChecked(u32, ma),
74-
.@"64" => try fbr.readIntChecked(u64, ma),
75-
};
76-
}
77-
7855
pub fn readBytes(fbr: *FixedBufferReader, len: usize) Error![]const u8 {
7956
if (fbr.buf.len - fbr.pos < len) return error.EndOfBuffer;
8057
defer fbr.pos += len;

0 commit comments

Comments
 (0)