简体   繁体   English

JVM如何通过JIT运行Java应用程序中生成的机器代码?

[英]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上,将VirtualAllocPAGE_EXECUTE一起使用。 Similar functions exist in Linux, AIX, etc... Linux,AIX等中也存在类似的功能...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在jvm上运行生成的frege Java代码? - How to run generated frege java code on jvm? JVM-非jit编译的代码如何运行 - JVM - How is non jit-compiled code run JVM JIT编译器如何优化“重复”的Java代码? - How “duplicated” Java code is optimized by the JVM JIT compiler? 是否可以查看JVM热点编译器生成的机器代码? - Is it possible to view the machine code generated by the JVM hotspot compiler? 如何在 JVM 中查看 JIT 编译的代码? - How to see JIT-compiled code in JVM? Java:如何通过jvm和http监视器在同一台机器上外部运行来保护应用程序安全? - Java: how to make application secure from jvm and http monitor running externally on same machine? 如何在基于Java / JVM的应用程序中拟合和评分机器学习模型 - how to fit and score a machine learning models in Java/JVM based application Java Crashed application-如何读取JVM生成的崩溃文件? - Java Crashed application - how to read crash file generated by JVM? 是否可以从不同的 JVM 调用 Java 应用程序中的方法? - Is it possible to call method within a Java application from a different JVM? 使用特定 jvm 在具有多个 jvm 的机器上运行 java 代码的正确方法 - correct way to use specific jvm to run java code on machine that has multiple jvms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM