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

Last change on this file since 4827 was 4827, checked in by wisk, 8 years ago

on win32, use a named pipe and IOCP to read stdout, stderr and debugfd correctly.

File size: 4.8 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))(LPCSTR, 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)(LPCSTR lpFileName, DWORD dwDesiredAccess,
70           DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
71           DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
72           HANDLE hTemplateFile)
73{
74    HANDLE ret;
75
76    ret = ORIG(CreateFileA)(lpFileName, dwDesiredAccess, dwShareMode,
77                            lpSecurityAttributes, dwCreationDisposition,
78                            dwFlagsAndAttributes, hTemplateFile);
79    debug("CreateFileA(\"%s\", 0x%x, 0x%x, {...}, 0x%x, 0x%x, {...}) = %i",
80          lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition,
81          dwFlagsAndAttributes, (int)ret);
82    return ret;
83}
84#endif
85
86#if defined HAVE_CREATEFILEW
87HANDLE __stdcall NEW(CreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess,
88           DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
89           DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
90           HANDLE hTemplateFile)
91{
92    HANDLE ret;
93    ret = ORIG(CreateFileW)(lpFileName, dwDesiredAccess, dwShareMode,
94                            lpSecurityAttributes, dwCreationDisposition,
95                            dwFlagsAndAttributes, hTemplateFile);
96    debug("CreateFileW(\"%S\", 0x%x, 0x%x, {...}, 0x%x, 0x%x, {...}) = %i",
97          lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition,
98          dwFlagsAndAttributes, (int)ret);
99    return ret;
100}
101#endif
102
103#if defined HAVE_REOPENFILE
104HANDLE __stdcall NEW(ReOpenFile)(HANDLE hOriginalFile, DWORD dwDesiredAccess,
105                                 DWORD dwShareMode, DWORD dwFlags)
106{
107    HANDLE ret;
108    ret = ORIG(ReOpenFile)(hOriginalFile, dwDesiredAccess,
109                           dwShareMode, dwFlags);
110    debug("ReOpenFile(%i, 0x%x, 0x%x, 0x%x) = %i", (int)hOriginalFile,
111          dwDesiredAccess, dwShareMode, dwFlags, (int)ret);
112    return ret;
113}
114#endif
115
116/*
117 * ReadFile
118 */
119
120#if defined HAVE_READFILE
121BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer,
122           DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
123           LPOVERLAPPED lpOverlapped)
124{
125    BOOL ret;
126    ret = ORIG(ReadFile)(hFile, lpBuffer, nNumberOfBytesToRead,
127                          lpNumberOfBytesRead, lpOverlapped);
128    debug("ReadFile(%#08x, %#08x, %#08x, %#08x, %#08x) = %s",
129        hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, (ret ? "TRUE" : "FALSE"));
130    return ret;
131}
132#endif
133
134/*
135 * CloseHandle
136 */
137
138#if defined HAVE_CLOSEHANDLE
139BOOL __stdcall NEW(CloseHandle)(HANDLE hObject)
140{
141    BOOL ret;
142    ret = ORIG(CloseHandle)(hObject);
143    debug("CloseHandle(%i) = %s", (int)hObject, (ret ? "TRUE" : "FALSE"));
144    return ret;
145}
146#endif
147
148/* Win32 function table */
149#if defined HAVE_WINDOWS_H
150#   define DIVERT(x) { "kernel32.dll", #x, \
151                      (void **)&x##_orig, (void *)x##_new }
152#   define DIVERT_END { NULL, NULL, NULL, NULL }
153
154zzuf_table_t table_win32[] =
155{
156    DIVERT(CloseHandle),
157    DIVERT(CreateFileA),
158    DIVERT(CreateFileW),
159    DIVERT(ReadFile),
160    DIVERT_END
161};
162#endif
163
Note: See TracBrowser for help on using the repository browser.