Skip to content

Commit

Permalink
mm/kasan: extern kasan API only MM_KASAN is enabled
Browse files Browse the repository at this point in the history
this PR will fix g_region_init is incorrectly linked to the image if MM_KASAN is not enabled.

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao authored and xiaoxiang781216 committed Oct 25, 2024
1 parent 6a2e21d commit 052ea5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 8 additions & 4 deletions include/nuttx/mm/kasan.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@
* Pre-processor Definitions
****************************************************************************/

#define kasan_init_early() kasan_stop()

#ifndef CONFIG_MM_KASAN
# define kasan_poison(addr, size)
# define kasan_unpoison(addr, size) addr
# define kasan_register(addr, size)
# define kasan_unregister(addr)
# define kasan_reset_tag(addr) addr
# define kasan_start()
# define kasan_stop()
# define kasan_debugpoint(t,a,s) 0
# define kasan_init_early()
#else

# define kasan_init_early() kasan_stop()

/****************************************************************************
* Public Function Prototypes
****************************************************************************/
Expand Down Expand Up @@ -141,8 +145,6 @@ void kasan_unregister(FAR void *addr);

FAR void *kasan_reset_tag(FAR const void *addr);

#endif /* CONFIG_MM_KASAN */

/****************************************************************************
* Name: kasan_start
*
Expand Down Expand Up @@ -204,4 +206,6 @@ int kasan_debugpoint(int type, FAR void *addr, size_t size);
}
#endif

#endif /* CONFIG_MM_KASAN */

#endif /* __INCLUDE_NUTTX_MM_KASAN_H */
16 changes: 11 additions & 5 deletions mm/kasan/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ struct kasan_watchpoint_s
static struct kasan_watchpoint_s g_watchpoint[MM_KASAN_WATCHPOINT];
#endif

#ifdef CONFIG_MM_KASAN
static uint32_t g_region_init;
#endif

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -225,34 +227,37 @@ static inline void kasan_check_report(FAR const void *addr, size_t size,
bool is_write,
FAR void *return_address)
{
#ifdef CONFIG_MM_KASAN
if (predict_false(size == 0 || g_region_init != KASAN_INIT_VALUE))
{
return;
}

#ifndef CONFIG_MM_KASAN_DISABLE_NULL_POINTER_CHECK
# ifndef CONFIG_MM_KASAN_DISABLE_NULL_POINTER_CHECK
if (predict_false(addr == NULL))
{
kasan_report(addr, size, is_write, return_address);
}
#endif
# endif

#ifndef CONFIG_MM_KASAN_NONE
# ifndef CONFIG_MM_KASAN_NONE
if (predict_false(kasan_is_poisoned(addr, size)))
{
kasan_report(addr, size, is_write, return_address);
}
#endif
# endif

#if MM_KASAN_WATCHPOINT > 0
# if MM_KASAN_WATCHPOINT > 0
kasan_check_watchpoint(addr, size, is_write, return_address);
# endif
#endif
}

/****************************************************************************
* Public Functions
****************************************************************************/

#ifdef CONFIG_MM_KASAN
void kasan_start(void)
{
g_region_init = KASAN_INIT_VALUE;
Expand All @@ -262,6 +267,7 @@ void kasan_stop(void)
{
g_region_init = 0;
}
#endif

/****************************************************************************
* Name: kasan_debugpoint
Expand Down

0 comments on commit 052ea5b

Please sign in to comment.