
Monkey Patching in Go - Sirupsen
http://bouk.co/blog/monkey-patching-in-go/
======
GauntletWizard
This is awful, dangerous, and compiler designers and writers will yell at you.
Well done :)

I've always wanted a way to monkey-patch otherwise statically and well-bound
code, but only for testing. In C, it's fairly easy to set up preprocessor
rules to do so, though also litters your primary code with test specific
stuff. I have mostly focused on how to integrate this into compiler logic;
Building some functionality to replace specified symbols in the build phase.
Simply replacing the function pointers in the runtime is brilliant, if more
than a little hacky and prone to problems.

I look forward to playing around with this.

------
kylequest
This is not monkey patching. This is the good old function/api "hooking"
that's pretty common in the compiled language world:
[http://en.wikipedia.org/wiki/Hooking](http://en.wikipedia.org/wiki/Hooking)

Monkey patching is a less invasive version of hooking used in dynamic
languages. Bottom line: if you are dealing with assembly it's no longer monkey
patching :-)

~~~
tptacek
I feel like I'm pretty familiar with "monkey patching" as practiced in
Rubyland, and I'm a systems C programmer from the 90s. Can you help me
understand the distinction you're drawing here? I don't have an understanding
of "monkey patching" that involves how "invasive" it is; in fact, a repeated
complaint about Ruby is how _uninvasive_ it makes monkey patches, since the
very concept of monkey patching breaks assumptions other programmers might
rely on it.

~~~
haberman
I think the important distinction is that Ruby/Python monkey-patching work
within the defined semantics of the language, whereas this is targeting
implementation-specific details of one particular runtime.

~~~
tedunangst
At the end of the day however, it's still patching code you don't own. Doesn't
the term strongly imply modifications outside the scope of the author's
intentions? Even in ruby and python, monkey patching is rarely the
_documented_ means to modify a library's behavior.

------
4ad
This does not work for any non-trivial function because of missing stack map
for the GC and stack copier.

------
mpnordland
So, this is a step towards implementing plugins for go right? It seems to me
like this could be used to implement dynamic code loading.

~~~
andrewchambers
No, this is just a hack that is probably broken because the runtime doesn't
know about the patched functions. The Go team are working on proper loadable
modules.

~~~
bouk
How is it broken?

~~~
andrewchambers
Well, in the future the go runtime might require more detailed stack info
other things to enable future things like concurrent garbage collection. Also,
different implementations of Go have different requirements and calling
conventions.

------
zik
This is admirable work in the pursuit of evil.

------
marvy
Is this safe for such short functions? What if the patch takes more space than
the original code?

~~~
bouk
All functions take up at least 16 bytes, for alignment reasons I assume. The
space that isn't used just gets filled with zeroes.

The patch is 12 bytes

~~~
marvy
On all platforms?

------
infogulch
Could this be expanded to hot-swap code as you write it?

