[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <213d67e912f340b0815dd4bd989befce@AcuMS.aculab.com>
Date: Sat, 15 Aug 2020 22:17:44 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Nick Desaulniers' <ndesaulniers@...gle.com>,
Andrew Morton <akpm@...ux-foundation.org>
CC: Dávid Bolvanský <david.bolvansky@...il.com>,
"Eli Friedman" <efriedma@...cinc.com>,
"stable@...r.kernel.org" <stable@...r.kernel.org>,
Sami Tolvanen <samitolvanen@...gle.com>,
"Joe Perches" <joe@...ches.com>, Tony Luck <tony.luck@...el.com>,
Yury Norov <yury.norov@...il.com>,
Daniel Axtens <dja@...ens.net>,
Arvind Sankar <nivedita@...m.mit.edu>,
Dan Williams <dan.j.williams@...el.com>,
"Joel Fernandes (Google)" <joel@...lfernandes.org>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Kees Cook <keescook@...omium.org>,
Alexandru Ardelean <alexandru.ardelean@...log.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"clang-built-linux@...glegroups.com"
<clang-built-linux@...glegroups.com>
Subject: RE: [PATCH] lib/string.c: implement stpcpy
From: Nick Desaulniers
> 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 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`.
>
..
...
> diff --git a/include/linux/string.h b/include/linux/string.h
> index b1f3894a0a3e..e570b9b10f50 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -31,6 +31,9 @@ size_t strlcpy(char *, const char *, size_t);
> #ifndef __HAVE_ARCH_STRSCPY
> ssize_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);
> diff --git a/lib/string.c b/lib/string.c
> index 6012c385fb31..81bc4d62c256 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -272,6 +272,29 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count)
> }
> EXPORT_SYMBOL(strscpy_pad);
>
> +#ifndef __HAVE_ARCH_STPCPY
...
> +#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)
Powered by blists - more mailing lists