-
-
Save jsm222/38279218adf608b48985c174cedad014 to your computer and use it in GitHub Desktop.
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
#define _BSD_SOURCE // MAP_ANONYMOUS | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#define PAGE_SIZE 4096 | |
struct asmbuf { | |
uint8_t code[PAGE_SIZE - sizeof(uint64_t)]; | |
uint64_t count; | |
}; | |
struct asmbuf * | |
asmbuf_create(void) | |
{ | |
int prot = PROT_READ | PROT_WRITE | PROT_EXEC; | |
int flags = MAP_ANONYMOUS | MAP_PRIVATE; | |
return mmap(NULL, PAGE_SIZE, prot, flags, -1, 0); | |
} | |
void | |
asmbuf_free(struct asmbuf *buf) | |
{ | |
munmap(buf, PAGE_SIZE); | |
} | |
void | |
asmbuf_finalize(struct asmbuf *buf) | |
{ | |
int i=0; | |
mprotect(buf, PAGE_SIZE, PROT_READ|PROT_EXEC); | |
} | |
void | |
asm_insbuf(struct asmbuf *buf, int size, uint64_t ins) | |
{ | |
for (int i = 0;i<size; i++) { | |
buf->code[buf->count++] = (ins>> (i * 8)) & 0xff; | |
} | |
} | |
void | |
asmbuf_immediate(struct asmbuf *buf, int size, const void *value) | |
{ | |
memcpy(buf->code + buf->count, value, size); | |
buf->count += size; | |
} | |
int main() | |
{ | |
/* Compile input program */ | |
struct asmbuf *buf = asmbuf_create(); | |
asm_insbuf(buf,4,0xd10043ff); | |
asm_insbuf(buf,4,0x2a1f03e0); | |
asm_insbuf(buf,4,0xb80ff3ff); | |
asm_insbuf(buf,4,0x910043ff); | |
//asm_insbuf(buf,4,0xd2800040); | |
asm_insbuf(buf,4,0xd2800088); | |
asm_insbuf(buf,4,0xd4000001); | |
asm_insbuf(buf,4,0xd65f03c0); | |
/* | |
0: d10043ff sub sp, sp, #0x10 | |
4: 2a1f03e0 mov w0, wzr | |
8: b9000fff str wzr, [sp, #0xc] | |
c: 910043ff add sp, sp, #0x10 | |
*/ | |
int(*test)(int,char*,int) = (void *)buf->code; | |
//asmbuf_finalize(buf); | |
test(11,"helloworld\n",11); | |
asmbuf_free(buf); | |
return 0; | |
} |
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
#define _BSD_SOURCE // MAP_ANONYMOUS | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <sys/mman.h> | |
#define PAGE_SIZE 4096 | |
struct asmbuf { | |
uint8_t code[PAGE_SIZE - sizeof(uint64_t)]; | |
uint64_t count; | |
}; | |
struct asmbuf * | |
asmbuf_create(void) | |
{ | |
int prot = PROT_READ | PROT_WRITE; | |
int flags = MAP_ANONYMOUS | MAP_PRIVATE; | |
return mmap(NULL, PAGE_SIZE, prot, flags, -1, 0); | |
} | |
void | |
asmbuf_free(struct asmbuf *buf) | |
{ | |
munmap(buf, PAGE_SIZE); | |
} | |
void | |
asmbuf_finalize(struct asmbuf *buf) | |
{ | |
int i=0; | |
mprotect(buf, PAGE_SIZE, PROT_READ|PROT_EXEC); | |
} | |
void | |
asm_insbuf(struct asmbuf *buf, int size, uint64_t ins) | |
{ | |
for (int i = 0;i<size; i++) { | |
buf->code[buf->count++] = (ins>> (i * 8)) & 0xff; | |
} | |
} | |
void | |
asmbuf_immediate(struct asmbuf *buf, int size, const void *value) | |
{ | |
memcpy(buf->code + buf->count, value, size); | |
buf->count += size; | |
} | |
int main() | |
{ | |
/* Compile input program */ | |
struct asmbuf *buf = asmbuf_create(); | |
asm_insbuf(buf,4,0xd10043ff); | |
asm_insbuf(buf,4,0x2a1f03e0); | |
asm_insbuf(buf,4,0xb80ff3ff); | |
asm_insbuf(buf,4,0x910043ff); | |
//asm_insbuf(buf,4,0xd2800040); | |
asm_insbuf(buf,4,0xd2800088); | |
asm_insbuf(buf,4,0xd4000001); | |
asm_insbuf(buf,4,0xd65f03c0); | |
/* | |
0: d10043ff sub sp, sp, #0x10 | |
4: 2a1f03e0 mov w0, wzr | |
8: b9000fff str wzr, [sp, #0xc] | |
c: 910043ff add sp, sp, #0x10 | |
*/ | |
int(*test)(int,char*,int) = (void *)buf->code; | |
asmbuf_finalize(buf); | |
test(11,"helloworld\n",11); | |
asmbuf_free(buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment