-
Notifications
You must be signed in to change notification settings - Fork 577
[TrimmableTypeMap] Refuse [JniAddNativeMethodRegistrationAttribute] with XA4251 #11274
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
882ce7c
Use trimmable Java proxy runtime sources
simonrozsival 0644b70
Address trimmable runtime review comments
simonrozsival dc13b42
Use trimmable virtual-constructor fixtures
simonrozsival b1d1947
Merge branch 'main' into trimmable-java-proxy-object
simonrozsival 80d4af4
Address Java proxy review comments
simonrozsival ac015d1
Merge remote-tracking branch 'origin/main' into trimmable-java-proxy-…
simonrozsival 2cb67ee
Merge branch 'trimmable-java-proxy-object' into trimmable-virtual-con…
simonrozsival 43bc2ed
Simplify trimmable runtime artifacts
simonrozsival 92a4836
Merge main into trimmable-java-proxy-object to pick up CI fixes
simonrozsival 5a7e957
Merge trimmable-java-proxy-object into trimmable-virtual-constructor …
simonrozsival 244c81f
Merge remote-tracking branch 'origin/main' into trimmable-virtual-con…
simonrozsival f863ba1
Drop unrelated obsolete-jar cleanup from java-runtime.targets and Tri…
simonrozsival 7a6dde8
Refuse [JniAddNativeMethodRegistrationAttribute] in the trimmable typ…
simonrozsival 5f1ce53
Move XA4251 reporting into JavaPeerScanner
simonrozsival 6b47cef
Restore tests/Mono.Android-Tests/Java.Interop-Tests/java-trimmable/ne…
simonrozsival 3dfd44e
Exclude InvokeVirtualFromConstructor fixtures for trimmable
simonrozsival 7d607c5
Detect [JniAddNativeMethodRegistrationAttribute] on non-peer types too
simonrozsival 1a71bb3
Skip per-method XA4251 walk when assembly doesn't reference the attri…
simonrozsival 414c224
Merge branch 'main' into trimmable-virtual-constructor
simonrozsival c9c0800
Check JNI registration attribute namespace
simonrozsival fc71573
Merge branch 'main' into trimmable-virtual-constructor
jonathanpeppers 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
121 changes: 121 additions & 0 deletions
121
...roid-Tests/Java.Interop-Tests/Java.Interop-trimmable/CallVirtualFromConstructorDerived.cs
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,121 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
jonathanpeppers marked this conversation as resolved.
Outdated
|
||
|
|
||
| using Java.Interop; | ||
|
|
||
| namespace Java.InteropTests | ||
| { | ||
| [JniTypeSignature (CallVirtualFromConstructorDerived.JniTypeName, GenerateJavaPeer=false)] | ||
| public class CallVirtualFromConstructorDerived : CallVirtualFromConstructorBase { | ||
| new internal const string JniTypeName = "net/dot/jni/test/CallVirtualFromConstructorDerived"; | ||
| static readonly JniPeerMembers _members = new JniPeerMembers (JniTypeName, typeof (CallVirtualFromConstructorDerived)); | ||
|
|
||
| [JniAddNativeMethodRegistrationAttribute] | ||
| static void RegisterNativeMembers (JniNativeMethodRegistrationArguments args) | ||
| { | ||
| args.Registrations.Add (new JniNativeMethodRegistration ("calledFromConstructor", "(I)V", (CalledFromConstructorMarshalMethod)CalledFromConstructorHandler)); | ||
| } | ||
|
|
||
| public override JniPeerMembers JniPeerMembers { | ||
| get {return _members;} | ||
| } | ||
|
|
||
| int calledValue; | ||
|
|
||
| public bool InvokedConstructor; | ||
|
|
||
| [Register (".ctor", "(I)V", "")] | ||
| public CallVirtualFromConstructorDerived (int value) | ||
| : this (value, useNewObject: false) | ||
| { | ||
| } | ||
|
|
||
| public CallVirtualFromConstructorDerived (int value, bool useNewObject) | ||
| : base (value, useNewObject) | ||
| { | ||
| InvokedConstructor = true; | ||
|
|
||
| if (useNewObject && calledValue != 0) { | ||
| // calledValue was set on a *different* instance! So it's 0 here. | ||
| throw new ArgumentException ( | ||
| string.Format ("value '{0}' doesn't match expected value '{1}'.", value, 0), | ||
| "value"); | ||
| } | ||
| if (!useNewObject && value != calledValue) | ||
| throw new ArgumentException ( | ||
| string.Format ("value '{0}' doesn't match expected value '{1}'.", value, calledValue), | ||
| "value"); | ||
| } | ||
|
|
||
| public bool InvokedActivationConstructor; | ||
|
|
||
| public static CallVirtualFromConstructorDerived? Intermediate_FromCalledFromConstructor; | ||
| public static CallVirtualFromConstructorDerived? Intermediate_FromActivationConstructor; | ||
|
|
||
| public CallVirtualFromConstructorDerived (ref JniObjectReference reference, JniObjectReferenceOptions options) | ||
| : base (ref reference, options) | ||
| { | ||
| InvokedActivationConstructor = true; | ||
|
|
||
| Intermediate_FromActivationConstructor = this; | ||
| } | ||
|
|
||
| public bool Called; | ||
|
|
||
| [Register ("calledFromConstructor", "(I)V", "")] | ||
| public override void CalledFromConstructor (int value) | ||
| { | ||
| Called = true; | ||
| calledValue = value; | ||
|
|
||
| Intermediate_FromCalledFromConstructor = this; | ||
| } | ||
|
|
||
| public static unsafe CallVirtualFromConstructorDerived NewInstance (int value) | ||
| { | ||
| JniArgumentValue* args = stackalloc JniArgumentValue [1]; | ||
| args [0] = new JniArgumentValue (value); | ||
| var o = _members.StaticMethods.InvokeObjectMethod ("newInstance.(I)Lnet/dot/jni/test/CallVirtualFromConstructorDerived;", args); | ||
| var result = JniEnvironment.Runtime.ValueManager.GetValue<CallVirtualFromConstructorDerived> (ref o, JniObjectReferenceOptions.CopyAndDispose); | ||
| if (result == null) | ||
| throw new InvalidOperationException ("newInstance returned null."); | ||
| return result; | ||
| } | ||
|
|
||
| delegate void CalledFromConstructorMarshalMethod (IntPtr jnienv, IntPtr n_self, int value); | ||
| static void CalledFromConstructorHandler (IntPtr jnienv, IntPtr n_self, int value) | ||
| { | ||
| n_CalledFromConstructor (jnienv, n_self, value); | ||
| } | ||
|
|
||
| static void n_CalledFromConstructor (IntPtr jnienv, IntPtr n_self, int value) | ||
| { | ||
| var envp = new JniTransition (jnienv); | ||
| try { | ||
| var r_self = new JniObjectReference (n_self); | ||
| var self = JniEnvironment.Runtime.ValueManager.GetValue<CallVirtualFromConstructorDerived>(ref r_self, JniObjectReferenceOptions.Copy); | ||
| if (self == null) | ||
| throw new InvalidOperationException ("calledFromConstructor received null self."); | ||
| self.CalledFromConstructor (value); | ||
| self.InvokedConstructor = true; | ||
| ((IJavaPeerable) self).SetJniManagedPeerState (self.JniManagedPeerState | JniManagedPeerStates.Replaceable); | ||
| self.DisposeUnlessReferenced (); | ||
| } | ||
| catch (Exception e) when (JniEnvironment.Runtime.ExceptionShouldTransitionToJni (e)) { | ||
| envp.SetPendingException (e); | ||
| } | ||
| finally { | ||
| envp.Dispose (); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [AttributeUsage (AttributeTargets.Constructor | AttributeTargets.Method)] | ||
| sealed class RegisterAttribute : Attribute { | ||
| public RegisterAttribute (string name, string signature, string connector) | ||
| { | ||
| } | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
...ts/Java.Interop-Tests/java-trimmable/net/dot/jni/test/CallVirtualFromConstructorBase.java
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,47 @@ | ||
| package net.dot.jni.test; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| import net.dot.jni.GCUserPeerable; | ||
|
|
||
| public class CallVirtualFromConstructorBase implements GCUserPeerable { | ||
|
|
||
| static { | ||
| registerNatives (); | ||
| } | ||
|
|
||
| ArrayList<Object> managedReferences = new ArrayList<Object>(); | ||
|
|
||
| public CallVirtualFromConstructorBase (int value) { | ||
| if (CallVirtualFromConstructorBase.class == getClass ()) { | ||
| nctor_0 (value); | ||
| } | ||
| calledFromConstructor (value); | ||
| } | ||
|
|
||
| private native void nctor_0 (int value); | ||
|
|
||
| public void calledFromConstructor (int value) { | ||
| } | ||
|
|
||
| public void jiAddManagedReference (java.lang.Object obj) | ||
| { | ||
| managedReferences.add (obj); | ||
| } | ||
|
|
||
| public void jiClearManagedReferences () | ||
| { | ||
| managedReferences.clear (); | ||
| } | ||
|
|
||
| static void registerNatives () | ||
| { | ||
| try { | ||
| Class<?> runtime = Class.forName ("mono.android.Runtime"); | ||
| java.lang.reflect.Method registerNatives = runtime.getMethod ("registerNatives", Class.class); | ||
| registerNatives.invoke (null, CallVirtualFromConstructorBase.class); | ||
| } catch (Exception e) { | ||
| throw new Error (e); | ||
| } | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
...Java.Interop-Tests/java-trimmable/net/dot/jni/test/CallVirtualFromConstructorDerived.java
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,59 @@ | ||
| package net.dot.jni.test; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| import net.dot.jni.GCUserPeerable; | ||
|
|
||
| public class CallVirtualFromConstructorDerived | ||
| extends CallVirtualFromConstructorBase | ||
| implements GCUserPeerable | ||
| { | ||
| static { | ||
| registerNatives (); | ||
| } | ||
|
|
||
| ArrayList<Object> managedReferences = new ArrayList<Object>(); | ||
| boolean calledFromConstructorInvoked; | ||
|
|
||
| public CallVirtualFromConstructorDerived (int value) { | ||
| super (value); | ||
| if (CallVirtualFromConstructorDerived.class == getClass () && !calledFromConstructorInvoked) { | ||
| nctor_0 (value); | ||
| } | ||
| } | ||
|
|
||
| public static CallVirtualFromConstructorDerived newInstance (int value) | ||
| { | ||
| return new CallVirtualFromConstructorDerived (value); | ||
| } | ||
|
|
||
| public void calledFromConstructor (int value) { | ||
| calledFromConstructorInvoked = true; | ||
| n_CalledFromConstructor (value); | ||
| } | ||
|
|
||
| public native void n_CalledFromConstructor (int value); | ||
|
|
||
| private native void nctor_0 (int value); | ||
|
|
||
| public void jiAddManagedReference (java.lang.Object obj) | ||
| { | ||
| managedReferences.add (obj); | ||
| } | ||
|
|
||
| public void jiClearManagedReferences () | ||
| { | ||
| managedReferences.clear (); | ||
| } | ||
|
|
||
| static void registerNatives () | ||
| { | ||
| try { | ||
| Class<?> runtime = Class.forName ("mono.android.Runtime"); | ||
| java.lang.reflect.Method registerNatives = runtime.getMethod ("registerNatives", Class.class); | ||
| registerNatives.invoke (null, CallVirtualFromConstructorDerived.class); | ||
| } catch (Exception e) { | ||
| throw new Error (e); | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.