4

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?

JavaMan
  • 4,954
  • 4
  • 41
  • 69
  • possible duplicate of [JIT compilation and DEP](http://stackoverflow.com/questions/570257/jit-compilation-and-dep) (Truly *possible* because this may be meant broader and that question only answers it indirectly) –  May 22 '11 at 10:48
  • See also [this question](http://stackoverflow.com/questions/2837635/how-does-jit-replace-optimized-machine-code-during-runtime). – Piotr Praszmo May 22 '11 at 10:55
  • Whewt, I already thought I wouldn't find an answer to this. So far I only found "you can use this for self-modifying code, and that is evil, go away devil!" But of course, if that were the case, languages like java would be rather slow. :) – cib Sep 25 '11 at 03:05

1 Answers1

7

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).

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119