guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Executing Arbitrary Machine Code in Guile


From: Elzair
Subject: Executing Arbitrary Machine Code in Guile
Date: Fri, 21 Aug 2015 19:31:56 -0400

The other day I came across a page on Rosetta Code showing how to directly execute x86 instructions across several languages: http://rosettacode.org/wiki/Machine_code

For example, here is the code for Racket.
#lang racket/base
 
(require ffi/unsafe)
 
; set up access to racket internals
(define scheme-malloc-code
(get-ffi-obj 'scheme_malloc_code #f (_fun (len : _intptr) -> _pointer)))
(define scheme-free-code
(get-ffi-obj 'scheme_free_code #f (_fun _pointer -> _void)))
 
(define opcodes '(139 68 36 4 3 68 36 8 195))
 
(define code (scheme-malloc-code 64))
 
(for ([byte opcodes]
[i (in-naturals)])
(ptr-set! code _ubyte i byte))
 
(define function (cast code _pointer (_fun _ubyte _ubyte -> _ubyte)))
 
(function 7 12)
 
(scheme-free-code code)

Is this possible in Guile (with, say, the FFI)?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]