← 输入框|github.com|刷新
Skip to content

Fix mono x86 struct returns - #368

Merged
nike4613 merged 4 commits into
MonoMod:reorganizefrom
xADDBx:fix-mono-x86-struct-returns
Nov 29, 2026
Merged

nike4613 merged 4 commits into
MonoMod:reorganizefrom
xADDBx:fix-mono-x86-struct-returns

Conversation

@xADDBx

@xADDBx xADDBx commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 4 as 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.

  • Assuming 1MiB Stack Space
  • Assuming 0.5MiB are already used
public DateTime IAmHookedGetCurrentTime() { /* */ }

public void ILoop() {
    for (int i = 0; i < SomeNumber; i++) {
        // Each call leaks 4 Bytes of stack space
        IAmHookedGetCurrentTime();
    }
    // Once the method returns, i.e. the program jumps to ebp, the stack space is "recovered"
}

=> 512KiB of stack space are still free when ILoop is called. If SomeNumber is ~131000, that space will all be leaked, so a StackOverflow will occur.

Technical explanation:

Issues with other fixes:

  • We can't manually emit a ret 4 via IL
  • We don't have any IL that allows us to modify the stack pointer directly
  • Forcing the ret 4 by making the glue returning a struct on instance struct methods doesn't work; because a struct gets its instance passed as ref this. And a ref isn'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 the this pointer for the original method.

@nike4613 nike4613 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/MonoMod.Core/Platforms/Abi.cs Outdated
Comment on lines +102 to +107
/// <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; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda hate this. I would almost rather an (optional) IRuntime.EmitAbiFixupEpilog or something over this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xADDBx xADDBx Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

This comment has been minimized.

Comment on lines +77 to +83
/// <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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

This comment has been minimized.

@nike4613
nike4613 merged commit 9310bc4 into MonoMod:reorganize Nov 29, 2026
194 of 195 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants