pussuw opened a new issue #5811:
URL: https://github.com/apache/incubator-nuttx/issues/5811


   This happens with CONFIG_BUILD_KERNEL=y if the initial heap size is not 
large enough to hold the initial stack.
   
   The issue is with the tcb/group initialization order when loading a task 
with binfmt_execmodule / exec_module.
   
   There was a patch to fix this, but the fix only works if the initial heap is 
large enough to contain the task's stack + tls structure:
   
https://github.com/apache/incubator-nuttx/commit/a44a0a08cd9d899a2ee601c80d9c36fa3a51123a
   
   If the initial heap is not large enough, sbrk is called and the system 
crashes.
   
   Details on what happens in order:
   
   1. An address environment was created when the binary was loaded. That is 
instantiated on line 156: ret = up_addrenv_select(&binp->addrenv, &oldenv); 
this is fine
   2. The initial heap is initialized by line 165: umm_initialize((FAR void 
*)CONFIG_ARCH_HEAP_VBASE, binp->addrenv.heapsize);
   3. nxtask_init() allocates memory for the group structure.
   4. nxtask_init() allocates the task stack on line 124  ret = 
up_create_stack(&tcb->cmn, up_tls_size() + stack_size,  ttype);
   5. The allocation is done from user heap via kumm_malloc
   6. **If up_tls_size() + stack_size > binp->addrenv.heapsize** this will 
fail, as kumm_malloc will eventually call sbrk which in turn calls pgalloc()
   7. pgalloc has the following test DEBUGASSERT((group->tg_flags & 
GROUP_FLAG_ADDRENV) != 0); which fill fail, because 
group->tg_flags.GROUP_FLAG_ADDRENV is not yet set
   8. The flag GROUP_FLAG_ADDRENV is set later in binfmt_execmodule line 238: 
tcb->cmn.group->tg_flags |= GROUP_FLAG_ADDRENV; but this is too late.
   
   The initialization order has to be changed / modified to mark the task's 
address environment as valid sooner, or define a new flag to tell sbkr/pgalloc 
to do the allocation without testing for that flag.
   
   I can fix this but it requires modifying the existing kernel code and I need 
a suggestion how to do this correctly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to