Monkey Patching in Go (2015)
bou.ke
bou.ke
There is an issue with this on MacOS Big Sur where pages may not be writeable and executable in a hardened runtime. To get around that you need to write the machine code first, then mark it executable.
To execute the code you can cast it to a function pointer and call it.
There are a whole bunch of ways to do this, but a common one is to map a region as writable, build your JITted machine code into it, flip write and execute, and point your program counter at it. Typically the part that does the last part also sets up the return handling for when the JITted segment is finished.
If you're JITting entire functions at a time, you could instead use the dynamic symbol functionality that most OSes provide: build up your machine code (or other representation), convert it into an object file, load it in, and grab the function by its name.
Monkey Patching in Golang (2015) - https://news.ycombinator.com/item?id=22442170 - Feb 2020 (22 comments)
Monkey Patching in Go - https://news.ycombinator.com/item?id=9290917 - March 2015 (22 comments)
Project includes a dependancy that has a license that forbids its use - https://news.ycombinator.com/item?id=28263721 - (125 comments)