mm/kasan: Fix the issue where Kasan Global cannot be used

1. The shadow area of global variables is different from heap
2. This modification has little impact on performance under O3 optimization

Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
This commit is contained in:
wangmingrong1 2024-09-06 17:27:45 +08:00 committed by Xiang Xiao
parent 4473f2f847
commit 27f8f87331

View File

@ -92,7 +92,8 @@ extern const unsigned char g_globals_region[];
****************************************************************************/
static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size,
unsigned int *bit)
FAR unsigned int *bit,
FAR size_t *align)
{
FAR struct kasan_region_s *region;
uintptr_t addr = (uintptr_t)ptr;
@ -103,6 +104,7 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size,
{
DEBUGASSERT(addr + size <= region->end);
addr -= region->begin;
*align = KASAN_SHADOW_SCALE;
addr /= KASAN_SHADOW_SCALE;
*bit = addr % KASAN_BITS_PER_WORD;
return &region->shadow[addr / KASAN_BITS_PER_WORD];
@ -118,6 +120,7 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size,
{
DEBUGASSERT(addr + size <= region->end);
addr -= region->begin;
*align = KASAN_GLOBAL_SHADOW_SCALE;
addr /= KASAN_GLOBAL_SHADOW_SCALE;
*bit = addr % KASAN_BITS_PER_WORD;
return &region->shadow[addr / KASAN_BITS_PER_WORD];
@ -136,8 +139,9 @@ static void kasan_set_poison(FAR const void *addr, size_t size,
unsigned int bit;
unsigned int nbit;
uintptr_t mask;
size_t align;
p = kasan_mem_to_shadow(addr, size, &bit);
p = kasan_mem_to_shadow(addr, size, &bit, &align);
if (p == NULL)
{
return;
@ -145,7 +149,7 @@ static void kasan_set_poison(FAR const void *addr, size_t size,
nbit = KASAN_BITS_PER_WORD - bit % KASAN_BITS_PER_WORD;
mask = KASAN_FIRST_WORD_MASK(bit);
size /= KASAN_SHADOW_SCALE;
size /= align;
flags = spin_lock_irqsave(&g_lock);
while (size >= nbit)
@ -197,22 +201,23 @@ bool kasan_is_poisoned(FAR const void *addr, size_t size)
unsigned int bit;
unsigned int nbit;
uintptr_t mask;
size_t align;
p = kasan_mem_to_shadow(addr, size, &bit);
p = kasan_mem_to_shadow(addr, size, &bit, &align);
if (p == NULL)
{
return false;
}
if (size <= KASAN_SHADOW_SCALE)
if (size <= align)
{
return ((*p >> bit) & 1);
}
nbit = KASAN_BITS_PER_WORD - bit % KASAN_BITS_PER_WORD;
mask = KASAN_FIRST_WORD_MASK(bit);
size = ALIGN_UP(size, KASAN_SHADOW_SCALE);
size /= KASAN_SHADOW_SCALE;
size = ALIGN_UP(size, align);
size /= align;
while (size >= nbit)
{