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 X-Patchwork-Id: 88227 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1018940qgi; Thu, 15 Dec 2016 12:39:04 -0800 (PST) X-Received: by 10.98.63.148 with SMTP id z20mr4006517pfj.151.1481834344588; Thu, 15 Dec 2016 12:39:04 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id k4si4075507plb.70.2016.12.15.12.39.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Dec 2016 12:39:04 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-75952-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-75952-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-75952-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=UAn+vaWnUJ75GVa1oQeAswF4sbxbrxUfuR29lYumu+lD3NxRepWtY PXp8iH0ijQGlRQ3M2avXI1lUewuNuGr4QQLp1E8nR9Tbs8CfaTW0JZTduCBvp2gT AQrASjAmM0dmu/hRXm6eUimqNyzRK6yq4UM2kQ4JCC4NY+RgzT3DXQ= 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=MSAOaOkot9pMnxlIWAWAKwR5iXA=; b=izFRfkVAcnilsGMYEFWC/UxcRR2r INb4QrVpKWE9DZp0fMEouvgxv+q26KH+WDYACpzT8bHMFZkyZj6PdWbzDFTrWQqX B3KLSg17K72nX2KBpmWtnZiKxG+QfOFTuWQdmyau1Z9irlGaF44hE2CeGL69ZLll zVO5BGENyc0gdCk= 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(-) -- 2.7.4 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