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 74067226ee3ed30b86e6485d7ed61bfc3311f840
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 21:44:36 2026 +0100

    binfmt/exec_module: Release the address environment on an early error.
    
    load_module() makes the address environment of the program, and
    exec_module() gives its reference to the new task with addrenv_attach().
    On an error before that (the copies of the lists, nxtask_init(): for
    example a file action of posix_spawn() that fails), nothing released
    the reference: unload_module() does not release the address
    environment.  So each such error lost the pages of the program: its
    page tables, its segments and its heap.
    
    Now exec_module() sets binp->addrenv to NULL when the task has the
    reference, and exec_internal() releases a binp->addrenv that is still
    there after an error of exec_module(), next to unload_module().  After
    addrenv_attach(), nxtask_uninit() releases it as before.  Nothing uses
    binp->addrenv after addrenv_attach().
    
    This covers every build with CONFIG_ARCH_ADDRENV: the kernel builds,
    and also a flat or protected build with it (for example sama5d4-ek:elf
    and fvp-armv8r:pnsh).  Builds without CONFIG_ARCH_ADDRENV do not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 binfmt/binfmt_exec.c          | 6 ++++++
 binfmt/binfmt_execmodule.c    | 4 ++++
 include/nuttx/binfmt/binfmt.h | 2 ++
 3 files changed, 12 insertions(+)

diff --git a/binfmt/binfmt_exec.c b/binfmt/binfmt_exec.c
index 1c1e4b40523..46f91d6c08f 100644
--- a/binfmt/binfmt_exec.c
+++ b/binfmt/binfmt_exec.c
@@ -32,6 +32,7 @@
 #include <nuttx/debug.h>
 #include <errno.h>
 
+#include <nuttx/addrenv.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/sched.h>
 #include <nuttx/binfmt/binfmt.h>
@@ -192,6 +193,11 @@ static int exec_internal(FAR const char *filename,
 
 errout_with_lock:
   unload_module(bin);
+#ifdef CONFIG_ARCH_ADDRENV
+  /* No task got the address environment of the program: release it */
+
+  addrenv_drop(bin->addrenv, false);
+#endif
 errout_with_bin:
 #ifdef CONFIG_BINFMT_LOADABLE
   kmm_free(bin);
diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c
index ca043a4ce70..265b5093a2c 100644
--- a/binfmt/binfmt_execmodule.c
+++ b/binfmt/binfmt_execmodule.c
@@ -367,6 +367,10 @@ int exec_module(FAR struct binary_s *binp,
       berr("ERROR: addrenv_attach() failed: %d\n", ret);
       goto errout_with_tcbinit;
     }
+
+  /* The task has the address environment now: the caller does not drop it */
+
+  binp->addrenv = NULL;
 #endif
 
 #ifdef CONFIG_SCHED_USER_IDENTITY
diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h
index 0b8b6a63079..bcb4df71ec9 100644
--- a/include/nuttx/binfmt/binfmt.h
+++ b/include/nuttx/binfmt/binfmt.h
@@ -243,6 +243,8 @@ int unload_module(FAR struct binary_s *bin);
  *
  * Description:
  *   Execute a module that has been loaded into memory by load_module().
+ *   The new task takes the address environment of the module.  On failure
+ *   the caller releases a binp->addrenv that is not NULL.
  *
  * Returned Value:
  *   This is a NuttX internal function so it follows the convention that

Reply via email to