klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gen-random-bout.cpp
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <unistd.h>
9 
11 
12 #if defined(__FreeBSD__) || defined(__minix)
13 #define stat64 stat
14 #endif
15 
16 // --sym-args 0 1 10 --sym-args 0 2 2 --sym-files 1 8 --sym-stdout
17 static int getint(char *i) {
18  if(!i) {
19  fprintf(stderr, "ran out of arguments!\n");
20  assert(i);
21  }
22  return atoi(i);
23 }
24 
25 #define MAX 64
26 static void push_obj(KTest *b, const char *name, unsigned non_zero_bytes,
27  unsigned total_bytes) {
28  KTestObject *o = &b->objects[b->numObjects++];
29  assert(b->numObjects < MAX);
30 
31  o->name = strdup(name);
32  o->numBytes = total_bytes;
33  o->bytes = (unsigned char *)malloc(o->numBytes);
34 
35  unsigned i;
36  for(i = 0; i < non_zero_bytes; i++)
37  o->bytes[i] = random();
38 
39  for(i = non_zero_bytes; i < total_bytes; i++)
40  o->bytes[i] = 0;
41 }
42 
43 
44 static void push_range(KTest *b, const char *name, unsigned value) {
45  KTestObject *o = &b->objects[b->numObjects++];
46  assert(b->numObjects < MAX);
47 
48  o->name = strdup(name);
49  o->numBytes = 4;
50  o->bytes = (unsigned char *)malloc(o->numBytes);
51 
52  *(unsigned*)o->bytes = value;
53 }
54 
55 int main(int argc, char *argv[]) {
56  unsigned i, narg;
57  unsigned sym_stdout = 0;
58 
59  if (argc < 2) {
60  fprintf(stderr, "Usage: %s <random-seed> <argument-types>\n", argv[0]);
61  fprintf(stderr, " If <random-seed> is 0, time(NULL)*getpid() is used as a seed\n");
62  fprintf(stderr, " <argument-types> are the ones accepted by KLEE: --sym-args, --sym-files etc.\n");
63  fprintf(stderr, " Ex: %s 100 --sym-args 0 2 2 --sym-files 1 8\n", argv[0]);
64  exit(1);
65  }
66 
67  unsigned seed = atoi(argv[1]);
68  if (seed)
69  srandom(seed);
70  else srandom(time(NULL) * getpid());
71 
72  KTest b;
73  b.numArgs = argc;
74  b.args = argv;
75  b.symArgvs = 0;
76  b.symArgvLen = 0;
77 
78  b.numObjects = 0;
79  b.objects = (KTestObject *)malloc(MAX * sizeof *b.objects);
80 
81  for(i = 2; i < (unsigned)argc; i++) {
82  if(strcmp(argv[i], "--sym-args") == 0) {
83  unsigned lb = getint(argv[++i]);
84  unsigned ub = getint(argv[++i]);
85  unsigned nbytes = getint(argv[++i]);
86 
87  narg = random() % (ub - lb) + lb;
88  push_range(&b, "range", narg);
89 
90  while(narg-- > 0) {
91  unsigned x = random() % (nbytes + 1);
92 
93  // A little different than how klee does it but more natural
94  // for random.
95  static int total_args;
96  char arg[1024];
97 
98  sprintf(arg, "arg%d", total_args++);
99  push_obj(&b, arg, x, nbytes+1);
100  }
101  } else if(strcmp(argv[i], "--sym-stdout") == 0) {
102  if(!sym_stdout) {
103  sym_stdout = 1;
104  push_obj(&b, "stdout", 1024, 1024);
105  push_obj(&b, "stdout-stat", sizeof(struct stat64),
106  sizeof(struct stat64));
107  }
108  } else if(strcmp(argv[i], "--sym-files") == 0) {
109  unsigned nfiles = getint(argv[++i]);
110  unsigned nbytes = getint(argv[++i]);
111 
112  while(nfiles-- > 0) {
113  push_obj(&b, "file", nbytes, nbytes);
114  push_obj(&b, "file-stat", sizeof(struct stat64), sizeof(struct stat64));
115  }
116 
117  push_obj(&b, "stdin", nbytes, nbytes);
118  push_obj(&b, "stdin-stat", sizeof(struct stat64), sizeof(struct stat64));
119  } else {
120  fprintf(stderr, "unexpected option <%s>\n", argv[i]);
121  assert(0);
122  }
123  }
124 
125  if (!kTest_toFile(&b, "file.bout"))
126  assert(0);
127  return 0;
128 }
Definition: KTest.h:26
unsigned symArgvs
Definition: KTest.h:33
unsigned numBytes
Definition: KTest.h:21
int main(int argc, char *argv[])
unsigned numArgs
Definition: KTest.h:30
unsigned numObjects
Definition: KTest.h:36
#define MAX
char ** args
Definition: KTest.h:31
int kTest_toFile(KTest *bo, const char *path)
Definition: KTest.cpp:178
unsigned char * bytes
Definition: KTest.h:22
KTestObject * objects
Definition: KTest.h:37
unsigned symArgvLen
Definition: KTest.h:34
static void push_range(KTest *b, const char *name, unsigned value)
char * name
Definition: KTest.h:20
static void push_obj(KTest *b, const char *name, unsigned non_zero_bytes, unsigned total_bytes)
static int getint(char *i)