Created
August 7, 2022 00:38
-
-
Save pablogsal/a9f1594a3a9090187d57817ae7b78ca3 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
#include <stdio.h> #include <stdlib.h> #include <sys/mman.h> | |
#include <stdio.h> | |
typedef int(*fn)(void*, void*); | |
typedef int(*lel)(void*); | |
int say_hello(void* f) { | |
int* val = (int*)f; | |
printf("Say hello: %d\n", *val); | |
sleep(10); | |
return 12; | |
} | |
fn compile_blech(void) { | |
char *memory = mmap(NULL, // address | |
4096, // size | |
PROT_READ | PROT_WRITE | PROT_EXEC, | |
MAP_PRIVATE | MAP_ANONYMOUS, | |
-1, // fd (not used here) | |
0); // offset (not used here) | |
if (!memory) { | |
perror("failed to allocate memory"); | |
exit(1); | |
} | |
//0x000000000000120e <+0>: f3 0f 1e fa endbr64 | |
//0x0000000000001212 <+4>: 48 83 ec 08 sub $0x8,%rsp | |
//0x0000000000001216 <+8>: 48 89 f8 mov %rdi,%rax | |
//0x0000000000001219 <+11>: 48 89 f7 mov %rsi,%rdi | |
//0x000000000000121c <+14>: ff d0 callq *%rax | |
//0x000000000000121e <+16>: 48 83 c4 08 add $0x8,%rsp | |
//0x0000000000001222 <+20>: c3 retq | |
//0x000000000000120e <+0>: f3 0f 1e fa endbr64 | |
//0x0000000000001212 <+4>: 48 83 ec 08 sub $0x8,%rsp | |
//0x0000000000001216 <+8>: 48 89 f8 mov %rdi,%rax | |
//0x0000000000001219 <+11>: 48 89 f7 mov %rsi,%rdi | |
//0x000000000000121c <+14>: ff d0 callq *%rax | |
//0x000000000000121e <+16>: 48 83 c4 08 add $0x8,%rsp | |
//0x0000000000001222 <+20>: c3 retq | |
int i = 0; | |
memory[i++] = 0xf3; | |
memory[i++] = 0x0f; | |
memory[i++] = 0x1e; | |
memory[i++] = 0xfa; | |
memory[i++] = 0x48; | |
memory[i++] = 0x83; | |
memory[i++] = 0xec; | |
memory[i++] = 0x08; | |
memory[i++] = 0x48; | |
memory[i++] = 0x89; | |
memory[i++] = 0xf8; | |
memory[i++] = 0x48; | |
memory[i++] = 0x89; | |
memory[i++] = 0xf7; | |
memory[i++] = 0xff; | |
memory[i++] = 0xd0; | |
memory[i++] = 0x48; | |
memory[i++] = 0x83; | |
memory[i++] = 0xc4; | |
memory[i++] = 0x08; | |
memory[i++] = 0xc3; | |
return (fn) memory; | |
} | |
int do_the_thing() { | |
fn f = compile_blech(); | |
int i = 42; | |
int ret = f(&say_hello, &i); | |
printf("Say goodby: %d\n", ret); | |
munmap(f, 4096); | |
return 0; | |
} | |
int main () { | |
do_the_thing(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment