@@ -39,35 +39,39 @@ static inline void copy_mem(void *destination, const void *source, size_t len) {
3939}
4040
4141int read_mem (void * destination , const void * source , size_t len ) {
42+ ARG_CHECK (destination != NULL );
43+ ARG_CHECK (source != NULL );
44+ ARG_CHECK (len != 0 );
4245 int kr = 0 ;
4346 vm_offset_t data ;
4447 mach_msg_type_number_t dataCnt ;
45- kr = mach_vm_read (mach_task_self (), (mach_vm_address_t )source , len , & data , & dataCnt );
48+ mach_port_t task = mach_task_self ();
49+ kr = mach_vm_read (task , (mach_vm_address_t )source , len , & data , & dataCnt );
4650 if (kr != 0 ) {
47- LOG_ERROR ("mach_vm_read: %s" , mach_error_string (kr ));
51+ LOG_ERROR ("mach_vm_read failed for address %p : %s" , source , mach_error_string (kr ));
4852 return kr ;
4953 }
50- memcpy (( void * ) destination , (void * )data , dataCnt );
51- kr = mach_vm_deallocate (mach_task_self () , data , dataCnt );
54+ memcpy (destination , (void * )data , dataCnt );
55+ kr = mach_vm_deallocate (task , data , dataCnt );
5256 if (kr != 0 ) {
5357 LOG_ERROR ("mach_vm_deallocate: %s" , mach_error_string (kr ));
5458 }
5559 return kr ;
5660}
5761
5862int write_mem (void * destination , const void * source , size_t len ) {
59- int kr = 0 ;
60- kr = mach_vm_protect_trap (mach_task_self (), (mach_vm_address_t )destination , len , FALSE,
61- VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY );
63+ ARG_CHECK (destination != NULL );
64+ ARG_CHECK (source != NULL );
65+ ARG_CHECK (len != 0 );
66+ mach_port_t task = mach_task_self ();
67+ mach_vm_address_t dst = (mach_vm_address_t )destination ;
68+ int kr = mach_vm_protect_trap (task , dst , len , FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY );
6269 if (kr != 0 ) {
63- LOG_ERROR ("mach_vm_protect: %s" , mach_error_string (kr ));
70+ LOG_ERROR ("mach_vm_protect failed for address %p : %s" , destination , mach_error_string (kr ));
6471 return kr ;
6572 }
6673 copy_mem (destination , source , len );
67- kr = mach_vm_protect_trap (mach_task_self (), (mach_vm_address_t )destination , len , FALSE,
68- VM_PROT_READ | VM_PROT_EXECUTE );
69- if (kr != 0 ) {
70- LOG_ERROR ("mach_vm_protect: %s" , mach_error_string (kr ));
71- }
72- return kr ;
74+ mach_vm_protect_trap (task , dst , len , FALSE, VM_PROT_READ | VM_PROT_EXECUTE );
75+ // might fail when editing __DATA, but not a big deal
76+ return 0 ;
7377}
0 commit comments