Skip to content

Commit c58d8fc

Browse files
dmantipovxhernandez
authored andcommitted
libglusterfs: annotate synctasks with AddressSanitizer API
If '--enable-asan' is specified and API headers are detected, annotate synctask context switch with AddressSanitizer API. Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com> Fixes: gluster#1000
1 parent e7bae2f commit c58d8fc

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

configure.ac

+15
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ if test "x$enable_asan" = "xyes"; then
290290
SANITIZER=asan
291291
AC_CHECK_LIB([asan], [__asan_init], ,
292292
[AC_MSG_ERROR([--enable-asan requires libasan.so, exiting])])
293+
if test "x$ac_cv_lib_asan___asan_init" = xyes; then
294+
AC_MSG_CHECKING([whether asan API can be used])
295+
saved_CFLAGS=${CFLAGS}
296+
CFLAGS="${CFLAGS} -fsanitize=address"
297+
AC_COMPILE_IFELSE(
298+
[AC_LANG_PROGRAM([
299+
[#include <sanitizer/common_interface_defs.h>]],
300+
[[__sanitizer_finish_switch_fiber(0, 0, 0)]])],
301+
[ASAN_API=yes], [ASAN_API=no])
302+
AC_MSG_RESULT([$ASAN_API])
303+
if test x$ASAN_API = "xyes"; then
304+
AC_DEFINE(HAVE_ASAN_API, 1, [Define if asan API can be used.])
305+
fi
306+
CFLAGS=${saved_CFLAGS}
307+
fi
293308
GF_CFLAGS="${GF_CFLAGS} -O2 -g -fsanitize=address -fno-omit-frame-pointer"
294309
GF_LDFLAGS="${GF_LDFLAGS} -lasan"
295310
fi

libglusterfs/src/glusterfs/syncop.h

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ struct synctask {
8383
} tsan;
8484
#endif
8585

86+
#ifdef HAVE_ASAN_API
87+
void *fake_stack;
88+
#endif
89+
8690
#ifdef HAVE_VALGRIND_API
8791
unsigned stackid;
8892
#endif
@@ -107,6 +111,10 @@ struct syncproc {
107111
} tsan;
108112
#endif
109113

114+
#ifdef HAVE_ASAN_API
115+
void *fake_stack;
116+
#endif
117+
110118
#ifdef HAVE_VALGRIND_API
111119
unsigned stackid;
112120
#endif

libglusterfs/src/syncop.c

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "glusterfs/syncop.h"
1212
#include "glusterfs/libglusterfs-messages.h"
1313

14+
#ifdef HAVE_ASAN_API
15+
#include <sanitizer/common_interface_defs.h>
16+
#endif
17+
1418
#ifdef HAVE_TSAN_API
1519
#include <sanitizer/tsan_interface.h>
1620
#endif
@@ -275,11 +279,21 @@ synctask_yield(struct synctask *task, struct timespec *delta)
275279
__tsan_switch_to_fiber(task->proc->tsan.fiber, 0);
276280
#endif
277281

282+
#ifdef HAVE_ASAN_API
283+
__sanitizer_start_switch_fiber(&task->fake_stack,
284+
task->proc->sched.uc_stack.ss_sp,
285+
task->proc->sched.uc_stack.ss_size);
286+
#endif
287+
278288
if (swapcontext(&task->ctx, &task->proc->sched) < 0) {
279289
gf_msg("syncop", GF_LOG_ERROR, errno, LG_MSG_SWAPCONTEXT_FAILED,
280290
"swapcontext failed");
281291
}
282292

293+
#ifdef HAVE_ASAN_API
294+
__sanitizer_finish_switch_fiber(task->proc->fake_stack, NULL, NULL);
295+
#endif
296+
283297
THIS = oldTHIS;
284298
}
285299

@@ -363,6 +377,11 @@ synctask_wrap(void)
363377
wrong and can lead to crashes. */
364378

365379
task = synctask_get();
380+
381+
#ifdef HAVE_ASAN_API
382+
__sanitizer_finish_switch_fiber(task->fake_stack, NULL, NULL);
383+
#endif
384+
366385
task->ret = task->syncfn(task->opaque);
367386
if (task->synccbk)
368387
task->synccbk(task->ret, task->frame, task->opaque);
@@ -694,11 +713,21 @@ synctask_switchto(struct synctask *task)
694713
__tsan_switch_to_fiber(task->tsan.fiber, 0);
695714
#endif
696715

716+
#ifdef HAVE_ASAN_API
717+
__sanitizer_start_switch_fiber(&task->proc->fake_stack,
718+
task->ctx.uc_stack.ss_sp,
719+
task->ctx.uc_stack.ss_size);
720+
#endif
721+
697722
if (swapcontext(&task->proc->sched, &task->ctx) < 0) {
698723
gf_msg("syncop", GF_LOG_ERROR, errno, LG_MSG_SWAPCONTEXT_FAILED,
699724
"swapcontext failed");
700725
}
701726

727+
#ifdef HAVE_ASAN_API
728+
__sanitizer_finish_switch_fiber(task->fake_stack, NULL, NULL);
729+
#endif
730+
702731
if (task->state == SYNCTASK_DONE) {
703732
synctask_done(task);
704733
return;

0 commit comments

Comments
 (0)