2 ms·
Monkey Patching in Go (2015)
- weirdtech 5y agonice
- omginternets 5y agoThis seems like a related question: what is the high-level process for building a JIT compiler? Specifically, where does the output of the compiler actually get stored, how does it get marked as executable, and how does it get "fed" into program execution?
- duped 5y agoYou give executable permissions to a region of memory with machine code in it. The easy way to do this is mmap with PROT_EXEC then memcpy to it. Here's a good tutorial: https://eli.thegreenplace.net/2013/11/05/how-to-jit-an-introduction https://eli.thegreenplace.net/2013/11/05/how-to-jit-an-intro... 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.
- woodruffw 5y ago> This seems like a related question: what is the high-level process for building a JIT compiler? Specifically, where does the output of the compiler actually get stored, how does it get marked as executable, and how does it get "fed" into program execution? 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.
- dang 5y agoPast related threads: Monkey Patching in Golang (2015) - https://news.ycombinator.com/item?id=22442170 https://news.ycombinator.com/item?id=22442170 - Feb 2020 (22 comments) Monkey Patching in Go - https://news.ycombinator.com/item?id=9290917 https://news.ycombinator.com/item?id=9290917 - March 2015 (22 comments)
- david_allison 5y agoOn the front page today: Project includes a dependancy that has a license that forbids its use - https://news.ycombinator.com/item?id=28263721 https://news.ycombinator.com/item?id=28263721 - (125 comments)
- dang 5y agoAh yes - this one was clearly a follow-up to that one, which makes it less interesting (https://hn.algolia.com/?dateRange=all&page=0&prefix=true&sort=byDate&type=comment&query=follow-up%20by%3Adang https://hn.algolia.com/?dateRange=all&page=0&prefix=true&sor...).