about summary refs log tree commit diff homepage
path: root/tools/klee-replay/file-creator.c
blob: b8f9addd4d8ed923954f22bb673b6fcbb6acec2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//===-- file-creator.c ----------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "klee-replay.h"

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/wait.h>

#ifdef HAVE_PTY_H
#include <pty.h>
#elif defined(HAVE_UTIL_H)
#include <util.h>
#endif

#if defined(__APPLE__)
#include <sys/termios.h>
#else
#include <termios.h>
#endif

static void create_file(int target_fd,
                       const char *target_name,
                       exe_disk_file_t *dfile,
                       const char *tmpdir);
static void check_file(int index, exe_disk_file_t *dfile);
static void delete_file(const char *path, int recurse);


#define __STDIN (-1)
#define __STDOUT (-2)

static int create_link(const char *fname,
                       exe_disk_file_t *dfile,
                       const char *tmpdir) {
  char buf[64];
  struct stat64 *s = dfile->stat;

  // XXX Broken, we want this path to be somewhere else most likely.
  sprintf(buf, "%s.lnk", fname);
  s->st_mode = (s->st_mode & ~S_IFMT) | S_IFREG;
  create_file(-1, buf, dfile, tmpdir);

  int res = symlink(buf, fname);
  if (res < 0) {
    perror("symlink");
  }

  return open(fname, O_RDWR);
}


static int create_dir(const char *fname, exe_disk_file_t *dfile,
                      const char *tmpdir) {
  int res = mkdir(fname, dfile->stat->st_mode);
  if (res < 0) {
    perror("mkdir");
    return -1;
  }
  return open(fname, O_RDWR);
}

double getTime() {
  struct timeval t;
  gettimeofday(&t, NULL);

  return (double) t.tv_sec + ((double) t.tv_usec / 1000000.0);
}

/// Return true if program exited, false if timed out.
int wait_for_timeout_or_exit(pid_t pid, const char *name, int *statusp) {
  char *t = getenv("KLEE_REPLAY_TIMEOUT");
  int timeout = t ? atoi(t) : 5;
  double wait = timeout * .5;
  double start = getTime();
  fprintf(stderr, "note: %s: waiting %.2fs\n", name, wait);
  while (getTime() - start < wait) {
    struct timespec r = {0, 1000000};
    nanosleep(&r, 0);
    int res = waitpid(pid, statusp, WNOHANG);
    if (res==pid)
      return 1;
  }

  return 0;
}

static int create_char_dev(const char *fname, exe_disk_file_t *dfile,
                           const char *tmpdir) {
  struct stat64 *s = dfile->stat;
  unsigned flen = dfile->size;
  char* contents = dfile->contents;

  // Assume tty, kinda broken, need an actual device id or something
  struct termios term, *ts=&term;
  struct winsize win = { 24, 80, 0, 0 };
  /* Just copied from my system, munged to match what fields
     uclibc thinks are there. */
  ts->c_iflag = 27906;
  ts->c_oflag = 5;
  ts->c_cflag = 1215;
  ts->c_lflag = 35287;
#ifndef __APPLE__
  ts->c_line = 0;
#endif
  ts->c_cc[0] = '\x03';
  ts->c_cc[1] = '\x1c';
  ts->c_cc[2] = '\x7f';
  ts->c_cc[3] = '\x15';
  ts->c_cc[4] = '\x04';
  ts->c_cc[5] = '\x00';
  ts->c_cc[6] = '\x01';
  ts->c_cc[7] = '\xff';
  ts->c_cc[8] = '\x11';
  ts->c_cc[9] = '\x13';
  ts->c_cc[10] = '\x1a';
  ts->c_cc[11] = '\xff';
  ts->c_cc[12] = '\x12';
  ts->c_cc[13] = '\x0f';
  ts->c_cc[14] = '\x17';
  ts->c_cc[15] = '\x16';
  ts->c_cc[16] = '\xff';
  ts->c_cc[17] = '\x00';
  ts->c_cc[18] = '\x00';

  {
    char name[1024];
    int amaster, aslave;
    int res = openpty(&amaster, &aslave, name, &term, &win);
    if (res < 0) {
      perror("openpty");
      exit(1);
    }

    if (symlink(name, fname) == -1) {
      fprintf(stderr, "unable to create sym link to tty\n");
      perror("symlink");
    }

    // pty will not be world writeable
    s->st_mode &= ~02;

    pid_t pid = fork();
    if (pid < 0) {
      perror("fork failed\n");
      exit(1);
    } else if (pid == 0) {
      close(amaster);

      fprintf(stderr, "note: pty slave: setting raw mode\n");
      {
        struct termios mode;

        int res = tcgetattr(aslave, &mode);
        assert(!res);
        mode.c_iflag = IGNBRK;
#if defined(__APPLE__)
        mode.c_oflag &= ~(ONLCR | OCRNL | ONLRET);
#else
        mode.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONLRET);
#endif
        mode.c_lflag = 0;
        mode.c_cc[VMIN] = 1;
        mode.c_cc[VTIME] = 0;
        res = tcsetattr(aslave, TCSANOW, &mode);
        assert(res == 0);
      }

      return aslave;
    } else {
      unsigned pos = 0;
      int status;
      fprintf(stderr, "note: pty master: starting\n");
      close(aslave);

      while (pos < flen) {
        ssize_t res = write(amaster, &contents[pos], flen - pos);
        if (res<0) {
          if (errno != EINTR) {
            fprintf(stderr, "note: pty master: write error\n");
            perror("errno");
            break;
          }
        } else if (res) {
          fprintf(stderr, "note: pty master: wrote: %zd (of %d)\n", res, flen);
          pos += res;
        }
      }

      if (wait_for_timeout_or_exit(pid, "pty master", &status))
        goto pty_exit;

      fprintf(stderr, "note: pty master: closing & waiting\n");
      close(amaster);
      while (1) {
        pid_t res = waitpid(pid, &status, 0);
        if (res < 0) {
          if (errno != EINTR)
            break;
        } else {
          break;
        }
      }

    pty_exit:
      close(amaster);
      fprintf(stderr, "note: pty master: done\n");
      process_status(status, 0, "PTY MASTER");
    }
  }
}

static int create_pipe(const char *fname, exe_disk_file_t *dfile,
                       const char *tmpdir) {
  //struct stat64 *s = dfile->stat;
  unsigned flen = dfile->size;
  char* contents = dfile->contents;

  // XXX what is direction ? need more data
  pid_t pid;
  int fds[2];
  int res = pipe(fds);
  if (res < 0) {
    perror("pipe");
    exit(1);
  }

  pid  = fork();
  if (pid < 0) {
    perror("fork");
    exit(1);
  } else if (pid == 0) {
    close(fds[1]);
    return fds[0];
  } else {
    unsigned pos = 0;
    int status;
    fprintf(stderr, "note: pipe master: starting\n");
    close(fds[0]);

    while (pos < flen) {
      int res = write(fds[1], &contents[pos], flen - pos);
      if (res<0) {
        if (errno != EINTR)
          break;
      } else if (res) {
        pos += res;
      }
    }

    if (wait_for_timeout_or_exit(pid, "pipe master", &status))
      goto pipe_exit;

    fprintf(stderr, "note: pipe master: closing & waiting\n");
    close(fds[1]);
    while (1) {
      pid_t res = waitpid(pid, &status, 0);
      if (res < 0) {
        if (errno != EINTR)
          break;
      } else {
        break;
      }
    }

  pipe_exit:
    close(fds[1]);
    fprintf(stderr, "note: pipe master: done\n");
    process_status(status, 0, "PTY MASTER");
  }
}


static int create_reg_file(const char *fname, exe_disk_file_t *dfile,
                           const char *tmpdir) {
  struct stat64 *s = dfile->stat;
  char* contents = dfile->contents;
  unsigned flen = dfile->size;
  unsigned mode = s->st_mode & 0777;

  //fprintf(stderr, "Creating regular file\n");

  // Open in RDWR just in case we have to end up using this fd.

  if (__exe_env.version == 0 && mode == 0)
    mode = 0644;

  int fd = open(fname, O_CREAT | O_RDWR, mode);
  //    int fd = open(fname, O_CREAT | O_WRONLY, s->st_mode&0777);
  if (fd < 0) {
    fprintf(stderr, "Cannot create file %s\n", fname);
    exit(1);
  }

  ssize_t r = write(fd, contents, flen);
  if (r < 0 || (unsigned) r != flen) {
    fprintf(stderr, "Cannot write file %s\n", fname);
    exit(1);
  }

  struct timeval tv[2];
  tv[0].tv_sec = s->st_atime;
  tv[0].tv_usec = 0;
  tv[1].tv_sec = s->st_mtime;
  tv[1].tv_usec = 0;
  futimes(fd, tv);

  // XXX: Now what we should do is reopen a new fd with the correct modes
  // as they were given to the process.
  lseek(fd, 0, SEEK_SET);

  return fd;
}

static int delete_dir(const char *path, int recurse) {
  if (recurse) {
    DIR *d = opendir(path);
    struct dirent *de;

    if (d) {
      while ((de = readdir(d))) {
        if (strcmp(de->d_name, ".")!=0 && strcmp(de->d_name, "..")!=0) {
          char tmp[PATH_MAX];
          sprintf(tmp, "%s/%s", path, de->d_name);
          delete_file(tmp, 0);
        }
      }

      closedir(d);
    }
  }

  if (rmdir(path) == -1) {
    fprintf(stderr, "Cannot create file %s (exists, is dir, can't remove)\n", path);
    perror("rmdir");
    return -1;
  }

  return 0;
}

static void delete_file(const char *path, int recurse) {
  if (unlink(path) < 0 && errno != ENOENT) {
    if (errno == EISDIR) {
      delete_dir(path, 1);
    } else {
      fprintf(stderr, "Cannot create file %s (already exists)\n", path);
      perror("unlink");
    }
  }
}

static void create_file(int target_fd,
                        const char *target_name,
                        exe_disk_file_t *dfile,
                        const char *tmpdir) {
  struct stat64 *s = dfile->stat;
  char tmpname[PATH_MAX];
  const char *target;
  int fd;

  assert((target_fd == -1) ^ (target_name == NULL));

  if (target_name) {
    target = target_name;
  } else {
    sprintf(tmpname, "%s/fd%d", tmpdir, target_fd);
    target = tmpname;
  }

  delete_file(target, 1);

  // XXX get rid of me once a reasonable solution is found
  s->st_uid = geteuid();
  s->st_gid = getegid();

  if (S_ISLNK(s->st_mode)) {
    fd = create_link(target, dfile, tmpdir);
  }
  else if (S_ISDIR(s->st_mode)) {
    fd = create_dir(target, dfile, tmpdir);
  }
  else if (S_ISCHR(s->st_mode)) {
    fd = create_char_dev(target, dfile, tmpdir);
  }
  else if (S_ISFIFO(s->st_mode) ||
           (target_fd==0 && (s->st_mode & S_IFMT) == 0)) { // XXX hack
    fd = create_pipe(target, dfile, tmpdir);
  }
  else {
    fd = create_reg_file(target, dfile, tmpdir);
  }

  if (fd >= 0) {
    if (target_fd != -1) {
      close(target_fd);
      if (dup2(fd, target_fd) < 0) {
        fprintf(stderr, "note: dup2 failed for target: %d\n", target_fd);
        perror("dup2");
      }
      close(fd);
    } else {
      // Only worry about 1 vs !1
      if (s->st_nlink > 1) {
        char tmp2[PATH_MAX];
        sprintf(tmp2, "%s/%s.link2", tmpdir, target_name);
        if (link(target_name, tmp2) < 0) {
          perror("link");
          exit(1);
        }
      }

      close(fd);
    }
  }
}

void replay_create_files(exe_file_system_t *exe_fs) {
  char tmpdir[PATH_MAX];
  unsigned k;

  if (!getcwd(tmpdir, PATH_MAX)) {
    perror("getcwd");
    exit(1);
  }

  strcat(tmpdir, ".temps");
  delete_file(tmpdir, 1);
  mkdir(tmpdir, 0755);

  umask(0);
  for (k=0; k < exe_fs->n_sym_files; k++) {
    char name[2];
    sprintf(name, "%c", 'A' + k);
    create_file(-1, name, &exe_fs->sym_files[k], tmpdir);
  }

  if (exe_fs->sym_stdin)
    create_file(0, NULL, exe_fs->sym_stdin, tmpdir);

  if (exe_fs->sym_stdout)
    create_file(1, NULL, exe_fs->sym_stdout, tmpdir);

  if (exe_fs->sym_stdin)
    check_file(__STDIN, exe_fs->sym_stdin);

  if (exe_fs->sym_stdout)
    check_file(__STDOUT, exe_fs->sym_stdout);

  for (k=0; k<exe_fs->n_sym_files; ++k)
    check_file(k, &exe_fs->sym_files[k]);
}

static void check_file(int index, exe_disk_file_t *dfile) {
  struct stat s;
  int res;
  char name[32];

  switch (index) {
  case __STDIN:
    strcpy(name, "stdin");
    res = fstat(0, &s);
    break;
  case __STDOUT:
    strcpy(name, "stdout");
    res = fstat(1, &s);
    break;
  default:
    name[0] = 'A' + index;
    name[1] = '\0';
    res = stat(name, &s);
    break;
  }

  if (res < 0) {
    fprintf(stderr, "warning: check_file %d: stat failure\n", index);
    return;
  }

  if (s.st_dev != dfile->stat->st_dev) {
    fprintf(stderr, "warning: check_file %s: dev mismatch: %d vs %d\n",
            name, (int) s.st_dev, (int) dfile->stat->st_dev);
  }
/*   if (s.st_ino != dfile->stat->st_ino) { */
/*     fprintf(stderr, "warning: check_file %s: ino mismatch: %d vs %d\n",  */
/*             name, (int) s.st_ino, (int) dfile->stat->st_ino);     */
/*   } */
  if (s.st_mode != dfile->stat->st_mode) {
    fprintf(stderr, "warning: check_file %s: mode mismatch: %#o vs %#o\n",
            name, s.st_mode, dfile->stat->st_mode);
  }
  if (s.st_nlink != dfile->stat->st_nlink) {
    fprintf(stderr, "warning: check_file %s: nlink mismatch: %d vs %d\n",
            name, (int) s.st_nlink, (int) dfile->stat->st_nlink);
  }
  if (s.st_uid != dfile->stat->st_uid) {
    fprintf(stderr, "warning: check_file %s: uid mismatch: %d vs %d\n",
            name, s.st_uid, dfile->stat->st_uid);
  }
  if (s.st_gid != dfile->stat->st_gid) {
    fprintf(stderr, "warning: check_file %s: gid mismatch: %d vs %d\n",
            name, s.st_gid, dfile->stat->st_gid);
  }
  if (s.st_rdev != dfile->stat->st_rdev) {
    fprintf(stderr, "warning: check_file %s: rdev mismatch: %d vs %d\n",
            name, (int) s.st_rdev, (int) dfile->stat->st_rdev);
  }
  if (s.st_size != dfile->stat->st_size) {
    fprintf(stderr, "warning: check_file %s: size mismatch: %d vs %d\n",
            name, (int) s.st_size, (int) dfile->stat->st_size);
  }
  if (s.st_blksize != dfile->stat->st_blksize) {
    fprintf(stderr, "warning: check_file %s: blksize mismatch: %d vs %d\n",
            name, (int) s.st_blksize, (int) dfile->stat->st_blksize);
  }
  if (s.st_blocks != dfile->stat->st_blocks) {
    fprintf(stderr, "warning: check_file %s: blocks mismatch: %d vs %d\n",
            name, (int) s.st_blocks, (int) dfile->stat->st_blocks);
  }
/*   if (s.st_atime != dfile->stat->st_atime) { */
/*     fprintf(stderr, "warning: check_file %s: atime mismatch: %d vs %d\n",  */
/*             name, (int) s.st_atime, (int) dfile->stat->st_atime); */
/*   } */
/*   if (s.st_mtime != dfile->stat->st_mtime) { */
/*     fprintf(stderr, "warning: check_file %s: mtime mismatch: %d vs %d\n",  */
/*             name, (int) s.st_mtime, (int) dfile->stat->st_mtime);     */
/*   } */
/*   if (s.st_ctime != dfile->stat->st_ctime) { */
/*     fprintf(stderr, "warning: check_file %s: ctime mismatch: %d vs %d\n",  */
/*             name, (int) s.st_ctime, (int) dfile->stat->st_ctime);     */
/*   } */
}