10#define _FILE_OFFSET_BITS 64
27#define SMALL_BUFFER_SIZE 64
30#define MAX_FILE_SIZES 256
36 vfprintf(stderr, fmt, args);
46 long int n = strtol(i, NULL, 10);
47 if (n < 0 || n == LONG_MIN || n == LONG_MAX) {
48 error_exit(
"%s:%d: Error in conversion to unsigned: %s\n", __FILE__,
56 unsigned total_bytes) {
60 if ((o->
name = strdup(name)) == NULL) {
61 error_exit(
"%s:%d: strdup() failure\n", __FILE__, __LINE__);
64 if ((o->
bytes = (
unsigned char *)malloc(o->
numBytes)) == NULL) {
65 error_exit(
"%s:%d: malloc() failure\n", __FILE__, __LINE__);
69 for (i = 0; i < non_zero_bytes; i++) {
70 o->
bytes[i] = random() % 255 + 1;
73 for (i = non_zero_bytes; i < total_bytes; i++)
78 unsigned char *content) {
82 if ((o->
name = strdup(name)) == NULL) {
83 error_exit(
"%s:%d: strdup() failure\n", __FILE__, __LINE__);
86 if ((o->
bytes = (
unsigned char *)malloc(total_bytes)) == NULL) {
87 error_exit(
"%s:%d: malloc() failure\n", __FILE__, __LINE__);
89 memcpy(o->
bytes, content, total_bytes);
93 push_obj(b, name, 4, (
unsigned char *)&value);
97 char filename_template[] =
"/tmp/klee-gen-random-bout-XXXXXX";
102 if ((filename = strdup(filename_template)) == NULL) {
103 error_exit(
"%s:%d: strdup() failure\n", __FILE__, __LINE__);
106 if (((fd = mkstemp(filename)) < 0) ||
107 ((fchmod(fd, S_IRWXU | S_IRWXG | S_IRWXO)) < 0)) {
108 error_exit(
"%s:%d: Failure creating %s\n", __FILE__, __LINE__, filename);
111 if ((buf = (
unsigned char *)malloc(size)) == NULL) {
112 error_exit(
"%s:%d: malloc() failure\n", __FILE__, __LINE__);
115 if (write(fd, memset(buf, 0, size), size) != (
int)size) {
118 error_exit(
"%s:%d: Error writing %s\n", __FILE__, __LINE__, filename);
131int main(
int argc,
char *argv[]) {
133 unsigned sym_stdout = 0;
134 unsigned stdin_size = 0;
136 unsigned total_files = 0;
139 char *bout_file = NULL;
143 "Usage: %s <random-seed> <argument-types>\n"
144 " If <random-seed> is 0, time(NULL)*getpid() is used as a seed\n"
145 " <argument-types> are the ones accepted by KLEE: --sym-args, "
146 "--sym-files etc. and --bout-file <filename> for the output file (default: random.bout).\n"
147 " Ex: %s 100 --sym-args 0 2 2 --sym-files 1 8\n",
151 unsigned seed = atoi(argv[1]);
155 srandom(time(NULL) * getpid());
157 if ((argv_copy = (
char **) malloc((argc - 1) *
sizeof(
char *))) == NULL) {
158 error_exit(
"%s:%d: malloc() failure\n", __FILE__, __LINE__);
160 argv_copy[0] = argv[0];
161 for (i = 2; i < (unsigned)argc; ++i) {
162 argv_copy[i - 1] = argv[i];
174 for (i = 2; i < (unsigned)argc; i++) {
175 if (strcmp(argv[i],
"--sym-arg") == 0 || strcmp(argv[i],
"-sym-arg") == 0) {
180 unsigned x = random() % (nbytes + 1);
184 }
else if (strcmp(argv[i],
"--sym-args") == 0 ||
185 strcmp(argv[i],
"-sym-args") == 0) {
191 error_exit(
"--sym-args first argument should be no more than its "
192 "second argument\n");
195 narg = random() % (ub - lb + 1) + lb;
199 unsigned x = random() % (nbytes + 1);
208 }
else if (strcmp(argv[i],
"--sym-stdout") == 0 ||
209 strcmp(argv[i],
"-sym-stdout") == 0) {
211 }
else if (strcmp(argv[i],
"--sym-stdin") == 0 ||
212 strcmp(argv[i],
"-sym-stdin") == 0) {
214 }
else if (strcmp(argv[i],
"--sym-files") == 0 ||
215 strcmp(argv[i],
"-sym-files") == 0) {
220 while (nfiles-- > 0) {
222 error_exit(
"%s:%d: Maximum number of file sizes exceeded (%d)\n",
225 file_sizes[total_files++] = nbytes;
228 }
else if (strcmp(argv[i],
"--bout-file") == 0 ||
229 strcmp(argv[i],
"-bout-file") == 0) {
230 if ((
unsigned)argc == ++i)
231 error_exit(
"Missing file name for --bout-file");
235 error_exit(
"Unexpected option <%s>\n", argv[i]);
239 for (i = 0; i < total_files; ++i) {
240 char filename[] =
"A-data";
241 char file_stat[] =
"A-data-stat";
246 fprintf(stderr,
"%s:%d: Maximum number of file sizes exceeded (%d)\n",
250 nbytes = file_sizes[i];
258 push_obj(&b, file_stat,
sizeof(
struct stat), (
unsigned char *)&s);
268 push_obj(&b,
"stdin-stat",
sizeof(
struct stat), (
unsigned char *)&s);
277 push_obj(&b,
"stdout-stat",
sizeof(
struct stat), (
unsigned char *)&s);
281 if (!
kTest_toFile(&b, bout_file ? bout_file :
"random.bout")) {
282 error_exit(
"Error in storing data into random.bout\n");
int kTest_toFile(KTest *bo, const char *path)
static void error_exit(const char *fmt,...)
int main(int argc, char *argv[])
static unsigned get_unsigned(char *i)
void create_stat(size_t size, struct stat *s)
static void push_obj(KTest *b, const char *name, unsigned total_bytes, unsigned char *content)
static void push_random_obj(KTest *b, const char *name, unsigned non_zero_bytes, unsigned total_bytes)
static void push_range(KTest *b, const char *name, unsigned value)
#define SMALL_BUFFER_SIZE