From patchwork Thu Dec 15 20:38:33 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: 18498 Received: (qmail 39643 invoked by alias); 15 Dec 2016 20:38:53 -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 39629 invoked by uid 89); 15 Dec 2016 20:38:52 -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=uintptr_t X-HELO: mail-ua0-f178.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=VAMcwu47GXE8chwZA/tBoaTNsNxGpv3hWJJ4C5/PBc0=; b=TtXs8sLzyGTBzvziVH9+jYkEDYcmgA0CPWOsHsxV9qllcAT9l444cKVHqDRFL+1jtn 6OzZDWgyBcuJTx+SONYaE16NUckifevh70qWpHvs60FOfrOKvGTc60XXGKKyku4yRMYp GZzd8FQXZr0r1I9bWJA85ehpd1YSJIrI3S0J6R9JJJ2qqt3SdNh5LwQZQEH590NC/f/O wBZpWjpYHhRPVDXv7JqysPnEO4fAhZWh0OB04bBWHAevsGDBakLtsSs4petmg6kL6Ahu m9lSglzg914cmrt/2x/uWgsSEW4pN8T7ARfvB5rXyxaSldfncL4XWEAA8R8DPK7zFdcm WfDg== X-Gm-Message-State: AKaTC02v6U4GhmBSrQ2tJQvd/CKyqmBTZ0j+QpjYGjMPj8znoeEM45ym4IrBjGBwezX1Ie3E X-Received: by 10.176.83.100 with SMTP id y33mr4649982uay.130.1481834320468; Thu, 15 Dec 2016 12:38:40 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] Fix x86_64 memchr for large input sizes Date: Thu, 15 Dec 2016 18:38:33 -0200 Message-Id: <1481834313-5626-1-git-send-email-adhemerval.zanella@linaro.org> 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). This is similar of the fix for powerpc64/power7 version [1] and rely on for test-memchr.c changes. Checked on x86_64-linux-gnu and powerpc64-linux-gnu. [1] https://sourceware.org/ml/libc-alpha/2016-12/msg00576.html [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. --- ChangeLog | 6 ++++++ string/test-memchr.c | 9 ++++----- sysdeps/x86_64/memchr.S | 8 ++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/string/test-memchr.c b/string/test-memchr.c index 7431386..6901711 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, 32, SIZE_MAX, 23); - do_test (i, 16 - j, 32, 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..b140de1 100644 --- a/sysdeps/x86_64/memchr.S +++ b/sysdeps/x86_64/memchr.S @@ -76,7 +76,15 @@ 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 %eax, %eax + movslq %eax, %rax + orq %rdx, %rax + mov %rax, %rdx sub $16, %rdx jbe L(return_null) add $16, %rdi