This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit dc6b7d34a40a469dc81046dd1f75bef89b0d72e4 Author: ganjing <[email protected]> AuthorDate: Tue Nov 24 12:20:56 2026 +0800 libc/machine: Use the generic strlen() in KASAN wrappers without an arch one. The KASAN wrappers of strcmp(), strcpy(), strchr(), strchrnul() and strrchr() measure the string with the architecture strlen(), and the wrapper of strncmp() with the architecture strnlen(). An architecture can provide one of those functions without its own strlen() or strnlen(), and the link then fails with an undefined reference to arch_strlen() or arch_strnlen() (for example x86_64 with X86_64_STRNCMP and without X86_64_STRNLEN). Use the generic strlen() and strnlen() in that case. Signed-off-by: ganjing <[email protected]> --- libs/libc/machine/arch_libc.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/libs/libc/machine/arch_libc.c b/libs/libc/machine/arch_libc.c index cbda598ff89..fcf360dea25 100644 --- a/libs/libc/machine/arch_libc.c +++ b/libs/libc/machine/arch_libc.c @@ -26,6 +26,27 @@ #include <string.h> +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The access checks below measure strings. An architecture may provide + * some of these functions without its own strlen() or strnlen(), so fall + * back to the generic ones then. + */ + +#ifdef CONFIG_LIBC_ARCH_STRLEN +# define kasan_strlen(s) ARCH_LIBCFUN(strlen)(s) +#else +# define kasan_strlen(s) strlen(s) +#endif + +#ifdef CONFIG_LIBC_ARCH_STRNLEN +# define kasan_strnlen(s, n) ARCH_LIBCFUN(strnlen)(s, n) +#else +# define kasan_strnlen(s, n) strnlen(s, n) +#endif + /**************************************************************************** * Public Functions prototypes ****************************************************************************/ @@ -185,8 +206,8 @@ int strcmp(FAR const char *s1, FAR const char *s2) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - __asan_loadN((FAR void *)s1, ARCH_LIBCFUN(strlen)(s1) + 1); - __asan_loadN((FAR void *)s2, ARCH_LIBCFUN(strlen)(s2) + 1); + __asan_loadN((FAR void *)s1, kasan_strlen(s1) + 1); + __asan_loadN((FAR void *)s2, kasan_strlen(s2) + 1); # endif # endif return ARCH_LIBCFUN(strcmp)(s1, s2); @@ -200,10 +221,10 @@ FAR char *strcpy(FAR char *dest, FAR const char *src) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_WRITES_CHECK - __asan_storeN(dest, ARCH_LIBCFUN(strlen)(src) + 1); + __asan_storeN(dest, kasan_strlen(src) + 1); # endif # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - __asan_loadN((FAR void *)src, ARCH_LIBCFUN(strlen)(src) + 1); + __asan_loadN((FAR void *)src, kasan_strlen(src) + 1); # endif # endif return ARCH_LIBCFUN(strcpy)(dest, src); @@ -249,7 +270,7 @@ FAR char *strchr(FAR const char *s, int c) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - __asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1); + __asan_loadN((FAR void *)s, kasan_strlen(s) + 1); # endif # endif @@ -262,7 +283,7 @@ FAR char *strchrnul(FAR const char *s, int c) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - __asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1); + __asan_loadN((FAR void *)s, kasan_strlen(s) + 1); # endif # endif return ARCH_LIBCFUN(strchrnul)(s, c); @@ -276,8 +297,8 @@ int strncmp(FAR const char *s1, FAR const char *s2, size_t n) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - size_t size_s1 = ARCH_LIBCFUN(strnlen)(s1, n); - size_t size_s2 = ARCH_LIBCFUN(strnlen)(s2, n); + size_t size_s1 = kasan_strnlen(s1, n); + size_t size_s2 = kasan_strnlen(s2, n); size_t size; size = size_s1 < size_s2 ? size_s1 : size_s2; @@ -310,7 +331,7 @@ FAR char *strrchr(FAR const char *s, int c) { # ifdef CONFIG_MM_KASAN_INSTRUMENT # ifndef CONFIG_MM_KASAN_DISABLE_READS_CHECK - __asan_loadN((FAR void *)s, ARCH_LIBCFUN(strlen)(s) + 1); + __asan_loadN((FAR void *)s, kasan_strlen(s) + 1); # endif # endif return ARCH_LIBCFUN(strrchr)(s, c);
