Message ID | 20221222140506.1961281-1-linux@rasmusvillemoes.dk |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp344076wrn; Thu, 22 Dec 2022 06:07:44 -0800 (PST) X-Google-Smtp-Source: AMrXdXtqsuIDuBeROsuvPFx/CmGtMnph3R5gL8xT9M3ykJ9+yCrK8GAGqbcZGMfBil3+G7jJkOEA X-Received: by 2002:a50:eac6:0:b0:45a:7d2:9b35 with SMTP id u6-20020a50eac6000000b0045a07d29b35mr5198246edp.0.1671718064489; Thu, 22 Dec 2022 06:07:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671718064; cv=none; d=google.com; s=arc-20160816; b=ePUh3ySObKXJV0Fkwq/CdIH2LI0dpXmJMB1kvVLtQDXzfGRjuaPpyYzokRGZTFvLiv Mm2Wr/1HLdQG4ATCE7geEJbHp0YBvIfNZoKKRzl+m/DFR2DBOSpixRrJOX3oop7YNxXq AiyBGYXAj30AU5gn8VDP8HOtebU0HurNrjgfqS8P8BEJNInP9kW16mR3ra3tkXCG5M6M WVM/XJxAuKJeYtRCfnZ+uCYjISD6dpqa3iZ2gqBc17JNvNYhJio1e7ZeLVrWJ1oRHXi8 51b1Dx4KohEfnde+GSepZ8a/ItnEHfFoW0gAwsm2Qnz5tl+kJ9Whutgk71466KrlUTPw IJ/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=H4bOXqBLEUAELZx0PBkLRni+ahOZSxFQFD0xQwRwD7M=; b=r1oJOcwxtSyfIScHXDuMSv13xFvMs+OUuPyvFQ+W5Bjn1B0h54X+Nt0E4yar62DBhY NKN0B9uBZp7c0eIXjhsQTjECCWgXXZdmJ84JSUKNqmEOFpCB5JfaZjGi6Feukxh5gj+b w0S1QvUYKlNrggLT7HgOiHKsGhleg2XfYvDgVtOocWc5RpxYf4pCMLEBEZsjR03nH7vF bWxIM6vT0231/kg3nZyuHIXRlGGxIJ0brpI+sAQol3sZAULwuwNaKAGQXB0RPZ7vI9nK AMSpDJJ8pByR+UymiaIIFaS9TNCSd9ie6ylF7U8C/vBwx3tmK56a8fEJ1LjQFyVlWK23 tfgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b="R/YuABqQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f8-20020a056402354800b004614acc0706si863051edd.250.2022.12.22.06.07.10; Thu, 22 Dec 2022 06:07:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b="R/YuABqQ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235382AbiLVOFQ (ORCPT <rfc822;pacteraone@gmail.com> + 99 others); Thu, 22 Dec 2022 09:05:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235369AbiLVOFN (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 22 Dec 2022 09:05:13 -0500 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D53F12716E for <linux-kernel@vger.kernel.org>; Thu, 22 Dec 2022 06:05:11 -0800 (PST) Received: by mail-lf1-x12d.google.com with SMTP id g13so2869837lfv.7 for <linux-kernel@vger.kernel.org>; Thu, 22 Dec 2022 06:05:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=H4bOXqBLEUAELZx0PBkLRni+ahOZSxFQFD0xQwRwD7M=; b=R/YuABqQYNQjUDTuiFo7CrY5tcfWuW86rLqIi+gm8z7bYA/mnOwVSxfWDQRYWzbuez cg7hRymS7avgjEG+pQ9SywyNYDFWlYJ70Ijl8XSTwUogGvWUEtIe/wPxDxhAvONH8IXG ipKLu+oTCNSol50sSYdmGeDRg1EXgzceYZWR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=H4bOXqBLEUAELZx0PBkLRni+ahOZSxFQFD0xQwRwD7M=; b=H9QbyGcVL1We2fwIz3+2hzLRauCYO5130z1zhvWg53gD2NBZ0HMwyFLzU+8WmDtPSd pGpTL46u2beG7shCoVWz1mHVpsf+Ff6wNm0x2aufCtR+qeVFiiR2leXKyKWnAH5uJWnO j1ylxmltmmEmGm5HN88orDx245DIFermoDsGYl2RuVlJnJZ+uaO4U7bsAFhUjOouJ2ua jtHKWse9DtYKxpA54jIGgQKSnppf8Pfhpk7l81BccgzjGJSpAqiRm4t4wvPieHKDFA7P YfMToUXPW11ElVfbd8HFbi1ojEH+g4BIeUIRH7iR6pUh9CS85nPdOMxWFUiV9BeIiQNt g/RQ== X-Gm-Message-State: AFqh2kqRxl7sAA7h+v7Qc+Gf9GrXKub9yWMz+3PSsntelHGe4mUIf0Ih ViHm+vz+m91RO2zZqRv3r7R54Q== X-Received: by 2002:a19:c20d:0:b0:4b5:7859:349f with SMTP id l13-20020a19c20d000000b004b57859349fmr1519190lfc.48.1671717910195; Thu, 22 Dec 2022 06:05:10 -0800 (PST) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id x11-20020a0565123f8b00b004b5adb59ed5sm75261lfa.297.2022.12.22.06.05.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 06:05:09 -0800 (PST) From: Rasmus Villemoes <linux@rasmusvillemoes.dk> To: Linus Torvalds <torvalds@linux-foundation.org>, Andy Shevchenko <andy@kernel.org> Cc: Jason Donenfeld <Jason@zx2c4.com>, Kees Cook <keescook@chromium.org>, Andrew Morton <akpm@linux-foundation.org>, Rasmus Villemoes <linux@rasmusvillemoes.dk>, linux-kernel@vger.kernel.org Subject: [PATCH] string.c: test *cmp for all possible 1-character strings Date: Thu, 22 Dec 2022 15:05:06 +0100 Message-Id: <20221222140506.1961281-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752923441086590711?= X-GMAIL-MSGID: =?utf-8?q?1752923441086590711?= |
Series |
string.c: test *cmp for all possible 1-character strings
|
|
Commit Message
Rasmus Villemoes
Dec. 22, 2022, 2:05 p.m. UTC
The switch to -funsigned-char made a pre-existing bug on m68k more
apparent. That is now fixed (by removing m68k's private strcmp(), see
commit 7c0846125358), but we still have quite a few architectures that
provide one or more of strcmp(), strncmp() and memcmp().
They probably all work fine for the cases where the input is all
ASCII, and/or where the caller only wants to know about equality or
not (i.e. only checks whether the return value is 0 or not).
Let's check that all these implementations also behave correctly for
bytes with the high bit set, and provide the correct ordering -
independent of us now building with -funsigned-char, the C standard
says that these *cmp functions should consider the buffers as
consisting of unsigned chars.
This is only intended to help find other latent bugs and can/should be
ripped out again before v6.2, or perhaps moved to test_string.c in
some form, but for now I think it's worth doing unconditionally.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/string.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
Comments
On Thu, Dec 22, 2022 at 03:05:06PM +0100, Rasmus Villemoes wrote: > The switch to -funsigned-char made a pre-existing bug on m68k more > apparent. That is now fixed (by removing m68k's private strcmp(), see > commit 7c0846125358), but we still have quite a few architectures that > provide one or more of strcmp(), strncmp() and memcmp(). > > They probably all work fine for the cases where the input is all > ASCII, and/or where the caller only wants to know about equality or > not (i.e. only checks whether the return value is 0 or not). > > Let's check that all these implementations also behave correctly for > bytes with the high bit set, and provide the correct ordering - > independent of us now building with -funsigned-char, the C standard > says that these *cmp functions should consider the buffers as > consisting of unsigned chars. > > This is only intended to help find other latent bugs and can/should be > ripped out again before v6.2, or perhaps moved to test_string.c in > some form, but for now I think it's worth doing unconditionally. > > Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> > --- > lib/string.c | 27 +++++++++++++++++++++++++++ > 1 file changed, 27 insertions(+) > > diff --git a/lib/string.c b/lib/string.c > index 4fb566ea610f..1718f96e8082 100644 > --- a/lib/string.c > +++ b/lib/string.c > @@ -880,3 +880,30 @@ void *memchr_inv(const void *start, int c, size_t bytes) > return check_bytes8(start, value, bytes % 8); > } > EXPORT_SYMBOL(memchr_inv); > + > +static int sign(int x) > +{ > + return (x > 0) - (x < 0); > +} > + > +static int test_xxxcmp(void) > +{ > + char a[2], b[2]; > + int i, j; > + > + a[1] = b[1] = 0; > + for (i = 0; i < 256; ++i) { > + a[0] = i; > + for (j = 0; j < 256; ++j) { > + b[0] = j; > + WARN_ONCE(sign(strcmp(a, b)) != sign(i - j), > + "strcmp() broken for (%2ph, %2ph)\n", a, b); > + WARN_ONCE(sign(memcmp(a, b, 2)) != sign(i - j), > + "memcmp() broken for (%2ph, %2ph)\n", a, b); > + WARN_ONCE(sign(strncmp(a, b, 2)) != sign(i - j), > + "strncmp() broken for (%2ph, %2ph)\n", a, b); > + } > + } > + return 0; > +} > +late_initcall(test_xxxcmp); This probably belongs in some config-gated selftest file that can be compiled out, rather than running unconditionally on every boot, right? Jason
On 22/12/2022 16.15, Jason A. Donenfeld wrote: > On Thu, Dec 22, 2022 at 03:05:06PM +0100, Rasmus Villemoes wrote: >> This is only intended to help find other latent bugs and can/should be >> ripped out again before v6.2, or perhaps moved to test_string.c in >> some form, but for now I think it's worth doing unconditionally. >> > This probably belongs in some config-gated selftest file that can be > compiled out, rather than running unconditionally on every boot, right? I believe this was already answered in the last paragraph of the commit log. Rasmus
Hi Rasmus, I love your patch! Yet something to improve: [auto build test ERROR on linux/master] [also build test ERROR on linus/master v6.1 next-20221220] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/string-c-test-cmp-for-all-possible-1-character-strings/20221222-220708 patch link: https://lore.kernel.org/r/20221222140506.1961281-1-linux%40rasmusvillemoes.dk patch subject: [PATCH] string.c: test *cmp for all possible 1-character strings config: riscv-randconfig-r042-20221219 compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 98b13979fb05f3ed288a900deb843e7b27589e58) reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # install riscv cross compiling tool for clang build # apt-get install binutils-riscv64-linux-gnu # https://github.com/intel-lab-lkp/linux/commit/0235c6544a848ef03332c7840c87b356c08a4b1d git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Rasmus-Villemoes/string-c-test-cmp-for-all-possible-1-character-strings/20221222-220708 git checkout 0235c6544a848ef03332c7840c87b356c08a4b1d # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv SHELL=/bin/bash If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> All errors (new ones prefixed by >>): >> ld.lld: error: undefined symbol: __warn_printk >>> referenced by ctype.c >>> arch/riscv/purgatory/purgatory.ro:(test_xxxcmp) >>> referenced by ctype.c >>> arch/riscv/purgatory/purgatory.ro:(test_xxxcmp) >>> referenced by ctype.c >>> arch/riscv/purgatory/purgatory.ro:(test_xxxcmp)
Hi Rasmus,
I love your patch! Yet something to improve:
[auto build test ERROR on linux/master]
[also build test ERROR on linus/master v6.1 next-20221220]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Rasmus-Villemoes/string-c-test-cmp-for-all-possible-1-character-strings/20221222-220708
patch link: https://lore.kernel.org/r/20221222140506.1961281-1-linux%40rasmusvillemoes.dk
patch subject: [PATCH] string.c: test *cmp for all possible 1-character strings
config: riscv-allyesconfig
compiler: riscv64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/0235c6544a848ef03332c7840c87b356c08a4b1d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rasmus-Villemoes/string-c-test-cmp-for-all-possible-1-character-strings/20221222-220708
git checkout 0235c6544a848ef03332c7840c87b356c08a4b1d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=riscv olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=riscv SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
All errors (new ones prefixed by >>):
riscv64-linux-ld: arch/riscv/purgatory/purgatory.ro: in function `.L13':
>> string.c:(.text+0x1832): undefined reference to `__warn_printk'
riscv64-linux-ld: arch/riscv/purgatory/purgatory.ro: in function `.L3':
string.c:(.text+0x187a): undefined reference to `__warn_printk'
riscv64-linux-ld: arch/riscv/purgatory/purgatory.ro: in function `.L6':
string.c:(.text+0x18c4): undefined reference to `__warn_printk'
diff --git a/lib/string.c b/lib/string.c index 4fb566ea610f..1718f96e8082 100644 --- a/lib/string.c +++ b/lib/string.c @@ -880,3 +880,30 @@ void *memchr_inv(const void *start, int c, size_t bytes) return check_bytes8(start, value, bytes % 8); } EXPORT_SYMBOL(memchr_inv); + +static int sign(int x) +{ + return (x > 0) - (x < 0); +} + +static int test_xxxcmp(void) +{ + char a[2], b[2]; + int i, j; + + a[1] = b[1] = 0; + for (i = 0; i < 256; ++i) { + a[0] = i; + for (j = 0; j < 256; ++j) { + b[0] = j; + WARN_ONCE(sign(strcmp(a, b)) != sign(i - j), + "strcmp() broken for (%2ph, %2ph)\n", a, b); + WARN_ONCE(sign(memcmp(a, b, 2)) != sign(i - j), + "memcmp() broken for (%2ph, %2ph)\n", a, b); + WARN_ONCE(sign(strncmp(a, b, 2)) != sign(i - j), + "strncmp() broken for (%2ph, %2ph)\n", a, b); + } + } + return 0; +} +late_initcall(test_xxxcmp);