From patchwork Tue Nov 18 16:42:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Enkovich X-Patchwork-Id: 412087 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 11BE4140179 for ; Wed, 19 Nov 2014 03:42:31 +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:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=eRjjZaSv+tfuzCQdQ El45bzbKoeBmaikuEiC8/wzyDaHee6jTOtBp9JF67ZXclDiw+ApexkCBoNzk+o/Q v7mnxSXLBXlTx81Etg/V6bd2xcpGCqTkk/UlEHYqeHerMfP7ntfqpA8knWMpb5DE E7kj/a9nsTMJhMcFhafD1bDkjw= 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:references:mime-version :content-type:in-reply-to; s=default; bh=mGcnlxaQGee6V7m0D/2fIWV gzs0=; b=GfiZeTczy6LjEIBUsmMrQKOU3l2TEEEat/AOBrRUMRDiScuREu1K7eV 1XWPRRvgTACvTRHCH1zupa8SVAe8EeYQbGUaVO9GYoGeIPP1RwqYr6ETwTObttEa oDTwMpuoXVGddnVdetiJf/9KBq574g50ChuxmCRkZHHDiqxil3dg= Received: (qmail 20439 invoked by alias); 18 Nov 2014 16:42:22 -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 20423 invoked by uid 89); 18 Nov 2014 16:42:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.2 required=5.0 tests=AWL, BAYES_50, FREEMAIL_FROM, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 X-HELO: mail-pd0-f178.google.com Received: from mail-pd0-f178.google.com (HELO mail-pd0-f178.google.com) (209.85.192.178) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 18 Nov 2014 16:42:20 +0000 Received: by mail-pd0-f178.google.com with SMTP id y13so8636089pdi.9 for ; Tue, 18 Nov 2014 08:42:18 -0800 (PST) X-Received: by 10.70.61.136 with SMTP id p8mr38124231pdr.98.1416328938175; Tue, 18 Nov 2014 08:42:18 -0800 (PST) Received: from msticlxl57.ims.intel.com ([192.55.55.41]) by mx.google.com with ESMTPSA id al4sm38366111pbc.19.2014.11.18.08.42.15 for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Nov 2014 08:42:17 -0800 (PST) Date: Tue, 18 Nov 2014 19:42:11 +0300 From: Ilya Enkovich To: Joseph Myers Cc: Jeff Law , gcc-patches@gcc.gnu.org Subject: Re: [PATCH, MPX wrappers 2/3] Replace some function calls with wrapper calls during instrumentation Message-ID: <20141118164211.GB47331@msticlxl57.ims.intel.com> References: <20141114172932.GB20207@msticlxl57.ims.intel.com> <5466FDC1.6000806@redhat.com> <20141118151007.GA47331@msticlxl57.ims.intel.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes On 18 Nov 16:23, Joseph Myers wrote: > On Tue, 18 Nov 2014, Ilya Enkovich wrote: > > > +@item -fcheck-pointer-bounds > > +@opindex fcheck-pointer-bounds > > +@opindex fno-check-pointer-bounds > > +Enable Pointer Bounds Checker instrumentation. Each memory reference > > +is instrumented with checks of pointer used for memory access against > > +bounds associated with that pointer. Generated instrumentation may > > +be controlled by various @option{-fchkp-*} options. > > If this is only operational given -mmpx and when the generated code is run > on a processor supporting MPX, I think the documentation needs to make > that clear. > > > +@item -fchkp-use-fast-string-functions > > +@opindex fchkp-use-fast-string-functions > > +@opindex fno-chkp-use-fast-string-functions > > +Allow to use *_nobnd versions of string functions (not copying bounds) > > +by Pointer Bounds Checker. Disabled by default. > > @code{*_nobnd}. > > > +@item -fchkp-use-nochk-string-functions > > +@opindex fchkp-use-nochk-string-functions > > +@opindex fno-chkp-use-nochk-string-functions > > +Allow to use *_nochk versions of string functions (not checking bounds) > > +by Pointer Bounds Checker. Disabled by default. > > @code{*_nochk). > > > +@item -fchkp-instrument-marked-only > > +@opindex fchkp-instrument-marked-only > > +@opindex fno-chkp-instrument-marked-only > > +Instructs Pointer Bounds Checker to instrument only functions > > +marked with bnd_instrument attribute. Disabled by default. > > @code{bnd_instrument}. > > > +@item -fchkp-use-wrappers > > +@opindex fchkp-use-wrappers > > +@opindex fno-chkp-use-wrappers > > +Allows Pointer Bounds Checker to replace calls to builtin function > > +with calls to wrapper functions. Enabled by default. > > "built-in functions". > > -- > Joseph S. Myers > joseph@codesourcery.com Thank you for comments! Below is a fixed version. Ilya diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 85dcb98..a9f8f99 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1040,6 +1040,10 @@ fchkp-instrument-marked-only C ObjC C++ ObjC++ LTO Report Var(flag_chkp_instrument_marked_only) Init(0) Instrument only functions marked with bnd_instrument attribute. +fchkp-use-wrappers +C ObjC C++ ObjC++ LTO Report Var(flag_chkp_use_wrappers) Init(1) +Transform instrumented builtin calls into calls to wrappers. + fcilkplus C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) Enable Cilk Plus diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 89edddb..60056da 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -299,6 +299,15 @@ Objective-C and Objective-C++ Dialects}. @gccoptlist{-d@var{letters} -dumpspecs -dumpmachine -dumpversion @gol -fsanitize=@var{style} -fsanitize-recover -fsanitize-recover=@var{style} @gol -fasan-shadow-offset=@var{number} -fsanitize-undefined-trap-on-error @gol +-fcheck-pointer-bounds -fchkp-check-incomplete-type @gol +-fchkp-first-field-has-own-bounds -fchkp-narrow-bounds @gol +-fchkp-narrow-to-innermost-array -fchkp-optimize @gol +-fchkp-use-fast-string-functions -fchkp-use-nochk-string-functions @gol +-fchkp-use-static-bounds -fchkp-use-static-const-bounds @gol +-fchkp-treat-zero-dynamic-size-as-infinite -fchkp-check-read @gol +-fchkp-check-read -fchkp-check-write -fchkp-store-bounds @gol +-fchkp-instrument-calls -fchkp-instrument-marked-only @gol +-fchkp-use-wrappers @gol -fdbg-cnt-list -fdbg-cnt=@var{counter-value-list} @gol -fdisable-ipa-@var{pass_name} @gol -fdisable-rtl-@var{pass_name} @gol @@ -5693,6 +5702,119 @@ a @code{libubsan} library routine. The advantage of this is that the @code{libubsan} library is not needed and will not be linked in, so this is usable even for use in freestanding environments. +@item -fcheck-pointer-bounds +@opindex fcheck-pointer-bounds +@opindex fno-check-pointer-bounds +Enable Pointer Bounds Checker instrumentation. Each memory reference +is instrumented with checks of pointer used for memory access against +bounds associated with that pointer. Generated instrumentation may +be controlled by various @option{-fchkp-*} options. Currently there +is only Intel MPX based implementation available, thus i386 target +and @option{-mmpx} are required. + +@item -fchkp-check-incomplete-type +@opindex fchkp-check-incomplete-type +@opindex fno-chkp-check-incomplete-type +Generate pointer bounds checks for variables with incomplete type. +Enabled by default + +@item -fchkp-narrow-bounds +@opindex fchkp-narrow-bounds +@opindex fno-chkp-narrow-bounds +Controls bounds used by Pointer Bounds Checker for pointers to object +fields. If narrowing is enabled then field bounds are used. Otherwise +object bounds are used. See also @option{-fchkp-narrow-to-innermost-array} +and @option{-fchkp-first-field-has-own-bounds}. Enabled by default. + +@item -fchkp-first-field-has-own-bounds +@opindex fchkp-first-field-has-own-bounds +@opindex fno-chkp-first-field-has-own-bounds +Forces Pointer Bounds Checker to use narrowed bounds for address of the +first field in the structure. By default pointer to the first field has +the same bounds as pointer to the whole structure. + +@item -fchkp-narrow-to-innermost-array +@opindex fchkp-narrow-to-innermost-array +@opindex fno-chkp-narrow-to-innermost-array +Forces Pointer Bounds Checker to use bounds of the innermost arrays in +case of nested static arryas access. By default it is disabled and +bounds of the outermost array are used. + +@item -fchkp-optimize +@opindex fchkp-optimize +@opindex fno-chkp-optimize +Enables Pointer Bounds Checker optimizations. Enabled by default at +optimization levels @option{-O}, @option{-O2}, @option{-O3}. + +@item -fchkp-use-fast-string-functions +@opindex fchkp-use-fast-string-functions +@opindex fno-chkp-use-fast-string-functions +Allow to use @code{*_nobnd} versions of string functions (not copying bounds) +by Pointer Bounds Checker. Disabled by default. + +@item -fchkp-use-nochk-string-functions +@opindex fchkp-use-nochk-string-functions +@opindex fno-chkp-use-nochk-string-functions +Allow to use @code{*_nochk} versions of string functions (not checking bounds) +by Pointer Bounds Checker. Disabled by default. + +@item -fchkp-use-static-bounds +@opindex fchkp-use-static-bounds +@opindex fno-chkp-use-static-bounds +Allow Pointer Bounds Checker to generate static bounds holding +bounds of static variables. Enabled by default. + +@item -fchkp-use-static-const-bounds +@opindex fchkp-use-static-const-bounds +@opindex fno-chkp-use-static-const-bounds +Use statically initialized bounds for constant bounds instead of +generating them each time it is required. By default enabled when +@option{-fchkp-use-static-bounds} is enabled. + +@item -fchkp-treat-zero-dynamic-size-as-infinite +@opindex fchkp-treat-zero-dynamic-size-as-infinite +@opindex fno-chkp-treat-zero-dynamic-size-as-infinite +With this option zero size obtained dynamically for objects with +incomplete type will be treated as infinite by Pointer Bounds +Checker. It may be helpful if program is linked with a library +missing size information for some symbols. Disabled by default. + +@item -fchkp-check-read +@opindex fchkp-check-read +@opindex fno-chkp-check-read +Instructs Pointer Bounds Checker to generate checks for all read +accesses to memory. Enabled by default. + +@item -fchkp-check-write +@opindex fchkp-check-write +@opindex fno-chkp-check-write +Instructs Pointer Bounds Checker to generate checks for all write +accesses to memory. Enabled by default. + +@item -fchkp-store-bounds +@opindex fchkp-store-bounds +@opindex fno-chkp-store-bounds +Instructs Pointer Bounds Checker to generate bounds stores for +pointer writes. Enabled by default. + +@item -fchkp-instrument-calls +@opindex fchkp-instrument-calls +@opindex fno-chkp-instrument-calls +Instructs Pointer Bounds Checker to pass pointer bounds to calls. +Enabled by default. + +@item -fchkp-instrument-marked-only +@opindex fchkp-instrument-marked-only +@opindex fno-chkp-instrument-marked-only +Instructs Pointer Bounds Checker to instrument only functions +marked with @code{bnd_instrument} attribute. Disabled by default. + +@item -fchkp-use-wrappers +@opindex fchkp-use-wrappers +@opindex fno-chkp-use-wrappers +Allows Pointer Bounds Checker to replace calls to built-in function +with calls to wrapper functions. Enabled by default. + @item -fdump-final-insns@r{[}=@var{file}@r{]} @opindex fdump-final-insns Dump the final internal representation (RTL) to @var{file}. If the diff --git a/gcc/ipa-chkp.c b/gcc/ipa-chkp.c index 46b2139..00b398a 100644 --- a/gcc/ipa-chkp.c +++ b/gcc/ipa-chkp.c @@ -89,6 +89,44 @@ along with GCC; see the file COPYING3. If not see removed. */ #define CHKP_BOUNDS_OF_SYMBOL_PREFIX "__chkp_bounds_of_" +#define CHKP_WRAPPER_SYMBOL_PREFIX "__mpx_wrapper_" + +/* Return 1 calls to FNDECL should be replaced with + a call to wrapper function. */ +static bool +chkp_wrap_function (tree fndecl) +{ + if (!flag_chkp_use_wrappers) + return false; + + if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) + { + switch (DECL_FUNCTION_CODE (fndecl)) + { + case BUILT_IN_STRLEN: + case BUILT_IN_STRCPY: + case BUILT_IN_STRNCPY: + case BUILT_IN_STPCPY: + case BUILT_IN_STPNCPY: + case BUILT_IN_STRCAT: + case BUILT_IN_STRNCAT: + case BUILT_IN_MEMCPY: + case BUILT_IN_MEMPCPY: + case BUILT_IN_MEMSET: + case BUILT_IN_MEMMOVE: + case BUILT_IN_BZERO: + case BUILT_IN_MALLOC: + case BUILT_IN_CALLOC: + case BUILT_IN_REALLOC: + return 1; + + default: + return 0; + } + } + + return false; +} /* Build a clone of FNDECL with a modified name. */ @@ -113,11 +151,20 @@ chkp_build_instrumented_fndecl (tree fndecl) because it conflicts with decl merging algorithms in LTO. Achieve the result by using transparent alias name for the instrumented version. */ - s = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)); - s += ".chkp"; - new_name = get_identifier (s.c_str ()); - IDENTIFIER_TRANSPARENT_ALIAS (new_name) = 1; - TREE_CHAIN (new_name) = DECL_ASSEMBLER_NAME (fndecl); + if (chkp_wrap_function(fndecl)) + { + s = CHKP_WRAPPER_SYMBOL_PREFIX; + s += IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)); + new_name = get_identifier (s.c_str ()); + } + else + { + s = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)); + s += ".chkp"; + new_name = get_identifier (s.c_str ()); + IDENTIFIER_TRANSPARENT_ALIAS (new_name) = 1; + TREE_CHAIN (new_name) = DECL_ASSEMBLER_NAME (fndecl); + } SET_DECL_ASSEMBLER_NAME (new_decl, new_name); /* For functions with body versioning will make a copy of arguments.