bpo-36618: Add -fmax-type-align=8 flag for clang (GH-12809)

Add -fmax-type-align=8 to CFLAGS when clang compiler is detected.

The pymalloc memory allocator aligns memory on 8 bytes. On x86-64,
clang expects alignment on 16 bytes by default and so uses MOVAPS
instruction which can lead to segmentation fault. Instruct clang that
Python is limited to alignemnt on 8 bytes to use MOVUPS instruction
instead: slower but don't trigger a SIGSEGV if the memory is not
aligned on 16 bytes.

Sadly, the flag must be expected to CFLAGS and not just
CFLAGS_NODIST, since third party C extensions can have the same
issue.
This commit is contained in:
Victor Stinner 2019-04-12 21:27:37 +02:00 committed by GitHub
parent 606c66a17f
commit 23a683adf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 26 deletions

View File

@ -0,0 +1,8 @@
Add ``-fmax-type-align=8`` to CFLAGS when clang compiler is detected. The
pymalloc memory allocator aligns memory on 8 bytes. On x86-64, clang expects
alignment on 16 bytes by default and so uses MOVAPS instruction which can
lead to segmentation fault. Instruct clang that Python is limited to
alignemnt on 8 bytes to use MOVUPS instruction instead: slower but don't
trigger a SIGSEGV if the memory is not aligned on 16 bytes. Sadly, the flag
must be expected to ``CFLAGS`` and not just ``CFLAGS_NODIST``, since third
party C extensions can have the same issue.

42
configure vendored
View File

@ -6813,19 +6813,6 @@ esac
# compiler and platform. BASECFLAGS tweaks need to be made even if the # compiler and platform. BASECFLAGS tweaks need to be made even if the
# user set OPT. # user set OPT.
# tweak OPT based on compiler and platform, only if the user didn't set
# it on the command line
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
case $CC in case $CC in
*clang*) *clang*)
cc_is_clang=1 cc_is_clang=1
@ -6839,6 +6826,19 @@ then
fi fi
esac esac
# tweak OPT based on compiler and platform, only if the user didn't set
# it on the command line
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}" if test -n "${cc_is_clang}"
then then
# Clang also needs -fwrapv # Clang also needs -fwrapv
@ -6879,6 +6879,21 @@ then
esac esac
fi fi
if test -n "${cc_is_clang}"
then
# bpo-36618: Add -fmax-type-align=8 to CFLAGS when clang compiler is
# detected. The pymalloc memory allocator aligns memory on 8 bytes. On
# x86-64, clang expects alignment on 16 bytes by default and so uses MOVAPS
# instruction which can lead to segmentation fault. Instruct clang that
# Python is limited to alignemnt on 8 bytes to use MOVUPS instruction
# instead: slower but don't trigger a SIGSEGV if the memory is not aligned
# on 16 bytes.
#
# Sadly, the flag must be expected to CFLAGS and not just CFLAGS_NODIST,
# since third party C extensions can have the same issue.
CFLAGS="$CFLAGS -fmax-type-align=8"
fi
@ -10200,6 +10215,7 @@ fi
if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
if test -n "$ac_tool_prefix"; then if test -n "$ac_tool_prefix"; then
# Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args. # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args.

View File

@ -1464,19 +1464,6 @@ esac
# compiler and platform. BASECFLAGS tweaks need to be made even if the # compiler and platform. BASECFLAGS tweaks need to be made even if the
# user set OPT. # user set OPT.
# tweak OPT based on compiler and platform, only if the user didn't set
# it on the command line
AC_SUBST(OPT)
AC_SUBST(CFLAGS_ALIASING)
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
case $CC in case $CC in
*clang*) *clang*)
cc_is_clang=1 cc_is_clang=1
@ -1490,6 +1477,19 @@ then
fi fi
esac esac
# tweak OPT based on compiler and platform, only if the user didn't set
# it on the command line
AC_SUBST(OPT)
AC_SUBST(CFLAGS_ALIASING)
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}" if test -n "${cc_is_clang}"
then then
# Clang also needs -fwrapv # Clang also needs -fwrapv
@ -1530,6 +1530,21 @@ then
esac esac
fi fi
if test -n "${cc_is_clang}"
then
# bpo-36618: Add -fmax-type-align=8 to CFLAGS when clang compiler is
# detected. The pymalloc memory allocator aligns memory on 8 bytes. On
# x86-64, clang expects alignment on 16 bytes by default and so uses MOVAPS
# instruction which can lead to segmentation fault. Instruct clang that
# Python is limited to alignemnt on 8 bytes to use MOVUPS instruction
# instead: slower but don't trigger a SIGSEGV if the memory is not aligned
# on 16 bytes.
#
# Sadly, the flag must be expected to CFLAGS and not just CFLAGS_NODIST,
# since third party C extensions can have the same issue.
CFLAGS="$CFLAGS -fmax-type-align=8"
fi
AC_SUBST(BASECFLAGS) AC_SUBST(BASECFLAGS)
AC_SUBST(CFLAGS_NODIST) AC_SUBST(CFLAGS_NODIST)
AC_SUBST(LDFLAGS_NODIST) AC_SUBST(LDFLAGS_NODIST)