From patchwork Thu Jan 21 21:35:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1430107 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DMG0l18ZPz9rx8 for ; Fri, 22 Jan 2021 08:36:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC3E8398B8B9; Thu, 21 Jan 2021 21:35:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 79DEB398B8B9 for ; Thu, 21 Jan 2021 21:35:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 79DEB398B8B9 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oliva@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 52500116C64; Thu, 21 Jan 2021 16:35:56 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id NjsPlFtpwial; Thu, 21 Jan 2021 16:35:56 -0500 (EST) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id 1C299116701; Thu, 21 Jan 2021 16:35:55 -0500 (EST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 10LLZmoN429545 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 21 Jan 2021 18:35:50 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: follow SSA defs for asan base Organization: Free thinker, does not speak for AdaCore Date: Thu, 21 Jan 2021 18:35:48 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jakub Jelinek , Kostya Serebryany , Dodji Seketeli Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Ada makes extensive use of nested functions, which turn all automatic variables of the enclosing function that are used in nested ones into members of an artificial FRAME record type. The address of a local variable is usually passed to asan marking functions without using a temporary. Taking the address of a member of FRAME within a nested function, however, is not regarded as a gimple val: while introducing FRAME variables, current_function_decl is always the outermost function, even while processing a nested function, so decl_address_invariant_p returns false for such ADDR_EXPRs. So, as automatic variables are moved into FRAME, any asan call that marks such a variable has its ADDR_EXPR replaced with a SSA_NAME set to the ADDR_EXPR of the FRAME member. asan_expand_mark_ifn was not prepared to deal with ADDR_EXPRs split out into SSA_NAMEs. This patch deals with such cases. [It does NOT deal with PHI nodes and whatnot. I'm not even sure it should. Maybe we want the ADDR_EXPR to be a gimple val instead, but this more conservative fix felt more appropriate for this stage.] Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog * asan.c (asan_expand_mark_ifn): Follow SSA_NAME defs for an ADDR_EXPR base. for gcc/testsuite/ChangeLog * gcc.dg/asan/nested-1.c: New. --- gcc/asan.c | 21 +++++++++++++++++++++ gcc/testsuite/gcc.dg/asan/nested-1.c | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/asan/nested-1.c diff --git a/gcc/asan.c b/gcc/asan.c index 89ecd99b18294..2d2fb97098b2f 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -3629,6 +3629,27 @@ asan_expand_mark_ifn (gimple_stmt_iterator *iter) bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON; tree base = gimple_call_arg (g, 1); + while (TREE_CODE (base) == SSA_NAME) + { + gimple *def = SSA_NAME_DEF_STMT (base); + if (!def) + break; + + if (!is_gimple_assign (def)) + break; + + if (!SINGLE_SSA_TREE_OPERAND (def, SSA_OP_DEF)) + break; + + if (gimple_num_ops (def) != 2) + break; + + if (gimple_expr_code (def) == ADDR_EXPR + || gimple_expr_code (def) == SSA_NAME) + base = gimple_assign_rhs1 (def); + else + break; + } gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR); tree decl = TREE_OPERAND (base, 0); diff --git a/gcc/testsuite/gcc.dg/asan/nested-1.c b/gcc/testsuite/gcc.dg/asan/nested-1.c new file mode 100644 index 0000000000000..87e842098077c --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/nested-1.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=address" } */ + +int f(int i) { + auto int h() { + int r; + int *p; + + { + int x[3]; + + auto int g() { + return x[i]; + } + + p = &r; + *p = g(); + } + + return *p; + } + + return h(); +}