Executing native code using the JVM / CLR

How does the JVM / CLR compile native JIT code? Is it by injecting code or by copying code into executable memory? What are the system calls that enable dynamic code execution?

+6
clr jvm jit
source share
3 answers

I can explain how we do this in the CACAO VM (JVM JIT study). First, the machine code for the method is generated in some block of memory allocated by the heap. After compilation, the final code length is known, and a fragment of executable memory is allocated using the mmap and PROT_EXEC (the corresponding CACAO code is here ). Then the machine code is copied to the mmapped area. After this, many architectures require some mechanism to clear the machineโ€™s cache. As an example, consider the cache clear function for PowerPC 64. It is noteworthy that there is nothing to do on the i386 and x86_64. After this step, the processor is ready to execute the newly generated code. Alternatively, already allocated memory pages can be marked as executable with mprotect . Note that mmap / mprotect are Unix objects.

+3
source share

I donโ€™t know specifically how Java does this, but in general you should embed the "trap" opcodes in the interpreter instruction stream. There are two opcodes in the JVM specification http://download.java.net/jdk6/source/

+2
source share

The runtime of the Common Language Runtime has a table method for each type with positions that point to its own code or its own stub for managed JIT code, and then fixes the tabletable method with a pointer to the newly created native code.

MSDN has a more detailed explanation explanation in the MethodDesc section

This blog post by Dave Notario explains how the CLR JIT compiler works.

+2
source share

All Articles