authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-19 09:45:05+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-19 09:45:05+01:00
logf026939a40b3567f1a798547391b398dc40db49a
tree699f7b7b6c1af84f878a787260e8b598f20b8e26
parent6f15eedff1bd32085808ab58f095ab549b493745

macho: enable hot update state only when on compatible host


1 files changed, 27 insertions(+), 12 deletions(-)

src/link/MachO.zig+27-12
......@@ -221,8 +221,13 @@ lazy_bindings: BindingTable = .{},
221221/// Table of tracked Decls.
222222decls: std.AutoArrayHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
223223
224/// Mach task used when the compiler is in hot-code swapping mode.
225mach_task: ?std.os.darwin.MachTask = null,
224/// Hot-code swapping state.
225hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{},
226
227const is_hot_update_compatible = switch (builtin.target.os.tag) {
228 .macos => true,
229 else => false,
230};
226231
227232const DeclMetadata = struct {
228233 atom: Atom.Index,
......@@ -303,6 +308,10 @@ pub const SymbolWithLoc = struct {
303308 }
304309};
305310
311const HotUpdateState = struct {
312 mach_task: ?std.os.darwin.MachTask = null,
313};
314
306315/// When allocating, the ideal_capacity is calculated by
307316/// actual_capacity + (actual_capacity / ideal_factor)
308317const ideal_factor = 3;
......@@ -1085,14 +1094,16 @@ pub fn writeAtom(self: *MachO, atom_index: Atom.Index, code: []u8) !void {
10851094 try Atom.resolveRelocations(self, atom_index, relocs.items, code);
10861095 }
10871096
1088 if (self.base.child_pid) |pid| blk: {
1089 const task = self.mach_task orelse {
1090 log.warn("cannot hot swap: no Mach task acquired for child process with pid {d}", .{pid});
1091 break :blk;
1092 };
1093 self.updateAtomInMemory(task, section.segment_index, sym.n_value, code) catch |err| {
1094 log.warn("cannot hot swap: writing to memory failed: {s}", .{@errorName(err)});
1095 };
1097 if (is_hot_update_compatible) {
1098 if (self.base.child_pid) |pid| blk: {
1099 const task = self.hot_state.mach_task orelse {
1100 log.warn("cannot hot swap: no Mach task acquired for child process with pid {d}", .{pid});
1101 break :blk;
1102 };
1103 self.updateAtomInMemory(task, section.segment_index, sym.n_value, code) catch |err| {
1104 log.warn("cannot hot swap: writing to memory failed: {s}", .{@errorName(err)});
1105 };
1106 }
10961107 }
10971108
10981109 try self.base.file.?.pwriteAll(code, file_offset);
......@@ -3812,9 +3823,11 @@ pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 {
38123823}
38133824
38143825pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void {
3826 if (!is_hot_update_compatible) return;
3827
38153828 const mach_task = try std.os.darwin.machTaskForPid(pid);
38163829 log.debug("Mach task for pid {d}: {any}", .{ pid, mach_task });
3817 self.mach_task = mach_task;
3830 self.hot_state.mach_task = mach_task;
38183831
38193832 // TODO start exception handler in another thread
38203833
......@@ -3823,6 +3836,8 @@ pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void {
38233836}
38243837
38253838pub fn ptraceDetach(self: *MachO, pid: std.os.pid_t) !void {
3839 if (!is_hot_update_compatible) return;
3840
38263841 _ = pid;
38273842
38283843 // TODO stop exception handler
......@@ -3830,7 +3845,7 @@ pub fn ptraceDetach(self: *MachO, pid: std.os.pid_t) !void {
38303845 // TODO see comment in ptraceAttach
38313846 // try std.os.ptrace(std.os.darwin.PT.DETACH, pid, 0, 0);
38323847
3833 self.mach_task = null;
3848 self.hot_state.mach_task = null;
38343849}
38353850
38363851pub fn makeStaticString(bytes: []const u8) [16]u8 {