int main(int argc, char *argv[]) { unsigned i, narg; unsigned sym_stdout = 0; if (argc < 2) { fprintf(stderr, "Usage: %s <random-seed> <argument-types>\n", argv[0]); fprintf(stderr, " If <random-seed> is 0, time(NULL)*getpid() is used as a seed\n"); fprintf(stderr, " <argument-types> are the ones accepted by KLEE: --sym-args, --sym-files etc.\n"); fprintf(stderr, " Ex: %s 100 --sym-args 0 2 2 --sym-files 1 8\n", argv[0]); exit(1); } unsigned seed = atoi(argv[1]); if (seed) srandom(seed); else srandom(time(NULL) * getpid()); KTest b; b.numArgs = argc; b.args = argv; b.symArgvs = 0; b.symArgvLen = 0; b.numObjects = 0; b.objects = (KTestObject *)malloc(MAX * sizeof *b.objects); for(i = 2; i < (unsigned)argc; i++) { if(strcmp(argv[i], "--sym-args") == 0) { unsigned lb = getint(argv[++i]); unsigned ub = getint(argv[++i]); unsigned nbytes = getint(argv[++i]); narg = random() % (ub - lb) + lb; push_range(&b, "range", narg); while(narg-- > 0) { unsigned x = random() % (nbytes + 1); // A little different than how klee does it but more natural // for random. static int total_args; char arg[1024]; sprintf(arg, "arg%d", total_args++); push_obj(&b, arg, x, nbytes+1); } } else if(strcmp(argv[i], "--sym-stdout") == 0) { if(!sym_stdout) { sym_stdout = 1; push_obj(&b, "stdout", 1024, 1024); push_obj(&b, "stdout-stat", sizeof(struct stat64), sizeof(struct stat64)); } } else if(strcmp(argv[i], "--sym-files") == 0) { unsigned nfiles = getint(argv[++i]); unsigned nbytes = getint(argv[++i]); while(nfiles-- > 0) { push_obj(&b, "file", nbytes, nbytes); push_obj(&b, "file-stat", sizeof(struct stat64), sizeof(struct stat64)); } push_obj(&b, "stdin", nbytes, nbytes); push_obj(&b, "stdin-stat", sizeof(struct stat64), sizeof(struct stat64)); } else { fprintf(stderr, "unexpected option <%s>\n", argv[i]); assert(0); } } if (!kTest_toFile(&b, "file.bout")) assert(0); return 0; }
int main(int argc, char *argv[]) { unsigned i, argv_copy_idx; unsigned file_counter = 0; char *stdout_content_filename = NULL; char *stdin_content_filename = NULL; char *content_filenames_list[1024]; char **argv_copy; if (argc < 2) print_usage_and_exit(argv[0]); KTest b; b.symArgvs = 0; b.symArgvLen = 0; b.numObjects = 0; b.objects = (KTestObject *)malloc(MAX * sizeof *b.objects); if ((argv_copy = (char **)malloc(sizeof(char *) * argc * 2)) == NULL) { fprintf(stderr, "Could not allocate more memory\n"); return 1; } argv_copy[0] = (char *)malloc(strlen(argv[0]) + 1); strcpy(argv_copy[0], argv[0]); argv_copy_idx = 1; for (i = 1; i < (unsigned)argc; i++) { if (strcmp(argv[i], "--sym-stdout") == 0 || strcmp(argv[i], "-sym-stdout") == 0) { if (++i == (unsigned)argc || argv[i][0] == '-') print_usage_and_exit(argv[0]); if (stdout_content_filename) print_usage_and_exit(argv[0]); stdout_content_filename = argv[i]; } else if (strcmp(argv[i], "--sym-stdin") == 0 || strcmp(argv[i], "-sym-stdin") == 0) { if (++i == (unsigned)argc || argv[i][0] == '-') print_usage_and_exit(argv[0]); if (stdin_content_filename) print_usage_and_exit(argv[0]); stdin_content_filename = argv[i]; } else if (strcmp(argv[i], "--sym-file") == 0 || strcmp(argv[i], "-sym-file") == 0) { if (++i == (unsigned)argc || argv[i][0] == '-') print_usage_and_exit(argv[0]); content_filenames_list[file_counter++] = argv[i]; } else { long nbytes = strlen(argv[i]) + 1; static int total_args = 0; char arg[1024]; sprintf(arg, "arg%d", total_args++); push_obj(&b, (const char *)arg, nbytes, (unsigned char *)argv[i]); char *buf1 = (char *)malloc(1024); char *buf2 = (char *)malloc(1024); strcpy(buf1, "-sym-arg"); sprintf(buf2, "%ld", nbytes - 1); argv_copy[argv_copy_idx++] = buf1; argv_copy[argv_copy_idx++] = buf2; } } if (file_counter > 0) { FILE *fp; struct stat64 file_stat; char *content_filename = content_filenames_list[file_counter - 1]; if ((fp = fopen(content_filename, "r")) == NULL || stat64(content_filename, &file_stat) < 0) { fprintf(stderr, "Failure opening %s\n", content_filename); print_usage_and_exit(argv[0]); } long nbytes = file_stat.st_size; char filename[7] = "A-data"; char statname[12] = "A-data-stat"; unsigned char *file_content, *fptr; if ((file_content = (unsigned char *)malloc(nbytes)) == NULL) { fprintf(stderr, "Memory allocation failure\n"); exit(1); } int read_char; fptr = file_content; while ((read_char = fgetc(fp)) != EOF) { *fptr = (unsigned char)read_char; fptr++; } push_obj(&b, filename, nbytes, file_content); push_obj(&b, statname, sizeof(struct stat64), (unsigned char *)&file_stat); free(file_content); char *buf1 = (char *)malloc(1024); char *buf2 = (char *)malloc(1024); char *buf3 = (char *)malloc(1024); sprintf(buf1, "-sym-files"); sprintf(buf2, "1"); sprintf(buf3, "%ld", nbytes); argv_copy[argv_copy_idx++] = buf1; argv_copy[argv_copy_idx++] = buf2; argv_copy[argv_copy_idx++] = buf3; } if (stdin_content_filename) { FILE *fp; struct stat64 file_stat; char filename[6] = "stdin"; char statname[11] = "stdin-stat"; if ((fp = fopen(stdin_content_filename, "r")) == NULL || stat64(stdin_content_filename, &file_stat) < 0) { fprintf(stderr, "Failure opening %s\n", stdin_content_filename); print_usage_and_exit(argv[0]); } unsigned char *file_content, *fptr; if ((file_content = (unsigned char *)malloc(file_stat.st_size)) == NULL) { fprintf(stderr, "Memory allocation failure\n"); exit(1); } int read_char; fptr = file_content; while ((read_char = fgetc(fp)) != EOF) { *fptr = (unsigned char)read_char; fptr++; } push_obj(&b, filename, file_stat.st_size, file_content); push_obj(&b, statname, sizeof(struct stat64), (unsigned char *)&file_stat); free(file_content); char *buf1 = (char *)malloc(1024); char *buf2 = (char *)malloc(1024); sprintf(buf1, "-sym-stdin"); sprintf(buf2, "%ld", file_stat.st_size); argv_copy[argv_copy_idx++] = buf1; argv_copy[argv_copy_idx++] = buf2; } if (stdout_content_filename) { FILE *fp; struct stat64 file_stat; unsigned char file_content[1024]; char filename[7] = "stdout"; char statname[12] = "stdout-stat"; if ((fp = fopen(stdout_content_filename, "r")) == NULL || stat64(stdout_content_filename, &file_stat) < 0) { fprintf(stderr, "Failure opening %s\n", stdout_content_filename); print_usage_and_exit(argv[0]); } int read_char; for (int i = 0; i < file_stat.st_size && i < 1024; ++i) { read_char = fgetc(fp); file_content[i] = (unsigned char)read_char; } for (int i = file_stat.st_size; i < 1024; ++i) { file_content[i] = 0; } file_stat.st_size = 1024; push_obj(&b, filename, 1024, file_content); push_obj(&b, statname, sizeof(struct stat64), (unsigned char *)&file_stat); char *buf = (char *)malloc(1024); sprintf(buf, "-sym-stdout"); argv_copy[argv_copy_idx++] = buf; } argv_copy[argv_copy_idx] = 0; b.numArgs = argv_copy_idx; b.args = argv_copy; push_range(&b, "model_version", 1); if (!kTest_toFile(&b, "file.bout")) assert(0); for (int i = 0; i < (int)b.numObjects; ++i) { free(b.objects[i].name); free(b.objects[i].bytes); } free(b.objects); for (int i = 0; i < (int)argv_copy_idx; ++i) { free(argv_copy[i]); } free(argv_copy); return 0; }