C#代碼中插入X86彙編

這兩天在看C# SIMD相關的東西, 在爆棧上面搜到一段代碼, 表示很震驚, 還是得貼出來…

 1 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
 2 delegate void VectorAddDelegate(float[] C, float[] B, float[] A, int length);
 3 
 4 [DllImport("kernel32.dll", SetLastError = true)]
 5 static extern IntPtr VirtualAlloc(
 6     IntPtr lpAddress, UIntPtr dwSize,
 7     IntPtr flAllocationType, IntPtr flProtect);
 8 
 9 //This array of bytes has been produced from
10 //the SSE assembly version – it is a complete
11 //function that accepts four parameters (three
12 //vectors and length) and adds the vectors
13 
14 byte[] sseAssemblyBytes = {
15     0x8b, 0x5c, 0x24, 0x10, 0x8b, 0x74, 0x24, 0x0c, 0x8b,
16     0x7c, 0x24, 0x08, 0x8b, 0x4c, 0x24, 0x04, 0x31, 0xd2,
17     0x0f, 0x10, 0x0c, 0x97, 0x0f, 0x10, 0x04, 0x96, 0x0f,
18     0x58, 0xc8, 0x0f, 0x11, 0x0c, 0x91, 0x83, 0xc2, 0x04,
19     0x39, 0xda, 0x7f, 0xea, 0xc2, 0x10, 0x00 };
20 
21 IntPtr codeBuffer = VirtualAlloc(
22     IntPtr.Zero, new UIntPtr((uint)sseAssemblyBytes.Length),
23     0x1000 | 0x2000, //MEM_COMMIT | MEM_RESERVE
24     0x40 //EXECUTE_READWRITE
25     );
26 
27 Marshal.Copy(sseAssemblyBytes, 0, codeBuffer, sseAssemblyBytes.Length);
28 
29 VectorAddDelegate addVectors = (VectorAddDelegate)
30     Marshal.GetDelegateForFunctionPointer(codeBuffer, typeof(VectorAddDelegate));
31 //We can now use ‘addVectors’ to add vectors!

這段代碼裏面的sseAssemblyBytes, 實際上是一段彙編代碼, 他先把這段彙編代碼拷貝到虛擬內存裏面去, 然後設置這塊內存可以被執行EXECUTE_READWRITE, 從而在這塊內存上創建一個函數指針….

雖然這代碼不能移植, 但是感覺還是有一點點屌

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章