From patchwork Tue Dec 2 13:33:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Enkovich X-Patchwork-Id: 416874 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 49CDF14017B for ; Wed, 3 Dec 2014 00:34:13 +1100 (AEDT) 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:subject:message-id:mime-version:content-type; q=dns; s= default; b=q+hSvqyC/0PjX0+ynwUYnYdcQiFtHE0RNyGCr+7V70qF2oolezvrZ uoyF7E7AphVanAwnxicEGEYOwdXHMaNx1t3L/+M9p0dHFCj2mIg7A9tqEAik1kpL yQgMhPwUHs23khEnBfQvNwR3HEc4IRAfudGfuUF4emaHpClGGhUJZQ= 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:subject:message-id:mime-version:content-type; s= default; bh=vH05BV11qs6XAZEH5SptXhxtJZ8=; b=Mz6cpHo69YRE/JkE3RBp 364g1Sj6FkqJB/Jqaohm5LE424il5XGGxsAsWGXOwYmhJeoZVQ+7sjqgSyFalmsm FQiXzCXbB9xPjNDFkzNxfmmXEM0LossdYfLuReqMWdcPO/r7J4ARrcDjOSKW/5DM +3qDnX9adE3ZFilFf+t26o0= Received: (qmail 22047 invoked by alias); 2 Dec 2014 13:34:06 -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 22035 invoked by uid 89); 2 Dec 2014 13:34:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f47.google.com Received: from mail-pa0-f47.google.com (HELO mail-pa0-f47.google.com) (209.85.220.47) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 02 Dec 2014 13:33:59 +0000 Received: by mail-pa0-f47.google.com with SMTP id kq14so13353177pab.6 for ; Tue, 02 Dec 2014 05:33:57 -0800 (PST) X-Received: by 10.67.14.36 with SMTP id fd4mr109628041pad.79.1417527237508; Tue, 02 Dec 2014 05:33:57 -0800 (PST) Received: from msticlxl57.ims.intel.com (fmdmzpr02-ext.fm.intel.com. [192.55.55.37]) by mx.google.com with ESMTPSA id nc9sm20313333pbc.55.2014.12.02.05.33.56 for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Dec 2014 05:33:57 -0800 (PST) Date: Tue, 2 Dec 2014 16:33:51 +0300 From: Ilya Enkovich To: gcc-patches@gcc.gnu.org Subject: [PATCH, CHKP] Don't generate bndret for not instrumented calls Message-ID: <20141202132812.GC2963@msticlxl57.ims.intel.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes Hi, Currently bndret is generated each time we need to get bounds for returned pointer. It causes bndret generated for not instrumented calls incuding builtin function calls. Troubles appear when such builtin call is optimized out - bndret needs to be handled appropriately. Since we don't want not instrumented builtin calls optimizers to be affected by instrumentation, we better avoid bndret for not instrumented calls. This patch uses zero bounds when we don't expect call to return bounds. Bootstrapped and tested on x86_64-unknown-linux-gnu. OK for trunk? Thanks, Ilya --- gcc/ 2014-12-02 Ilya Enkovich * tree-chkp.c (chkp_call_returns_bounds_p): New. (chkp_build_returned_bound): Use zero bounds as returned by calls not returning bounds. gcc/testsuite/ 2014-12-02 Ilya Enkovich * gcc.target/i386/chkp-bndret.c: New. * gcc.target/i386/chkp-strchr.c: New. diff --git a/gcc/testsuite/gcc.target/i386/chkp-bndret.c b/gcc/testsuite/gcc.target/i386/chkp-bndret.c new file mode 100644 index 0000000..3498058 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/chkp-bndret.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target mpx } */ +/* { dg-options "-fcheck-pointer-bounds -mmpx -O2 -fdump-tree-chkp" } */ +/* { dg-final { scan-tree-dump-not "bndret" "chkp" } } */ +/* { dg-final { cleanup-tree-dump "chkp" } } */ + +#include "string.h" + +extern int *test1 (int *p) __attribute__((bnd_legacy)); + +int * +test2 (int *p) +{ + return test1 (p); +} diff --git a/gcc/testsuite/gcc.target/i386/chkp-strchr.c b/gcc/testsuite/gcc.target/i386/chkp-strchr.c new file mode 100644 index 0000000..94a5eaa --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/chkp-strchr.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target mpx } */ +/* { dg-options "-fcheck-pointer-bounds -mmpx -O2" } */ + +#include "string.h" + +static char * +test1 (char *str) +{ + return strrchr (str, '_'); +} + +char * +test2 () +{ + return test1 ("test_string"); +} diff --git a/gcc/tree-chkp.c b/gcc/tree-chkp.c index 3e38691..f7def51 100644 --- a/gcc/tree-chkp.c +++ b/gcc/tree-chkp.c @@ -2080,6 +2080,38 @@ chkp_get_nonpointer_load_bounds (void) return chkp_get_zero_bounds (); } +/* Return 1 if may use bndret call to get bounds for pointer + returned by CALL. */ +static bool +chkp_call_returns_bounds_p (gcall *call) +{ + if (gimple_call_internal_p (call)) + return false; + + tree fndecl = gimple_call_fndecl (call); + + if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) + return false; + + if (fndecl + && lookup_attribute ("bnd_legacy", DECL_ATTRIBUTES (fndecl))) + return false; + + if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) + { + if (chkp_instrument_normal_builtin (fndecl)) + return true; + + if (!lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl))) + return false; + + struct cgraph_node *clone = chkp_maybe_create_clone (fndecl); + return (clone && gimple_has_body_p (clone->decl)); + } + + return true; +} + /* Build bounds returned by CALL. */ static tree chkp_build_returned_bound (gcall *call) @@ -2154,7 +2186,7 @@ chkp_build_returned_bound (gcall *call) bounds = chkp_find_bounds (gimple_call_arg (call, argno), &iter); } - else + else if (chkp_call_returns_bounds_p (call)) { gcc_assert (TREE_CODE (gimple_call_lhs (call)) == SSA_NAME); @@ -2172,6 +2204,8 @@ chkp_build_returned_bound (gcall *call) update_stmt (stmt); } + else + bounds = chkp_get_zero_bounds (); if (dump_file && (dump_flags & TDF_DETAILS)) {