lib/test_string.c: Add test for strlen()
PATCH 1/1 lib/test_string.c: Add test for strlen()
Add a simple test for strlen() functionality, including using it as a constant expression.
---
I'll be taking this as part of my Clang FORTIFY_SOURCE series.
--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
lib/test_string.c +37 -0
@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ; } +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); + +static __init int strlen_selftest (void ) +{ +/* String length ruler: 123456789012345 */ +static const char normal[] ="I am normal" ; +static const char *ptr ="where do I go?" ; +static const char trailing[] ="hidden NULLs\0\0\0" ; +static const char leading[] ="\0\0hidden text" ; + +if (strlen (normal) !=11 ) +return 0x100001 ; +if (strlen (ptr++) !=14 ) +return 0x100002 ; +if (strlen (ptr++) !=13 ) +return 0x100003 ; +if (strlen (trailing) !=12 ) +return 0x100004 ; +if (strlen (leading) !=0 ) +return 0x100005 ; +if (strlen_ce !=27 ) +return 0x100006 ; + +return 0 ; +} + static __exit void string_selftest_remove(void) { }
@@ -212,6 +244,11 @@ static __init int string_selftest_init(void)if (subtest) goto fail; + test =5 ; + subtest =strlen_selftest (); +if (subtest) + goto fail; +pr_info ("String selftests succeeded\n" );return 0 ; fail:
Signature · 1 line
2.30.2
Quoted 3 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.
...
lib/test_string.c static __init int strnchr_selftest(void) · line 188
Quoted 7 lines
lib/test_string.c +7
-0+/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" );
So, the compiler will replace this by a constant and then eliminate the condition completely from the code. Did I understand this correctly?
Quoted 11 lines
lib/test_string.c +11
-0+static __init int strlen_selftest (void ) +{ +/* String length ruler: 123456789012345 */ +static const char normal[] ="I am normal" ; +static const char *ptr ="where do I go?" ; +static const char trailing[] ="hidden NULLs\0\0\0" ; +static const char leading[] ="\0\0hidden text" ; + +if (strlen (normal) !=11 ) +return 0x100001 ; +if (strlen (ptr++) !=14 )
lib/test_string.c static __init int strnchr_selftest(void) · line 209
Quoted 10 lines
lib/test_string.c +9
-0+return 0x100002 ; +if (strlen (ptr++) !=13 ) +return 0x100003 ; +if (strlen (trailing) !=12 ) +return 0x100004 ; +if (strlen (leading) !=0 ) +return 0x100005 ; +if (strlen_ce !=27 ) +return 0x100006 ;
...so this part won't ever appear in the assembly (assuming -O2).
Same to the rest? If so, why is this not a part of the compiler tests?
Quoted 2 lines
lib/test_string.c +2
-0+return 0 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression....
Quoted 11 lines
lib/test_string.c static __init int strnchr_selftest(void) · line 188
Quoted 7 lines
lib/test_string.c +7
-0+/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" );So, the compiler will replace this by a constant and then eliminate the condition completely from the code. Did I understand this correctly?
Yup! See: https://godbolt.org/z/nTqPaszTh
There a few rare places in the kernel that do this, which is how I noticed. (I broke strlen() with the recent FORTIFY changes.)
Quoted 19 lines
Quoted 18 lines
lib/test_string.c +18
-0+static __init int strlen_selftest (void ) +{ +/* String length ruler: 123456789012345 */ +static const char normal[] ="I am normal" ; +static const char *ptr ="where do I go?" ; +static const char trailing[] ="hidden NULLs\0\0\0" ; +static const char leading[] ="\0\0hidden text" ; + +if (strlen (normal) !=11 ) +return 0x100001 ; +if (strlen (ptr++) !=14 ) +return 0x100002 ; +if (strlen (ptr++) !=13 ) +return 0x100003 ; +if (strlen (trailing) !=12 ) +return 0x100004 ; +if (strlen (leading) !=0 ) +return 0x100005 ;
Quoted 4 lines
lib/test_string.c static __init int strnchr_selftest(void) · line 209
Quoted 2 lines
lib/test_string.c +2
-0+if (strlen_ce !=27 ) +return 0x100006 ;...so this part won't ever appear in the assembly (assuming -O2).
Correct, unless strlen() breaks.
Quoted 1 line
Same to the rest? If so, why is this not a part of the compiler tests?
I wanted to keep everything together -- this includes a macro side-effect test as well ("ptr++").
-Kees
Signature · 1 line
Kees Cook
kernel test robot
build botI love your patch! Yet something to improve:
Hi Kees,
I love your patch! Yet something to improve:
[auto build test ERROR on linux/master]
[also build test ERROR on linus/master kees/for-next/pstore v5.17-rc2 next-20220128] [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]
url: https://github.com/0day-ci/linux/commits/Kees-Cook/lib-test_string-c-Add-test-for-strlen/20220131-023726 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2c271fe77d52a0555161926c232cd5bc07178b39 config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20220131/202201310303.HQlCsvvd-lkp(a)intel.com/config) compiler: sh4-linux-gcc (GCC) 11.2.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/0day-ci/linux/commit/d22fe883339c5141953dbca980b51466ac3e2329
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kees-Cook/lib-test_string-c-Add-test-for-strlen/20220131-023726 git checkout d22fe883339c5141953dbca980b51466ac3e2329
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=sh SHELL=/bin/bash M=lib/
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> lib/test_string.c:188:30: error: initializer element is not constant
188 | static const int strlen_ce = strlen("tada, a constant expression");
| ^~~~~~vim +188 lib/test_string.c
181
182 /*
183 * Unlike many other string functions, strlen() can be used in
184 * static initializers when string lengths are known at compile
185 * time. (i.e. Under these conditions, strlen() is a constant
186 * expression.) Make sure it can be used this way.
187 */
> 188 static const int strlen_ce = strlen("tada, a constant expression");189
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 25 lines · kernel test robot
Hi Kees,
I love your patch! Yet something to improve:
[auto build test ERROR on linux/master]
[also build test ERROR on linus/master kees/for-next/pstore v5.17-rc2 next-20220128] [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]url: https://github.com/0day-ci/linux/commits/Kees-Cook/lib-test_string-c-Add-test-for-strlen/20220131-023726 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 2c271fe77d52a0555161926c232cd5bc07178b39 config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20220131/202201310303.HQlCsvvd-lkp(a)intel.com/config) compiler: sh4-linux-gcc (GCC) 11.2.0reproduce (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/0day-ci/linux/commit/d22fe883339c5141953dbca980b51466ac3e2329
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Kees-Cook/lib-test_string-c-Add-test-for-strlen/20220131-023726 git checkoutd22fe883339c5141953dbca980b51466ac3e2329
# save the config file to linux build tree
mkdirbuild_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=sh SHELL=/bin/bash M=lib/
Quoted 8 lines
If you fix the issue, kindly add following tag as appropriate
Reported-by kernel test robot <[email protected]>All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
lib/test_string.c:188:30: error: initializer element is not constant188 | static const int strlen_ce = strlen("tada, a constant expression"); | ^~~~~~
Thanks; this is expected -- the strlen() in -next is currently broken (and will be fixed in the next -next).
-Kees
Quoted 17 lines
vim +188
lib/test_string.c181
182 /*
183 * Unlike many other string functions,strlen()can be used in
184 * static initializers when string lengths are known at compile
185 * time. (i.e. Under these conditions,strlen()is a constant
186 * expression.) Make sure it can be used this way.
187 */Quoted 1 line
188 static const int
strlen_ce=strlen("tada, a constant expression");189
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Signature · 1 line
Kees Cook
Quoted 18 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.Cc Geert Uytterhoeven <[email protected]>Cc Peter Rosin <[email protected]>Signed-off-by Kees Cook <[email protected]>---
I'll be taking this as part of my ClangFORTIFY_SOURCEseries.--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)lib/test_string.c +0
-0@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ;
lib/test_string.c static __init int strnchr_selftest(void) · line 189
Quoted 10 lines
lib/test_string.c +8
-0} +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); +
This results in:
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const int strlen_ce = strlen("tada, a constant expression");
for several of my tests. I don't think you can mandate that a compiler implements this.
Guenter
Quoted 42 lines
lib/test_string.c +29
-0+static __init int strlen_selftest (void ) +{ +/* String length ruler: 123456789012345 */ +static const char normal[] ="I am normal" ; +static const char *ptr ="where do I go?" ; +static const char trailing[] ="hidden NULLs\0\0\0" ; +static const char leading[] ="\0\0hidden text" ; + +if (strlen (normal) !=11 ) +return 0x100001 ; +if (strlen (ptr++) !=14 ) +return 0x100002 ; +if (strlen (ptr++) !=13 ) +return 0x100003 ; +if (strlen (trailing) !=12 ) +return 0x100004 ; +if (strlen (leading) !=0 ) +return 0x100005 ; +if (strlen_ce !=27 ) +return 0x100006 ; + +return 0 ; +} + static __exit void string_selftest_remove(void) { }@@ -212,6 +244,11 @@ static __init int string_selftest_init(void)if (subtest) goto fail; + test =5 ; + subtest =strlen_selftest (); +if (subtest) + goto fail; +pr_info ("String selftests succeeded\n" );return 0 ; fail:Signature · 1 line
2.30.2
Quoted 1 line · Guenter Roeck
On Sun, Jan 30, 2022 at 10:36:53AM -0800, Kees Cook wrote:
...
Quoted 9 lines
Quoted 1 line
+static const int
strlen_ce=strlen("tada, a constant expression");This results in:
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");for several of my tests. I don't think you can mandate that a compiler implements this.
With -O2 probably one can, otherwise I agree.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 32 lines · Guenter Roeck
Quoted 18 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.Cc Geert Uytterhoeven <[email protected]>Cc Peter Rosin <[email protected]>Signed-off-by Kees Cook <[email protected]>---
I'll be taking this as part of my ClangFORTIFY_SOURCEseries.--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)lib/test_string.c +0
-0@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ;lib/test_string.c static __init int strnchr_selftest(void) · line 189
Quoted 10 lines
lib/test_string.c +8
-0} +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); +This results in:
Quoted 5 lines
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");for several of my tests. I don't think you can mandate that a compiler implements this.
Which tests?
This property of strlen() is already required by our builds (this is how I tripped over it). For example:
drivers/firmware/xilinx/zynqmp-debug.c:
#define PM_API(id) {id, #id, strlen(#id)}
static struct pm_api_info pm_api_list[] = {
PM_API(PM_GET_API_VERSION),
PM_API(PM_QUERY_DATA),
};Quoted 45 lines
Guenter
Quoted 42 lines
lib/test_string.c +29
-0+static __init int strlen_selftest (void ) +{ +/* String length ruler: 123456789012345 */ +static const char normal[] ="I am normal" ; +static const char *ptr ="where do I go?" ; +static const char trailing[] ="hidden NULLs\0\0\0" ; +static const char leading[] ="\0\0hidden text" ; + +if (strlen (normal) !=11 ) +return 0x100001 ; +if (strlen (ptr++) !=14 ) +return 0x100002 ; +if (strlen (ptr++) !=13 ) +return 0x100003 ; +if (strlen (trailing) !=12 ) +return 0x100004 ; +if (strlen (leading) !=0 ) +return 0x100005 ; +if (strlen_ce !=27 ) +return 0x100006 ; + +return 0 ; +} + static __exit void string_selftest_remove(void) { }@@ -212,6 +244,11 @@ static __init int string_selftest_init(void)if (subtest) goto fail; + test =5 ; + subtest =strlen_selftest (); +if (subtest) + goto fail; +pr_info ("String selftests succeeded\n" );return 0 ; fail:Signature · 1 line
2.30.2
Signature · 1 line
Kees Cook
Quoted 33 lines · Kees Cook
Quoted 32 lines · Guenter Roeck
Quoted 18 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.Cc Geert Uytterhoeven <[email protected]>Cc Peter Rosin <[email protected]>Signed-off-by Kees Cook <[email protected]>---
I'll be taking this as part of my ClangFORTIFY_SOURCEseries.--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)lib/test_string.c +0
-0@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ;lib/test_string.c static __init int strnchr_selftest(void) · line 189
Quoted 10 lines
lib/test_string.c +8
-0} +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); +This results in:
Quoted 8 lines
Quoted 5 lines
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");for several of my tests. I don't think you can mandate that a compiler implements this.
Which tests?
Some examples:
Build reference: next-20220202
Compiler version: m68k-linux-gcc (GCC) 11.2.0
Building m68k:defconfig ... failed
--------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const int strlen_ce = strlen("tada, a constant expression");
Building mips:malta_defconfig:nocd:smp:net,e1000:initrd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
static const int strlen_ce = strlen("tada, a constant expression");
Building i386:q35:Broadwell:defconfig:smp:ata:net,rtl8139:hd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const int strlen_ce = strlen("tada, a constant expression");
i386 and is defconfig + CONFIG_STRING_SELFTEST=y; mips is malta_defconfig + CONFIG_STRING_SELFTEST=y. All use gcc 11.2.
There may be more, but there are so many failures in -next right now that I may be missing some.
Quoted 10 lines
This property of
strlen()is already required by our builds (this is how I tripped over it). For example:
drivers/firmware/xilinx/zynqmp-debug.c:#define
PM_API(id){id, #id, strlen(#id)}static struct pm_api_info pm_api_list[] = { PM_API(PM_GET_API_VERSION), PM_API(PM_QUERY_DATA), };
I do not think that it is a C standard that strlen() on a constant string must be compile-time evaluated and result in a constant.
Anyway, key difference, I think, is the presence of an architecture-specific version of strlen(), or the maybe non-presence of __HAVE_ARCH_STRLEN, or the definition of strlen() in include/linux/fortify-string.h.
Guenter
Hi Günter,
Quoted 77 lines · Guenter Roeck
Quoted 33 lines · Kees Cook
Quoted 32 lines · Guenter Roeck
Quoted 18 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.Cc Geert Uytterhoeven <[email protected]>Cc Peter Rosin <[email protected]>Signed-off-by Kees Cook <[email protected]>---
I'll be taking this as part of my ClangFORTIFY_SOURCEseries.--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)lib/test_string.c +0
-0@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ;lib/test_string.c static __init int strnchr_selftest(void) · line 189
Quoted 10 lines
lib/test_string.c +8
-0} +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); +This results in:
Quoted 8 lines
Quoted 5 lines
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");for several of my tests. I don't think you can mandate that a compiler implements this.
Which tests?
Some examples:
Build reference: next-20220202
Compiler version: m68k-linux-gcc (GCC) 11.2.0Building m68k:defconfig ... failed
--------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");Building mips:malta_defconfig:nocd:smp:net,e1000:initrd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
static const intstrlen_ce=strlen("tada, a constant expression");Building i386:q35:Broadwell:defconfig:smp:ata:net,rtl8139:hd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");i386 and is defconfig +
CONFIG_STRING_SELFTEST=y; mips ismalta_defconfig+CONFIG_STRING_SELFTEST=y. All use gcc 11.2.There may be more, but there are so many failures in -next right now that I may be missing some.
Quoted 5 lines
This property of
strlen()is already required by our builds (this is how I tripped over it). For example:
drivers/firmware/xilinx/zynqmp-debug.c:
Quoted 8 lines
Quoted 5 lines
#define
PM_API(id){id, #id, strlen(#id)}static struct pm_api_info pm_api_list[] = { PM_API(PM_GET_API_VERSION), PM_API(PM_QUERY_DATA), };I do not think that it is a C standard that
strlen()on a constant string must be compile-time evaluated and result in a constant.
Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g. strncat() -> strlen() + store, strncmp() -> strcmp()), which breaks linking if the latter is only provided inline.
Quoted 3 lines
Anyway, key difference, I think, is the presence of an architecture-specific version of
strlen(), or the maybe non-presence of__HAVE_ARCH_STRLEN, or the definition ofstrlen()ininclude/linux/fortify-string.h.
It works after dropping -ffreestanding.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 95 lines · Geert Uytterhoeven
Hi Günter,
Quoted 77 lines · Guenter Roeck
Quoted 33 lines · Kees Cook
Quoted 32 lines · Guenter Roeck
Quoted 18 lines · Kees Cook
Add a simple test for
strlen()functionality, including using it as a constant expression.Cc Geert Uytterhoeven <[email protected]>Cc Peter Rosin <[email protected]>Signed-off-by Kees Cook <[email protected]>---
I'll be taking this as part of my ClangFORTIFY_SOURCEseries.--- lib/test_string.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)lib/test_string.c +0
-0@@ -179,6 +179,38 @@ static __init int strnchr_selftest(void)return 0 ;lib/test_string.c static __init int strnchr_selftest(void) · line 189
Quoted 10 lines
lib/test_string.c +8
-0} +/* +* Unlike many other string functions, strlen() can be used in +* static initializers when string lengths are known at compile +* time. (i.e. Under these conditions, strlen() is a constant +* expression.) Make sure it can be used this way. +*/ +static const int strlen_ce =strlen ("tada, a constant expression" ); +This results in:
Quoted 8 lines
Quoted 5 lines
lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");for several of my tests. I don't think you can mandate that a compiler implements this.
Which tests?
Some examples:
Build reference: next-20220202
Compiler version: m68k-linux-gcc (GCC) 11.2.0Building m68k:defconfig ... failed
--------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");Building mips:malta_defconfig:nocd:smp:net,e1000:initrd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
static const intstrlen_ce=strlen("tada, a constant expression");Building i386:q35:Broadwell:defconfig:smp:ata:net,rtl8139:hd ... failed
------------
Error log:lib/test_string.c:188:30: error: initializer element is not constant
188 | static const intstrlen_ce=strlen("tada, a constant expression");i386 and is defconfig +
CONFIG_STRING_SELFTEST=y; mips ismalta_defconfig+CONFIG_STRING_SELFTEST=y. All use gcc 11.2.There may be more, but there are so many failures in -next right now that I may be missing some.
Quoted 5 lines
This property of
strlen()is already required by our builds (this is how I tripped over it). For example:
drivers/firmware/xilinx/zynqmp-debug.c:Quoted 8 lines
Quoted 5 lines
#define
PM_API(id){id, #id, strlen(#id)}static struct pm_api_info pm_api_list[] = { PM_API(PM_GET_API_VERSION), PM_API(PM_QUERY_DATA), };I do not think that it is a C standard that
strlen()on a constant string must be compile-time evaluated and result in a constant.Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g.
strncat()->strlen()+ store,strncmp()->strcmp()), which breaks linking if the latter is only provided inline.
Quoted 5 lines
Quoted 3 lines
Anyway, key difference, I think, is the presence of an architecture-specific version of
strlen(), or the maybe non-presence of__HAVE_ARCH_STRLEN, or the definition ofstrlen()ininclude/linux/fortify-string.h.It works after dropping -ffreestanding.
Ah-ha, thanks for the clue here. I'll see if there is some way to detect this (or, I guess, drop the patch). I don't like that this requirement depends on the architecture, so I'd rather it behave the same everywhere. Hmm
Signature · 1 line
Kees Cook
Quoted 5 lines · Geert Uytterhoeven
Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g.
strncat()->strlen()+ store,strncmp()->strcmp()), which breaks linking if the latter is only provided inline.
Hah, for i386:
arch/x86/Makefile
# temporary until string.h is fixed KBUILD_CFLAGS += -ffreestanding
This "temporary" is from 2006. ;)
6edfba1b33c7 ("[PATCH] x86_64: Don't define string functions to builtin")
Removing that appears to solve it, and appears to build correctly. I'll continue testing.
Quoted 1 line
It works after dropping -ffreestanding.
I wonder if the other architectures were just copying x86?
Signature · 1 line
Kees Cook
Hi Kees,
Quoted 7 lines · Kees Cook
Quoted 5 lines · Geert Uytterhoeven
Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g.
strncat()->strlen()+ store,strncmp()->strcmp()), which breaks linking if the latter is only provided inline.
Quoted 7 lines
Hah, for i386:
arch/x86/Makefile# temporary until string.h is fixed KBUILD_CFLAGS += -ffreestandingThis "temporary" is from 2006. ;)
And before that, we had it in the main Makefile.
Quoted 8 lines
6edfba1b33c7("[PATCH]x86_64: Don't define string functions to builtin")Removing that appears to solve it, and appears to build correctly. I'll continue testing.
Quoted 1 line
It works after dropping -ffreestanding.
I wonder if the other architectures were just copying x86?
At least on m68k it was added because gcc added new optimizations that broke if an architecture provides some functions as inline. As the kernel is not supposed to be linked with the standard C library, -ffreestanding should be correct, isn't it?
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 18 lines · Geert Uytterhoeven
Hi Kees,
Quoted 14 lines · Kees Cook
Quoted 5 lines · Geert Uytterhoeven
Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g.
strncat()->strlen()+ store,strncmp()->strcmp()), which breaks linking if the latter is only provided inline.Hah, for i386:
arch/x86/Makefile# temporary until string.h is fixed KBUILD_CFLAGS += -ffreestandingThis "temporary" is from 2006. ;)\
IIRC I sent a patch removing that. Yeah, Kees even signed off on it.
https://lore.kernel.org/lkml/[email protected]/#t
I still think that's the right way to go, perhaps worth a resend to rekick discussions.
Quoted 8 lines
And before that, we had it in the main
Makefile.Quoted 5 lines
6edfba1b33c7("[PATCH]x86_64: Don't define string functions to builtin")Removing that appears to solve it, and appears to build correctly. I'll continue testing.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
It works after dropping -ffreestanding.
I wonder if the other architectures were just copying x86?
At least on m68k it was added because gcc added new optimizations that broke if an architecture provides some functions as inline. As the kernel is not supposed to be linked with the standard C library, -ffreestanding should be correct, isn't it?
The kernel does not link against a libc; but it does provide many
symbols that libc would provide, with the same or similar enough
semantics that I would strongly recommend we _don't_ use
-ffreestanding in order to get such libcall optimizations (there are a
lot; see https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
for some examples) and simply use -fno-builtin-* when necessary, or
fix the kernel implementations individually.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 18 lines · Nick Desaulniers
Quoted 17 lines · Geert Uytterhoeven
Hi Kees,
Quoted 13 lines · Kees Cook
Quoted 5 lines · Geert Uytterhoeven
Not if -ffreestanding, which is what several architectures are using nowadays, to a.o. prevent gcc from replacing calls to stdlib functions to other stdlib functions (e.g.
strncat()->strlen()+ store,strncmp()->strcmp()), which breaks linking if the latter is only provided inline.Hah, for i386:
arch/x86/Makefile# temporary until string.h is fixed KBUILD_CFLAGS += -ffreestanding
Quoted 6 lines
Quoted 1 line
Quoted 1 line
This "temporary" is from 2006. ;)\
IIRC I sent a patch removing that. Yeah, Kees even signed off on it.
https://lore.kernel.org/lkml/[email protected]/#t
I still think that's the right way to go, perhaps worth a resend to rekick discussions.
Hah. Yay. I'll go pick this into the memcpy topic branch. Building x86_64 and ia32 differently makes no sense (and this solves the head-scratching compile-time test failures I was seeing on ia32 too).
Quoted 17 lines
Quoted 8 lines
And before that, we had it in the main
Makefile.Quoted 5 lines
6edfba1b33c7("[PATCH]x86_64: Don't define string functions to builtin")Removing that appears to solve it, and appears to build correctly. I'll continue testing.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
It works after dropping -ffreestanding.
I wonder if the other architectures were just copying x86?
At least on m68k it was added because gcc added new optimizations that broke if an architecture provides some functions as inline. As the kernel is not supposed to be linked with the standard C library, -ffreestanding should be correct, isn't it?
Quoted 7 lines
The kernel does not link against a libc; but it does provide many
symbols that libc would provide, with the same or similar enough
semantics that I would strongly recommend we _don't_ use
-ffreestanding in order to get such libcall optimizations (there are a
lot; see https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
for some examples) and simply use -fno-builtin-* when necessary, or
fix the kernel implementations individually.
Right, so, I think for x86 it's straight forward. For the other architectures it may need more careful checking, so I'm just going to drop the new test from the memcpy topic branch, and maybe start a "-ffreestanding removal" topic branch so there isn't a cross-dependency here.
Signature · 1 line
Kees Cook
Quoted 7 lines · Nick Desaulniers
The kernel does not link against a libc; but it does provide many
symbols that libc would provide, with the same or similar enough
semantics that I would strongly recommend we _don't_ use
-ffreestanding in order to get such libcall optimizations (there are a
lot; see https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
for some examples) and simply use -fno-builtin-* when necessary, or
fix the kernel implementations individually.
Right, we really don't want -ffreestanding. Rather, we want to not link against libgcc. This is mostly true already, though some of the smaller architectures still do:
$ git grep print-libgcc arch/arc/Makefile:LIBGCC = $(shell $(CC) $(cflags-y) --print-libgcc-file-name) arch/csky/Makefile: $(shell $(CC) $(KBUILD_CFLAGS) $(KCFLAGS) -print-libgcc-file-name) arch/h8300/boot/compressed/Makefile:LIBGCC := $(shell $(CROSS-COMPILE)$(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name 2>/dev/null) arch/nios2/Makefile:LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) $(KCFLAGS) -print-libgcc-file-name) arch/openrisc/Makefile:LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name) arch/parisc/Makefile:LIBGCC := $(shell $(CC) -print-libgcc-file-name) arch/xtensa/Makefile:LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name) arch/xtensa/boot/boot-redboot/Makefile:LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name)
Signature · 1 line
Kees Cook