Skip to content

Commit 619d313

Browse files
authored
Implement vm_protect sys call (#3)
1 parent f582a06 commit 619d313

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

Sources/SimpleDebugger/SimpleDebugger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <mach-o/dyld_images.h>
1818

1919
#include "mach_messages.h"
20+
#include "emg_vm_protect.h"
2021

2122
#include <mach/exception.h>
2223
#include <mach/arm/thread_state.h>
@@ -67,7 +68,7 @@ void SimpleDebugger::setExceptionCallback(ExceptionCallback callback) {
6768
#define ARM64_BREAK_INSTRUCTION 0xD4200000
6869

6970
void protectPage(vm_address_t address, vm_size_t size, vm_prot_t newProtection) {
70-
kern_return_t result = vm_protect(mach_task_self(), address, size, 0, newProtection);
71+
kern_return_t result = emg_vm_protect(mach_task_self(), address, size, 0, newProtection);
7172

7273
if (result != 0) {
7374
printf("error calling vm_protect: %s (response value: %d)\n", mach_error_string(result), result);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// emg_vm_protect.c
3+
// SimpleDebugger
4+
//
5+
// Created by Noah Martin on 12/10/24.
6+
//
7+
8+
#import "emg_vm_protect.h"
9+
#import <mach/mach.h>
10+
11+
extern kern_return_t _kern_rpc_emg_vm_prot_trap(
12+
mach_port_name_t target,
13+
mach_vm_address_t address,
14+
mach_vm_size_t size,
15+
boolean_t set_maximum,
16+
vm_prot_t new_protection
17+
);
18+
19+
asm(
20+
".globl __kern_rpc_emg_vm_prot_trap\n"
21+
".text\n"
22+
".align 2\n"
23+
"__kern_rpc_emg_vm_prot_trap:\n"
24+
" mov x16, #-14\n"
25+
" svc 0x80\n"
26+
" ret\n"
27+
);
28+
29+
kern_return_t emg_vm_protect(mach_port_t target, mach_vm_address_t address, mach_vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection) {
30+
return _kern_rpc_emg_vm_prot_trap(target, address, size, set_maximum, new_protection);
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// emg_vm_protect.h
3+
// SimpleDebugger
4+
//
5+
// Created by Noah Martin on 12/10/24.
6+
//
7+
8+
#import <mach/mach.h>
9+
10+
#ifndef EMG_VM_PROTECT
11+
#define EMG_VM_PROTECT
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
kern_return_t emg_vm_protect(mach_port_t target, mach_vm_address_t address, mach_vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection);
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif
22+
23+
#endif

0 commit comments

Comments
 (0)