[英]How is it possible for JVM to run generated machine code from a Java application via JIT?
How is it possible that JVM compiles bytecode into native code dynamically and then executes it? JVM如何动态地将字节码编译为本机代码然后执行呢? I can imagine that it is possible to write data values into memory but if I remember correctly, a program can't write to the memory that contains instructions (otherwise viruses could use this feature and proliferate quickly).
我可以想象有可能将数据值写入内存,但是如果我没记错的话,程序将无法写入包含指令的内存(否则病毒可能会使用此功能并迅速扩散)。
Very few architectures implement the level of memory protection (only the OS has write access to memory areas containing code) you're talking about, the ones where Java uses a JIT definitely don't. 很少有架构实现您所谈论的内存保护级别(只有操作系统具有对包含代码的内存区域的写访问权),而Java使用JIT的架构绝对不会。
And viruses do use this feature and even more to proliferate quickly. 病毒确实使用此功能,甚至更多功能可以快速扩散。 But when you think about it, there's nothing inherently dangerous in a process modifying its own code.
但是,当您考虑它时,在修改自己的代码的过程中并没有固有的危险。 It isn't any more dangerous than being able to write to a file and then load a library.
它比能够写入文件然后加载库没有更多危险。
Normally, you can't write to the section of memory that contains code directly, but there are ways to override that. 通常,您不能直接写入包含代码的内存部分,但是有一些方法可以覆盖它。 For JITs, often what is done is to have some read-write data space on the heap and then use an operation such as
mprotect
to make it executable. 对于JIT,通常要做的是在堆上具有一些读写数据空间,然后使用诸如
mprotect
的操作使其可执行。
OSes do provide facilities to allocate "executable" memory. 操作系统确实提供了分配“可执行”内存的功能。 The JVM needs to allocate the target memory in a different way than standard malloc() when generating JITed code.
生成JIT代码时,JVM需要以与标准malloc()不同的方式分配目标内存。
For example, on Windows, use VirtualAlloc with PAGE_EXECUTE . 例如,在Windows上,将VirtualAlloc与PAGE_EXECUTE一起使用。 Similar functions exist in Linux, AIX, etc...
Linux,AIX等中也存在类似的功能...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.