Created
October 8, 2012 06:35
-
-
Save warabanshi/3851051 to your computer and use it in GitHub Desktop.
python-JIT
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
.intel_syntax noprefix | |
.globl main | |
main: | |
push r12 | |
push r13 | |
push rbx | |
mov r13, 0x123456789abcdef0 | |
mov r12, 26 | |
mov rbx, 0x41 | |
loop: | |
cmp r12, 0 | |
jz end | |
mov rdi, rbx | |
call r13 | |
inc rbx | |
dec r12 | |
jmp loop | |
end: | |
mov rdi, 0xa | |
call r13 | |
pop rbx | |
pop r13 | |
pop r12 | |
ret |
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
a.out: file format elf64-x86-64 | |
Disassembly of section .text: | |
0000000000000000 <main>: | |
0: 41 54 push r12 | |
2: 41 55 push r13 | |
4: 53 push rbx | |
5: 49 bd f0 de bc 9a 78 movabs r13,0x123456789abcdef0 | |
c: 56 34 12 | |
f: 49 c7 c4 1a 00 00 00 mov r12,0x1a | |
16: 48 c7 c3 41 00 00 00 mov rbx,0x41 | |
000000000000001d <loop>: | |
1d: 49 83 fc 00 cmp r12,0x0 | |
21: 74 0e je 31 <end> | |
23: 48 89 df mov rdi,rbx | |
26: 41 ff d5 call r13 | |
29: 48 ff c3 inc rbx | |
2c: 49 ff cc dec r12 | |
2f: eb ec jmp 1d <loop> | |
0000000000000031 <end>: | |
31: 48 c7 c7 0a 00 00 00 mov rdi,0xa | |
38: 41 ff d5 call r13 | |
3b: 5b pop rbx | |
3c: 41 5d pop r13 | |
3e: 41 5c pop r12 | |
40: c3 ret |
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
import sys, struct | |
from ctypes import * | |
libc = cdll.LoadLibrary("libc.so.6") | |
free = libc.free | |
printf = libc.printf | |
putchar = libc.putchar | |
mmap = libc.mmap | |
mmap.restype = c_void_p | |
munmap = libc.munmap | |
munmap.argtype = [c_void_p, c_size_t] | |
PROT_READ = 1 | |
PROT_WRITE = 2 | |
PROT_EXEC = 4 | |
MAP_PRIVATE = 2 | |
MAP_ANONYMOUS = 0x20 | |
def conv64(dw): | |
return map(ord, struct.pack("<q" if dw < 0 else "<Q", dw)) | |
codes = (c_ubyte * 128) ( | |
0x41, 0x54, # push r12 | |
0x41, 0x55, # push r13 | |
0x53, # push rbx | |
0x49, 0xbd, 0x00, 0x00, 0x00, 0x00, 0x00, # mov r13, (long) | |
0x00, 0x00, 0x00, | |
0x49, 0xc7, 0xc4, 0x1a, 0x00, 0x00, 0x00, # mov r12, 0x1a | |
0x48, 0xc7, 0xc3, 0x41, 0x00, 0x00, 0x00, # mov rbx, 0x41 | |
0x49, 0x83, 0xfc, 0x00, # cmp r12, 0 | |
0x74, 0x0e, # je <end> | |
0x48, 0x89, 0xdf, # mov rdi, rbx | |
0x41, 0xff, 0xd5, # call r13 | |
0x48, 0xff, 0xc3, # inc rbx | |
0x49, 0xff, 0xcc, # dec r12 | |
0xeb, 0xec, # jmp <loop> | |
0x48, 0xc7, 0xc7, 0x0a, 0x00, 0x00, 0x00, # mov rdi, 0xa | |
0x41, 0xff, 0xd5, # call r13 | |
0x5b, # pop rbx | |
0x41, 0x5d, # pop r13 | |
0x41, 0x5c, # pop r12 | |
0xc3, # ret | |
) | |
buflen = len(codes) | |
p = mmap( | |
0, buflen, | |
PROT_READ | PROT_WRITE | PROT_EXEC, | |
MAP_PRIVATE | MAP_ANONYMOUS, | |
-1, 0 | |
) | |
getaddr = CFUNCTYPE(c_void_p, c_void_p)(lambda p: p) | |
f = CFUNCTYPE(c_void_p)(p) | |
codes[7:15] = conv64(getaddr(putchar)) | |
memmove(p, addressof(codes), buflen) | |
f() | |
munmap(p, buflen) |
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
.intel_syntax noprefix | |
.globl main | |
main: | |
mov r12, 26 | |
mov rbx, 0x41 | |
loop: | |
cmp r12, 0 | |
jz end | |
mov rdi, rbx | |
call putchar | |
inc rbx | |
dec r12 | |
jmp loop | |
end: | |
mov rdi, 0xa | |
call putchar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment