This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit e2ebc9d5c3957995dd8e550d4246c73f2d0a2584 Author: wangmingrong1 <[email protected]> AuthorDate: Wed Jun 4 17:29:34 2025 +0800 kasan: Support asan poison memory The instrumentation is retained, and the sim asan global variables, stack and other checks can still be used [skip-cmake-parity] Signed-off-by: wangmingrong1 <[email protected]> --- arch/sim/src/cmake/Toolchain.cmake | 2 +- arch/sim/src/sim/sim_fork.c | 2 +- arch/sim/src/sim/sim_head.c | 2 +- arch/sim/src/sim/sim_initialstate.c | 4 +- boards/sim/sim/sim/scripts/Make.defs | 2 +- mm/kasan/CMakeLists.txt | 3 +- mm/kasan/Kconfig | 6 +++ mm/kasan/Make.defs | 1 + mm/kasan/hook.c | 2 + mm/kasan/sim_asan.c | 74 ++++++++++++++++++++++++++++++++++++ 10 files changed, 91 insertions(+), 7 deletions(-) diff --git a/arch/sim/src/cmake/Toolchain.cmake b/arch/sim/src/cmake/Toolchain.cmake index cb75b9c696e..0b1d75a7c5c 100644 --- a/arch/sim/src/cmake/Toolchain.cmake +++ b/arch/sim/src/cmake/Toolchain.cmake @@ -161,7 +161,7 @@ if(CONFIG_PROFILE_ALL OR CONFIG_SIM_PROFILE) add_compile_options(-pg) endif() -if(CONFIG_SIM_ASAN) +if(CONFIG_SIM_ASAN OR CONFIG_MM_KASAN_SIM) add_compile_options(-fsanitize=address) add_link_options(-fsanitize=address) add_compile_options(-fsanitize-address-use-after-scope) diff --git a/arch/sim/src/sim/sim_fork.c b/arch/sim/src/sim/sim_fork.c index 74197b8aec7..d2064ed8025 100644 --- a/arch/sim/src/sim/sim_fork.c +++ b/arch/sim/src/sim/sim_fork.c @@ -87,7 +87,7 @@ * ****************************************************************************/ -#ifdef CONFIG_SIM_ASAN +#if defined(CONFIG_SIM_ASAN) || defined(CONFIG_MM_KASAN_SIM) nosanitize_address #endif pid_t sim_fork(bool vfork, const xcpt_reg_t *context) diff --git a/arch/sim/src/sim/sim_head.c b/arch/sim/src/sim/sim_head.c index 58b4ff18010..e41cca08222 100644 --- a/arch/sim/src/sim/sim_head.c +++ b/arch/sim/src/sim/sim_head.c @@ -113,7 +113,7 @@ static void allsyms_relocate(void) * ****************************************************************************/ -#ifdef CONFIG_SIM_ASAN +#if defined(CONFIG_SIM_ASAN) || defined(CONFIG_MM_KASAN_SIM) noprofile_function const char *__asan_default_options(void) { return "abort_on_error=1" diff --git a/arch/sim/src/sim/sim_initialstate.c b/arch/sim/src/sim/sim_initialstate.c index 73175c4c0a9..d0f69ba8c25 100644 --- a/arch/sim/src/sim/sim_initialstate.c +++ b/arch/sim/src/sim/sim_initialstate.c @@ -28,7 +28,7 @@ #include <stdint.h> #include <string.h> -#ifdef CONFIG_SIM_ASAN +#if defined(CONFIG_SIM_ASAN) || defined(CONFIG_MM_KASAN_SIM) #include <sanitizer/asan_interface.h> #endif @@ -119,7 +119,7 @@ void up_initial_state(struct tcb_s *tcb) tcb->xcp.regs[JB_PC] = (xcpt_reg_t)pre_start; -#ifdef CONFIG_SIM_ASAN +#if defined(CONFIG_SIM_ASAN) || defined(CONFIG_MM_KASAN_SIM) __asan_unpoison_memory_region(tcb->stack_alloc_ptr, tcb->adj_stack_size); #endif } diff --git a/boards/sim/sim/sim/scripts/Make.defs b/boards/sim/sim/sim/scripts/Make.defs index 5f2ed0436e4..6fb7ae1e8cb 100644 --- a/boards/sim/sim/sim/scripts/Make.defs +++ b/boards/sim/sim/sim/scripts/Make.defs @@ -86,7 +86,7 @@ ifneq ($(CONFIG_PROFILE_ALL)$(CONFIG_SIM_PROFILE),) ARCHOPTIMIZATION += -pg endif -ifeq ($(CONFIG_SIM_ASAN),y) +ifneq ($(CONFIG_SIM_ASAN)$(CONFIG_MM_KASAN_SIM),) ARCHOPTIMIZATION += -fsanitize=address -fsanitize-address-use-after-scope ARCHOPTIMIZATION += -fsanitize=pointer-compare -fsanitize=pointer-subtract else ifeq ($(CONFIG_MM_KASAN_INSTRUMENT_ALL),y) diff --git a/mm/kasan/CMakeLists.txt b/mm/kasan/CMakeLists.txt index d802a55b1a4..14c634a2461 100644 --- a/mm/kasan/CMakeLists.txt +++ b/mm/kasan/CMakeLists.txt @@ -24,7 +24,8 @@ set(SRCS hook.c) if(CONFIG_MM_KASAN) list(APPEND FLAGS ${NO_LTO}) list(APPEND FLAGS -fno-builtin) - list(APPEND FLAGS -fno-sanitize=kernel-address) + list(APPEND FLAGS -fno-sanitize=kernel-address -fno-sanitize=address + -fno-sanitize=pointer-compare -fno-sanitize=pointer-subtract) if(NOT "${CONFIG_MM_KASAN_MARK_LOCATION}" STREQUAL "") target_compile_definitions( diff --git a/mm/kasan/Kconfig b/mm/kasan/Kconfig index 94934c6cf21..74c278f4f79 100644 --- a/mm/kasan/Kconfig +++ b/mm/kasan/Kconfig @@ -44,6 +44,12 @@ config MM_KASAN_HW_TAGS ---help--- KAsan based on hardware tags +config MM_KASAN_SIM + bool "KAsan on simulator" + depends on ARCH_SIM && FRAME_POINTER + ---help--- + KASan on simulator that use asan api to protect memory + endchoice config MM_KASAN_INSTRUMENT_ALL diff --git a/mm/kasan/Make.defs b/mm/kasan/Make.defs index 911e77a7925..c38bedb7368 100644 --- a/mm/kasan/Make.defs +++ b/mm/kasan/Make.defs @@ -26,6 +26,7 @@ ifeq ($(CONFIG_MM_KASAN),y) CFLAGS += -fno-lto CFLAGS += -fno-builtin CFLAGS += -fno-sanitize=kernel-address + CFLAGS += -fno-sanitize=address -fno-sanitize=pointer-compare -fno-sanitize=pointer-subtract endif ifneq ($(CONFIG_MM_KASAN_MARK_LOCATION),"") diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index dcac69e5205..3af7037453f 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -43,6 +43,8 @@ # include "sw_tags.c" #elif defined(CONFIG_MM_KASAN_HW_TAGS) # include "hw_tags.c" +#elif defined(CONFIG_MM_KASAN_SIM) +# include "sim_asan.c" #else # define kasan_is_poisoned(addr, size) false #endif diff --git a/mm/kasan/sim_asan.c b/mm/kasan/sim_asan.c new file mode 100644 index 00000000000..ab5db22bce9 --- /dev/null +++ b/mm/kasan/sim_asan.c @@ -0,0 +1,74 @@ +/**************************************************************************** + * mm/kasan/sim_asan.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sanitizer/asan_interface.h> + +#include <nuttx/nuttx.h> +#include <stdint.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +uint8_t kasan_get_tag(FAR const void *addr) +{ + return 0; +} + +FAR void *kasan_set_tag(FAR const void *addr, uint8_t tag) +{ + return (FAR void *)addr; +} + +FAR void *kasan_clear_tag(FAR const void *addr) +{ + return (FAR void *)addr; +} + +void kasan_poison(FAR const void *addr, size_t size) +{ + __asan_poison_memory_region(addr, size); +} + +FAR void *kasan_unpoison(FAR const void *addr, size_t size) +{ + __asan_unpoison_memory_region(addr, size); + return (FAR void *)addr; +} + +bool kasan_bypass(bool state) +{ + return false; +} + +void kasan_register(FAR void *addr, FAR size_t *size) +{ + kasan_poison(addr, *size); +} + +void kasan_unregister(FAR void *addr) +{ +}
