-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Enable R2R precompilation of blittable ObjC P/Invokes #124770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
davidnguyen-tech
wants to merge
14
commits into
dotnet:main
Choose a base branch
from
davidnguyen-tech:feature/r2r-objc-pinvoke-stubs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
01a7fe1
Add ObjectiveCMarshal.ThrowPendingExceptionObject helper
davidnguyen-tech b41aa09
Emit ThrowPendingExceptionObject in R2R P/Invoke IL stubs
davidnguyen-tech e04bc85
Move ObjC pending exception check to PInvokeILStubMethodIL
davidnguyen-tech d289c6c
Add review assessment and implementation plan for PR #124770
PureWeen d390ab2
Update PR #124770 analysis with LibraryImport and P/Invoke inlining f…
PureWeen 9e02c64
Update PR #124770 notes for March 17 force-push
PureWeen d1ec12d
Add R2R map validation test for ObjC P/Invoke stubs
PureWeen 76f4347
Add R2R map validation test for ObjC P/Invoke stubs
PureWeen 1716e40
Improve R2R test guidance and enforce negative verification
PureWeen d747f89
Add R2R domain rules to code-review skill
PureWeen 0e20bbc
Merge branch 'main' into feature/r2r-objc-pinvoke-stubs
kotlarmilos 02711b6
Address review comments
kotlarmilos f4f554f
Drop unrelated .github and README changes
kotlarmilos 23d441f
Pre-register ObjectiveCMarshal references for non-composite R2R
kotlarmilos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.InteropServices.ObjectiveC; | ||
|
|
||
| // Validates that blittable objc_msgSend P/Invoke stubs are precompiled into the R2R image | ||
| // (via the crossgen2 --map output) and that the emitted stub actually executes the | ||
| // pending-exception path at runtime. | ||
| public static unsafe class ObjCPInvokeR2RTest | ||
| { | ||
| [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")] | ||
| private static extern IntPtr objc_msgSend(IntPtr receiver, IntPtr selector); | ||
|
|
||
| [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")] | ||
| private static extern IntPtr objc_msgSend_2(IntPtr receiver, IntPtr selector, IntPtr arg1); | ||
|
|
||
| [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend_stret")] | ||
| private static extern void objc_msgSend_stret(IntPtr receiver, IntPtr selector); | ||
|
|
||
| private sealed class PendingException : Exception | ||
| { | ||
| public PendingException(string message) : base(message) { } | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly] | ||
| private static IntPtr MsgSendCallback(IntPtr inst, IntPtr sel) | ||
| { | ||
| ObjectiveCMarshal.SetMessageSendPendingException(new PendingException(nameof(MsgSendCallback))); | ||
| return IntPtr.Zero; | ||
| } | ||
|
|
||
| public static int Main() | ||
| { | ||
| if (!ValidateMapFile()) | ||
| return 1; | ||
|
|
||
| if (!ValidatePendingExceptionPropagates()) | ||
| return 1; | ||
|
|
||
| Console.WriteLine("PASSED: ObjC P/Invoke stubs are precompiled and the pending-exception path executes."); | ||
| return 100; | ||
| } | ||
|
|
||
| private static bool ValidateMapFile() | ||
| { | ||
| string mapFile = Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, "map"); | ||
| if (!File.Exists(mapFile)) | ||
| { | ||
| Console.WriteLine($"FAILED: Map file not found at {mapFile}"); | ||
| return false; | ||
| } | ||
|
|
||
| // Only MethodWithGCInfo entries prove the stub was compiled into the image. | ||
| string[] compiledStubs = File.ReadAllLines(mapFile) | ||
| .Where(l => l.Contains("objc_msgSend") && l.Contains("MethodWithGCInfo")) | ||
| .ToArray(); | ||
|
|
||
| string[] expectedStubs = new[] | ||
| { | ||
| "__objc_msgSend ", | ||
| "__objc_msgSend_2 ", | ||
| "__objc_msgSend_stret ", | ||
| }; | ||
|
|
||
| bool allFound = true; | ||
| foreach (string expected in expectedStubs) | ||
| { | ||
| bool found = compiledStubs.Any(l => l.Contains(expected)); | ||
| Console.WriteLine($" {(found ? "OK" : "MISSING")}: {expected.Trim()}"); | ||
| if (!found) | ||
| allFound = false; | ||
| } | ||
|
|
||
| if (!allFound) | ||
| Console.WriteLine("FAILED: Not all objc_msgSend P/Invoke stubs were precompiled into the R2R image."); | ||
|
|
||
| return allFound; | ||
| } | ||
|
|
||
| private static bool ValidatePendingExceptionPropagates() | ||
| { | ||
| IntPtr callback = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, IntPtr>)&MsgSendCallback; | ||
| ObjectiveCMarshal.SetMessageSendCallback(MessageSendFunction.MsgSend, callback); | ||
|
Check failure on line 89 in src/tests/readytorun/ObjCPInvokeR2R/ObjCPInvokeR2R.cs
|
||
|
|
||
| try | ||
| { | ||
| objc_msgSend(IntPtr.Zero, IntPtr.Zero); | ||
| } | ||
| catch (PendingException ex) when (ex.Message == nameof(MsgSendCallback)) | ||
| { | ||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"FAILED: unexpected exception from objc_msgSend: {ex.GetType()} - {ex.Message}"); | ||
| return false; | ||
| } | ||
|
|
||
| Console.WriteLine("FAILED: objc_msgSend returned without throwing the pending exception."); | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator> | ||
| <CrossGenTest>false</CrossGenTest> | ||
| <!-- https://github.com/dotnet/runtime/issues/73138 --> | ||
| <IlasmRoundTripIncompatible>true</IlasmRoundTripIncompatible> | ||
| <!-- Only run on Apple platforms where ShouldCheckForPendingException recognizes objc_msgSend --> | ||
| <CLRTestTargetUnsupported Condition="'$(TargetsOSX)' != 'true'">true</CLRTestTargetUnsupported> | ||
| <!-- Skip when sanitizers are enabled — crossgen2 invocation requires non-sanitized jitinterface --> | ||
| <CLRTestTargetUnsupported Condition="'$(EnableNativeSanitizers)' != ''">true</CLRTestTargetUnsupported> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ObjCPInvokeR2R.cs" /> | ||
|
kotlarmilos marked this conversation as resolved.
|
||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="$(TestLibraryProjectPath)" /> | ||
| </ItemGroup> | ||
| <PropertyGroup> | ||
| <CLRTestBatchPreCommands><![CDATA[ | ||
| $(CLRTestBatchPreCommands) | ||
|
|
||
| REM ObjC P/Invoke R2R test is macOS-only — batch commands are a no-op placeholder | ||
| echo ObjC P/Invoke R2R test is not supported on Windows | ||
| exit /b 0 | ||
| ]]></CLRTestBatchPreCommands> | ||
| <CLRTestBashPreCommands><![CDATA[ | ||
| $(CLRTestBashPreCommands) | ||
|
|
||
| # Suppress some DOTNET variables for the duration of Crossgen2 execution | ||
| export -n DOTNET_GCName DOTNET_GCStress DOTNET_HeapVerify DOTNET_ReadyToRun | ||
|
|
||
| mkdir -p IL_DLLS | ||
|
|
||
| if [ ! -f IL_DLLS/ObjCPInvokeR2R.dll ] | ||
| then | ||
| cp ObjCPInvokeR2R.dll IL_DLLS/ObjCPInvokeR2R.dll | ||
| fi | ||
| if [ ! -f IL_DLLS/ObjCPInvokeR2R.dll ] | ||
| then | ||
| echo Failed to copy ObjCPInvokeR2R.dll to IL_DLLS | ||
| exit 1 | ||
| fi | ||
|
|
||
| "$CORE_ROOT"/crossgen2/crossgen2 --map --inputbubble -r:"$CORE_ROOT"/*.dll -o:ObjCPInvokeR2R.dll IL_DLLS/ObjCPInvokeR2R.dll | ||
|
|
||
| __cgExitCode=$? | ||
| if [ $__cgExitCode -ne 0 ] | ||
| then | ||
| echo Crossgen2 failed with exitcode: $__cgExitCode | ||
| exit 1 | ||
| fi | ||
| if [ ! -f ObjCPInvokeR2R.map ] | ||
| then | ||
| echo FAILED: crossgen2 did not produce map file | ||
| exit 1 | ||
| fi | ||
|
|
||
| export DOTNET_GCName DOTNET_GCStress DOTNET_HeapVerify DOTNET_ReadyToRun | ||
| ]]></CLRTestBashPreCommands> | ||
| </PropertyGroup> | ||
| </Project> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO for me: Look into this
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kotlarmilos What is the purpose of this ? Did you check that it actually produces an error ? It is completely normal for a call to be made while the IL stack is not empty. I don't believe copilot's analysis makes any sense.