How JIT Compilers Operate
JIT compilers, by definition, generate code on the fly for execution. But in, say, Windows,开发者_开发百科 we have all kinds of protection that prevent self modifying code or executing from data memory (DEP).
So how is it possible for JIT compilers to generate code on the fly?
They ask the OS for some memory which is readable, writeable and executable.
e.g. you can allocate such memory using mmap()
with PROT_READ | PROT_WRITE | PROT_EXEC
(POSIX), or VirtualAlloc()
with PAGE_EXECUTE_READWRITE
(Windows).
For a real example, see LLVM's llvm::sys::Memory::AllocateRWX
(Unix implementation; Windows implementation).
精彩评论