21#if defined(__FreeBSD__) || defined(__minix) || defined(__APPLE__)
28 unsigned char *bytes) {
32 o->
name = strdup(name);
36 memcpy(o->
bytes, bytes, total_bytes);
43 o->
name = strdup(name);
47 *(
unsigned *)o->
bytes = value;
52 "%s: Tool for generating a ktest file from concrete input, e.g., for using a concrete crashing input as a ktest seed.\n"
53 "Usage: %s <arguments>\n"
54 " <arguments> are the command-line arguments of the program, with the following treated as special:\n"
55 " --bout-file <filename> - Specifying the output file name for the ktest file (default: file.bout).\n"
56 " --sym-stdin <filename> - Specifying a file that is the content of stdin (only once).\n"
57 " --sym-stdout <filename> - Specifying a file that is the content of stdout (only once).\n"
58 " --sym-file <filename> - Specifying a file that is the content of a file named A provided for the program (only once).\n"
59 " Ex: %s -o -p -q file1 --sym-stdin file2 --sym-file file3 --sym-stdout file4\n",
60 program_name, program_name, program_name);
64int main(
int argc,
char *argv[]) {
65 unsigned i, argv_copy_idx;
66 unsigned file_counter = 0;
67 char *stdout_content_filename = NULL;
68 char *stdin_content_filename = NULL;
69 char *content_filenames_list[1024];
71 char *bout_file = NULL;
83 if ((argv_copy = (
char **)malloc(
sizeof(
char *) * argc * 2)) == NULL) {
84 fprintf(stderr,
"Could not allocate more memory\n");
88 argv_copy[0] = (
char *)malloc(strlen(argv[0]) + 1);
89 strcpy(argv_copy[0], argv[0]);
92 for (i = 1; i < (unsigned)argc; i++) {
93 if (strcmp(argv[i],
"--sym-stdout") == 0 ||
94 strcmp(argv[i],
"-sym-stdout") == 0) {
95 if (++i == (
unsigned)argc || argv[i][0] ==
'-')
98 if (stdout_content_filename)
101 stdout_content_filename = argv[i];
103 }
else if (strcmp(argv[i],
"--sym-stdin") == 0 ||
104 strcmp(argv[i],
"-sym-stdin") == 0) {
105 if (++i == (
unsigned)argc || argv[i][0] ==
'-')
108 if (stdin_content_filename)
111 stdin_content_filename = argv[i];
112 }
else if (strcmp(argv[i],
"--sym-file") == 0 ||
113 strcmp(argv[i],
"-sym-file") == 0) {
114 if (++i == (
unsigned)argc || argv[i][0] ==
'-')
117 content_filenames_list[file_counter++] = argv[i];
118 }
else if (strcmp(argv[i],
"--bout-file") == 0 ||
119 strcmp(argv[i],
"-bout-file") == 0) {
120 if (++i == (
unsigned)argc)
125 long nbytes = strlen(argv[i]) + 1;
126 static int total_args = 0;
129 snprintf(arg,
sizeof(arg),
"arg%02d", total_args++);
130 push_obj(&b, (
const char *)arg, nbytes, (
unsigned char *)argv[i]);
132 char *buf1 = (
char *)malloc(1024);
133 char *buf2 = (
char *)malloc(1024);
134 strcpy(buf1,
"-sym-arg");
135 snprintf(buf2, 1024,
"%ld", nbytes - 1);
136 argv_copy[argv_copy_idx++] = buf1;
137 argv_copy[argv_copy_idx++] = buf2;
141 if (file_counter > 0) {
142 char filename[7] =
"A-data";
143 char statname[12] =
"A-data-stat";
144 char sym_file_name =
'A';
145 FILE *fp[file_counter];
146 unsigned char *file_content[file_counter];
147 struct stat64 file_stat[file_counter];
148 long max_file_size = 0;
150 for (
unsigned current_file = 0; current_file < file_counter;
152 char *content_filename = content_filenames_list[current_file];
154 if ((fp[current_file] = fopen(content_filename,
"r")) == NULL ||
155 stat64(content_filename, file_stat + current_file) < 0) {
156 perror(
"Failed to open");
157 fprintf(stderr,
"Failure opening %s %p\n", content_filename,
162 long nbytes = file_stat[current_file].st_size;
163 max_file_size = max_file_size > nbytes ? max_file_size : nbytes;
165 if ((file_content[current_file] = (
unsigned char *)malloc(nbytes)) ==
167 fputs(
"Memory allocation failure\n", stderr);
173 fptr = file_content[current_file];
174 while ((read_char = fgetc(fp[current_file])) != EOF) {
175 *fptr = (
unsigned char)read_char;
182 for (
unsigned current_file = 0; current_file < file_counter;
184 long nbytes = file_stat[current_file].st_size;
185 if (nbytes < max_file_size) {
186 file_content[current_file] =
187 (
unsigned char *)realloc(file_content[current_file], max_file_size);
189 file_content[current_file][nbytes] = EOF;
190 for (
int i = nbytes; i < max_file_size; i++) {
191 file_content[current_file][i] =
'\0';
195 filename[0] = sym_file_name;
196 statname[0] = sym_file_name;
197 push_obj(&b, filename, max_file_size, file_content[current_file]);
198 push_obj(&b, statname,
sizeof(
struct stat64),
199 (
unsigned char *)&file_stat[current_file]);
200 free(file_content[current_file]);
201 file_content[current_file] = NULL;
205 char *buf1 = (
char *)malloc(1024);
206 char *buf2 = (
char *)malloc(1024);
207 char *buf3 = (
char *)malloc(1024);
208 snprintf(buf1, 1024,
"-sym-files");
209 snprintf(buf2, 1024,
"%d", file_counter);
210 snprintf(buf3, 1024,
"%ld", max_file_size);
211 argv_copy[argv_copy_idx++] = buf1;
212 argv_copy[argv_copy_idx++] = buf2;
213 argv_copy[argv_copy_idx++] = buf3;
216 if (stdin_content_filename) {
218 struct stat64 file_stat;
219 char filename[6] =
"stdin";
220 char statname[11] =
"stdin-stat";
222 if ((fp = fopen(stdin_content_filename,
"r")) == NULL ||
223 stat64(stdin_content_filename, &file_stat) < 0) {
224 fprintf(stderr,
"Failure opening %s\n", stdin_content_filename);
228 unsigned char *file_content, *fptr;
229 if ((file_content = (
unsigned char *)malloc(file_stat.st_size)) == NULL) {
230 fputs(
"Memory allocation failure\n", stderr);
236 while ((read_char = fgetc(fp)) != EOF) {
237 *fptr = (
unsigned char)read_char;
241 push_obj(&b, filename, file_stat.st_size, file_content);
242 push_obj(&b, statname,
sizeof(
struct stat64), (
unsigned char *)&file_stat);
246 char *buf1 = (
char *)malloc(1024);
247 char *buf2 = (
char *)malloc(1024);
248 snprintf(buf1, 1024,
"-sym-stdin");
249 snprintf(buf2, 1024,
"%lld", (
long long int) file_stat.st_size);
250 argv_copy[argv_copy_idx++] = buf1;
251 argv_copy[argv_copy_idx++] = buf2;
254 if (stdout_content_filename) {
256 struct stat64 file_stat;
257 unsigned char file_content[1024];
258 char filename[7] =
"stdout";
259 char statname[12] =
"stdout-stat";
261 if ((fp = fopen(stdout_content_filename,
"r")) == NULL ||
262 stat64(stdout_content_filename, &file_stat) < 0) {
263 fprintf(stderr,
"Failure opening %s\n", stdout_content_filename);
268 for (
int i = 0; i < file_stat.st_size && i < 1024; ++i) {
269 read_char = fgetc(fp);
270 file_content[i] = (
unsigned char)read_char;
273 for (
int i = file_stat.st_size; i < 1024; ++i) {
277 file_stat.st_size = 1024;
279 push_obj(&b, filename, 1024, file_content);
280 push_obj(&b, statname,
sizeof(
struct stat64), (
unsigned char *)&file_stat);
282 char *buf = (
char *)malloc(1024);
283 snprintf(buf, 1024,
"-sym-stdout");
284 argv_copy[argv_copy_idx++] = buf;
287 argv_copy[argv_copy_idx] = 0;
294 if (!
kTest_toFile(&b, bout_file ? bout_file :
"file.bout"))
303 for (
int i = 0; i < (int)argv_copy_idx; ++i) {
int kTest_toFile(KTest *bo, const char *path)
int main(int argc, char *argv[])
void print_usage_and_exit(char *program_name)
static void push_obj(KTest *b, const char *name, unsigned total_bytes, unsigned char *bytes)
static void push_range(KTest *b, const char *name, unsigned value)