Skip to content

Commit b6d395e

Browse files
author
florian
committed
Add support for building with -fsanitize=undefined.
- add configure option --enable-ubsan - add __ubsan helpers (by Julian) This requires gcc 4.9.2 or later. Not all platforms are supported, though. With this change and VEX r3099 regression tests pass on amd64 with a valgrind compiled with -fsanitize=undefined. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14995 a5019735-40e9-0310-863c-91ae7b9d1cf9
1 parent 1087a63 commit b6d395e

File tree

7 files changed

+125
-13
lines changed

7 files changed

+125
-13
lines changed

Makefile.all.am

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ AM_CFLAGS_BASE = \
111111
@FLAG_W_FORMAT@ \
112112
@FLAG_W_FORMAT_SECURITY@ \
113113
@FLAG_FNO_STACK_PROTECTOR@ \
114+
@FLAG_FSANITIZE@ \
114115
-fno-strict-aliasing \
115116
-fno-builtin
116117

auxprogs/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_SOURCES = getoff.c
7272
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CPPFLAGS = $(AM_CPPFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
7373
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CFLAGS = $(AM_CFLAGS_@VGCONF_PLATFORM_PRI_CAPS@)
7474
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_CCASFLAGS = $(AM_CCASFLAGS_PRI)
75-
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDFLAGS = $(AM_CFLAGS_PRI)
75+
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDFLAGS = $(AM_CFLAGS_PRI) @LIB_UBSAN@
7676
if HAVE_DLINFO_RTLD_DI_TLS_MODID
7777
getoff_@VGCONF_ARCH_PRI@_@VGCONF_OS@_LDADD = $(LDADD) -ldl
7878
endif

configure.ac

+32
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,16 @@ else
827827
VALT_LOAD_ADDRESS_SEC=$valt_load_address_sec_norml
828828
fi
829829

830+
#----------------------------------------------------------------------------
831+
# Undefined behaviour sanitiser
832+
#----------------------------------------------------------------------------
833+
# Check whether we should build with the undefined beahviour sanitiser.
834+
835+
AC_CACHE_CHECK([for using the undefined behaviour sanitiser], vg_cv_ubsan,
836+
[AC_ARG_ENABLE(ubsan,
837+
[ --enable-ubsan enables the undefined behaviour sanitiser],
838+
[vg_cv_ubsan=$enableval],
839+
[vg_cv_ubsan=no])])
830840

831841
#----------------------------------------------------------------------------
832842
# Define MIPS_PAGE_SHIFT (--with-pagesize)
@@ -1777,6 +1787,28 @@ CFLAGS=$safe_CFLAGS
17771787

17781788
AC_SUBST(FLAG_FNO_STACK_PROTECTOR)
17791789

1790+
1791+
# Does this compiler support -fsanitize=undefined?
1792+
# Only checked for if --enable-ubsan was given.
1793+
if test "x${vg_cv_ubsan}" = "xyes"; then
1794+
AC_MSG_CHECKING([if gcc accepts -fsanitize=undefined])
1795+
safe_CFLAGS=$CFLAGS
1796+
CFLAGS="-fsanitize=undefined"
1797+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
1798+
return 0;
1799+
]])], [
1800+
FLAG_FSANITIZE="-fsanitize=undefined"
1801+
LIB_UBSAN="-static-libubsan"
1802+
AC_MSG_RESULT([yes])
1803+
], [
1804+
FLAG_FSANITIZE=""
1805+
LIB_UBSAN=""
1806+
AC_MSG_RESULT([no])
1807+
])
1808+
CFLAGS=$safe_CFLAGS
1809+
AC_SUBST(FLAG_FSANITIZE)
1810+
AC_SUBST(LIB_UBSAN)
1811+
fi
17801812
# does this compiler support --param inline-unit-growth=... ?
17811813

17821814
AC_MSG_CHECKING([if gcc accepts --param inline-unit-growth])

coregrind/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ endif
4848
valgrind_CPPFLAGS = $(AM_CPPFLAGS_PRI)
4949
valgrind_CFLAGS = $(AM_CFLAGS_PRI)
5050
valgrind_CCASFLAGS = $(AM_CCASFLAGS_PRI)
51-
valgrind_LDFLAGS = $(AM_CFLAGS_PRI)
51+
valgrind_LDFLAGS = $(AM_CFLAGS_PRI) @LIB_UBSAN@
5252
if VGCONF_PLATFORMS_INCLUDE_X86_DARWIN
5353
valgrind_LDFLAGS += -Wl,-read_only_relocs -Wl,suppress
5454
endif
@@ -77,7 +77,7 @@ endif
7777
vgdb_CPPFLAGS = $(AM_CPPFLAGS_PRI)
7878
vgdb_CFLAGS = $(AM_CFLAGS_PRI)
7979
vgdb_CCASFLAGS = $(AM_CCASFLAGS_PRI)
80-
vgdb_LDFLAGS = $(AM_CFLAGS_PRI)
80+
vgdb_LDFLAGS = $(AM_CFLAGS_PRI) @LIB_UBSAN@
8181
if VGCONF_PLATVARIANT_IS_ANDROID
8282
vgdb_CFLAGS += -static
8383
endif

coregrind/m_compiler.c

+77
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "config.h"
4040
#include "pub_core_basics.h"
4141
#include "pub_core_libcbase.h"
42+
#include "pub_core_libcassert.h"
43+
#include "pub_core_debuglog.h"
4244

4345
#ifndef HAVE_BUILTIN_POPCOUT
4446

@@ -218,6 +220,81 @@ _intel_fast_memset(void *dest, int value, SizeT num)
218220

219221
#endif
220222

223+
224+
/*====================================================================*/
225+
/*=== gcc -fsanitize=undefined helper function support ===*/
226+
/*====================================================================*/
227+
228+
void __ubsan_handle_type_mismatch ( void );
229+
void __ubsan_handle_type_mismatch ( void )
230+
{
231+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
232+
vg_assert(0);
233+
}
234+
235+
void __ubsan_handle_mul_overflow ( void );
236+
void __ubsan_handle_mul_overflow ( void )
237+
{
238+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
239+
vg_assert(0);
240+
}
241+
242+
void __ubsan_handle_add_overflow ( void );
243+
void __ubsan_handle_add_overflow ( void )
244+
{
245+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
246+
vg_assert(0);
247+
}
248+
249+
void __ubsan_handle_sub_overflow ( void );
250+
void __ubsan_handle_sub_overflow ( void )
251+
{
252+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
253+
vg_assert(0);
254+
}
255+
256+
void __ubsan_handle_divrem_overflow ( void );
257+
void __ubsan_handle_divrem_overflow ( void )
258+
{
259+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
260+
vg_assert(0);
261+
}
262+
263+
void __ubsan_handle_negate_overflow ( void );
264+
void __ubsan_handle_negate_overflow ( void )
265+
{
266+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
267+
vg_assert(0);
268+
}
269+
270+
void __ubsan_handle_out_of_bounds ( void );
271+
void __ubsan_handle_out_of_bounds ( void )
272+
{
273+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
274+
vg_assert(0);
275+
}
276+
277+
void __ubsan_handle_shift_out_of_bounds ( void );
278+
void __ubsan_handle_shift_out_of_bounds ( void )
279+
{
280+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
281+
vg_assert(0);
282+
}
283+
284+
void __ubsan_handle_vla_bound_not_positive ( void );
285+
void __ubsan_handle_vla_bound_not_positive ( void )
286+
{
287+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
288+
vg_assert(0);
289+
}
290+
291+
void __ubsan_handle_nonnull_arg ( void );
292+
void __ubsan_handle_nonnull_arg ( void )
293+
{
294+
VG_(debugLog)(0, "main:ubsan", "In %s", __func__);
295+
vg_assert(0);
296+
}
297+
221298
/*--------------------------------------------------------------------*/
222299
/*--- end ---*/
223300
/*--------------------------------------------------------------------*/

coregrind/m_libcbase.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -694,28 +694,30 @@ void* VG_(memmove)(void *dest, const void *src, SizeT sz)
694694

695695
void* VG_(memset) ( void *destV, Int c, SizeT sz )
696696
{
697-
Int c4;
698-
HChar* d = (HChar*)destV;
697+
UInt c4;
698+
UChar* d = destV;
699+
UChar uc = c;
700+
699701
while ((!VG_IS_4_ALIGNED(d)) && sz >= 1) {
700-
d[0] = c;
702+
d[0] = uc;
701703
d++;
702704
sz--;
703705
}
704706
if (sz == 0)
705707
return destV;
706-
c4 = c & 0xFF;
708+
c4 = uc;
707709
c4 |= (c4 << 8);
708710
c4 |= (c4 << 16);
709711
while (sz >= 16) {
710-
((Int*)d)[0] = c4;
711-
((Int*)d)[1] = c4;
712-
((Int*)d)[2] = c4;
713-
((Int*)d)[3] = c4;
712+
((UInt*)d)[0] = c4;
713+
((UInt*)d)[1] = c4;
714+
((UInt*)d)[2] = c4;
715+
((UInt*)d)[3] = c4;
714716
d += 16;
715717
sz -= 16;
716718
}
717719
while (sz >= 4) {
718-
((Int*)d)[0] = c4;
720+
((UInt*)d)[0] = c4;
719721
d += 4;
720722
sz -= 4;
721723
}

memcheck/tests/vbit-test/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ vbit_test_CPPFLAGS = $(AM_CPPFLAGS_PRI) \
4040
vbit_test_CFLAGS = $(AM_CFLAGS_PRI) -std=c99
4141
vbit_test_DEPENDENCIES =
4242
vbit_test_LDADD =
43-
vbit_test_LDFLAGS = $(AM_CFLAGS_PRI) -std=c99
43+
vbit_test_LDFLAGS = $(AM_CFLAGS_PRI) -std=c99 @LIB_UBSAN@

0 commit comments

Comments
 (0)