You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/08/10 15:30:27 UTC

[nuttx] branch master updated: sim: Fix sim stack smashing problem

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d6e1aea3b sim: Fix sim stack smashing problem
6d6e1aea3b is described below

commit 6d6e1aea3b13b56f8f815355951287290f1e274d
Author: chenxiaoyi <ch...@xiaomi.com>
AuthorDate: Mon Apr 24 12:48:48 2023 +0800

    sim: Fix sim stack smashing problem
    
    The reason is that the new created task receives signal while
    it has not entered its stack frame.
    
    Signed-off-by: chenxiaoyi <ch...@xiaomi.com>
---
 arch/sim/src/sim/sim_initialstate.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/arch/sim/src/sim/sim_initialstate.c b/arch/sim/src/sim/sim_initialstate.c
index 6b3f6968df..76d967d28f 100644
--- a/arch/sim/src/sim/sim_initialstate.c
+++ b/arch/sim/src/sim/sim_initialstate.c
@@ -32,8 +32,26 @@
 
 #include <nuttx/arch.h>
 
+#include "sched/sched.h"
 #include "sim_internal.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void pre_start(void)
+{
+  struct tcb_s *tcb = this_task();
+
+  /* Enable signal delivery */
+
+  up_irq_restore(0);
+
+  /* Then call the real start function */
+
+  tcb->start();
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -93,7 +111,11 @@ void up_initial_state(struct tcb_s *tcb)
 #endif
                                    + tcb->adj_stack_size;
 
-  tcb->xcp.regs[JB_PC] = (xcpt_reg_t)tcb->start;
+  /* Mask the interrupt until switching to the new task */
+
+  memset(&tcb->xcp.regs[JB_FLAG], 0xff, sizeof(xcpt_reg_t) * 2);
+
+  tcb->xcp.regs[JB_PC] = (xcpt_reg_t)pre_start;
 
 #ifdef CONFIG_SIM_ASAN
   __asan_unpoison_memory_region(tcb->stack_alloc_ptr, tcb->adj_stack_size);