annotate libmpx/mpxrt/mpxrt-utils.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* mpxrt-utils.c -*-C++-*-
kono
parents:
diff changeset
2 *
kono
parents:
diff changeset
3 *************************************************************************
kono
parents:
diff changeset
4 *
kono
parents:
diff changeset
5 * @copyright
kono
parents:
diff changeset
6 * Copyright (C) 2014, Intel Corporation
kono
parents:
diff changeset
7 * All rights reserved.
kono
parents:
diff changeset
8 *
kono
parents:
diff changeset
9 * @copyright
kono
parents:
diff changeset
10 * Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
11 * modification, are permitted provided that the following conditions
kono
parents:
diff changeset
12 * are met:
kono
parents:
diff changeset
13 *
kono
parents:
diff changeset
14 * * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
16 * * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
17 * notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
18 * the documentation and/or other materials provided with the
kono
parents:
diff changeset
19 * distribution.
kono
parents:
diff changeset
20 * * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
21 * contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
22 * from this software without specific prior written permission.
kono
parents:
diff changeset
23 *
kono
parents:
diff changeset
24 * @copyright
kono
parents:
diff changeset
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
kono
parents:
diff changeset
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
kono
parents:
diff changeset
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kono
parents:
diff changeset
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
kono
parents:
diff changeset
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kono
parents:
diff changeset
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
kono
parents:
diff changeset
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
36 * POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
37 *
kono
parents:
diff changeset
38 **************************************************************************/
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #define __STDC_FORMAT_MACROS
kono
parents:
diff changeset
41 #include "config.h"
kono
parents:
diff changeset
42 #include <inttypes.h>
kono
parents:
diff changeset
43 #include <unistd.h>
kono
parents:
diff changeset
44 #include <stdio.h>
kono
parents:
diff changeset
45 #include <stdarg.h>
kono
parents:
diff changeset
46 #include <stdlib.h>
kono
parents:
diff changeset
47 #include <string.h>
kono
parents:
diff changeset
48 #include <limits.h>
kono
parents:
diff changeset
49 #include <pthread.h>
kono
parents:
diff changeset
50 #include "mpxrt-utils.h"
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 #ifndef HAVE_SECURE_GETENV
kono
parents:
diff changeset
53 #define secure_getenv __secure_getenv
kono
parents:
diff changeset
54 #endif
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 #define MPX_RT_OUT "CHKP_RT_OUT_FILE"
kono
parents:
diff changeset
57 #define MPX_RT_ERR "CHKP_RT_ERR_FILE"
kono
parents:
diff changeset
58 #define MPX_RT_VERBOSE "CHKP_RT_VERBOSE"
kono
parents:
diff changeset
59 #define MPX_RT_VERBOSE_DEFAULT VERB_BR
kono
parents:
diff changeset
60 #define MPX_RT_MODE "CHKP_RT_MODE"
kono
parents:
diff changeset
61 #define MPX_RT_MODE_DEFAULT MPX_RT_COUNT
kono
parents:
diff changeset
62 #define MPX_RT_MODE_DEFAULT_STR "count"
kono
parents:
diff changeset
63 #define MPX_RT_STOP_HANDLER "CHKP_RT_STOP_HANDLER"
kono
parents:
diff changeset
64 #define MPX_RT_STOP_HANDLER_DEFAULT MPX_RT_STOP_HANDLER_ABORT
kono
parents:
diff changeset
65 #define MPX_RT_STOP_HANDLER_DEFAULT_STR "abort"
kono
parents:
diff changeset
66 #define MPX_RT_HELP "CHKP_RT_HELP"
kono
parents:
diff changeset
67 #define MPX_RT_ADDPID "CHKP_RT_ADDPID"
kono
parents:
diff changeset
68 #define MPX_RT_BNDPRESERVE "CHKP_RT_BNDPRESERVE"
kono
parents:
diff changeset
69 #define MPX_RT_BNDPRESERVE_DEFAULT 0
kono
parents:
diff changeset
70 #define MPX_RT_PRINT_SUMMARY "CHKP_RT_PRINT_SUMMARY"
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 #define MAX_FILE_NAME PATH_MAX
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 typedef struct env_var_s {
kono
parents:
diff changeset
75 char *env_name;
kono
parents:
diff changeset
76 char *env_val;
kono
parents:
diff changeset
77 struct env_var_s *next;
kono
parents:
diff changeset
78 } env_var_t;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 typedef struct {
kono
parents:
diff changeset
81 env_var_t *first;
kono
parents:
diff changeset
82 env_var_t *last;
kono
parents:
diff changeset
83 } env_var_list_t;
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* Following vars are initialized at process startup only
kono
parents:
diff changeset
86 and thus are considered to be thread safe. */
kono
parents:
diff changeset
87 static int summary;
kono
parents:
diff changeset
88 static int add_pid;
kono
parents:
diff changeset
89 static mpx_rt_mode_t mode;
kono
parents:
diff changeset
90 static mpx_rt_stop_mode_handler_t stop_handler;
kono
parents:
diff changeset
91 static env_var_list_t env_var_list;
kono
parents:
diff changeset
92 static verbose_type verbose_val;
kono
parents:
diff changeset
93 static FILE *out;
kono
parents:
diff changeset
94 static FILE *err;
kono
parents:
diff changeset
95 static char out_name[MAX_FILE_NAME];
kono
parents:
diff changeset
96 static char err_name[MAX_FILE_NAME];
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* Following vars are read at process finalization only.
kono
parents:
diff changeset
99 All write accesses use the same value and thus are
kono
parents:
diff changeset
100 considered to be thread safe. */
kono
parents:
diff changeset
101 static int out_file_dirty;
kono
parents:
diff changeset
102 static int err_file_dirty;
kono
parents:
diff changeset
103 static int files_overwritten;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* Mutex used to sync output. */
kono
parents:
diff changeset
106 static pthread_mutex_t lock;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 static void *
kono
parents:
diff changeset
109 malloc_check (size_t size)
kono
parents:
diff changeset
110 {
kono
parents:
diff changeset
111 void *res = malloc (size);
kono
parents:
diff changeset
112 if (!res)
kono
parents:
diff changeset
113 __mpxrt_print (VERB_ERROR, "Couldn't allocate %zu bytes.", size);
kono
parents:
diff changeset
114 else
kono
parents:
diff changeset
115 memset (res, 0, size);
kono
parents:
diff changeset
116 return res;
kono
parents:
diff changeset
117 }
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 static void
kono
parents:
diff changeset
120 env_var_list_add (const char* env, const char* val)
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 env_var_t* n;
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 if (val == 0)
kono
parents:
diff changeset
125 return;
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 n = (env_var_t *)malloc_check (sizeof (env_var_t));
kono
parents:
diff changeset
128 if (!n)
kono
parents:
diff changeset
129 return;
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 if (env_var_list.first == 0)
kono
parents:
diff changeset
132 env_var_list.first = n;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 if (env_var_list.last)
kono
parents:
diff changeset
135 env_var_list.last->next = n;
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 env_var_list.last = n;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 n->env_name = (char *)malloc_check (strlen (env) + 1);
kono
parents:
diff changeset
140 n->env_val = (char *)malloc_check (strlen (val) + 1);
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 if (!n->env_name || !n->env_val)
kono
parents:
diff changeset
143 return;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 strcpy (n->env_name, env);
kono
parents:
diff changeset
146 strcpy (n->env_val, val);
kono
parents:
diff changeset
147 }
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 static void
kono
parents:
diff changeset
150 set_file_stream (FILE** file, char* file_name,
kono
parents:
diff changeset
151 const char* env, FILE* deflt)
kono
parents:
diff changeset
152 {
kono
parents:
diff changeset
153 int pid;
kono
parents:
diff changeset
154 if (env != 0)
kono
parents:
diff changeset
155 {
kono
parents:
diff changeset
156 if (add_pid)
kono
parents:
diff changeset
157 {
kono
parents:
diff changeset
158 pid = getpid ();
kono
parents:
diff changeset
159 snprintf (file_name, MAX_FILE_NAME, "%s.%d", env, pid);
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161 else
kono
parents:
diff changeset
162 snprintf (file_name, MAX_FILE_NAME, "%s", env);
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 *file = fopen (file_name, "we");
kono
parents:
diff changeset
165 if (*file != 0)
kono
parents:
diff changeset
166 return;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168 *file = deflt;
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /*
kono
parents:
diff changeset
172 * this function will be called after fork in the child
kono
parents:
diff changeset
173 * open new files with pid of the process
kono
parents:
diff changeset
174 */
kono
parents:
diff changeset
175 static void
kono
parents:
diff changeset
176 open_child_files ()
kono
parents:
diff changeset
177 {
kono
parents:
diff changeset
178 char *out_env;
kono
parents:
diff changeset
179 char *err_env;
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 out_env = secure_getenv (MPX_RT_OUT);
kono
parents:
diff changeset
182 err_env = secure_getenv (MPX_RT_ERR);
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 if (add_pid == 0 && (out_env != 0 || err_env != 0))
kono
parents:
diff changeset
185 {
kono
parents:
diff changeset
186 __mpxrt_print (VERB_ERROR, "MPX RUNTIME WARNING: out/err files are "
kono
parents:
diff changeset
187 "overwritten in new processes since %s was not set.\n",
kono
parents:
diff changeset
188 MPX_RT_ADDPID);
kono
parents:
diff changeset
189 files_overwritten = 1;
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 set_file_stream (&out, out_name, out_env, stdout);
kono
parents:
diff changeset
193 if (out_env == 0 || err_env == 0 || (strcmp (out_env, err_env) != 0))
kono
parents:
diff changeset
194 set_file_stream (&err, err_name, err_env, stderr);
kono
parents:
diff changeset
195 else
kono
parents:
diff changeset
196 /* in case we get the same file name for err and out */
kono
parents:
diff changeset
197 err = out;
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 /*
kono
parents:
diff changeset
201 * this function is called after fork in the parent
kono
parents:
diff changeset
202 */
kono
parents:
diff changeset
203 static void
kono
parents:
diff changeset
204 at_fork_check (void)
kono
parents:
diff changeset
205 {
kono
parents:
diff changeset
206 char *out_env;
kono
parents:
diff changeset
207 char *err_env;
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 out_env = secure_getenv (MPX_RT_OUT);
kono
parents:
diff changeset
210 err_env = secure_getenv (MPX_RT_ERR);
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 if (add_pid == 0 && (out_env != 0 || err_env != 0))
kono
parents:
diff changeset
213 files_overwritten = 1;
kono
parents:
diff changeset
214 }
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 static mpx_rt_mode_t
kono
parents:
diff changeset
217 set_mpx_rt_mode (const char *env)
kono
parents:
diff changeset
218 {
kono
parents:
diff changeset
219 if (env == 0)
kono
parents:
diff changeset
220 return MPX_RT_MODE_DEFAULT;
kono
parents:
diff changeset
221 else if (strcmp (env, "stop") == 0)
kono
parents:
diff changeset
222 return MPX_RT_STOP;
kono
parents:
diff changeset
223 else if (strcmp (env,"count") == 0)
kono
parents:
diff changeset
224 return MPX_RT_COUNT;
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 __mpxrt_print (VERB_ERROR, "Illegal value '%s' for %s. Legal values are"
kono
parents:
diff changeset
227 "[stop | count]\nUsing default value %s\n",
kono
parents:
diff changeset
228 env, MPX_RT_MODE, MPX_RT_MODE_DEFAULT_STR);
kono
parents:
diff changeset
229 return MPX_RT_MODE_DEFAULT;
kono
parents:
diff changeset
230 }
kono
parents:
diff changeset
231 }
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 static mpx_rt_stop_mode_handler_t
kono
parents:
diff changeset
234 set_mpx_rt_stop_handler (const char *env)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 if (env == 0)
kono
parents:
diff changeset
237 return MPX_RT_STOP_HANDLER_DEFAULT;
kono
parents:
diff changeset
238 else if (strcmp (env, "abort") == 0)
kono
parents:
diff changeset
239 return MPX_RT_STOP_HANDLER_ABORT;
kono
parents:
diff changeset
240 else if (strcmp (env, "exit") == 0)
kono
parents:
diff changeset
241 return MPX_RT_STOP_HANDLER_EXIT;
kono
parents:
diff changeset
242 {
kono
parents:
diff changeset
243 __mpxrt_print (VERB_ERROR, "Illegal value '%s' for %s. Legal values are"
kono
parents:
diff changeset
244 "[abort | exit]\nUsing default value %s\n",
kono
parents:
diff changeset
245 env, MPX_RT_STOP_HANDLER, MPX_RT_STOP_HANDLER_DEFAULT);
kono
parents:
diff changeset
246 return MPX_RT_STOP_HANDLER_DEFAULT;
kono
parents:
diff changeset
247 }
kono
parents:
diff changeset
248 }
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 static void
kono
parents:
diff changeset
251 print_help (void)
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 fprintf (out, "MPX Runtime environment variables help.\n");
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 fprintf (out, "%s \t set output file for info & debug [default: stdout]\n",
kono
parents:
diff changeset
256 MPX_RT_OUT);
kono
parents:
diff changeset
257 fprintf (out, "%s \t set output file for error [default: stderr]\n",
kono
parents:
diff changeset
258 MPX_RT_ERR);
kono
parents:
diff changeset
259 fprintf (out, "%s \t set verbosity type [default: %d]\n"
kono
parents:
diff changeset
260 "\t\t\t 0 - print only internal run time errors\n"
kono
parents:
diff changeset
261 "\t\t\t 1 - just print summary\n"
kono
parents:
diff changeset
262 "\t\t\t 2 - print summary and bound violation information\n "
kono
parents:
diff changeset
263 "\t\t\t 3 - print debug information\n",
kono
parents:
diff changeset
264 MPX_RT_VERBOSE, MPX_RT_VERBOSE_DEFAULT);
kono
parents:
diff changeset
265 fprintf (out, "%s \t\t set MPX runtime behavior on #BR exception."
kono
parents:
diff changeset
266 " [stop | count]\n"
kono
parents:
diff changeset
267 "\t\t\t [default: %s]\n", MPX_RT_MODE, MPX_RT_MODE_DEFAULT_STR);
kono
parents:
diff changeset
268 fprintf (out, "%s \t set the handler function MPX runtime will call\n"
kono
parents:
diff changeset
269 "\t\t\t on #BR exception when %s is set to \'stop\'."
kono
parents:
diff changeset
270 " [abort | exit]\n"
kono
parents:
diff changeset
271 "\t\t\t [default: %s]\n", MPX_RT_STOP_HANDLER, MPX_RT_MODE,
kono
parents:
diff changeset
272 MPX_RT_STOP_HANDLER_DEFAULT_STR);
kono
parents:
diff changeset
273 fprintf (out, "%s \t\t generate out,err file for each process.\n"
kono
parents:
diff changeset
274 "\t\t\t generated file will be MPX_RT_{OUT,ERR}_FILE.pid\n"
kono
parents:
diff changeset
275 "\t\t\t [default: no]\n", MPX_RT_ADDPID);
kono
parents:
diff changeset
276 fprintf (out, "%s \t set value for BNDPRESERVE bit.\n"
kono
parents:
diff changeset
277 "\t\t\t BNDPRESERVE = 0 flush bounds on unprefixed call/ret/jmp\n"
kono
parents:
diff changeset
278 "\t\t\t BNDPRESERVE = 1 do NOT flush bounds\n"
kono
parents:
diff changeset
279 "\t\t\t [default: %d]\n", MPX_RT_BNDPRESERVE,
kono
parents:
diff changeset
280 MPX_RT_BNDPRESERVE_DEFAULT);
kono
parents:
diff changeset
281 fprintf (out, "%s \t print summary at the end of the run\n"
kono
parents:
diff changeset
282 "\t\t\t [default: no]\n", MPX_RT_PRINT_SUMMARY);
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 fprintf (out, "%s \t\t print this help and exit.\n"
kono
parents:
diff changeset
285 "\t\t\t [default: no]\n", MPX_RT_HELP);
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 exit (0);
kono
parents:
diff changeset
288 }
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 static void
kono
parents:
diff changeset
291 validate_bndpreserve (const char *env, int *bndpreserve)
kono
parents:
diff changeset
292 {
kono
parents:
diff changeset
293 if (env == 0)
kono
parents:
diff changeset
294 bndpreserve = MPX_RT_BNDPRESERVE_DEFAULT;
kono
parents:
diff changeset
295 else if (strcmp (env, "0") == 0)
kono
parents:
diff changeset
296 *bndpreserve = 0;
kono
parents:
diff changeset
297 else if (strcmp (env, "1") == 0)
kono
parents:
diff changeset
298 *bndpreserve = 1;
kono
parents:
diff changeset
299 else
kono
parents:
diff changeset
300 {
kono
parents:
diff changeset
301 __mpxrt_print (VERB_ERROR, "Illegal value '%s' for %s. Legal values "
kono
parents:
diff changeset
302 "are [0 | 1]\nUsing default value %d\n",
kono
parents:
diff changeset
303 env, MPX_RT_BNDPRESERVE, MPX_RT_BNDPRESERVE_DEFAULT);
kono
parents:
diff changeset
304 *bndpreserve = MPX_RT_BNDPRESERVE_DEFAULT;
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306 }
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 static verbose_type
kono
parents:
diff changeset
309 init_verbose_val (const char *env)
kono
parents:
diff changeset
310 {
kono
parents:
diff changeset
311 if (env == 0)
kono
parents:
diff changeset
312 return MPX_RT_VERBOSE_DEFAULT;
kono
parents:
diff changeset
313 else if (strcmp(env, "0") == 0)
kono
parents:
diff changeset
314 return VERB_ERROR;
kono
parents:
diff changeset
315 else if (strcmp(env, "1") == 0)
kono
parents:
diff changeset
316 return VERB_INFO;
kono
parents:
diff changeset
317 else if (strcmp(env, "2") == 0)
kono
parents:
diff changeset
318 return VERB_BR;
kono
parents:
diff changeset
319 else if (strcmp(env, "3") == 0)
kono
parents:
diff changeset
320 return VERB_DEBUG;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 __mpxrt_print (VERB_ERROR, "Illegal value '%s' for %s. Legal values "
kono
parents:
diff changeset
323 "are [0..3]\nUsing default value %d\n",
kono
parents:
diff changeset
324 env, MPX_RT_VERBOSE, (int)MPX_RT_VERBOSE_DEFAULT);
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 return MPX_RT_VERBOSE_DEFAULT;
kono
parents:
diff changeset
327 }
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 static void
kono
parents:
diff changeset
330 env_var_print_summary (void)
kono
parents:
diff changeset
331 {
kono
parents:
diff changeset
332 env_var_t* node;
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 __mpxrt_print (VERB_DEBUG, "Used environment variables:\n");
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 node = env_var_list.first;
kono
parents:
diff changeset
337 while (node != 0)
kono
parents:
diff changeset
338 {
kono
parents:
diff changeset
339 __mpxrt_print (VERB_DEBUG, " %s = %s\n", node->env_name, node->env_val);
kono
parents:
diff changeset
340 node = node->next;
kono
parents:
diff changeset
341 }
kono
parents:
diff changeset
342 }
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 /* Return 1 if passes env var value should enable feature. */
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 static int
kono
parents:
diff changeset
347 check_yes (const char *val)
kono
parents:
diff changeset
348 {
kono
parents:
diff changeset
349 return val && (!strcmp (val, "yes") || !strcmp (val, "1"));
kono
parents:
diff changeset
350 }
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 void
kono
parents:
diff changeset
353 __mpxrt_init_env_vars (int* bndpreserve)
kono
parents:
diff changeset
354 {
kono
parents:
diff changeset
355 char *out_env;
kono
parents:
diff changeset
356 char *err_env;
kono
parents:
diff changeset
357 char *env;
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 pthread_mutex_init (&lock, NULL);
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 out_env = secure_getenv (MPX_RT_OUT);
kono
parents:
diff changeset
362 env_var_list_add (MPX_RT_OUT, out_env);
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 err_env = secure_getenv (MPX_RT_ERR);
kono
parents:
diff changeset
365 env_var_list_add (MPX_RT_ERR, err_env);
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 env = secure_getenv (MPX_RT_ADDPID);
kono
parents:
diff changeset
368 env_var_list_add (MPX_RT_ADDPID, env);
kono
parents:
diff changeset
369 add_pid = check_yes (env);
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 set_file_stream (&out, out_name, out_env, stdout);
kono
parents:
diff changeset
372 if (out_env == 0 || err_env == 0 || (strcmp (out_env, err_env) != 0))
kono
parents:
diff changeset
373 set_file_stream (&err, err_name, err_env, stderr);
kono
parents:
diff changeset
374 else
kono
parents:
diff changeset
375 /* in case we get the same file name for err and out */
kono
parents:
diff changeset
376 err = out;
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 env = secure_getenv (MPX_RT_VERBOSE);
kono
parents:
diff changeset
379 env_var_list_add (MPX_RT_VERBOSE, env);
kono
parents:
diff changeset
380 verbose_val = init_verbose_val (env);
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 env = secure_getenv (MPX_RT_MODE);
kono
parents:
diff changeset
383 env_var_list_add (MPX_RT_MODE, env);
kono
parents:
diff changeset
384 mode = set_mpx_rt_mode (env);
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 env = secure_getenv (MPX_RT_STOP_HANDLER);
kono
parents:
diff changeset
387 env_var_list_add (MPX_RT_STOP_HANDLER, env);
kono
parents:
diff changeset
388 stop_handler = set_mpx_rt_stop_handler (env);
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 env = secure_getenv (MPX_RT_BNDPRESERVE);
kono
parents:
diff changeset
391 env_var_list_add (MPX_RT_BNDPRESERVE, env);
kono
parents:
diff changeset
392 validate_bndpreserve (env, bndpreserve);
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 env = secure_getenv (MPX_RT_PRINT_SUMMARY);
kono
parents:
diff changeset
395 env_var_list_add (MPX_RT_PRINT_SUMMARY, env);
kono
parents:
diff changeset
396 summary = check_yes (env);
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 env = secure_getenv (MPX_RT_HELP);
kono
parents:
diff changeset
399 if (check_yes (env))
kono
parents:
diff changeset
400 print_help ();
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 /*
kono
parents:
diff changeset
403 * at fork - create new files for output and err according
kono
parents:
diff changeset
404 * to the env vars.
kono
parents:
diff changeset
405 */
kono
parents:
diff changeset
406 pthread_atfork (NULL, at_fork_check, open_child_files);
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 env_var_print_summary ();
kono
parents:
diff changeset
409 }
kono
parents:
diff changeset
410
kono
parents:
diff changeset
411 void
kono
parents:
diff changeset
412 __mpxrt_utils_free (void)
kono
parents:
diff changeset
413 {
kono
parents:
diff changeset
414 if (files_overwritten)
kono
parents:
diff changeset
415 __mpxrt_print (VERB_INFO, "\nMPX RUNTIME WARNING: out/err files are"
kono
parents:
diff changeset
416 " overwritten in new processes since %s was not set.\n",
kono
parents:
diff changeset
417 MPX_RT_ADDPID);
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 if (out != stdout)
kono
parents:
diff changeset
420 {
kono
parents:
diff changeset
421 fclose (out);
kono
parents:
diff changeset
422 if (out_file_dirty != 1)
kono
parents:
diff changeset
423 remove (out_name);
kono
parents:
diff changeset
424 }
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 if (err != stderr)
kono
parents:
diff changeset
427 {
kono
parents:
diff changeset
428 fclose (err);
kono
parents:
diff changeset
429 if (err_file_dirty != 1)
kono
parents:
diff changeset
430 remove (err_name);
kono
parents:
diff changeset
431 }
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 pthread_mutex_destroy (&lock);
kono
parents:
diff changeset
434 }
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 void
kono
parents:
diff changeset
437 __mpxrt_write_uint (verbose_type vt, uint64_t val, unsigned base)
kono
parents:
diff changeset
438 {
kono
parents:
diff changeset
439 static const char digits[] = {
kono
parents:
diff changeset
440 '0', '1', '2', '3', '4', '5', '6', '7',
kono
parents:
diff changeset
441 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
kono
parents:
diff changeset
442 char str[65];
kono
parents:
diff changeset
443 int pos = 64;;
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 str[pos--] = 0;
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 if (vt > verbose_val || base <= 1 || base > sizeof (digits))
kono
parents:
diff changeset
448 return;
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 if (val < base)
kono
parents:
diff changeset
451 str[pos--] = digits[val];
kono
parents:
diff changeset
452 else
kono
parents:
diff changeset
453 while (val)
kono
parents:
diff changeset
454 {
kono
parents:
diff changeset
455 str[pos--] = digits[val % base];
kono
parents:
diff changeset
456 val = val / base;
kono
parents:
diff changeset
457 }
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 __mpxrt_write (vt, str + pos + 1);
kono
parents:
diff changeset
460 }
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 void
kono
parents:
diff changeset
463 __mpxrt_write (verbose_type vt, const char* str)
kono
parents:
diff changeset
464 {
kono
parents:
diff changeset
465 va_list argp;
kono
parents:
diff changeset
466 FILE *print_to;
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 if (vt > verbose_val)
kono
parents:
diff changeset
469 return;
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 if (vt == VERB_ERROR)
kono
parents:
diff changeset
472 {
kono
parents:
diff changeset
473 print_to = err;
kono
parents:
diff changeset
474 err_file_dirty = 1;
kono
parents:
diff changeset
475 }
kono
parents:
diff changeset
476 else
kono
parents:
diff changeset
477 {
kono
parents:
diff changeset
478 print_to = out;
kono
parents:
diff changeset
479 out_file_dirty = 1;
kono
parents:
diff changeset
480 }
kono
parents:
diff changeset
481 pthread_mutex_lock (&lock);
kono
parents:
diff changeset
482 write (fileno (print_to), str, strlen (str));
kono
parents:
diff changeset
483 pthread_mutex_unlock (&lock);
kono
parents:
diff changeset
484 va_end (argp);
kono
parents:
diff changeset
485 }
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 void
kono
parents:
diff changeset
488 __mpxrt_print (verbose_type vt, const char* frmt, ...)
kono
parents:
diff changeset
489 {
kono
parents:
diff changeset
490 va_list argp;
kono
parents:
diff changeset
491 FILE *print_to;
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 if (vt > verbose_val)
kono
parents:
diff changeset
494 return;
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 va_start (argp, frmt);
kono
parents:
diff changeset
497 if (vt == VERB_ERROR)
kono
parents:
diff changeset
498 {
kono
parents:
diff changeset
499 print_to = err;
kono
parents:
diff changeset
500 err_file_dirty = 1;
kono
parents:
diff changeset
501 }
kono
parents:
diff changeset
502 else
kono
parents:
diff changeset
503 {
kono
parents:
diff changeset
504 print_to = out;
kono
parents:
diff changeset
505 out_file_dirty = 1;
kono
parents:
diff changeset
506 }
kono
parents:
diff changeset
507 pthread_mutex_lock (&lock);
kono
parents:
diff changeset
508 vfprintf (print_to, frmt, argp);
kono
parents:
diff changeset
509 fflush (print_to);
kono
parents:
diff changeset
510 pthread_mutex_unlock (&lock);
kono
parents:
diff changeset
511 va_end (argp);
kono
parents:
diff changeset
512 }
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 mpx_rt_mode_t
kono
parents:
diff changeset
515 __mpxrt_mode (void)
kono
parents:
diff changeset
516 {
kono
parents:
diff changeset
517 return mode;
kono
parents:
diff changeset
518 }
kono
parents:
diff changeset
519
kono
parents:
diff changeset
520 mpx_rt_mode_t
kono
parents:
diff changeset
521 __mpxrt_stop_handler (void)
kono
parents:
diff changeset
522 {
kono
parents:
diff changeset
523 return stop_handler;
kono
parents:
diff changeset
524 }
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 void __attribute__ ((noreturn))
kono
parents:
diff changeset
527 __mpxrt_stop (void)
kono
parents:
diff changeset
528 {
kono
parents:
diff changeset
529 if (__mpxrt_stop_handler () == MPX_RT_STOP_HANDLER_ABORT)
kono
parents:
diff changeset
530 abort ();
kono
parents:
diff changeset
531 else if (__mpxrt_stop_handler () == MPX_RT_STOP_HANDLER_EXIT)
kono
parents:
diff changeset
532 exit (255);
kono
parents:
diff changeset
533 __builtin_unreachable ();
kono
parents:
diff changeset
534 }
kono
parents:
diff changeset
535
kono
parents:
diff changeset
536 void
kono
parents:
diff changeset
537 __mpxrt_print_summary (uint64_t num_brs, uint64_t l1_size)
kono
parents:
diff changeset
538 {
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 if (summary == 0)
kono
parents:
diff changeset
541 return;
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 out_file_dirty = 1;
kono
parents:
diff changeset
544
kono
parents:
diff changeset
545 pthread_mutex_lock (&lock);
kono
parents:
diff changeset
546 fprintf (out, "MPX runtime summary:\n");
kono
parents:
diff changeset
547 fprintf (out, " Number of bounds violations: %" PRIu64 ".\n", num_brs);
kono
parents:
diff changeset
548 fprintf (out, " Size of allocated L1: %" PRIu64 "B\n", l1_size);
kono
parents:
diff changeset
549 fflush (out);
kono
parents:
diff changeset
550 pthread_mutex_unlock (&lock);
kono
parents:
diff changeset
551 }