From f172f222beef11fd699a887a305e2bc3c101cf50 Mon Sep 17 00:00:00 2001 From: Bowen Wang Date: Thu, 29 Feb 2024 16:34:53 +0800 Subject: [PATCH] arm64_checkstack: fix crash, optimize the stack color, sync with arm Signed-off-by: Bowen Wang Signed-off-by: ligd --- arch/arm64/src/common/arm64_checkstack.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/arch/arm64/src/common/arm64_checkstack.c b/arch/arm64/src/common/arm64_checkstack.c index c9cc227c928..f3f7824c119 100644 --- a/arch/arm64/src/common/arm64_checkstack.c +++ b/arch/arm64/src/common/arm64_checkstack.c @@ -160,12 +160,24 @@ void arm64_stack_color(void *stackbase, size_t nbytes) uintptr_t end; size_t nwords; uint32_t *ptr; + uintptr_t sp; /* Take extra care that we do not write outside the stack boundaries */ - start = STACK_ALIGN_UP((uintptr_t)stackbase); - end = nbytes ? STACK_ALIGN_DOWN((uintptr_t)stackbase + nbytes) : - up_getsp(); /* 0: colorize the running stack */ + start = (uintptr_t)STACK_ALIGN_UP((uintptr_t)stackbase); + + if (nbytes == 0) /* 0: colorize the running stack */ + { + end = up_getsp(); + if (end > (uintptr_t)&sp) + { + end = (uintptr_t)&sp; + } + } + else + { + end = (uintptr_t)stackbase + nbytes; + } /* Get the adjusted size based on the top and bottom of the stack */