From patchwork Wed Jan 18 07:54:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Ostapenko X-Patchwork-Id: 716537 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 3v3K575RfDz9t1Q for ; Wed, 18 Jan 2017 18:55:06 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="YVVpWWJw"; 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:from :subject:to:cc:message-id:date:mime-version:content-type :references; q=dns; s=default; b=LP9L3+DbnfBQi5s2yzacn4BcVY+HXmC oELYNkOs9kstYb+uCtClH/zOyQHpvaWD9jgHIgBPrzpiV48cmZnIlfE1yXguxEDq nvVPFtMxmYWuorFKggkctfGLfiPnV466HU2hbCqVkTNiUZfYG91pmC59TtpITMEB MONQpQQG/zIs= 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:from :subject:to:cc:message-id:date:mime-version:content-type :references; s=default; bh=UaxFt/3KCLMUza5zHclwEbhnUtw=; b=YVVpW WJw5TAG0yixIZqOKMRED8DlykxCCFPcyHJgBiLQiCzC6GBqKyVJjkJCVNksA7p8n CmZnZMq1y+NqWs+8GOLRAxp6+H4ygJWJXwNJxfQOgtmqkw6WnKPGgEaDyTZ86BpG 68E9O+ATR4TDbn+kkl0NaaxxeBgIcZYDhokAF8= Received: (qmail 83003 invoked by alias); 18 Jan 2017 07:54:57 -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 82984 invoked by uid 89); 18 Jan 2017 07:54:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.1 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=resolutions, Hx-languages-length:6099, fiasco, HContent-type:multipart X-HELO: mailout3.w1.samsung.com Received: from mailout3.w1.samsung.com (HELO mailout3.w1.samsung.com) (210.118.77.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Jan 2017 07:54:45 +0000 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OJY00J09TZ4XG70@mailout3.w1.samsung.com> for gcc-patches@gcc.gnu.org; Wed, 18 Jan 2017 07:54:40 +0000 (GMT) Received: from eusmges5.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170118075439eucas1p13c01a5da93d9f36a248b86956005a34a~azoDO7zDu1960319603eucas1p1Q; Wed, 18 Jan 2017 07:54:39 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges5.samsung.com (EUCPMTA) with SMTP id 54.1A.17477.F3F1F785; Wed, 18 Jan 2017 07:54:39 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170118075439eucas1p1f216c219e19fc0e5f19832a382b15af7~azoCluDfj1896318963eucas1p1Y; Wed, 18 Jan 2017 07:54:39 +0000 (GMT) Received: from eusync3.samsung.com ( [203.254.199.213]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id C3.7C.10233.93F1F785; Wed, 18 Jan 2017 07:54:33 +0000 (GMT) Received: from [106.109.129.18] by eusync3.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OJY00DNQTZ2PV70@eusync3.samsung.com>; Wed, 18 Jan 2017 07:54:38 +0000 (GMT) From: Maxim Ostapenko Subject: [PATCH][PR lto/79061] Fix LTO plus ASAN fails with "AddressSanitizer: initialization-order-fiasco". To: GCC Patches Cc: Jakub Jelinek , Richard Biener Message-id: <587F1F3A.6050208@samsung.com> Date: Wed, 18 Jan 2017 10:54:34 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-version: 1.0 Content-type: multipart/mixed; boundary=------------000107040105080409030506 X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170118075439eucas1p1f216c219e19fc0e5f19832a382b15af7 X-Msg-Generator: CA X-Sender-IP: 182.198.249.180 X-Local-Sender: =?UTF-8?B?TWFrc2ltIE9zdGFwZW5rbxtTUlItU1cgVG9vbHMgTGFiGw==?= =?UTF-8?B?7IK87ISx7KCE7J6QG0VuZ2luZWVy?= X-Global-Sender: =?UTF-8?B?TWF4aW0gT3N0YXBlbmtvG1NSUi1TVyBUb29scyBMYWIbU2Ft?= =?UTF-8?B?c3VuZ8KgRWxlY3Ryb25pY3MbRW5naW5lZXI=?= X-Sender-Code: =?UTF-8?B?QzEwG0NJU0hRG0MxMEdEMDFHRDAxMDE1Nw==?= CMS-TYPE: 201P X-HopCount: 7 X-CMS-RootMailID: 20170118075439eucas1p1f216c219e19fc0e5f19832a382b15af7 X-RootMTR: 20170118075439eucas1p1f216c219e19fc0e5f19832a382b15af7 References: X-IsSubscribed: yes Hi, as was figured out in PR LTO + ASan raises false initialization order fiasco alarm due to in LTO case main_input_filename doesn't match module name passed to __asan_before_dynamic_init. Following Jakub's suggestion I used TRANSLATION_UNIT_DECL for corresponding globals to overcome this issue (I needed to create a source location for each TRANSLATION_UNIT_DECL). However, when testing, I hit on a nasty issue: for some reason linemap_ordinary_map_lookup, called from lto_output_location for given TRANSLATION_UNIT_DECL, hit an assert: [...] linemap_assert (line >= MAP_START_LOCATION (result)); return result; } due to line == 2. After some investigation I noticed that source locations are propagated through location cache that can be partially invalidated by lto_location_cache::revert_location_cache call. And that was my case: after adding source location for TRANSLATION_UNIT_DECL into location cache, it was reverted by calling lto_location_cache::revert_location_cache from unify_scc before it was accepted: static void lto_read_decls (struct lto_file_decl_data *decl_data, const void *data, vec resolutions) { [...] /* Try to unify the SCC with already existing ones. */ if (!flag_ltrans && unify_scc (data_in, from, len, scc_entry_len, scc_hash)) continue; For now I can overcome it by calling location_cache.accept_location_cache for TRANSLATION_UNIT_DECL, but I wonder if more reliable fix is possible. Attached patch fixes the issue mentioned in PR and passes regression testing and LTO bootstrap on x86_64-unknown-linux-gnu. Could you please take a look on it? -Maxim gcc/lto/ChangeLog: 2017-01-17 Maxim Ostapenko * lto.c (lto_read_decls): accept location cache for TRANSLATION_UNIT_DECL. gcc/testsuite/ChangeLog: 2017-01-17 Maxim Ostapenko * gcc.dg/cpp/mi1.c: Adjust testcase. * gcc.dg/pch/cpp-3.c: Likewise. gcc/ChangeLog: 2017-01-17 Maxim Ostapenko * asan.c (get_translation_unit_decl): New function. (asan_add_global): Extract modules file name from globals TRANSLATION_UNIT_DECL in lto mode. * tree.c (build_translation_unit_decl): Add source location for newly built TRANSLATION_UNIT_DECL. diff --git a/gcc/asan.c b/gcc/asan.c index 7450044..9a59fe4 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -2372,6 +2372,22 @@ asan_needs_odr_indicator_p (tree decl) && TREE_PUBLIC (decl)); } +/* For given DECL return its corresponding TRANSLATION_UNIT_DECL. */ + +static const_tree +get_translation_unit_decl (tree decl) +{ + const_tree context = decl; + while (context && TREE_CODE (context) != TRANSLATION_UNIT_DECL) + { + if (TREE_CODE (context) == BLOCK) + context = BLOCK_SUPERCONTEXT (context); + else + context = get_containing_scope (context); + } + return context; +} + /* Append description of a single global DECL into vector V. TYPE is __asan_global struct type as returned by asan_global_struct. */ @@ -2391,7 +2407,14 @@ asan_add_global (tree decl, tree type, vec *v) pp_string (&asan_pp, ""); str_cst = asan_pp_string (&asan_pp); - pp_string (&module_name_pp, main_input_filename); + const char *filename = main_input_filename; + if (in_lto_p) + { + const_tree translation_unit_decl = get_translation_unit_decl (decl); + if (translation_unit_decl) + filename = DECL_SOURCE_FILE (translation_unit_decl); + } + pp_string (&module_name_pp, filename); module_name_cst = asan_pp_string (&module_name_pp); if (asan_needs_local_alias (decl)) diff --git a/gcc/lto/lto.c b/gcc/lto/lto.c index d77d85d..c65e7cd 100644 --- a/gcc/lto/lto.c +++ b/gcc/lto/lto.c @@ -1707,7 +1707,13 @@ lto_read_decls (struct lto_file_decl_data *decl_data, const void *data, && (TREE_CODE (first) == IDENTIFIER_NODE || TREE_CODE (first) == INTEGER_CST || TREE_CODE (first) == TRANSLATION_UNIT_DECL)) - continue; + { + /* For TRANSLATION_UNIT_DECL we need to accept location cache now + to avoid possible reverting during following unify_scc call. */ + if (TREE_CODE (first) == TRANSLATION_UNIT_DECL) + data_in->location_cache.accept_location_cache (); + continue; + } /* Try to unify the SCC with already existing ones. */ if (!flag_ltrans diff --git a/gcc/testsuite/gcc.dg/cpp/mi1.c b/gcc/testsuite/gcc.dg/cpp/mi1.c index 0cfedad..9817431 100644 --- a/gcc/testsuite/gcc.dg/cpp/mi1.c +++ b/gcc/testsuite/gcc.dg/cpp/mi1.c @@ -13,7 +13,7 @@ /* { dg-do compile } { dg-options "-H" } - { dg-message "mi1c\.h\n\[^\n\]*mi1cc\.h\n\[^\n\]*mi1nd\.h\n\[^\n\]*mi1ndp\.h\n\[^\n\]*mi1x\.h" "redundant include check" { target *-*-* } 0 } */ + { dg-message "mi1c\.h\n\[^\n\]*mi1cc\.h\n\[^\n\]*mi1nd\.h\n\[^\n\]*mi1ndp\.h\n\[^\n\]*mi1x\.h\n\[^\n\]*mi1\.c" "redundant include check" { target *-*-* } 0 } */ #include "mi1c.h" #include "mi1c.h" diff --git a/gcc/testsuite/gcc.dg/pch/cpp-3.c b/gcc/testsuite/gcc.dg/pch/cpp-3.c index 25b5ca4..d635706 100644 --- a/gcc/testsuite/gcc.dg/pch/cpp-3.c +++ b/gcc/testsuite/gcc.dg/pch/cpp-3.c @@ -1,7 +1,7 @@ /* PR preprocessor/36649 */ /* { dg-do compile } */ /* { dg-options "-H -I." } */ -/* { dg-message "cpp-3.h\[^\n\]*(\n\[^\n\]*cpp-3.c)?\n\[^\n\]*cpp-3a.h\n\[^\n\]*cpp-3b.h" "" { target *-*-* } 0 } */ +/* { dg-message "cpp-3.h\[^\n\]*(\n\[^\n\]*cpp-3.c)?\n\[^\n\]*cpp-3a.h\n\[^\n\]*cpp-3b.h\n\[^\n\]*cpp-3.c" "" { target *-*-* } 0 } */ #include "cpp-3.h" #include "cpp-3a.h" diff --git a/gcc/tree.c b/gcc/tree.c index cffa36d..59fe8d4 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -4758,7 +4758,10 @@ vec *all_translation_units; tree build_translation_unit_decl (tree name) { - tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL, + linemap_add (line_table, LC_ENTER, false, main_input_filename, 1); + location_t loc = linemap_line_start (line_table, 1, 0); + linemap_add (line_table, LC_LEAVE, false, NULL, 0); + tree tu = build_decl (loc, TRANSLATION_UNIT_DECL, name, NULL_TREE); TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name; vec_safe_push (all_translation_units, tu);