Author Topic: Executing JIT binary in Linux (Hardcore)  (Read 1884 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Executing JIT binary in Linux (Hardcore)
« on: March 25, 2014, 10:08:43 AM »
This overcomes the particular obstacle of DEP (Data Execution Prevention), which is active by default on Ubuntu and other systems. The binary must be held in mapped pages of memory with the required attributes:

JIT (Just-in-time) compiling is what OxygenBasic does to execute binaries directly in memory.

Code: [Select]

/*

Linux memory allocation for JIT compiling
Avoids DEP (Data Execution Prevention)

gcc  mb.c -o a
*/


/*

http://stackoverflow.com/questions/570257/jit-compilation-and-dep

http://linux.die.net/man/2/mmap

Name
mmap, munmap - map or unmap files or devices into memory
Synopsis

#include <sys/mman.h>
void *mmap(void *addr, size_t lengthint " prot ", int " flags ,
           int fd, off_t offset);int munmap(void *addr, size_t length);


*/

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int (*call)(); // prototype for machine code calls

int main(int argc, char* argv[])
{
  int a;
  int ps= sysconf(_SC_PAGESIZE);
  char*c = mmap(NULL, ps, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, 0, 0);
  c[0]=0xc3;               // insert ret instruction
  call=(void*) c;          // assign calling address
  a=call();                // call the binary
  printf("ok: %x\n", a);   // display return value
  munmap((void*) c,ps);    // release the mapped memory
};

JRS

  • Guest
Re: Executing JIT binary in Linux (Hardcore)
« Reply #1 on: March 25, 2014, 10:41:34 AM »
How are arguments passed?

This looks very promising!

Charles Pegge

  • Guest
Re: Executing JIT binary in Linux (Hardcore)
« Reply #2 on: March 25, 2014, 11:03:33 AM »
Params can also be encoded in JIT binary.  cdecl, Ms64, Amd64 and Arm are all very different, so I think it makes sense to bypass static inline assembler and go directly to dynamically  encoded binary.

JRS

  • Guest
Re: Executing JIT binary in Linux (Hardcore)
« Reply #3 on: March 25, 2014, 11:15:09 AM »
Quote
so I think it makes sense to bypass static inline assembler and go directly to dynamically  encoded binary.

I was thinking the same thing.  ;D ;D

This puts a new twist on O2 Virtual DLLs.