自修改代码的用例?
在冯诺依曼架构上,程序和数据都存储在内存中,因此程序可以修改自身。这对程序员有用吗?你能举一些例子吗?
On a Von Neumann architecture, program and data are both stored in memory, so a program can modify itself. Is this useful for a programmer? Could you give some examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变形
我想到的一个(有问题的)用例是变形计算机病毒。这些是恶意软件,它们通过重写自己的代码来隐藏自己,从而避免基于签名的检测机器代码到语义上等效的表示,看起来不同。
蹦床
另一个(更复杂,但也更常见)用例是蹦床,一种基于动态代码生成的技术,用于解决嵌套函数调用的某些问题。
JIT 编译
我能想到的动态代码生成最常见的用法是 JIT(即时)编译。 .NET 或 Java 等现代语言不会编译为本机机器代码,而是编译为某种中间语言(称为字节码)。然后在执行程序时(通过为目标体系结构编写的虚拟机)解释该字节码。同时,后台进程会检查代码的哪些部分执行得非常频繁。然后,这些部分有很好的机会被动态编译为本机机器语言,以获得最大性能。这一切都发生在程序运行时!
安全隐患
需要记住的一件事是,将数据解释为代码的可能性对于利用计算机软件中的安全漏洞非常有用,这就是为什么现代硬件和操作系统的趋势是启用和,如果可能的话,甚至强制执行代码和数据的分离(另请参阅 NX 位 和 DEP)。
Metamorphism
One (questionable) use case that comes to my mind is metamorphic computer viruses. These are malicious pieces of software that conceal themselves from signature based detection by rewriting their own machine code to an semantically equivalent representation that looks different.
Trampolining
Another (more complex, but also more common) use case is trampolining, a technique based on dynamic code generation to solve certain problems with nested function calls.
JIT compilation
The most common usage of dynamic code generation that I can think of is JIT (just-in-time) compilation. Modern languages like .NET or Java are not compiled into native machine code, but into some kind of intermediate language (called bytecode). This bytecode is then interpreted when the program is executed (by a virtual machine written for the target architecture). At the same time, a background process checks which parts of the code are executed very often. These parts then have a good chance of being dynamically compiled into native machine language for maximum performance. All this happens during the run time of the program!
Security implications
One thing to keep in mind is that the possibility to interpret data as code is useful for exploiting security holes in computer software, which is why the trend in modern hardware and operating systems is to enable and, if possible, even enforce the separation of code and data (also see NX bit and DEP).
我可以通过向您推荐类似(写得非常好且回答得很好)问题的答案来最好地回答这个问题,该问题也在 StackOverflow 上 - 同音且“不受限制”的自修改代码 + lisp 真的可以自修改吗?。答案集中在 Lisp 上,这是一种以将“代码就是数据”提升到新水平而闻名的家族语言,并探讨了它在人工智能中的用途。
I can best answer this by referring you to an answer to a similar (exceptionally well written and answered) question, also on StackOverflow - Homoiconic and "unrestricted" self modifying code + Is lisp really self modifying?. The answer focuses on Lisp, a family languages known for taking "code is data" to the next level, and explores the uses of that in AI.