This is an automated email from the ASF dual-hosted git repository.
acassis 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 28acd505978 sched: Free the task information of a vfork() child in the
parent's heap.
28acd505978 is described below
commit 28acd5059782016f889831a16f579ab6ea76b656
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Oct 29 19:17:32 2026 +0100
sched: Free the task information of a vfork() child in the parent's heap.
In the kernel build a vfork() child is in the address environment of its
parent. group_allocate() and env_dup() put the task information and the
environment of the child in the user heap of the parent, because that is
the heap of the current address environment. When the child ends,
task_uninit_info() returns at once for an unprivileged group, because
the user heap of the group goes away with its address environment. The
heap of a vfork() child is the heap of its parent, and it stays. Thus
each vfork() lost these blocks in the heap of the parent. A shell runs
each command with vfork(), so its heap grew with each command.
Add GROUP_FLAG_VFORK. nxtask_setup_fork() sets it when a vfork() child
joins the address environment of its parent. task_uninit_info() frees
the task information and the environment of a group with this flag.
group_leave() clears the flag when the address environment of the caller
is not the one of the child (another process ends the child): a free
from there goes into the heap of the caller. Then the blocks stay in the
heap of the parent, as before.
Flat and protected builds do not change: only CONFIG_ARCH_ADDRENV sets
the flag, and only CONFIG_BUILD_KERNEL uses it.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
include/nuttx/sched.h | 2 +-
sched/group/group_leave.c | 13 +++++++++++++
sched/task/task_fork.c | 6 ++++++
sched/tls/task_uninitinfo.c | 5 +++--
4 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index ea5a72ccfd6..5bdea2c168b 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -123,7 +123,7 @@
#define GROUP_FLAG_FD_BACKTRACE (1 << 4) /* Bit 4:
Enable FD backtrace for the group */
#define GROUP_FLAG_SECURE_EXEC (1 << 5) /* Bit 5:
Secure (setuid/setgid) executable */
#define GROUP_FLAG_DUMPABLE (1 << 6) /* Bit 6:
Process may be traced / coredumped */
- /* Bit 7:
Available */
+#define GROUP_FLAG_VFORK (1 << 7) /* Bit 7: In
the address environment of the vfork() parent */
/* Values for struct child_status_s ch_flags */
diff --git a/sched/group/group_leave.c b/sched/group/group_leave.c
index 2ea918dc151..2138839385d 100644
--- a/sched/group/group_leave.c
+++ b/sched/group/group_leave.c
@@ -241,6 +241,19 @@ void group_leave(FAR struct tcb_s *tcb)
{
/* Yes.. Release all of the resource held by the task group */
+#ifdef CONFIG_BUILD_KERNEL
+ /* A vfork() child uses the heap of its parent. Free into it
+ * only when it is the current heap: the child ends itself.
+ */
+
+ if ((group->tg_flags & GROUP_FLAG_VFORK) != 0 &&
+ (tcb->addrenv_own == NULL ||
+ tcb->addrenv_own != this_task()->addrenv_curr))
+ {
+ group->tg_flags &= ~GROUP_FLAG_VFORK;
+ }
+#endif
+
group_release(group);
}
diff --git a/sched/task/task_fork.c b/sched/task/task_fork.c
index 50ecf10ab6c..96bc689af17 100644
--- a/sched/task/task_fork.c
+++ b/sched/task/task_fork.c
@@ -282,6 +282,12 @@ FAR struct tcb_s *nxtask_setup_fork(start_t retaddr, bool
vfork)
*/
ret = addrenv_join(parent, child);
+ if (ret >= 0)
+ {
+ /* The child uses the heap of the parent: see group_leave() */
+
+ child->group->tg_flags |= GROUP_FLAG_VFORK;
+ }
}
#ifdef CONFIG_ARCH_HAVE_FORK
else
diff --git a/sched/tls/task_uninitinfo.c b/sched/tls/task_uninitinfo.c
index bd104741109..7752227f020 100644
--- a/sched/tls/task_uninitinfo.c
+++ b/sched/tls/task_uninitinfo.c
@@ -128,10 +128,11 @@ void task_uninit_info(FAR struct task_group_s *group)
#ifdef CONFIG_BUILD_KERNEL
/* The user heap is reclaimed with the address environment. Its lists
* may have been corrupted by the exiting task; neither traversing them
- * nor freeing their user allocations is necessary in the kernel.
+ * nor freeing their user allocations is necessary in the kernel. A
+ * vfork() child uses the heap of its parent, which stays.
*/
- if ((group->tg_flags & GROUP_FLAG_PRIVILEGED) == 0)
+ if ((group->tg_flags & (GROUP_FLAG_PRIVILEGED | GROUP_FLAG_VFORK)) == 0)
{
return;
}