Support calls with unaligned stack on x86
As far as I can see, on x86, this library assumes that target functions will always be called with %rsp properly aligned.
However, some of the functions I'd like to hook are called with %rsp unaligned. If transit function is not used, it jumps to the hook function and the stack is still unaligned. This may cause issues.
It gets even worse when the transit procedure is used, as it contains movdqa instructions which immediately segfault.
From what I can see, to support this, one needs to:
- force transit usage
- add code to check and fix alignment in the transit function.
Is this right?
I tried following the above steps:
- I use the new
prehookfunctionality which always uses transit. - I made some changes to transit function for
x86_64-sysv
It seems to be working for me.
Fixing alignment for hook functions isn't that simple. (1) Fixed alignment must be back to the original before returning to the caller. (2) Hook functions get incorrect arguments when alignment is fixed and the arguments are passed on the stack.
On the other hand, fixing alignment for prehook is simple as you did.
- I made some changes to transit function for x86_64-sysv
I would do it as follows.
- add
or $0xfffffffffffffff0, %rspjust aftersub $0xd0, %rsp. - replace all
movdqawithmovdqu.