| ... | ... | @@ -1,9 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const c = std.c.darwin; |
| 2 | 3 | const log = std.log; |
| 3 | 4 | const mem = std.mem; |
| 4 | | const os = @This(); |
| 5 | | |
| 6 | | pub usingnamespace @import("../c.zig"); |
| 7 | 5 | |
| 8 | 6 | pub const MachError = error{ |
| 9 | 7 | /// Not enough permissions held to perform the requested kernel |
| ... | ... | @@ -16,24 +14,24 @@ pub const MachError = error{ |
| 16 | 14 | }; |
| 17 | 15 | |
| 18 | 16 | pub const MachTask = struct { |
| 19 | | port: os.mach_port_name_t, |
| 17 | port: c.mach_port_name_t, |
| 20 | 18 | |
| 21 | 19 | pub fn isValid(self: MachTask) bool { |
| 22 | 20 | return self.port != 0; |
| 23 | 21 | } |
| 24 | 22 | |
| 25 | | pub fn getCurrProtection(task: MachTask, address: u64, len: usize) MachError!os.vm_prot_t { |
| 23 | pub fn getCurrProtection(task: MachTask, address: u64, len: usize) MachError!c.vm_prot_t { |
| 26 | 24 | var base_addr = address; |
| 27 | | var base_len: os.mach_vm_size_t = if (len == 1) 2 else len; |
| 28 | | var objname: os.mach_port_t = undefined; |
| 29 | | var info: os.vm_region_submap_info_64 = undefined; |
| 30 | | var count: os.mach_msg_type_number_t = os.VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; |
| 31 | | switch (os.getKernError(os.mach_vm_region( |
| 25 | var base_len: c.mach_vm_size_t = if (len == 1) 2 else len; |
| 26 | var objname: c.mach_port_t = undefined; |
| 27 | var info: c.vm_region_submap_info_64 = undefined; |
| 28 | var count: c.mach_msg_type_number_t = c.VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; |
| 29 | switch (c.getKernError(c.mach_vm_region( |
| 32 | 30 | task.port, |
| 33 | 31 | &base_addr, |
| 34 | 32 | &base_len, |
| 35 | | os.VM_REGION_BASIC_INFO_64, |
| 36 | | @ptrCast(os.vm_region_info_t, &info), |
| 33 | c.VM_REGION_BASIC_INFO_64, |
| 34 | @ptrCast(c.vm_region_info_t, &info), |
| 37 | 35 | &count, |
| 38 | 36 | &objname, |
| 39 | 37 | ))) { |
| ... | ... | @@ -46,16 +44,16 @@ pub const MachTask = struct { |
| 46 | 44 | } |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | | pub fn setMaxProtection(task: MachTask, address: u64, len: usize, prot: os.vm_prot_t) MachError!void { |
| 47 | pub fn setMaxProtection(task: MachTask, address: u64, len: usize, prot: c.vm_prot_t) MachError!void { |
| 50 | 48 | return task.setProtectionImpl(address, len, true, prot); |
| 51 | 49 | } |
| 52 | 50 | |
| 53 | | pub fn setCurrProtection(task: MachTask, address: u64, len: usize, prot: os.vm_prot_t) MachError!void { |
| 51 | pub fn setCurrProtection(task: MachTask, address: u64, len: usize, prot: c.vm_prot_t) MachError!void { |
| 54 | 52 | return task.setProtectionImpl(address, len, false, prot); |
| 55 | 53 | } |
| 56 | 54 | |
| 57 | | fn setProtectionImpl(task: MachTask, address: u64, len: usize, set_max: bool, prot: os.vm_prot_t) MachError!void { |
| 58 | | switch (os.getKernError(os.mach_vm_protect(task.port, address, len, @boolToInt(set_max), prot))) { |
| 55 | fn setProtectionImpl(task: MachTask, address: u64, len: usize, set_max: bool, prot: c.vm_prot_t) MachError!void { |
| 56 | switch (c.getKernError(c.mach_vm_protect(task.port, address, len, @boolToInt(set_max), prot))) { |
| 59 | 57 | .SUCCESS => return, |
| 60 | 58 | .FAILURE => return error.PermissionDenied, |
| 61 | 59 | else => |err| { |
| ... | ... | @@ -73,7 +71,7 @@ pub const MachTask = struct { |
| 73 | 71 | try task.setCurrProtection( |
| 74 | 72 | address, |
| 75 | 73 | buf.len, |
| 76 | | os.PROT.READ | os.PROT.WRITE | os.PROT.COPY, |
| 74 | c.PROT.READ | c.PROT.WRITE | c.PROT.COPY, |
| 77 | 75 | ); |
| 78 | 76 | defer { |
| 79 | 77 | task.setCurrProtection(address, buf.len, curr_prot) catch {}; |
| ... | ... | @@ -90,11 +88,11 @@ pub const MachTask = struct { |
| 90 | 88 | |
| 91 | 89 | while (total_written < count) { |
| 92 | 90 | const curr_size = maxBytesLeftInPage(page_size, curr_addr, count - total_written); |
| 93 | | switch (os.getKernError(os.mach_vm_write( |
| 91 | switch (c.getKernError(c.mach_vm_write( |
| 94 | 92 | task.port, |
| 95 | 93 | curr_addr, |
| 96 | 94 | @ptrToInt(out_buf.ptr), |
| 97 | | @intCast(os.mach_msg_type_number_t, curr_size), |
| 95 | @intCast(c.mach_msg_type_number_t, curr_size), |
| 98 | 96 | ))) { |
| 99 | 97 | .SUCCESS => {}, |
| 100 | 98 | .FAILURE => return error.PermissionDenied, |
| ... | ... | @@ -106,12 +104,12 @@ pub const MachTask = struct { |
| 106 | 104 | |
| 107 | 105 | switch (arch) { |
| 108 | 106 | .aarch64 => { |
| 109 | | var mattr_value: os.vm_machine_attribute_val_t = os.MATTR_VAL_CACHE_FLUSH; |
| 110 | | switch (os.getKernError(os.vm_machine_attribute( |
| 107 | var mattr_value: c.vm_machine_attribute_val_t = c.MATTR_VAL_CACHE_FLUSH; |
| 108 | switch (c.getKernError(c.vm_machine_attribute( |
| 111 | 109 | task.port, |
| 112 | 110 | curr_addr, |
| 113 | 111 | curr_size, |
| 114 | | os.MATTR_CACHE, |
| 112 | c.MATTR_CACHE, |
| 115 | 113 | &mattr_value, |
| 116 | 114 | ))) { |
| 117 | 115 | .SUCCESS => {}, |
| ... | ... | @@ -143,9 +141,9 @@ pub const MachTask = struct { |
| 143 | 141 | |
| 144 | 142 | while (total_read < count) { |
| 145 | 143 | const curr_size = maxBytesLeftInPage(page_size, curr_addr, count - total_read); |
| 146 | | var curr_bytes_read: os.mach_msg_type_number_t = 0; |
| 147 | | var vm_memory: os.vm_offset_t = undefined; |
| 148 | | switch (os.getKernError(os.mach_vm_read(task.port, curr_addr, curr_size, &vm_memory, &curr_bytes_read))) { |
| 144 | var curr_bytes_read: c.mach_msg_type_number_t = 0; |
| 145 | var vm_memory: c.vm_offset_t = undefined; |
| 146 | switch (c.getKernError(c.mach_vm_read(task.port, curr_addr, curr_size, &vm_memory, &curr_bytes_read))) { |
| 149 | 147 | .SUCCESS => {}, |
| 150 | 148 | .FAILURE => return error.PermissionDenied, |
| 151 | 149 | else => |err| { |
| ... | ... | @@ -155,7 +153,7 @@ pub const MachTask = struct { |
| 155 | 153 | } |
| 156 | 154 | |
| 157 | 155 | @memcpy(out_buf[0..].ptr, @intToPtr([*]const u8, vm_memory), curr_bytes_read); |
| 158 | | _ = os.vm_deallocate(os.mach_task_self(), vm_memory, curr_bytes_read); |
| 156 | _ = c.vm_deallocate(c.mach_task_self(), vm_memory, curr_bytes_read); |
| 159 | 157 | |
| 160 | 158 | out_buf = out_buf[curr_bytes_read..]; |
| 161 | 159 | curr_addr += curr_bytes_read; |
| ... | ... | @@ -179,20 +177,20 @@ pub const MachTask = struct { |
| 179 | 177 | |
| 180 | 178 | fn getPageSize(task: MachTask) MachError!usize { |
| 181 | 179 | if (task.isValid()) { |
| 182 | | var info_count = os.TASK_VM_INFO_COUNT; |
| 183 | | var vm_info: os.task_vm_info_data_t = undefined; |
| 184 | | switch (os.getKernError(os.task_info( |
| 180 | var info_count = c.TASK_VM_INFO_COUNT; |
| 181 | var vm_info: c.task_vm_info_data_t = undefined; |
| 182 | switch (c.getKernError(c.task_info( |
| 185 | 183 | task.port, |
| 186 | | os.TASK_VM_INFO, |
| 187 | | @ptrCast(os.task_info_t, &vm_info), |
| 184 | c.TASK_VM_INFO, |
| 185 | @ptrCast(c.task_info_t, &vm_info), |
| 188 | 186 | &info_count, |
| 189 | 187 | ))) { |
| 190 | 188 | .SUCCESS => return @intCast(usize, vm_info.page_size), |
| 191 | 189 | else => {}, |
| 192 | 190 | } |
| 193 | 191 | } |
| 194 | | var page_size: os.vm_size_t = undefined; |
| 195 | | switch (os.getKernError(os._host_page_size(os.mach_host_self(), &page_size))) { |
| 192 | var page_size: c.vm_size_t = undefined; |
| 193 | switch (c.getKernError(c._host_page_size(c.mach_host_self(), &page_size))) { |
| 196 | 194 | .SUCCESS => return page_size, |
| 197 | 195 | else => |err| { |
| 198 | 196 | log.err("_host_page_size kernel call failed with error code: {s}", .{@tagName(err)}); |
| ... | ... | @@ -202,9 +200,9 @@ pub const MachTask = struct { |
| 202 | 200 | } |
| 203 | 201 | }; |
| 204 | 202 | |
| 205 | | pub fn machTaskForPid(pid: os.pid_t) MachError!MachTask { |
| 206 | | var port: os.mach_port_name_t = undefined; |
| 207 | | switch (os.getKernError(os.task_for_pid(os.mach_task_self(), pid, &port))) { |
| 203 | pub fn machTaskForPid(pid: c.pid_t) MachError!MachTask { |
| 204 | var port: c.mach_port_name_t = undefined; |
| 205 | switch (c.getKernError(c.task_for_pid(c.mach_task_self(), pid, &port))) { |
| 208 | 206 | .SUCCESS => {}, |
| 209 | 207 | .FAILURE => return error.PermissionDenied, |
| 210 | 208 | else => |err| { |