From patchwork Mon Dec 26 20:08:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 89032 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp4679598qgi; Mon, 26 Dec 2016 12:09:00 -0800 (PST) X-Received: by 10.99.128.198 with SMTP id j189mr50966891pgd.180.1482782940033; Mon, 26 Dec 2016 12:09:00 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q5si22242731pgi.316.2016.12.26.12.08.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Dec 2016 12:09:00 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-76342-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-76342-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-76342-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=cPf0Gcz/sBQRpTMTGpVl22yqk+9MmeCAatx8rMJdk/iI9z42zTcYD 4rB6Ebf2WRymY1xh0GqQyxcge6WPB7ov11dZ9b/Tl9/rjdESx/VclZVncyzdQkRi qYesHKYMrA7XeNL0pgvN9BBZRCPJ0L+Vi+6nFwsCUld3yk/t1S6gy8= 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=jEYiK6DeqTEw6NdECqSQHf4XC0Y=; b=gVgXGxzZ9s+K7/jUL/l6zUhAImb3 YbL4yuolDVKkZtYtmy+tQ+AUPLjH59CDnKy7ue0r3bdWkAq3s9CX4r0iezTkX9cg KEEzsHeKapzL8x1x9/9hBKj7KlgtESOSj462vvzTLcTlPXAi7XQ1OOW5/F0mSPwb shi0u5GtAfrAVtY= Received: (qmail 113244 invoked by alias); 26 Dec 2016 20:08:50 -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 113230 invoked by uid 89); 26 Dec 2016 20:08:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=saturated, UINTPTR_MAX, uintptr_max X-HELO: mail-yw0-f174.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=bZTLdyl1mSfrVyGZl3pD2BBxdmKsOs6rE0pGx0hsd1E=; b=D1tlZHQydZdkeD8od0JNRnTtfuVvfT2a2lNdynpJNxL/EXlK6ED6YFn/T8i8/R4m30 etKlNd1diXJQiY+u+kEBGCJ/LspKDBjR0Jp25mov9Ule2NeydODOq1JHMRwZ3MQmUPQR 77B1xzXip5mOjjvo4yPH7z1nYi4IqJFY6vIAJk7+VW7AlJN7EAJjbbqI8luyW/o2VzaN RIxxtsWslzrSEs0cqwuaOsJJzmYrOTAntW01Tbrf4DP8RoLAhOrGy3ibXnu43n1OI5rH UO8hrgWJ2P5l4gpAvb1VmxPRHcurcKd9z1z8AjgOth380zx6t5ZynTsYUldRhm5BCGar kAiw== X-Gm-Message-State: AIkVDXIYknrfAP8b2hqiwfqViFgHVlsi0CmoIM/EX+Ot2w5u+8nwjQM0AybxjTk/WgQgG7pM X-Received: by 10.129.145.85 with SMTP id i82mr23979271ywg.263.1482782918078; Mon, 26 Dec 2016 12:08:38 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] Fix x86_64 memchr for large input sizes Date: Mon, 26 Dec 2016 18:08:30 -0200 Message-Id: <1482782910-22756-1-git-send-email-adhemerval.zanella@linaro.org> Changes from previous version - Simplify uintptr_t generation and check. If noone opposes it I will commit it shortly. -- Current optimized memchr for x86_64 does for input arguments pointers module 64 in range of [49,63] if there is no searchr char in the rest of 64-byte block a pointer addition which might overflow: * sysdeps/x86_64/memchr.S 77 .p2align 4 78 L(unaligned_no_match): 79 add %rcx, %rdx Add (uintptr_t)s % 16 to n in %rdx. 80 sub $16, %rdx 81 jbe L(return_null) This patch fixes by adding a saturated math that sets a maximum pointer value if it overflows (UINTPTR_MAX). Checked on x86_64-linux-gnu and powerpc64-linux-gnu. [BZ# 19387] * sysdeps/x86_64/memchr.S (memchr): Avoid overflow in pointer addition. * string/test-memchr.c (do_test): Remove alignment limitation. (test_main): Add test that trigger BZ# 19387. --- string/test-memchr.c | 9 ++++----- sysdeps/x86_64/memchr.S | 6 ++++++ 3 files changed, 18 insertions(+), 5 deletions(-) -- 2.7.4 diff --git a/string/test-memchr.c b/string/test-memchr.c index 0690cb4..10d696a 100644 --- a/string/test-memchr.c +++ b/string/test-memchr.c @@ -76,7 +76,6 @@ do_test (size_t align, size_t pos, size_t len, size_t n, int seek_char) size_t i; CHAR *result; - align &= 7; if ((align + len) * sizeof (CHAR) >= page_size) return; @@ -194,12 +193,12 @@ test_main (void) do_test (i, 64, 256, SIZE_MAX, 0); } - for (i = 1; i < 16; ++i) + for (i = 1; i < 64; ++i) { - for (j = 1; j < 16; j++) + for (j = 1; j < 64; j++) { - do_test (0, 16 - j, 16, SIZE_MAX, 23); - do_test (i, 16 - j, 16, SIZE_MAX, 23); + do_test (0, 64 - j, 64, SIZE_MAX, 23); + do_test (i, 64 - j, 64, SIZE_MAX, 23); } } diff --git a/sysdeps/x86_64/memchr.S b/sysdeps/x86_64/memchr.S index 132eacb..66e986c 100644 --- a/sysdeps/x86_64/memchr.S +++ b/sysdeps/x86_64/memchr.S @@ -76,7 +76,13 @@ L(crosscache): .p2align 4 L(unaligned_no_match): + /* Calculate the last acceptable address and check for possible + addition overflow by using satured math: + rdx = rcx + rdx + rdx |= -(rdx < x) */ add %rcx, %rdx + sbb %rax, %rax + or %rax, %rdx sub $16, %rdx jbe L(return_null) add $16, %rdi