Open madLifeZPC opened 2 years ago
There is a lot of work that needs to be done to fix this problem.
Indeed. I did a naive try and managed to change the assembly logic to match the latest mechanism of passing arguments and successfully trigger the reflect function and capture the method receiver, however, the current challenge is reflect.callReflect itself does not seem to handle the case for method call, whose first argument is always the receiver object. This is ease to hack before go1.17 since all you need to do is just add a 8 offset to the stack frame, after 1.17, it seems to be very challenging to hack the register. There is no other way I can come up with except for copy the reflect internal logic and ignore the first argument, but that also seems to be very fragile and result in unforeseen problems. Another thing, this solution is also slow due to the heavy usage of reflection, comparing with calling original function directly, it has 4 times latency. I am currently looking at other solutions.
Indeed. I did a naive try and managed to change the assembly logic to match the latest mechanism of passing arguments and successfully trigger the reflect function and capture the method receiver, however, the current challenge is reflect.callReflect itself does not seem to handle the case for method call, whose first argument is always the receiver object. This is ease to hack before go1.17 since all you need to do is just add a 8 offset to the stack frame, after 1.17, it seems to be very challenging to hack the register. There is no other way I can come up with except for copy the reflect internal logic and ignore the first argument, but that also seems to be very fragile and result in unforeseen problems. Another thing, this solution is also slow due to the heavy usage of reflection, comparing with calling original function directly, it has 4 times latency. I am currently looking at other solutions.
You are right!
Did you find any other solutions?
Indeed. I did a naive try and managed to change the assembly logic to match the latest mechanism of passing arguments and successfully trigger the reflect function and capture the method receiver, however, the current challenge is reflect.callReflect itself does not seem to handle the case for method call, whose first argument is always the receiver object. This is ease to hack before go1.17 since all you need to do is just add a 8 offset to the stack frame, after 1.17, it seems to be very challenging to hack the register. There is no other way I can come up with except for copy the reflect internal logic and ignore the first argument, but that also seems to be very fragile and result in unforeseen problems. Another thing, this solution is also slow due to the heavy usage of reflection, comparing with calling original function directly, it has 4 times latency. I am currently looking at other solutions.
You can create new function type with receiver via reflect.FuncOf
, and then pass resulting type to reflect.MakeFunc
. This way the registers will be correctly aligned with function arguments.
Hi,
It seems this solution only works on Go below or equal to 1.16. On 1.17 or above, it panics:
Did some exploration, it turns out this solution relies on assembly to acquire the method parameters address through FP and then pass it to the call, would it be affected by the new convention of passing arguments through register instead of stack mentioned in https://go.dev/doc/go1.17#compiler? After some debugging, I found on go1.17 the
var fc = *(**fnContext)(ptr)
will always result in an invalid value, which means the pointer is actually wrong. Is it possible to fix this issue? Not sure whether it is still possible to acquire all arguments in asm on 1.17 and above.