JIT 如何在运行时替换优化的机器代码?

发布于 2024-09-01 18:48:54 字数 88 浏览 8 评论 0原文

我正在浏览 OpenJDK 源代码,但找不到替换优化代码的地方。

我想知道如何在保护模式下完成此操作,这不是操作系统应该阻止的某种自我修改代码吗?

I'm browsing through OpenJDK sources and cannot find the place where optimized code is replaced.

I wonder how this can be done in protected mode, isn't it some kind of selfmodifing code which should be prevented by the OS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-09-08 18:48:55

“JITer”在堆或堆栈中分配空间,并将汇编代码插入其中。不,自我修改代码完全没问题。 VirtualProtect (Windows) 和 mmap (Unix) 可以将页面映射为可执行文件。默认情况下,通用操作系统会将可执行页面标记为读/执行,但不标记为写,您通常仍然可以在运行时更改此设置。

如果无法修改代码,就无法加载 dll,除非将其加载到固定的虚拟地址并共享到每个进程的地址空间;那么你会得到地址空间地狱而不是dll地狱。

我猜您听说过 NX 位或 DEP 等,它们只是保护您免于执行不可执行的代码,这有助于防止堆栈溢出等问题。

The "JITer" allocates space in say the heap or stack and inserts assembly code into it. No, self modifying code is perfectly fine. VirtualProtect (Windows) and mmap (Unix) can map pages as executable. General purpose operating systems by default will mark executable pages as read/execute but not write, you can still typically change this at runtime.

If there was no way to modify code, there would be no way to load a dll unless it's loaded to a fixed Virutal Address and shared into each process's address space; then you'd get address space hell instead of dll hell.

I'm guessing you heard of the NX bit or DEP etc, those just protect you from executing non-executable code, which helps a bit against stack overflows and the likes.

百善笑为先 2024-09-08 18:48:55

JIT 代码不会取代优化的机器代码;它替换加载的 Java 字节码。我不知道 OpenJDK 中是如何实现的,但通常,JVM 加载字节代码并将其保存在某种形式的内部结构中,通常保存在具有一个或多个用于执行代码的虚拟函数的类中。当它被即时编译时,指向该内部结构的指针被指向具有相同接口的类的指针所取代,其中底层表示是本机机器代码而不是 Java 字节代码,并且虚拟方法的实现如下他们调用本机代码而不是解释字节码。没有修改代码,只是指向不同的地方。

The JIT code doesn't replace optimized machine code; it replaces loaded Java bytecode. I don't know how this is implemented in OpenJDK, but typically, the JVM loads the byte code and keeps it in some form of internal structure, usually in a class that has a virtual function or virtual functions for executing the code. When it is just-in-time compiled, the pointer to that internal structure is replaced by a pointer to a class with the same interface, where the underlying representation is native machine code instead of Java byte code, and the virtual methods are implemented such that they invoke the native code rather than interpreting the byte code. There is no modification of code, merely pointing to different places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文