A Simple Virtual Machine |
Written by Alexey Lyashko | ||||
Wednesday, 01 February 2012 | ||||
Page 3 of 3
Virtual MachineAlright, now we have some sort of a "compiler" and we can start working on the VM itself. First of all, let us define a structure, that would represent our virtual CPU: typedef struct _VCPU
In addition, you should implement at least some functions for the following:
In my case, the final source looks like this: (click to enlarge)
I decided not to cite the VM's code here as you should be able to write it yourself if the subject is interesting enough for you. Although, the code in this article does not contain any checks for correct return values, you should take care of them. SummaryAlthough, this article describes a trivial virtual machine which is only able to encode/decode a fixed length buffer, the concept itself may serve you well in software/data protection as hacking into VM is several times harder then cracking native code. One more thing to add. Our design allows us to call procedures provided by the pseudo executable, but there are several ways to allow the pseudo executable to "talk to us". The simplest (as it seems to me) is to implement interrupts. The encoded result would be: V{rrq2>Iqlrz?
Sample code for this article may be found here. This article originally appeared on Alexey Lyashko's blog System Programming and is reproduced here with kind permission of the author.
Comments
or email your comment to: comments@i-programmer.info
To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter.
|
||||
Last Updated ( Wednesday, 01 February 2012 ) |