From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 96864 invoked by alias); 19 Jun 2017 14:40:19 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 96478 invoked by uid 89); 19 Jun 2017 14:40:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=martins, Hx-languages-length:2989 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 19 Jun 2017 14:40:17 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3E1EA80C11; Mon, 19 Jun 2017 14:40:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3E1EA80C11 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jakub@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3E1EA80C11 Received: from tucnak.zalov.cz (ovpn-116-68.ams2.redhat.com [10.36.116.68]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D70F585D8E; Mon, 19 Jun 2017 14:40:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id v5JEeH8m005855; Mon, 19 Jun 2017 16:40:18 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id v5JEeGEU005854; Mon, 19 Jun 2017 16:40:16 +0200 Date: Mon, 19 Jun 2017 14:40:00 -0000 From: Jakub Jelinek To: Richard Biener , Martin =?utf-8?B?TGnFoWth?= Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix -fsanitize=undefined ubsan_encode_value ICE (PR sanitizer/81111) Message-ID: <20170619144016.GR2123@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes X-SW-Source: 2017-06/txt/msg01317.txt.bz2 Hi! Martin's recent patch that introduced sanitize_flags_p causes us to instrument operations even when current_function_decl is NULL. If it is valid constant expression it will be folded away soon, otherwise usually we emit a runtime initializer in the static ctors function for it. In any case, neither gimple_add_tmp_var that create_tmp_var calls normark_addressable actually work in that case, fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux plus bootstrapped/regtested with bootstrap-ubsan, ok for trunk? 2017-06-19 Jakub Jelinek PR sanitizer/81111 * ubsan.c (ubsan_encode_value): If current_function_decl is NULL, use create_tmp_var_raw instead of create_tmp_var, mark it addressable just by setting TREE_ADDRESSABLE on the result and use a TARGET_EXPR. * g++.dg/ubsan/pr81111.C: New test. --- gcc/ubsan.c.jj 2017-06-16 13:27:48.000000000 +0200 +++ gcc/ubsan.c 2017-06-16 16:28:29.099155949 +0200 @@ -145,9 +145,17 @@ ubsan_encode_value (tree t, bool in_expa { /* The reason for this is that we don't want to pessimize code by making vars unnecessarily addressable. */ - tree var = create_tmp_var (type); - tree tem = build2 (MODIFY_EXPR, void_type_node, var, t); - mark_addressable (var); + tree var; + if (current_function_decl) + { + var = create_tmp_var (type); + mark_addressable (var); + } + else + { + var = create_tmp_var_raw (type); + TREE_ADDRESSABLE (var) = 1; + } if (in_expand_p) { rtx mem @@ -158,8 +166,17 @@ ubsan_encode_value (tree t, bool in_expa expand_assignment (var, t, false); return build_fold_addr_expr (var); } - t = build_fold_addr_expr (var); - return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t); + if (current_function_decl) + { + tree tem = build2 (MODIFY_EXPR, void_type_node, var, t); + t = build_fold_addr_expr (var); + return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t); + } + else + { + var = build4 (TARGET_EXPR, type, var, t, NULL_TREE, NULL_TREE); + return build_fold_addr_expr (var); + } } else return build_fold_addr_expr (t); --- gcc/testsuite/g++.dg/ubsan/pr81111.C.jj 2017-06-16 15:39:57.752886010 +0200 +++ gcc/testsuite/g++.dg/ubsan/pr81111.C 2017-06-16 15:39:37.000000000 +0200 @@ -0,0 +1,45 @@ +// PR sanitizer/81111 +// { dg-do compile } +// { dg-options "-fsanitize=shift" } + +template +struct N +{ + static const V m = (((V)(-1) < 0) + ? (V)1 << (sizeof(V) * __CHAR_BIT__ - ((V)(-1) < 0)) + : (V) 0); +}; + +template +const V N::m; + +template +struct O +{ + static const V m = (V)1 << sizeof(V) * __CHAR_BIT__; +}; + +template +const V O::m; + +void +foo () +{ + N::m; + N::m; +#ifdef __SIZEOF_INT128__ + N<__int128>::m; + N::m; +#endif +} + +void +bar () +{ + O::m; + O::m; +#ifdef __SIZEOF_INT128__ + O<__int128>::m; + O::m; +#endif +} Jakub