Question

In Java, JVMs (e.g. HotSpot) are capable of JIT compilation and this technique is used to speed up execution by compiling bytecode into native code. My question is, how does this technically happen? My understanding was that modern processors mark memory areas into sections that are read-only, and sections that are executable in order to prevent malicious code from executing. So, the JVM can't really write new "executable code" into memory spaces that it has access to (i.e. self modifying code). So, I am guessing that the JVM produces native code, writes it into a file and then uses the operating systems services to dynamically load that native code into memory, and maintains some internal mapping table of the addresses of the native code (function) locations in memory after the operating system has loaded this dynamic code so it can branch out to those native instructions.

I did see this answer: How is JIT compiled code injected in memory and executed?, but I'm confused as to why operating systems would allow user programs READ+EXECUTE memory regions. Do other operating systems i.e. Linux etc offer something similar in order for JIT to work?

Can someone help clarify my understanding?

Était-ce utile?

La solution

In Linux, a memory segment can be set up to be writable and executable (and can be later changed on its protections). Look at the mmap(2) and mprotect(2) syscalls.

The JVM will probably produce machine code in memory, without using any disk files. Its JIT machinery probably just write bytes in executable memory.

Notice that the JVM might not want to change the generated machine code protection (it probably could generate all the machine code inside writable and executable memory segments), because since it is generating itself that code, it can be made sure to not doing nasty things (read about proof-carrying code).

Read the Just-in-time compilation and HotSpot and Virtual Memory wiki pages, and try strace-ing some java process...

Some JVMs are free software (e.g. the one inside OpenJdk), you could study their source code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top