authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-17 09:25:49+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-18 21:53:36+01:00
log37192bcdcb38be2266133f6d46dce5a842984c06
treef529668b8c48d0c2b6d123bccfc0bf8556c218a1
parentf1e25cf43ec60075a4fc6f3eceb5a3af1f9f0712

macos: HCS PoC working


4 files changed, 43 insertions(+), 33 deletions(-)

lib/std/c/darwin.zig+1-1
......@@ -3316,7 +3316,7 @@ pub fn getKernError(err: kern_return_t) KernE {
33163316
33173317pub fn unexpectedKernError(err: KernE) std.os.UnexpectedError {
33183318 if (std.os.unexpected_error_tracing) {
3319 std.debug.print("unexpected errno: {d}\n", .{@enumToInt(err)});
3319 std.debug.print("unexpected error: {d}\n", .{@enumToInt(err)});
33203320 std.debug.dumpCurrentStackTrace(null);
33213321 }
33223322 return error.Unexpected;
src/link.zig+9-6
......@@ -397,9 +397,10 @@ pub const File = struct {
397397 if (macho.mach_task == null) {
398398 if (std.os.darwin.machTaskForPid(pid)) |task| {
399399 macho.mach_task = task;
400 std.os.ptrace(std.os.darwin.PT.ATTACHEXC, pid, 0, 0) catch |err| {
401 log.warn("ptrace failure: {s}", .{@errorName(err)});
402 };
400 // TODO enable ones we register for exceptions
401 // std.os.ptrace(std.os.darwin.PT.ATTACHEXC, pid, 0, 0) catch |err| {
402 // log.warn("ptrace failure: {s}", .{@errorName(err)});
403 // };
403404 } else |err| {
404405 log.warn("failed to acquire Mach task for child process: {s}", .{@errorName(err)});
405406 }
......@@ -443,9 +444,11 @@ pub const File = struct {
443444 .linux => std.os.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0) catch |err| {
444445 log.warn("ptrace failure: {s}", .{@errorName(err)});
445446 },
446 .macos => std.os.ptrace(std.os.darwin.PT.KILL, pid, 0, 0) catch |err| {
447 log.warn("ptrace failure: {s}", .{@errorName(err)});
448 },
447 .macos => {},
448 // TODO see comment above in makeWritable
449 // .macos => std.os.ptrace(std.os.darwin.PT.DETACH, pid, 0, 0) catch |err| {
450 // log.warn("ptrace failure: {s}", .{@errorName(err)});
451 // },
449452 else => return error.HotSwapUnavailableOnHostOperatingSystem,
450453 }
451454 }
src/link/MachO.zig+18-8
......@@ -587,7 +587,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
587587 try self.allocateSpecialSymbols();
588588
589589 for (self.relocs.keys()) |atom_index| {
590 if (self.relocs.get(atom_index) == null) continue;
590 const relocs = self.relocs.get(atom_index).?;
591 const needs_update = for (relocs.items) |reloc| {
592 if (reloc.dirty) break true;
593 } else false;
594
595 if (!needs_update) continue;
591596
592597 const atom = self.getAtom(atom_index);
593598 const sym = atom.getSymbol(self);
......@@ -1085,7 +1090,7 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void {
10851090 log.warn("cannot hot swap: no Mach task acquired for child process with pid {d}", .{pid});
10861091 break :blk;
10871092 };
1088 self.writeAtomToMemory(task, section.segment_index, sym.n_value, code) catch |err| {
1093 self.updateAtomInMemory(task, section.segment_index, sym.n_value, code) catch |err| {
10891094 log.warn("cannot hot swap: writing to memory failed: {s}", .{@errorName(err)});
10901095 };
10911096 }
......@@ -1093,13 +1098,13 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void {
10931098 try self.base.file.?.pwriteAll(code, file_offset);
10941099}
10951100
1096fn writeAtomToMemory(self: *MachO, task: std.os.darwin.MachTask, segment_index: u8, addr: u64, code: []const u8) !void {
1101fn updateAtomInMemory(self: *MachO, task: std.os.darwin.MachTask, segment_index: u8, addr: u64, code: []const u8) !void {
10971102 const segment = self.segments.items[segment_index];
1098 if (!segment.isWriteable()) {
1099 try task.setCurrProtection(addr, code.len, macho.PROT.READ | macho.PROT.WRITE | macho.PROT.COPY);
1100 }
1101 defer if (!segment.isWriteable()) task.setCurrProtection(addr, code.len, segment.initprot) catch {};
1102 const nwritten = try task.writeMem(addr, code, self.base.options.target.cpu.arch);
1103 const cpu_arch = self.base.options.target.cpu.arch;
1104 const nwritten = if (!segment.isWriteable())
1105 try task.writeMemProtected(addr, code, cpu_arch)
1106 else
1107 try task.writeMem(addr, code, cpu_arch);
11031108 if (nwritten != code.len) return error.InputOutput;
11041109}
11051110
......@@ -1109,6 +1114,7 @@ fn writePtrWidthAtom(self: *MachO, atom_index: Atom.Index) !void {
11091114}
11101115
11111116fn markRelocsDirtyByTarget(self: *MachO, target: SymbolWithLoc) void {
1117 log.debug("marking relocs dirty by target: {}", .{target});
11121118 // TODO: reverse-lookup might come in handy here
11131119 for (self.relocs.values()) |*relocs| {
11141120 for (relocs.items) |*reloc| {
......@@ -1119,6 +1125,7 @@ fn markRelocsDirtyByTarget(self: *MachO, target: SymbolWithLoc) void {
11191125}
11201126
11211127fn markRelocsDirtyByAddress(self: *MachO, addr: u64) void {
1128 log.debug("marking relocs dirty by address: {x}", .{addr});
11221129 for (self.relocs.values()) |*relocs| {
11231130 for (relocs.items) |*reloc| {
11241131 const target_atom_index = reloc.getTargetAtomIndex(self) orelse continue;
......@@ -1743,6 +1750,8 @@ pub fn resolveDyldStubBinder(self: *MachO) !void {
17431750 if (self.dyld_stub_binder_index != null) return;
17441751 if (self.unresolved.count() == 0) return; // no need for a stub binder if we don't have any imports
17451752
1753 log.debug("resolving dyld_stub_binder", .{});
1754
17461755 const gpa = self.base.allocator;
17471756 const sym_index = try self.allocateSymbol();
17481757 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
......@@ -2829,6 +2838,7 @@ pub fn populateMissingMetadata(self: *MachO) !void {
28292838
28302839 if (self.linkedit_segment_cmd_index == null) {
28312840 self.linkedit_segment_cmd_index = @intCast(u8, self.segments.items.len);
2841
28322842 try self.segments.append(gpa, .{
28332843 .segname = makeStaticString("__LINKEDIT"),
28342844 .maxprot = macho.PROT.READ,
src/main.zig+15-18
......@@ -3320,21 +3320,20 @@ fn buildOutputType(
33203320
33213321 try server.listen(.{ .in = ip4_addr });
33223322
3323 while (true) {
3324 const conn = try server.accept();
3325 defer conn.stream.close();
3326
3327 try serve(
3328 comp,
3329 .{ .handle = conn.stream.handle },
3330 .{ .handle = conn.stream.handle },
3331 test_exec_args.items,
3332 self_exe_path,
3333 arg_mode,
3334 all_args,
3335 runtime_args_start,
3336 );
3337 }
3323 const conn = try server.accept();
3324 defer conn.stream.close();
3325
3326 try serve(
3327 comp,
3328 .{ .handle = conn.stream.handle },
3329 .{ .handle = conn.stream.handle },
3330 test_exec_args.items,
3331 self_exe_path,
3332 arg_mode,
3333 all_args,
3334 runtime_args_start,
3335 );
3336 return cleanExit();
33383337 },
33393338 }
33403339
......@@ -3465,9 +3464,7 @@ fn serve(
34653464 const hdr = try server.receiveMessage();
34663465
34673466 switch (hdr.tag) {
3468 .exit => {
3469 return cleanExit();
3470 },
3467 .exit => return,
34713468 .update => {
34723469 assert(main_progress_node.recently_updated_child == null);
34733470 tracy.frameMark();