authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-06 20:04:33-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-07-20 22:58:15-04:00
log8547c42ba542f77c55ce50253446fd2bf4f3592b
treee8d03a2cdbea5fbefe21bdda9dc6d16dd189d64e
parent424b1299a88b4ac3ae50128118ed510a79c3ab4b

dwarf: expression fixups for non-64bit arches, check call_frame_context when writing expressions


2 files changed, 42 insertions(+), 31 deletions(-)

lib/std/dwarf.zig+1-1
......@@ -1783,7 +1783,7 @@ pub const UnwindContext = struct {
17831783 reg_ctx: abi.RegisterContext,
17841784 isValidMemory: *const fn (address: usize) bool,
17851785 vm: call_frame.VirtualMachine = .{},
1786 stack_machine: expressions.StackMachine(.{ .call_frame_mode = true }) = .{},
1786 stack_machine: expressions.StackMachine(.{ .call_frame_context = true }) = .{},
17871787
17881788 pub fn init(allocator: mem.Allocator, ucontext: *const os.ucontext_t, isValidMemory: *const fn (address: usize) bool) !UnwindContext {
17891789 const pc = mem.readIntSliceNative(usize, try abi.regBytes(ucontext, abi.ipRegNum(), null));
lib/std/dwarf/expressions.zig+41-30
......@@ -33,7 +33,7 @@ pub const ExpressionOptions = struct {
3333 endian: std.builtin.Endian = .Little,
3434
3535 /// Restrict the stack machine to a subset of opcodes used in call frame instructions
36 call_frame_mode: bool = false,
36 call_frame_context: bool = false,
3737};
3838
3939/// A stack machine that can decode and run DWARF expressions.
......@@ -111,13 +111,15 @@ pub fn StackMachine(comptime options: ExpressionOptions) type {
111111 // TODO: For these two prongs, look up the type and assert it's integral?
112112 .regval_type => |regval_type| regval_type.value,
113113 .const_type => |const_type| {
114 return switch (const_type.value_bytes.len) {
114 const value: u64 = switch (const_type.value_bytes.len) {
115115 1 => mem.readIntSliceNative(u8, const_type.value_bytes),
116116 2 => mem.readIntSliceNative(u16, const_type.value_bytes),
117117 4 => mem.readIntSliceNative(u32, const_type.value_bytes),
118118 8 => mem.readIntSliceNative(u64, const_type.value_bytes),
119 else => error.InvalidIntegralTypeLength,
119 else => return error.InvalidIntegralTypeSize,
120120 };
121
122 return std.math.cast(addr_type, value) orelse error.TruncatedIntegralType;
121123 },
122124 };
123125 }
......@@ -278,26 +280,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type {
278280 @compileError("Execution of non-native address sizees / endianness is not supported");
279281
280282 const opcode = try stream.reader().readByte();
281 if (options.call_frame_mode) {
282 // Certain opcodes are not allowed in a CFA context, see 6.4.2
283 switch (opcode) {
284 OP.addrx,
285 OP.call2,
286 OP.call4,
287 OP.call_ref,
288 OP.const_type,
289 OP.constx,
290 OP.convert,
291 OP.deref_type,
292 OP.regval_type,
293 OP.reinterpret,
294 OP.push_object_address,
295 OP.call_frame_cfa,
296 => return error.InvalidCFAExpression,
297 else => {},
298 }
299 }
300
283 if (options.call_frame_context and !opcodeValidInCFA(opcode)) return error.InvalidCFAOpcode;
301284 switch (opcode) {
302285
303286 // 2.5.1.1: Literal Encodings
......@@ -420,7 +403,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type {
420403 OP.xderef_type,
421404 => {
422405 if (self.stack.items.len == 0) return error.InvalidExpression;
423 var addr = try self.stack.pop().asIntegral();
406 var addr = try self.stack.items[self.stack.items.len - 1].asIntegral();
424407 const addr_space_identifier: ?usize = switch (opcode) {
425408 OP.xderef,
426409 OP.xderef_size,
......@@ -447,24 +430,24 @@ pub fn StackMachine(comptime options: ExpressionOptions) type {
447430 else => unreachable,
448431 };
449432
450 const value: u64 = switch (size) {
433 const value: addr_type = std.math.cast(addr_type, @as(u64, switch (size) {
451434 1 => @as(*const u8, @ptrFromInt(addr)).*,
452435 2 => @as(*const u16, @ptrFromInt(addr)).*,
453436 4 => @as(*const u32, @ptrFromInt(addr)).*,
454437 8 => @as(*const u64, @ptrFromInt(addr)).*,
455438 else => return error.InvalidExpression,
456 };
439 })) orelse return error.InvalidExpression;
457440
458441 if (opcode == OP.deref_type) {
459 try self.stack.append(allocator, .{
442 self.stack.items[self.stack.items.len - 1] = .{
460443 .regval_type = .{
461444 .type_offset = operand.?.deref_type.type_offset,
462445 .type_size = operand.?.deref_type.size,
463446 .value = value,
464447 },
465 });
448 };
466449 } else {
467 try self.stack.append(allocator, .{ .generic = value });
450 self.stack.items[self.stack.items.len - 1] = .{ .generic = value };
468451 }
469452 },
470453 OP.push_object_address,
......@@ -738,6 +721,7 @@ pub fn Writer(options: ExpressionOptions) type {
738721 return struct {
739722 /// Zero-operand instructions
740723 pub fn writeOpcode(writer: anytype, comptime opcode: u8) !void {
724 if (options.call_frame_context and !comptime opcodeValidInCFA(opcode)) return error.InvalidCFAOpcode;
741725 switch (opcode) {
742726 OP.dup,
743727 OP.drop,
......@@ -820,6 +804,7 @@ pub fn Writer(options: ExpressionOptions) type {
820804 }
821805
822806 pub fn writeConstType(writer: anytype, die_offset: anytype, size: u8, value_bytes: []const u8) !void {
807 if (options.call_frame_context) return error.InvalidCFAOpcode;
823808 if (size != value_bytes.len) return error.InvalidValueSize;
824809 try writer.writeByte(OP.const_type);
825810 try leb.writeULEB128(writer, die_offset);
......@@ -833,6 +818,7 @@ pub fn Writer(options: ExpressionOptions) type {
833818 }
834819
835820 pub fn writeAddrx(writer: anytype, debug_addr_offset: anytype) !void {
821 if (options.call_frame_context) return error.InvalidCFAOpcode;
836822 try writer.writeByte(OP.addrx);
837823 try leb.writeULEB128(writer, debug_addr_offset);
838824 }
......@@ -856,6 +842,7 @@ pub fn Writer(options: ExpressionOptions) type {
856842 }
857843
858844 pub fn writeRegvalType(writer: anytype, register: anytype, offset: anytype) !void {
845 if (options.call_frame_context) return error.InvalidCFAOpcode;
859846 try writer.writeByte(OP.bregx);
860847 try leb.writeULEB128(writer, register);
861848 try leb.writeULEB128(writer, offset);
......@@ -878,6 +865,7 @@ pub fn Writer(options: ExpressionOptions) type {
878865 }
879866
880867 pub fn writeDerefType(writer: anytype, size: u8, die_offset: anytype) !void {
868 if (options.call_frame_context) return error.InvalidCFAOpcode;
881869 try writer.writeByte(OP.deref_type);
882870 try writer.writeByte(size);
883871 try leb.writeULEB128(writer, die_offset);
......@@ -909,6 +897,7 @@ pub fn Writer(options: ExpressionOptions) type {
909897 }
910898
911899 pub fn writeCall(writer: anytype, comptime T: type, offset: T) !void {
900 if (options.call_frame_context) return error.InvalidCFAOpcode;
912901 switch (T) {
913902 u16 => try writer.writeByte(OP.call2),
914903 u32 => try writer.writeByte(OP.call4),
......@@ -919,16 +908,19 @@ pub fn Writer(options: ExpressionOptions) type {
919908 }
920909
921910 pub fn writeCallRef(writer: anytype, debug_info_offset: addr_type) !void {
911 if (options.call_frame_context) return error.InvalidCFAOpcode;
922912 try writer.writeByte(OP.call_ref);
923913 try writer.writeInt(addr_type, debug_info_offset, options.endian);
924914 }
925915
926916 pub fn writeConvert(writer: anytype, die_offset: anytype) !void {
917 if (options.call_frame_context) return error.InvalidCFAOpcode;
927918 try writer.writeByte(OP.convert);
928919 try leb.writeULEB128(writer, die_offset);
929920 }
930921
931922 pub fn writeReinterpret(writer: anytype, die_offset: anytype) !void {
923 if (options.call_frame_context) return error.InvalidCFAOpcode;
932924 try writer.writeByte(OP.reinterpret);
933925 try leb.writeULEB128(writer, die_offset);
934926 }
......@@ -942,12 +934,31 @@ pub fn Writer(options: ExpressionOptions) type {
942934 }
943935
944936 // 2.6: Location Descriptions
945
946937 // TODO
947938
948939 };
949940}
950941
942// Certain opcodes are not allowed in a CFA context, see 6.4.2
943fn opcodeValidInCFA(opcode: u8) bool {
944 return switch (opcode) {
945 OP.addrx,
946 OP.call2,
947 OP.call4,
948 OP.call_ref,
949 OP.const_type,
950 OP.constx,
951 OP.convert,
952 OP.deref_type,
953 OP.regval_type,
954 OP.reinterpret,
955 OP.push_object_address,
956 OP.call_frame_cfa,
957 => false,
958 else => true,
959 };
960}
961
951962test "DWARF expressions" {
952963 const allocator = std.testing.allocator;
953964