From 23a63f4ce445d26d7fc577eecc6c3f5ca129a007 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 22 Jun 2022 10:27:51 +0200 Subject: link-tests: rename CheckMachOStep to CheckObjectStep and accept obj format --- lib/std/build/CheckObjectStep.zig | 355 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 355 insertions(+) create mode 100644 lib/std/build/CheckObjectStep.zig (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig new file mode 100644 index 0000000000..dae17b93db --- /dev/null +++ b/lib/std/build/CheckObjectStep.zig @@ -0,0 +1,355 @@ +const std = @import("../std.zig"); +const assert = std.debug.assert; +const build = std.build; +const fs = std.fs; +const macho = std.macho; +const mem = std.mem; + +const CheckObjectStep = @This(); + +const Allocator = mem.Allocator; +const Builder = build.Builder; +const Step = build.Step; + +pub const base_id = .check_obj; + +step: Step, +builder: *Builder, +source: build.FileSource, +max_bytes: usize = 20 * 1024 * 1024, +checks: std.ArrayList(Check), +dump_symtab: bool = false, +obj_format: std.Target.ObjectFormat, + +pub fn create(builder: *Builder, source: build.FileSource, obj_format: std.Target.ObjectFormat) *CheckObjectStep { + const gpa = builder.allocator; + const self = gpa.create(CheckObjectStep) catch unreachable; + self.* = .{ + .builder = builder, + .step = Step.init(.check_file, "CheckObject", gpa, make), + .source = source.dupe(builder), + .checks = std.ArrayList(Check).init(gpa), + .obj_format = obj_format, + }; + self.source.addStepDependencies(&self.step); + return self; +} + +const Action = union(enum) { + exact_match: []const u8, + extract_var: struct { + fuzzy_match: []const u8, + var_name: []const u8, + var_value: u64, + }, + compare: CompareAction, +}; + +const CompareAction = struct { + expected: union(enum) { + literal: u64, + varr: []const u8, + }, + var_stack: std.ArrayList([]const u8), + op_stack: std.ArrayList(Op), + + const Op = enum { + add, + }; +}; + +const Check = struct { + builder: *Builder, + actions: std.ArrayList(Action), + + fn create(b: *Builder) Check { + return .{ + .builder = b, + .actions = std.ArrayList(Action).init(b.allocator), + }; + } + + fn exactMatch(self: *Check, phrase: []const u8) void { + self.actions.append(.{ + .exact_match = self.builder.dupe(phrase), + }) catch unreachable; + } + + fn extractVar(self: *Check, phrase: []const u8, var_name: []const u8) void { + self.actions.append(.{ + .extract_var = .{ + .fuzzy_match = self.builder.dupe(phrase), + .var_name = self.builder.dupe(var_name), + .var_value = undefined, + }, + }) catch unreachable; + } +}; + +pub fn check(self: *CheckObjectStep, phrase: []const u8) void { + var new_check = Check.create(self.builder); + new_check.exactMatch(phrase); + self.checks.append(new_check) catch unreachable; +} + +pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { + assert(self.checks.items.len > 0); + const last = &self.checks.items[self.checks.items.len - 1]; + last.exactMatch(phrase); +} + +pub fn checkNextExtract(self: *CheckObjectStep, comptime phrase: []const u8) void { + assert(self.checks.items.len > 0); + const matcher_start = comptime mem.indexOf(u8, phrase, "{") orelse + @compileError("missing { } matcher"); + const matcher_end = comptime mem.indexOf(u8, phrase, "}") orelse + @compileError("missing { } matcher"); + const last = &self.checks.items[self.checks.items.len - 1]; + last.extractVar(phrase[0..matcher_start], phrase[matcher_start + 1 .. matcher_end]); +} + +pub fn checkInSymtab(self: *CheckObjectStep) void { + self.dump_symtab = true; + self.check("symtab"); +} + +pub fn checkCompare(self: *CheckObjectStep, comptime phrase: []const u8, expected: anytype) void { + comptime assert(phrase[0] == '{'); + comptime assert(phrase[phrase.len - 1] == '}'); + + const gpa = self.builder.allocator; + var ca = CompareAction{ + .expected = expected, + .var_stack = std.ArrayList([]const u8).init(gpa), + .op_stack = std.ArrayList(CompareAction.Op).init(gpa), + }; + + var it = mem.tokenize(u8, phrase[1 .. phrase.len - 1], " "); + while (it.next()) |next| { + if (mem.eql(u8, next, "+")) { + ca.op_stack.append(.add) catch unreachable; + } else { + ca.var_stack.append(self.builder.dupe(next)) catch unreachable; + } + } + + var new_check = Check.create(self.builder); + new_check.actions.append(.{ .compare = ca }) catch unreachable; + self.checks.append(new_check) catch unreachable; +} + +fn make(step: *Step) !void { + const self = @fieldParentPtr(CheckObjectStep, "step", step); + + const gpa = self.builder.allocator; + const src_path = self.source.getPath(self.builder); + const contents = try fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes); + + const output = switch (self.obj_format) { + .macho => try MachODumper.parseAndDump(contents, .{ + .gpa = gpa, + .dump_symtab = self.dump_symtab, + }), + .elf => @panic("TODO elf parser"), + .coff => @panic("TODO coff parser"), + .wasm => @panic("TODO wasm parser"), + else => unreachable, + }; + + var vars = std.StringHashMap(u64).init(gpa); + + for (self.checks.items) |chk| { + const first_action = chk.actions.items[0]; + + switch (first_action) { + .exact_match => |first| { + if (mem.indexOf(u8, output, first)) |index| { + // TODO backtrack to track current scope + var it = std.mem.tokenize(u8, output[index..], "\r\n"); + + outer: for (chk.actions.items[1..]) |next_action| { + switch (next_action) { + .exact_match => |exact| { + while (it.next()) |line| { + if (mem.eql(u8, line, exact)) { + std.debug.print("{s} == {s}\n", .{ line, exact }); + continue :outer; + } + std.debug.print("{s} != {s}\n", .{ line, exact }); + } else { + return error.TestFailed; + } + }, + .extract_var => |extract| { + const phrase = extract.fuzzy_match; + while (it.next()) |line| { + if (mem.indexOf(u8, line, phrase)) |found| { + std.debug.print("{s} in {s}\n", .{ phrase, line }); + // Extract variable and save back in the action. + const trimmed = mem.trim(u8, line[found + phrase.len ..], " "); + const parsed = try std.fmt.parseInt(u64, trimmed, 16); + try vars.putNoClobber(extract.var_name, parsed); + continue :outer; + } + std.debug.print("{s} not in {s}\n", .{ extract.fuzzy_match, line }); + } + }, + .compare => unreachable, + } + } + } else { + return error.TestFailed; + } + }, + .compare => |act| { + var values = std.ArrayList(u64).init(gpa); + try values.ensureTotalCapacity(act.var_stack.items.len); + for (act.var_stack.items) |vv| { + const val = vars.get(vv) orelse return error.TestFailed; + values.appendAssumeCapacity(val); + } + + var op_i: usize = 1; + var reduced: u64 = values.items[0]; + for (act.op_stack.items) |op| { + const other = values.items[op_i]; + switch (op) { + .add => { + reduced += other; + }, + } + } + + const expected = switch (act.expected) { + .literal => |exp| exp, + .varr => |vv| vars.get(vv) orelse return error.TestFailed, + }; + if (reduced != expected) return error.TestFailed; + }, + .extract_var => unreachable, + } + } + + var it = vars.iterator(); + while (it.next()) |entry| { + std.debug.print(" {s} => {x}", .{ entry.key_ptr.*, entry.value_ptr.* }); + } +} + +const Opts = struct { + gpa: ?Allocator = null, + dump_symtab: bool = false, +}; + +const MachODumper = struct { + fn parseAndDump(bytes: []const u8, opts: Opts) ![]const u8 { + const gpa = opts.gpa orelse unreachable; // MachO dumper requires an allocator + var stream = std.io.fixedBufferStream(bytes); + const reader = stream.reader(); + + const hdr = try reader.readStruct(macho.mach_header_64); + if (hdr.magic != macho.MH_MAGIC_64) { + return error.InvalidMagicNumber; + } + + var output = std.ArrayList(u8).init(gpa); + const writer = output.writer(); + + var symtab_cmd: ?macho.symtab_command = null; + var i: u16 = 0; + while (i < hdr.ncmds) : (i += 1) { + var cmd = try macho.LoadCommand.read(gpa, reader); + + if (opts.dump_symtab and cmd.cmd() == .SYMTAB) { + symtab_cmd = cmd.symtab; + } + + try dumpLoadCommand(cmd, i, writer); + try writer.writeByte('\n'); + } + + if (symtab_cmd) |cmd| { + try writer.writeAll("symtab\n"); + const strtab = bytes[cmd.stroff..][0..cmd.strsize]; + const symtab = @ptrCast( + [*]const macho.nlist_64, + @alignCast(@alignOf(macho.nlist_64), bytes.ptr + cmd.symoff), + )[0..cmd.nsyms]; + + for (symtab) |sym| { + if (sym.stab()) continue; + const sym_name = mem.sliceTo(@ptrCast([*:0]const u8, strtab.ptr + sym.n_strx), 0); + try writer.print("{s} {x}\n", .{ sym_name, sym.n_value }); + } + } + + return output.toOwnedSlice(); + } + + fn dumpLoadCommand(lc: macho.LoadCommand, index: u16, writer: anytype) !void { + // print header first + try writer.print( + \\LC {d} + \\cmd {s} + \\cmdsize {d} + , .{ index, @tagName(lc.cmd()), lc.cmdsize() }); + + switch (lc.cmd()) { + .SEGMENT_64 => { + // TODO dump section headers + const seg = lc.segment.inner; + try writer.writeByte('\n'); + try writer.print( + \\segname {s} + \\vmaddr {x} + \\vmsize {x} + \\fileoff {x} + \\filesz {x} + , .{ + seg.segName(), + seg.vmaddr, + seg.vmsize, + seg.fileoff, + seg.filesize, + }); + }, + + .ID_DYLIB, + .LOAD_DYLIB, + => { + const dylib = lc.dylib.inner.dylib; + try writer.writeByte('\n'); + try writer.print( + \\path {s} + \\timestamp {d} + \\current version {x} + \\compatibility version {x} + , .{ + mem.sliceTo(lc.dylib.data, 0), + dylib.timestamp, + dylib.current_version, + dylib.compatibility_version, + }); + }, + + .MAIN => { + try writer.writeByte('\n'); + try writer.print( + \\entryoff {x} + \\stacksize {x} + , .{ lc.main.entryoff, lc.main.stacksize }); + }, + + .RPATH => { + try writer.writeByte('\n'); + try writer.print( + \\path {s} + , .{ + mem.sliceTo(lc.rpath.data, 0), + }); + }, + + else => {}, + } + } +}; -- cgit v1.2.3 From 211de9b63b53b7252a6321074ace9018cf392db2 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 22 Jun 2022 10:40:10 +0200 Subject: link-tests: fix dumping of LOAD_DYLIB: name instead of path field --- lib/std/build/CheckObjectStep.zig | 2 +- test/link/macho/dylib/build.zig | 4 ++-- test/link/macho/frameworks/build.zig | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index dae17b93db..4f9a7f0997 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -320,7 +320,7 @@ const MachODumper = struct { const dylib = lc.dylib.inner.dylib; try writer.writeByte('\n'); try writer.print( - \\path {s} + \\name {s} \\timestamp {d} \\current version {x} \\compatibility version {x} diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index a613f02b4a..ad116e23e9 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *Builder) void { const check_dylib = dylib.checkObject(.macho); check_dylib.check("cmd ID_DYLIB"); - check_dylib.checkNext("path @rpath/liba.dylib"); + check_dylib.checkNext("name @rpath/liba.dylib"); check_dylib.checkNext("timestamp 2"); check_dylib.checkNext("current version 10000"); check_dylib.checkNext("compatibility version 10000"); @@ -32,7 +32,7 @@ pub fn build(b: *Builder) void { const check_exe = exe.checkObject(.macho); check_exe.check("cmd LOAD_DYLIB"); - check_exe.checkNext("path @rpath/liba.dylib"); + check_exe.checkNext("name @rpath/liba.dylib"); check_exe.checkNext("timestamp 2"); check_exe.checkNext("current version 10000"); check_exe.checkNext("compatibility version 10000"); diff --git a/test/link/macho/frameworks/build.zig b/test/link/macho/frameworks/build.zig index 5700422a41..f196f47b1b 100644 --- a/test/link/macho/frameworks/build.zig +++ b/test/link/macho/frameworks/build.zig @@ -11,10 +11,14 @@ pub fn build(b: *Builder) void { exe.addCSourceFile("main.c", &[0][]const u8{}); exe.setBuildMode(mode); exe.linkLibC(); - // TODO when we figure out how to ship framework stubs for cross-compilation, - // populate paths to the sysroot here. exe.linkFramework("Cocoa"); + const check = exe.checkObject(.macho); + check.check("cmd LOAD_DYLIB"); + check.checkNext("name /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa"); + + test_step.dependOn(&check.step); + const run_cmd = exe.run(); test_step.dependOn(&run_cmd.step); } -- cgit v1.2.3 From b35e434caeb7448a93c14119e73d7e54d3864337 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 22 Jun 2022 18:34:39 +0200 Subject: link-tests: clean up linker testing harness --- lib/std/build/CheckObjectStep.zig | 193 ++++++++++++++++------------------- test/link/macho/entry/build.zig | 8 +- test/link/macho/frameworks/build.zig | 5 +- 3 files changed, 97 insertions(+), 109 deletions(-) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 4f9a7f0997..40266d0c85 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -36,20 +36,52 @@ pub fn create(builder: *Builder, source: build.FileSource, obj_format: std.Targe } const Action = union(enum) { - exact_match: []const u8, - extract_var: struct { - fuzzy_match: []const u8, - var_name: []const u8, - var_value: u64, - }, - compare: CompareAction, + match: MatchAction, + compute_eq: ComputeEqAction, }; -const CompareAction = struct { - expected: union(enum) { - literal: u64, - varr: []const u8, - }, +const MatchAction = struct { + needle: []const u8, + + fn match(act: MatchAction, haystack: []const u8, global_vars: anytype) !bool { + var hay_it = mem.tokenize(u8, mem.trim(u8, haystack, " "), " "); + var needle_it = mem.tokenize(u8, mem.trim(u8, act.needle, " "), " "); + + while (needle_it.next()) |needle_tok| { + const hay_tok = hay_it.next() orelse return false; + + if (mem.indexOf(u8, needle_tok, "{*}")) |index| { + // We have fuzzy matchers within the search pattern, so we match substrings. + var start = index; + var n_tok = needle_tok; + var h_tok = hay_tok; + while (true) { + n_tok = n_tok[start + 3 ..]; + const inner = if (mem.indexOf(u8, n_tok, "{*}")) |sub_end| + n_tok[0..sub_end] + else + n_tok; + if (mem.indexOf(u8, h_tok, inner) == null) return false; + start = mem.indexOf(u8, n_tok, "{*}") orelse break; + } + } else if (mem.startsWith(u8, needle_tok, "{")) { + const closing_brace = mem.indexOf(u8, needle_tok, "}") orelse return error.MissingClosingBrace; + if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast; + + const name = needle_tok[1..closing_brace]; + const value = try std.fmt.parseInt(u64, hay_tok, 16); + try global_vars.putNoClobber(name, value); + } else { + if (!mem.eql(u8, hay_tok, needle_tok)) return false; + } + } + + return true; + } +}; + +const ComputeEqAction = struct { + expected: []const u8, var_stack: std.ArrayList([]const u8), op_stack: std.ArrayList(Op), @@ -69,43 +101,29 @@ const Check = struct { }; } - fn exactMatch(self: *Check, phrase: []const u8) void { + fn match(self: *Check, needle: []const u8) void { self.actions.append(.{ - .exact_match = self.builder.dupe(phrase), + .match = .{ .needle = self.builder.dupe(needle) }, }) catch unreachable; } - fn extractVar(self: *Check, phrase: []const u8, var_name: []const u8) void { + fn computeEq(self: *Check, act: ComputeEqAction) void { self.actions.append(.{ - .extract_var = .{ - .fuzzy_match = self.builder.dupe(phrase), - .var_name = self.builder.dupe(var_name), - .var_value = undefined, - }, + .compute_eq = act, }) catch unreachable; } }; pub fn check(self: *CheckObjectStep, phrase: []const u8) void { var new_check = Check.create(self.builder); - new_check.exactMatch(phrase); + new_check.match(phrase); self.checks.append(new_check) catch unreachable; } pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; - last.exactMatch(phrase); -} - -pub fn checkNextExtract(self: *CheckObjectStep, comptime phrase: []const u8) void { - assert(self.checks.items.len > 0); - const matcher_start = comptime mem.indexOf(u8, phrase, "{") orelse - @compileError("missing { } matcher"); - const matcher_end = comptime mem.indexOf(u8, phrase, "}") orelse - @compileError("missing { } matcher"); - const last = &self.checks.items[self.checks.items.len - 1]; - last.extractVar(phrase[0..matcher_start], phrase[matcher_start + 1 .. matcher_end]); + last.match(phrase); } pub fn checkInSymtab(self: *CheckObjectStep) void { @@ -113,18 +131,15 @@ pub fn checkInSymtab(self: *CheckObjectStep) void { self.check("symtab"); } -pub fn checkCompare(self: *CheckObjectStep, comptime phrase: []const u8, expected: anytype) void { - comptime assert(phrase[0] == '{'); - comptime assert(phrase[phrase.len - 1] == '}'); - +pub fn checkComputeEq(self: *CheckObjectStep, program: []const u8, expected: []const u8) void { const gpa = self.builder.allocator; - var ca = CompareAction{ + var ca = ComputeEqAction{ .expected = expected, .var_stack = std.ArrayList([]const u8).init(gpa), - .op_stack = std.ArrayList(CompareAction.Op).init(gpa), + .op_stack = std.ArrayList(ComputeEqAction.Op).init(gpa), }; - var it = mem.tokenize(u8, phrase[1 .. phrase.len - 1], " "); + var it = mem.tokenize(u8, program, " "); while (it.next()) |next| { if (mem.eql(u8, next, "+")) { ca.op_stack.append(.add) catch unreachable; @@ -134,7 +149,7 @@ pub fn checkCompare(self: *CheckObjectStep, comptime phrase: []const u8, expecte } var new_check = Check.create(self.builder); - new_check.actions.append(.{ .compare = ca }) catch unreachable; + new_check.computeEq(ca); self.checks.append(new_check) catch unreachable; } @@ -159,80 +174,50 @@ fn make(step: *Step) !void { var vars = std.StringHashMap(u64).init(gpa); for (self.checks.items) |chk| { - const first_action = chk.actions.items[0]; - - switch (first_action) { - .exact_match => |first| { - if (mem.indexOf(u8, output, first)) |index| { - // TODO backtrack to track current scope - var it = std.mem.tokenize(u8, output[index..], "\r\n"); - - outer: for (chk.actions.items[1..]) |next_action| { - switch (next_action) { - .exact_match => |exact| { - while (it.next()) |line| { - if (mem.eql(u8, line, exact)) { - std.debug.print("{s} == {s}\n", .{ line, exact }); - continue :outer; - } - std.debug.print("{s} != {s}\n", .{ line, exact }); - } else { - return error.TestFailed; - } - }, - .extract_var => |extract| { - const phrase = extract.fuzzy_match; - while (it.next()) |line| { - if (mem.indexOf(u8, line, phrase)) |found| { - std.debug.print("{s} in {s}\n", .{ phrase, line }); - // Extract variable and save back in the action. - const trimmed = mem.trim(u8, line[found + phrase.len ..], " "); - const parsed = try std.fmt.parseInt(u64, trimmed, 16); - try vars.putNoClobber(extract.var_name, parsed); - continue :outer; - } - std.debug.print("{s} not in {s}\n", .{ extract.fuzzy_match, line }); - } - }, - .compare => unreachable, + var it = mem.tokenize(u8, output, "\r\n"); + for (chk.actions.items) |act| { + switch (act) { + .match => |match_act| { + while (it.next()) |line| { + if (try match_act.match(line, &vars)) { + std.debug.print("{s} == {s}\n", .{ line, match_act.needle }); + break; + } else { + std.debug.print("{s} != {s}\n", .{ line, match_act.needle }); } + } else { + return error.TestFailed; + } + }, + .compute_eq => |c_eq| { + var values = std.ArrayList(u64).init(gpa); + try values.ensureTotalCapacity(c_eq.var_stack.items.len); + for (c_eq.var_stack.items) |vv| { + const val = vars.get(vv) orelse return error.TestFailed; + values.appendAssumeCapacity(val); } - } else { - return error.TestFailed; - } - }, - .compare => |act| { - var values = std.ArrayList(u64).init(gpa); - try values.ensureTotalCapacity(act.var_stack.items.len); - for (act.var_stack.items) |vv| { - const val = vars.get(vv) orelse return error.TestFailed; - values.appendAssumeCapacity(val); - } - var op_i: usize = 1; - var reduced: u64 = values.items[0]; - for (act.op_stack.items) |op| { - const other = values.items[op_i]; - switch (op) { - .add => { - reduced += other; - }, + var op_i: usize = 1; + var reduced: u64 = values.items[0]; + for (c_eq.op_stack.items) |op| { + const other = values.items[op_i]; + switch (op) { + .add => { + reduced += other; + }, + } } - } - const expected = switch (act.expected) { - .literal => |exp| exp, - .varr => |vv| vars.get(vv) orelse return error.TestFailed, - }; - if (reduced != expected) return error.TestFailed; - }, - .extract_var => unreachable, + const expected = vars.get(c_eq.expected) orelse return error.TestFailed; + if (reduced != expected) return error.TestFailed; + }, + } } } var it = vars.iterator(); while (it.next()) |entry| { - std.debug.print(" {s} => {x}", .{ entry.key_ptr.*, entry.value_ptr.* }); + std.debug.print(" {s} => {x}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); } } diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index 78225b0d73..82d3917521 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -16,15 +16,15 @@ pub fn build(b: *Builder) void { const check_exe = exe.checkObject(.macho); check_exe.check("segname __TEXT"); - check_exe.checkNextExtract("vmaddr {vmaddr}"); + check_exe.checkNext("vmaddr {vmaddr}"); check_exe.check("cmd MAIN"); - check_exe.checkNextExtract("entryoff {entryoff}"); + check_exe.checkNext("entryoff {entryoff}"); check_exe.checkInSymtab(); - check_exe.checkNextExtract("_non_main {n_value}"); + check_exe.checkNext("_non_main {n_value}"); - check_exe.checkCompare("{vmaddr entryoff +}", .{ .varr = "n_value" }); + check_exe.checkComputeEq("vmaddr entryoff +", "n_value"); test_step.dependOn(&check_exe.step); diff --git a/test/link/macho/frameworks/build.zig b/test/link/macho/frameworks/build.zig index f196f47b1b..a85f6a7350 100644 --- a/test/link/macho/frameworks/build.zig +++ b/test/link/macho/frameworks/build.zig @@ -15,7 +15,10 @@ pub fn build(b: *Builder) void { const check = exe.checkObject(.macho); check.check("cmd LOAD_DYLIB"); - check.checkNext("name /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa"); + check.checkNext("name {*}Cocoa"); + + check.check("cmd LOAD_DYLIB"); + check.checkNext("name {*}libobjc{*}.dylib"); test_step.dependOn(&check.step); -- cgit v1.2.3 From 51f2442fc4160415796cf6271d1951969ab71c83 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 22 Jun 2022 22:24:52 +0200 Subject: link-tests: clean up error messages in case of failure --- lib/std/build/CheckObjectStep.zig | 47 +++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 40266d0c85..66edc084cd 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -4,6 +4,7 @@ const build = std.build; const fs = std.fs; const macho = std.macho; const mem = std.mem; +const testing = std.testing; const CheckObjectStep = @This(); @@ -179,13 +180,16 @@ fn make(step: *Step) !void { switch (act) { .match => |match_act| { while (it.next()) |line| { - if (try match_act.match(line, &vars)) { - std.debug.print("{s} == {s}\n", .{ line, match_act.needle }); - break; - } else { - std.debug.print("{s} != {s}\n", .{ line, match_act.needle }); - } + if (try match_act.match(line, &vars)) break; } else { + std.debug.print( + \\ + \\========= Expected to find: ========================== + \\{s} + \\========= But parsed file does not contain it: ======= + \\{s} + \\ + , .{ match_act.needle, output }); return error.TestFailed; } }, @@ -193,7 +197,17 @@ fn make(step: *Step) !void { var values = std.ArrayList(u64).init(gpa); try values.ensureTotalCapacity(c_eq.var_stack.items.len); for (c_eq.var_stack.items) |vv| { - const val = vars.get(vv) orelse return error.TestFailed; + const val = vars.get(vv) orelse { + std.debug.print( + \\ + \\========= Variable was not extracted: =========== + \\{s} + \\========= From parsed file: ===================== + \\{s} + \\ + , .{ vv, output }); + return error.TestFailed; + }; values.appendAssumeCapacity(val); } @@ -208,17 +222,22 @@ fn make(step: *Step) !void { } } - const expected = vars.get(c_eq.expected) orelse return error.TestFailed; - if (reduced != expected) return error.TestFailed; + const expected = vars.get(c_eq.expected) orelse { + std.debug.print( + \\ + \\========= Variable was not extracted: =========== + \\{s} + \\========= From parsed file: ===================== + \\{s} + \\ + , .{ c_eq.expected, output }); + return error.TestFailed; + }; + try testing.expectEqual(reduced, expected); }, } } } - - var it = vars.iterator(); - while (it.next()) |entry| { - std.debug.print(" {s} => {x}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); - } } const Opts = struct { -- cgit v1.2.3 From e6c012c7432a6de409bf08eb7964b3d99fe4362e Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 22 Jun 2022 22:40:05 +0200 Subject: link-tests: add better docs --- lib/std/build/CheckObjectStep.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 66edc084cd..9af66d88fa 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -41,9 +41,22 @@ const Action = union(enum) { compute_eq: ComputeEqAction, }; +/// MatchAction is the main building block of standard matchers with optional eat-all token `{*}` +/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature +/// i.e., it won't really handle edge cases/nontrivial examples. But given that we do want to use +/// it mainly to test the output of our object format parser-dumpers when testing the linkers, etc. +/// it should be plenty useful in its current form. const MatchAction = struct { needle: []const u8, + /// Will return true if the `needle` was found in the `haystack`. + /// Some examples include: + /// + /// LC 0 => will match in its entirety + /// vmaddr {vmaddr} => will match `vmaddr` and then extract the following value as u64 + /// and save under `vmaddr` global name (see `global_vars` param) + /// name {*}libobjc{*}.dylib => will match `name` followed by a token which contains `libobjc` and `.dylib` + /// in that order with other letters in between fn match(act: MatchAction, haystack: []const u8, global_vars: anytype) !bool { var hay_it = mem.tokenize(u8, mem.trim(u8, haystack, " "), " "); var needle_it = mem.tokenize(u8, mem.trim(u8, act.needle, " "), " "); @@ -81,6 +94,12 @@ const MatchAction = struct { } }; +/// ComputeEqAction can be used to perform an operation on the extracted global variables +/// using the MatchAction. It currently only supports an addition. The operation is required +/// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well, +/// to avoid any parsing really). +/// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively +/// they could then be added with this simple program `vmaddr entryoff +`. const ComputeEqAction = struct { expected: []const u8, var_stack: std.ArrayList([]const u8), @@ -115,23 +134,32 @@ const Check = struct { } }; +/// Creates a new sequence of actions with `phrase` as the first anchor searched phrase. pub fn check(self: *CheckObjectStep, phrase: []const u8) void { var new_check = Check.create(self.builder); new_check.match(phrase); self.checks.append(new_check) catch unreachable; } +/// Adds another searched phrase to the latest created Check with `CheckObjectStep.check(...)`. +/// Asserts at least one check already exists. pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { assert(self.checks.items.len > 0); const last = &self.checks.items[self.checks.items.len - 1]; last.match(phrase); } +/// Creates a new check checking specifically symbol table parsed and dumped from the object +/// file. +/// Issuing this check will force parsing and dumping of the symbol table. pub fn checkInSymtab(self: *CheckObjectStep) void { self.dump_symtab = true; self.check("symtab"); } +/// Creates a new standalone, singular check which allows running simple binary operations +/// on the extracted variables. It will then compare the reduced program with the value of +/// the expected variable. pub fn checkComputeEq(self: *CheckObjectStep, program: []const u8, expected: []const u8) void { const gpa = self.builder.allocator; var ca = ComputeEqAction{ -- cgit v1.2.3 From 6e04c2faabf4d632f80fa97ccbb0a20ad42a5e9f Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 23 Jun 2022 12:14:10 +0200 Subject: link-tests: fix parsing symtab for macho --- lib/std/build/CheckObjectStep.zig | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 9af66d88fa..2aaec8c4a7 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -303,10 +303,8 @@ const MachODumper = struct { if (symtab_cmd) |cmd| { try writer.writeAll("symtab\n"); const strtab = bytes[cmd.stroff..][0..cmd.strsize]; - const symtab = @ptrCast( - [*]const macho.nlist_64, - @alignCast(@alignOf(macho.nlist_64), bytes.ptr + cmd.symoff), - )[0..cmd.nsyms]; + const raw_symtab = bytes[cmd.symoff..][0 .. cmd.nsyms * @sizeOf(macho.nlist_64)]; + const symtab = mem.bytesAsSlice(macho.nlist_64, raw_symtab); for (symtab) |sym| { if (sym.stab()) continue; -- cgit v1.2.3 From 03ddb42b8bb96815c1bb4b857ffdfb94191ab861 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 23 Jun 2022 12:56:28 +0200 Subject: link-tests: rename check() to checkStart() Do not hardcode the symtab label; instead allow each parser to define its own. Check for missing extractor value in the matcher when matching `{}`. --- lib/std/build/CheckObjectStep.zig | 15 +++++++++++---- test/link/macho/dylib/build.zig | 6 +++--- test/link/macho/entry/build.zig | 4 ++-- test/link/macho/frameworks/build.zig | 4 ++-- test/link/macho/pagezero/build.zig | 6 +++--- test/link/macho/stack_size/build.zig | 2 +- 6 files changed, 22 insertions(+), 15 deletions(-) (limited to 'lib/std/build/CheckObjectStep.zig') diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 2aaec8c4a7..65a57f8832 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -83,6 +83,7 @@ const MatchAction = struct { if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast; const name = needle_tok[1..closing_brace]; + if (name.len == 0) return error.MissingBraceValue; const value = try std.fmt.parseInt(u64, hay_tok, 16); try global_vars.putNoClobber(name, value); } else { @@ -135,13 +136,13 @@ const Check = struct { }; /// Creates a new sequence of actions with `phrase` as the first anchor searched phrase. -pub fn check(self: *CheckObjectStep, phrase: []const u8) void { +pub fn checkStart(self: *CheckObjectStep, phrase: []const u8) void { var new_check = Check.create(self.builder); new_check.match(phrase); self.checks.append(new_check) catch unreachable; } -/// Adds another searched phrase to the latest created Check with `CheckObjectStep.check(...)`. +/// Adds another searched phrase to the latest created Check with `CheckObjectStep.checkStart(...)`. /// Asserts at least one check already exists. pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { assert(self.checks.items.len > 0); @@ -154,7 +155,11 @@ pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { /// Issuing this check will force parsing and dumping of the symbol table. pub fn checkInSymtab(self: *CheckObjectStep) void { self.dump_symtab = true; - self.check("symtab"); + const symtab_label = switch (self.obj_format) { + .macho => MachODumper.symtab_label, + else => @panic("TODO other parsers"), + }; + self.checkStart(symtab_label); } /// Creates a new standalone, singular check which allows running simple binary operations @@ -274,6 +279,8 @@ const Opts = struct { }; const MachODumper = struct { + const symtab_label = "symtab"; + fn parseAndDump(bytes: []const u8, opts: Opts) ![]const u8 { const gpa = opts.gpa orelse unreachable; // MachO dumper requires an allocator var stream = std.io.fixedBufferStream(bytes); @@ -301,7 +308,7 @@ const MachODumper = struct { } if (symtab_cmd) |cmd| { - try writer.writeAll("symtab\n"); + try writer.writeAll(symtab_label ++ "\n"); const strtab = bytes[cmd.stroff..][0..cmd.strsize]; const raw_symtab = bytes[cmd.symoff..][0 .. cmd.nsyms * @sizeOf(macho.nlist_64)]; const symtab = mem.bytesAsSlice(macho.nlist_64, raw_symtab); diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index ad116e23e9..1587def9b8 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *Builder) void { dylib.install(); const check_dylib = dylib.checkObject(.macho); - check_dylib.check("cmd ID_DYLIB"); + check_dylib.checkStart("cmd ID_DYLIB"); check_dylib.checkNext("name @rpath/liba.dylib"); check_dylib.checkNext("timestamp 2"); check_dylib.checkNext("current version 10000"); @@ -31,13 +31,13 @@ pub fn build(b: *Builder) void { exe.addRPath(b.pathFromRoot("zig-out/lib")); const check_exe = exe.checkObject(.macho); - check_exe.check("cmd LOAD_DYLIB"); + check_exe.checkStart("cmd LOAD_DYLIB"); check_exe.checkNext("name @rpath/liba.dylib"); check_exe.checkNext("timestamp 2"); check_exe.checkNext("current version 10000"); check_exe.checkNext("compatibility version 10000"); - check_exe.check("cmd RPATH"); + check_exe.checkStart("cmd RPATH"); check_exe.checkNext(std.fmt.allocPrint(b.allocator, "path {s}", .{b.pathFromRoot("zig-out/lib")}) catch unreachable); test_step.dependOn(&check_exe.step); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index 82d3917521..f0ac084e6a 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -15,10 +15,10 @@ pub fn build(b: *Builder) void { const check_exe = exe.checkObject(.macho); - check_exe.check("segname __TEXT"); + check_exe.checkStart("segname __TEXT"); check_exe.checkNext("vmaddr {vmaddr}"); - check_exe.check("cmd MAIN"); + check_exe.checkStart("cmd MAIN"); check_exe.checkNext("entryoff {entryoff}"); check_exe.checkInSymtab(); diff --git a/test/link/macho/frameworks/build.zig b/test/link/macho/frameworks/build.zig index 2e1c8f3211..7086606f30 100644 --- a/test/link/macho/frameworks/build.zig +++ b/test/link/macho/frameworks/build.zig @@ -14,12 +14,12 @@ pub fn build(b: *Builder) void { exe.linkFramework("Cocoa"); const check = exe.checkObject(.macho); - check.check("cmd LOAD_DYLIB"); + check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name {*}Cocoa"); switch (mode) { .Debug, .ReleaseSafe => { - check.check("cmd LOAD_DYLIB"); + check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name {*}libobjc{*}.dylib"); }, else => {}, diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index a35be919c5..e858d1f4d8 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -15,12 +15,12 @@ pub fn build(b: *Builder) void { exe.pagezero_size = 0x4000; const check = exe.checkObject(.macho); - check.check("LC 0"); + check.checkStart("LC 0"); check.checkNext("segname __PAGEZERO"); check.checkNext("vmaddr 0"); check.checkNext("vmsize 4000"); - check.check("segname __TEXT"); + check.checkStart("segname __TEXT"); check.checkNext("vmaddr 4000"); test_step.dependOn(&check.step); @@ -34,7 +34,7 @@ pub fn build(b: *Builder) void { exe.pagezero_size = 0; const check = exe.checkObject(.macho); - check.check("LC 0"); + check.checkStart("LC 0"); check.checkNext("segname __TEXT"); check.checkNext("vmaddr 0"); diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index b840f8928c..8da59dcb53 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *Builder) void { exe.stack_size = 0x100000000; const check_exe = exe.checkObject(.macho); - check_exe.check("cmd MAIN"); + check_exe.checkStart("cmd MAIN"); check_exe.checkNext("stacksize 100000000"); test_step.dependOn(&check_exe.step); -- cgit v1.2.3