From patchwork Wed Jul 29 13:57:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulrich Weigand X-Patchwork-Id: 501728 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5603A14030D for ; Wed, 29 Jul 2015 23:57:44 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=h8dqG2S1; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:date:from:cc:mime-version:content-type :content-transfer-encoding:message-id; q=dns; s=default; b=Vzmw9 8j9buEshTxX+zuMit4tPbJag3IVD5TcfkpFJA2CoXve64kNsXmAznpeI7cGvdR8R dEYDHQJ99q6a/rsLGGun5PWR1q7EiU49uAD8oK0roXiiKQdNJBro9QqMO2ZmUjgv QLdg1DGGFGa9EAp6qDVc5Pz9oO5NFJ7yXcIWb8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:date:from:cc:mime-version:content-type :content-transfer-encoding:message-id; s=default; bh=dN0KAU+V0JI AAoD1yGTaRf5UmoE=; b=h8dqG2S1Go/W+f0TZzjvSDLtJUIe70xoFmTyeRTkUU3 sWAQlzBdiLdv8txYCsM/EoK9wYHKsGpG9B0T/ptyCCFDkoxTZ++Ec+/oYtfJG13b HHe3fJV9pL4LusA9RzQAQ7l1F9JVpoMU5zibB0WGrJS1os73NjnPDipX2s/liVxM = Received: (qmail 87528 invoked by alias); 29 Jul 2015 13:57:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 87517 invoked by uid 89); 29 Jul 2015 13:57:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: e06smtp14.uk.ibm.com Received: from e06smtp14.uk.ibm.com (HELO e06smtp14.uk.ibm.com) (195.75.94.110) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Wed, 29 Jul 2015 13:57:35 +0000 Received: from /spool/local by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 29 Jul 2015 14:57:32 +0100 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 29 Jul 2015 14:57:31 +0100 X-MailFrom: uweigand@de.ibm.com X-RcptTo: gcc-patches@gcc.gnu.org Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id D5CFC17D8024 for ; Wed, 29 Jul 2015 14:58:56 +0100 (BST) Received: from d06av10.portsmouth.uk.ibm.com (d06av10.portsmouth.uk.ibm.com [9.149.37.251]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t6TDvUgB25821224 for ; Wed, 29 Jul 2015 13:57:30 GMT Received: from d06av10.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av10.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t6TDvUS7012484 for ; Wed, 29 Jul 2015 07:57:30 -0600 Received: from oc7340732750.ibm.com (dyn-9-152-213-24.boeblingen.de.ibm.com [9.152.213.24]) by d06av10.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t6TDvU51012478; Wed, 29 Jul 2015 07:57:30 -0600 Received: by oc7340732750.ibm.com (Postfix, from userid 500) id 568AAB045; Wed, 29 Jul 2015 15:57:30 +0200 (CEST) Subject: [PATCH] Work around host compiler placement new aliasing bug To: gcc-patches@gcc.gnu.org Date: Wed, 29 Jul 2015 15:57:30 +0200 (CEST) From: "Ulrich Weigand" Cc: richard.guenther@gmail.com, mliska@suse.cz MIME-Version: 1.0 Message-Id: <20150729135730.568AAB045@oc7340732750.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15072913-0017-0000-0000-000004E46753 Hello, this patch is a workaround for the problem discussed here: https://gcc.gnu.org/ml/gcc-patches/2015-07/msg01597.html The problem is that the new pool allocator code relies on C++ aliasing rules related to placement new (basically, that placement new changes the dynamic type of the referenced memory). GCC compilers prior to version 4.3 did not implement this rule correctly (PR 29286). When building current GCC with a host compiler that is affected by this bug, and we build with optimization enabled (this typically only happens when building a cross-compiler), the resulting compiler binary may be miscompiled. The patch below attempts to detect this situation by checking whether the host compiler is a version of GCC prior to 4.3 (but stil accepts the -fno-strict-aliasing flag). If so, -fno-strict-aliasing is added to the flags when building the compiler binary. Tested on i686-linux, and when building an SPU cross-compiler using a gcc 4.1 powerpc64-linux host compiler. OK for mainline? Bye, Ulrich gcc/ChangeLog: * configure.ac: Set aliasing_flags to -fno-strict-aliasing if the host compiler is affected by placement new aliasing bug. * configure: Regenerate. * Makefile.in (ALIASING_FLAGS): New variable. (ALL_CXXFLAGS): Add $(ALIASING_FLAGS). Index: gcc/configure =================================================================== --- gcc/configure (revision 226312) +++ gcc/configure (working copy) @@ -789,6 +789,7 @@ c_strict_warn strict_warn c_loose_warn loose_warn +aliasing_flags CPP EGREP GREP @@ -6526,6 +6527,42 @@ fi rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext fi +# Check whether compiler is affected by placement new aliasing bug (PR 29286). +# If the host compiler is affected by the bug, and we build with optimization +# enabled (which happens e.g. when cross-compiling), the pool allocator may +# get miscompiled. Use -fno-strict-aliasing to work around this problem. +# Since there is no reliable feature check for the presence of this bug, +# we simply use a GCC version number check. (This should never trigger for +# stages 2 or 3 of a native bootstrap.) +aliasing_flags= +if test "$GCC" = yes; then + saved_CXXFLAGS="$CXXFLAGS" + + # The following test compilation will succeed if and only if $CXX accepts + # -fno-strict-aliasing *and* is older than GCC 4.3. + CXXFLAGS="$CXXFLAGS -fno-strict-aliasing" + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX is affected by placement new aliasing bug" >&5 +$as_echo_n "checking whether $CXX is affected by placement new aliasing bug... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#error compiler not affected by placement new aliasing bug +#endif + +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; }; aliasing_flags='-fno-strict-aliasing' +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + + CXXFLAGS="$saved_CXXFLAGS" +fi + @@ -18301,7 +18338,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 18304 "configure" +#line 18341 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -18407,7 +18444,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 18410 "configure" +#line 18447 "configure" #include "confdefs.h" #if HAVE_DLFCN_H Index: gcc/configure.ac =================================================================== --- gcc/configure.ac (revision 226312) +++ gcc/configure.ac (working copy) @@ -416,6 +416,32 @@ struct X { typedef long long ]], [[X::t x;]])],[],[AC_MSG_ERROR([error verifying int64_t uses long long])]) fi +# Check whether compiler is affected by placement new aliasing bug (PR 29286). +# If the host compiler is affected by the bug, and we build with optimization +# enabled (which happens e.g. when cross-compiling), the pool allocator may +# get miscompiled. Use -fno-strict-aliasing to work around this problem. +# Since there is no reliable feature check for the presence of this bug, +# we simply use a GCC version number check. (This should never trigger for +# stages 2 or 3 of a native bootstrap.) +aliasing_flags= +if test "$GCC" = yes; then + saved_CXXFLAGS="$CXXFLAGS" + + # The following test compilation will succeed if and only if $CXX accepts + # -fno-strict-aliasing *and* is older than GCC 4.3. + CXXFLAGS="$CXXFLAGS -fno-strict-aliasing" + AC_MSG_CHECKING([whether $CXX is affected by placement new aliasing bug]) + AC_COMPILE_IFELSE([ +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#error compiler not affected by placement new aliasing bug +#endif +], + [AC_MSG_RESULT([yes]); aliasing_flags='-fno-strict-aliasing'], + [AC_MSG_RESULT([no])]) + + CXXFLAGS="$saved_CXXFLAGS" +fi +AC_SUBST(aliasing_flags) Index: gcc/Makefile.in =================================================================== --- gcc/Makefile.in (revision 226312) +++ gcc/Makefile.in (working copy) @@ -180,6 +180,8 @@ NOCOMMON_FLAG = @nocommon_flag@ NOEXCEPTION_FLAGS = @noexception_flags@ +ALIASING_FLAGS = @aliasing_flags@ + # This is set by --disable-maintainer-mode (default) to "#" # FIXME: 'MAINT' will always be set to an empty string, no matter if # --disable-maintainer-mode is used or not. This is because the @@ -986,7 +988,8 @@ ALL_CFLAGS = $(T_CFLAGS) $(CFLAGS-$@) \ # The C++ version. ALL_CXXFLAGS = $(T_CFLAGS) $(CFLAGS-$@) $(CXXFLAGS) $(INTERNAL_CFLAGS) \ - $(COVERAGE_FLAGS) $(NOEXCEPTION_FLAGS) $(WARN_CXXFLAGS) @DEFS@ + $(COVERAGE_FLAGS) $(ALIASING_FLAGS) $(NOEXCEPTION_FLAGS) \ + $(WARN_CXXFLAGS) @DEFS@ # Likewise. Put INCLUDES at the beginning: this way, if some autoconf macro # puts -I options in CPPFLAGS, our include files in the srcdir will always