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
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ public static bool IsAmbiguousCallbackKind (JniParamKind kind)
/// </summary>
public static bool HasAmbiguousCallbackType (string jniSignature)
{
if (IsAmbiguousCallbackKind (ParseReturnType (jniSignature))) {
return true;
}
foreach (var kind in ParseParameterTypes (jniSignature)) {
if (IsAmbiguousCallbackKind (kind)) {
int i = 1;
while (i < jniSignature.Length && jniSignature [i] != ')') {
if (IsAmbiguousCallbackKind (ParseSingleType (jniSignature, ref i))) {
return true;
}
}
return false;
if (i >= jniSignature.Length) {
throw new ArgumentException ($"Malformed JNI signature '{jniSignature}': missing ')'");
}
i++;
return IsAmbiguousCallbackKind (ParseSingleType (jniSignature, ref i));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

Expand Down Expand Up @@ -29,8 +30,8 @@ public static Guid DeterministicMvid (string moduleName, ReadOnlySpan<byte> cont
public static byte [] ComputeContentFingerprint (TypeMapAssemblyData data)
{
using var sha = SHA256.Create ();
using var stream = new System.IO.MemoryStream ();
using var writer = new System.IO.BinaryWriter (stream, Encoding.UTF8);
using var stream = new MemoryStream ();
using var writer = new BinaryWriter (stream, Encoding.UTF8);
foreach (var entry in data.Entries) {
writer.Write (entry.MapKey);
writer.Write (entry.ProxyTypeReference);
Expand Down Expand Up @@ -62,10 +63,10 @@ public static byte [] ComputeContentFingerprint (TypeMapAssemblyData data)
writer.Write (assoc.AliasProxyTypeReference);
}
writer.Flush ();
return sha.ComputeHash (stream.ToArray ());
return sha.ComputeHash (stream.GetBuffer (), 0, checked ((int) stream.Length));
}

static void WriteTypeRef (this System.IO.BinaryWriter writer, TypeRefData type)
static void WriteTypeRef (this BinaryWriter writer, TypeRefData type)
{
writer.Write (type.ManagedTypeName);
writer.Write (type.AssemblyName);
Expand All @@ -77,7 +78,7 @@ static void WriteTypeRef (this System.IO.BinaryWriter writer, TypeRefData type)
}
}

static void WriteUcoMethod (this System.IO.BinaryWriter writer, UcoMethodData method)
static void WriteUcoMethod (this BinaryWriter writer, UcoMethodData method)
{
writer.Write (method.WrapperName);
writer.Write (method.CallbackMethodName);
Expand All @@ -86,7 +87,7 @@ static void WriteUcoMethod (this System.IO.BinaryWriter writer, UcoMethodData me
writer.WriteExportMethodDispatch (method.ExportMethodDispatch);
}

static void WriteExportMethodDispatch (this System.IO.BinaryWriter writer, ExportMethodDispatchData? dispatch)
static void WriteExportMethodDispatch (this BinaryWriter writer, ExportMethodDispatchData? dispatch)
{
writer.Write (dispatch is not null);
if (dispatch is null) {
Expand All @@ -107,7 +108,7 @@ static void WriteExportMethodDispatch (this System.IO.BinaryWriter writer, Expor
writer.Write (dispatch.IsStatic);
}

static void WriteUcoConstructor (this System.IO.BinaryWriter writer, UcoConstructorData constructor)
static void WriteUcoConstructor (this BinaryWriter writer, UcoConstructorData constructor)
{
writer.Write (constructor.WrapperName);
writer.WriteTypeRef (constructor.TargetType);
Expand All @@ -119,7 +120,7 @@ static void WriteUcoConstructor (this System.IO.BinaryWriter writer, UcoConstruc
}
}

static void WriteNativeRegistration (this System.IO.BinaryWriter writer, NativeRegistrationData registration)
static void WriteNativeRegistration (this BinaryWriter writer, NativeRegistrationData registration)
{
writer.Write (registration.JniMethodName);
writer.Write (registration.JniSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ static void BuildNativeRegistrations (TypeMapAssemblyData model)
}

foreach (var proxy in model.ProxyTypes) {
var reusedUcoMethods = new HashSet<UcoMethodData> ();
HashSet<UcoMethodData>? reusedUcoMethods = null;

foreach (var uco in proxy.UcoMethods) {
var wrapperTarget = UcoWrapperTargetData.From (proxy, uco.WrapperName);
if (CanReuseUcoWrapper (proxy, uco) &&
sharedWrapperTargets.TryGetValue (CreateUcoWrapperReuseKey (uco), out var sharedWrapperTarget)) {
wrapperTarget = sharedWrapperTarget;
reusedUcoMethods.Add (uco);
(reusedUcoMethods ??= new ()).Add (uco);
}
proxy.NativeRegistrations.Add (new NativeRegistrationData {
JniMethodName = uco.CallbackMethodName,
Expand All @@ -425,7 +425,7 @@ static void BuildNativeRegistrations (TypeMapAssemblyData model)
});
}

if (reusedUcoMethods.Count > 0) {
if (reusedUcoMethods is not null) {
proxy.UcoMethods.RemoveAll (uco => reusedUcoMethods.Contains (uco));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public void WritePE (Stream stream)
deterministicIdProvider: DeterministicContentId);
var peBlob = new BlobBuilder ();
peBuilder.Serialize (peBlob);
if (stream is MemoryStream memoryStream && memoryStream.Length == 0 && memoryStream.Capacity < peBlob.Count) {
memoryStream.Capacity = peBlob.Count;
}
Comment thread
simonrozsival marked this conversation as resolved.
Resolved
peBlob.WriteContentTo (stream);
}

Expand Down Expand Up @@ -289,24 +292,20 @@ public void PrepareUtf8Fields (IEnumerable<string> values)
foreach (var group in valuesBySize) {
var sizedType = GetOrCreateSizedType (group.Key);
foreach (string value in group.Value) {
AddUtf8Field (value, group.Key, sizedType);
AddUtf8Field (value, sizedType);
}
}
}

void AddUtf8Field (string value, int size, TypeDefinitionHandle sizedType)
void AddUtf8Field (string value, TypeDefinitionHandle sizedType)
{
// Encode to null-terminated UTF-8 (all JNI names/signatures are ASCII).
int byteCount = size - 1;
var bytes = new byte [size];
System.Text.Encoding.UTF8.GetBytes (value, 0, value.Length, bytes, 0);
// bytes[byteCount] is already 0 (null terminator)

_sigBlob.Clear ();
new BlobEncoder (_sigBlob).FieldSignature ().Type (sizedType, true);

int rva = _mappedFieldData.Count;
_mappedFieldData.WriteBytes (bytes);
_mappedFieldData.WriteUTF8 (value);
_mappedFieldData.WriteByte (0);

var fieldHandle = Metadata.AddFieldDefinition (
FieldAttributes.Static | FieldAttributes.Assembly | FieldAttributes.HasFieldRVA | FieldAttributes.InitOnly,
Expand Down Expand Up @@ -513,6 +512,27 @@ public BlobHandle BuildAttributeBlob (Action<BlobBuilder> writePayload)
return Metadata.GetOrAddBlob (_attrBlob);
}

public BlobHandle BuildAttributeBlob (string first, string second)
{
_attrBlob.Clear ();
_attrBlob.WriteUInt16 (0x0001);
_attrBlob.WriteSerializedString (first);
_attrBlob.WriteSerializedString (second);
_attrBlob.WriteUInt16 (0x0000);
return Metadata.GetOrAddBlob (_attrBlob);
}

public BlobHandle BuildAttributeBlob (string first, string second, string third)
{
_attrBlob.Clear ();
_attrBlob.WriteUInt16 (0x0001);
_attrBlob.WriteSerializedString (first);
_attrBlob.WriteSerializedString (second);
_attrBlob.WriteSerializedString (third);
_attrBlob.WriteUInt16 (0x0000);
return Metadata.GetOrAddBlob (_attrBlob);
}

/// <summary>
/// Emits the <c>IgnoresAccessChecksToAttribute</c> type and applies
/// <c>[assembly: IgnoresAccessChecksTo("...")]</c> for each assembly name.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
Expand Down Expand Up @@ -196,10 +195,7 @@ void EmitCore (TypeMapAssemblyData model, bool useSharedTypemapUniverse)
}
EmitMemberReferences ();

_pe.PrepareUtf8Fields (model.ProxyTypes
.Where (proxy => proxy.IsAcw)
.SelectMany (proxy => proxy.NativeRegistrations)
.SelectMany (registration => new [] { registration.JniMethodName, registration.JniSignature }));
_pe.PrepareUtf8Fields (EnumerateNativeRegistrationStrings (model.ProxyTypes));

// Track wrapper targets → handles for RegisterNatives.
var wrapperHandles = new Dictionary<UcoWrapperTargetData, MethodDefinitionHandle> ();
Expand All @@ -223,6 +219,19 @@ void EmitCore (TypeMapAssemblyData model, bool useSharedTypemapUniverse)
_pe.EmitIgnoresAccessChecksToAttribute (model.IgnoresAccessChecksTo);
}

static IEnumerable<string> EnumerateNativeRegistrationStrings (IReadOnlyList<JavaPeerProxyData> proxies)
{
foreach (var proxy in proxies) {
if (!proxy.IsAcw) {
continue;
}
foreach (var registration in proxy.NativeRegistrations) {
yield return registration.JniMethodName;
yield return registration.JniSignature;
}
}
}

static List<JavaPeerProxyData> OrderProxiesForWrapperTargets (IReadOnlyList<JavaPeerProxyData> proxies)
{
var proxyByType = new Dictionary<(string Namespace, string TypeName), JavaPeerProxyData> ();
Expand Down Expand Up @@ -1717,27 +1726,23 @@ void EmitTypeMapAttribute (TypeMapAttributeData entry)
{
var ctorRef = entry.IsUnconditional ? _typeMapAttrCtorRef2Arg : _typeMapAttrCtorRef3Arg;

var blob = _pe.BuildAttributeBlob (b => {
b.WriteSerializedString (entry.MapKey);
b.WriteSerializedString (entry.ProxyTypeReference);
if (!entry.IsUnconditional) {
if (entry.TargetTypeReference is null) {
throw new InvalidOperationException ($"TargetTypeReference must not be null for conditional entry '{entry.MapKey}'");
}
b.WriteSerializedString (entry.TargetTypeReference);
BlobHandle blob;
if (entry.IsUnconditional) {
blob = _pe.BuildAttributeBlob (entry.MapKey, entry.ProxyTypeReference);
} else {
if (entry.TargetTypeReference is null) {
throw new InvalidOperationException ($"TargetTypeReference must not be null for conditional entry '{entry.MapKey}'");
}
});
blob = _pe.BuildAttributeBlob (entry.MapKey, entry.ProxyTypeReference, entry.TargetTypeReference);
}
_pe.Metadata.AddCustomAttribute (EntityHandle.AssemblyDefinition, ctorRef, blob);
}

void EmitTypeMapAssociationAttribute (TypeMapAssociationData assoc)
{
var ctorRef = _typeMapAssociationAttrCtorRef;

var blob = _pe.BuildAttributeBlob (b => {
b.WriteSerializedString (assoc.SourceTypeReference);
b.WriteSerializedString (assoc.AliasProxyTypeReference);
});
var blob = _pe.BuildAttributeBlob (assoc.SourceTypeReference, assoc.AliasProxyTypeReference);
_pe.Metadata.AddCustomAttribute (EntityHandle.AssemblyDefinition, ctorRef, blob);
}

Expand Down
Loading
Loading