-
-
Save mgeeky/ef217445b9702e017ea1706d09f2f04b to your computer and use it in GitHub Desktop.
haha funny jit go brrrr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
namespace FunkyJit | |
{ | |
class Program | |
{ | |
public static void Nothing() { Console.WriteLine(); } | |
static void Main(string[] args) | |
{ | |
IntPtr pMem = GenerateRWXMemory(4096); // Doesn't divide cleanly so we get 4113 (17 extra bytes) of space | |
Console.WriteLine("Method Table: 0x{0:X}", (long)typeof(Program).TypeHandle.Value); | |
Console.WriteLine("JIT: 0x{0:X} ?", (long)pMem); | |
Console.ReadKey(); | |
} | |
public static IntPtr GenerateRWXMemory(int ByteCount) | |
{ | |
AssemblyName AssemblyName = new AssemblyName("Assembly"); | |
AssemblyBuilder AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess.Run); | |
ModuleBuilder ModuleBuilder = AssemblyBuilder.DefineDynamicModule("Module"); | |
MethodBuilder MethodBuilder = ModuleBuilder.DefineGlobalMethod( | |
"MethodName", | |
MethodAttributes.Public | MethodAttributes.Static, | |
typeof(void), //arbitrary return type hehexd | |
new Type[] { }); // no args, but no real reason | |
ILGenerator il = MethodBuilder.GetILGenerator(); | |
// sub rsp,28h (0x48, 0x83, 0xec, 0x28) [4] | |
// Every Emit.WriteLine results in 18 bytes | |
// mov rcx,1D3E2F736A8h (0xe8, 0x7a, 0x07, 0x45, 0x5e) [5] | |
// rcx,qword ptr [rcx] (0x48, 0x8b, 0x09) [3] | |
// call mscorlib_ni!System.Console.WriteLine (0x48, 0xb9, 0xa8, 0x36, 0xf7, 0xe2, 0x2d, 0x30, 0x10, 0x00) [10] | |
// Ends with | |
// ret (0xc3) [1] | |
while (ByteCount > 0) | |
{ | |
il.EmitWriteLine("bruh"); | |
ByteCount -= 18; | |
} | |
il.Emit(OpCodes.Ret); // JIT to 0xc3 | |
ModuleBuilder.CreateGlobalFunctions(); | |
RuntimeMethodHandle mh = ModuleBuilder.GetMethods()[0].MethodHandle; | |
RuntimeHelpers.PrepareMethod(mh); | |
return mh.GetFunctionPointer(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment