Skip to content

Commit 91c12eb

Browse files
committed
disable pymalloc tricks with the --with-valgrind option #2422
Patch from James Henstridge.
1 parent 0299d0d commit 91c12eb

5 files changed

Lines changed: 225 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ Core and Builtins
487487
- Issue #3739: The unicode-internal encoder now reports the number of characters
488488
consumed like any other encoder (instead of the number of bytes).
489489

490+
- Issue #2422: When compiled with the ``--with-valgrind`` option, the
491+
pymalloc allocator will be automatically disabled when running under
492+
Valgrind. This gives improved memory leak detection when running
493+
under Valgrind, while taking advantage of pymalloc at other times.
494+
490495
Library
491496
-------
492497

Objects/obmalloc.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
#ifdef WITH_PYMALLOC
44

5+
#ifdef WITH_VALGRIND
6+
#include <valgrind/valgrind.h>
7+
8+
/* If we're using GCC, use __builtin_expect() to reduce overhead of
9+
the valgrind checks */
10+
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
11+
# define UNLIKELY(value) __builtin_expect((value), 0)
12+
#else
13+
# define UNLIKELY(value) (value)
14+
#endif
15+
16+
/* -1 indicates that we haven't checked that we're running on valgrind yet. */
17+
static int running_on_valgrind = -1;
18+
#endif
19+
520
/* An object allocator for Python.
621
722
Here is an introduction to the layers of the Python memory architecture,
@@ -728,6 +743,13 @@ PyObject_Malloc(size_t nbytes)
728743
poolp next;
729744
uint size;
730745

746+
#ifdef WITH_VALGRIND
747+
if (UNLIKELY(running_on_valgrind == -1))
748+
running_on_valgrind = RUNNING_ON_VALGRIND;
749+
if (UNLIKELY(running_on_valgrind))
750+
goto redirect;
751+
#endif
752+
731753
/*
732754
* Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
733755
* Most python internals blindly use a signed Py_ssize_t to track
@@ -927,6 +949,11 @@ PyObject_Free(void *p)
927949
if (p == NULL) /* free(NULL) has no effect */
928950
return;
929951

952+
#ifdef WITH_VALGRIND
953+
if (UNLIKELY(running_on_valgrind > 0))
954+
goto redirect;
955+
#endif
956+
930957
pool = POOL_ADDR(p);
931958
if (Py_ADDRESS_IN_RANGE(p, pool)) {
932959
/* We allocated this address. */
@@ -1121,6 +1148,9 @@ PyObject_Free(void *p)
11211148
return;
11221149
}
11231150

1151+
#ifdef WITH_VALGRIND
1152+
redirect:
1153+
#endif
11241154
/* We didn't allocate this address. */
11251155
free(p);
11261156
}
@@ -1150,6 +1180,12 @@ PyObject_Realloc(void *p, size_t nbytes)
11501180
if (nbytes > PY_SSIZE_T_MAX)
11511181
return NULL;
11521182

1183+
#ifdef WITH_VALGRIND
1184+
/* Treat running_on_valgrind == -1 the same as 0 */
1185+
if (UNLIKELY(running_on_valgrind > 0))
1186+
goto redirect;
1187+
#endif
1188+
11531189
pool = POOL_ADDR(p);
11541190
if (Py_ADDRESS_IN_RANGE(p, pool)) {
11551191
/* We're in charge of this block */
@@ -1177,6 +1213,9 @@ PyObject_Realloc(void *p, size_t nbytes)
11771213
}
11781214
return bp;
11791215
}
1216+
#ifdef WITH_VALGRIND
1217+
redirect:
1218+
#endif
11801219
/* We're not managing this block. If nbytes <=
11811220
* SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this
11821221
* block. However, if we do, we need to copy the valid data from

configure

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /bin/sh
2-
# From configure.in Revision: 76568 .
2+
# From configure.in Revision: 76636 .
33
# Guess values for system-dependent variables and create Makefiles.
44
# Generated by GNU Autoconf 2.61 for python 2.7.
55
#
@@ -1362,6 +1362,7 @@ Optional Packages:
13621362
--with(out)-doc-strings disable/enable documentation strings
13631363
--with(out)-tsc enable/disable timestamp counter profile
13641364
--with(out)-pymalloc disable/enable specialized mallocs
1365+
--with-valgrind Enable Valgrind support
13651366
--with-wctype-functions use wctype.h functions
13661367
--with-fpectl enable SIGFPE catching
13671368
--with-libm=STRING math library
@@ -17616,6 +17617,166 @@ fi
1761617617
{ echo "$as_me:$LINENO: result: $with_pymalloc" >&5
1761717618
echo "${ECHO_T}$with_pymalloc" >&6; }
1761817619

17620+
# Check for Valgrind support
17621+
{ echo "$as_me:$LINENO: checking for --with-valgrind" >&5
17622+
echo $ECHO_N "checking for --with-valgrind... $ECHO_C" >&6; }
17623+
17624+
# Check whether --with-valgrind was given.
17625+
if test "${with_valgrind+set}" = set; then
17626+
withval=$with_valgrind;
17627+
else
17628+
with_valgrind=no
17629+
fi
17630+
17631+
{ echo "$as_me:$LINENO: result: $with_valgrind" >&5
17632+
echo "${ECHO_T}$with_valgrind" >&6; }
17633+
if test "$with_valgrind" != no; then
17634+
if test "${ac_cv_header_valgrind_valgrind_h+set}" = set; then
17635+
{ echo "$as_me:$LINENO: checking for valgrind/valgrind.h" >&5
17636+
echo $ECHO_N "checking for valgrind/valgrind.h... $ECHO_C" >&6; }
17637+
if test "${ac_cv_header_valgrind_valgrind_h+set}" = set; then
17638+
echo $ECHO_N "(cached) $ECHO_C" >&6
17639+
fi
17640+
{ echo "$as_me:$LINENO: result: $ac_cv_header_valgrind_valgrind_h" >&5
17641+
echo "${ECHO_T}$ac_cv_header_valgrind_valgrind_h" >&6; }
17642+
else
17643+
# Is the header compilable?
17644+
{ echo "$as_me:$LINENO: checking valgrind/valgrind.h usability" >&5
17645+
echo $ECHO_N "checking valgrind/valgrind.h usability... $ECHO_C" >&6; }
17646+
cat >conftest.$ac_ext <<_ACEOF
17647+
/* confdefs.h. */
17648+
_ACEOF
17649+
cat confdefs.h >>conftest.$ac_ext
17650+
cat >>conftest.$ac_ext <<_ACEOF
17651+
/* end confdefs.h. */
17652+
$ac_includes_default
17653+
#include <valgrind/valgrind.h>
17654+
_ACEOF
17655+
rm -f conftest.$ac_objext
17656+
if { (ac_try="$ac_compile"
17657+
case "(($ac_try" in
17658+
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
17659+
*) ac_try_echo=$ac_try;;
17660+
esac
17661+
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
17662+
(eval "$ac_compile") 2>conftest.er1
17663+
ac_status=$?
17664+
grep -v '^ *+' conftest.er1 >conftest.err
17665+
rm -f conftest.er1
17666+
cat conftest.err >&5
17667+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
17668+
(exit $ac_status); } && {
17669+
test -z "$ac_c_werror_flag" ||
17670+
test ! -s conftest.err
17671+
} && test -s conftest.$ac_objext; then
17672+
ac_header_compiler=yes
17673+
else
17674+
echo "$as_me: failed program was:" >&5
17675+
sed 's/^/| /' conftest.$ac_ext >&5
17676+
17677+
ac_header_compiler=no
17678+
fi
17679+
17680+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
17681+
{ echo "$as_me:$LINENO: result: $ac_header_compiler" >&5
17682+
echo "${ECHO_T}$ac_header_compiler" >&6; }
17683+
17684+
# Is the header present?
17685+
{ echo "$as_me:$LINENO: checking valgrind/valgrind.h presence" >&5
17686+
echo $ECHO_N "checking valgrind/valgrind.h presence... $ECHO_C" >&6; }
17687+
cat >conftest.$ac_ext <<_ACEOF
17688+
/* confdefs.h. */
17689+
_ACEOF
17690+
cat confdefs.h >>conftest.$ac_ext
17691+
cat >>conftest.$ac_ext <<_ACEOF
17692+
/* end confdefs.h. */
17693+
#include <valgrind/valgrind.h>
17694+
_ACEOF
17695+
if { (ac_try="$ac_cpp conftest.$ac_ext"
17696+
case "(($ac_try" in
17697+
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
17698+
*) ac_try_echo=$ac_try;;
17699+
esac
17700+
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
17701+
(eval "$ac_cpp conftest.$ac_ext") 2>conftest.er1
17702+
ac_status=$?
17703+
grep -v '^ *+' conftest.er1 >conftest.err
17704+
rm -f conftest.er1
17705+
cat conftest.err >&5
17706+
echo "$as_me:$LINENO: \$? = $ac_status" >&5
17707+
(exit $ac_status); } >/dev/null && {
17708+
test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" ||
17709+
test ! -s conftest.err
17710+
}; then
17711+
ac_header_preproc=yes
17712+
else
17713+
echo "$as_me: failed program was:" >&5
17714+
sed 's/^/| /' conftest.$ac_ext >&5
17715+
17716+
ac_header_preproc=no
17717+
fi
17718+
17719+
rm -f conftest.err conftest.$ac_ext
17720+
{ echo "$as_me:$LINENO: result: $ac_header_preproc" >&5
17721+
echo "${ECHO_T}$ac_header_preproc" >&6; }
17722+
17723+
# So? What about this header?
17724+
case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in
17725+
yes:no: )
17726+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: accepted by the compiler, rejected by the preprocessor!" >&5
17727+
echo "$as_me: WARNING: valgrind/valgrind.h: accepted by the compiler, rejected by the preprocessor!" >&2;}
17728+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: proceeding with the compiler's result" >&5
17729+
echo "$as_me: WARNING: valgrind/valgrind.h: proceeding with the compiler's result" >&2;}
17730+
ac_header_preproc=yes
17731+
;;
17732+
no:yes:* )
17733+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: present but cannot be compiled" >&5
17734+
echo "$as_me: WARNING: valgrind/valgrind.h: present but cannot be compiled" >&2;}
17735+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: check for missing prerequisite headers?" >&5
17736+
echo "$as_me: WARNING: valgrind/valgrind.h: check for missing prerequisite headers?" >&2;}
17737+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: see the Autoconf documentation" >&5
17738+
echo "$as_me: WARNING: valgrind/valgrind.h: see the Autoconf documentation" >&2;}
17739+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: section \"Present But Cannot Be Compiled\"" >&5
17740+
echo "$as_me: WARNING: valgrind/valgrind.h: section \"Present But Cannot Be Compiled\"" >&2;}
17741+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: proceeding with the preprocessor's result" >&5
17742+
echo "$as_me: WARNING: valgrind/valgrind.h: proceeding with the preprocessor's result" >&2;}
17743+
{ echo "$as_me:$LINENO: WARNING: valgrind/valgrind.h: in the future, the compiler will take precedence" >&5
17744+
echo "$as_me: WARNING: valgrind/valgrind.h: in the future, the compiler will take precedence" >&2;}
17745+
( cat <<\_ASBOX
17746+
## -------------------------------------- ##
17747+
## Report this to http://bugs.python.org/ ##
17748+
## -------------------------------------- ##
17749+
_ASBOX
17750+
) | sed "s/^/$as_me: WARNING: /" >&2
17751+
;;
17752+
esac
17753+
{ echo "$as_me:$LINENO: checking for valgrind/valgrind.h" >&5
17754+
echo $ECHO_N "checking for valgrind/valgrind.h... $ECHO_C" >&6; }
17755+
if test "${ac_cv_header_valgrind_valgrind_h+set}" = set; then
17756+
echo $ECHO_N "(cached) $ECHO_C" >&6
17757+
else
17758+
ac_cv_header_valgrind_valgrind_h=$ac_header_preproc
17759+
fi
17760+
{ echo "$as_me:$LINENO: result: $ac_cv_header_valgrind_valgrind_h" >&5
17761+
echo "${ECHO_T}$ac_cv_header_valgrind_valgrind_h" >&6; }
17762+
17763+
fi
17764+
if test $ac_cv_header_valgrind_valgrind_h = yes; then
17765+
17766+
cat >>confdefs.h <<\_ACEOF
17767+
#define WITH_VALGRIND 1
17768+
_ACEOF
17769+
17770+
else
17771+
{ { echo "$as_me:$LINENO: error: Valgrind support requested but headers not available" >&5
17772+
echo "$as_me: error: Valgrind support requested but headers not available" >&2;}
17773+
{ (exit 1); exit 1; }; }
17774+
17775+
fi
17776+
17777+
17778+
fi
17779+
1761917780
# Check for --with-wctype-functions
1762017781
{ echo "$as_me:$LINENO: checking for --with-wctype-functions" >&5
1762117782
echo $ECHO_N "checking for --with-wctype-functions... $ECHO_C" >&6; }

configure.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,19 @@ then
24722472
fi
24732473
AC_MSG_RESULT($with_pymalloc)
24742474

2475+
# Check for Valgrind support
2476+
AC_MSG_CHECKING([for --with-valgrind])
2477+
AC_ARG_WITH([valgrind],
2478+
AC_HELP_STRING([--with-valgrind], [Enable Valgrind support]),,
2479+
with_valgrind=no)
2480+
AC_MSG_RESULT([$with_valgrind])
2481+
if test "$with_valgrind" != no; then
2482+
AC_CHECK_HEADER([valgrind/valgrind.h],
2483+
[AC_DEFINE([WITH_VALGRIND], 1, [Define if you want pymalloc to be disabled when running under valgrind])],
2484+
[AC_MSG_ERROR([Valgrind support requested but headers not available])]
2485+
)
2486+
fi
2487+
24752488
# Check for --with-wctype-functions
24762489
AC_MSG_CHECKING(for --with-wctype-functions)
24772490
AC_ARG_WITH(wctype-functions,

pyconfig.h.in

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,6 @@
301301
/* Define to 1 if you have the `getpeername' function. */
302302
#undef HAVE_GETPEERNAME
303303

304-
/* Define to 1 if you have the `initgroups' function. */
305-
#undef HAVE_INITGROUPS
306-
307304
/* Define to 1 if you have the `getpgid' function. */
308305
#undef HAVE_GETPGID
309306

@@ -358,6 +355,9 @@
358355
/* Define if you have the 'inet_pton' function. */
359356
#undef HAVE_INET_PTON
360357

358+
/* Define to 1 if you have the `initgroups' function. */
359+
#undef HAVE_INITGROUPS
360+
361361
/* Define to 1 if you have the <inttypes.h> header file. */
362362
#undef HAVE_INTTYPES_H
363363

@@ -1061,6 +1061,9 @@
10611061
/* Define to profile with the Pentium timestamp counter */
10621062
#undef WITH_TSC
10631063

1064+
/* Define if you want pymalloc to be disabled when running under valgrind */
1065+
#undef WITH_VALGRIND
1066+
10641067
/* Define to 1 if your processor stores words with the most significant byte
10651068
first (like Motorola and SPARC, unlike Intel and VAX). */
10661069
#undef WORDS_BIGENDIAN

0 commit comments

Comments
 (0)