1 /* mpxrt-utils.c -*-C++-*-
3 *************************************************************************
6 * Copyright (C) 2014, Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 **************************************************************************/
40 #define __STDC_FORMAT_MACROS
50 #include "mpxrt-utils.h"
52 #ifndef HAVE_SECURE_GETENV
53 #define secure_getenv __secure_getenv
56 #define MPX_RT_OUT "CHKP_RT_OUT_FILE"
57 #define MPX_RT_ERR "CHKP_RT_ERR_FILE"
58 #define MPX_RT_VERBOSE "CHKP_RT_VERBOSE"
59 #define MPX_RT_VERBOSE_DEFAULT VERB_BR
60 #define MPX_RT_MODE "CHKP_RT_MODE"
61 #define MPX_RT_MODE_DEFAULT MPX_RT_COUNT
62 #define MPX_RT_MODE_DEFAULT_STR "count"
63 #define MPX_RT_HELP "CHKP_RT_HELP"
64 #define MPX_RT_ADDPID "CHKP_RT_ADDPID"
65 #define MPX_RT_BNDPRESERVE "CHKP_RT_BNDPRESERVE"
66 #define MPX_RT_BNDPRESERVE_DEFAULT 0
67 #define MPX_RT_PRINT_SUMMARY "CHKP_RT_PRINT_SUMMARY"
69 #define MAX_FILE_NAME PATH_MAX
71 typedef struct env_var_s
{
74 struct env_var_s
*next
;
82 /* Following vars are initialized at process startup only
83 and thus are considered to be thread safe. */
86 static mpx_rt_mode_t mode
;
87 static env_var_list_t env_var_list
;
88 static verbose_type verbose_val
;
91 static char out_name
[MAX_FILE_NAME
];
92 static char err_name
[MAX_FILE_NAME
];
94 /* Following vars are read at process finalization only.
95 All write accesses use the same value and thus are
96 considered to be thread safe. */
97 static int out_file_dirty
;
98 static int err_file_dirty
;
99 static int files_overwritten
;
101 /* Mutex used to sync output. */
102 static pthread_mutex_t lock
;
105 malloc_check (size_t size
)
107 void *res
= malloc (size
);
109 __mpxrt_print (VERB_ERROR
, "Couldn't allocate %zu bytes.", size
);
111 memset (res
, 0, size
);
116 env_var_list_add (const char* env
, const char* val
)
123 n
= (env_var_t
*)malloc_check (sizeof (env_var_t
));
127 if (env_var_list
.first
== 0)
128 env_var_list
.first
= n
;
130 if (env_var_list
.last
)
131 env_var_list
.last
->next
= n
;
133 env_var_list
.last
= n
;
135 n
->env_name
= (char *)malloc_check (strlen (env
) + 1);
136 n
->env_val
= (char *)malloc_check (strlen (val
) + 1);
138 if (!n
->env_name
|| !n
->env_val
)
141 strcpy (n
->env_name
, env
);
142 strcpy (n
->env_val
, val
);
146 set_file_stream (FILE** file
, char* file_name
,
147 const char* env
, FILE* deflt
)
155 snprintf (file_name
, MAX_FILE_NAME
, "%s.%d", env
, pid
);
158 snprintf (file_name
, MAX_FILE_NAME
, "%s", env
);
160 *file
= fopen (file_name
, "we");
168 * this function will be called after fork in the child
169 * open new files with pid of the process
177 out_env
= secure_getenv (MPX_RT_OUT
);
178 err_env
= secure_getenv (MPX_RT_ERR
);
180 if (add_pid
== 0 && (out_env
!= 0 || err_env
!= 0))
182 __mpxrt_print (VERB_ERROR
, "MPX RUNTIME WARNING: out/err files are "
183 "overwritten in new processes since %s was not set.\n",
185 files_overwritten
= 1;
188 set_file_stream (&out
, out_name
, out_env
, stdout
);
189 if (out_env
== 0 || err_env
== 0 || (strcmp (out_env
, err_env
) != 0))
190 set_file_stream (&err
, err_name
, err_env
, stderr
);
192 /* in case we get the same file name for err and out */
197 * this function is called after fork in the parent
205 out_env
= secure_getenv (MPX_RT_OUT
);
206 err_env
= secure_getenv (MPX_RT_ERR
);
208 if (add_pid
== 0 && (out_env
!= 0 || err_env
!= 0))
209 files_overwritten
= 1;
213 set_mpx_rt_mode (const char *env
)
216 return MPX_RT_MODE_DEFAULT
;
217 else if (strcmp (env
, "stop") == 0)
219 else if (strcmp (env
,"count") == 0)
222 __mpxrt_print (VERB_ERROR
, "Illegal value '%s' for %s. Legal values are"
223 "[stop | count]\nUsing default value %s\n",
224 env
, MPX_RT_MODE
, MPX_RT_MODE_DEFAULT_STR
);
225 return MPX_RT_MODE_DEFAULT
;
232 fprintf (out
, "MPX Runtime environment variables help.\n");
234 fprintf (out
, "%s \t set output file for info & debug [default: stdout]\n",
236 fprintf (out
, "%s \t set output file for error [default: stderr]\n",
238 fprintf (out
, "%s \t set verbosity type [default: %d]\n"
239 "\t\t\t 0 - print only internal run time errors\n"
240 "\t\t\t 1 - just print summary\n"
241 "\t\t\t 2 - print summary and bound violation information\n "
242 "\t\t\t 3 - print debug information\n",
243 MPX_RT_VERBOSE
, MPX_RT_VERBOSE_DEFAULT
);
244 fprintf (out
, "%s \t\t set MPX runtime behavior on #BR exception."
246 "\t\t\t [default: %s]\n", MPX_RT_MODE
, MPX_RT_MODE_DEFAULT_STR
);
247 fprintf (out
, "%s \t\t generate out,err file for each process.\n"
248 "\t\t\t generated file will be MPX_RT_{OUT,ERR}_FILE.pid\n"
249 "\t\t\t [default: no]\n", MPX_RT_ADDPID
);
250 fprintf (out
, "%s \t set value for BNDPRESERVE bit.\n"
251 "\t\t\t BNDPRESERVE = 0 flush bounds on unprefixed call/ret/jmp\n"
252 "\t\t\t BNDPRESERVE = 1 do NOT flush bounds\n"
253 "\t\t\t [default: %d]\n", MPX_RT_BNDPRESERVE
,
254 MPX_RT_BNDPRESERVE_DEFAULT
);
255 fprintf (out
, "%s \t print summary at the end of the run\n"
256 "\t\t\t [default: no]\n", MPX_RT_PRINT_SUMMARY
);
258 fprintf (out
, "%s \t\t print this help and exit.\n"
259 "\t\t\t [default: no]\n", MPX_RT_HELP
);
265 validate_bndpreserve (const char *env
, int *bndpreserve
)
268 bndpreserve
= MPX_RT_BNDPRESERVE_DEFAULT
;
269 else if (strcmp (env
, "0") == 0)
271 else if (strcmp (env
, "1") == 0)
275 __mpxrt_print (VERB_ERROR
, "Illegal value '%s' for %s. Legal values "
276 "are [0 | 1]\nUsing default value %d\n",
277 env
, MPX_RT_BNDPRESERVE
, MPX_RT_BNDPRESERVE_DEFAULT
);
278 *bndpreserve
= MPX_RT_BNDPRESERVE_DEFAULT
;
283 init_verbose_val (const char *env
)
286 return MPX_RT_VERBOSE_DEFAULT
;
287 else if (strcmp(env
, "0") == 0)
289 else if (strcmp(env
, "1") == 0)
291 else if (strcmp(env
, "2") == 0)
293 else if (strcmp(env
, "3") == 0)
296 __mpxrt_print (VERB_ERROR
, "Illegal value '%s' for %s. Legal values "
297 "are [0..3]\nUsing default value %d\n",
298 env
, MPX_RT_VERBOSE
, (int)MPX_RT_VERBOSE_DEFAULT
);
300 return MPX_RT_VERBOSE_DEFAULT
;
304 env_var_print_summary (void)
308 __mpxrt_print (VERB_DEBUG
, "Used environment variables:\n");
310 node
= env_var_list
.first
;
313 __mpxrt_print (VERB_DEBUG
, " %s = %s\n", node
->env_name
, node
->env_val
);
318 /* Return 1 if passes env var value should enable feature. */
321 check_yes (const char *val
)
323 return val
&& (!strcmp (val
, "yes") || !strcmp (val
, "1"));
327 __mpxrt_init_env_vars (int* bndpreserve
)
333 pthread_mutex_init (&lock
, NULL
);
335 out_env
= secure_getenv (MPX_RT_OUT
);
336 env_var_list_add (MPX_RT_OUT
, out_env
);
338 err_env
= secure_getenv (MPX_RT_ERR
);
339 env_var_list_add (MPX_RT_ERR
, err_env
);
341 env
= secure_getenv (MPX_RT_ADDPID
);
342 env_var_list_add (MPX_RT_ADDPID
, env
);
343 add_pid
= check_yes (env
);
345 set_file_stream (&out
, out_name
, out_env
, stdout
);
346 if (out_env
== 0 || err_env
== 0 || (strcmp (out_env
, err_env
) != 0))
347 set_file_stream (&err
, err_name
, err_env
, stderr
);
349 /* in case we get the same file name for err and out */
352 env
= secure_getenv (MPX_RT_VERBOSE
);
353 env_var_list_add (MPX_RT_VERBOSE
, env
);
354 verbose_val
= init_verbose_val (env
);
356 env
= secure_getenv (MPX_RT_MODE
);
357 env_var_list_add (MPX_RT_MODE
, env
);
358 mode
= set_mpx_rt_mode (env
);
360 env
= secure_getenv (MPX_RT_BNDPRESERVE
);
361 env_var_list_add (MPX_RT_BNDPRESERVE
, env
);
362 validate_bndpreserve (env
, bndpreserve
);
364 env
= secure_getenv (MPX_RT_PRINT_SUMMARY
);
365 env_var_list_add (MPX_RT_PRINT_SUMMARY
, env
);
366 summary
= check_yes (env
);
368 env
= secure_getenv (MPX_RT_HELP
);
373 * at fork - create new files for output and err according
376 pthread_atfork (NULL
, at_fork_check
, open_child_files
);
378 env_var_print_summary ();
382 __mpxrt_utils_free (void)
384 if (files_overwritten
)
385 __mpxrt_print (VERB_INFO
, "\nMPX RUNTIME WARNING: out/err files are"
386 " overwritten in new processes since %s was not set.\n",
392 if (out_file_dirty
!= 1)
399 if (err_file_dirty
!= 1)
403 pthread_mutex_destroy (&lock
);
407 __mpxrt_write_uint (verbose_type vt
, uint64_t val
, unsigned base
)
409 static const char digits
[] = {
410 '0', '1', '2', '3', '4', '5', '6', '7',
411 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
417 if (vt
> verbose_val
|| base
<= 1 || base
> sizeof (digits
))
421 str
[pos
--] = digits
[val
];
425 str
[pos
--] = digits
[val
% base
];
429 __mpxrt_write (vt
, str
+ pos
+ 1);
433 __mpxrt_write (verbose_type vt
, const char* str
)
438 if (vt
> verbose_val
)
441 if (vt
== VERB_ERROR
)
451 pthread_mutex_lock (&lock
);
452 write (fileno (print_to
), str
, strlen (str
));
453 pthread_mutex_unlock (&lock
);
458 __mpxrt_print (verbose_type vt
, const char* frmt
, ...)
463 if (vt
> verbose_val
)
466 va_start (argp
, frmt
);
467 if (vt
== VERB_ERROR
)
477 pthread_mutex_lock (&lock
);
478 vfprintf (print_to
, frmt
, argp
);
480 pthread_mutex_unlock (&lock
);
491 __mpxrt_print_summary (uint64_t num_brs
, uint64_t l1_size
)
499 pthread_mutex_lock (&lock
);
500 fprintf (out
, "MPX runtime summary:\n");
501 fprintf (out
, " Number of bounds violations: %" PRIu64
".\n", num_brs
);
502 fprintf (out
, " Size of allocated L1: %" PRIu64
"B\n", l1_size
);
504 pthread_mutex_unlock (&lock
);