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 44b39142c3347e7dd60b0694d45263fc074c5168
Author: wangmingrong1 <[email protected]>
AuthorDate: Mon Nov 2 18:59:58 2026 +0800

    mm/kasan: fix crash in sw_tags due to rand() TLS dependency during early 
boot
    
    kasan_random_tag() calls rand() which accesses per-task seed via TLS
    (tl_task->ta_rand_seed). During early heap initialization,
    kasan_register() -> kasan_unpoison() -> kasan_random_tag() is called
    before the task/TLS infrastructure is ready, causing a NULL pointer
    dereference at offset 0x1b8 (data abort, level 2 translation fault).
    Replace rand() with a self-contained LCG using a local static seed,
    removing the TLS dependency entirely.
    
    Signed-off-by: wangmingrong1 <[email protected]>
---
 mm/kasan/sw_tags.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/mm/kasan/sw_tags.c b/mm/kasan/sw_tags.c
index b6c3202fab2..bdb096061df 100644
--- a/mm/kasan/sw_tags.c
+++ b/mm/kasan/sw_tags.c
@@ -36,8 +36,6 @@
 
 #define KASAN_TAG_SHIFT 56
 
-#define kasan_random_tag() (1 + rand() % ((1 << (64 - KASAN_TAG_SHIFT)) - 2))
-
 #define KASAN_SHADOW_SCALE (sizeof(uintptr_t))
 
 #define KASAN_SHADOW_SIZE(size) \
@@ -68,6 +66,18 @@ static spinlock_t g_lock;
  * Private Functions
  ****************************************************************************/
 
+static inline uint8_t kasan_random_tag(void)
+{
+  /* Use a simple LCG that does not depend on TLS (rand() requires
+   * per-task seed via tl_task which is NULL during early boot).
+   */
+
+  static unsigned long seed = 1;
+
+  seed = seed * 1103515245 + 12345;
+  return 1 + (seed >> 16) % ((1 << (64 - KASAN_TAG_SHIFT)) - 2);
+}
+
 static inline_function FAR uint8_t *
 kasan_mem_to_shadow(FAR const void *ptr, size_t size)
 {

Reply via email to