aboutsummaryrefslogtreecommitdiff
path: root/subhook_unix.c
diff options
context:
space:
mode:
authorDudi <dudicon@gmail.com>2021-11-20 22:12:51 +0200
committerDudi <dudicon@gmail.com>2021-11-20 22:12:51 +0200
commitba0872c0b24d9739d8f973f7e66683c1fc7dced5 (patch)
tree72bd9819a7cb55694bf71bdcac862ba46cae6a61 /subhook_unix.c
parentc5aa6ac920f0bfeacb9b3d685ec5d4117e3517f1 (diff)
downloadsubhook-ba0872c0b24d9739d8f973f7e66683c1fc7dced5.zip
subhook-ba0872c0b24d9739d8f973f7e66683c1fc7dced5.tar.gz
subhook-ba0872c0b24d9739d8f973f7e66683c1fc7dced5.tar.bz2
Removing subhook_macos.c
Diffstat (limited to 'subhook_unix.c')
-rw-r--r--subhook_unix.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/subhook_unix.c b/subhook_unix.c
index 6a5410b..4d9c03a 100644
--- a/subhook_unix.c
+++ b/subhook_unix.c
@@ -27,6 +27,11 @@
#include <stddef.h>
#include <unistd.h>
#include <sys/mman.h>
+#include "subhook.h"
+#ifdef SUBHOOK_APPLE
+#include <mach/mach.h>
+#endif
+
#define SUBHOOK_CODE_PROTECT_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC)
@@ -36,7 +41,20 @@ int subhook_unprotect(void *address, size_t size) {
pagesize = sysconf(_SC_PAGESIZE);
address = (void *)((long)address & ~(pagesize - 1));
- return mprotect(address, size, SUBHOOK_CODE_PROTECT_FLAGS);
+ int error = mprotect(address, size, SUBHOOK_CODE_PROTECT_FLAGS);
+#ifdef SUBHOOK_APPLE
+ if (-1 == error)
+ {
+ /* If mprotect fails, try to use VM_PROT_COPY with vm_protect. */
+ kern_return_t kret = vm_protect(mach_task_self(), (unsigned long)address, size, 0, SUBHOOK_CODE_PROTECT_FLAGS | VM_PROT_COPY);
+ if (kret != KERN_SUCCESS)
+ {
+ error = -1;
+ }
+ error = 0;
+ }
+#endif
+ return error;
}
void *subhook_alloc_code(size_t size) {