fix mutex_trylock bug
[c11tester.git] / common.cc
index a43064d527a03340d0f62d260b13b7638cf131f5..8bef5194201c59f3ee3ff16bf87e1dbaa18a3319 100644 (file)
--- a/common.cc
+++ b/common.cc
@@ -22,9 +22,7 @@ int model_out = STDOUT_FILENO;
 void print_trace(void)
 {
 #ifdef CONFIG_STACKTRACE
-       FILE *file = fdopen(model_out, "w");
-       print_stacktrace(file);
-       fclose(file);
+       print_stacktrace(model_out);
 #else
        void *array[MAX_TRACE_LEN];
        char **strings;
@@ -35,16 +33,11 @@ void print_trace(void)
 
        model_print("\nDumping stack trace (%d frames):\n", size);
 
-       for (i = 0; i < size; i++)
+       for (i = 0;i < size;i++)
                model_print("\t%s\n", strings[i]);
 
        free(strings);
-#endif /* CONFIG_STACKTRACE */
-}
-
-void model_print_summary(void)
-{
-       model->print_summary();
+#endif /* CONFIG_STACKTRACE */
 }
 
 void assert_hook(void)
@@ -57,14 +50,14 @@ void model_assert(bool expr, const char *file, int line)
        if (!expr) {
                char msg[100];
                sprintf(msg, "Program has hit assertion in file %s at line %d\n",
-                               file, line);
+                                               file, line);
                model->assert_user_bug(msg);
        }
 }
 
 #ifndef CONFIG_DEBUG
 
-static int fd_user_out; /**< @brief File descriptor from which to read user program output */
+static int fd_user_out;        /**< @brief File descriptor from which to read user program output */
 
 /**
  * @brief Setup output redirecting
@@ -86,25 +79,23 @@ static int fd_user_out; /**< @brief File descriptor from which to read user prog
  *
  * This function should only be called once.
  */
+char filename[256];
+
 void redirect_output()
 {
-       int fd;
-
        /* Save stdout for later use */
-       model_out = dup(fileno(stdout));
-
-       /* Redirect program output to a pipe */
-       int pipefd[2];
-       if (pipe(pipefd) < 0) {
-               perror("pipe");
+       model_out = dup(STDOUT_FILENO);
+       if (model_out < 0) {
+               perror("dup");
+               exit(EXIT_FAILURE);
+       }
+       snprintf_(filename, sizeof(filename), "C11FuzzerTmp%d", getpid());
+       fd_user_out = open(filename, O_CREAT | O_TRUNC| O_RDWR, S_IRWXU);
+       if (dup2(fd_user_out, STDOUT_FILENO) < 0) {
+               perror("dup2");
                exit(EXIT_FAILURE);
        }
-       fd = dup2(pipefd[1], fileno(stdout)); // STDOUT_FILENO
-       close(pipefd[1]);
 
-       /* Save the "read" side of the pipe for use later */
-       fcntl(pipefd[0], F_SETFL, O_NONBLOCK);
-       fd_user_out = pipefd[0];
 }
 
 /**
@@ -137,8 +128,8 @@ static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
 void clear_program_output()
 {
        fflush(stdout);
-       char buf[200];
-       while (read_to_buf(fd_user_out, buf, sizeof(buf)));
+       close(fd_user_out);
+       unlink(filename);
 }
 
 /** @brief Print out any pending program output */
@@ -146,9 +137,12 @@ void print_program_output()
 {
        char buf[200];
 
+       model_print("---- BEGIN PROGRAM OUTPUT ----\n");
+
        /* Gather all program output */
        fflush(stdout);
 
+       lseek(fd_user_out, 0, SEEK_SET);
        /* Read program output pipe and write to (real) stdout */
        ssize_t ret;
        while (1) {
@@ -164,5 +158,10 @@ void print_program_output()
                        ret -= res;
                }
        }
+
+       close(fd_user_out);
+       unlink(filename);
+
+       model_print("---- END PROGRAM OUTPUT   ----\n");
 }
-#endif /* ! CONFIG_DEBUG */
+#endif /* ! CONFIG_DEBUG */