简体   繁体   English

有没有办法动态生成本机x86代码并从.NET执行它?

[英]Is there a way to dynamically generate native x86 code and execute it from .NET?

I'd like to dynamically generate native unmanaged x86 code and then execute it super fast from managed .NET. 我想动态生成本机非托管x86代码,然后从托管.NET中快速执行它。 I know I could emit some IL code and execute that, but I'm thinking the jetting would take too much time to get the benefit of any speed gain I get from it. 我知道我可以发出一些IL代码并执行它,但我认为喷射会花费太多时间来获得我从中获得的任何速度增益的好处。

I need super fast code, I want to generate a function with the x86 opcodes in memory and pass a fixed pointer to a memory block to it, so it would make some really fast calculations on that block. 我需要超快速代码,我想在内存中生成一个带有x86操作码的函数,并将一个固定指针传递给它的内存块,因此它会在该块上进行一些非常快速的计算。

I just not sure how to call the native code from .net. 我只是不确定如何从.net调用本机代码。 remember this should be on the fly in memory, not building a dll. 记住这应该是在内存中,而不是构建一个DLL。 Speed is what really matters here. 速度才是真正重要的。 It's part of a genetic computation project. 它是遗传计算项目的一部分。

The C language is the "portable assembler" and you can generate x86 opcodes directly (writing in assembler would be better). C语言是“可移植的汇编程序”,你可以直接生成x86操作码(用汇编语言编写会更好)。 If you compiled the C code into an object, you could link it into .net as a library. 如果将C代码编译为对象,则可以将其作为库链接到.net。

If you are trying to generate executable code dynamically (on-the-fly) you would need to allocate a binary array, push the object code into the array, then get the beginning address of the array after the memory headed and assign that to a function pointer and call it. 如果您正在尝试动态生成可执行代码(即时),则需要分配二进制数组,将目标代码推送到数组中,然后在内存开头后获取数组的起始地址并将其分配给函数指针并调用它。

However, antivirus software specifically looks for this behavior and would identify your code a virus more than likely. 但是,防病毒软件专门查找此行为,并且很可能会将您的代码识别为病毒。

Also your processor is designed to have "code" memory segments and "data" memory segments. 此外,您的处理器设计为具有“代码”内存段和“数据”内存段。 Typically you cannot dynamically modify the code segment without causing a segfault and you cannot execute out of a data segment without causing a segfault. 通常,您不能在不导致段错误的情况下动态修改代码段,并且无法在不导致段错误的情况下执行数据段。

Also, you code would only run on a SISC processor (x86) and not on a RISC processor. 此外,您的代码只能在SISC处理器(x86)上运行,而不能在RISC处理器上运行。

There is a lot to consider. 有很多事要考虑。

We used to do this in assembler in the old days on SISC systems. 过去,我们曾在SISC系统上用汇编程序执行此操作。

Cheers, David 干杯,大卫

The short answer is that you can't do that with C#. 简短的回答是你不能用C#做到这一点。

You can do that with C++/CLI, by building a mixed-mode assembly that does your "super fast" calculations in native code. 您可以使用C ++ / CLI,通过构建一个在本机代码中进行“超快速”计算的混合模式程序集来实现。 That way, you wouldn't need (presumably) to generate executable code "on-the-fly". 这样,你就不需要(推测)“即时”生成可执行代码。

If for some reason you can't hard-code the calculation functions, then you will have to acquire executable memory from the operating system. 如果由于某种原因您无法对计算功能进行硬编码,则必须从操作系统获取可执行内存。

On Windows, you do that with the VirtualAlloc and VirtualProtect functions. 在Windows上,您可以使用VirtualAllocVirtualProtect函数执行此操作。 Again, doing that from C# would require P/Invoke interop, which would most likely reverse your speed gains. 同样,从C#执行此操作需要P / Invoke互操作,这很可能会逆转您的速度增益。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM