Advertisement
snake5

first step towards JIT

Oct 20th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. typedef int (*somefn) ();
  6.  
  7. int main()
  8. {
  9.     char prebuf[ 512 ];
  10.     int precnt = 0;
  11.    
  12. #define APPEND_CODE( cb, cbsize ) \
  13.     memcpy( prebuf + precnt, cb, cbsize ); \
  14.     precnt += cbsize;
  15.    
  16.     <$
  17.     mov eax, 5
  18.     add eax, eax
  19.     ret
  20.     $>
  21. #undef APPEND_CODE
  22.    
  23.     while( precnt % 4 != 0 )
  24.         prebuf[ precnt++ ] = 0x90; // padding NOP
  25.    
  26.     void* func = VirtualAlloc( NULL, precnt, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
  27.     memcpy( func, prebuf, precnt );
  28.    
  29.     int test = ((somefn)func)();
  30.     printf( "return value: %d\n", test );
  31.    
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement