From def007e2d76ae196f85c71315fa6d73ecda52f95 Mon Sep 17 00:00:00 2001 From: mage1 Date: Wed, 14 Apr 2021 15:43:16 +0800 Subject: [PATCH] add #undef for some libc function since it's useful to redirect these functions to others sometime(e.g. validate the memory before write). Change-Id: I6253a9231af8809e8362f4bc5a1bd67fb094c3b0 --- include/strings.h | 8 ++++++ libs/libc/stdlib/lib_aligned_alloc.c | 1 + libs/libc/stdlib/lib_atoi.c | 1 + libs/libc/stdlib/lib_atol.c | 1 + libs/libc/stdlib/lib_posix_memalign.c | 1 + libs/libc/stdlib/lib_valloc.c | 1 + libs/libc/string/lib_index.c | 1 + libs/libc/string/lib_memccpy.c | 1 + libs/libc/string/lib_memchr.c | 1 + libs/libc/string/lib_memcmp.c | 1 + libs/libc/string/lib_memcpy.c | 1 + libs/libc/string/lib_memmove.c | 1 + libs/libc/string/lib_memset.c | 1 + libs/libc/string/lib_rindex.c | 1 + libs/libc/string/lib_strcasecmp.c | 1 + libs/libc/string/lib_strcat.c | 1 + libs/libc/string/lib_strchr.c | 1 + libs/libc/string/lib_strcmp.c | 1 + libs/libc/string/lib_strcpy.c | 1 + libs/libc/string/lib_strcspn.c | 1 + libs/libc/string/lib_strdup.c | 1 + libs/libc/string/lib_strlen.c | 1 + libs/libc/string/lib_strncasecmp.c | 1 + libs/libc/string/lib_strncat.c | 1 + libs/libc/string/lib_strncmp.c | 1 + libs/libc/string/lib_strncpy.c | 1 + libs/libc/string/lib_strndup.c | 1 + libs/libc/string/lib_strpbrk.c | 1 + libs/libc/string/lib_strrchr.c | 1 + libs/libc/string/lib_strspn.c | 1 + libs/libc/string/lib_strstr.c | 1 + libs/libc/string/lib_strtok.c | 1 + mm/README.txt | 35 +++++++++++++++++++++++++++ mm/umm_heap/umm_calloc.c | 1 + mm/umm_heap/umm_free.c | 1 + mm/umm_heap/umm_malloc.c | 1 + mm/umm_heap/umm_malloc_size.c | 1 + mm/umm_heap/umm_memalign.c | 1 + mm/umm_heap/umm_realloc.c | 1 + mm/umm_heap/umm_zalloc.c | 1 + 40 files changed, 81 insertions(+) diff --git a/include/strings.h b/include/strings.h index 7096bca3dd9..2ceba616420 100644 --- a/include/strings.h +++ b/include/strings.h @@ -41,9 +41,17 @@ * IEEE Std 1003.1-2008 */ +#ifndef bcmp /* See mm/README.txt */ #define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len) +#endif + +#ifndef bcopy /* See mm/README.txt */ #define bcopy(b1,b2,len) (void)memmove(b2,b1,len) +#endif + +#ifndef bzero /* See mm/README.txt */ #define bzero(s,n) (void)memset(s,0,n) +#endif /**************************************************************************** * Inline Functions diff --git a/libs/libc/stdlib/lib_aligned_alloc.c b/libs/libc/stdlib/lib_aligned_alloc.c index ee025063b99..53bcebc99a0 100644 --- a/libs/libc/stdlib/lib_aligned_alloc.c +++ b/libs/libc/stdlib/lib_aligned_alloc.c @@ -30,6 +30,7 @@ * Public Functions ****************************************************************************/ +#undef aligned_alloc /* See mm/README.txt */ FAR void *aligned_alloc(size_t align, size_t size) { return lib_memalign(align, size); diff --git a/libs/libc/stdlib/lib_atoi.c b/libs/libc/stdlib/lib_atoi.c index 916dec70e16..4181464e50f 100644 --- a/libs/libc/stdlib/lib_atoi.c +++ b/libs/libc/stdlib/lib_atoi.c @@ -28,6 +28,7 @@ * Public Functions ****************************************************************************/ +#undef atoi /* See mm/README.txt */ int atoi(FAR const char *nptr) { return strtol(nptr, NULL, 10); diff --git a/libs/libc/stdlib/lib_atol.c b/libs/libc/stdlib/lib_atol.c index e3f20643012..d1d6ba84ed6 100644 --- a/libs/libc/stdlib/lib_atol.c +++ b/libs/libc/stdlib/lib_atol.c @@ -28,6 +28,7 @@ * Public Functions ****************************************************************************/ +#undef atol /* See mm/README.txt */ long atol(FAR const char *nptr) { return strtol(nptr, NULL, 10); diff --git a/libs/libc/stdlib/lib_posix_memalign.c b/libs/libc/stdlib/lib_posix_memalign.c index f7fea9952bf..f4f8e5ef9d4 100644 --- a/libs/libc/stdlib/lib_posix_memalign.c +++ b/libs/libc/stdlib/lib_posix_memalign.c @@ -31,6 +31,7 @@ * Public Functions ****************************************************************************/ +#undef posix_memalign /* See mm/README.txt */ int posix_memalign(FAR void **mem, size_t align, size_t size) { *mem = lib_memalign(align, size); diff --git a/libs/libc/stdlib/lib_valloc.c b/libs/libc/stdlib/lib_valloc.c index fa16ccc4df8..1c72cf7e921 100644 --- a/libs/libc/stdlib/lib_valloc.c +++ b/libs/libc/stdlib/lib_valloc.c @@ -49,6 +49,7 @@ * ****************************************************************************/ +#undef valloc /* See mm/README.txt */ FAR void *valloc(size_t size) { return lib_memalign(sysconf(_SC_PAGESIZE), size); diff --git a/libs/libc/string/lib_index.c b/libs/libc/string/lib_index.c index 94f43f33ab1..e62745ccf91 100644 --- a/libs/libc/string/lib_index.c +++ b/libs/libc/string/lib_index.c @@ -32,6 +32,7 @@ * Name: index ****************************************************************************/ +#undef index /* See mm/README.txt */ FAR char *index(FAR const char *s, int c) { return strchr(s, c); diff --git a/libs/libc/string/lib_memccpy.c b/libs/libc/string/lib_memccpy.c index 6b82152e69a..e9f1773ebbf 100644 --- a/libs/libc/string/lib_memccpy.c +++ b/libs/libc/string/lib_memccpy.c @@ -46,6 +46,7 @@ * ****************************************************************************/ +#undef memccpy /* See mm/README.txt */ FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, size_t n) { FAR unsigned char *pout = (FAR unsigned char *)s1; diff --git a/libs/libc/string/lib_memchr.c b/libs/libc/string/lib_memchr.c index 51b9c23612a..ec96b1cbc3f 100644 --- a/libs/libc/string/lib_memchr.c +++ b/libs/libc/string/lib_memchr.c @@ -44,6 +44,7 @@ * ****************************************************************************/ +#undef memchr /* See mm/README.txt */ FAR void *memchr(FAR const void *s, int c, size_t n) { FAR const unsigned char *p = (FAR const unsigned char *)s; diff --git a/libs/libc/string/lib_memcmp.c b/libs/libc/string/lib_memcmp.c index 21918597b8c..1f788f451ef 100644 --- a/libs/libc/string/lib_memcmp.c +++ b/libs/libc/string/lib_memcmp.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_MEMCMP +#undef memcmp /* See mm/README.txt */ int memcmp(FAR const void *s1, FAR const void *s2, size_t n) { unsigned char *p1 = (unsigned char *)s1; diff --git a/libs/libc/string/lib_memcpy.c b/libs/libc/string/lib_memcpy.c index d40bcc693e2..de4f8eed23b 100644 --- a/libs/libc/string/lib_memcpy.c +++ b/libs/libc/string/lib_memcpy.c @@ -35,6 +35,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_MEMCPY +#undef memcpy /* See mm/README.txt */ FAR void *memcpy(FAR void *dest, FAR const void *src, size_t n) { FAR unsigned char *pout = (FAR unsigned char *)dest; diff --git a/libs/libc/string/lib_memmove.c b/libs/libc/string/lib_memmove.c index 17dfc13bccf..bc72a85daf5 100644 --- a/libs/libc/string/lib_memmove.c +++ b/libs/libc/string/lib_memmove.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_MEMMOVE +#undef memmove /* See mm/README.txt */ FAR void *memmove(FAR void *dest, FAR const void *src, size_t count) { FAR char *tmp; diff --git a/libs/libc/string/lib_memset.c b/libs/libc/string/lib_memset.c index 961843ac09e..e22a6cd205f 100644 --- a/libs/libc/string/lib_memset.c +++ b/libs/libc/string/lib_memset.c @@ -47,6 +47,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_MEMSET +#undef memset /* See mm/README.txt */ FAR void *memset(FAR void *s, int c, size_t n) { #ifdef CONFIG_MEMSET_OPTSPEED diff --git a/libs/libc/string/lib_rindex.c b/libs/libc/string/lib_rindex.c index b59513b256a..2375ee0177c 100644 --- a/libs/libc/string/lib_rindex.c +++ b/libs/libc/string/lib_rindex.c @@ -32,6 +32,7 @@ * Name: rindex ****************************************************************************/ +#undef rindex /* See mm/README.txt */ FAR char *rindex(FAR const char *s, int c) { return strrchr(s, c); diff --git a/libs/libc/string/lib_strcasecmp.c b/libs/libc/string/lib_strcasecmp.c index 1cf59fe0dc7..e02fddd717a 100644 --- a/libs/libc/string/lib_strcasecmp.c +++ b/libs/libc/string/lib_strcasecmp.c @@ -32,6 +32,7 @@ ****************************************************************************/ #ifndef CONFIG_ARCH_STRCASECMP +#undef strcasecmp /* See mm/README.txt */ int strcasecmp(FAR const char *cs, FAR const char *ct) { int result; diff --git a/libs/libc/string/lib_strcat.c b/libs/libc/string/lib_strcat.c index 06f6feff97b..beb23e2652b 100644 --- a/libs/libc/string/lib_strcat.c +++ b/libs/libc/string/lib_strcat.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_ARCH_STRCAT +#undef strcat /* See mm/README.txt */ char *strcat(char *dest, const char *src) { char *ret = dest; diff --git a/libs/libc/string/lib_strchr.c b/libs/libc/string/lib_strchr.c index 81182b77ade..7da3bf99052 100644 --- a/libs/libc/string/lib_strchr.c +++ b/libs/libc/string/lib_strchr.c @@ -45,6 +45,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_STRCHR +#undef strchr /* See mm/README.txt */ FAR char *strchr(FAR const char *s, int c) { if (s) diff --git a/libs/libc/string/lib_strcmp.c b/libs/libc/string/lib_strcmp.c index 15da7a8cc75..2ac4f62619a 100644 --- a/libs/libc/string/lib_strcmp.c +++ b/libs/libc/string/lib_strcmp.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_STRCMP +#undef strcmp /* See mm/README.txt */ int strcmp(FAR const char *cs, FAR const char *ct) { register signed char result; diff --git a/libs/libc/string/lib_strcpy.c b/libs/libc/string/lib_strcpy.c index f40fe603592..d66dc7e5a49 100644 --- a/libs/libc/string/lib_strcpy.c +++ b/libs/libc/string/lib_strcpy.c @@ -43,6 +43,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_STRCPY +#undef strcpy /* See mm/README.txt */ FAR char *strcpy(FAR char *dest, FAR const char *src) { char *tmp = dest; diff --git a/libs/libc/string/lib_strcspn.c b/libs/libc/string/lib_strcspn.c index 5f250352a80..7a8211ef0a4 100644 --- a/libs/libc/string/lib_strcspn.c +++ b/libs/libc/string/lib_strcspn.c @@ -39,6 +39,7 @@ * ****************************************************************************/ +#undef strcspn /* See mm/README.txt */ size_t strcspn(const char *s, const char *reject) { size_t i; diff --git a/libs/libc/string/lib_strdup.c b/libs/libc/string/lib_strdup.c index 82c42dbdfc9..9e548bc4199 100644 --- a/libs/libc/string/lib_strdup.c +++ b/libs/libc/string/lib_strdup.c @@ -32,6 +32,7 @@ * Public Functions ****************************************************************************/ +#undef strdup /* See mm/README.txt */ FAR char *strdup(FAR const char *s) { FAR char *news = NULL; diff --git a/libs/libc/string/lib_strlen.c b/libs/libc/string/lib_strlen.c index c559257b7d2..647c6d06433 100644 --- a/libs/libc/string/lib_strlen.c +++ b/libs/libc/string/lib_strlen.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_STRLEN +#undef strlen /* See mm/README.txt */ size_t strlen(const char *s) { const char *sc; diff --git a/libs/libc/string/lib_strncasecmp.c b/libs/libc/string/lib_strncasecmp.c index 9a2cb52db1c..2b3ac6bfe3a 100644 --- a/libs/libc/string/lib_strncasecmp.c +++ b/libs/libc/string/lib_strncasecmp.c @@ -33,6 +33,7 @@ ****************************************************************************/ #ifndef CONFIG_ARCH_STRNCASECMP +#undef strncasecmp /* See mm/README.txt */ int strncasecmp(const char *cs, const char *ct, size_t nb) { int result = 0; diff --git a/libs/libc/string/lib_strncat.c b/libs/libc/string/lib_strncat.c index c9cf3efa0d0..cc68ea156a5 100644 --- a/libs/libc/string/lib_strncat.c +++ b/libs/libc/string/lib_strncat.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_ARCH_STRNCAT +#undef strncat /* See mm/README.txt */ char *strncat(char *dest, const char *src, size_t n) { char *ret = dest; diff --git a/libs/libc/string/lib_strncmp.c b/libs/libc/string/lib_strncmp.c index 8d9b51226b2..117f1fddf88 100644 --- a/libs/libc/string/lib_strncmp.c +++ b/libs/libc/string/lib_strncmp.c @@ -31,6 +31,7 @@ ****************************************************************************/ #ifndef CONFIG_ARCH_STRNCMP +#undef strncmp /* See mm/README.txt */ int strncmp(const char *cs, const char *ct, size_t nb) { int result = 0; diff --git a/libs/libc/string/lib_strncpy.c b/libs/libc/string/lib_strncpy.c index 90acbad695f..1a7cf58bb23 100644 --- a/libs/libc/string/lib_strncpy.c +++ b/libs/libc/string/lib_strncpy.c @@ -52,6 +52,7 @@ ****************************************************************************/ #ifndef CONFIG_LIBC_ARCH_STRNCPY +#undef strncpy /* See mm/README.txt */ FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n) { FAR char *ret = dest; /* Value to be returned */ diff --git a/libs/libc/string/lib_strndup.c b/libs/libc/string/lib_strndup.c index 07b124ed175..d6b0313695c 100644 --- a/libs/libc/string/lib_strndup.c +++ b/libs/libc/string/lib_strndup.c @@ -49,6 +49,7 @@ * ****************************************************************************/ +#undef strndup /* See mm/README.txt */ FAR char *strndup(FAR const char *s, size_t size) { FAR char *news = NULL; diff --git a/libs/libc/string/lib_strpbrk.c b/libs/libc/string/lib_strpbrk.c index 1f3ca0eb64e..90e56a86c1e 100644 --- a/libs/libc/string/lib_strpbrk.c +++ b/libs/libc/string/lib_strpbrk.c @@ -30,6 +30,7 @@ * Public Functions ****************************************************************************/ +#undef strpbrk /* See mm/README.txt */ FAR char *strpbrk(FAR const char *str, FAR const char *charset) { /* Sanity checking */ diff --git a/libs/libc/string/lib_strrchr.c b/libs/libc/string/lib_strrchr.c index 80b6d7d067d..9e5e4c80b0a 100644 --- a/libs/libc/string/lib_strrchr.c +++ b/libs/libc/string/lib_strrchr.c @@ -34,6 +34,7 @@ * occurrence of the character c in the string s. */ +#undef strrchr /* See mm/README.txt */ FAR char *strrchr(FAR const char *s, int c) { if (s) diff --git a/libs/libc/string/lib_strspn.c b/libs/libc/string/lib_strspn.c index ee88d509f7e..65443e04859 100644 --- a/libs/libc/string/lib_strspn.c +++ b/libs/libc/string/lib_strspn.c @@ -39,6 +39,7 @@ * ****************************************************************************/ +#undef strspn /* See mm/README.txt */ size_t strspn(const char *s, const char *accept) { size_t i; diff --git a/libs/libc/string/lib_strstr.c b/libs/libc/string/lib_strstr.c index a9c7f9ad90b..d8ee9f2768d 100644 --- a/libs/libc/string/lib_strstr.c +++ b/libs/libc/string/lib_strstr.c @@ -30,6 +30,7 @@ * Public Functions ****************************************************************************/ +#undef strstr /* See mm/README.txt */ FAR char *strstr(FAR const char *str, FAR const char *substr) { FAR const char *candidate; /* Candidate in str with matching start character */ diff --git a/libs/libc/string/lib_strtok.c b/libs/libc/string/lib_strtok.c index 4556d0ee55c..782294358b9 100644 --- a/libs/libc/string/lib_strtok.c +++ b/libs/libc/string/lib_strtok.c @@ -66,6 +66,7 @@ static char *g_saveptr = NULL; * ****************************************************************************/ +#undef strtok /* See mm/README.txt */ char *strtok(char *str, const char *delim) { return strtok_r(str, delim, &g_saveptr); diff --git a/mm/README.txt b/mm/README.txt index 8c2856a16b0..5607cbbaa95 100644 --- a/mm/README.txt +++ b/mm/README.txt @@ -74,6 +74,41 @@ This directory contains the NuttX memory management logic. This include: mm/umm_heap - Holds the user-mode memory allocation interfaces mm/kmm_heap - Holds the kernel-mode memory allocation interfaces + Debugging: + + Please follow these steps to hook all memory related routines: + + 1.Add a new header file(e.g. xxx_malloc.h): + + ... + #include + #include + #include + #include + + #ifndef __ASSEMBLY__ + FAR void *xxx_malloc(FAR const char *file, int line, size_t size); + void xxx_free(FAR const char *file, int line, FAR const void *ptr); + FAR void *xxx_memcpy(FAR const char *file, int line, + FAR void *dst, FAR const void *src, size_t len); + ... + #define malloc(s) xxx_malloc(__FILE__, __LINE__, s) + #define free(p) xxx_free(__FILE__, __LINE__, p) + #define memcpy(d, s, l) xxx_memcpy(__FILE__, __LINE__, d, s, l) + ... + #endif + ... + + 2.Implement xxx_malloc, xxx_free, xxx_memcpy... in source code, you can: + a.Modify some arguments(e.g. extend the allocation size for redzone) + d.Check the critical arguments(e.g. pointer and length) in the range + b.Forward to the original implementation(call malloc/free/memcpy) + c.Attach the context info(e.g. file and line) before return + + 3.Enable the hook by either: + a.Include xxx_malloc.h in your source code to hook one file + b.Add -include xxx_malloc.h to CFLAGS to hook all source code + 2) Granule Allocator. A non-standard granule allocator is also available in this directory The diff --git a/mm/umm_heap/umm_calloc.c b/mm/umm_heap/umm_calloc.c index d10142bd77e..69eadb746df 100644 --- a/mm/umm_heap/umm_calloc.c +++ b/mm/umm_heap/umm_calloc.c @@ -42,6 +42,7 @@ * ****************************************************************************/ +#undef calloc /* See mm/README.txt */ FAR void *calloc(size_t n, size_t elem_size) { #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) diff --git a/mm/umm_heap/umm_free.c b/mm/umm_heap/umm_free.c index fca7060013c..ebdf95274d9 100644 --- a/mm/umm_heap/umm_free.c +++ b/mm/umm_heap/umm_free.c @@ -43,6 +43,7 @@ * ****************************************************************************/ +#undef free /* See mm/README.txt */ void free(FAR void *mem) { mm_free(USR_HEAP, mem); diff --git a/mm/umm_heap/umm_malloc.c b/mm/umm_heap/umm_malloc.c index 1906eb1bd6d..261f0d8460c 100644 --- a/mm/umm_heap/umm_malloc.c +++ b/mm/umm_heap/umm_malloc.c @@ -49,6 +49,7 @@ * ****************************************************************************/ +#undef malloc /* See mm/README.txt */ FAR void *malloc(size_t size) { #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) diff --git a/mm/umm_heap/umm_malloc_size.c b/mm/umm_heap/umm_malloc_size.c index 74c0e76e967..c095e6bfd04 100644 --- a/mm/umm_heap/umm_malloc_size.c +++ b/mm/umm_heap/umm_malloc_size.c @@ -32,6 +32,7 @@ * Public Functions ****************************************************************************/ +#undef malloc_size /* See mm/README.txt */ size_t malloc_size(FAR void *mem) { return mm_malloc_size(mem); diff --git a/mm/umm_heap/umm_memalign.c b/mm/umm_heap/umm_memalign.c index 1a0f4d069d1..9181b349d52 100644 --- a/mm/umm_heap/umm_memalign.c +++ b/mm/umm_heap/umm_memalign.c @@ -48,6 +48,7 @@ * ****************************************************************************/ +#undef memalign /* See mm/README.txt */ FAR void *memalign(size_t alignment, size_t size) { #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) diff --git a/mm/umm_heap/umm_realloc.c b/mm/umm_heap/umm_realloc.c index 6466a03bf7c..a320a33303c 100644 --- a/mm/umm_heap/umm_realloc.c +++ b/mm/umm_heap/umm_realloc.c @@ -50,6 +50,7 @@ * ****************************************************************************/ +#undef realloc /* See mm/README.txt */ FAR void *realloc(FAR void *oldmem, size_t size) { #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) diff --git a/mm/umm_heap/umm_zalloc.c b/mm/umm_heap/umm_zalloc.c index 4c367f1f96e..11a7ba0a0c5 100644 --- a/mm/umm_heap/umm_zalloc.c +++ b/mm/umm_heap/umm_zalloc.c @@ -49,6 +49,7 @@ * ****************************************************************************/ +#undef zalloc /* See mm/README.txt */ FAR void *zalloc(size_t size) { #ifdef CONFIG_ARCH_ADDRENV