From patchwork Fri Feb 10 03:45:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aaron Sawdey X-Patchwork-Id: 726395 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 3vKLT83S2dz9s73 for ; Fri, 10 Feb 2017 14:46:03 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="AUjIVJcM"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:from:to:cc:date:content-type:mime-version:message-id; q=dns; s=default; b=jaDcbRlQtTRYGaMh8tnLCYKnpfyumUM7E2dxGcshmWN UhOeeyteuWPexcks6mzlf0viX/n2yBRJggzof3Ken56l3Fv2xMvY5B5ABIbQ8NTo t309kalc/XK0fdDkURPowTIzmSlLHMVxyFhk8DBuPEWjwWqEYALOTinmv8a1kV5I = 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 :subject:from:to:cc:date:content-type:mime-version:message-id; s=default; bh=nELq1lpbZD4j+KtSIXnDM3UqwPM=; b=AUjIVJcMzG93OBVPa ZMkX3Xnccq374QJ+XscSJppuVCcRhMu3712/O8dR74QNbqf307gLY45MUGkPzY0V ujn/Nm9iyEAnsz65V95270+0ACrjkDbtzXEdSjnu+6tRQvnWGkTrNsOpHQY2Ci0p 0JnTd9aHw2BL9iVKCYm5iB53dk= Received: (qmail 124363 invoked by alias); 10 Feb 2017 03:45:54 -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 124327 invoked by uid 89); 10 Feb 2017 03:45:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=BAYES_00, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=round_up, ROUND_UP, Toolchain, bgt X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0b-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.158.5) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 10 Feb 2017 03:45:51 +0000 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1A3huer018126 for ; Thu, 9 Feb 2017 22:45:50 -0500 Received: from e37.co.us.ibm.com (e37.co.us.ibm.com [32.97.110.158]) by mx0b-001b2d01.pphosted.com with ESMTP id 28gr4e98es-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 09 Feb 2017 22:45:49 -0500 Received: from localhost by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 9 Feb 2017 20:45:48 -0700 Received: from d03dlp02.boulder.ibm.com (9.17.202.178) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 9 Feb 2017 20:45:46 -0700 Received: from b01cxnp23033.gho.pok.ibm.com (b01cxnp23033.gho.pok.ibm.com [9.57.198.28]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id 807653E40055; Thu, 9 Feb 2017 20:45:45 -0700 (MST) Received: from b01ledav002.gho.pok.ibm.com (b01ledav002.gho.pok.ibm.com [9.57.199.107]) by b01cxnp23033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1A3jjWW18612620; Fri, 10 Feb 2017 03:45:45 GMT Received: from b01ledav002.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DC7E512403D; Thu, 9 Feb 2017 22:45:44 -0500 (EST) Received: from ragesh3a (unknown [9.80.224.228]) by b01ledav002.gho.pok.ibm.com (Postfix) with ESMTP id 6F538124049; Thu, 9 Feb 2017 22:45:44 -0500 (EST) Subject: [PATCH][PR target/79449][7 regression] fix ppc strncmp builtin expansion runtime boundary crossing check From: Aaron Sawdey To: gcc-patches@gcc.gnu.org Cc: Segher Boessenkool , Bill Schmidt Date: Thu, 09 Feb 2017 21:45:38 -0600 Mime-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17021003-0024-0000-0000-000015E74735 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006588; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000203; SDB=6.00819718; UDB=6.00400715; IPR=6.00597171; BA=6.00005130; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00014239; XFM=3.00000011; UTC=2017-02-10 03:45:47 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17021003-0025-0000-0000-000048AA7CD5 Message-Id: <1486698338.30084.17.camel@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-10_01:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702100037 X-IsSubscribed: yes Basic problem is that the runtime check for a 4k boundary crossing was checking that it didn't happen in the N bytes strncmp was being asked to look at. But, the code generation after the check assumed it could generate an 8-byte read and so it could step over the boundary.  The fix is to make sure that the minimum distance to the boundary being checked and the maximum read size being generated for these short comparisons are in sync. The added test case tests for this condition against both memcmp and strncmp builtin expansion. Assuming bootstrap/regtest passes on ppc variants and the new test case passes on x86_64 as well, ok for trunk? 2017-02-09  Aaron Sawdey   PR target/79449 * gcc.dg/strncmp-2.c: New. Test strncmp and memcmp builtin expansion for reading beyond a 4k boundary. 2017-02-09  Aaron Sawdey   PR target/79449 * config/rs6000/rs6000.c (expand_block_compare): Make sure runtime boundary crossing check and subsequent code generation agree. Index: gcc/config/rs6000/rs6000.c =================================================================== --- gcc/config/rs6000/rs6000.c (revision 245294) +++ gcc/config/rs6000/rs6000.c (working copy) @@ -19931,15 +19931,26 @@ cmpldi cr7,r8,4096-16 bgt cr7,L(pagecross) */ + /* Make sure that the length we use for the alignment test and + the subsequent code generation are in agreement so we do not + go past the length we tested for a 4k boundary crossing. */ + unsigned HOST_WIDE_INT align_test = compare_length; + if (align_test < 8) + { + align_test = 1 << ceil_log2 (align_test); + base_align = align_test; + } + else + { + align_test = ROUND_UP (align_test, 8); + base_align = 8; + } + if (align1 < 8) - expand_strncmp_align_check (strncmp_label, src1, compare_length); + expand_strncmp_align_check (strncmp_label, src1, align_test); if (align2 < 8) - expand_strncmp_align_check (strncmp_label, src2, compare_length); + expand_strncmp_align_check (strncmp_label, src2, align_test); - /* After the runtime alignment checks, we can use any alignment we - like as we know there is no 4k boundary crossing. */ - base_align = 8; - /* Now generate the following sequence: - branch to begin_compare - strncmp_label Index: gcc/testsuite/gcc.dg/strncmp-2.c =================================================================== --- gcc/testsuite/gcc.dg/strncmp-2.c (revision 0) +++ gcc/testsuite/gcc.dg/strncmp-2.c (working copy) @@ -0,0 +1,99 @@ +/* Test strncmp builtin expansion for compilation and proper execution. */ +/* { dg-do run { target *-*-linux* *-*-gnu* } } */ +/* { dg-options "-O2" } */ +/* { dg-require-effective-target ptr32plus } */ + +#include +#include +#include +#include +#include +#include + +int lib_memcmp(const void *a, const void *b, size_t n) asm("memcmp"); +int lib_strncmp(const char *a, const char *b, size_t n) asm("strncmp"); + +static void test_driver_strncmp (void (test_strncmp)(const char *, const char *, int), + void (test_memcmp)(const void *, const void *, int), + size_t sz) +{ + long pgsz = sysconf(_SC_PAGESIZE); + char buf1[sz+1]; + char *buf2 = aligned_alloc(pgsz,2*pgsz); + char *p2; + int r,i,e; + + r = mprotect (buf2+pgsz,pgsz,PROT_NONE); + if (r < 0) abort(); + + memset(buf1,'A',sz); + for(i=10; i>=0; i--) { + p2 = buf2+pgsz-sz-i; + memset(p2,'A',sz); + e = lib_strncmp(buf1,p2,sz); + (*test_strncmp)(buf1,p2,e); + e = lib_memcmp(buf1,p2,sz); + (*test_memcmp)(buf1,p2,e); + } +} + +#define RUN_TEST(SZ) test_driver_strncmp (test_strncmp_ ## SZ, test_memcmp_ ## SZ, SZ); + +#define DEF_TEST(SZ) \ +__attribute__((noinline)) \ +void test_strncmp_ ## SZ (const char *str1, const char *str2, int expect) \ +{ \ + int r; \ + r = strncmp(str1,str2,SZ); \ + if ( r < 0 && !(expect < 0) ) abort(); \ + if ( r > 0 && !(expect > 0) ) abort(); \ + if ( r == 0 && !(expect == 0) ) abort(); \ +} \ +__attribute__((noinline)) \ +void test_memcmp_ ## SZ (const void *p1, const void *p2, int expect) \ +{ \ + int r; \ + r = memcmp(p1,p2,SZ); \ + if ( r < 0 && !(expect < 0) ) abort(); \ + if ( r > 0 && !(expect > 0) ) abort(); \ + if ( r == 0 && !(expect == 0) ) abort(); \ +} + +DEF_TEST(1) +DEF_TEST(2) +DEF_TEST(3) +DEF_TEST(4) +DEF_TEST(5) +DEF_TEST(6) +DEF_TEST(7) +DEF_TEST(8) +DEF_TEST(9) +DEF_TEST(10) +DEF_TEST(11) +DEF_TEST(12) +DEF_TEST(13) +DEF_TEST(14) +DEF_TEST(15) +DEF_TEST(16) + +int +main(int argc, char **argv) +{ + RUN_TEST(1) ; + RUN_TEST(2) ; + RUN_TEST(3) ; + RUN_TEST(4) ; + RUN_TEST(5) ; + RUN_TEST(6) ; + RUN_TEST(7) ; + RUN_TEST(8) ; + RUN_TEST(9) ; + RUN_TEST(10); + RUN_TEST(11); + RUN_TEST(12); + RUN_TEST(13); + RUN_TEST(14); + RUN_TEST(15); + RUN_TEST(16); + return 0; +}