Skip to content

[Mono][Interpreter] Zero-extend bool and char callback return values - #136590

Open
mattleibow wants to merge 1 commit into
dotnet:mainfrom
mattleibow:mattleibow-effective-spork
Open

mattleibow wants to merge 1 commit into
dotnet:mainfrom
mattleibow:mattleibow-effective-spork

Conversation

@mattleibow

Copy link
Copy Markdown
Member

Fixes #136578

Summary

When a reverse P/Invoke callback ([UnmanagedCallersOnly] or native-to-managed function pointer) returning bool (MONO_TYPE_BOOLEAN) or char (MONO_TYPE_CHAR) is executed by the Mono interpreter on 64-bit targets:

  • On native-to-interpreter (n2i) exit (interp_entry_from_trampoline -> mono_arch_set_native_call_context_ret -> interp_frame_arg_to_data with index == -1) or interp_entry, stackval_to_data_sign_ext (src/mono/mono/mini/interp/interp.c) is called to store the return value into register-width return storage (&ccontext->gregs[0] or data->res).
  • stackval_to_data_sign_ext previously handled MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_I4, and MONO_TYPE_U4, but omitted MONO_TYPE_BOOLEAN and MONO_TYPE_CHAR, falling through to stackval_to_data.
  • stackval_to_data performs a narrow 1-byte (strb) or 2-byte (strh) store into ccontext->gregs[0], leaving bits 8..63 (for bool) and bits 16..63 (for char) dirty with the first incoming argument (x0 on ARM64) or uninitialized stack memory (CallContext on x64).
  • Under Apple's ARM64 ABI (Darwin), 1-byte bool and 2-byte uint16_t/char16_t return values must be zero-extended to 32 bits (w0) by the callee; Apple Clang compiles C/C++ callers using 32-bit cbz w0 / cmp w0, ... without masking upper bits, causing false (0) returns to be misread as true whenever x0 had non-zero bits in bits 8..31.

Changes

  • src/mono/mono/mini/interp/interp.c (stackval_to_data_sign_ext):
    • Handle MONO_TYPE_BOOLEAN alongside MONO_TYPE_U1 (zero-extending 1-byte unsigned value to mono_u).
    • Handle MONO_TYPE_CHAR alongside MONO_TYPE_U2 (zero-extending 2-byte unsigned value to mono_u).
  • Tests (src/tests/Interop/UnmanagedCallersOnly/):
    • Add UnmanagedCallersOnlyDisabledRuntimeMarshallingTest and native helpers CallManagedProcReturningBool / CallManagedProcReturningChar in UnmanagedCallersOnlyDll.cpp that dirty stack memory and pass 0x123456789ABCDEF0 as the first argument to [UnmanagedCallersOnly] callbacks returning bool (false) and char ('A'), covering both default calling convention and CallConvSwift.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @vitek-karas, @BrzVlad, @kotlarmilos
See info in area-owners.md if you want to be subscribed.

@BrzVlad

BrzVlad commented Oct 28, 2026

Copy link
Copy Markdown
Member

@mattleibow was this hit by a customer ? I'm wondering if it makes sense to backport to .net10 if that's not the case. There is normally pushback against backports that address issues which aren't regressions or reported by a customer.

@BrzVlad

BrzVlad commented Oct 28, 2026

Copy link
Copy Markdown
Member

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@mattleibow

Copy link
Copy Markdown
Member Author

I hit it in SkiaSharp. But I am working around it now by replacing bool with byte.

But this causes OOB reads.

using Xunit;
using TestLibrary;

[assembly: DisableRuntimeMarshalling]

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.

Rather than creating a new test project, maybe this could live inside already existing tests in https://github.com/dotnet/runtime/tree/main/src/tests/Interop/DisabledRuntimeMarshalling ? cc @AaronRobinsonMSFT if you have any observations around this test case.

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.

Thanks @BrzVlad.

@mattleibow I would prefer tests involving DisableRuntimeMarshalling be integrated into src/tests/Interop/DisabledRuntimeMarshalling. This looks like there are Swift scenarios too and that has its own area if it is specific to the Swift ABI. This sort of extension logic is tricky and I appreciate how it is being tested here, but it is also quiet fragile to maintain. I thought we tried to test something like this a while back and determined it wasn't worth the effort, but @jkoritzinsky might recall better than I.

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 see anything here that's swift specific (in fact, the native code doesn't call the callback with the swift calling convention in any case, so we have a callconv mismatch in this test, which is not ok).

I'd rather we add these tests to DisableRuntimeMarshalling than the UCO test suite.

And if we need a swift test case, the test should be under the swift calling convention tests and the native assets should call the callback with the swift callconv.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mono][Interpreter] Reverse P/Invoke / [UnmanagedCallersOnly] returning bool or char does not zero-extend return register on Apple ARM64

4 participants