Skip to content

Commit

Permalink
At function to determine if we're running under LSAN
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Apr 5, 2018
1 parent 2646680 commit e3356a8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions configure
Expand Up @@ -7520,6 +7520,7 @@ for ac_header in \
prot.h \
pwd.h \
resource.h \
sanitizer/common_interface_defs.h \
semaphore.h \
sia.h \
siad.h \
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -789,6 +789,7 @@ AC_CHECK_HEADERS( \
prot.h \
pwd.h \
resource.h \
sanitizer/common_interface_defs.h \
semaphore.h \
sia.h \
siad.h \
Expand Down
4 changes: 4 additions & 0 deletions src/include/autoconf.h.in
Expand Up @@ -356,6 +356,10 @@
/* Define to 1 if you have the <resource.h> header file. */
#undef HAVE_RESOURCE_H

/* Define to 1 if you have the <sanitizer/common_interface_defs.h> header
file. */
#undef HAVE_SANITIZER_COMMON_INTERFACE_DEFS_H

/* Define to 1 if you have the <semaphore.h> header file. */
#undef HAVE_SEMAPHORE_H

Expand Down
1 change: 1 addition & 0 deletions src/include/debug.h
Expand Up @@ -54,6 +54,7 @@ typedef void (*fr_fault_log_t)(char const *msg, ...) CC_HINT(format (printf, 1,
typedef int (*fr_fault_cb_t)(int signum);
typedef struct fr_bt_marker fr_bt_marker_t;

int fr_get_lsan_state(void);
void fr_debug_state_store(void);
char const *fr_debug_state_to_msg(fr_debug_state_t state);
void fr_debug_break(bool always);
Expand Down
87 changes: 85 additions & 2 deletions src/lib/util/debug.c
Expand Up @@ -21,13 +21,21 @@
* @copyright 2013 The FreeRADIUS server project
* @copyright 2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/libradius.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/libradius.h>

#if defined(HAVE_MALLOPT) && defined(HAVE_MALLOC_H)
# include <malloc.h>
#endif
Expand Down Expand Up @@ -108,6 +116,81 @@ static TALLOC_CTX *talloc_autofree_ctx;
# include <sys/capability.h>
# endif

#ifdef HAVE_SANITIZER_COMMON_INTERFACE_DEFS_H
# include <sanitizer/common_interface_defs.h>
#endif

#ifdef HAVE_SANITIZER_COMMON_INTERFACE_DEFS_H
static int lsan_test_pipe[2] = {-1, -1};
static int lsan_test_pid = -1;

/** Callback for LSAN - do not rename
*
*/
int CC_HINT(used) __lsan_is_turned_off(void)
{
uint8_t ret = 1;

/* Parent */
if (lsan_test_pid != 0) return 0;

/* Child */
if (write(lsan_test_pipe[1], &ret, sizeof(ret)) < 0) {
fprintf(stderr, "Writing LSAN status failed: %s", strerror(errno));
}
close(lsan_test_pipe[1]);
return 0;
}

/** Determine if we're running under LSAN (Leak Sanitizer)
*
* @return
* - 0 if we're not.
* - 1 if we are.
* - -1 if we can't tell because of an error.
* - -2 if we can't tell because we were compiled with support for the LSAN interface.
*/
int fr_get_lsan_state(void)
{
uint8_t ret = 0;

if (pipe(lsan_test_pipe) < 0) {
fr_strerror_printf("Failed opening internal pipe: %s", fr_syserror(errno));
return -1;
}

lsan_test_pid = fork();
if (lsan_test_pid == -1) {
fr_strerror_printf("Error forking: %s", fr_syserror(errno));
return -1;
}

/* Child */
if (lsan_test_pid == 0) {
close(lsan_test_pipe[0]); /* Close parent's side */
exit(EXIT_SUCCESS); /* Results in LSAN calling __lsan_is_turned_off via onexit handler */
}

/* Parent */
close(lsan_test_pipe[1]); /* Close child's side */

while ((read(lsan_test_pipe[0], &ret, sizeof(ret)) < 0) && (errno == EINTR));

close(lsan_test_pipe[0]); /* Close our side (so we don't leak FDs) */

/* Collect child */
waitpid(lsan_test_pid, NULL, 0);

return ret;
}
#else
int fr_get_lsan_state(void)
{
fr_strerror_printf("Not built with support for LSAN interface");
return -2;
}
#endif

/** Determine if we're running under a debugger by attempting to attach using pattach
*
* @return
Expand Down

0 comments on commit e3356a8

Please sign in to comment.