1
0
Fork 0
forked from nuttx/nuttx-update

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
This commit is contained in:
mage1 2021-04-14 15:43:16 +08:00 committed by Gustavo Henrique Nihei
parent 82c17fd6fa
commit def007e2d7
40 changed files with 81 additions and 0 deletions

View file

@ -41,9 +41,17 @@
* IEEE Std 1003.1-2008 * IEEE Std 1003.1-2008
*/ */
#ifndef bcmp /* See mm/README.txt */
#define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len) #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) #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) #define bzero(s,n) (void)memset(s,0,n)
#endif
/**************************************************************************** /****************************************************************************
* Inline Functions * Inline Functions

View file

@ -30,6 +30,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef aligned_alloc /* See mm/README.txt */
FAR void *aligned_alloc(size_t align, size_t size) FAR void *aligned_alloc(size_t align, size_t size)
{ {
return lib_memalign(align, size); return lib_memalign(align, size);

View file

@ -28,6 +28,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef atoi /* See mm/README.txt */
int atoi(FAR const char *nptr) int atoi(FAR const char *nptr)
{ {
return strtol(nptr, NULL, 10); return strtol(nptr, NULL, 10);

View file

@ -28,6 +28,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef atol /* See mm/README.txt */
long atol(FAR const char *nptr) long atol(FAR const char *nptr)
{ {
return strtol(nptr, NULL, 10); return strtol(nptr, NULL, 10);

View file

@ -31,6 +31,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef posix_memalign /* See mm/README.txt */
int posix_memalign(FAR void **mem, size_t align, size_t size) int posix_memalign(FAR void **mem, size_t align, size_t size)
{ {
*mem = lib_memalign(align, size); *mem = lib_memalign(align, size);

View file

@ -49,6 +49,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef valloc /* See mm/README.txt */
FAR void *valloc(size_t size) FAR void *valloc(size_t size)
{ {
return lib_memalign(sysconf(_SC_PAGESIZE), size); return lib_memalign(sysconf(_SC_PAGESIZE), size);

View file

@ -32,6 +32,7 @@
* Name: index * Name: index
****************************************************************************/ ****************************************************************************/
#undef index /* See mm/README.txt */
FAR char *index(FAR const char *s, int c) FAR char *index(FAR const char *s, int c)
{ {
return strchr(s, c); return strchr(s, c);

View file

@ -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 void *memccpy(FAR void *s1, FAR const void *s2, int c, size_t n)
{ {
FAR unsigned char *pout = (FAR unsigned char *)s1; FAR unsigned char *pout = (FAR unsigned char *)s1;

View file

@ -44,6 +44,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef memchr /* See mm/README.txt */
FAR void *memchr(FAR const void *s, int c, size_t n) FAR void *memchr(FAR const void *s, int c, size_t n)
{ {
FAR const unsigned char *p = (FAR const unsigned char *)s; FAR const unsigned char *p = (FAR const unsigned char *)s;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_MEMCMP #ifndef CONFIG_LIBC_ARCH_MEMCMP
#undef memcmp /* See mm/README.txt */
int memcmp(FAR const void *s1, FAR const void *s2, size_t n) int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
{ {
unsigned char *p1 = (unsigned char *)s1; unsigned char *p1 = (unsigned char *)s1;

View file

@ -35,6 +35,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_MEMCPY #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 void *memcpy(FAR void *dest, FAR const void *src, size_t n)
{ {
FAR unsigned char *pout = (FAR unsigned char *)dest; FAR unsigned char *pout = (FAR unsigned char *)dest;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_MEMMOVE #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 void *memmove(FAR void *dest, FAR const void *src, size_t count)
{ {
FAR char *tmp; FAR char *tmp;

View file

@ -47,6 +47,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_MEMSET #ifndef CONFIG_LIBC_ARCH_MEMSET
#undef memset /* See mm/README.txt */
FAR void *memset(FAR void *s, int c, size_t n) FAR void *memset(FAR void *s, int c, size_t n)
{ {
#ifdef CONFIG_MEMSET_OPTSPEED #ifdef CONFIG_MEMSET_OPTSPEED

View file

@ -32,6 +32,7 @@
* Name: rindex * Name: rindex
****************************************************************************/ ****************************************************************************/
#undef rindex /* See mm/README.txt */
FAR char *rindex(FAR const char *s, int c) FAR char *rindex(FAR const char *s, int c)
{ {
return strrchr(s, c); return strrchr(s, c);

View file

@ -32,6 +32,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_ARCH_STRCASECMP #ifndef CONFIG_ARCH_STRCASECMP
#undef strcasecmp /* See mm/README.txt */
int strcasecmp(FAR const char *cs, FAR const char *ct) int strcasecmp(FAR const char *cs, FAR const char *ct)
{ {
int result; int result;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_ARCH_STRCAT #ifndef CONFIG_ARCH_STRCAT
#undef strcat /* See mm/README.txt */
char *strcat(char *dest, const char *src) char *strcat(char *dest, const char *src)
{ {
char *ret = dest; char *ret = dest;

View file

@ -45,6 +45,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRCHR #ifndef CONFIG_LIBC_ARCH_STRCHR
#undef strchr /* See mm/README.txt */
FAR char *strchr(FAR const char *s, int c) FAR char *strchr(FAR const char *s, int c)
{ {
if (s) if (s)

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRCMP #ifndef CONFIG_LIBC_ARCH_STRCMP
#undef strcmp /* See mm/README.txt */
int strcmp(FAR const char *cs, FAR const char *ct) int strcmp(FAR const char *cs, FAR const char *ct)
{ {
register signed char result; register signed char result;

View file

@ -43,6 +43,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRCPY #ifndef CONFIG_LIBC_ARCH_STRCPY
#undef strcpy /* See mm/README.txt */
FAR char *strcpy(FAR char *dest, FAR const char *src) FAR char *strcpy(FAR char *dest, FAR const char *src)
{ {
char *tmp = dest; char *tmp = dest;

View file

@ -39,6 +39,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef strcspn /* See mm/README.txt */
size_t strcspn(const char *s, const char *reject) size_t strcspn(const char *s, const char *reject)
{ {
size_t i; size_t i;

View file

@ -32,6 +32,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef strdup /* See mm/README.txt */
FAR char *strdup(FAR const char *s) FAR char *strdup(FAR const char *s)
{ {
FAR char *news = NULL; FAR char *news = NULL;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRLEN #ifndef CONFIG_LIBC_ARCH_STRLEN
#undef strlen /* See mm/README.txt */
size_t strlen(const char *s) size_t strlen(const char *s)
{ {
const char *sc; const char *sc;

View file

@ -33,6 +33,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_ARCH_STRNCASECMP #ifndef CONFIG_ARCH_STRNCASECMP
#undef strncasecmp /* See mm/README.txt */
int strncasecmp(const char *cs, const char *ct, size_t nb) int strncasecmp(const char *cs, const char *ct, size_t nb)
{ {
int result = 0; int result = 0;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_ARCH_STRNCAT #ifndef CONFIG_ARCH_STRNCAT
#undef strncat /* See mm/README.txt */
char *strncat(char *dest, const char *src, size_t n) char *strncat(char *dest, const char *src, size_t n)
{ {
char *ret = dest; char *ret = dest;

View file

@ -31,6 +31,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_ARCH_STRNCMP #ifndef CONFIG_ARCH_STRNCMP
#undef strncmp /* See mm/README.txt */
int strncmp(const char *cs, const char *ct, size_t nb) int strncmp(const char *cs, const char *ct, size_t nb)
{ {
int result = 0; int result = 0;

View file

@ -52,6 +52,7 @@
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRNCPY #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 *strncpy(FAR char *dest, FAR const char *src, size_t n)
{ {
FAR char *ret = dest; /* Value to be returned */ FAR char *ret = dest; /* Value to be returned */

View file

@ -49,6 +49,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef strndup /* See mm/README.txt */
FAR char *strndup(FAR const char *s, size_t size) FAR char *strndup(FAR const char *s, size_t size)
{ {
FAR char *news = NULL; FAR char *news = NULL;

View file

@ -30,6 +30,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef strpbrk /* See mm/README.txt */
FAR char *strpbrk(FAR const char *str, FAR const char *charset) FAR char *strpbrk(FAR const char *str, FAR const char *charset)
{ {
/* Sanity checking */ /* Sanity checking */

View file

@ -34,6 +34,7 @@
* occurrence of the character c in the string s. * occurrence of the character c in the string s.
*/ */
#undef strrchr /* See mm/README.txt */
FAR char *strrchr(FAR const char *s, int c) FAR char *strrchr(FAR const char *s, int c)
{ {
if (s) if (s)

View file

@ -39,6 +39,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef strspn /* See mm/README.txt */
size_t strspn(const char *s, const char *accept) size_t strspn(const char *s, const char *accept)
{ {
size_t i; size_t i;

View file

@ -30,6 +30,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef strstr /* See mm/README.txt */
FAR char *strstr(FAR const char *str, FAR const char *substr) FAR char *strstr(FAR const char *str, FAR const char *substr)
{ {
FAR const char *candidate; /* Candidate in str with matching start character */ FAR const char *candidate; /* Candidate in str with matching start character */

View file

@ -66,6 +66,7 @@ static char *g_saveptr = NULL;
* *
****************************************************************************/ ****************************************************************************/
#undef strtok /* See mm/README.txt */
char *strtok(char *str, const char *delim) char *strtok(char *str, const char *delim)
{ {
return strtok_r(str, delim, &g_saveptr); return strtok_r(str, delim, &g_saveptr);

View file

@ -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/umm_heap - Holds the user-mode memory allocation interfaces
mm/kmm_heap - Holds the kernel-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 <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#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. 2) Granule Allocator.
A non-standard granule allocator is also available in this directory The A non-standard granule allocator is also available in this directory The

View file

@ -42,6 +42,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef calloc /* See mm/README.txt */
FAR void *calloc(size_t n, size_t elem_size) FAR void *calloc(size_t n, size_t elem_size)
{ {
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)

View file

@ -43,6 +43,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef free /* See mm/README.txt */
void free(FAR void *mem) void free(FAR void *mem)
{ {
mm_free(USR_HEAP, mem); mm_free(USR_HEAP, mem);

View file

@ -49,6 +49,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef malloc /* See mm/README.txt */
FAR void *malloc(size_t size) FAR void *malloc(size_t size)
{ {
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)

View file

@ -32,6 +32,7 @@
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#undef malloc_size /* See mm/README.txt */
size_t malloc_size(FAR void *mem) size_t malloc_size(FAR void *mem)
{ {
return mm_malloc_size(mem); return mm_malloc_size(mem);

View file

@ -48,6 +48,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef memalign /* See mm/README.txt */
FAR void *memalign(size_t alignment, size_t size) FAR void *memalign(size_t alignment, size_t size)
{ {
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)

View file

@ -50,6 +50,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef realloc /* See mm/README.txt */
FAR void *realloc(FAR void *oldmem, size_t size) FAR void *realloc(FAR void *oldmem, size_t size)
{ {
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)

View file

@ -49,6 +49,7 @@
* *
****************************************************************************/ ****************************************************************************/
#undef zalloc /* See mm/README.txt */
FAR void *zalloc(size_t size) FAR void *zalloc(size_t size)
{ {
#ifdef CONFIG_ARCH_ADDRENV #ifdef CONFIG_ARCH_ADDRENV