8 #include <model-assert.h>
12 #include "stacktrace.h"
15 #define MAX_TRACE_LEN 100
17 /** @brief Model-checker output file descriptor; default to stdout until redirected */
18 int model_out = STDOUT_FILENO;
20 #define CONFIG_STACKTRACE
21 /** Print a backtrace of the current program state. */
22 void print_trace(void)
24 #ifdef CONFIG_STACKTRACE
25 print_stacktrace(model_out);
27 void *array[MAX_TRACE_LEN];
31 size = backtrace(array, MAX_TRACE_LEN);
32 strings = backtrace_symbols(array, size);
34 model_print("\nDumping stack trace (%d frames):\n", size);
36 for (i = 0;i < size;i++)
37 model_print("\t%s\n", strings[i]);
40 #endif /* CONFIG_STACKTRACE */
43 void assert_hook(void)
45 model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
48 void model_assert(bool expr, const char *file, int line)
52 sprintf(msg, "Program has hit assertion in file %s at line %d\n",
54 model->assert_user_bug(msg);
60 static int fd_user_out; /**< @brief File descriptor from which to read user program output */
63 * @brief Setup output redirecting
65 * Redirects user program's stdout to a pipe so that we can dump it
66 * selectively, when displaying bugs, etc.
67 * Also connects a file descriptor 'model_out' directly to stdout, for printing
70 * The model-checker can selectively choose to print/hide the user program
72 * @see clear_program_output
73 * @see print_program_output
75 * Note that the user program's pipe has limited memory, so if a program will
76 * output much data, we will need to buffer it in user-space during execution.
77 * This also means that if ModelChecker decides not to print an execution, it
78 * should promptly clear the pipe.
80 * This function should only be called once.
84 void redirect_output()
86 /* Save stdout for later use */
87 model_out = dup(STDOUT_FILENO);
92 snprintf_(filename, sizeof(filename), "C11FuzzerTmp%d", getpid());
93 fd_user_out = open(filename, O_CREAT | O_TRUNC| O_RDWR, S_IRWXU);
94 if (dup2(fd_user_out, STDOUT_FILENO) < 0) {
102 * @brief Wrapper for reading data to buffer
104 * Besides a simple read, this handles the subtleties of EOF and nonblocking
105 * input (if fd is O_NONBLOCK).
107 * @param fd The file descriptor to read.
108 * @param buf Buffer to read to.
109 * @param maxlen Maximum data to read to buffer
110 * @return The length of data read. If zero, then we hit EOF or ran out of data
113 static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
115 ssize_t ret = read(fd, buf, maxlen);
117 if (errno == EAGAIN || errno == EWOULDBLOCK) {
127 /** @brief Dump any pending program output without printing */
128 void clear_program_output()
135 /** @brief Print out any pending program output */
136 void print_program_output()
140 model_print("---- BEGIN PROGRAM OUTPUT ----\n");
142 /* Gather all program output */
145 lseek(fd_user_out, 0, SEEK_SET);
146 /* Read program output pipe and write to (real) stdout */
149 ret = read_to_buf(fd_user_out, buf, sizeof(buf));
153 ssize_t res = write(model_out, buf, ret);
165 model_print("---- END PROGRAM OUTPUT ----\n");
167 #endif /* ! CONFIG_DEBUG */