Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Documentation/guides/internals/JavaJNI_Interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [Introduction](#introduction)
- [Java <-> Managed interoperability overview](#java-managed-interoperability-overview)
- [Java Callable Wrappers (JCW)](#java-callable-wrappers-jcw)
- [Trimmable type maps](#trimmable-type-maps)
- [Registration](#registration)
- [Dynamic registration](#dynamic-registration)
- [Dynamic Java Callable Wrappers registration code](#dynamic-java-callable-wrappers-registration-code)
Expand Down Expand Up @@ -148,6 +149,18 @@ mechanisms described in sections found later in this document. The
this example in order to explain the details of how the Managed type
and its methods are registered with the Java VM.

## Trimmable type maps

When the trimmable type map is enabled, generated proxies for sealed
managed types defined in `Mono.Android` pass `isSealed: true` to the
`JavaPeerProxy` constructor. The resulting `JavaPeerProxy.IsSealed`
property lets target-type lookups use `FindClass` and `IsInstanceOf`
without inspecting `Type.Assembly` at runtime. Other peers pass
`isSealed: false` and still use the object's actual Java class
hierarchy, even for sealed app and library types: these classes can use
a different class loader, so resolving their class by name could
incorrectly reject a valid object.

# Registration

Both mechanisms of method registration rely on generation of [Java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public static ModelFingerprints ComputeFingerprints (
writer.WriteString (incremental, proxy.Namespace);
}
WriteTypeRef (writer, both, proxy.TargetType);
writer.WriteBoolean (both, proxy.CanResolveFromSealedTargetType);
if (includeIncremental) {
WriteOptionalTypeRef (writer, incremental, proxy.InvokerType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ sealed class JavaPeerProxyData
/// </summary>
public required TypeRefData TargetType { get; init; }

/// <summary>
/// True when the sealed Mono.Android target can use the runtime's class loader.
/// </summary>
public bool CanResolveFromSealedTargetType { get; init; }

/// <summary>
/// Reference to the invoker type (for interfaces/abstract types). Null if not applicable.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ static JavaPeerProxyData BuildProxyType (JavaPeerInfo peer, string jniName, Hash
},
IsAcw = isAcw,
IsGenericDefinition = peer.IsGenericDefinition,
CanResolveFromSealedTargetType = peer.IsSealed && string.Equals (peer.AssemblyName, "Mono.Android", StringComparison.Ordinal),
CannotRegisterInStaticConstructor = peer.CannotRegisterInStaticConstructor,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,20 +682,20 @@ void EmitProxyType (JavaPeerProxyData proxy, Dictionary<UcoWrapperTargetData, Me
// open generic definitions (whose T isn't available via the TypeMapLazyDictionary loader),
// and interfaces (which have no constructors). Peers are activated from their generated
// CreateInstance / InvokerType, never by reflecting over the target type.
var proxyBaseType = (EntityHandle) _javaPeerProxyRef;
var baseCtorRef = _pe.AddMemberRef (_javaPeerProxyRef, ".ctor",
sig => sig.MethodSignature (isInstanceMethod: true).Parameters (2,
sig => sig.MethodSignature (isInstanceMethod: true).Parameters (3,
rt => rt.Void (),
p => {
p.AddParameter ().Type ().String ();
p.AddParameter ().Type ().Type (_systemTypeRef, false);
p.AddParameter ().Type ().Boolean ();
}));

var typeDefHandle = metadata.AddTypeDefinition (
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Class,
metadata.GetOrAddString (proxy.Namespace),
metadata.GetOrAddString (proxy.TypeName),
proxyBaseType,
_javaPeerProxyRef,
MetadataTokens.FieldDefinitionHandle (metadata.GetRowCount (TableIndex.Field) + 1),
MetadataTokens.MethodDefinitionHandle (metadata.GetRowCount (TableIndex.MethodDef) + 1));

Expand All @@ -712,7 +712,8 @@ void EmitProxyType (JavaPeerProxyData proxy, Dictionary<UcoWrapperTargetData, Me
encoder.LoadString (metadata.GetOrAddUserString (proxy.JniName));
encoder.LoadToken (targetTypeRef);
encoder.Call (_getTypeFromHandleRef, parameterCount: 1, returnsValue: true);
encoder.Call (baseCtorRef, parameterCount: 2, isInstance: true);
encoder.LoadConstantI4 (proxy.CanResolveFromSealedTargetType ? 1 : 0);
encoder.Call (baseCtorRef, parameterCount: 3, isInstance: true);
encoder.Return ();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public sealed record JavaPeerInfo

public bool IsInterface { get; init; }
public bool IsAbstract { get; init; }
public bool IsSealed { get; init; }

/// <summary>
/// If true, this is a Managed Callable Wrapper (MCW) binding type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ void ScanAssembly (AssemblyIndex index, Dictionary<(string ManagedName, string A
var isGenericDefinition = typeDef.GetGenericParameters ().Count > 0;
var isInterface = (typeDef.Attributes & TypeAttributes.Interface) != 0;
var isAbstract = (typeDef.Attributes & TypeAttributes.Abstract) != 0;
var isSealed = (typeDef.Attributes & TypeAttributes.Sealed) != 0;

var isFrameworkAssembly = frameworkAssemblyNames.Contains (index.AssemblyName);
var isUnconditional = !isFrameworkAssembly &&
Expand Down Expand Up @@ -437,6 +438,7 @@ void ScanAssembly (AssemblyIndex index, Dictionary<(string ManagedName, string A
Annotations = annotationParser.Parse (typeDef.GetCustomAttributes (), index),
IsInterface = isInterface,
IsAbstract = isAbstract,
IsSealed = isSealed,
DoNotGenerateAcw = doNotGenerateAcw,
IsFromJniTypeSignature = registerInfo?.IsFromJniTypeSignature ?? false,
IsUnconditional = isUnconditional,
Expand Down
15 changes: 15 additions & 0 deletions src/Mono.Android/Java.Interop/JavaPeerProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ public sealed class JavaPeerAliasesAttribute : Attribute
public abstract class JavaPeerProxy : Attribute
{
protected JavaPeerProxy (string jniName, Type targetType)
: this (jniName, targetType, false)
{
}

protected JavaPeerProxy (string jniName, Type targetType, bool isSealed)
{
ArgumentNullException.ThrowIfNull (jniName);
ArgumentNullException.ThrowIfNull (targetType);

JniName = jniName;
TargetType = targetType;
IsSealed = isSealed;
}

/// <summary>
Expand All @@ -69,6 +75,15 @@ protected JavaPeerProxy (string jniName, Type targetType)
/// </summary>
public Type TargetType { get; }

/// <summary>
/// Gets whether the sealed target can be resolved directly using the runtime's class loader.
/// </summary>
/// <remarks>
/// Generated proxies pass <see langword="true"/> only for sealed Mono.Android targets. External
/// targets pass <see langword="false"/> even when sealed, because they can use a different class loader.
/// </remarks>
public bool IsSealed { get; }

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.

What if we made this EditorBrowsable.Hidden? We'd also want to do that in main.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't see how that would be very helpful. This property is in no way different from the other properties. If anything, we should add it to the whole JavaPeerProxy class


/// <summary>
/// Returns <see langword="true"/> when the UCO constructor callback should skip
/// activation because a managed peer already exists for the given JNI handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,10 @@ bool TryResolveProxyFromSealedTargetType (
{
proxy = null;
// App peers can use class loaders which differ from the runtime fallback loader.
// The generator marks only sealed Mono.Android targets as safe for this lookup.
if (targetType is null ||
!targetType.IsSealed ||
targetType.Assembly != typeof (Java.Lang.Object).Assembly) {
return false;
}

var targetProxy = GetProxyForManagedType (targetType);
if (targetProxy is null) {
GetProxyForManagedType (targetType) is not { IsSealed: true } targetProxy) {
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mono.Android/PublicAPI/API-37/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2352,7 +2352,9 @@ Java.Interop.JavaPeerAliasesAttribute
Java.Interop.JavaPeerAliasesAttribute.Aliases.get -> string![]!
Java.Interop.JavaPeerAliasesAttribute.JavaPeerAliasesAttribute(params string![]! aliases) -> void
Java.Interop.JavaPeerProxy
Java.Interop.JavaPeerProxy.IsSealed.get -> bool
Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType) -> void
Java.Interop.JavaPeerProxy.JavaPeerProxy(string! jniName, System.Type! targetType, bool isSealed) -> void
Comment on lines +2355 to +2357

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.

@simonrozsival is it a problem to add new APIs in a service release?

What would happen is someone with the new workload could create a NuGet package that wouldn't work for folks with the old one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The typemap code, which uses this, is always generated at app build time. No libraries (incl. Mono.Android) use this ctor. Also, this currently only affects Native AOT apps by default. I will think this through tomorrow and I'll try to think about the risks.

At the same time, this is just performance improvement that we do not need to backport and we can keep this .NET 12 only.

Java.Interop.JavaPeerProxy.JniName.get -> string!
Java.Interop.JavaPeerProxy.TargetType.get -> System.Type!
Java.Lang.ICharSequence.GetChars(int srcBegin, int srcEnd, char[]! dst, int dstBegin) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace Microsoft.Android.Sdk.TrimmableTypeMap.Tests;
/// <summary>
/// The fingerprints are computed by streaming model fields straight into SHA-256 instead of
/// buffering a <see cref="BinaryWriter"/> serialisation in a <see cref="MemoryStream"/>. These
/// tests pin that rewrite to the previous byte stream: the content fingerprint seeds the emitted
/// assembly's deterministic MVID and the incremental fingerprint is an incremental-build contract,
/// so neither may drift.
/// tests compare the streaming output with an independent buffered serializer. The content
/// fingerprint seeds the emitted assembly's deterministic MVID, and the incremental fingerprint
/// is an incremental-build contract.
/// </summary>
public class FingerprintWriterTests : FixtureTestBase
{
Expand Down Expand Up @@ -116,15 +116,24 @@ public void IncrementalSink_FlushesBufferedDataSpanningMultipleFlushes ()
[Theory]
[InlineData (true)]
[InlineData (false)]
public void ComputeFingerprints_MatchesLegacyBufferedSerialization (bool useSharedTypemapUniverse)
public void ComputeFingerprints_MatchesBufferedSerialization (bool useSharedTypemapUniverse)
{
var systemRuntimeVersion = new Version (11, 0, 0, 0);
var peers = new List<JavaPeerInfo> (ScanFixtures ()) {
MakePeerWithActivation ("example/SealedPeer", "Example.SealedPeer", "Mono.Android") with {
IsFrameworkAssembly = true,
IsSealed = true,
DoNotGenerateAcw = true,
},
};
var model = new TypeMapAssemblyGenerator (systemRuntimeVersion)
.CreateModel (ScanFixtures (), "_TestFixtures.TypeMap");
.CreateModel (peers, "_TestFixtures.TypeMap");

// The fixtures must exercise every part of the walk that the two fingerprints share.
Assert.NotEmpty (model.Entries);
Assert.NotEmpty (model.ProxyTypes);
Assert.Contains (model.ProxyTypes, proxy => proxy.CanResolveFromSealedTargetType);
Assert.Contains (model.ProxyTypes, proxy => !proxy.CanResolveFromSealedTargetType);

var fingerprints = MetadataHelper.ComputeFingerprints (model, systemRuntimeVersion, useSharedTypemapUniverse, includeIncremental: true);

Expand Down Expand Up @@ -215,9 +224,8 @@ static byte [] Sha256 (byte [] bytes)
return sha.ComputeHash (bytes);
}

// The implementations below are the buffered BinaryWriter serialisation used before the
// streaming rewrite. They are intentionally verbatim copies so the tests fail if the new
// walk changes the byte stream in any way.
// Buffered reference serializers, based on the implementations used before the streaming
// rewrite and updated when the fingerprint format intentionally changes.

static byte [] LegacyContentFingerprint (TypeMapAssemblyData data) => Sha256 (BinaryWriterBytes (writer => {
foreach (var entry in data.Entries) {
Expand All @@ -228,6 +236,7 @@ static byte [] LegacyContentFingerprint (TypeMapAssemblyData data) => Sha256 (Bi
foreach (var proxy in data.ProxyTypes) {
writer.Write (proxy.TypeName);
LegacyWriteTypeRef (writer, proxy.TargetType);
writer.Write (proxy.CanResolveFromSealedTargetType);
writer.Write ((byte) (proxy.ActivationCtor?.Style ?? 0));
if (proxy.ActivationCtor is not null) {
LegacyWriteTypeRef (writer, proxy.ActivationCtor.DeclaringType);
Expand Down Expand Up @@ -271,6 +280,7 @@ static byte [] LegacyIncrementalFingerprint (TypeMapAssemblyData data, Version s
writer.Write (proxy.JniName);
writer.Write (proxy.Namespace);
LegacyWriteTypeRef (writer, proxy.TargetType);
writer.Write (proxy.CanResolveFromSealedTargetType);
LegacyWriteOptionalTypeRef (writer, proxy.InvokerType);
writer.Write (proxy.InvokerActivationCtorStyle.HasValue);
if (proxy.InvokerActivationCtorStyle.HasValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,57 @@ public void GenerateTypeMapAssemblies_UnchangedFingerprintsSkipAllEmission ()
Assert.Empty (second);
}

[Theory]
[InlineData (false)]
[InlineData (true)]
public void GenerateTypeMapAssemblies_ChangedFrameworkSealednessRegeneratesAssembly (bool isSealed)
{
var peer = CreatePeer ("Mono.Android", "Java.Lang.String", "java/lang/String") with {
IsSealed = isSealed,
};
var peers = new List<JavaPeerInfo> { peer };
var fingerprints = new Dictionary<string, byte []> (StringComparer.Ordinal);
var generator = CreateGenerator ();
var first = generator.GenerateTypeMapAssemblies (
peers,
new Version (11, 0),
useSharedTypemapUniverse: true,
(name, fingerprint) => {
fingerprints.Add (name, fingerprint);
return true;
});
var original = Assert.Single (first, assembly => assembly.Name == "_Mono.Android.TypeMap");
original.Content.Position = 0;
Guid originalMvid;
using (var pe = new PEReader (original.Content, PEStreamOptions.LeaveOpen)) {
var reader = pe.GetMetadataReader ();
originalMvid = reader.GetGuid (reader.GetModuleDefinition ().Mvid);
}
DisposeGeneratedAssemblies (first);

var unchanged = generator.GenerateTypeMapAssemblies (
peers,
new Version (11, 0),
useSharedTypemapUniverse: true,
(name, fingerprint) => !fingerprints [name].SequenceEqual (fingerprint));
Assert.Empty (unchanged);

peers [0] = peer with { IsSealed = !isSealed };
var regenerated = generator.GenerateTypeMapAssemblies (
peers,
new Version (11, 0),
useSharedTypemapUniverse: true,
(name, fingerprint) => !fingerprints [name].SequenceEqual (fingerprint));
var changed = Assert.Single (regenerated);
Assert.Equal ("_Mono.Android.TypeMap", changed.Name);
changed.Content.Position = 0;
using (var pe = new PEReader (changed.Content, PEStreamOptions.LeaveOpen)) {
var reader = pe.GetMetadataReader ();
Assert.NotEqual (originalMvid, reader.GetGuid (reader.GetModuleDefinition ().Mvid));
}
DisposeGeneratedAssemblies (regenerated);
}

[Fact]
public void Execute_IncrementalCallbackPreservesGeneratedBytes ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,79 @@ public void Generate_HasRequiredAssemblyReferences ()
Assert.Contains ("System.Runtime.InteropServices", asmRefs);
}

[Theory]
[InlineData ("Mono.Android", false)]
[InlineData ("Mono.Android", true)]
[InlineData ("Other", false)]
[InlineData ("Other", true)]
public void Generate_ProxyConstructor_PassesSealedFrameworkEligibility (string assemblyName, bool isSealed)
{
foreach (bool alias in new [] { false, true }) {
foreach (bool generic in new [] { false, true }) {
var peer = MakePeerWithActivation (
"example/Peer", generic ? "Example.Peer`1" : "Example.Peer", assemblyName) with {
IsFrameworkAssembly = true,
IsSealed = isSealed,
IsGenericDefinition = generic,
DoNotGenerateAcw = true,
};
var peers = new List<JavaPeerInfo> { peer };
if (alias) {
peers.Add (MakePeerWithActivation ("example/Peer", "Example.OtherPeer", assemblyName) with {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 💡 Testing — Consider adding a mixed-assembly alias case: a sealed Mono.Android peer and a sealed external peer sharing a JNI name, passed through MergeCrossAssemblyAliases, then assert that their emitted constructor arguments are true and false, respectively. The current alias cases keep both peers in the same input assembly. The mixed case would pin the important distinction between a peer's defining assembly and the typemap assembly that owns its alias group, protecting the class-loader safeguard against future refactoring.

{Rule: Test edge cases; verify interop boundary behavior}

IsFrameworkAssembly = true,
DoNotGenerateAcw = true,
});
}

var generator = new TypeMapAssemblyGenerator (new Version (11, 0));
var model = generator.CreateModel (peers, "_Example.TypeMap");
using var stream = new MemoryStream ();
generator.Generate (model, stream, useSharedTypemapUniverse: true);
stream.Position = 0;
using var pe = new PEReader (stream);
var reader = pe.GetMetadataReader ();
var baseConstructors = FindCtorMemberRefs (
reader, "Java.Interop", "JavaPeerProxy", "System.String", "System.Type", "System.Boolean");
Assert.NotEmpty (baseConstructors);
Assert.Equal (alias ? 2 : 1, model.ProxyTypes.Count);

foreach (var proxy in model.ProxyTypes) {
bool eligible = assemblyName == "Mono.Android" && isSealed &&
proxy.TargetType.ManagedTypeName == peer.ManagedTypeName;
Assert.Equal (eligible, proxy.CanResolveFromSealedTargetType);
var type = reader.TypeDefinitions
.Select (reader.GetTypeDefinition)
.Single (type => reader.GetString (type.Namespace) == proxy.Namespace &&
reader.GetString (type.Name) == proxy.TypeName);
Assert.Equal (HandleKind.TypeReference, type.BaseType.Kind);
var baseType = reader.GetTypeReference ((TypeReferenceHandle) type.BaseType);
Assert.Equal ("Java.Interop", reader.GetString (baseType.Namespace));
Assert.Equal ("JavaPeerProxy", reader.GetString (baseType.Name));
Assert.DoesNotContain (type.GetProperties (),
handle => reader.GetString (reader.GetPropertyDefinition (handle).Name) == "IsSealed");

var constructor = type.GetMethods ()
.Select (reader.GetMethodDefinition)
.Single (method => reader.GetString (method.Name) == ".ctor");
var il = pe.GetMethodBody (constructor.RelativeVirtualAddress).GetILBytes ();
Assert.NotNull (il);
bool callsBaseWithExpectedValue = false;
foreach (var handle in baseConstructors) {
int token = MetadataTokens.GetToken (handle);
byte [] call = [
(byte) (eligible ? ILOpCode.Ldc_i4_1 : ILOpCode.Ldc_i4_0),
(byte) ILOpCode.Call,
(byte) token, (byte) (token >> 8), (byte) (token >> 16), (byte) (token >> 24),
];
callsBaseWithExpectedValue |= il.AsSpan ().IndexOf (call) >= 0;
}
Assert.True (callsBaseWithExpectedValue,
$"{proxy.TypeName}: assembly={assemblyName}, sealed={isSealed}, alias={alias}, generic={generic}");
}
}
}
}

[Fact]
public void Generate_InheritedGenericBaseCallback_UsesValueTypeGenericArgument ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void Scan_TypeMetadata_IsCorrect ()
Assert.True (FindFixtureByJavaName ("my/app/AbstractBase").IsAbstract);
Assert.True (FindFixtureByManagedName ("Android.Views.IOnClickListener").IsInterface);
Assert.False (FindFixtureByManagedName ("Android.Views.IOnClickListener").DoNotGenerateAcw);
Assert.True (FindFixtureByManagedName ("Android.Views.IOnClickListenerInvoker").IsSealed);
Assert.False (FindFixtureByManagedName ("Android.App.Activity").IsSealed);

var generic = FindFixtureByJavaName ("my/app/GenericHolder");
Assert.True (generic.IsGenericDefinition);
Expand Down
Loading
Loading