mystikos icon indicating copy to clipboard operation
mystikos copied to clipboard

Pseudo fork: fix up pointers to stack variables

Open jxyang opened this issue 3 years ago • 0 comments

When we copy stack from the parent, we don't fix up the pointers on the stack that point to another stack variable. For example:

void fork_child(char* path, char* child_argv[]) 
{
    if (fork() == 0)
    {
      sleep(1);
       printf("*** inside child.. reading argv[0]: %s\n", child_argv[0]);
       exit(2);
    }
}

int main()
{
    char* path = "/bin/fork_child";
    char* argv[] = {path, NULL};
    fork_child(path, argv);

    child_argv[0] = NULL;
    return 0;
}

Here child_argv is a pointer parameter to array argv which is also on the stack. When I run the program on Linux, the output prints: *** inside child.. reading argv[0]: /bin/fork_child. But when I run with Mystikos, the output reads: *** inside child.. reading argv[0]: (null).

I think that's because the point child_argv in the child stills points to the array on parent's stack, even though we copied the array over.

jxyang avatar Jun 28 '21 22:06 jxyang