From patchwork Tue Jan 3 14:56:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 89670 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp8040620qgi; Tue, 3 Jan 2017 06:56:29 -0800 (PST) X-Received: by 10.84.218.11 with SMTP id q11mr136218475pli.138.1483455389123; Tue, 03 Jan 2017 06:56:29 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id n190si69218059pgn.27.2017.01.03.06.56.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Jan 2017 06:56:29 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-76571-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-76571-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-76571-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=n5AUjqzR5eyUGKLW3WGWP876pjeuDhAh4LzxwUgmImgHVtco7riSU zLrQgw3V1Jde9cU1bg7s6ZJ4QwTupaZL/e9BwJQXZmNMymVOrklj8BY+vP+sQNab E69B2lOAXGqn4WjuDQcn9UnYb3/RnQJMgOTVi5aKk8FnO+i3/ac7io= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=qWez4P6Gad0EkYJiEI1WAB7crbc=; b=LrLprirGuv59Zcrx040Ih39xppVn Mx8kqDyOJbGhfQNrulTF7A13ErJS9EbMfv8RynEHSKKSijP2Yd9oB1TPI629uY5c jqRMO949ISdlDi+0UsTAxav42exehU1mvr+6nLBoOIyq5bQdsOl9Uru/v8IJ5zxq 90yE0qP6RGhncXM= Received: (qmail 8330 invoked by alias); 3 Jan 2017 14:56:20 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 8315 invoked by uid 89); 3 Jan 2017 14:56:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=998, 2.7.4, 297, 6, 2976 X-HELO: mail-yw0-f175.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=0p7joD1jHZlzL5M9yp5WMb8zI2kS0/Ot35Iy2MmbgL8=; b=a7hdDWHM5ZAceDVNlESHrhY318hW8wU9EypMPUmGqpxB7/qECDYbhGaYh0vuTHcGQ/ Zpnu6mbCWk1Kgg3nhAcaq1MwJ+GB2sGTKyecQD1lxTB0gbjQTIJ4OTN19qb02g1XSQ1w e+lCvImcnPkVzxdHtyKDXWbk92+jT7LxE9X5mQDZp5gxlAqGhvENMO9BzYIwlb8Eco+S 1zuw9UrFlQIraKZKk7kAy5T1MNWdfItS4CRMJBTzTfJlap4VqZxjdQsI2rViugAI4nL8 PbPPSYwg2BXIAcIsGuT5fp2rzElhXfTyaxZwm2K+rGU5EIpLf0BlzX9JjbP6Vaw5jcdA AuSg== X-Gm-Message-State: AIkVDXK4gZenKV/Ut9+kyQV0Bi0dqHmpE5Dd1l8rUQXmv1P9Mlq3+OVddTRxZMjhRX9HNsgg X-Received: by 10.129.167.1 with SMTP id e1mr60143243ywh.323.1483455367827; Tue, 03 Jan 2017 06:56:07 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] Fix x86 strncat optimized implementation for large sizes Date: Tue, 3 Jan 2017 12:56:00 -0200 Message-Id: <1483455360-818-1-git-send-email-adhemerval.zanella@linaro.org> Similar to BZ#19387, BZ#21014, and BZ#20971, both x86 sse2 strncat optimized assembly implementations do not handle the size overflow correctly. The x86_64 one is in fact an issue with strcpy-sse2-unaligned, but that is triggered also with strncat optimized implementation. This patch uses a similar strategy used on 3daef2c8ee4df2, where saturared math is used for overflow case. Checked on x86_64-linux-gnu and i686-linux-gnu. [BZ #19390] * string/test-strncat.c (test_main): Add tests with SIZE_MAX as maximum string size. * sysdeps/i386/i686/multiarch/strcat-sse2.S (STRCAT): Avoid overflow in pointer addition. * sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S (STRCPY): Likewise. --- ChangeLog | 10 ++++++++++ string/test-strncat.c | 15 +++++++++++++++ sysdeps/i386/i686/multiarch/strcat-sse2.S | 2 ++ sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S | 2 ++ 4 files changed, 29 insertions(+) -- 2.7.4 diff --git a/string/test-strncat.c b/string/test-strncat.c index 55d6cc5..26a6b5b 100644 --- a/string/test-strncat.c +++ b/string/test-strncat.c @@ -284,12 +284,23 @@ test_main (void) do_test (0, 0, 8, 8, n, SMALL_CHAR); do_test (0, 8, 8, 8, n, SMALL_CHAR); + do_test (0, 2, 2, 2, SIZE_MAX, SMALL_CHAR); + do_test (0, 0, 4, 4, SIZE_MAX, SMALL_CHAR); + do_test (4, 0, 4, 4, SIZE_MAX, BIG_CHAR); + do_test (0, 0, 8, 8, SIZE_MAX, SMALL_CHAR); + do_test (0, 8, 8, 8, SIZE_MAX, SMALL_CHAR); + for (i = 1; i < 8; ++i) { do_test (0, 0, 8 << i, 8 << i, n, SMALL_CHAR); do_test (8 - i, 2 * i, 8 << i, 8 << i, n, SMALL_CHAR); do_test (0, 0, 8 << i, 2 << i, n, SMALL_CHAR); do_test (8 - i, 2 * i, 8 << i, 2 << i, n, SMALL_CHAR); + + do_test (0, 0, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR); + do_test (8 - i, 2 * i, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR); + do_test (0, 0, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR); + do_test (8 - i, 2 * i, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR); } for (i = 1; i < 8; ++i) @@ -297,6 +308,10 @@ test_main (void) do_test (i, 2 * i, 8 << i, 1, n, SMALL_CHAR); do_test (2 * i, i, 8 << i, 1, n, BIG_CHAR); do_test (i, i, 8 << i, 10, n, SMALL_CHAR); + + do_test (i, 2 * i, 8 << i, 1, SIZE_MAX, SMALL_CHAR); + do_test (2 * i, i, 8 << i, 1, SIZE_MAX, BIG_CHAR); + do_test (i, i, 8 << i, 10, SIZE_MAX, SMALL_CHAR); } } diff --git a/sysdeps/i386/i686/multiarch/strcat-sse2.S b/sysdeps/i386/i686/multiarch/strcat-sse2.S index 145ae66..6359c73 100644 --- a/sysdeps/i386/i686/multiarch/strcat-sse2.S +++ b/sysdeps/i386/i686/multiarch/strcat-sse2.S @@ -227,6 +227,8 @@ L(StartStrcpyPart): pxor %xmm0, %xmm0 # ifdef USE_AS_STRNCAT add %ecx, %ebx + sbb %edx, %edx + or %edx, %ebx # endif sub %ecx, %eax jmp L(Unalign16Both) diff --git a/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S b/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S index c038043..6a5ab7a 100644 --- a/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S +++ b/sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S @@ -99,6 +99,8 @@ L(Unalign16Both): sub %rcx, %rdi # ifdef USE_AS_STRNCPY add %rcx, %r8 + sbb %rcx, %rcx + or %rcx, %r8 # endif mov $16, %rcx movdqa (%rsi, %rcx), %xmm1