gen-random-bout.cpp

Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <time.h>
00008 #include <unistd.h>
00009 
00010 #include "klee/Internal/ADT/KTest.h"
00011 
00012 // --sym-args 0 1 10 --sym-args 0 2 2 --sym-files 1 8 --sym-stdout
00013 static int getint(char *i) {
00014   if(!i) {
00015     fprintf(stderr, "ran out of arguments!\n");
00016     assert(i);
00017   }
00018   return atoi(i);
00019 }
00020 
00021 #define MAX 64
00022 static void push_obj(KTest *b, const char *name, unsigned non_zero_bytes, 
00023                      unsigned total_bytes) {
00024   KTestObject *o = &b->objects[b->numObjects++];
00025   assert(b->numObjects < MAX);
00026 
00027   o->name = strdup(name);
00028   o->numBytes = total_bytes;
00029   o->bytes = (unsigned char *)malloc(o->numBytes);
00030 
00031   unsigned i;
00032   for(i = 0; i < non_zero_bytes; i++)
00033     o->bytes[i] = random();
00034 
00035   for(i = non_zero_bytes; i < total_bytes; i++)
00036     o->bytes[i] = 0;
00037 }
00038 
00039 
00040 static void push_range(KTest *b, const char *name, unsigned value) {
00041   KTestObject *o = &b->objects[b->numObjects++];
00042   assert(b->numObjects < MAX);
00043 
00044   o->name = strdup(name);
00045   o->numBytes = 4;
00046   o->bytes = (unsigned char *)malloc(o->numBytes);
00047 
00048   *(unsigned*)o->bytes = value;
00049 }
00050 
00051 int main(int argc, char *argv[]) {
00052   unsigned i, narg;
00053   unsigned sym_stdout = 0;
00054 
00055   if (argc < 2) {
00056     fprintf(stderr, "Usage: %s <random-seed> <argument-types>\n", argv[0]);
00057     fprintf(stderr, "       If <random-seed> is 0, time(NULL)*getpid() is used as a seed\n");
00058     fprintf(stderr, "       <argument-types> are the ones accepted by KLEE: --sym-args, --sym-files etc.\n");
00059     fprintf(stderr, "   Ex: %s 100 --sym-args 0 2 2 --sym-files 1 8\n", argv[0]);
00060     exit(1);
00061   }
00062 
00063   unsigned seed = atoi(argv[1]);
00064   if (seed)
00065     srandom(seed);
00066   else srandom(time(NULL) * getpid());
00067 
00068   KTest b;
00069   b.numArgs = argc;
00070   b.args = argv;
00071   b.symArgvs = 0;
00072   b.symArgvLen = 0;
00073 
00074   b.numObjects = 0;
00075   b.objects = (KTestObject *)malloc(MAX * sizeof *b.objects);
00076 
00077   for(i = 2; i < (unsigned)argc; i++) {
00078     if(strcmp(argv[i], "--sym-args") == 0) {
00079       unsigned lb = getint(argv[++i]);
00080       unsigned ub = getint(argv[++i]);
00081       unsigned nbytes = getint(argv[++i]);
00082 
00083       narg = random() % (ub - lb) + lb;
00084       push_range(&b, "range", narg);
00085 
00086       while(narg-- > 0) {
00087         unsigned x = random() % (nbytes + 1);
00088 
00089         // A little different than how klee does it but more natural
00090         // for random.
00091         static int total_args;
00092         char arg[1024];
00093 
00094         sprintf(arg, "arg%d", total_args++);
00095         push_obj(&b, arg, x, nbytes+1);
00096       }
00097     } else if(strcmp(argv[i], "--sym-stdout") == 0) {
00098       if(!sym_stdout) {
00099         sym_stdout = 1;
00100         push_obj(&b, "stdout", 1024, 1024);
00101         push_obj(&b, "stdout-stat", sizeof(struct stat64), 
00102                  sizeof(struct stat64));
00103       }
00104     } else if(strcmp(argv[i], "--sym-files") == 0) {
00105       unsigned nfiles = getint(argv[++i]);
00106       unsigned nbytes = getint(argv[++i]);
00107 
00108       while(nfiles-- > 0) {
00109         push_obj(&b, "file", nbytes, nbytes);
00110         push_obj(&b, "file-stat", sizeof(struct stat64), sizeof(struct stat64));
00111       }
00112 
00113       push_obj(&b, "stdin", nbytes, nbytes);
00114       push_obj(&b, "stdin-stat", sizeof(struct stat64), sizeof(struct stat64));
00115     } else {
00116       fprintf(stderr, "unexpected option <%s>\n", argv[i]);
00117       assert(0);
00118     }
00119   }
00120 
00121   if (!kTest_toFile(&b, "file.bout"))
00122     assert(0);
00123   return 0;
00124 }

Generated on Fri Jun 5 03:31:32 2009 for klee by  doxygen 1.5.8