From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 88838 invoked by alias); 9 Apr 2015 18:11:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 88828 invoked by uid 89); 9 Apr 2015 18:11:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 09 Apr 2015 18:11:29 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t39IBStl012392 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 9 Apr 2015 14:11:28 -0400 Received: from tucnak.zalov.cz (ovpn-116-58.ams2.redhat.com [10.36.116.58]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t39IBQHM018859 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 9 Apr 2015 14:11:27 -0400 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.14.9/8.14.9) with ESMTP id t39IBO9r006228; Thu, 9 Apr 2015 20:11:24 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.14.9/8.14.9/Submit) id t39IBM8Y006227; Thu, 9 Apr 2015 20:11:22 +0200 Date: Thu, 09 Apr 2015 18:11:00 -0000 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix ubsan type reporting (PR tree-optimization/65709) Message-ID: <20150409181122.GM19273@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg00404.txt.bz2 Hi! As can be seen on the following testcase, instrument_mem_ref (for both -fsanitize=alignment and -fsanitize=null) has been using wrong type to find out what is the access type - instead of the type of MEM_REF which is the access type it was using the TREE_TYPE of MEM_REF's argument type, which can be some arbitrary other type, either due to type punning, or if it is a SSA_NAME it can be random other type because most pointer types are considered type compatible in GIMPLE. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2015-04-09 Jakub Jelinek PR tree-optimization/65709 * ubsan.c (instrument_mem_ref): Use TREE_TYPE (base) instead of TREE_TYPE (TREE_TYPE (t)). * c-c++-common/ubsan/align-9.c: New test. --- gcc/ubsan.c.jj 2015-03-27 10:48:33.000000000 +0100 +++ gcc/ubsan.c 2015-04-09 10:05:48.841221438 +0200 @@ -1232,9 +1232,9 @@ instrument_mem_ref (tree mem, tree base, tree t = TREE_OPERAND (base, 0); if (!POINTER_TYPE_P (TREE_TYPE (t))) return; - if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_TYPE (t))) && mem != base) + if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (base)) && mem != base) ikind = UBSAN_MEMBER_ACCESS; - tree kind = build_int_cst (TREE_TYPE (t), ikind); + tree kind = build_int_cst (build_pointer_type (TREE_TYPE (base)), ikind); tree alignt = build_int_cst (pointer_sized_int_node, align); gcall *g = gimple_build_call_internal (IFN_UBSAN_NULL, 3, t, kind, alignt); gimple_set_location (g, gimple_location (gsi_stmt (*iter))); --- gcc/testsuite/c-c++-common/ubsan/align-9.c.jj 2015-04-09 10:11:15.227973011 +0200 +++ gcc/testsuite/c-c++-common/ubsan/align-9.c 2015-04-09 10:13:16.857017169 +0200 @@ -0,0 +1,21 @@ +/* Limit this to known non-strict alignment targets. */ +/* { dg-do run { target { i?86-*-linux* x86_64-*-linux* } } } */ +/* { dg-options "-O2 -fsanitize=alignment -fsanitize-recover=alignment" } */ + +__attribute__((noinline, noclone)) void +foo (void *p, const void *q) +{ + *(long int *) p = *(const long int *) q; +} + +int +main () +{ + struct S { long c; char f[64]; char d; char e[2 * sizeof (long)]; char g[64]; } s; + __builtin_memset (&s, '\0', sizeof s); + foo (&s.e[0], &s.e[sizeof (long)]); + return 0; +} + +/* { dg-output "\.c:8:\[0-9]*: \[^\n\r]*load of misaligned address 0x\[0-9a-fA-F]* for type 'const long int', which requires \[48] byte alignment.*" } */ +/* { dg-output "\.c:8:\[0-9]*: \[^\n\r]*store to misaligned address 0x\[0-9a-fA-F]* for type 'long int', which requires \[48] byte alignment" } */ Jakub