In kernel mode, we have to duplicate the callers argv[] buffer when exec'ing new tasks. When the argv[] buffer is needed, the caller's address environment will not longer be in place
This commit is contained in:
parent
2eb71d5a35
commit
62880f60c4
@ -58,6 +58,89 @@
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: binfmt_copyargv
|
||||
*
|
||||
* Description:
|
||||
* In the kernel build, the argv list will likely lie in the caller's
|
||||
* address environment and, hence, by inaccessible when we swith to the
|
||||
* address environment of the new process address environment. So we
|
||||
* do not have any real option other than to copy the callers argv[] list.
|
||||
*
|
||||
* Input Parameter:
|
||||
* bin - Load structure
|
||||
* argv - Argument list
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on sucess; a negater erro value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static inline int binfmt_copyargv(FAR struct binary_s *bin, FAR char * const *argv)
|
||||
{
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
FAR char *ptr;
|
||||
size_t argsize;
|
||||
int i;
|
||||
|
||||
/* Get the size of the argument list */
|
||||
|
||||
bin->argbuffer = (FAR char *)NULL;
|
||||
i = 0;
|
||||
|
||||
if (argv)
|
||||
{
|
||||
argsize = 0;
|
||||
for (i = 0; i < CONFIG_MAX_TASK_ARGS && argv[i]; i++)
|
||||
{
|
||||
argsize += (strlen(argv[i]) + 1);
|
||||
}
|
||||
|
||||
bvdbg("args=%d argsize=%lu\n", i, (unsigned long)argsize);
|
||||
|
||||
/* Allocate a temporary argument buffer */
|
||||
|
||||
i = 0;
|
||||
|
||||
if (argsize > 0)
|
||||
{
|
||||
bin->argbuffer = (FAR char *)kmm_malloc(argsize);
|
||||
if (!bin->argbuffer)
|
||||
{
|
||||
bdbg("ERROR: Failed to allocate the argument buffer\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* Copy the argv list */
|
||||
|
||||
ptr = bin->argbuffer;
|
||||
for (; i < CONFIG_MAX_TASK_ARGS && argv[i]; i++)
|
||||
{
|
||||
bin->argv[i] = ptr;
|
||||
argsize = strlen(argv[i]) + 1;
|
||||
memcpy(ptr, argv[i], argsize);
|
||||
ptr += argsize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Nullify the remainder of the list */
|
||||
|
||||
for (; i <= CONFIG_MAX_TASK_ARGS; i++)
|
||||
{
|
||||
bin->argv[i] = NULL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
|
||||
#else
|
||||
/* Just save the caller's argv pointer */
|
||||
|
||||
bin->argv = argv;
|
||||
return OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
@ -115,19 +198,30 @@ int exec(FAR const char *filename, FAR char * const *argv,
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Load the module into memory */
|
||||
/* Initialize the binary structure */
|
||||
|
||||
bin->filename = filename;
|
||||
bin->argv = argv;
|
||||
bin->exports = exports;
|
||||
bin->nexports = nexports;
|
||||
|
||||
/* Copy the argv[] list */
|
||||
|
||||
ret = binfmt_copyargv(bin, argv);
|
||||
if (ret < 0)
|
||||
{
|
||||
err = -ret;
|
||||
bdbg("ERROR: Failed to copy argv[]: %d\n", err);
|
||||
goto errout_with_bin;
|
||||
}
|
||||
|
||||
/* Load the module into memory */
|
||||
|
||||
ret = load_module(bin);
|
||||
if (ret < 0)
|
||||
{
|
||||
err = get_errno();
|
||||
bdbg("ERROR: Failed to load program '%s': %d\n", filename, err);
|
||||
goto errout_with_bin;
|
||||
goto errout_with_argv;
|
||||
}
|
||||
|
||||
/* Disable pre-emption so that the executed module does
|
||||
@ -164,6 +258,8 @@ int exec(FAR const char *filename, FAR char * const *argv,
|
||||
errout_with_lock:
|
||||
sched_unlock();
|
||||
unload_module(bin);
|
||||
errout_with_argv:
|
||||
binfmt_freeargv(bin);
|
||||
errout_with_bin:
|
||||
kmm_free(bin);
|
||||
errout:
|
||||
|
@ -192,7 +192,7 @@ int exec_module(FAR const struct binary_s *binp)
|
||||
if (!stack)
|
||||
{
|
||||
err = ENOMEM;
|
||||
goto errout_with_kstack;
|
||||
goto errout_with_addrenv;
|
||||
}
|
||||
|
||||
/* Initialize the task */
|
||||
@ -203,9 +203,13 @@ int exec_module(FAR const struct binary_s *binp)
|
||||
{
|
||||
err = get_errno();
|
||||
bdbg("task_init() failed: %d\n", err);
|
||||
goto errout_with_kstack;
|
||||
goto errout_with_addrenv;
|
||||
}
|
||||
|
||||
/* We can free the argument buffer now */
|
||||
|
||||
binfmt_freeargv(binp);
|
||||
|
||||
/* Note that tcb->flags are not modified. 0=normal task */
|
||||
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
||||
|
||||
@ -217,7 +221,7 @@ int exec_module(FAR const struct binary_s *binp)
|
||||
{
|
||||
bdbg("ERROR: up_addrenv_select() failed: %d\n", ret);
|
||||
err = -ret;
|
||||
goto errout_with_addrenv;
|
||||
goto errout_with_tcbinit;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -292,12 +296,7 @@ errout_with_tcbinit:
|
||||
kumm_free(stack);
|
||||
goto errout;
|
||||
|
||||
errout_with_kstack:
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
(void)up_addrenv_kstackfree(tcb);
|
||||
|
||||
errout_with_addrenv:
|
||||
#endif
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
(void)up_addrenv_restore(&oldenv);
|
||||
|
||||
|
@ -55,7 +55,8 @@
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
#define EXTERN extern "C"
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
@ -71,14 +72,46 @@ EXTERN FAR struct binfmt_s *g_binfmts;
|
||||
* Public Function Prototypes
|
||||
***********************************************************************/
|
||||
|
||||
/* Dump the contents of struct binary_s */
|
||||
/****************************************************************************
|
||||
* Name: dump_module
|
||||
*
|
||||
* Description:
|
||||
* Dump the contents of struct binary_s.
|
||||
*
|
||||
* Input Parameter:
|
||||
* bin - Load structure
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negater errno value on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
|
||||
EXTERN int dump_module(FAR const struct binary_s *bin);
|
||||
int dump_module(FAR const struct binary_s *bin);
|
||||
#else
|
||||
# define dump_module(bin)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: binfmt_freeargv
|
||||
*
|
||||
* Description:
|
||||
* Release the copied argv[] list.
|
||||
*
|
||||
* Input Parameter:
|
||||
* bin - Load structure
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
void binfmt_freeargv(FAR struct binary_s *bin);
|
||||
#else
|
||||
# define binfmt_freeargv(bin)
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
@ -179,6 +179,10 @@ int unload_module(FAR struct binary_s *binp)
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Free any allocated argv[] strings */
|
||||
|
||||
binfmt_freeargv(binp);
|
||||
|
||||
/* Unmap mapped address spaces */
|
||||
|
||||
if (binp->mapped)
|
||||
@ -207,5 +211,32 @@ int unload_module(FAR struct binary_s *binp)
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: binfmt_freeargv
|
||||
*
|
||||
* Description:
|
||||
* Release the copied argv[] list.
|
||||
*
|
||||
* Input Parameter:
|
||||
* binp - Load structure
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
void binfmt_freeargv(FAR struct binary_s *binp)
|
||||
{
|
||||
if (binp->argbuffer)
|
||||
{
|
||||
/* Free the argument buffer */
|
||||
|
||||
kmm_free(binp->argbuffer);
|
||||
binp->argbuffer = NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_BINFMT_DISABLE */
|
||||
|
||||
|
@ -97,7 +97,12 @@ struct binary_s
|
||||
/* Information provided to the loader to load and bind a module */
|
||||
|
||||
FAR const char *filename; /* Full path to the binary to be loaded (See NOTE 1 above) */
|
||||
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
||||
FAR char *argbuffer; /* Allocated argument list */
|
||||
FAR char *argv[CONFIG_MAX_TASK_ARGS+1]; /* Copy of argument list */
|
||||
#else
|
||||
FAR char * const *argv; /* Argument list */
|
||||
#endif
|
||||
FAR const struct symtab_s *exports; /* Table of exported symbols */
|
||||
int nexports; /* The number of symbols in exports[] */
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user