Commit 5fbdfb3f34

Jakub Konka <kubkon@jakubkonka.com>
2022-06-21 15:44:22
link-tests: add CheckMachOStep
CheckMachOStep specialises CheckFileStep into directed (surgical) MachO file fuzzy searches. This will be the building block for comprehensive MachO linker tests.
1 parent 2d09540
Changed files (4)
lib
test
link
dylib
pagezero
lib/std/build/CheckMachOStep.zig
@@ -0,0 +1,210 @@
+const std = @import("../std.zig");
+const build = std.build;
+const Step = build.Step;
+const Builder = build.Builder;
+const fs = std.fs;
+const macho = std.macho;
+const mem = std.mem;
+
+const CheckMachOStep = @This();
+
+pub const base_id = .check_macho;
+
+step: Step,
+builder: *Builder,
+source: build.FileSource,
+max_bytes: usize = 20 * 1024 * 1024,
+lc_checks: std.ArrayList(LCCheck),
+
+const LCCheck = struct {
+    // common to most LCs
+    cmd: macho.LC,
+    name: ?[]const u8 = null,
+    // LC.SEGMENT_64 specific
+    index: ?usize = null,
+    vaddr: ?u64 = null,
+    memsz: ?u64 = null,
+    offset: ?u64 = null,
+    filesz: ?u64 = null,
+    // LC.LOAD_DYLIB specific
+    timestamp: ?u64 = null,
+    current_version: ?u32 = null,
+    compat_version: ?u32 = null,
+};
+
+pub fn create(builder: *Builder, source: build.FileSource) *CheckMachOStep {
+    const gpa = builder.allocator;
+    const self = gpa.create(CheckMachOStep) catch unreachable;
+    self.* = CheckMachOStep{
+        .builder = builder,
+        .step = Step.init(.check_file, "CheckMachO", gpa, make),
+        .source = source.dupe(builder),
+        .lc_checks = std.ArrayList(LCCheck).init(gpa),
+    };
+    self.source.addStepDependencies(&self.step);
+    return self;
+}
+
+pub fn checkLoadCommand(self: *CheckMachOStep, check: LCCheck) void {
+    self.lc_checks.append(.{
+        .cmd = check.cmd,
+        .index = check.index,
+        .name = if (check.name) |name| self.builder.dupe(name) else null,
+        .vaddr = check.vaddr,
+        .memsz = check.memsz,
+        .offset = check.offset,
+        .filesz = check.filesz,
+        .timestamp = check.timestamp,
+        .current_version = check.current_version,
+        .compat_version = check.compat_version,
+    }) catch unreachable;
+}
+
+fn make(step: *Step) !void {
+    const self = @fieldParentPtr(CheckMachOStep, "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);
+
+    // Parse the object file's header
+    var stream = std.io.fixedBufferStream(contents);
+    const reader = stream.reader();
+
+    const hdr = try reader.readStruct(macho.mach_header_64);
+    if (hdr.magic != macho.MH_MAGIC_64) {
+        return error.InvalidMagicNumber;
+    }
+
+    var load_commands = std.ArrayList(macho.LoadCommand).init(gpa);
+    try load_commands.ensureTotalCapacity(hdr.ncmds);
+
+    var i: u16 = 0;
+    while (i < hdr.ncmds) : (i += 1) {
+        var cmd = try macho.LoadCommand.read(gpa, reader);
+        load_commands.appendAssumeCapacity(cmd);
+    }
+
+    outer: for (self.lc_checks.items) |ch| {
+        if (ch.index) |index| {
+            const lc = load_commands.items[index];
+            try cmpLoadCommand(ch, lc);
+        } else {
+            for (load_commands.items) |lc| {
+                if (lc.cmd() == ch.cmd) {
+                    try cmpLoadCommand(ch, lc);
+                    continue :outer;
+                }
+            } else {
+                return err("LC not found", ch.cmd, "");
+            }
+        }
+    }
+}
+
+fn cmpLoadCommand(exp: LCCheck, given: macho.LoadCommand) error{TestFailed}!void {
+    if (exp.cmd != given.cmd()) {
+        return err("LC mismatch", exp.cmd, given.cmd());
+    }
+    switch (exp.cmd) {
+        .SEGMENT_64 => {
+            const lc = given.segment.inner;
+            if (exp.name) |name| {
+                if (!mem.eql(u8, name, lc.segName())) {
+                    return err("segment name mismatch", name, lc.segName());
+                }
+            }
+            if (exp.vaddr) |vaddr| {
+                if (vaddr != lc.vmaddr) {
+                    return err("segment VM address mismatch", vaddr, lc.vmaddr);
+                }
+            }
+            if (exp.memsz) |memsz| {
+                if (memsz != lc.vmsize) {
+                    return err("segment VM size mismatch", memsz, lc.vmsize);
+                }
+            }
+            if (exp.offset) |offset| {
+                if (offset != lc.fileoff) {
+                    return err("segment file offset mismatch", offset, lc.fileoff);
+                }
+            }
+            if (exp.filesz) |filesz| {
+                if (filesz != lc.filesize) {
+                    return err("segment file size mismatch", filesz, lc.filesize);
+                }
+            }
+        },
+        .ID_DYLIB, .LOAD_DYLIB => {
+            const lc = given.dylib;
+            if (exp.name) |name| {
+                if (!mem.eql(u8, name, mem.sliceTo(lc.data, 0))) {
+                    return err("dylib path mismatch", name, mem.sliceTo(lc.data, 0));
+                }
+            }
+            if (exp.timestamp) |ts| {
+                if (ts != lc.inner.dylib.timestamp) {
+                    return err("timestamp mismatch", ts, lc.inner.dylib.timestamp);
+                }
+            }
+            if (exp.current_version) |cv| {
+                if (cv != lc.inner.dylib.current_version) {
+                    return err("current version mismatch", cv, lc.inner.dylib.current_version);
+                }
+            }
+            if (exp.compat_version) |cv| {
+                if (cv != lc.inner.dylib.compatibility_version) {
+                    return err("compatibility version mismatch", cv, lc.inner.dylib.compatibility_version);
+                }
+            }
+        },
+        .RPATH => {
+            const lc = given.rpath;
+            if (exp.name) |name| {
+                if (!mem.eql(u8, name, mem.sliceTo(lc.data, 0))) {
+                    return err("rpath path mismatch", name, mem.sliceTo(lc.data, 0));
+                }
+            }
+        },
+        else => @panic("TODO compare more load commands"),
+    }
+}
+
+fn err(msg: []const u8, exp: anytype, giv: anytype) error{TestFailed} {
+    const fmt_specifier = if (comptime isString(@TypeOf(exp))) "{s}" else switch (@typeInfo(@TypeOf(exp))) {
+        .Int => "{x}",
+        .Float => "{d}",
+        else => "{any}",
+    };
+    std.debug.print(
+        \\=====================================
+        \\{s}
+        \\
+        \\======== Expected to find: ==========
+        \\
+    ++ fmt_specifier ++
+        \\
+        \\======== But instead found: =========
+        \\
+    ++ fmt_specifier ++
+        \\
+        \\
+    , .{ msg, exp, giv });
+    return error.TestFailed;
+}
+
+fn isString(comptime T: type) bool {
+    switch (@typeInfo(T)) {
+        .Array => return std.meta.Elem(T) == u8,
+        .Pointer => |pinfo| {
+            switch (pinfo.size) {
+                .Slice, .Many => return std.meta.Elem(T) == u8,
+                else => switch (@typeInfo(pinfo.child)) {
+                    .Array => return isString(pinfo.child),
+                    else => return false,
+                },
+            }
+        },
+        else => return false,
+    }
+}
lib/std/build.zig
@@ -24,6 +24,7 @@ pub const TranslateCStep = @import("build/TranslateCStep.zig");
 pub const WriteFileStep = @import("build/WriteFileStep.zig");
 pub const RunStep = @import("build/RunStep.zig");
 pub const CheckFileStep = @import("build/CheckFileStep.zig");
+pub const CheckMachOStep = @import("build/CheckMachOStep.zig");
 pub const InstallRawStep = @import("build/InstallRawStep.zig");
 pub const OptionsStep = @import("build/OptionsStep.zig");
 
@@ -1864,6 +1865,10 @@ pub const LibExeObjStep = struct {
         return run_step;
     }
 
+    pub fn checkMachO(self: *LibExeObjStep) *CheckMachOStep {
+        return CheckMachOStep.create(self.builder, self.getOutputSource());
+    }
+
     pub fn setLinkerScriptPath(self: *LibExeObjStep, source: FileSource) void {
         self.linker_script = source.dupe(self.builder);
         source.addStepDependencies(&self.step);
@@ -3450,6 +3455,7 @@ pub const Step = struct {
         write_file,
         run,
         check_file,
+        check_macho,
         install_raw,
         options,
         custom,
test/link/dylib/build.zig
@@ -5,6 +5,7 @@ pub fn build(b: *Builder) void {
     const mode = b.standardReleaseOptions();
 
     const test_step = b.step("test", "Test");
+    test_step.dependOn(b.getInstallStep());
 
     const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
     dylib.setBuildMode(mode);
@@ -12,6 +13,18 @@ pub fn build(b: *Builder) void {
     dylib.linkLibC();
     dylib.install();
 
+    {
+        const check_macho = dylib.checkMachO();
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.ID_DYLIB,
+            .name = "@rpath/liba.dylib",
+            .timestamp = 2,
+            .current_version = 0x10000,
+            .compat_version = 0x10000,
+        });
+        test_step.dependOn(&check_macho.step);
+    }
+
     const exe = b.addExecutable("main", null);
     exe.setBuildMode(mode);
     exe.addCSourceFile("main.c", &.{});
@@ -20,17 +33,28 @@ pub fn build(b: *Builder) void {
     exe.addLibraryPath(b.pathFromRoot("zig-out/lib/"));
     exe.addRPath(b.pathFromRoot("zig-out/lib"));
 
+    {
+        const check_macho = exe.checkMachO();
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.LOAD_DYLIB,
+            .name = "@rpath/liba.dylib",
+            .timestamp = 2,
+            .current_version = 0x10000,
+            .compat_version = 0x10000,
+        });
+        test_step.dependOn(&check_macho.step);
+    }
+    {
+        const check_macho = exe.checkMachO();
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.RPATH,
+            .name = b.pathFromRoot("zig-out/lib"),
+        });
+        test_step.dependOn(&check_macho.step);
+    }
+
     const run = exe.run();
     run.cwd = b.pathFromRoot(".");
     run.expectStdOutEqual("Hello world");
-
-    const exp_dylib = std.macho.createLoadDylibCommand(b.allocator, "@rpath/liba.dylib", 2, 0x10000, 0x10000) catch unreachable;
-    var buf = std.ArrayList(u8).init(b.allocator);
-    defer buf.deinit();
-    exp_dylib.write(buf.writer()) catch unreachable;
-    const check_file = std.build.CheckFileStep.create(b, exe.getOutputSource(), &[_][]const u8{buf.items});
-
-    test_step.dependOn(b.getInstallStep());
     test_step.dependOn(&run.step);
-    test_step.dependOn(&check_file.step);
 }
test/link/pagezero/build.zig
@@ -5,30 +5,48 @@ pub fn build(b: *Builder) void {
     const mode = b.standardReleaseOptions();
 
     const test_step = b.step("test", "Test");
+    test_step.dependOn(b.getInstallStep());
 
-    const exe = b.addExecutable("main", null);
-    exe.setBuildMode(mode);
-    exe.addCSourceFile("main.c", &.{});
-    exe.linkLibC();
-    exe.pagezero_size = 0x4000;
-
-    var name: [16]u8 = undefined;
-    std.mem.set(u8, &name, 0);
-    std.mem.copy(u8, &name, "__PAGEZERO");
-    const pagezero_seg = std.macho.segment_command_64{
-        .cmdsize = @sizeOf(std.macho.segment_command_64),
-        .segname = name,
-        .vmaddr = 0,
-        .vmsize = 0x4000,
-        .fileoff = 0,
-        .filesize = 0,
-        .maxprot = 0,
-        .initprot = 0,
-        .nsects = 0,
-        .flags = 0,
-    };
-    const check_file = std.build.CheckFileStep.create(b, exe.getOutputSource(), &[_][]const u8{std.mem.asBytes(&pagezero_seg)});
+    {
+        const exe = b.addExecutable("pagezero", null);
+        exe.setBuildMode(mode);
+        exe.addCSourceFile("main.c", &.{});
+        exe.linkLibC();
+        exe.pagezero_size = 0x4000;
 
-    test_step.dependOn(b.getInstallStep());
-    test_step.dependOn(&check_file.step);
+        const check_macho = exe.checkMachO();
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.SEGMENT_64,
+            .index = 0,
+            .name = "__PAGEZERO",
+            .vaddr = 0,
+            .memsz = 0x4000,
+        });
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.SEGMENT_64,
+            .index = 1,
+            .name = "__TEXT",
+            .vaddr = 0x4000,
+        });
+
+        test_step.dependOn(&check_macho.step);
+    }
+
+    {
+        const exe = b.addExecutable("no_pagezero", null);
+        exe.setBuildMode(mode);
+        exe.addCSourceFile("main.c", &.{});
+        exe.linkLibC();
+        exe.pagezero_size = 0;
+
+        const check_macho = exe.checkMachO();
+        check_macho.checkLoadCommand(.{
+            .cmd = std.macho.LC.SEGMENT_64,
+            .index = 0,
+            .name = "__TEXT",
+            .vaddr = 0,
+        });
+
+        test_step.dependOn(&check_macho.step);
+    }
 }