I write a virtual machine in C just for fun. Hmm, I know, but, fortunately, I am SO, so I hope no one dares :)
I wrote a very fast dirty virtual machine that reads the lines of (my) ASM and does everything. Right now I have only 3 commands: add , jmp , end . Everything is fine, and actually pretty cool when you can feed the lines (by doing something like write_line(&prog[1], "jmp", regA, regB, 0); and then running the program:
while (machine.code_pointer <= BOUNDS && DONE != true) { run_line(&prog[machine.cp]); }
I use the operation code lookup table (which may be inefficient but elegant) in C, and everything seems to be working fine.
My question is more about "best practice", but I think the answer is there. I force the virtual machine to read clean files (storing bytes in unsigned char[] ) and run the bytecode. My question is: is it VM's job to make sure that the bytecode is well-formed, or is it just a compiler task to make sure that the binary file that it spits out is well-formed?
I only ask about this because something will happen if someone edits the binary and curls (delete arbitrary parts of it, etc.). Obviously, the program will be a mistake and probably will not work. Is this even a VM problem? I’m sure that people understood the solutions to these problems much smarter than me;
c assembly programming-languages vm-implementation
David titarenco
source share