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.
/*
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
};