Repository navigation
Fix mono x86 struct returns - #368
Conversation
nike4613
left a comment
There was a problem hiding this comment.
I don't think I like this approach, it seems really hacky. With CoreCLR runtime-async, we have some vaguely similar funny ABI behavior with the continuation return going in a separate return register, and we deal with it there by emitting a mandatory tailcall. Maybe we can do something similar for this case?
| /// <summary> | ||
| /// The method to jump to in order to return and pop the return buffer pointer off the stack, if functions are expected to do so; | ||
| /// <see langword="null"/> otherwise. | ||
| /// </summary> | ||
| public MethodInfo? PopReturnBuffer { get; init; } | ||
|
|
There was a problem hiding this comment.
I kinda hate this. I would almost rather an (optional) IRuntime.EmitAbiFixupEpilog or something over this.
There was a problem hiding this comment.
So if I understand this right; instead of jumping to a patched method with ret 4 if the Abi requires it; You'd rather have the runtime patch the epilog of each affected method to turn ret into ret 4 where necessary?
There was a problem hiding this comment.
That wasn't what I had in mind, no. I meant that that method would be called to emit the IL of the epilog for the ABI fixup instead of including the specifics of this case in the general code.
There was a problem hiding this comment.
So instead adding the conditional to GetRealDetourTarget, there's a single call to EmitAbiFixupEpilog on the runtime (which is a noop on all but mono); and on mono it would check whether the current system is x86 (how? do we really want to repeat the check which abi init already does?). If it is it would emit the jmp to the method like here.
Roughly this? That would prevent having to add something to the Abi
There was a problem hiding this comment.
Something like that, yes. I don't like this special casing in ABI, especially when it emits invalid(!) IL that is nonetheless required for the one runtime where the special case is required.
There was a problem hiding this comment.
Is this version roughly what you imagined?
It emits dead code for the original case (the original ret); but it's the cleanest implementation for the general path; and since we're already doing invalid IL, that bit of dead code shouldn't cause a difference (for the x86 mono struct return path)
There was a problem hiding this comment.
I think we can easily deal with this with my suggested feature flag, by requiring that if the flag is set, the function always emits whatever the correct epilog is (including returning retbuf if needed). I don't mind this though, since Mono doesn't really care about dead IL.
This comment has been minimized.
This comment has been minimized.
| /// <summary> | ||
| /// Emits any runtime-specific IL needed to return from an ABI fixup method, if there is any. | ||
| /// </summary> | ||
| /// <param name="il">The <see cref="ILProcessor"/> of the ABI fixup method.</param> | ||
| /// <param name="abi">The <see cref="MethodAbi"/> which the ABI fixup method implements.</param> | ||
| void EmitAbiFixupEpilog(ILProcessor il, MethodAbi abi); | ||
|
|
There was a problem hiding this comment.
I was originally imagining this being behind a feature flag, so we can avoid the interface dispatch for the cases where this is a nop, but I don't especially mind this as-is.
I mentioned this before, but the actual x86 mono ABI always sends struct returns through the return buffer. In my previous PR #362 I excluded structs < 8 bytes to keep previous behaviour, because current x86 ABI is not perfectly handled, so a stack drift would occur if those cases were to be routed through the glue. Since the struct methods which do need a return buffer currently don't use the glue, current upstream behaviour basically returns the wrong value with the X86Classifier. That problem is fixed with this PR.
I originally just wanted to accept the issue caused by this (explained below), but while writing this PR, I did manage to think of a solution, which is jumping to another method which just has
ret 4as body. It cheats a little by patching the native body of the method since I couldn't figure out how else to do that. But that's the cleanest solution I could think of.On x86 Mono. When you detour a method which returns a struct. Each call to the detoured method will leak 4 bytes of stack space until you return from the current frame.
E.g.
=> 512KiB of stack space are still free when ILoop is called. If
SomeNumberis ~131000, that space will all be leaked, so a StackOverflow will occur.Technical explanation:
ret 4, thereby freeing 4 Bytes of stack. https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/mini/mini-x86.c#L531-L533sub esp, 4; i.e. allocating 4 Bytes on the stack. https://github.com/mono/mono/blob/0f53e9e151d92944cacab3e24ac359410c606df6/mono/mini/mini-x86.c#L3218-L3221ret 4; so those 4 bytes are never popped. But the caller will still allocate 4 bytes per call, thereby leaking those 4 bytes of stack space.espbased onebp, the "leaked" stack space is recovered without issueebp; so a moved stack pointer does not influence those.esp; so that still works without issuesIssues with other fixes:
ret 4via ILret 4by making the glue returning a struct on instance struct methods doesn't work; because a struct gets its instance passed asref this. And arefisn't a reference in mono (it's a managed pointer). Meaning Mono would want a layout of [buffer, ref this, args]. But mono itself obviously gets [this, buffer, args] because it's thethispointer for the original method.