Skip to content

Commit

Permalink
hydra: use dynamic array for exec arg list
Browse files Browse the repository at this point in the history
It is unsafe to use arbitrary limit -- HYD_NUM_TMP_STRINGS -- to static
allocate exec arg list. Use dynamic array instead.
  • Loading branch information
hzhou committed Sep 19, 2023
1 parent 20f4183 commit 0f68836
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/pm/hydra/lib/hydra.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ struct HYD_env_global {

/* Executable information */
struct HYD_exec {
char *exec[HYD_NUM_TMP_STRINGS];
char **exec;
int exec_len, exec_size;
char *wdir;

int proc_count;
Expand Down Expand Up @@ -476,6 +477,7 @@ HYD_status HYDU_alloc_node(struct HYD_node **node);
void HYDU_free_node_list(struct HYD_node *node_list);
void HYDU_free_proxy_list(struct HYD_proxy *proxy_list, int count);
HYD_status HYDU_alloc_exec(struct HYD_exec **exec);
HYD_status HYDU_exec_add_arg(struct HYD_exec *exec, const char *arg);
void HYDU_free_exec_list(struct HYD_exec *exec_list);
HYD_status HYDU_create_proxy_list_singleton(struct HYD_node *node, int pgid,
int *proxy_count_p, struct HYD_proxy **proxy_list_p);
Expand Down
53 changes: 45 additions & 8 deletions src/pm/hydra/lib/utils/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ HYD_status HYDU_alloc_exec(struct HYD_exec **exec)
HYDU_FUNC_ENTER();

HYDU_MALLOC_OR_JUMP(*exec, struct HYD_exec *, sizeof(struct HYD_exec), status);
(*exec)->exec[0] = NULL;
(*exec)->exec = NULL;
(*exec)->exec_len = 0;
(*exec)->exec_size = 0;
(*exec)->wdir = NULL;
(*exec)->proc_count = -1;
(*exec)->env_prop = NULL;
Expand All @@ -171,6 +173,33 @@ HYD_status HYDU_alloc_exec(struct HYD_exec **exec)
goto fn_exit;
}

HYD_status HYDU_exec_add_arg(struct HYD_exec *exec, const char *arg)
{
HYD_status status = HYD_SUCCESS;

if (exec->exec_size == 0) {
exec->exec_size = 10;
exec->exec = MPL_malloc(sizeof(char *) * exec->exec_size, MPL_MEM_OTHER);
if (!exec->exec) {
HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "Can't allocate exec\n");
}
} else if (exec->exec_len + 1 >= exec->exec_size) {
exec->exec_size = exec->exec_size * 2;
exec->exec = MPL_realloc(exec->exec, sizeof(char *) * exec->exec_size, MPL_MEM_OTHER);
if (!exec->exec) {
HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "Can't allocate exec\n");
}
}

exec->exec[exec->exec_len++] = MPL_strdup(arg);
exec->exec[exec->exec_len] = NULL;

fn_exit:
return status;
fn_fail:
goto fn_exit;
}

void HYDU_free_exec_list(struct HYD_exec *exec_list)
{
struct HYD_exec *exec, *run;
Expand All @@ -180,7 +209,13 @@ void HYDU_free_exec_list(struct HYD_exec *exec_list)
exec = exec_list;
while (exec) {
run = exec->next;
HYDU_free_strlist(exec->exec);
if (exec->exec) {
HYDU_free_strlist(exec->exec);
MPL_free(exec->exec);
exec->exec = NULL;
exec->exec_len = 0;
exec->exec_size = 0;
}

MPL_free(exec->wdir);
MPL_free(exec->env_prop);
Expand All @@ -205,9 +240,10 @@ static HYD_status add_exec_to_proxy(struct HYD_exec *exec, struct HYD_proxy *pro
status = HYDU_alloc_exec(&proxy->exec_list);
HYDU_ERR_POP(status, "unable to allocate proxy exec\n");

for (i = 0; exec->exec[i]; i++)
proxy->exec_list->exec[i] = MPL_strdup(exec->exec[i]);
proxy->exec_list->exec[i] = NULL;
for (i = 0; exec->exec[i]; i++) {
status = HYDU_exec_add_arg(proxy->exec_list, exec->exec[i]);
HYDU_ERR_POP(status, "unable to add exec arg\n");
}

proxy->exec_list->wdir = exec->wdir ? MPL_strdup(exec->wdir) : NULL;
proxy->exec_list->proc_count = num_procs;
Expand All @@ -221,9 +257,10 @@ static HYD_status add_exec_to_proxy(struct HYD_exec *exec, struct HYD_proxy *pro

texec = texec->next;

for (i = 0; exec->exec[i]; i++)
texec->exec[i] = MPL_strdup(exec->exec[i]);
texec->exec[i] = NULL;
for (i = 0; exec->exec[i]; i++) {
status = HYDU_exec_add_arg(texec, exec->exec[i]);
HYDU_ERR_POP(status, "unable to add exec arg\n");
}

texec->wdir = exec->wdir ? MPL_strdup(exec->wdir) : NULL;
texec->proc_count = num_procs;
Expand Down
7 changes: 2 additions & 5 deletions src/pm/hydra/mpiexec/get_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,8 @@ static HYD_status parse_args(char **t_argv, int reading_config_file)
break;
}

i = 0;
while (exec->exec[i] != NULL)
i++;
exec->exec[i] = MPL_strdup(*argv);
exec->exec[i + 1] = NULL;
status = HYDU_exec_add_arg(exec, *argv);
HYDU_ERR_POP(status, "unable to add exec arg\n");
} while (++argv && *argv);
} while (1);

Expand Down
2 changes: 1 addition & 1 deletion src/pm/hydra/mpiexec/mpiexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char **argv)

/* Check if any exec argument is missing */
for (exec = HYD_uii_mpx_exec_list; exec; exec = exec->next) {
if (exec->exec[0] == NULL) {
if (exec->exec_len == 0) {
HYDU_ERR_SETANDJUMP(status, HYD_INVALID_PARAM,
"Missing executable. Try -h for usages.\n");
}
Expand Down
7 changes: 4 additions & 3 deletions src/pm/hydra/mpiexec/pmiserv_spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ static HYD_status fill_exec_params(struct HYD_exec *exec, const char *execname,
status = HYDU_correct_wdir(&exec->wdir);
HYDU_ERR_POP(status, "unable to correct wdir\n");

exec->exec[0] = get_exec_path(execname, path);
status = HYDU_exec_add_arg(exec, get_exec_path(execname, path));
HYDU_ERR_POP(status, "unable to add exec arg\n");
for (int i = 0; i < argcnt; i++) {
exec->exec[i + 1] = MPL_strdup(argv[i]);
status = HYDU_exec_add_arg(exec, argv[i]);
HYDU_ERR_POP(status, "unable to add exec arg\n");
}
exec->exec[argcnt + 1] = NULL;

exec->proc_count = nprocs;

Expand Down
4 changes: 2 additions & 2 deletions src/pm/hydra/proxy/pmip_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,9 @@ static HYD_status exec_args_fn(char *arg, char ***argv)
HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "Exec arg not convertible to integer\n");
for (i = 0; i < count; i++) {
(*argv)++;
exec->exec[i] = MPL_strdup(**argv);
status = HYDU_exec_add_arg(exec, **argv);
HYDU_ERR_POP(status, "unable to add exec arg\n");
}
exec->exec[i] = NULL;
(*argv)++;

fn_exit:
Expand Down

0 comments on commit 0f68836

Please sign in to comment.