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: 708821 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 3tnVST05zKz9syB for ; Tue, 27 Dec 2016 07:08:56 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="bnNvl8YE"; dkim-atps=neutral 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=WgeqikMiW9dcHj907hZ0dXvoGCQTKAWASwUAYohOdFTOP2ESBTIrB cSkj/GRskdmxdWqO0LVUM3IpI0QILbJ3z5aJckaJ/wLFNF52JfVyb7/ocxtbA6Cl cVNdAi+kieCtBHz/DfxlvAcJMdERuPBMGzfwRldWQ+KNvfgx7QZOZc= 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=bnNvl8YE1H4fDwC2kVLKGQjdty1d 09nebzkm8p0Edq1gxECpQiXIB+jVbYaghOEGaIcRBSO9dK2kUgcPYPX6f4YFaYeO pwqmk+mpuGIBifcWOj1XOuD6mQmzUkgnw8rtTnctRWb6JpwtU7bjwcG1ma1lvWBd ET+idoX+nQj9KIM= 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(-) 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