From patchwork Tue Jan 9 08:48:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Filippov X-Patchwork-Id: 857292 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=uclibc-ng.org (client-ip=2a00:1828:2000:679::23; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NcWjPekJ"; dkim-atps=neutral Received: from helium.openadk.org (helium.openadk.org [IPv6:2a00:1828:2000:679::23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zG5Qv06jKz9s7v for ; Tue, 9 Jan 2018 19:48:54 +1100 (AEDT) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id 518ED1011C; Tue, 9 Jan 2018 09:48:51 +0100 (CET) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from mail-lf0-f68.google.com (mail-lf0-f68.google.com [209.85.215.68]) by helium.openadk.org (Postfix) with ESMTPS id 5FDA41011C for ; Tue, 9 Jan 2018 09:48:50 +0100 (CET) Received: by mail-lf0-f68.google.com with SMTP id e27so15021946lfb.9 for ; Tue, 09 Jan 2018 00:48:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=AZI38H5rujyBH3c++SjDEcVQ7AuV/f24qPY7msTcX+w=; b=NcWjPekJ3CMLQRt218eE/xr0TlV47hpC2at43xgudF6tGOJrD21IhBqK4Thybs2OCa Sb7/CgFzQjVTodbanMKf5/xZlHeoMd7TDLhW0fWNf5KLOBvAqgdnsNK7lwPY8Wj+R03v hQXXi+VqaUuAsTUb9s4/gexhdvOW0RDqQcXMkRSHX24JzZ/TYDhTAy6R9aYo0b8qDXXB 7MmosLQdbvtT5dXxAQOYmPISNXWtWxwzFwiCI/8p9hLBp0wasyKQXxTbIGXU/eO06Wv2 e45TVu+b45JcBMXontVEDKduSUCH+JYAnSnQr44iJ2XCMvvL9UPQIEm1U8kbZX3WMtj+ SCuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=AZI38H5rujyBH3c++SjDEcVQ7AuV/f24qPY7msTcX+w=; b=OHPLrNpRkXp1/ZHpQZUexGuwjW9sdpovp5zB+nr7JL+F38hLQ5A0Zxdyny6fI/zEN3 YOQYP2Q52u2mpP2nU7+IDVel4YZbuSQr6LnJSUdxZpDEcVcmUn/b2IAgYUYI+PluIkEf SOOiNGCc+Baer4y/iyp/ZMEK0MOwacdbIMZfSQishO/Pp1W4qKV9+vrdXp5eHmoD+BG7 S9HMqbjD0QHbcY+zc09a2/SV3kb+jDuAiVXyvefhl2L6HgtddI1YAyGkzP3toIbo/LCs FwR4ZUyNg39yw3n1u8rac9V00sqpWQ7+EMnd0XlgUTocs7TeY8FyPpCg636h4B8PpD/r fAwA== X-Gm-Message-State: AKGB3mKfUnUX0osct/03qzcX3EZxGxN2JFv5V4MA/FhhOGysUeLrISKI 8nkxqW0gG85pCS+Ygf8V+0vBUQ== X-Google-Smtp-Source: ACJfBouyoQjdavS9oF/lgh9FYsG4iGTdYGc+cpLAnnOxiG30ht26MbQ2IzONfbvojS87CHg2ktTxzQ== X-Received: by 10.46.71.21 with SMTP id u21mr7876049lja.67.1515487729615; Tue, 09 Jan 2018 00:48:49 -0800 (PST) Received: from octofox.net (jcmvbkbc-1-pt.tunnel.tserv24.sto1.ipv6.he.net. [2001:470:27:1fa::2]) by smtp.gmail.com with ESMTPSA id h11sm2416702lfd.35.2018.01.09.00.48.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 09 Jan 2018 00:48:49 -0800 (PST) From: Max Filippov To: devel@uclibc-ng.org Date: Tue, 9 Jan 2018 00:48:31 -0800 Message-Id: <1515487711-26507-1-git-send-email-jcmvbkbc@gmail.com> X-Mailer: git-send-email 2.1.4 Cc: linux-xtensa@linux-xtensa.org, Chris Zankel Subject: [uclibc-ng-devel] [PATCH v2] xtensa: fix strcmp X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" Loops with 'loop forever' annotation inside strcmp are actually meant to loop forever. Falling through the end of the first loop may result in equal strings being compared unequal, e.g.: #include int main(void) { char a[4096] __attribute__((aligned(4))); char b[4096] __attribute__((aligned(4))); memset(a, ' ', 258 * 8); memset(b, ' ', 258 * 8); a[255 * 8] = 0; a[256 * 8] = 'a'; b[255 * 8] = 0; b[256 * 8] = 'b'; return !(strcmp(a, b) == 0); } Falling through the end of the second loop may result in unequal strings being compared as equal, e.g.: #include int main(void) { char a[4096] __attribute__((aligned(4))); char b[4096] __attribute__((aligned(4))); memset(a, ' ', 514 * 6); memset(b, ' ', 514 * 6); a[514 * 6 + 0] = 'a'; a[514 * 6 + 1] = 0; b[514 * 6 + 0] = 'b'; b[514 * 6 + 1] = 0; return !(strcmp(a, b) != 0); } Use 0 as a loop counter to make 2^32 - 1 iterations which is enough to cover all addressable memory. While at it drop useless nop at the end of the first loop and use a11 for all loop counters. Signed-off-by: Max Filippov --- Changes v1->v2: - wrong test case for the second loop, fixed. libc/string/xtensa/strcmp.S | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/libc/string/xtensa/strcmp.S b/libc/string/xtensa/strcmp.S index 2dce590dbd42..a16da5da2e92 100644 --- a/libc/string/xtensa/strcmp.S +++ b/libc/string/xtensa/strcmp.S @@ -92,8 +92,8 @@ ENTRY (strcmp) /* (2 mod 4) alignment for loop instruction */ .Lunaligned: #if XCHAL_HAVE_LOOPS - _movi.n a8, 0 /* set up for the maximum loop count */ - loop a8, .Lretdiff /* loop forever (almost anyway) */ + movi a11, 0 /* set up for the maximum loop count */ + loop a11, .Lretdiff /* loop forever (almost anyway) */ #endif .Lnextbyte: l8ui a8, a2, 0 @@ -131,11 +131,10 @@ ENTRY (strcmp) #if XCHAL_HAVE_LOOPS .Laligned: .begin no-transform + movi a11, 0 l32r a4, .Lmask0 /* mask for byte 0 */ l32r a7, .Lmask4 - /* Loop forever. (a4 is more than than the maximum number - of iterations) */ - loop a4, .Laligned_done + loop a11, .Laligned_done /* Loop forever. */ /* First unrolled loop body. */ l32i a8, a2, 0 /* get word from s1 */ @@ -156,8 +155,6 @@ ENTRY (strcmp) addi a2, a2, 8 /* advance s1 pointer */ addi a3, a3, 8 /* advance s2 pointer */ .Laligned_done: - or a1, a1, a1 /* nop */ - .Lprobeq2: /* Adjust pointers to account for the loop unrolling. */ addi a2, a2, 4 @@ -198,7 +195,7 @@ ENTRY (strcmp) #if XCHAL_HAVE_LOOPS /* align (1 mod 4) */ - loop a4, .Leq /* loop forever (a4 is bigger than max iters) */ + loop a11, .Leq /* loop forever */ .end no-transform l32i a8, a2, 0 /* get word from s1 */