source: zzuf/trunk/src/libzzuf/lib-win32.c @ 4813

Last change on this file since 4813 was 4813, checked in by Sam Hocevar, 8 years ago

win32: add debug information to the function diversion code.

File size: 4.6 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  load-win32.c: loaded Win32 functions
15 */
16
17#include "config.h"
18
19#if defined HAVE_STDINT_H
20#   include <stdint.h>
21#elif defined HAVE_INTTYPES_H
22#   include <inttypes.h>
23#endif
24
25#include <stdio.h>
26
27#if defined HAVE_WINDOWS_H
28#   include <windows.h>
29#endif
30#if defined HAVE_IO_H
31#   include <io.h>
32#endif
33
34#include "common.h"
35#include "libzzuf.h"
36#include "lib-load.h"
37#include "debug.h"
38#include "fuzz.h"
39#include "fd.h"
40
41/* Kernel functions that we divert */
42#if defined HAVE_CREATEFILEA
43static HANDLE (__stdcall *ORIG(CreateFileA))(LPCTSTR, DWORD, DWORD,
44                                             LPSECURITY_ATTRIBUTES,
45                                             DWORD, DWORD, HANDLE);
46#endif
47#if defined HAVE_CREATEFILEW
48static HANDLE (__stdcall *ORIG(CreateFileW))(LPCWSTR, DWORD, DWORD,
49                                             LPSECURITY_ATTRIBUTES,
50                                             DWORD, DWORD, HANDLE);
51#endif
52#if defined HAVE_REOPENFILE
53static HANDLE (__stdcall *ORIG(ReOpenFile))(HANDLE, DWORD,
54                                            DWORD, DWORD);
55#endif
56#if defined HAVE_READFILE
57static BOOL (__stdcall *ORIG(ReadFile))(HANDLE, LPVOID, DWORD, LPDWORD,
58                                        LPOVERLAPPED);
59#endif
60#if defined HAVE_CLOSEHANDLE
61static BOOL (__stdcall *ORIG(CloseHandle))(HANDLE);
62#endif
63
64/*
65 * CreateFileA, CreateFileW
66 */
67
68#if defined HAVE_CREATEFILEA
69HANDLE __stdcall NEW(CreateFileA)(LPCTSTR lpFileName, DWORD dwDesiredAccess,
70           DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
71           DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
72           HANDLE hTemplateFile)
73{
74    HANDLE ret;
75    ret = ORIG(CreateFileA)(lpFileName, dwDesiredAccess, dwShareMode,
76                            lpSecurityAttributes, dwCreationDisposition,
77                            dwFlagsAndAttributes, hTemplateFile);
78    debug("CreateFileA(\"%s\", 0x%x, 0x%x, {...}, 0x%x, 0x%x, {...}) = %i",
79          lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition,
80          dwFlagsAndAttributes, (int)ret);
81    return ret;
82}
83#endif
84
85#if defined HAVE_CREATEFILEW
86HANDLE __stdcall NEW(CreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess,
87           DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
88           DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
89           HANDLE hTemplateFile)
90{
91    HANDLE ret;
92    ret = ORIG(CreateFileW)(lpFileName, dwDesiredAccess, dwShareMode,
93                            lpSecurityAttributes, dwCreationDisposition,
94                            dwFlagsAndAttributes, hTemplateFile);
95    debug("CreateFileW(\"%S\", 0x%x, 0x%x, {...}, 0x%x, 0x%x, {...}) = %i",
96          lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition,
97          dwFlagsAndAttributes, (int)ret);
98    return ret;
99}
100#endif
101
102#if defined HAVE_REOPENFILE
103HANDLE __stdcall NEW(ReOpenFile)(HANDLE hOriginalFile, DWORD dwDesiredAccess,
104                                 DWORD dwShareMode, DWORD dwFlags)
105{
106    HANDLE ret;
107    ret = ORIG(ReOpenFile)(hOriginalFile, dwDesiredAccess,
108                           dwShareMode, dwFlags);
109    debug("ReOpenFile(%i, 0x%x, 0x%x, 0x%x) = %i", (int)hOriginalFile,
110          dwDesiredAccess, dwShareMode, dwFlags, (int)ret);
111    return ret;
112}
113#endif
114
115/*
116 * ReadFile
117 */
118
119#if defined HAVE_READFILE
120BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer,
121           DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
122           LPOVERLAPPED lpOverlapped)
123{
124    return ORIG(ReadFile)(hFile, lpBuffer, nNumberOfBytesToRead,
125                          lpNumberOfBytesRead, lpOverlapped);
126}
127#endif
128
129/*
130 * CloseHandle
131 */
132
133#if defined HAVE_CLOSEHANDLE
134BOOL __stdcall NEW(CloseHandle)(HANDLE hObject)
135{
136    BOOL ret;
137    ret = ORIG(CloseHandle)(hObject);
138    debug("CloseHandle(%i) = %i", (int)hObject, ret);
139    return ret;
140}
141#endif
142
143/* Win32 function table */
144#if defined HAVE_WINDOWS_H
145#   define DIVERT(x) { "kernel32.dll", #x, \
146                      (void **)&x##_orig, (void *)x##_new }
147#   define DIVERT_END { NULL, NULL, NULL, NULL }
148
149zzuf_table_t table_win32[] =
150{
151    DIVERT(CloseHandle),
152    DIVERT(CreateFileA),
153    DIVERT(CreateFileW),
154    DIVERT(ReadFile),
155    DIVERT_END
156};
157#endif
158
Note: See TracBrowser for help on using the repository browser.