From 5bc4417d2a5155ceb10c73b5f56229c8f66539fc Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 17 Apr 2023 13:35:39 +0200 Subject: [PATCH 1/2] tapi: fix memory bugs in yaml parser --- src/link/tapi/parse.zig | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/link/tapi/parse.zig b/src/link/tapi/parse.zig index 09774cf00a..89e0b23824 100644 --- a/src/link/tapi/parse.zig +++ b/src/link/tapi/parse.zig @@ -40,10 +40,26 @@ pub const Node = struct { pub fn deinit(self: *Node, allocator: Allocator) void { switch (self.tag) { - .doc => @fieldParentPtr(Node.Doc, "base", self).deinit(allocator), - .map => @fieldParentPtr(Node.Map, "base", self).deinit(allocator), - .list => @fieldParentPtr(Node.List, "base", self).deinit(allocator), - .value => @fieldParentPtr(Node.Value, "base", self).deinit(allocator), + .doc => { + const parent = @fieldParentPtr(Node.Doc, "base", self); + parent.deinit(allocator); + allocator.destroy(parent); + }, + .map => { + const parent = @fieldParentPtr(Node.Map, "base", self); + parent.deinit(allocator); + allocator.destroy(parent); + }, + .list => { + const parent = @fieldParentPtr(Node.List, "base", self); + parent.deinit(allocator); + allocator.destroy(parent); + }, + .value => { + const parent = @fieldParentPtr(Node.Value, "base", self); + parent.deinit(allocator); + allocator.destroy(parent); + }, } } @@ -76,7 +92,6 @@ pub const Node = struct { pub fn deinit(self: *Doc, allocator: Allocator) void { if (self.value) |node| { node.deinit(allocator); - allocator.destroy(node); } } @@ -122,7 +137,6 @@ pub const Node = struct { for (self.values.items) |entry| { if (entry.value) |value| { value.deinit(allocator); - allocator.destroy(value); } } self.values.deinit(allocator); @@ -163,7 +177,6 @@ pub const Node = struct { pub fn deinit(self: *List, allocator: Allocator) void { for (self.values.items) |node| { node.deinit(allocator); - allocator.destroy(node); } self.values.deinit(allocator); } @@ -239,7 +252,6 @@ pub const Tree = struct { self.line_cols.deinit(); for (self.docs.items) |doc| { doc.deinit(self.allocator); - self.allocator.destroy(doc); } self.docs.deinit(self.allocator); } @@ -386,7 +398,6 @@ const Parser = struct { } errdefer if (node.value) |val| { val.deinit(self.allocator); - self.allocator.destroy(val); }; // Parse footer @@ -426,7 +437,6 @@ const Parser = struct { for (node.values.items) |entry| { if (entry.value) |val| { val.deinit(self.allocator); - self.allocator.destroy(val); } } node.values.deinit(self.allocator); @@ -467,7 +477,6 @@ const Parser = struct { const val = try self.value(); errdefer if (val) |v| { v.deinit(self.allocator); - self.allocator.destroy(v); }; if (val) |v| { @@ -503,7 +512,6 @@ const Parser = struct { errdefer { for (node.values.items) |val| { val.deinit(self.allocator); - self.allocator.destroy(val); } node.values.deinit(self.allocator); } @@ -535,7 +543,6 @@ const Parser = struct { errdefer { for (node.values.items) |val| { val.deinit(self.allocator); - self.allocator.destroy(val); } node.values.deinit(self.allocator); } From 13503b7cba2c826633017e34e8bda2bf5072e7f6 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 17 Apr 2023 13:43:01 +0200 Subject: [PATCH 2/2] x86_64: clean up formatting functions for Instruction and Operand --- src/arch/x86_64/CodeGen.zig | 4 +--- src/arch/x86_64/encoder.zig | 47 ++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 20c945e451..5cd05508e4 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -439,9 +439,7 @@ fn dumpWipMir(self: *Self, inst: Mir.Inst) !void { }, else => |e| return e, }) |lower_inst| { - try stderr.writeAll(" | "); - try lower_inst.fmtPrint(stderr); - try stderr.writeByte('\n'); + try stderr.print(" | {}\n", .{lower_inst}); } } diff --git a/src/arch/x86_64/encoder.zig b/src/arch/x86_64/encoder.zig index 663dad8727..578dba5f5d 100644 --- a/src/arch/x86_64/encoder.zig +++ b/src/arch/x86_64/encoder.zig @@ -54,7 +54,34 @@ pub const Instruction = struct { }; } - pub fn fmtPrint(op: Operand, enc_op: Encoding.Op, writer: anytype) @TypeOf(writer).Error!void { + fn format( + op: Operand, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) !void { + _ = op; + _ = unused_format_string; + _ = options; + _ = writer; + @compileError("do not format Operand directly; use fmtPrint() instead"); + } + + const FormatContext = struct { + op: Operand, + enc_op: Encoding.Op, + }; + + fn fmt( + ctx: FormatContext, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) @TypeOf(writer).Error!void { + _ = unused_format_string; + _ = options; + const op = ctx.op; + const enc_op = ctx.enc_op; switch (op) { .none => {}, .reg => |reg| try writer.writeAll(@tagName(reg)), @@ -105,6 +132,13 @@ pub const Instruction = struct { .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.bitSize())}), } } + + pub fn fmtPrint(op: Operand, enc_op: Encoding.Op) std.fmt.Formatter(fmt) { + return .{ .data = .{ + .op = op, + .enc_op = enc_op, + } }; + } }; pub fn new(prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) !Instruction { @@ -130,14 +164,21 @@ pub const Instruction = struct { return inst; } - pub fn fmtPrint(inst: Instruction, writer: anytype) @TypeOf(writer).Error!void { + pub fn format( + inst: Instruction, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) @TypeOf(writer).Error!void { + _ = unused_format_string; + _ = options; if (inst.prefix != .none) try writer.print("{s} ", .{@tagName(inst.prefix)}); try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)}); for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| { if (op == .none) break; if (i > 0) try writer.writeByte(','); try writer.writeByte(' '); - try op.fmtPrint(enc, writer); + try writer.print("{}", .{op.fmtPrint(enc)}); } }