lib/string.c: implement stpcpy
Subsystems (5)
Nick Desaulniers
reviewerPATCH 1/1 lib/string.c: implement stpcpy
LLVM implemented a recent "libcall optimization" that lowers calls to sprintf(dest, "%s", str) where the return value is used to stpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings.
stpcpy is just like strcpy except:
1. it returns the pointer to the new tail of dest. This allows you to chain multiple calls to stpcpy in one statement.
2. it requires the parameters not to overlap. Calling sprintf with overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpy was first standardized in POSIX.1-2008.
Implement this so that we don't observe linkage failures due to missing symbol definitions for stpcpy.
Similar to last year's fire drill with:
commit 5f074f3e192f ("lib/string.c: implement a basic bcmp")
This optimization was introduced into clang-12.
--- include/linux/string.h | 3 +++ lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)
include/linux/string.h +3 -0
@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict,const char *__restrict__); +#endif /* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count);
lib/string.c +23 -0
@@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) }EXPORT_SYMBOL (strscpy_pad); +#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is the new NULL terminated character. (for strcpy, the +* return value is a pointer to src. +*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return dest; +} +#endif +#ifndef __HAVE_ARCH_STRCAT/** * strcat - Append one %NUL-terminated string to another
Signature · 1 line
2.28.0.220.ged08abb693-goog
Quoted 13 lines · Nick Desaulniers
lib/string.c +13
-0+#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is the new NULL terminated character. (for strcpy, the +* return value is a pointer to src.
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 297
Quoted 10 lines
lib/string.c +10
-0+*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return dest; +} +#endif +
Won't this return a pointer that's one _past_ the terminating NUL? I think you need the increments to be inside the loop body, rather than as part of the condition.
Nit: NUL is more correct than NULL to refer to the string terminator.
Quoted 29 lines · Arvind Sankar
Quoted 13 lines · Nick Desaulniers
lib/string.c +13
-0+#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is the new NULL terminated character. (for strcpy, the +* return value is a pointer to src. lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 297
Quoted 10 lines
lib/string.c +10
-0+*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return dest; +} +#endif +Won't this return a pointer that's one _past_ the terminating NUL? I think you need the increments to be inside the loop body, rather than as part of the condition.
Quoted 1 line
Nit: NUL is more correct than NULL to refer to the string terminator.
Also, strcpy (at least the one in the C standard) is undefined if the strings overlap, so that's not really a difference.
Nick Desaulniers
reviewerPATCH v2 1/1 lib/string.c: implement stpcpy
LLVM implemented a recent "libcall optimization" that lowers calls to sprintf(dest, "%s", str) where the return value is used to stpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Calling sprintf with overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpy is just like strcpy except it returns the pointer to the new tail of dest. This allows you to chain multiple calls to stpcpy in one statement.
stpcpy was first standardized in POSIX.1-2008.
Implement this so that we don't observe linkage failures due to missing symbol definitions for stpcpy.
Similar to last year's fire drill with:
commit 5f074f3e192f ("lib/string.c: implement a basic bcmp")
This optimization was introduced into clang-12.
---
Changes V2:
* Added Sami's Tested by; though the patch changed implementation, the missing symbol at link time was the problem Sami was observing.
* Fix __restrict -> __restrict__ typo as per Joe.
* Drop note about restrict from commit message as per Arvind.
* Fix NULL -> NUL as per Arvind; NUL is ASCII '\0'. TIL
* Fix off by one error as per Arvind; I had another off by one error in my test program that was masking this.
include/linux/string.h | 3 +++ lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)
include/linux/string.h +3 -0
@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict__,const char *__restrict__); +#endif /* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count);
lib/string.c +23 -0
@@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) }EXPORT_SYMBOL (strscpy_pad); +#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NUL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is the new NULL terminated character. (for strcpy, the +* return value is a pointer to src. +*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return --dest; +} +#endif +#ifndef __HAVE_ARCH_STRCAT/** * strcat - Append one %NUL-terminated string to another
Signature · 1 line
2.28.0.220.ged08abb693-goog
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_O What?
No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example sprintf() pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more than PAGE_SIZE bytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have a PAGE_SIZE buffer).
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded sprintf() uses. (And to do so without introducing bugs related to using snprintf() when scnprintf() is expected[4].)
-Kees
[1] https://github.com/KSPP/linux/issues/88
[2] https://github.com/KSPP/linux/issues/89
[3] https://github.com/KSPP/linux/issues/90
[4] https://lore.kernel.org/lkml/20200810092100.GA42813@2f5448a72a42/
Signature · 1 line
Kees Cook
Nick Desaulniers
reviewerQuoted 23 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).
Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Thoughts on not exposing the declaration in the header, and changing the comment to be to the effect of:
"Exists only for optimizing compilers to replace calls to sprintf with; generally unsafe as bounds checks aren't performed, do not use." It's still a worthwhile optimization to have, even if the use that generated it didn't do any bounds checking.
Quoted 10 lines
-Kees
[1] https://github.com/KSPP/linux/issues/88
[2] https://github.com/KSPP/linux/issues/89
[3] https://github.com/KSPP/linux/issues/90
[4] https://lore.kernel.org/lkml/20200810092100.GA42813@2f5448a72a42/--
Kees Cook
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 29 lines · Nick Desaulniers
Quoted 22 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)
Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in compiler-clang.h so it isn't generic and public.
Something like:
--- include/linux/compiler-clang.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
include/linux/compiler-clang.h +27 -0
@@ -61,3 +61,30 @@#if __has_feature (shadow_call_stack)# define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))#endif + +#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to buffer being copied into. +* Must be large enough to receive copy. +* @src: pointer to the beginning of string being copied from. +* Must not overlap dest. +* +* This function exists _only_ to support clang's possible conversion of +* sprintf calls to stpcpy. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is dest's NUL termination character after copy. +* (for strcpy, the return value is a pointer to src) +*/ + +static inline char *stpcpy(char __restrict *dest, const char __restrict *src) +{ +while ((*dest++ = *src++) !='\0' ) { + ;/* nothing */ + } +return --dest; +} +#endif
Nick Desaulniers
reviewerQuoted 31 lines · Joe Perches
Quoted 29 lines · Nick Desaulniers
Quoted 27 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)
Quoted 6 lines
Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.
https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and
https://bugs.llvm.org/show_bug.cgi?id=47144
Seem to imply that Clang is not the only compiler that can lower a sequence of libcalls to stpcpy. Do we want to wait until we have a fire drill w/ GCC to move such an implementation from include/linux/compiler-clang.h back in to lib/string.c?
Quoted 44 lines
Something like:
--- include/linux/compiler-clang.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)include/linux/compiler-clang.h +27
-0@@ -61,3 +61,30 @@#if __has_feature (shadow_call_stack)# define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))#endif + +#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to buffer being copied into. +* Must be large enough to receive copy. +* @src: pointer to the beginning of string being copied from. +* Must not overlap dest. +* +* This function exists _only_ to support clang's possible conversion of +* sprintf calls to stpcpy. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is dest's NUL termination character after copy. +* (for strcpy, the return value is a pointer to src) +*/ + +static inline char *stpcpy(char __restrict *dest, const char __restrict *src) +{ +while ((*dest++ = *src++) !='\0' ) { + ;/* nothing */ + } +return --dest; +} +#endif
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 38 lines · Nick Desaulniers
Quoted 30 lines · Joe Perches
Quoted 29 lines · Nick Desaulniers
Quoted 27 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)Quoted 6 lines
Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.
Quoted 6 lines
https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and
https://bugs.llvm.org/show_bug.cgi?id=47144
Seem to imply that Clang is not the only compiler that can lower a sequence of libcalls to stpcpy. Do we want to wait until we have a fire drill w/ GCC to move such an implementation frominclude/linux/compiler-clang.hback in tolib/string.c?
My guess is yes, wait until gcc, if ever, needs it.
Nick Desaulniers
reviewerQuoted 40 lines · Joe Perches
Quoted 38 lines · Nick Desaulniers
Quoted 36 lines · Joe Perches
Quoted 29 lines · Nick Desaulniers
Quoted 22 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.
Quoted 8 lines
Quoted 6 lines
https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and
https://bugs.llvm.org/show_bug.cgi?id=47144
Seem to imply that Clang is not the only compiler that can lower a sequence of libcalls to stpcpy. Do we want to wait until we have a fire drill w/ GCC to move such an implementation frominclude/linux/compiler-clang.hback in tolib/string.c?My guess is yes, wait until gcc, if ever, needs it.
The suggestion to use static inline doesn't even make sense. The compiler is lowering calls to other library routines; stpcpy isn't being explicitly called. Even if it was, not sure we want it being inlined. No symbol definition will be emitted; problem not solved. And I refuse to add any more code using extern inline. Putting the definition in lib/string.c is the most straightforward and avoids revisiting this issue in the future for other toolchains. I'll limit access by removing the declaration, and adding a comment to avoid its use. But if you're going to use a gnu target triple without using -ffreestanding because you *want* libcall optimizations, then you have to provide symbols for all possible library routines!
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 50 lines · Nick Desaulniers' via Clang Built Linux
Quoted 40 lines · Joe Perches
Quoted 38 lines · Nick Desaulniers
Quoted 36 lines · Joe Perches
Quoted 29 lines · Nick Desaulniers
Quoted 22 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.Quoted 8 lines
Quoted 6 lines
https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and
https://bugs.llvm.org/show_bug.cgi?id=47144
Seem to imply that Clang is not the only compiler that can lower a sequence of libcalls to stpcpy. Do we want to wait until we have a fire drill w/ GCC to move such an implementation frominclude/linux/compiler-clang.hback in tolib/string.c?My guess is yes, wait until gcc, if ever, needs it.
Quoted 11 lines
The suggestion to use static inline doesn't even make sense. The compiler is lowering calls to other library routines;
stpcpyisn't being explicitly called. Even if it was, not sure we want it being inlined. No symbol definition will be emitted; problem not solved. And I refuse to add any more code usingextern inline. Putting the definition inlib/string.cis the most straightforward and avoids revisiting this issue in the future for other toolchains. I'll limit access by removing the declaration, and adding a comment to avoid its use. But if you're going to use a gnu target triple without using -ffreestanding because you *want* libcall optimizations, then you have to provide symbols for all possible library routines!
Adding a definition without a declaration for stpcpy looks good. Clang LTO will work.
(If the kernel does not want to provide these routines,
is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912
probably wrong? (why remove -ffreestanding from the main Makefile) )
Quoted 64 lines · Fangrui Song' via Clang Built Linux
Quoted 50 lines · Nick Desaulniers' via Clang Built Linux
Quoted 40 lines · Joe Perches
Quoted 38 lines · Nick Desaulniers
Quoted 36 lines · Joe Perches
Quoted 29 lines · Nick Desaulniers
Quoted 22 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.Quoted 8 lines
Quoted 6 lines
https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and
https://bugs.llvm.org/show_bug.cgi?id=47144
Seem to imply that Clang is not the only compiler that can lower a sequence of libcalls to stpcpy. Do we want to wait until we have a fire drill w/ GCC to move such an implementation frominclude/linux/compiler-clang.hback in tolib/string.c?My guess is yes, wait until gcc, if ever, needs it.
Quoted 11 lines
The suggestion to use static inline doesn't even make sense. The compiler is lowering calls to other library routines;
stpcpyisn't being explicitly called. Even if it was, not sure we want it being inlined. No symbol definition will be emitted; problem not solved. And I refuse to add any more code usingextern inline. Putting the definition inlib/string.cis the most straightforward and avoids revisiting this issue in the future for other toolchains. I'll limit access by removing the declaration, and adding a comment to avoid its use. But if you're going to use a gnu target triple without using -ffreestanding because you *want* libcall optimizations, then you have to provide symbols for all possible library routines!
Quoted 7 lines
Adding a definition without a declaration for stpcpy looks good. Clang LTO will work.
(If the kernel does not want to provide these routines,
is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912
probably wrong? (why remove -ffreestanding from the mainMakefile) )
We had some many issues in arch/x86 where *FLAGS were removed or used differently and had to re-add them :-(.
So if -ffreestanding is used in arch/x86 and was! used in top-level Makefile - it makes sense to re-add it back?
( I cannot speak for archs other than x86. )
- Sedat -
Quoted 11 lines · Sedat Dilek
Quoted 8 lines · Fangrui Song' via Clang Built Linux
Adding a definition without a declaration for stpcpy looks good. Clang LTO will work.
(If the kernel does not want to provide these routines,
is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912
probably wrong? (why remove -ffreestanding from the mainMakefile) )
Quoted 8 lines
We had some many issues in
arch/x86where *FLAGS were removed or used differently and had to re-add them :-(.So if -ffreestanding is used in
arch/x86and was! used in top-levelMakefile- it makes sense to re-add it back?
( I cannot speak for archs other than x86. )- Sedat -
-ffreestanding disables _all_ builtins and libcall optimizations, which is probably not desirable. If we added it back, we'd need to also go back to #define various string functions to the __builtin versions.
Though I don't understand the original issue, with -ffreestanding, sprintf shouldn't have been turned into strcpy in the first place.
32-bit still has -ffreestanding -- I wonder if that's actually necessary any more?
Why does -fno-builtin-stpcpy not work with clang LTO? Isn't that a compiler bug?
Thanks.
Quoted 26 lines · Arvind Sankar
Quoted 11 lines · Sedat Dilek
Quoted 8 lines · Fangrui Song' via Clang Built Linux
Adding a definition without a declaration for stpcpy looks good. Clang LTO will work.
(If the kernel does not want to provide these routines,
is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912
probably wrong? (why remove -ffreestanding from the mainMakefile) )Quoted 8 lines
We had some many issues in
arch/x86where *FLAGS were removed or used differently and had to re-add them :-(.So if -ffreestanding is used in
arch/x86and was! used in top-levelMakefile- it makes sense to re-add it back?
( I cannot speak for archs other than x86. )- Sedat -
-ffreestanding disables _all_ builtins and libcall optimizations, which is probably not desirable. If we added it back, we'd need to also go back to #define various string functions to the
__builtinversions.
Quoted 8 lines
Though I don't understand the original issue, with -ffreestanding, sprintf shouldn't have been turned into strcpy in the first place.
32-bit still has -ffreestanding -- I wonder if that's actually necessary any more?
Why does -fno-builtin-stpcpy not work with clang LTO? Isn't that a compiler bug?
I just confirmed that adding -fno-builtin-stpcpy to KBUILD_CFLAGS does work with LTO as well.
Sami
Nick Desaulniers
reviewerQuoted 18 lines · Sami Tolvanen
Quoted 16 lines · Arvind Sankar
Quoted 14 lines · Sedat Dilek
Quoted 8 lines · Fangrui Song' via Clang Built Linux
Adding a definition without a declaration for stpcpy looks good. Clang LTO will work.
(If the kernel does not want to provide these routines,
is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912
probably wrong? (why remove -ffreestanding from the mainMakefile) )We had some many issues in
arch/x86where *FLAGS were removed or used differently and had to re-add them :-(.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
So if -ffreestanding is used in
arch/x86and was! used in top-levelMakefile- it makes sense to re-add it back?
( I cannot speak for archs other than x86. )- Sedat -
-ffreestanding disables _all_ builtins and libcall optimizations, which is probably not desirable. If we added it back, we'd need to also go
I agree.
Quoted 4 lines
Quoted 4 lines
back to #define various string functions to the
__builtinversions.Though I don't understand the original issue, with -ffreestanding, sprintf shouldn't have been turned into strcpy in the first place.
Huh? The original issue for this thread is because -ffreestanding *isn't* being used for most targets (oh boy, actually mixed usage by ARCH. Looks like MIPS, m68k, superH, xtensa, and 32b x86 use it?); and I'm not suggesting it be used.
Quoted 2 lines
Quoted 2 lines
32-bit still has -ffreestanding -- I wonder if that's actually necessary any more?
Fair question. Someone will have to go chase git history, since 0a6ef376d4ba covers it up. If anyone has any tricks to do so quickly; I'd love to know. I generally checkout the commit prior, then use vim fugitive to get git blame.
Quoted 2 lines
Quoted 2 lines
Why does -fno-builtin-stpcpy not work with clang LTO? Isn't that a compiler bug?
Yes; Sami found a recent patch that looks to me like it may have recently solved that bug.
https://reviews.llvm.org/D71193 which landed Dec 12 2019. The bug report was based on
https://github.com/ClangBuiltLinux/linux/issues/416#issuecomment-472231304
(Issue reported March 8 2019). And I do recall being able to reproduce the bug when I sent
commit 5f074f3e192f ("lib/string.c: implement a basic bcmp")
Now that that is fixed as reported by Sami below, I don't mind sending a revert for 5f074f3e192f that adds -fno-builtin-bcmp, because the current implementation of bcmp isn't useful.
That said, this libcall optimization/transformation (sprintf->stpcpy) does look useful to me. Kees, do you have thoughts on me providing the implementation without exposing it in a header vs using -fno-builtin-stpcpy? (I would need to add the missing EXPORT_SYMBOL, as pointed out by 0day bot and on the github thread). I don't care either way; I'd just like your input before sending a V+1. Maybe better to just not implement it and never implement it?
Quoted 5 lines
I just confirmed that adding -fno-builtin-stpcpy to
KBUILD_CFLAGSdoes work with LTO as well.Sami
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 7 lines · Nick Desaulniers
That said, this libcall optimization/transformation (
sprintf->stpcpy) does look useful to me. Kees, do you have thoughts on me providing the implementation without exposing it in a header vs using -fno-builtin-stpcpy? (I would need to add the missingEXPORT_SYMBOL, as pointed out by 0day bot and on the github thread). I don't care either way; I'd just like your input before sending a V+1. Maybe better to just not implement it and never implement it?
I think I would ultimately prefer -fno-builtin-stpcpy, but for now, sure, an implementation without a header (and a biiig comment above it detailing why and a reference to the bug) would be fine by me.
Signature · 1 line
Kees Cook
Quoted 8 lines · Nick Desaulniers
Quoted 2 lines
Quoted 2 lines
Though I don't understand the original issue, with -ffreestanding, sprintf shouldn't have been turned into strcpy in the first place.
Huh? The original issue for this thread is because
-ffreestanding*isn't* being used for most targets (oh boy, actually mixed usage by ARCH. Looks like MIPS, m68k, superH, xtensa, and 32b x86 use it?); and I'm not suggesting it be used.
Sorry, I meant the issue mentioned in the commit that removed -ffreestanding, not the stpcpy one you're solving now. It says that sprintf got converted into strcpy, which caused failures because back then, strcpy was #define'd to __builtin_strcpy, and the default implementation was actually of a function called __builtin_strcpy o_O, not strcpy.
Anyway, that's water under the bridge now.
6edfba1b33c7 ("x86_64: Don't define string functions to builtin")
gcc should handle this anyways, and it causes problems when sprintf is turned into strcpy by gcc behind our backs and the C fallback version of strcpy is actually defining __builtin_strcpy
Nick Desaulniers
reviewerQuoted 18 lines · Arvind Sankar
Quoted 8 lines · Nick Desaulniers
Quoted 2 lines
Quoted 2 lines
Though I don't understand the original issue, with -ffreestanding, sprintf shouldn't have been turned into strcpy in the first place.
Huh? The original issue for this thread is because
-ffreestanding*isn't* being used for most targets (oh boy, actually mixed usage by ARCH. Looks like MIPS, m68k, superH, xtensa, and 32b x86 use it?); and I'm not suggesting it be used.Sorry, I meant the issue mentioned in the commit that removed -ffreestanding, not the stpcpy one you're solving now. It says that sprintf got converted into strcpy, which caused failures because back then, strcpy was #define'd to
__builtin_strcpy, and the default implementation was actually of a function called__builtin_strcpyo_O, not strcpy.
Quoted 6 lines
Anyway, that's water under the bridge now.
6edfba1b33c7("x86_64: Don't define string functions to builtin")
gcc should handle this anyways, and it causes problems when sprintf is turned into strcpy by gcc behind our backs and the C fallback version of strcpy is actually defining__builtin_strcpy
For fun, I tried removing -ffreestanding from arch/x86/Makefile; both gcc and clang can compile+boot the i386 defconfig just fine. Why don't I send a patch removing it with your suggested by in a series of fixes for stpcpy and bcmp?
Signature · 2 lines
Thanks, ~Nick Desaulniers
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 10 lines · David Laight
From: Nick Desaulniers
Quoted 1 line
Sent: 17 August 2020 19:37
..
Quoted 2 lines
That said, this libcall optimization/transformation (
sprintf->stpcpy) does look useful to me.I'd rather get a cow if I ask for a cow...
Maybe checkpatch (etc) could report places where
snprintf()could be replaced by a simpler function.
You mean sprintf no?
Reminds me of seq_printf vs seq_puts...
Quoted 2 lines · Sami Tolvanen
I just confirmed that adding -fno-builtin-stpcpy to
KBUILD_CFLAGSdoes work with LTO as well.
Oh, I read this out of order; sorry! Yes, if -fno-builtin-stpcpy works, let's use that instead. Doesn't that solve it too?
Signature · 1 line
Kees Cook
-fno-builtin-stpcpy can be used to disable stpcpy but Nick at llvm bugzilla wrote that these flags are broken with LTO.
Quoted 82 lines
Dňa 15. 8. 2020 o 23:24 užívateľ Joe Perches <[email protected]> napísal:
Quoted 29 lines · Nick Desaulniers
Quoted 23 lines · Kees Cook
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).Quoted 5 lines
But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)Quoted 3 lines
Well, everything (-next, mainline, stable) is broken right now (with ToT Clang) without providing this symbol. I'm not going to go clean the entire kernel's use of sprintf to get our CI back to being green.
Maybe this should get place in
compiler-clang.hso it isn't generic and public.Something like:
---
include/linux/compiler-clang.h| 27 +++++++++++++++++++++++++++1 file changed, 27 insertions(+)include/linux/compiler-clang.h +27
-0@@ -61,3 +61,30 @@#if __has_feature (shadow_call_stack)# define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))#endif + +#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to buffer being copied into. +* Must be large enough to receive copy. +* @src: pointer to the beginning of string being copied from. +* Must not overlap dest. +* +* This function exists _only_ to support clang's possible conversion of +* sprintf calls to stpcpy. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is dest's NUL termination character after copy. +* (for strcpy, the return value is a pointer to src) +*/ + +static inline char *stpcpy(char __restrict *dest, const char __restrict *src) +{ +while ((*dest++ = *src++) !='\0' ) { + ;/* nothing */ + } +return --dest; +} +#endif
Yeah, sprintf calls should be replaced with something safer.
Quoted 39 lines
Dňa 15. 8. 2020 o 18:34 užívateľ Kees Cook <[email protected]> napísal:
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
O_OWhat?No; this is a _terrible_ API: there is no bounds checking, there are no buffer sizes. Anything using the example
sprintf()pattern is _already_ wrong and must be removed from the kernel. (Yes, I realize that the kernel is *filled* with this bad assumption that "I'll never write more thanPAGE_SIZEbytes to this buffer", but that's both theoretically wrong ("640k is enough for anybody") and has been known to be wrong in practice too (e.g. when suddenly your writing routine is reachable by splice(2) and you may not have aPAGE_SIZEbuffer).But we cannot _add_ another dangerous string API. We're already in a terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This needs to be addressed up by removing the unbounded
sprintf()uses. (And to do so without introducing bugs related to usingsnprintf()whenscnprintf()is expected[4].)-Kees
[1] https://github.com/KSPP/linux/issues/88
[2] https://github.com/KSPP/linux/issues/89
[3] https://github.com/KSPP/linux/issues/90
[4] https://lore.kernel.org/lkml/20200810092100.GA42813@2f5448a72a42/Signature · 1 line
Kees Cook
Quoted 68 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
stpcpywas first standardized in POSIX.1-2008.Implement this so that we don't observe linkage failures due to missing symbol definitions for
stpcpy.Similar to last year's fire drill with:
commit5f074f3e192f("lib/string.c: implement a basic bcmp")This optimization was introduced into clang-12.
Suggested-by Arvind Sankar <[email protected]>Suggested-by Joe Perches <[email protected]>Reported-by Sami Tolvanen <[email protected]>Tested-by Sami Tolvanen <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>---
Changes V2:
* Added Sami's Tested by; though the patch changed implementation, the missing symbol at link time was the problem Sami was observing.
* Fix__restrict->__restrict__typo as per Joe.
* Drop note about restrict from commit message as per Arvind.
* Fix NULL -> NUL as per Arvind; NUL is ASCII '\0'. TIL
* Fix off by one error as per Arvind; I had another off by one error in my test program that was masking this.include/linux/string.h | 3 +++ lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)include/linux/string.h +3
-0@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict__,const char *__restrict__); +#endif /* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count);lib/string.c +1
-0@@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) }EXPORT_SYMBOL (strscpy_pad); +#ifndef __HAVE_ARCH_STPCPY
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 285
Quoted 10 lines
lib/string.c +10
-0+/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NUL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap.
Looks like you missed my second email: strcpy also does not allow inputs to overlap. Couple typos below.
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 286
Quoted 1 line
lib/string.c +1
-0+* 2. return value is the new NULL terminated character. (for strcpy, the
^^ NUL terminator.
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 287
Quoted 1 line
lib/string.c +1
-0+* return value is a pointer to src.
^^ dest.)
Quoted 16 lines
lib/string.c +10
-0+*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return --dest; +} +#endif +#ifndef __HAVE_ARCH_STRCAT/** * strcat - Append one %NUL-terminated string to another Signature · 1 line
2.28.0.220.ged08abb693-goog
The kernel-doc comments in string.c currently have a mix of %NUL and NUL, but the former seems to be more common. %NUL-terminator appears to be the preferred wording.
Quoted 9 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpyis just likestrcpyexcept it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
[]
include/linux/string.h
Quoted 1 line
include/linux/string.h +0
-0
[]
include/linux/string.h size_t strlcpy(char *, const char *, size_t); · line 35
Quoted 6 lines
include/linux/string.h +2
-0@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict__,const char *__restrict__);
If __restrict__ is used, perhaps it should follow the kernel style used by attributes like __iomem and __user
extern char *stpcpy(char __restrict *dest, const char __restrict *src);
(though I would lose the extern too)
char *stpcpy(char __restrict *dest, const char __restrict *src);
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Nick,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on hnaz-linux-mm/master]
[also build test ERROR on kees/for-next/pstore linus/master v5.8 next-20200814] [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/Nick-Desaulniers/lib-string-c-implement-stpcpy/20200816-090542 base: https://github.com/hnaz/linux-mm master config: ia64-defconfig (attached as .config) compiler: ia64-linux-gcc (GCC) 9.3.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
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=ia64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: "stpcpy" [sound/pci/ac97/snd-ac97-codec.ko] undefined!
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Nick Desaulniers
reviewerQuoted 21 lines · Arvind Sankar
Quoted 19 lines · Nick Desaulniers
lib/string.c +19
-0+#ifndef __HAVE_ARCH_STPCPY +/** +* stpcpy - copy a string from src to dest returning a pointer to the new end +* of dest, including src's NULL terminator. May overrun dest. +* @dest: pointer to end of string being copied into. Must be large enough +* to receive copy. +* @src: pointer to the beginning of string being copied from. Must not overlap +* dest. +* +* stpcpy differs from strcpy in two key ways: +* 1. inputs must not overlap. +* 2. return value is the new NULL terminated character. (for strcpy, the +* return value is a pointer to src. +*/ +#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ;
Quoted 8 lines
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 297
Quoted 4 lines
lib/string.c +4
-0+ return dest; +} +#endif +Won't this return a pointer that's one _past_ the terminating NUL? I think you need the increments to be inside the loop body, rather than as part of the condition.
Yep, looks like I had a bug in my test program that masked this. Thanks for triple checking.
Quoted 2 lines
Nit: NUL is more correct than NULL to refer to the string terminator.
TIL.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Hi Nick,
Quoted 31 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings.
stpcpyis just likestrcpyexcept:
1. it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
2. it requires the parameters not to overlap. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpywas first standardized in POSIX.1-2008.Implement this so that we don't observe linkage failures due to missing symbol definitions for
stpcpy.Similar to last year's fire drill with:
commit5f074f3e192f("lib/string.c: implement a basic bcmp")This optimization was introduced into clang-12.
Reported-by Sami Tolvanen <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Quoted 4 lines
--- include/linux/string.h | 3 +++ lib/string.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)
Thank you for the patch! I can confirm that this fixes the build for me with ToT Clang.
Sami
Quoted 4 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings.
[]
include/linux/string.h
Quoted 1 line
include/linux/string.h +0
-0
[]
include/linux/string.h size_t strlcpy(char *, const char *, size_t); · line 35
Quoted 6 lines
include/linux/string.h +2
-0@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict,const char *__restrict__);
Why use two different forms for __restrict and __restrict__? Any real reason to use __restrict__ at all?
It's used nowhere else in the kernel.
$ git grep -w -P '__restrict_{0,2}'
scripts/genksyms/keywords.c: // According to rth, c99 defines "_Bool", __restrict", __restrict__", "restrict". KAO
scripts/genksyms/keywords.c: { "__restrict__", RESTRICT_KEYW },Nick Desaulniers
reviewerQuoted 9 lines · Joe Perches
Quoted 4 lines · Nick Desaulniers
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings.[]
include/linux/string.h
Quoted 1 line
include/linux/string.h +0
-0[]
Quoted 9 lines
include/linux/string.h size_t strlcpy(char *, const char *, size_t); · line 35
Quoted 6 lines
include/linux/string.h +2
-0@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict,const char *__restrict__);Why use two different forms for
__restrictand__restrict__? Any real reason to use__restrict__at all?
Bah, sorry, I recently enabled some setting in my ~/.vimrc to help me find my cursor better:
" highlight cursor
set cursorline
set cursorcolumn
Turns out this makes it pretty difficult to see underscores, or the lack thereof. Will fix up.
Quoted 8 lines
It's used nowhere else in the kernel.
$ git grep -w -P '__restrict_{0,2}' scripts/genksyms/keywords.c: // According to rth, c99 defines "_Bool", __restrict", __restrict__", "restrict". KAO scripts/genksyms/keywords.c: { "__restrict__", RESTRICT_KEYW },
Signature · 2 lines
Thanks, ~Nick Desaulniers
From: Nick Desaulniers
Quoted 19 lines
Sent: 15 August 2020 01:24
LLVM implemented a recent "libcall optimization" that lowers calls to
sprintf(dest, "%s", str)where the return value is used tostpcpy(dest, str) - dest. This generally avoids the machinery involved in parsing format strings.
stpcpyis just likestrcpyexcept:
1. it returns the pointer to the new tail ofdest. This allows you to chain multiple calls tostpcpyin one statement.
2. it requires the parameters not to overlap. Callingsprintfwith overlapping arguments was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior.
stpcpywas first standardized in POSIX.1-2008.Implement this so that we don't observe linkage failures due to missing symbol definitions for
stpcpy.
..
...
Quoted 23 lines
include/linux/string.h +3
-0@@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);#ifndef __HAVE_ARCH_STRSCPYssize_t strscpy (char *,const char *,size_t );#endif +#ifndef __HAVE_ARCH_STPCPY +extern char *stpcpy (char *__restrict,const char *__restrict__); +#endif /* Wraps calls to strscpy()/memset(), no arch specific code required */ ssize_t strscpy_pad (char *dest,const char *src,size_t count);lib/string.c +1
-0@@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) }EXPORT_SYMBOL (strscpy_pad); +#ifndef __HAVE_ARCH_STPCPY
...
lib/string.c ssize_t strscpy_pad(char *dest, const char *src, size_t count) · line 297
Quoted 9 lines
lib/string.c +9
-0+#undef stpcpy +char *stpcpy (char *__restrict__ dest,const char *__restrict__ src) +{ +while ((*dest++ = *src++) !='\0' ) +/* nothing */ ; +return dest; +} +#endif +
Hmmmm....
Maybe the compiler should just inline the above?
OTOH there are faster copies on 64bit systems
(for moderate length strings).
It would also be nicer if the compiler actually used/required a symbol in the 'reserved for the implementation' namespace. Then the linker should be able to do a fixup to a differently name symbol - if that is required.
But compiler writers enjoy making embedded coders life hell.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)