forked from uci-plrg/jaaru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cc
206 lines (181 loc) · 4.93 KB
/
common.cc
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
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "common.h"
#include "model.h"
#include "execution.h"
#include "stacktrace.h"
#include "output.h"
#include "utils.h"
#define MAX_TRACE_LEN 100
/** @brief Model-checker output file descriptor; default to stdout until redirected */
int model_out = STDOUT_FILENO;
// #define CONFIG_STACKTRACE
// #define DETAILED_STACKTRACE
#ifdef DETAILED_STACKTRACE
static void run_cmd(char * cmd, char *result) {
FILE *fp = popen(cmd, "r");
char output [1024];
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(output, sizeof(output), fp) != NULL) {
replace_char(output, '\n', ':');
strcat(result, output);
}
/* close */
pclose(fp);
}
#endif
/** Print a backtrace of the current program state. */
void print_trace(void)
{
#ifdef CONFIG_STACKTRACE
print_stacktrace(model_out);
#else
void *array[MAX_TRACE_LEN];
char **strings;
int size, i;
size = backtrace(array, MAX_TRACE_LEN);
strings = backtrace_symbols(array, size);
model_print("\nDumping stack trace (%d frames):\n", size);
for (i = 0;i < size;i++) {
#ifdef DETAILED_STACKTRACE
char *sharedlib = strtok(strings[i], "(");
char *func = strtok(NULL, "+");
char *offset = strtok(NULL, ")");
char cmd[2048] = {0};
if(offset) {
sprintf(cmd,"funcaddr=$(nm %s | grep %s | head -n1 | cut -d \" \" -f1) && funcoffset=$(python -c \"print hex(0x${funcaddr}+%s)\") && addr2line -e %s ${funcoffset}",
sharedlib, func, offset, sharedlib);
} else {
offset = strtok(func, ")");
sprintf(cmd,"addr2line -a -f --exe=%s %s", sharedlib, offset);
}
char output[1024] ={0};
run_cmd(cmd, output);
model_print("\t%s:%s(%s)\n", sharedlib, func, output);
#else
model_print("\t%s\n", strings[i]);
#endif
}
free(strings);
#endif /* CONFIG_STACKTRACE */
}
void assert_hook(void)
{
model->get_execution()->print_summary(true, true);
model_print("Add breakpoint to line %u in file %s.\n", __LINE__, __FILE__);
}
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);
model->assert_user_bug(msg);
}
}
#ifndef CONFIG_DEBUG
static int fd_user_out; /**< @brief File descriptor from which to read user program output */
/**
* @brief Setup output redirecting
*
* Redirects user program's stdout to a pipe so that we can dump it
* selectively, when displaying bugs, etc.
* Also connects a file descriptor 'model_out' directly to stdout, for printing
* data when needed.
*
* The model-checker can selectively choose to print/hide the user program
* output.
* @see clear_program_output
* @see print_program_output
*
* Note that the user program's pipe has limited memory, so if a program will
* output much data, we will need to buffer it in user-space during execution.
* This also means that if ModelChecker decides not to print an execution, it
* should promptly clear the pipe.
*
* This function should only be called once.
*/
char filename[256];
void redirect_output()
{
/* Save stdout for later use */
model_out = dup(STDOUT_FILENO);
if (model_out < 0) {
perror("Error in dup\n");
exit(EXIT_FAILURE);
}
snprintf_(filename, sizeof(filename), "PMCheckOutput%d", getpid());
fd_user_out = open(filename, O_CREAT | O_TRUNC| O_RDWR, S_IRWXU);
if (dup2(fd_user_out, STDOUT_FILENO) < 0) {
perror("Error in dup2");
exit(EXIT_FAILURE);
}
}
/**
* @brief Wrapper for reading data to buffer
*
* Besides a simple read, this handles the subtleties of EOF and nonblocking
* input (if fd is O_NONBLOCK).
*
* @param fd The file descriptor to read.
* @param buf Buffer to read to.
* @param maxlen Maximum data to read to buffer
* @return The length of data read. If zero, then we hit EOF or ran out of data
* (non-blocking)
*/
static ssize_t read_to_buf(int fd, char *buf, size_t maxlen)
{
ssize_t ret = read(fd, buf, maxlen);
if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
} else {
perror("read");
exit(EXIT_FAILURE);
}
}
return ret;
}
/** @brief Dump any pending program output without printing */
void clear_program_output()
{
fflush(stdout);
close(fd_user_out);
unlink(filename);
}
/** @brief Print out any pending program output */
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) {
ret = read_to_buf(fd_user_out, buf, sizeof(buf));
if (!ret)
break;
while (ret > 0) {
ssize_t res = write(model_out, buf, ret);
if (res < 0) {
perror("write");
exit(EXIT_FAILURE);
}
ret -= res;
}
}
close(fd_user_out);
unlink(filename);
model_print("---- END PROGRAM OUTPUT ----\n");
}
#endif /* ! CONFIG_DEBUG */