
x64asm: C++ Library with in-memory assembler, parser, and linker - tomcam
https://github.com/StanfordPL/x64asm
======
udp
How do W^X restrictions affect something like this? I assume there must be a
way to work around it given the prolificacy of JITing VMs, but interested in
the details. The last time I was fiddling with machine code in memory was
before W^X was a thing.

~~~
skissane
Use mprotect to change the access protection flags on a page of memory.
Assuming it is initially allocated as write but no execute
(PROT_READ|PROT_WRITE), write your machine code to it, then change it to read-
only and execute (PROT_READ|PROT_EXEC).

[http://man7.org/linux/man-
pages/man2/mprotect.2.html](http://man7.org/linux/man-
pages/man2/mprotect.2.html)

mprotect is part of POSIX, so works on Linux, macOS, etc.

The Windows equivalent is VirtualProtect

[https://msdn.microsoft.com/en-
us/library/windows/desktop/aa3...](https://msdn.microsoft.com/en-
us/library/windows/desktop/aa366898\(v=vs.85\).aspx)

~~~
jheriko
Annoyingly this doesn't apply on the most modern platforms... see windows
phone/universal etc as well as mobile platforms, consoles and all sorts of
embedded systems.

(ps I did a significant chunk of this for
[https://code.google.com/archive/p/fridgescript/](https://code.google.com/archive/p/fridgescript/)
back when this made real sense, I'm not just making hot air... this is quite a
mediocre result imo ... bedroom stuff from years ago..,)

~~~
my123
There are AsApp variants on UWP, if you claim the JIT permission.

------
chris_wot
I must be very dense today - what exactly is this? I'm a tad confused...

~~~
gens
Seems to be a slightly over-designed (amd64) assembler in the form of a
library, plus some (in-library) tools that are slightly cool but nobody will
use.

~~~
wtetzner
> tools that are slightly cool but nobody will use.

It seems like this project could be quite useful for learning, though.

------
blt
fuuuuh, I could have used this 2.5 years ago when I was messing around with a
JIT compiler for elementwise array operations. Started with a VM, wanted to
move to x86 but gave up due to difficulty of codegen.

~~~
ant6n
Why not use LLVM?

~~~
neopallium
I have tried using LLVM as a JIT for Lua [0] (7 years ago), but found the
codegen to be too slow [1] and use too much memory. It might have improved
since then, but I really don't think it can compete with LuaJit's highly
optimized tracing JIT.

Others [2] have also found LLVM to be unsuitable as a JIT.

0\. [https://github.com/Neopallium/llvm-
lua](https://github.com/Neopallium/llvm-lua)

1\. [http://stackoverflow.com/questions/4077396/llvm-jit-speed-
up...](http://stackoverflow.com/questions/4077396/llvm-jit-speed-up-
choices/4097930#4097930)

2\.
[http://stackoverflow.com/a/6833494/98107](http://stackoverflow.com/a/6833494/98107)

~~~
patrec
Why not use DynASM, especially since you are using the Lua eco-system already?

[https://luajit.org/dynasm.html](https://luajit.org/dynasm.html)

~~~
neopallium
LLVM-lua was just an experiment. I wanted to see what kind of performance
statically compiled Lua could achieve.

I didn't really need the JIT feature. But I did add support for it because Lua
code can dynamically generate/load more Lua code.

I just found that the runtime overhead (memory and stack space) of LLVM was
too high for my needs.

