All,

this patch adds support for asan for i?86/x86_64-*freebsd*.

Test results can be found on the list.

These modifications belong only to gcc. There is one modification to asan/asan_linux.cc, this one is sent upstream. Until this one is in, my patch is on hold.

One thing to note, FreeBSD does not need to link against -ldl. That is why I added an extra config check.

But nevertheless I'd like to get some comments on the patch.

Thanks to Jakub and Dan McGregor.

Thanks,
Andreas


2015-11-29  Andreas Tobler  <andre...@gcc.gnu.org>

        * config/i386/i386.h: Define two new macros:
        SUBTARGET_SHADOW_OFFSET_64 and SUBTARGET_SHADOW_OFFSET_32.
        * config/i386/i386.c (ix86_asan_shadow_offset): Use these macros.
        * config/i386/darwin.h: Override the SUBTARGET_SHADOW_OFFSET_64
        macro.
        * config/i386/freebsd.h: Override the SUBTARGET_SHADOW_OFFSET_64
        and the SUBTARGET_SHADOW_OFFSET_32 macro.
        * config/freebsd.h (LIBASAN_EARLY_SPEC): Define.
        (LIBTSAN_EARLY_SPEC): Likewise.
        (LIBLSAN_EARLY_SPEC): Likewise.

2015-11-29  Andreas Tobler  <andre...@gcc.gnu.org>

        * configure.ac: Replace the hard-coded -ldl requirement for
        link_sanitizer_common with a configure time check for -ldl.
        * configure: Regenerate.
        * configure.tgt: Add x86_64- and i?86-*-freebsd* targets.
Index: gcc/config/freebsd.h
===================================================================
--- gcc/config/freebsd.h        (revision 231047)
+++ gcc/config/freebsd.h        (working copy)
@@ -62,6 +62,27 @@
 #define USE_LD_AS_NEEDED 1
 #endif
 
+/* Link -lasan early on the command line.  For -static-libasan, don't link
+   it for -shared link, the executable should be compiled with -static-libasan
+   in that case, and for executable link with --{,no-}whole-archive around
+   it to force everything into the executable.  And similarly for -ltsan
+   and -llsan.  */
+#if defined(HAVE_LD_STATIC_DYNAMIC)
+#undef LIBASAN_EARLY_SPEC
+#define LIBASAN_EARLY_SPEC "%{!shared:libasan_preinit%O%s} " \
+  "%{static-libasan:%{!shared:" \
+  LD_STATIC_OPTION " --whole-archive -lasan --no-whole-archive " \
+  LD_DYNAMIC_OPTION "}}%{!static-libasan:-lasan -lpthread}"
+#undef LIBTSAN_EARLY_SPEC
+#define LIBTSAN_EARLY_SPEC "%{static-libtsan:%{!shared:" \
+  LD_STATIC_OPTION " --whole-archive -ltsan --no-whole-archive " \
+  LD_DYNAMIC_OPTION "}}%{!static-libtsan:-ltsan -lpthread}"
+#undef LIBLSAN_EARLY_SPEC
+#define LIBLSAN_EARLY_SPEC "%{static-liblsan:%{!shared:" \
+  LD_STATIC_OPTION " --whole-archive -llsan --no-whole-archive " \
+  LD_DYNAMIC_OPTION "}}%{!static-liblsan:-llsan -lpthread}"
+#endif
+
 /************************[  Target stuff  ]***********************************/
 
 /* All FreeBSD Architectures support the ELF object file format.  */
Index: gcc/config/i386/darwin.h
===================================================================
--- gcc/config/i386/darwin.h    (revision 231047)
+++ gcc/config/i386/darwin.h    (working copy)
@@ -295,3 +295,8 @@
     = darwin_init_cfstring_builtins ((unsigned) (IX86_BUILTIN_CFSTRING));      
\
   darwin_rename_builtins ();                                   \
 } while(0)
+
+/* Define the shadow offset for asan.  */
+#undef SUBTARGET_SHADOW_OFFSET_64
+#define SUBTARGET_SHADOW_OFFSET_64  (HOST_WIDE_INT_1 << 44)
+
Index: gcc/config/i386/freebsd.h
===================================================================
--- gcc/config/i386/freebsd.h   (revision 231047)
+++ gcc/config/i386/freebsd.h   (working copy)
@@ -135,3 +135,9 @@
 
 #define TARGET_ASM_FILE_END file_end_indicate_exec_stack
 
+/* Define the shadow offsets for asan.  */
+#undef SUBTARGET_SHADOW_OFFSET_32
+#define SUBTARGET_SHADOW_OFFSET_32  (HOST_WIDE_INT_1 << 30)
+#undef SUBTARGET_SHADOW_OFFSET_64
+#define SUBTARGET_SHADOW_OFFSET_64  (HOST_WIDE_INT_1 << 46)
+
Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c      (revision 231047)
+++ gcc/config/i386/i386.c      (working copy)
@@ -7312,9 +7312,8 @@
 static unsigned HOST_WIDE_INT
 ix86_asan_shadow_offset (void)
 {
-  return TARGET_LP64 ? (TARGET_MACHO ? (HOST_WIDE_INT_1 << 44)
-                                    : HOST_WIDE_INT_C (0x7fff8000))
-                    : (HOST_WIDE_INT_1 << 29);
+  return TARGET_LP64 ? SUBTARGET_SHADOW_OFFSET_64 
+    : SUBTARGET_SHADOW_OFFSET_32; 
 }
 
 /* Argument support functions.  */
Index: gcc/config/i386/i386.h
===================================================================
--- gcc/config/i386/i386.h      (revision 231047)
+++ gcc/config/i386/i386.h      (working copy)
@@ -1272,6 +1272,16 @@
 #define SUBTARGET_FRAME_POINTER_REQUIRED 0
 #endif
 
+/* Define the shadow offset for asan. Other OS's can override in the
+   respective tm.h files.  */
+#ifndef SUBTARGET_SHADOW_OFFSET_32
+#define SUBTARGET_SHADOW_OFFSET_32  (HOST_WIDE_INT_1 << 29)
+#endif
+
+#ifndef SUBTARGET_SHADOW_OFFSET_64
+#define SUBTARGET_SHADOW_OFFSET_64  (HOST_WIDE_INT_C (0x7fff8000))
+#endif
+
 /* Make sure we can access arbitrary call frames.  */
 #define SETUP_FRAME_ADDRESSES()  ix86_setup_frame_addresses ()
 
Index: libsanitizer/configure.ac
===================================================================
--- libsanitizer/configure.ac   (revision 231047)
+++ libsanitizer/configure.ac   (working copy)
@@ -96,7 +96,7 @@
 AC_CHECK_FUNCS(clock_getres clock_gettime clock_settime)
 
 # Common libraries that we need to link against for all sanitizer libs.
-link_sanitizer_common='-lpthread -ldl -lm'
+link_sanitizer_common='-lpthread -lm'
 
 # At least for glibc, shm_open is in librt.  But don't pull that
 # in if it still doesn't give us the function we want.  This
@@ -130,6 +130,12 @@
 # Other sanitizers do not override clock_* API
 ])
 
+# Do a configure time check for -ldl
+AC_CHECK_LIB(dl, dlsym,
+    [link_libasan="-ldl $link_libasan"
+link_libtsan="-ldl $link_libtsan"
+])
+
 case "$host" in
   *-*-darwin*) MAC_INTERPOSE=true ; enable_static=no ;;
   *) MAC_INTERPOSE=false ;;
Index: libsanitizer/configure.tgt
===================================================================
--- libsanitizer/configure.tgt  (revision 231047)
+++ libsanitizer/configure.tgt  (working copy)
@@ -21,6 +21,8 @@
 # Filter out unsupported systems.
 TSAN_TARGET_DEPENDENT_OBJECTS=
 case "${target}" in
+  x86_64-*-freebsd* | i?86-*-freebsd*)
+       ;;
   x86_64-*-linux* | i?86-*-linux*)
        if test x$ac_cv_sizeof_void_p = x8; then
                TSAN_SUPPORTED=yes

Reply via email to