3 ms·
Machine code isn't some kind of magical idea. It is just bytes and you can craft the bytes any way you want. In my case I wrote my own compiler that generated
by onetimeuse92304 3y ago
Machine code isn't some kind of magical idea. It is just bytes and you can craft the bytes any way you want.
In my case I wrote my own compiler that generated machine code bytes based on my needs.
The compiler wasn't at all complicated. It was not general compiler that would have to be able to deal with everything you could throw at it. It only supported one model of CPU. It did not require any optimisation mechanisms because it was assumed the instructions it received were already optimised by high level logic that generated them. It would only support very small number of instructions or mechanisms, it was mostly manipulating registers and doing branches and jumps. It produced small pieces of code to be inserted into larger C program mostly, so that this C program did not have to run decisions on what to do based on something else.
You can also use SBCL to emit any vops you want to execute as part of your Lisp program. So if you want to have a piece of crafted code to run as part of your Lisp program (for example to do some complicated operation that would be inefficient in lisp) you can also do it. I actually used vops to do some low level stuff but that was another matter, most of the time I was just outputting a buffer with crafted native machine code which would then be sent to the receiving thread (core) to insert and execute it for the next market signal.
- dajt 3y agoHow did you inject the machine code into a running program on anything near a modern architecture? Unless you had your own OS, wouldn't code segments be RO and data segments not executable? I don't know enough about any processor created in the last 30 years to know if running bare metal without a commercial OS would allow you to not have those constraints.
- silasb 3y agoCan't you use `mprotect` to allow you to write to memory and execute? https://github.com/Frodox/execute-machine-code-from-memory/tree/master https://github.com/Frodox/execute-machine-code-from-memory/t... has several examples of how to do it. EDIT: https://www.kvakil.me/posts/2022-10-13-optimizing-mprotect-in-luajit-with-pkeys.html https://www.kvakil.me/posts/2022-10-13-optimizing-mprotect-i... is another good link that explains this.
- onetimeuse92304 3y agoIf you can modify your operating system you can do pretty much anything you want (Thanks, Linus!)