Skip to content

Commit

Permalink
pythongh-96821: Add config option --with-strict-overflow (python#96823
Browse files Browse the repository at this point in the history
)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Shantanu <hauntsaninja@gmail.com>
  • Loading branch information
5 people authored and hugovk committed Mar 6, 2023
1 parent f2062a4 commit c9931ce
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ also be used to improve performance.

Enable C-level code profiling with ``gprof`` (disabled by default).

.. cmdoption:: --with-strict-overflow

Add ``-fstrict-overflow`` to the C compiler flags (by default we add
``-fno-strict-overflow`` instead).


.. _debug-build:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Explicitly mark C extension modules that need defined signed integer overflow,
and add a configure option :option:`--with-strict-overflow`.
Patch by Matthias Görgens and Shantanu Jain.
78 changes: 69 additions & 9 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 45 additions & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,45 @@ case $CC in
fi
esac

dnl Historically, some of our code assumed that signed integer overflow
dnl is defined behaviour via twos-complement.
dnl Set STRICT_OVERFLOW_CFLAGS and NO_STRICT_OVERFLOW_CFLAGS depending on compiler support.
dnl Pass the latter to modules that depend on such behaviour.
_SAVE_VAR([CFLAGS])
CFLAGS="-fstrict-overflow -fno-strict-overflow"
AC_CACHE_CHECK([if $CC supports -fstrict-overflow and -fno-strict-overflow],
[ac_cv_cc_supports_fstrict_overflow],
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([[]], [[]])],
[ac_cv_cc_supports_fstrict_overflow=yes],
[ac_cv_cc_supports_fstrict_overflow=no]
)
)
_RESTORE_VAR([CFLAGS])

AS_VAR_IF([ac_cv_cc_supports_fstrict_overflow], [yes],
[STRICT_OVERFLOW_CFLAGS="-fstrict-overflow"
NO_STRICT_OVERFLOW_CFLAGS="-fno-strict-overflow"],
[STRICT_OVERFLOW_CFLAGS=""
NO_STRICT_OVERFLOW_CFLAGS=""])

AC_MSG_CHECKING([for --with-strict-overflow])
AC_ARG_WITH([strict-overflow],
AS_HELP_STRING(
[--with-strict-overflow],
[if 'yes', add -fstrict-overflow to CFLAGS, else add -fno-strict-overflow (default is no)]
),
[
AS_VAR_IF(
[ac_cv_cc_supports_fstrict_overflow], [no],
[AC_MSG_WARN([--with-strict-overflow=yes requires a compiler that supports -fstrict-overflow])],
[]
)
],
[with_strict_overflow=no]
)
AC_MSG_RESULT([$with_strict_overflow])

# Check if CC supports -Og optimization level
_SAVE_VAR([CFLAGS])
CFLAGS="-Og"
Expand Down Expand Up @@ -2103,15 +2142,8 @@ if test "${OPT-unset}" = "unset"
then
case $GCC in
yes)
# For gcc 4.x we need to use -fwrapv so lets check if its supported
if "$CC" -v --help 2>/dev/null |grep -- -fwrapv > /dev/null; then
WRAP="-fwrapv"
fi

if test -n "${cc_is_clang}"
then
# Clang also needs -fwrapv
WRAP="-fwrapv"
# bpo-30104: disable strict aliasing to compile correctly dtoa.c,
# see Makefile.pre.in for more information
CFLAGS_ALIASING="-fno-strict-aliasing"
Expand All @@ -2122,7 +2154,7 @@ then
if test "$Py_DEBUG" = 'true' ; then
OPT="-g $PYDEBUG_CFLAGS -Wall"
else
OPT="-g $WRAP -O3 -Wall"
OPT="-g -O3 -Wall"
fi
;;
*)
Expand Down Expand Up @@ -2237,6 +2269,10 @@ AC_DEFUN([PY_CHECK_CC_WARNING], [
])

# tweak BASECFLAGS based on compiler and platform
AS_VAR_IF([with_strict_overflow], [yes],
[BASECFLAGS="$BASECFLAGS $STRICT_OVERFLOW_CFLAGS"],
[BASECFLAGS="$BASECFLAGS $NO_STRICT_OVERFLOW_CFLAGS"])

case $GCC in
yes)
CFLAGS_NODIST="$CFLAGS_NODIST -std=c11"
Expand Down Expand Up @@ -7213,7 +7249,7 @@ PY_STDLIB_MOD([_crypt],
[$LIBCRYPT_CFLAGS], [$LIBCRYPT_LIBS])
PY_STDLIB_MOD([_ctypes],
[], [test "$have_libffi" = yes],
[$LIBFFI_CFLAGS], [$LIBFFI_LIBS])
[$NO_STRICT_OVERFLOW_CFLAGS $LIBFFI_CFLAGS], [$LIBFFI_LIBS])
PY_STDLIB_MOD([_curses],
[], [test "$have_curses" != "no"],
[$CURSES_CFLAGS], [$CURSES_LIBS]
Expand Down

0 comments on commit c9931ce

Please sign in to comment.