JIT编译code是如何注入内存和执行? [英] How is JIT compiled code injected in memory and executed?

查看:359
本文介绍了JIT编译code是如何注入内存和执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个典型的Windows x86或   AMD64架构,内存   可执行段划分的   不能写入和数据部分   可以写入,但也可以不   执行(想想DEP)。

"Consider a typical Windows x86 or AMD64 architecture, the memory is divided in executable sections that cannot be written to and data sections that can be written to but cannot be executed (think DEP)."

JIT编译内存的方法,并   (一般)没有任何存储   磁盘,而不是移动它周围的地方   下一指令指针可以达到   它改变了当前指令   指针(指向JIT)到点   到新生成的code和然后   执行它。

"JIT compiles methods in-memory, does (generally) not store anything to disk, instead moves it around where the next instruction pointer can reach it, changes the current instruction pointer (pointing to the JIT) to point to the newly generated code and then executes it."

这些两段,而有点过于简单,有什么我基本明白JIT和Windows的内存模型。我也知道,当我尝试用手工复制一些可执行code在内存中,并尝试执行它,我一般都无法做到这一点(除非用DLL注入)。

These two paragraphs, while a bit over-simplified, are what I basically understand of JIT and Windows' memory model. I also know that when I try to copy some executable code in memory by hand and try to execute it, I will generally be unable to do it (unless with DLL injection).

怎么了JIT,设计师克服这一障碍?他们使用一个环形0驱动程序或用户模式下完成这一切?

How did the JIT-designers overcome this hurdle? Do they use a ring-0 driver or is everything done in user mode?

推荐答案

这只不过是与Windows VirtualProtect的()API函数来完成。它改变了虚拟内存的页面属性。从PAGE_READWRITE所以JIT编译器可以写机器code到PAGE_EXECUTE_READ,因此它可以被执行。没有特殊的权限才能这样做,因为页面是由也运行JIT编译器的进程所拥有的。

It is simply done with the Windows VirtualProtect() API function. It changes the virtual memory page attributes. From PAGE_READWRITE so the JIT compiler can write the machine code to PAGE_EXECUTE_READ so it can be executed. No special privileges are required to do so since the page is owned by the process that also runs the JIT compiler.

这篇关于JIT编译code是如何注入内存和执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆