From patchwork Wed Jul 18 15:33:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 945768 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-481810-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Uns1CxaP"; dkim-atps=neutral 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 41W1QJ5LGjz9s3R for ; Thu, 19 Jul 2018 01:33:43 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=augVVLUxd/h/vzcn3Yos+4fdOrSJO c9HmTHq51zsSASKR5kBMrvdqMARXutqrvUbCxOkfmciAqqHYciyenyko5HSxNbln gSDCOJsw3rBx4qqlJ6bUUfygkUI+/84ndgV5+be6DO8boFd8/CuwJpdriJgmz2Ah oQlwo2YFVpfiR8= 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=ZuWL3aO9vXCSzDPcpzVmqihHqLI=; b=Uns 1CxaPNc6I+sbBcO28dlHQkwBoNpZfewq0PPb8oiDhdAcBTsgSUPZzwTEnSNM6tvY SAOlCOg/HXBN8hvT3Ep2xZu25xfQKXQMVeaeKj7hx8efvL+pCSeGWkhe8EpR8Kl6 jsd7k2gVMPHatMZwFgqmVoXDgnC1cJDZt3GWErMk= Received: (qmail 45172 invoked by alias); 18 Jul 2018 15:33:35 -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 45141 invoked by uid 89); 18 Jul 2018 15:33:34 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_STOCKGEN, NO_DNS_FOR_FROM autolearn=ham version=3.3.2 spammy=262723, 262716, METHOD_TYPE, 58618 X-HELO: mga01.intel.com Received: from mga01.intel.com (HELO mga01.intel.com) (192.55.52.88) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Jul 2018 15:33:32 +0000 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jul 2018 08:33:30 -0700 Received: from gnu-4.sc.intel.com ([172.25.70.231]) by orsmga003.jf.intel.com with ESMTP; 18 Jul 2018 08:33:30 -0700 Received: by gnu-4.sc.intel.com (Postfix, from userid 1000) id 19CD21C015E; Wed, 18 Jul 2018 08:33:30 -0700 (PDT) Date: Wed, 18 Jul 2018 08:33:30 -0700 From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Jan Hubicka , Uros Bizjak Subject: [PATCH 2/3] i386: Change indirect_return to function type attribute Message-ID: <20180718153329.GA13951@intel.com> Reply-To: "H.J. Lu" MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.0 (2018-05-17) In struct ucontext; typedef struct ucontext ucontext_t; extern int (*bar) (ucontext_t *__restrict __oucp, const ucontext_t *__restrict __ucp) __attribute__((__indirect_return__)); extern int res; void foo (ucontext_t *oucp, ucontext_t *ucp) { res = bar (oucp, ucp); } bar() may return via indirect branch. This patch changes indirect_return to type attribute to allow indirect_return attribute on variable or type of function pointer so that ENDBR can be inserted after call to bar(). Tested on i386 and x86-64. OK for trunk? Thanks. H.J. --- gcc/ PR target/86560 * config/i386/i386.c (rest_of_insert_endbranch): Lookup indirect_return as function type attribute. (ix86_attribute_table): Change indirect_return to function type attribute. * doc/extend.texi: Update indirect_return attribute. gcc/testsuite/ PR target/86560 * gcc.target/i386/pr86560-1.c: New test. * gcc.target/i386/pr86560-2.c: Likewise. * gcc.target/i386/pr86560-3.c: Likewise. --- gcc/config/i386/i386.c | 23 +++++++++++++++-------- gcc/doc/extend.texi | 5 +++-- gcc/testsuite/gcc.target/i386/pr86560-1.c | 16 ++++++++++++++++ gcc/testsuite/gcc.target/i386/pr86560-2.c | 16 ++++++++++++++++ gcc/testsuite/gcc.target/i386/pr86560-3.c | 17 +++++++++++++++++ 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr86560-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr86560-2.c create mode 100644 gcc/testsuite/gcc.target/i386/pr86560-3.c diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index aec739c3974..ac27248370b 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -2627,16 +2627,23 @@ rest_of_insert_endbranch (void) { rtx call = get_call_rtx_from (insn); rtx fnaddr = XEXP (call, 0); + tree fndecl = NULL_TREE; /* Also generate ENDBRANCH for non-tail call which may return via indirect branch. */ - if (MEM_P (fnaddr) - && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF) + if (GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF) + fndecl = SYMBOL_REF_DECL (XEXP (fnaddr, 0)); + if (fndecl == NULL_TREE) + fndecl = MEM_EXPR (fnaddr); + if (fndecl + && TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE + && TREE_CODE (TREE_TYPE (fndecl)) != METHOD_TYPE) + fndecl = NULL_TREE; + if (fndecl && TYPE_ARG_TYPES (TREE_TYPE (fndecl))) { - tree fndecl = SYMBOL_REF_DECL (XEXP (fnaddr, 0)); - if (fndecl - && lookup_attribute ("indirect_return", - DECL_ATTRIBUTES (fndecl))) + tree fntype = TREE_TYPE (fndecl); + if (lookup_attribute ("indirect_return", + TYPE_ATTRIBUTES (fntype))) need_endbr = true; } } @@ -46101,8 +46108,8 @@ static const struct attribute_spec ix86_attribute_table[] = ix86_handle_fndecl_attribute, NULL }, { "function_return", 1, 1, true, false, false, false, ix86_handle_fndecl_attribute, NULL }, - { "indirect_return", 0, 0, true, false, false, false, - ix86_handle_fndecl_attribute, NULL }, + { "indirect_return", 0, 0, false, true, true, false, + NULL, NULL }, /* End element. */ { NULL, 0, 0, false, false, false, false, NULL, NULL } diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 8b4d3fd9de3..edeaec6d872 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -5861,8 +5861,9 @@ foo (void) @item indirect_return @cindex @code{indirect_return} function attribute, x86 -The @code{indirect_return} attribute on a function is used to inform -the compiler that the function may return via indirect branch. +The @code{indirect_return} attribute can be applied to a function, +as well as variable or type of function pointer to inform the +compiler that the function may return via indirect branch. @end table diff --git a/gcc/testsuite/gcc.target/i386/pr86560-1.c b/gcc/testsuite/gcc.target/i386/pr86560-1.c new file mode 100644 index 00000000000..a2b702695c5 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr86560-1.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcf-protection" } */ +/* { dg-final { scan-assembler-times {\mendbr} 2 } } */ + +struct ucontext; + +extern int (*bar) (struct ucontext *) + __attribute__((__indirect_return__)); + +extern int res; + +void +foo (struct ucontext *oucp) +{ + res = bar (oucp); +} diff --git a/gcc/testsuite/gcc.target/i386/pr86560-2.c b/gcc/testsuite/gcc.target/i386/pr86560-2.c new file mode 100644 index 00000000000..6f01b385afd --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr86560-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcf-protection" } */ +/* { dg-final { scan-assembler-times {\mendbr} 2 } } */ + +struct ucontext; + +typedef int (*bar_p) (struct ucontext *) + __attribute__((__indirect_return__)); + +extern int res; + +void +foo (bar_p bar, struct ucontext *oucp) +{ + res = bar (oucp); +} diff --git a/gcc/testsuite/gcc.target/i386/pr86560-3.c b/gcc/testsuite/gcc.target/i386/pr86560-3.c new file mode 100644 index 00000000000..05328e24509 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr86560-3.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcf-protection" } */ +/* { dg-final { scan-assembler-times {\mendbr} 2 } } */ + +struct ucontext; + +extern int (*bar) (struct ucontext *); + +extern int res; + +void +foo (struct ucontext *oucp) +{ + int (*f) (struct ucontext *) __attribute__((__indirect_return__)) + = bar; + res = f (oucp); +}