Some side project: Brainfuck-JIT
I actually have wondered how JIT (Just-In-Time compiling) works for quite a long time, because it is basic knowledge that code segment in memory is write-protected, and you cannot move CPU instruction pointer to stack frame or heap for to security reason. But to do JIT you need to generate new machine code in memory and instantly execute it. How?
I stumbled upon this article titled “The Joy of Simple JITs”, which get me started. Apparently, you can do mmap
(POSIX) or VirtualAlloc
(Win32) to allocate virtual file in memory. And you can write to this section. And you can execute it. Looking great?