Executable memory

Alan alanpotteiger at gmail.com
Fri Oct 4 12:58:53 PDT 2013


Hello! Sorry if I appear to be posting a lot of questions (if you 
saw my LLVM one, thanks again for the help) I'm trying to throw 
some things together and learn a lot.

So I've been researching compilers and virtual machines recently, 
I've managed to implement some fairly good front ends and the 
past few weeks I've been looking into various options for back 
ends. Something that really interests me in virtual machine type 
systems.

I looked into things like the JVM and Google's V8 JavaScript 
engine and read that they directly execute native code--reason 
for their speed (at least V8).
So I did some research and found tons of code snippets and stuff 
for C/C++ (of course) but from that I managed to write this in D:
import std.stdio, core.memory;

void main()
{
	uint* opcodes = cast(uint*)GC.malloc(2);
	opcodes[0] = 0xCC;
	/* System call of some sort */
         /* Function pointer */
	void* delegate() func;
	func.ptr = cast(void*)opcodes;
         /* Call function */
	func();
	GC.free(opcodes);
}

This reserves some memory and places some CPU instructions, makes 
a function pointer to that block of memory, calls it, and frees 
the memory. The only problem is, is that most processors and OS's 
block direct memory execution like this, to my understanding 
there is some way to mark this block of memory executable. (Cause 
without doing so there is a segmentation fault) I've seen ways to 
do this in Linux and Windows in C/C++ but I have no clue where to 
start with this in D. If anyone has any ideas for at least Linux 
that would be great, thanks a lot everyone!


More information about the Digitalmars-d-learn mailing list