Ammar
I meant with PROT_EXEC flag.
Nils
Yep and with PROT_READ
Ammar
I am working on project similar case with you. Our JIT-ed code needs to be something like that.
Ammar
Yep and with PROT_READ
PROT_WRITE is more relevant though.
Ammar
Yes, to load the code to mapped memory.
Ammar
Oh.
Nils
Yes, to load the code to mapped memory.
I load the mapped memory from a file
Ammar
Depends, if you open() and put the fd to mmap(), then no need PROT_WRITE.
Nils
👌
Ammar
Btw @tuxifan, what IDE do you use? I am looking for __AARGS__ on google, it always brings me to __VA_ARGS__ instead.
Ammar
Hmm, still can't find it with "qt creator" keyword.
Ammar
__AARGS__ does not seem to be a built in macro in gcc 9.3.0 Linux x86_64.
Ammar
Dear admin, can I appeal to remove this warning? Is there really no tolerance to post a message link?
Dima
Removed
Ammar
Thanks :D
Nils
how do I create a new thread without libc on linux?
Nils
thx
Nils
clone syscall
The stack argument specifies the location of the stack used by the child process. Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process. But how do I allocate that stack without malloc()?
Ammar
Stack is VM_GROWSDOWN.
Ammar
It should be allocated by using mmap.
Nils
Using sbrk or mmap (the way malloc works)
sbrk() is not available either… what do I need to pass to mmap?
Nils
Any special stuff?
Ammar
Wait a minute.
Ammar
mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0)
Ammar
That is the call to mmap by pthread.
Nils
clone(payload_main, newstack, CLONE_FILES | CLONE_FS | CLONE_IO | CLONE_PTRACE | CLONE_VM, NULL); Like that?
Ammar
Please try to inspect the pthread call.
Ammar
#include <pthread.h> void *test(void *x) { return NULL; } int main() { pthread_t th; pthread_create(&th, NULL, test, NULL); pthread_join(th, NULL); }
Nils
Not really sure, but the clone arguments are different.
clone(int (*fn)(void *), void *stack, int flags, void *arg)
Ammar
what does pthread have to do with clone?
It uses clone to spawn the thread.
Ammar
You can follow how it creates the thread.
Nils
clone(payload_main, newstack, CLONE_FILES | CLONE_FS | CLONE_IO | CLONE_PTRACE | CLONE_VM, NULL); Like that?
clone(child_stack=0x7fff52447380, flags=CLONE_VM|CLONE_PIDFD|CLONE_PTRACE|CLONE_VFORK|CLONE_NEWNS|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID|CLONE_NEWCGROUP|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWPID|0x7f4200400000|71, parent_tid=0x80002700, tls=0x3, child_tidptr=0x2) = -1 EINVAL (Invalid argument)
Ammar
I have never worked with raw thread like that. Don't really know, but investigating the pthread and follow it may be the better way.
Ammar
I am not able to use pthread here
I meant, inspect the syscall and arguments done by pthread.
Ammar
It is using the syscall internally too.
Nils
I meant, inspect the syscall and arguments done by pthread.
but where to pass the childs entry function?
Ammar
I am still looking for that too.
Ammar
but where to pass the childs entry function?
Yeah, it is the first argument.
Nils
Yeah, it is the first argument.
but the first argument is child_stack??
Ammar
but the first argument is child_stack??
It seems clone by #include <sched.h> is different with literal clone syscall.
Ammar
Ammar
ouch
The important part: The raw clone() system call corresponds more closely to fork(2) in that execution in the child continues from the point of the call.
Nils
I have no idea what TLS is
Nils
or TID
Ammar
I have no idea what TLS is
The TLS (Thread Local Storage) descriptor is set to tls.
Ammar
It seems similar to Thread Safe Resource Manager stuff.
Ammar
Quoted from man: The TLS (Thread Local Storage) descriptor is set to tls. The interpretation of tls and the resulting effect is architecture dependent. On x86, tls is interpreted as a struct user_desc * (see set_thread_area(2)). On x86-64 it is the new value to be set for the %fs base register (see the ARCH_SET_FS argument to arch_prctl(2)). On architectures with a dedicated TLS register, it is the new value of that register. Use of this flag requires detailed knowledge and generally it should not be used except in libraries implementing threading.
Nils
The TLS (Thread Local Storage) descriptor is set to tls.
what if I need my thread to be in the same stack?
Ammar
what if I need my thread to be in the same stack?
Your thread will overwrite your main stack, this clearly will mess your program.
Ammar
Yes, every calls to a function store the return address into the stack.
Ammar
Your thread will call functions. So it is a problem if your thread uses main stack.
Nils
which is bad Ig?
Ammar
which is bad Ig?
Of course very bad.
Ammar
Your return statement won't work correctly.
Ammar
Your local variables too.
Nils
Of course very bad.
clone(child_stack=0x7ffdf36a0110, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_PTRACE|CLONE_IO) = 67238 strace: Detached unknown pid 67238 write(1, "Failed\n", 7 <unfinished ...> +++ killed by SIGSEGV +++ I see...
Ammar
And what do I have to set it to?
I can't find the simple example of this.
Ammar
I need to learn more about this, it is very complicated to me to make a conclusion from pthread sources.
Nils
So this is where I am stuck now.
Ammar
So this is where I am stuck now.
char newstack[1024]; will get removed when return.