Skip to content

Commit cb94f82

Browse files
GUIDINGLIxiaoxiang781216
authored andcommitted
kasan: add builtin_return_address(0) to kasan
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 03e220a commit cb94f82

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

include/nuttx/compiler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
*/
158158

159159
# define offsetof(a, b) __builtin_offsetof(a, b)
160+
# define return_address(x) __builtin_return_address(x)
160161

161162
/* Attributes
162163
*
@@ -632,6 +633,7 @@
632633
# undef CONFIG_HAVE_LONG_DOUBLE
633634

634635
# define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))
636+
# define return_address(x) 0
635637

636638
# define no_builtin(n)
637639

@@ -773,6 +775,7 @@
773775
# undef CONFIG_HAVE_LONG_DOUBLE
774776

775777
# define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))
778+
# define return_address(x) 0
776779

777780
# define no_builtin(n)
778781

@@ -843,6 +846,7 @@
843846
# define CONFIG_HAVE_FLOAT 1
844847

845848
# define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))
849+
# define return_address(x) 0
846850

847851
# define no_builtin(n)
848852

@@ -920,6 +924,7 @@
920924
# define UNUSED(a) ((void)(1 || &(a)))
921925

922926
# define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))
927+
# define return_address(x) 0
923928

924929
# define no_builtin(n)
925930

@@ -988,6 +993,7 @@
988993
# define UNUSED(a) ((void)(1 || &(a)))
989994

990995
# define offsetof(a, b) ((size_t)(&(((a *)(0))->b)))
996+
# define return_address(x) 0
991997

992998
# define no_builtin(n)
993999

mm/kasan/kasan.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,18 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size,
102102
return NULL;
103103
}
104104

105-
static void kasan_report(FAR const void *addr, size_t size, bool is_write)
105+
static void kasan_report(FAR const void *addr, size_t size,
106+
bool is_write,
107+
FAR void *return_address)
106108
{
107109
static int recursion;
108110

109111
if (++recursion == 1)
110112
{
111-
_alert("kasan detected a %s access error, address at %0#"PRIxPTR
112-
", size is %zu\n", is_write ? "write" : "read",
113-
(uintptr_t)addr, size);
113+
_alert("kasan detected a %s access error, address at %p,"
114+
"size is %zu, return address: %p\n",
115+
is_write ? "write" : "read",
116+
addr, size, return_address);
114117
PANIC();
115118
}
116119

@@ -178,6 +181,16 @@ static void kasan_set_poison(FAR const void *addr, size_t size,
178181
spin_unlock_irqrestore(&g_lock, flags);
179182
}
180183

184+
static inline void kasan_check_report(FAR const void *addr, size_t size,
185+
bool is_write,
186+
FAR void *return_address)
187+
{
188+
if (kasan_is_poisoned(addr, size))
189+
{
190+
kasan_report(addr, size, false, return_address);
191+
}
192+
}
193+
181194
/****************************************************************************
182195
* Public Functions
183196
****************************************************************************/
@@ -242,64 +255,58 @@ void __asan_handle_no_return(void)
242255

243256
void __asan_report_load_n_noabort(FAR void *addr, size_t size)
244257
{
245-
kasan_report(addr, size, false);
258+
kasan_report(addr, size, false, return_address(0));
246259
}
247260

248261
void __asan_report_store_n_noabort(FAR void *addr, size_t size)
249262
{
250-
kasan_report(addr, size, true);
263+
kasan_report(addr, size, true, return_address(0));
251264
}
252265

253266
void __asan_loadN_noabort(FAR void *addr, size_t size)
254267
{
255-
if (kasan_is_poisoned(addr, size))
256-
{
257-
kasan_report(addr, size, false);
258-
}
268+
kasan_check_report(addr, size, false, return_address(0));
259269
}
260270

261271
void __asan_storeN_noabort(FAR void * addr, size_t size)
262272
{
263-
if (kasan_is_poisoned(addr, size))
264-
{
265-
kasan_report(addr, size, true);
266-
}
273+
kasan_check_report(addr, size, true, return_address(0));
267274
}
268275

269276
void __asan_loadN(FAR void *addr, size_t size)
270277
{
271-
__asan_loadN_noabort(addr, size);
278+
kasan_check_report(addr, size, false, return_address(0));
272279
}
273280

274281
void __asan_storeN(FAR void *addr, size_t size)
275282
{
276-
__asan_storeN_noabort(addr, size);
283+
kasan_check_report(addr, size, true, return_address(0));
277284
}
278285

279286
#define DEFINE_ASAN_LOAD_STORE(size) \
280287
void __asan_report_load##size##_noabort(FAR void *addr) \
281288
{ \
282-
__asan_report_load_n_noabort(addr, size); \
289+
kasan_report(addr, size, false, return_address(0)); \
283290
} \
284291
void __asan_report_store##size##_noabort(FAR void *addr) \
285292
{ \
286-
__asan_report_store_n_noabort(addr, size); \
293+
kasan_report(addr, size, true, return_address(0)); \
287294
} \
288295
void __asan_load##size##_noabort(FAR void *addr) \
289296
{ \
290-
__asan_loadN_noabort(addr, size); \
297+
kasan_check_report(addr, size, false, return_address(0)); \
291298
} \
292299
void __asan_store##size##_noabort(FAR void *addr) \
293300
{ \
294-
__asan_storeN_noabort(addr, size); \
301+
kasan_check_report(addr, size, true, return_address(0)); \
295302
} \
296303
void __asan_load##size(FAR void *addr) \
297304
{ \
298-
__asan_load##size##_noabort(addr); \
305+
kasan_check_report(addr, size, false, return_address(0)); \
299306
} \
300307
void __asan_store##size(FAR void *addr) \
301308
{ \
302-
__asan_store##size##_noabort(addr); \
309+
kasan_check_report(addr, size, true, return_address(0)); \
303310
}
304311

305312
DEFINE_ASAN_LOAD_STORE(1)

0 commit comments

Comments
 (0)