24 lines
554 B
Plaintext
24 lines
554 B
Plaintext
/* mclib.c */
|
|
|
|
// gcc -c -fpic mclib.c
|
|
// gcc -shared -o mclib.so mclib.o
|
|
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
|
|
typedef unsigned char uchar;
|
|
|
|
extern uchar run_machine_code(const char *code, uchar a, uchar b, int len) {
|
|
void *buf;
|
|
uchar c;
|
|
/* copy code to executable buffer */
|
|
buf = mmap (0, len, PROT_READ|PROT_WRITE|PROT_EXEC,
|
|
MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
memcpy (buf, code, len);
|
|
/* run code */
|
|
c = ((uchar (*) (uchar, uchar))buf)(a, b);
|
|
/* free buffer */
|
|
munmap (buf, len);
|
|
return c;
|
|
}
|