So, in a feature request I filed against Node.js, I was looking for a way to replace the current Node process with another. In Linux and friends (really, any POSIX-compliant system), this is easy: use execve
and friends and call it a day. But obviously, that won't work on Windows, since it only has CreateProcess
(which execve
and friends delegate to, complete with async behavior). And it's not like people haven't wanted to do similar, leading to numerous duplicate questions on this site. (This isn't a duplicate because it's explicitly seeking a workaround given certain constraints, not just asking for direct replacement.)
Process replacement has several facets that have to addressed:
- All console I/O streams have to be forwarded to the new process.
- All signals need transparently forwarded to the new process.
- The data from the old process have to be destroyed, with as many resources reclaimed as possible.
- All pre-existing threads and child processes should be destroyed.
- All pre-existing handles should be destroyed apart from open file descriptors and named pipes/etc.
- Optimally, the old process's memory should be kept to a minimum after the process is created.
- For my particular use case, retaining the process ID is not important.
And for my particular case, there are a few constraints:
- I can control the initial process's startup as well as the location of my "process replacement" function.
- I could load arbitrary native code via add-ons at potentially any stack offset.
- Implication: I can't even dream of tracking
malloc
calls, handles, thread manipulation, or process manipulation to track and free them all, since DLL rewriting isn't exactly practical.
- Implication: I can't even dream of tracking
- I have no control over when my "process replacement" is called. It could be called through an add-on, which could've been called through either interpreted code via FFI or even another add-on recursively. It could even be called during add-on initialization.
- Implication: I would have no ability to know what's in the stack, even if I perfectly instrumented my side. And rewriting all their
call
s andpush
es is far from practical, and would just be all-around slow for obvious reasons.
- Implication: I would have no ability to know what's in the stack, even if I perfectly instrumented my side. And rewriting all their
So, here's the gist of what I was thinking: use something similar to a pseudo-trampoline.
- Statically allocate the following:
- A single pointer for the stack pointer.
MAX_PATH + 1
chars for the application path +'\0'
.MAX_PATH + 1
chars for the current working directory path +'\0'
.- 32768 chars for the arguments +
'\0'
. - 32768 chars for the environment +
'\0'
.
- On entry, set the global stack pointer reference to the stack pointer.
- On "replacement":
- Do relevant process cleanup and lock/release everything you can.
- Set the stack pointer to the stored original global one.
- Terminate each child thread.
- Kill each child process.
- Free each open handle.
- If possible (i.e. not in a UWP program), For each heap, destroy it if it's not the default heap or the temporary heap (if it exists).
- If possible, close each open handle.
- If possible, walk the default heap and free each segment associated with it.
- Create a new process with the statically allocated file/arguments/environment/etc. with no new window created.
- Proxy all future received signals, exceptions, etc. without modification to this process somehow. The standard signals are easy, but not so much with the exceptions.
- Wait for the process to end.
- Return with the process's exit code.
The idea here is to use a process-based trampoline and drop the current process size to an absolute minimum while the newly created one is started.
But where I'm not very familiar with Windows, I probably made quite a few mistakes here. Also, the above seems extremely inefficient and to an extent it just feels horribly wrong for something a kernel could just release a few memory pages, deallocate a bunch of memory handles, and move some memory around for the next process.
So, to summarize, what's the ideal way to emulate process replacement on Windows with the fewest limitations?