# The child process does not start with syscall(SYS\_clone3, ...) + CLONE\_VM

**URL:** <https://forum.devtalk.com/t/the-child-process-does-not-start-with-syscall-sys-clone3-clone-vm/168119>\
**Category:** Linux Questions/Help\
**Tags:** c-lang\
**Created:** [29 August 2024 15:18 UTC](https://forum.devtalk.com/t/the-child-process-does-not-start-with-syscall-sys-clone3-clone-vm/168119 "2024-08-29T15:18:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![0m3](https://forum.devtalk.com/user_avatar/forum.devtalk.com/0m3/32/6472_2.png) [@0m3](https://forum.devtalk.com/u/0m3)\
**Post date:** [29 August 2024 15:18 UTC](https://forum.devtalk.com/t/the-child-process-does-not-start-with-syscall-sys-clone3-clone-vm/168119/1 "2024-08-29T15:18:39Z")

</div>

Hello, everyone.

It is necessary that the calling process and the child process are in the same memory space.  
Therefore, I use the CLONE\_VM flag.  
But the child process does not start.  
It looks like something is wrong with the allocation of memory for the stack.  
Could you please explain the reason?

```c
#define _DEFAULT_SOURCE /* syscall() */
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64 /* getrlimit() */

#include <sched.h> /* CLONE_* constants */
#include <linux/sched.h> /* struct clone_args */
#include <sys/syscall.h> /* SYS_* constants */
#include <unistd.h>

#include <stdint.h> /* uintptr_t */

#include <stdio.h>
#include <stdlib.h>
#include <err.h> /* err() */
#include <string.h>
#include <unistd.h>

#include <sys/resource.h>
#include <sys/mman.h>
#include <signal.h>

void spawn(void)
{
    struct rlimit rlim;
    struct clone_args cl_args;
    __u64 stack_size;
    __u64 *stackBot;
    __u64 *stackTop;
    pid_t pid;

    memset(&cl_args, 0, sizeof(cl_args));

    if (getrlimit(RLIMIT_STACK, &rlim) == -1) {
        err(EXIT_FAILURE, "getrlimit");
    }
    stack_size = rlim.rlim_cur;

    stackBot = (__u64 *)
        mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN |
             MAP_STACK, -1, 0);
    if (stackBot == MAP_FAILED) {
        err(EXIT_FAILURE, "mmap");
    }

    stackTop = stackBot + (stack_size / sizeof(__u64));

    cl_args.flags = CLONE_FILES | CLONE_IO | CLONE_VM;
    cl_args.exit_signal = SIGCHLD;
    cl_args.stack = (__u64) (uintptr_t) stackTop;
    cl_args.stack_size = stack_size;

    pid = syscall(SYS_clone3, &cl_args, sizeof(cl_args));
    switch(pid) {
        case -1:
            munmap(stackBot, stack_size);
            err(EXIT_FAILURE, "syscall");
        case 0: /* Child */
            printf("Hello from child!\n");
            munmap(stackBot, stack_size);
            break;
        default: /* Parent */
            printf("Hello from parent!\n");
            break;
    }
}

int
main(int argc, char *argv[])
{
    spawn();

    printf("Before last while\n");
    while(1) {
    }

    return EXIT_SUCCESS;
}

```

---

<div class="post-metadata">

**Author:** ![0m3](https://forum.devtalk.com/user_avatar/forum.devtalk.com/0m3/32/6472_2.png) [@0m3](https://forum.devtalk.com/u/0m3)\
**Post date:** [31 August 2024 20:55 UTC](https://forum.devtalk.com/t/the-child-process-does-not-start-with-syscall-sys-clone3-clone-vm/168119/2 "2024-08-31T20:55:46Z")

</div>

I rewrote it, it still doesn’t work.

```auto
#define _DEFAULT_SOURCE /* syscall() */
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64 /* getrlimit() */

#include <sched.h> /* CLONE_* constants */
#include <linux/sched.h> /* struct clone_args */
#include <sys/syscall.h> /* SYS_* constants */
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> /* uintptr_t */
#include <err.h> /* err() */

#include <sys/resource.h>
#include <sys/mman.h>
#include <signal.h>

void spawn(void)
{
    struct rlimit rlim;
    struct clone_args cl_args = {0};
    uint64_t stack_size;
    void *stackBot;
    void *stackTop;
    pid_t pid;

    if (getrlimit(RLIMIT_STACK, &rlim) == -1)
        err(EXIT_FAILURE, "getrlimit");
    stack_size = rlim.rlim_cur;

    stackBot = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN |
                    MAP_STACK, -1, 0);
    if (stackBot == MAP_FAILED)
        err(EXIT_FAILURE, "mmap");

    long count = stack_size / sizeof(uint64_t);
    stackTop = (uint64_t *)stackBot + count - 1;

    printf("stack_size: %lu\n", stack_size);
    printf("stack_size/2: %lu\n", stack_size / 2);
    printf("stack_size/4: %lu\n", stack_size / 4);
    printf("stack_size/8: %lu\n", stack_size / 8);
    printf("stack_size/16: %lu\n", stack_size / 16);
    printf("stack_size/4096: %lu\n", stack_size / 4096); /* PAGESIZE */

    printf("stackBotAddr: %p\n", (void *)stackBot);
    printf("stackTopAddr: %p\n", (void *)stackTop);

    printf("stackBotVal: %lu\n", *(uint64_t *)(uintptr_t)stackBot);
    printf("stackTopVal: %lu\n", *(uint64_t *)(uintptr_t)stackTop);

    printf("stackTopAddr-stackBotAddr: %lu\n", (void *)stackTop - stackBot);

    printf("stackBotCast: %lu\n", (uint64_t)(uintptr_t)stackBot);
    printf("stackTopCast: %lu\n", (uint64_t)(uintptr_t)stackTop);

    cl_args.flags = CLONE_FILES | CLONE_IO | CLONE_VM;
    cl_args.exit_signal = SIGCHLD;
    cl_args.stack = (uint64_t)(uintptr_t)stackBot;
    cl_args.stack_size = (void *)stackTop - stackBot;

    pid = syscall(SYS_clone3, &cl_args, sizeof(cl_args));
    switch(pid) {
        case -1:
            munmap(stackBot, stack_size);
            err(EXIT_FAILURE, "syscall");
        case 0: /* Child */
            printf("Hello from child. Child's pid: %d\n", getpid());
            munmap(stackBot, stack_size);
            break;
        default: /* Parent */
            printf("Hello from parent. Parent's pid: %d\n", getpid());
            break;
    }
}

int
main(int argc, char *argv[])
{
    spawn();

    printf("Before last while\n");
    while(1) {
    }

    return EXIT_SUCCESS;
}

```

---

<div class="post-metadata">

**Author:** ![0m3](https://forum.devtalk.com/user_avatar/forum.devtalk.com/0m3/32/6472_2.png) [@0m3](https://forum.devtalk.com/u/0m3)\
**Post date:** [20 November 2024 21:16 UTC](https://forum.devtalk.com/t/the-child-process-does-not-start-with-syscall-sys-clone3-clone-vm/168119/3 "2024-11-20T21:16:53Z")

</div>

Hello, everyone.

The solution is here [The child process does not start with syscall(SYS\_clone3, …) + CLONE\_VM](https://stackoverflow.com/questions/78936025/the-child-process-does-not-start-with-syscallsys-clone3-clone-vm/).

BR, Denis
