Skip to content

Commit b2b5c36

Browse files
committed
Add VC2015 project files and fix to run tests on Windows.
1 parent c473518 commit b2b5c36

17 files changed

+712
-22
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
/src/Makefile
1212
/src/config.h
1313
/src/config.h.in
14+
/win32/.vs
15+
/win32/Debug
16+
/win32/Release
17+
/win32/x64

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ AC_SUBST(DUCKHOOK_CPU)
1919
if test "$EXEEXT"; then
2020
DUCKHOOK_OS=windows
2121
LIBDUCKHOOK_SO=duckhook.dll
22-
CFLAGS="$CFLAGS -DBUILD_DUCKHOOK_DLL"
22+
CFLAGS="$CFLAGS -DDUCKHOOK_EXPORTS"
2323
LINK_SHARED="\$(CC) -shared -Wl,--out-implib,duckhook.lib"
2424
IF_WIN32=
2525
else

include/duckhook.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
* or libduckhook.so. Others are invisible.
3636
*/
3737
#ifdef WIN32
38-
#ifdef BUILD_DUCKHOOK_DLL
38+
#ifdef DUCKHOOK_EXPORTS
3939
#define DUCKHOOK_EXPORT __declspec(dllexport)
40-
#else /* BUILD_DUCKHOOK_DLL */
40+
#else /* DUCKHOOK_EXPORTS */
4141
#define DUCKHOOK_EXPORT __declspec(dllimport)
42-
#endif /* BUILD_DUCKHOOK_DLL */
42+
#endif /* DUCKHOOK_EXPORTS */
4343
#elif defined(__GNUC__)
4444
#define DUCKHOOK_EXPORT __attribute__((visibility("default")))
4545
#else

src/duckhook.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ static int duckhook_prepare_internal(duckhook_t *duckhook, void **target_func, v
300300
#endif
301301
/* fix rip-relative offsets */
302302
src_addr = entry->trampoline + disp[0].src_addr_offset;
303-
*(uint32_t*)(entry->trampoline + disp[0].pos_offset) = (disp[0].dst_addr - src_addr);
303+
*(uint32_t*)(entry->trampoline + disp[0].pos_offset) = (uint32_t)(disp[0].dst_addr - src_addr);
304304
if (disp[1].dst_addr != 0) {
305305
src_addr = entry->trampoline + disp[1].src_addr_offset;
306-
*(uint32_t*)(entry->trampoline + disp[1].pos_offset) = (disp[1].dst_addr - src_addr);
306+
*(uint32_t*)(entry->trampoline + disp[1].pos_offset) = (uint32_t)(disp[1].dst_addr - src_addr);
307307
}
308308
duckhook_log_trampoline(duckhook, entry->trampoline);
309309

src/duckhook_internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ typedef struct {
9393

9494
typedef struct {
9595
const uint8_t *dst_addr;
96-
char src_addr_offset;
97-
char pos_offset;
96+
intptr_t src_addr_offset;
97+
intptr_t pos_offset;
9898
} rip_displacement_t;
9999

100100
typedef struct duckhook_page duckhook_page_t;

src/duckhook_windows.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int alloc_page_info(duckhook_t *duckhook, page_list_t **pl_out, void *hin
8181
(mbi.State == MEM_FREE) ? "free" : "used");
8282
if (mbi.State == MEM_FREE) {
8383
size_t addr = ROUND_UP((size_t)mbi.BaseAddress, allocation_unit);
84-
int diff = addr - (size_t)mbi.BaseAddress;
84+
intptr_t diff = addr - (size_t)mbi.BaseAddress;
8585
if (diff >= 0) {
8686
if (mbi.RegionSize - diff >= allocation_unit) {
8787
hint = (void*)addr;
@@ -125,8 +125,8 @@ static int alloc_page_info(duckhook_t *duckhook, page_list_t **pl_out, void *hin
125125
int duckhook_page_alloc(duckhook_t *duckhook, duckhook_page_t **page_out, uint8_t *func, rip_displacement_t *disp)
126126
{
127127
page_list_t *pl;
128-
duckhook_page_t *page;
129-
int i;
128+
duckhook_page_t *page = NULL;
129+
size_t i;
130130

131131
for (pl = page_list.next; pl != &page_list; pl = pl->next) {
132132
for (i = 0; i < max_num_pages; i++) {
@@ -138,7 +138,7 @@ int duckhook_page_alloc(duckhook_t *duckhook, duckhook_page_t **page_out, uint8_
138138
}
139139
}
140140
}
141-
if (pl == &page_list) {
141+
if (page == NULL) {
142142
/* no page_list is available. */
143143
int rv = alloc_page_info(duckhook, &pl, func);
144144
if (rv != 0) {
@@ -147,7 +147,7 @@ int duckhook_page_alloc(duckhook_t *duckhook, duckhook_page_t **page_out, uint8_
147147
i = 0;
148148
page = (duckhook_page_t *)((size_t)pl + page_size);
149149
}
150-
if (VirtualAlloc(page, page_size, MEM_COMMIT, PAGE_READWRITE) == NULL) {
150+
if (VirtualAlloc(page, page_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE) == NULL) {
151151
duckhook_set_error_message(duckhook, "Failed to commit page %p (base=%p(used=%d), idx=%d, size=%"SIZE_T_FMT"u, error=%lu)",
152152
page, pl, pl->num_used, i, page_size, GetLastError());
153153
return DUCKHOOK_ERROR_INTERNAL_ERROR;
@@ -236,7 +236,7 @@ int duckhook_unprotect_end(duckhook_t *duckhook, const mem_state_t *mstate)
236236
void *duckhook_resolve_func(duckhook_t *duckhook, void *func)
237237
{
238238
if (duckhook_debug_file != NULL) {
239-
char path[PATH_MAX];
239+
char path[MAX_PATH];
240240
DWORD len = GetMappedFileNameA(GetCurrentProcess(), func, path, sizeof(path));
241241
if (len > 0) {
242242
duckhook_log(duckhook, " func %p is in %.*s\n", func, (int)len, path);

src/duckhook_x86.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int duckhook_make_trampoline(duckhook_t *duckhook, rip_displacement_t *disp, con
116116
_CodeInfo ci;
117117
_DecodeResult decres;
118118
int rv;
119-
int i;
119+
unsigned int i;
120120

121121
memset(trampoline, NOP_INSTRUCTION, TRAMPOLINE_SIZE);
122122
ctx.duckhook = duckhook;
@@ -200,7 +200,7 @@ void duckhook_log_trampoline(duckhook_t *duckhook, const uint8_t *trampoline)
200200
unsigned int di_cnt = 0;
201201
_CodeInfo ci;
202202
_DecodeResult decres;
203-
int i;
203+
unsigned int i;
204204

205205
if (duckhook_debug_file == NULL) {
206206
return;
@@ -390,15 +390,15 @@ static void get_rip_relative(const make_trampoline_context_t *ctx, rip_relative_
390390
break;
391391
case O_PC:
392392
rel_imm->addr = (uint8_t*)(size_t)(di->addr + di->size + di->imm.addr);
393-
rel_imm->raddr = di->imm.addr;
393+
rel_imm->raddr = (intptr_t)di->imm.addr;
394394
rel_imm->size = op->size;
395395
imm_offset = opsiz;
396396
opsiz += op->size / 8;
397397
break;
398398
case O_SMEM:
399399
if (di->dispSize != 0 && op->index == R_RIP) {
400400
rel_disp->addr = (uint8_t*)(size_t)(di->addr + di->size + di->disp);
401-
rel_disp->raddr = di->disp;
401+
rel_disp->raddr = (intptr_t)di->disp;
402402
rel_disp->size = di->dispSize;
403403
disp_offset = opsiz;
404404
}

test/libduckhook_test.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
#ifdef WIN32
2+
#define DLLEXPORT __declspec(dllexport)
3+
#else
4+
#define DLLEXPORT
5+
#endif
6+
17
#if defined(WIN32) || defined(__APPLE__)
28
static int int_val;
3-
void set_int_val(int val)
9+
10+
DLLEXPORT void set_int_val(int val)
411
{
512
int_val = val;
613
}
714
#else
815
extern int int_val;
916
#endif
1017

11-
int get_val_in_shared_library()
18+
DLLEXPORT int get_val_in_shared_library()
1219
{
1320
return int_val;
1421
}

test/test_main.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
typedef int (*int_func_t)(void);
77

8-
extern void reset_retval(void);
8+
extern int reset_retval(void);
99
extern int get_val_in_shared_library(void);
1010
extern int x86_test_jump(void);
1111
extern int x86_test_call_get_pc_thunk_ax(void);
@@ -31,6 +31,13 @@ extern void set_int_val(int val);
3131
#define set_int_val(val) do {} while(0)
3232
#endif
3333

34+
#ifdef _MSC_VER
35+
int reset_retval()
36+
{
37+
return 0;
38+
}
39+
#endif
40+
3441
#if defined(WIN32)
3542
__declspec(dllexport) int int_val = 0xbaceba11;
3643
#else
@@ -43,7 +50,7 @@ static int error_cnt;
4350
static int hook_is_called;
4451
static int_func_t orig_func;
4552

46-
int get_val()
53+
int get_val(void)
4754
{
4855
return int_val;
4956
}
@@ -140,6 +147,7 @@ int main()
140147
TEST_DUCKHOOK_INT(get_val);
141148
TEST_DUCKHOOK_INT(get_val_in_shared_library);
142149

150+
#ifndef _MSC_VER
143151
#if defined __i386 || defined _M_I386
144152
TEST_DUCKHOOK_INT(x86_test_jump);
145153
TEST_DUCKHOOK_EXPECT_ERROR(x86_test_error_jump1, DUCKHOOK_ERROR_CANNOT_FIX_IP_RELATIVE);
@@ -161,6 +169,8 @@ int main()
161169
TEST_DUCKHOOK_INT(x86_test_call_and_pop_edi);
162170
TEST_DUCKHOOK_INT(x86_test_call_and_pop_ebp);
163171
#endif
172+
#endif
173+
164174
#endif
165175

166176
if (error_cnt == 0) {

win32/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* dummy */

win32/duckhook.sln

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duckhook", "duckhook.vcxproj", "{6E012034-BE65-43DB-BC03-9B6286723129}"
7+
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duckhook_test_dll", "duckhook_test_dll.vcxproj", "{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}"
9+
EndProject
10+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "duckhook_test", "duckhook_test.vcxproj", "{5FF07D47-19F0-4846-928D-ACE9041BAA10}"
11+
ProjectSection(ProjectDependencies) = postProject
12+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E} = {9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}
13+
{6E012034-BE65-43DB-BC03-9B6286723129} = {6E012034-BE65-43DB-BC03-9B6286723129}
14+
EndProjectSection
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|x64 = Debug|x64
19+
Debug|x86 = Debug|x86
20+
Release|x64 = Release|x64
21+
Release|x86 = Release|x86
22+
EndGlobalSection
23+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
24+
{6E012034-BE65-43DB-BC03-9B6286723129}.Debug|x64.ActiveCfg = Debug|x64
25+
{6E012034-BE65-43DB-BC03-9B6286723129}.Debug|x64.Build.0 = Debug|x64
26+
{6E012034-BE65-43DB-BC03-9B6286723129}.Debug|x86.ActiveCfg = Debug|Win32
27+
{6E012034-BE65-43DB-BC03-9B6286723129}.Debug|x86.Build.0 = Debug|Win32
28+
{6E012034-BE65-43DB-BC03-9B6286723129}.Release|x64.ActiveCfg = Release|x64
29+
{6E012034-BE65-43DB-BC03-9B6286723129}.Release|x64.Build.0 = Release|x64
30+
{6E012034-BE65-43DB-BC03-9B6286723129}.Release|x86.ActiveCfg = Release|Win32
31+
{6E012034-BE65-43DB-BC03-9B6286723129}.Release|x86.Build.0 = Release|Win32
32+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Debug|x64.ActiveCfg = Debug|x64
33+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Debug|x64.Build.0 = Debug|x64
34+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Debug|x86.ActiveCfg = Debug|Win32
35+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Debug|x86.Build.0 = Debug|Win32
36+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Release|x64.ActiveCfg = Release|x64
37+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Release|x64.Build.0 = Release|x64
38+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Release|x86.ActiveCfg = Release|Win32
39+
{9ABF8228-945D-44CF-BE1E-95C3C5A8CE9E}.Release|x86.Build.0 = Release|Win32
40+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Debug|x64.ActiveCfg = Debug|x64
41+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Debug|x64.Build.0 = Debug|x64
42+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Debug|x86.ActiveCfg = Debug|Win32
43+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Debug|x86.Build.0 = Debug|Win32
44+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Release|x64.ActiveCfg = Release|x64
45+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Release|x64.Build.0 = Release|x64
46+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Release|x86.ActiveCfg = Release|Win32
47+
{5FF07D47-19F0-4846-928D-ACE9041BAA10}.Release|x86.Build.0 = Release|Win32
48+
EndGlobalSection
49+
GlobalSection(SolutionProperties) = preSolution
50+
HideSolutionNode = FALSE
51+
EndGlobalSection
52+
EndGlobal

0 commit comments

Comments
 (0)