Repository navigation
[Mono][Interpreter] Zero-extend bool and char callback return values - #136590
mattleibow wants to merge 1 commit into
Conversation
Co-authored-by: Copilot <[email protected]>
|
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. |
|
Tagging subscribers to this area: @vitek-karas, @BrzVlad, @kotlarmilos |
|
@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. |
|
/azp run runtime-interpreter |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Fixes #136578
Summary
When a reverse P/Invoke callback (
[UnmanagedCallersOnly]or native-to-managed function pointer) returningbool(MONO_TYPE_BOOLEAN) orchar(MONO_TYPE_CHAR) is executed by the Mono interpreter on 64-bit targets:n2i) exit (interp_entry_from_trampoline->mono_arch_set_native_call_context_ret->interp_frame_arg_to_datawithindex == -1) orinterp_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]ordata->res).stackval_to_data_sign_extpreviously handledMONO_TYPE_I1,MONO_TYPE_U1,MONO_TYPE_I2,MONO_TYPE_U2,MONO_TYPE_I4, andMONO_TYPE_U4, but omittedMONO_TYPE_BOOLEANandMONO_TYPE_CHAR, falling through tostackval_to_data.stackval_to_dataperforms a narrow 1-byte (strb) or 2-byte (strh) store intoccontext->gregs[0], leavingbits 8..63(forbool) andbits 16..63(forchar) dirty with the first incoming argument (x0on ARM64) or uninitialized stack memory (CallContexton x64).booland 2-byteuint16_t/char16_treturn values must be zero-extended to 32 bits (w0) by the callee; Apple Clang compiles C/C++ callers using 32-bitcbz w0/cmp w0, ...without masking upper bits, causingfalse(0) returns to be misread astruewheneverx0had non-zero bits inbits 8..31.Changes
src/mono/mono/mini/interp/interp.c(stackval_to_data_sign_ext):MONO_TYPE_BOOLEANalongsideMONO_TYPE_U1(zero-extending 1-byte unsigned value tomono_u).MONO_TYPE_CHARalongsideMONO_TYPE_U2(zero-extending 2-byte unsigned value tomono_u).src/tests/Interop/UnmanagedCallersOnly/):UnmanagedCallersOnlyDisabledRuntimeMarshallingTestand native helpersCallManagedProcReturningBool/CallManagedProcReturningCharinUnmanagedCallersOnlyDll.cppthat dirty stack memory and pass0x123456789ABCDEF0as the first argument to[UnmanagedCallersOnly]callbacks returningbool(false) andchar('A'), covering both default calling convention andCallConvSwift.