Created
September 29, 2020 05:39
-
-
Save weskerfoot/3e8751d25925ef88183f4e54f3f43b06 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
import posix, system, os, memfiles | |
var program: array[0..4, uint8] = [ | |
# mov eax, 42 (0x2a) | |
0xb8.uint8, 0x2a.uint8, 0x00.uint8, 0x00.uint8, | |
# ret | |
0xc3.uint8 | |
] | |
const kProgramSize: int = program.sizeof | |
type JitFunction = proc (): cint {. cdecl .} | |
when isMainModule: | |
var nilMemfile: MemFile | |
nilMemfile.handle = -1 | |
var memory: pointer = mapMem(m=nilMemfile, | |
mode=fmReadWrite, | |
mappedSize=kProgramSize, | |
mapFlags=MAP_ANONYMOUS or MAP_PRIVATE) | |
moveMem(memory, program[0].addr, kProgramSize) | |
assert memory.mprotect(kProgramSize, PROT_EXEC) == 0, "mprotect failed" | |
var function: JitFunction = (cast[ptr JitFunction](memory.addr))[] | |
discard function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment