-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariation.c
175 lines (134 loc) · 3.14 KB
/
variation.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#ifndef USERMESG
#define USERMESG 2048
#endif
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
#ifndef VARIATION
#define VARIATION "./variation.c"
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
/* EXPAND_VAR_ARGS copy from ltp. */
/*
* EXPAND_VAR_ARGS - Expand the variable portion (arg_fmt) of a result
* message into the specified string.
*
* NOTE (garrcoop): arg_fmt _must_ be the last element in each function
* argument list that employs this.
*/
#define EXPAND_VAR_ARGS(buf, arg_fmt, buf_len) do {\
va_list ap; \
assert(arg_fmt != NULL); \
va_start(ap, arg_fmt); \
vsnprintf(buf, buf_len, arg_fmt, ap); \
va_end(ap); \
assert(strlen(buf) > 0); \
} while (0)
int read_fd;
int write_fd;
static int variation_error(void (*func)(void), const char *arg_fmt, ...)
{
char tmesg[USERMESG];
EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
printf("%s\n", tmesg);
if (func != NULL)
(*func)();
exit(-1);
}
static int variation_open(void (cleanup_fn)(void), const char *pathname,
int flags, ...)
{
va_list ap;
int rval;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
rval = open(pathname, flags, mode);
if (rval == -1) {
variation_error(cleanup_fn,
"%s:%d: open(%s,%d,0%o) failed",
__FILE__, __LINE__, pathname, flags, mode);
}
return rval;
}
static int variation_close(void (cleanup_fn)(void), int fd)
{
int rval;
rval = close(fd);
if (rval == -1) {
variation_error(cleanup_fn,
"%s:%d: close(%d) failed",
__FILE__, __LINE__, fd);
}
return rval;
}
static int variation_read(void (cleanup_fn)(void), int fd, char *buf,
size_t nbyte)
{
int rval = 0;
rval = read(fd, buf, nbyte);
if (rval < 0) {
variation_error(cleanup_fn,
"%s:%d: read(%d,%p,%zu) failed",
__FILE__, __LINE__, fd, buf, nbyte);
}
return rval;
}
static int variation_write(void (cleanup_fn)(void), int fd, char *buf,
size_t nbyte)
{
int rval = 0;
rval = write(fd, buf, nbyte);
if (rval < 0) {
variation_error(cleanup_fn,
"%s:%d: write(%d,%p,%zu) failed",
__FILE__, __LINE__, fd, buf, nbyte);
}
return rval;
}
static int variation_compile(void (cleanup_fn)(void), const char *filename)
{
char command[2*PATH_MAX];
sprintf(command, "gcc -o %s %s.c 2> /dev/null", filename, filename);
return system(command);
}
static void cleanup(void)
{
if (read_fd > 0)
close(read_fd);
if (write_fd > 0)
close(write_fd);
}
int main(void)
{
int len;
int index;
char buf[PAGE_SIZE];
read_fd = variation_open(cleanup, VARIATION, O_RDONLY);
len = variation_read(cleanup, read_fd, buf, PAGE_SIZE);
srandom((unsigned int)time(NULL));
index = random() % len;
while (1) {
buf[index] = random() % 0xff;
write_fd = variation_open(cleanup, "./tmp.c", O_WRONLY | O_CREAT, 0644);
variation_write(cleanup, write_fd, buf, len);
variation_close(cleanup, write_fd);
if (variation_compile(cleanup, "tmp") == 0)
break;
}
cleanup();
return 0;
}