From patchwork Thu Nov 1 19:52:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dodji Seketeli X-Patchwork-Id: 196358 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]) by ozlabs.org (Postfix) with SMTP id 18F6B2C0090 for ; Fri, 2 Nov 2012 06:53:34 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1352404415; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:From:To:Cc:Subject:Date:Message-Id:In-Reply-To: References:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=I+FmsH+ cgnB5glvTaTREWP4ereI=; b=Up+c6hgbzvzz/4x5SyGFliQOdx+HyqiYSG7Cpga Z15JixJJ92BerXmksLpTt5CGUukQ5WGoW2bHeSpy7JRmuMEFUKmdGG3x4v4iVPL/ OlfIFmeilI3ng/+4cJzugR75Fmchn0bZuecJqBVotjjUUZuJFlSeMos0xnexWLQM nskA= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=yJ2KnZyvVthPMQviocAlEx9LomFsf7r/1dlV9n+bYtVG1MsaBoqfYxMtkm3iu9 r+xZ4a5ucdjxXHinmBozcGDmQFA+5nF5V0uGQznAc2bN/CI6Osftw15ncEYSA4Mp DesRKqKOZGTVfivb97tD5ZYhT1LxlpH291IwICGyEP9JU=; Received: (qmail 20650 invoked by alias); 1 Nov 2012 19:53:04 -0000 Received: (qmail 20625 invoked by uid 22791); 1 Nov 2012 19:53:02 -0000 X-SWARE-Spam-Status: No, hits=-7.0 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_THREADED, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_SV X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 01 Nov 2012 19:52:50 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id qA1Jqnd7024513 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 1 Nov 2012 15:52:49 -0400 Received: from localhost (ovpn-116-40.ams2.redhat.com [10.36.116.40]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id qA1Jqmdb001136 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 1 Nov 2012 15:52:49 -0400 Received: by localhost (Postfix, from userid 1000) id 7DA6C2C0106; Thu, 1 Nov 2012 20:52:47 +0100 (CET) From: dodji@redhat.com To: gcc-patches@gcc.gnu.org Cc: dnovillo@google.com, jakub@redhat.com, wmi@google.com, davidxl@google.com, konstantin.s.serebryany@gmail.com Subject: [PATCH 03/13] Initial asan cleanups Date: Thu, 1 Nov 2012 20:52:36 +0100 Message-Id: <1351799566-31447-4-git-send-email-dodji@redhat.com> In-Reply-To: <1351799566-31447-1-git-send-email-dodji@redhat.com> References: <1351799566-31447-1-git-send-email-dodji@redhat.com> 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 From: jakub This patch defines a new asan_shadow_offset target macro, instead of having a mere macro in the asan.c file. It becomes thus cleaner to define the target macro for targets that supports asan, namely x86 for now. The ASAN_SHADOW_SHIFT (which, along with the asan_shadow_offset constant, is used to compute the address of the shadow memory byte for a given memory address) is defined in asan.h. * toplev.c (process_options): Warn and turn off -fasan if not supported by target. * asan.c: Include target.h. (asan_scale, asan_offset_log_32, asan_offset_log_64, asan_offset_log): Removed. (build_check_stmt): Use ASAN_SHADOW_SHIFT and targetm.asan_shadow_offset (). (asan_instrument): Don't initialize asan_offset_log. * asan.h (ASAN_SHADOW_SHIFT): Define. * target.def (TARGET_ASAN_SHADOW_OFFSET): New hook. * doc/tm.texi.in (TARGET_ASAN_SHADOW_OFFSET): Add it. * doc/tm.texi: Regenerated. * Makefile.in (asan.o): Depend on $(TARGET_H). * config/i386/i386.c (ix86_asan_shadow_offset): New function. (TARGET_ASAN_SHADOW_OFFSET): Define. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/asan@192372 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog.asan | 18 ++++++++++++++++++ gcc/Makefile.in | 2 +- gcc/asan.c | 25 ++++++------------------- gcc/asan.h | 6 +++++- gcc/config/i386/i386.c | 11 +++++++++++ gcc/doc/tm.texi | 6 ++++++ gcc/doc/tm.texi.in | 2 ++ gcc/target.def | 11 +++++++++++ gcc/toplev.c | 7 +++++++ 9 files changed, 67 insertions(+), 21 deletions(-) diff --git a/gcc/ChangeLog.asan b/gcc/ChangeLog.asan index c196bfe..0bc9420 100644 --- a/gcc/ChangeLog.asan +++ b/gcc/ChangeLog.asan @@ -1,3 +1,21 @@ +2012-10-11 Jakub Jelinek + + * toplev.c (process_options): Warn and turn off -fasan + if not supported by target. + * asan.c: Include target.h. + (asan_scale, asan_offset_log_32, asan_offset_log_64, + asan_offset_log): Removed. + (build_check_stmt): Use ASAN_SHADOW_SHIFT and + targetm.asan_shadow_offset (). + (asan_instrument): Don't initialize asan_offset_log. + * asan.h (ASAN_SHADOW_SHIFT): Define. + * target.def (TARGET_ASAN_SHADOW_OFFSET): New hook. + * doc/tm.texi.in (TARGET_ASAN_SHADOW_OFFSET): Add it. + * doc/tm.texi: Regenerated. + * Makefile.in (asan.o): Depend on $(TARGET_H). + * config/i386/i386.c (ix86_asan_shadow_offset): New function. + (TARGET_ASAN_SHADOW_OFFSET): Define. + 2012-10-10 Diego Novillo * asan.c: Rename from tree-asan.c. diff --git a/gcc/Makefile.in b/gcc/Makefile.in index a9da161..bdc5afb 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -2213,7 +2213,7 @@ stor-layout.o : stor-layout.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \ asan.o : asan.c asan.h $(CONFIG_H) pointer-set.h \ $(SYSTEM_H) $(TREE_H) $(GIMPLE_H) \ output.h $(DIAGNOSTIC_H) coretypes.h $(TREE_DUMP_H) $(FLAGS_H) \ - tree-pretty-print.h + tree-pretty-print.h $(TARGET_H) tree-ssa-tail-merge.o: tree-ssa-tail-merge.c \ $(SYSTEM_H) $(CONFIG_H) coretypes.h $(TM_H) $(BITMAP_H) \ $(FLAGS_H) $(TM_P_H) $(BASIC_BLOCK_H) \ diff --git a/gcc/asan.c b/gcc/asan.c index a6ceb57..e95be47 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -1,5 +1,5 @@ /* AddressSanitizer, a fast memory error detector. - Copyright (C) 2011 Free Software Foundation, Inc. + Copyright (C) 2011, 2012 Free Software Foundation, Inc. Contributed by Kostya Serebryany This file is part of GCC. @@ -42,6 +42,7 @@ along with GCC; see the file COPYING3. If not see #include "gimple.h" #include "asan.h" #include "gimple-pretty-print.h" +#include "target.h" /* AddressSanitizer finds out-of-bounds and use-after-free bugs @@ -78,15 +79,6 @@ along with GCC; see the file COPYING3. If not see to create redzones for stack and global object and poison them. */ -/* The shadow address is computed as (X>>asan_scale) + (1<> asan_scale) + (1 << asan_offset_log). */ + /* Build + (base_addr >> ASAN_SHADOW_SHIFT) | targetm.asan_shadow_offset (). */ t = build2 (RSHIFT_EXPR, uintptr_type, base_addr, - build_int_cst (uintptr_type, asan_scale)); + build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT)); t = build2 (PLUS_EXPR, uintptr_type, t, - build2 (LSHIFT_EXPR, uintptr_type, - build_int_cst (uintptr_type, 1), - build_int_cst (uintptr_type, asan_offset_log) - )); + build_int_cst (uintptr_type, targetm.asan_shadow_offset ())); t = build1 (INDIRECT_REF, shadow_type, build1 (VIEW_CONVERT_EXPR, shadow_ptr_type, t)); t = force_gimple_operand (t, &stmts, false, NULL_TREE); @@ -367,9 +357,6 @@ static unsigned int asan_instrument (void) { struct gimplify_ctx gctx; - tree uintptr_type = lang_hooks.types.type_for_mode (ptr_mode, true); - int is_64 = tree_low_cst (TYPE_SIZE (uintptr_type), 0) == 64; - asan_offset_log = is_64 ? asan_offset_log_64 : asan_offset_log_32; push_gimplify_context (&gctx); transform_statements (); pop_gimplify_context (NULL); diff --git a/gcc/asan.h b/gcc/asan.h index 590cf35..699820b 100644 --- a/gcc/asan.h +++ b/gcc/asan.h @@ -1,5 +1,5 @@ /* AddressSanitizer, a fast memory error detector. - Copyright (C) 2011 Free Software Foundation, Inc. + Copyright (C) 2011, 2012 Free Software Foundation, Inc. Contributed by Kostya Serebryany This file is part of GCC. @@ -23,4 +23,8 @@ along with GCC; see the file COPYING3. If not see extern void asan_finish_file(void); +/* Shadow memory is found at + (address >> ASAN_SHADOW_SHIFT) | targetm.asan_shadow_offset (). */ +#define ASAN_SHADOW_SHIFT 3 + #endif /* TREE_ASAN */ diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 1c34bb2..bf84b65 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -5647,6 +5647,14 @@ ix86_legitimate_combined_insn (rtx insn) return true; } +/* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */ + +static unsigned HOST_WIDE_INT +ix86_asan_shadow_offset (void) +{ + return (unsigned HOST_WIDE_INT) 1 << (TARGET_LP64 ? 44 : 29); +} + /* Argument support functions. */ /* Return true when register may be used to pass function parameters. */ @@ -41379,6 +41387,9 @@ ix86_memmodel_check (unsigned HOST_WIDE_INT val) #undef TARGET_LEGITIMATE_COMBINED_INSN #define TARGET_LEGITIMATE_COMBINED_INSN ix86_legitimate_combined_insn +#undef TARGET_ASAN_SHADOW_OFFSET +#define TARGET_ASAN_SHADOW_OFFSET ix86_asan_shadow_offset + #undef TARGET_GIMPLIFY_VA_ARG_EXPR #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 665c5b1..908ddbf 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -11326,6 +11326,12 @@ MIPS, where add-immediate takes a 16-bit signed value, is zero, which disables this optimization. @end deftypevr +@deftypefn {Target Hook} {unsigned HOST_WIDE_INT} TARGET_ASAN_SHADOW_OFFSET (void) +Return the offset bitwise ored into shifted address to get corresponding +Address Sanitizer shadow memory address. NULL if Address Sanitizer is not +supported by the target. +@end deftypefn + @deftypefn {Target Hook} {unsigned HOST_WIDE_INT} TARGET_MEMMODEL_CHECK (unsigned HOST_WIDE_INT @var{val}) Validate target specific memory model mask bits. When NULL no target specific memory model bits are allowed. diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 289934b..0786691 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -11168,6 +11168,8 @@ MIPS, where add-immediate takes a 16-bit signed value, is zero, which disables this optimization. @end deftypevr +@hook TARGET_ASAN_SHADOW_OFFSET + @hook TARGET_MEMMODEL_CHECK Validate target specific memory model mask bits. When NULL no target specific memory model bits are allowed. diff --git a/gcc/target.def b/gcc/target.def index 5865224..f8781a8 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -2025,6 +2025,17 @@ DEFHOOK "", unsigned HOST_WIDE_INT, (unsigned HOST_WIDE_INT val), NULL) +/* Defines an offset bitwise ored into shifted address to get corresponding + Address Sanitizer shadow address, or -1 if Address Sanitizer is not + supported by the target. */ +DEFHOOK +(asan_shadow_offset, + "Return the offset bitwise ored into shifted address to get corresponding\n\ +Address Sanitizer shadow memory address. NULL if Address Sanitizer is not\n\ +supported by the target.", + unsigned HOST_WIDE_INT, (void), + NULL) + /* Functions relating to calls - argument passing, returns, etc. */ /* Members of struct call have no special macro prefix. */ HOOK_VECTOR (TARGET_CALLS, calls) diff --git a/gcc/toplev.c b/gcc/toplev.c index 3ca0736..68849f5 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1541,6 +1541,13 @@ process_options (void) flag_omit_frame_pointer = 0; } + /* Address Sanitizer needs porting to each target architecture. */ + if (flag_asan && targetm.asan_shadow_offset == NULL) + { + warning (0, "-fasan not supported for this target"); + flag_asan = 0; + } + /* Enable -Werror=coverage-mismatch when -Werror and -Wno-error have not been set. */ if (!global_options_set.x_warnings_are_errors