aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--subhook.c2
-rw-r--r--subhook.h2
-rw-r--r--subhook_private.h2
-rw-r--r--subhook_unix.c8
-rw-r--r--subhook_windows.c8
-rw-r--r--subhook_x86.c49
-rw-r--r--test/foo_main.c4
-rw-r--r--test/test.c10
8 files changed, 44 insertions, 41 deletions
diff --git a/subhook.c b/subhook.c
index a0433eb..7118884 100644
--- a/subhook.c
+++ b/subhook.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
diff --git a/subhook.h b/subhook.h
index 3fd0dc8..addc7eb 100644
--- a/subhook.h
+++ b/subhook.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
diff --git a/subhook_private.h b/subhook_private.h
index 0458969..37ef7b5 100644
--- a/subhook_private.h
+++ b/subhook_private.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
diff --git a/subhook_unix.c b/subhook_unix.c
index e074f20..1c5260d 100644
--- a/subhook_unix.c
+++ b/subhook_unix.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -33,9 +33,9 @@ void *subhook_unprotect(void *address, size_t size) {
pagesize = sysconf(_SC_PAGESIZE);
address = (void *)((long)address & ~(pagesize - 1));
- if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
+ if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
+ return address;
+ } else {
return NULL;
}
-
- return address;
}
diff --git a/subhook_windows.c b/subhook_windows.c
index f28317c..13a514b 100644
--- a/subhook_windows.c
+++ b/subhook_windows.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -29,9 +29,9 @@
void *subhook_unprotect(void *address, size_t size) {
DWORD old;
- if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &old) == 0) {
+ if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &old) != 0) {
+ return address;
+ } else {
return NULL;
}
-
- return address;
}
diff --git a/subhook_x86.c b/subhook_x86.c
index 7ed9779..0e6d925 100644
--- a/subhook_x86.c
+++ b/subhook_x86.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2015 Zeex
+/* Copyright (c) 2012-2018 Zeex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -39,12 +39,12 @@
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
- #if SUBHOOK_BITS == 32
- typedef __int32 intptr_t;
- typedef unsigned __int32 uintptr_t;
- #else
+ #ifdef SUBHOOK_X86_64
typedef __int64 intptr_t;
typedef unsigned __int64 uintptr_t;
+ #else
+ typedef __int32 intptr_t;
+ typedef unsigned __int32 uintptr_t;
#endif
#else
#include <stdint.h>
@@ -162,7 +162,6 @@ static size_t subhook_disasm(void *src, int32_t *reloc_op_offset) {
size_t i;
size_t len = 0;
size_t operand_size = 4;
- size_t address_size = 4;
uint8_t opcode = 0;
for (i = 0; i < sizeof(prefixes) / sizeof(*prefixes); i++) {
@@ -171,18 +170,18 @@ static size_t subhook_disasm(void *src, int32_t *reloc_op_offset) {
if (prefixes[i] == 0x66) {
operand_size = 2;
}
- if (prefixes[i] == 0x67) {
- address_size = SUBHOOK_BITS / 8 / 2;
- }
}
}
-#if SUBHOOK_BITS == 64
- if (code[len] >= 0x40 && code[len] <= 0x4F) {
- len++; /* it's a REX prefix */
+#ifdef SUBHOOK_X86_64
+ if ((code[len] & 0xF0) == 0x40) {
+ /* This is a REX prefix (40H - 4FH). REX prefixes are valid only in
+ * 64-bit mode.
+ */
+ uint8_t rex = code[len++];
- uint8_t rex = code[len];
if (rex & 8) {
+ /* REX.W changes size of immediate operand to 64 bits. */
operand_size = 8;
}
}
@@ -219,12 +218,12 @@ static size_t subhook_disasm(void *src, int32_t *reloc_op_offset) {
}
if (opcodes[i].flags & MODRM) {
- uint8_t modrm = code[len++]; /* Mod/RM byte is present */
+ uint8_t modrm = code[len++]; /* +1 for Mod/RM byte */
uint8_t mod = modrm >> 6;
uint8_t rm = modrm & 7;
if (mod != 3 && rm == 4) {
- uint8_t sib = code[len++]; /* SIB byte is present*/
+ uint8_t sib = code[len++]; /* +1 for SIB byte */
uint8_t base = sib & 7;
if (base == 5) {
@@ -239,7 +238,7 @@ static size_t subhook_disasm(void *src, int32_t *reloc_op_offset) {
}
}
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
if (reloc_op_offset != NULL && rm == 5) {
*reloc_op_offset = (int32_t)len; /* RIP-relative addressing */
}
@@ -267,12 +266,14 @@ static size_t subhook_disasm(void *src, int32_t *reloc_op_offset) {
}
static size_t subhook_get_jmp_size(subhook_options_t options) {
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
if ((options & SUBHOOK_OPTION_64BIT_OFFSET) != 0) {
return sizeof(struct subhook_jmp64);
}
-#endif
+#else
+ (void)options;
return sizeof(struct subhook_jmp32);
+#endif
}
static int subhook_make_jmp32(void *src, void *dst) {
@@ -296,7 +297,7 @@ static int subhook_make_jmp32(void *src, void *dst) {
return 0;
}
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
static int subhook_make_jmp64(void *src, void *dst) {
struct subhook_jmp64 *jmp = (struct subhook_jmp64 *)src;
@@ -318,12 +319,14 @@ static int subhook_make_jmp64(void *src, void *dst) {
static int subhook_make_jmp(void *src,
void *dst,
subhook_options_t options) {
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
if ((options & SUBHOOK_OPTION_64BIT_OFFSET) != 0) {
return subhook_make_jmp64(src, dst);
}
-#endif
+#else
+ (void)options;
return subhook_make_jmp32(src, dst);
+#endif
}
static int subhook_make_trampoline(void *trampoline,
@@ -481,7 +484,7 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(subhook_t hook) {
SUBHOOK_EXPORT void *SUBHOOK_API subhook_read_dst(void *src) {
struct subhook_jmp32 *maybe_jmp32 = (struct subhook_jmp32 *)src;
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
struct subhook_jmp64 *maybe_jmp64 = (struct subhook_jmp64 *)src;
#endif
@@ -490,7 +493,7 @@ SUBHOOK_EXPORT void *SUBHOOK_API subhook_read_dst(void *src) {
maybe_jmp32->offset + (uintptr_t)src + sizeof(*maybe_jmp32));
}
-#if SUBHOOK_BITS == 64
+#ifdef SUBHOOK_X86_64
if (maybe_jmp64->push_opcode == PUSH_OPCODE
&& maybe_jmp64->mov_opcode == MOV_OPCODE
&& maybe_jmp64->mov_modrm == MOV_MODRM_BYTE
diff --git a/test/foo_main.c b/test/foo_main.c
index 5e0c43a..a26e671 100644
--- a/test/foo_main.c
+++ b/test/foo_main.c
@@ -1,6 +1,6 @@
-extern void foo();
+extern void foo(void);
int main() {
foo();
return 0;
-} \ No newline at end of file
+}
diff --git a/test/test.c b/test/test.c
index 2447e8c..278a4fd 100644
--- a/test/test.c
+++ b/test/test.c
@@ -3,9 +3,9 @@
#include <string.h>
#include <subhook.h>
-typedef void (*foo_func_t)();
+typedef void (*foo_func_t)(void);
-#if SUBHOOK_BITS == 32
+#ifdef SUBHOOK_X86
#if defined SUBHOOK_WINDOWS
#define FOO_CALL __cdecl
#elif defined SUBHOOK_UNIX
@@ -15,14 +15,14 @@ typedef void (*foo_func_t)();
#define FOO_CALL
#endif
-extern void FOO_CALL foo();
+extern void FOO_CALL foo(void);
foo_func_t foo_tr = NULL;
-void foo_hooked() {
+void foo_hooked(void) {
puts("foo_hooked() called");
}
-void foo_hooked_tr() {
+void foo_hooked_tr(void) {
puts("foo_hooked_tr() called");
foo_tr();
}