This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit edd1269253b1869785351c9dedc9c348e751a47f Author: makejian <[email protected]> AuthorDate: Thu Jul 2 10:25:35 2026 +0800 mm/kasan: expose compiler-rt __asan_(un)poison_memory_region Code written against user-space AddressSanitizer (for example the mbedtls test framework) includes <sanitizer/asan_interface.h> and calls __asan_poison_memory_region()/__asan_unpoison_memory_region() to enforce memory access policies. On a KASAN target NuttX does not link the compiler-rt user-space ASan runtime, so these standard entry points were undefined and such code failed to link. Provide them from mm/kasan alongside the other compiler-rt __asan_* entry points already implemented in hook.c, bridging to the in-kernel KASAN shadow via kasan_poison()/kasan_unpoison(). Add a matching public header include/sanitizer/asan_interface.h so the standard include path resolves on a target build. They are excluded for the SIM mode, where the host compiler-rt runtime already defines these symbols and kasan_poison() forwards to them. Signed-off-by: makejian <[email protected]> --- include/sanitizer/asan_interface.h | 80 ++++++++++++++++++++++++++++++++++++++ mm/kasan/hook.c | 51 ++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/include/sanitizer/asan_interface.h b/include/sanitizer/asan_interface.h new file mode 100644 index 00000000000..3bec781da5d --- /dev/null +++ b/include/sanitizer/asan_interface.h @@ -0,0 +1,80 @@ +/**************************************************************************** + * include/sanitizer/asan_interface.h + * + * 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. + * + ****************************************************************************/ + +/* AddressSanitizer public interface. + * + * This is the same header the host compiler-rt runtime provides so that code + * written against user-space ASan (for example the mbedtls test framework) + * can include <sanitizer/asan_interface.h> and call + * __asan_(un)poison_memory_region() unchanged on a NuttX target. NuttX does + * not link the compiler-rt user-space ASan runtime; on a KASAN target these + * entry points are provided by mm/kasan and bridge to the in-kernel KASAN + * shadow. The signatures match the upstream compiler-rt header. + */ + +#ifndef __INCLUDE_SANITIZER_ASAN_INTERFACE_H +#define __INCLUDE_SANITIZER_ASAN_INTERFACE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/compiler.h> + +#include <stddef.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: __asan_poison_memory_region + * + * Description: + * Mark the memory region [addr, addr+size) as unaddressable, so that any + * instrumented access to it triggers a KASAN report until unpoisoned. + * + ****************************************************************************/ + +void __asan_poison_memory_region(FAR void const volatile *addr, size_t size); + +/**************************************************************************** + * Name: __asan_unpoison_memory_region + * + * Description: + * Mark the memory region [addr, addr+size) as addressable again. + * + ****************************************************************************/ + +void __asan_unpoison_memory_region(FAR void const volatile *addr, + size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_SANITIZER_ASAN_INTERFACE_H */ diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index 7230a557a73..d069ebe5ca5 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -25,6 +25,8 @@ #include <nuttx/mm/kasan.h> #include <nuttx/compiler.h> +#include <sanitizer/asan_interface.h> + #include <assert.h> #include <nuttx/debug.h> #include <execinfo.h> @@ -378,6 +380,55 @@ void __asan_unregister_globals(void *ptr, size_t size) /* Shut up compiler complaints */ } +/**************************************************************************** + * Name: __asan_poison_memory_region + * + * Description: + * Compiler-rt AddressSanitizer public interface. User-space code built for + * ASan (for example the mbedtls test framework) includes + * <sanitizer/asan_interface.h> and calls this to mark a region as + * unaddressable. NuttX has no compiler-rt user-space ASan runtime, so on a + * KASAN target this bridges the standard entry point to the in-kernel + * KASAN shadow. Regions not registered with KASAN are ignored by + * kasan_poison(). + * + * Input Parameters: + * addr - range start address + * size - range size + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void __asan_poison_memory_region(FAR void const volatile *addr, size_t size) +{ + kasan_poison((FAR const void *)addr, size); +} + +/**************************************************************************** + * Name: __asan_unpoison_memory_region + * + * Description: + * Compiler-rt AddressSanitizer public interface, the counterpart of + * __asan_poison_memory_region(). Marks a region as addressable again by + * bridging to the in-kernel KASAN shadow. + * + * Input Parameters: + * addr - range start address + * size - range size + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void __asan_unpoison_memory_region(FAR void const volatile *addr, + size_t size) +{ + kasan_unpoison((FAR const void *)addr, size); +} + void __sanitizer_annotate_contiguous_container(FAR const void *beg, FAR const void *end, FAR const void *old_mid,
