-
Notifications
You must be signed in to change notification settings - Fork 534
/
winaflpt-debug.c
137 lines (103 loc) · 2.47 KB
/
winaflpt-debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdbool.h>
#include <direct.h>
#include "windows.h"
#include "types.h"
#include "config.h"
#include "debug.h"
#include "alloc-inl.h"
#include "winaflpt.h"
u8 *trace_bits;
u8 sinkhole_stds = 0;
u64 mem_limit = 0;
u64 cpu_aff = 0;
// todo the below functions are copied from afl-fuzz.c
// they should be taken out to a separate file to avoid duplication
u64 get_cur_time(void) {
u64 ret;
FILETIME filetime;
GetSystemTimeAsFileTime(&filetime);
ret = (((u64)filetime.dwHighDateTime) << 32) + (u64)filetime.dwLowDateTime;
return ret / 10000;
}
//quoting on Windows is weird
size_t ArgvQuote(char *in, char *out) {
int needs_quoting = 0;
size_t size = 0;
char *p = in;
size_t i;
//check if quoting is necessary
if (strchr(in, ' ')) needs_quoting = 1;
if (strchr(in, '\"')) needs_quoting = 1;
if (strchr(in, '\t')) needs_quoting = 1;
if (strchr(in, '\n')) needs_quoting = 1;
if (strchr(in, '\v')) needs_quoting = 1;
if (!needs_quoting) {
size = strlen(in);
if (out) memcpy(out, in, size);
return size;
}
if (out) out[size] = '\"';
size++;
while (*p) {
size_t num_backslashes = 0;
while ((*p) && (*p == '\\')) {
p++;
num_backslashes++;
}
if (*p == 0) {
for (i = 0; i < (num_backslashes * 2); i++) {
if (out) out[size] = '\\';
size++;
}
break;
}
else if (*p == '\"') {
for (i = 0; i < (num_backslashes * 2 + 1); i++) {
if (out) out[size] = '\\';
size++;
}
if (out) out[size] = *p;
size++;
}
else {
for (i = 0; i < num_backslashes; i++) {
if (out) out[size] = '\\';
size++;
}
if (out) out[size] = *p;
size++;
}
p++;
}
if (out) out[size] = '\"';
size++;
return size;
}
char *argv_to_cmd(char** argv) {
u32 len = 0, i;
u8* buf, *ret;
//todo shell-escape
for (i = 0; argv[i]; i++)
len += ArgvQuote(argv[i], NULL) + 1;
if (!len) FATAL("Error creating command line");
buf = ret = ck_alloc(len);
for (i = 0; argv[i]; i++) {
u32 l = ArgvQuote(argv[i], buf);
buf += l;
*(buf++) = ' ';
}
ret[len - 1] = 0;
return ret;
}
int main(int argc, char **argv)
{
_mkdir(".\\ptmodules");
int target_opt_ind = pt_init(argc, argv, ".\\ptmodules");
if (!target_opt_ind) {
printf("Usage: %s <instrumentation-options> -- <target command line>\n", argv[0]);
return 0;
}
debug_target_pt(argv + target_opt_ind + 1);
return 0;
}