Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Created September 29, 2020 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weskerfoot/3e8751d25925ef88183f4e54f3f43b06 to your computer and use it in GitHub Desktop.
Save weskerfoot/3e8751d25925ef88183f4e54f3f43b06 to your computer and use it in GitHub Desktop.
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