Skip to content

Commit

Permalink
[WIP] Replace MAM with JniRemapping, fix a JI test
Browse files Browse the repository at this point in the history
  • Loading branch information
grendello committed Jun 6, 2022
1 parent b7c65dd commit 0044761
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 583 deletions.
10 changes: 5 additions & 5 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public override void DeleteWeakGlobalReference (ref JniObjectReference value)
}

class AndroidTypeManager : JniRuntime.JniTypeManager {
struct MAMReplacementMethod
struct JniRemappingReplacementMethod
{
public string target_type;
public string target_name;
Expand Down Expand Up @@ -329,7 +329,7 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)

protected override string? GetReplacementTypeCore (string jniSimpleReference)
{
if (!JNIEnv.mamInUse) {
if (!JNIEnv.jniRemappingInUse) {
return null;
}

Expand All @@ -346,7 +346,7 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)

protected override JniRuntime.ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSourceType, string jniMethodName, string jniMethodSignature)
{
if (!JNIEnv.mamInUse) {
if (!JNIEnv.jniRemappingInUse) {
return null;
}

Expand All @@ -355,8 +355,8 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)
return null;
}

var method = new MAMReplacementMethod ();
method = Marshal.PtrToStructure<MAMReplacementMethod>(retInfo);
var method = new JniRemappingReplacementMethod ();
method = Marshal.PtrToStructure<JniRemappingReplacementMethod>(retInfo);

int? paramCount = null;
if (method.is_static) {
Expand Down
6 changes: 3 additions & 3 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct JnienvInitializeArgs {
public int packageNamingPolicy;
public byte ioExceptionType;
public int jniAddNativeMethodRegistrationAttributePresent;
public bool mamInUse;
public bool jniRemappingInUse;
}
#pragma warning restore 0649

Expand All @@ -49,7 +49,7 @@ public static partial class JNIEnv {
static int androidSdkVersion;

static bool AllocObjectSupported;
internal static bool mamInUse;
internal static bool jniRemappingInUse;

static IntPtr grefIGCUserPeer_class;

Expand Down Expand Up @@ -157,7 +157,7 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)

gref_gc_threshold = args->grefGcThreshold;

mamInUse = args->mamInUse;
jniRemappingInUse = args->jniRemappingInUse;
java_vm = args->javaVm;

version = args->version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@

namespace Xamarin.Android.Tasks
{
public class GenerateMamNativeCode : AndroidTask
public class GenerateJniRemappingNativeCode : AndroidTask
{
internal const string MamNativeCodeInfoKey = ".:!MamNativeCodeInfo!:.";
internal sealed class MamNativeCodeInfo
internal const string JniRemappingNativeCodeInfoKey = ".:!JniRemappingNativeCodeInfo!:.";

internal sealed class JniRemappingNativeCodeInfo
{
public int ReplacementTypeCount { get; }
public int ReplacementMethodIndexEntryCount { get; }

public MamNativeCodeInfo (int replacementTypeCount, int replacementMethodIndexEntryCount)
public JniRemappingNativeCodeInfo (int replacementTypeCount, int replacementMethodIndexEntryCount)
{
ReplacementTypeCount = replacementTypeCount;
ReplacementMethodIndexEntryCount = replacementMethodIndexEntryCount;
}
}

public override string TaskPrefix => "GMAMNC";
public override string TaskPrefix => "GJRNC";

public ITaskItem RemappingXmlFilePath { get; set; }

Expand Down Expand Up @@ -53,13 +54,13 @@ public override bool RunTask ()

void GenerateEmpty ()
{
Generate (new MamRemappingAssemblyGenerator (), typeReplacementsCount: 0);
Generate (new JniRemappingAssemblyGenerator (), typeReplacementsCount: 0);
}

void Generate ()
{
var typeReplacements = new List<MamTypeReplacement> ();
var methodReplacements = new List<MamMethodReplacement> ();
var typeReplacements = new List<JniRemappingTypeReplacement> ();
var methodReplacements = new List<JniRemappingMethodReplacement> ();

var readerSettings = new XmlReaderSettings {
XmlResolver = null,
Expand All @@ -73,32 +74,32 @@ void Generate ()
}
}

Generate (new MamRemappingAssemblyGenerator (typeReplacements, methodReplacements), typeReplacements.Count);
Generate (new JniRemappingAssemblyGenerator (typeReplacements, methodReplacements), typeReplacements.Count);
}

void Generate (MamRemappingAssemblyGenerator mamGenerator, int typeReplacementsCount)
void Generate (JniRemappingAssemblyGenerator jniRemappingGenerator, int typeReplacementsCount)
{
mamGenerator.Init ();
jniRemappingGenerator.Init ();

foreach (string abi in SupportedAbis) {
string baseAsmFilePath = Path.Combine (OutputDirectory, $"mam_remap.{abi.ToLowerInvariant ()}");
string baseAsmFilePath = Path.Combine (OutputDirectory, $"jni_remap.{abi.ToLowerInvariant ()}");
string llFilePath = $"{baseAsmFilePath}.ll";

using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
mamGenerator.Write (GeneratePackageManagerJava.GetAndroidTargetArchForAbi (abi), sw, llFilePath);
jniRemappingGenerator.Write (GeneratePackageManagerJava.GetAndroidTargetArchForAbi (abi), sw, llFilePath);
sw.Flush ();
Files.CopyIfStreamChanged (sw.BaseStream, llFilePath);
}
}

BuildEngine4.RegisterTaskObjectAssemblyLocal (
MamNativeCodeInfoKey,
new MamNativeCodeInfo (typeReplacementsCount, mamGenerator.ReplacementMethodIndexEntryCount),
JniRemappingNativeCodeInfoKey,
new JniRemappingNativeCodeInfo (typeReplacementsCount, jniRemappingGenerator.ReplacementMethodIndexEntryCount),
RegisteredTaskObjectLifetime.Build
);
}

void ReadXml (XmlReader reader, List<MamTypeReplacement> typeReplacements, List<MamMethodReplacement> methodReplacements)
void ReadXml (XmlReader reader, List<JniRemappingTypeReplacement> typeReplacements, List<JniRemappingMethodReplacement> methodReplacements)
{
bool haveAllAttributes;

Expand All @@ -115,7 +116,7 @@ void ReadXml (XmlReader reader, List<MamTypeReplacement> typeReplacements, List<
continue;
}

typeReplacements.Add (new MamTypeReplacement (from, to));
typeReplacements.Add (new JniRemappingTypeReplacement (from, to));
} else if (String.Compare ("replace-method", reader.LocalName, StringComparison.Ordinal) == 0) {
haveAllAttributes &= GetRequiredAttribute ("source-type", out string sourceType);
haveAllAttributes &= GetRequiredAttribute ("source-method-name", out string sourceMethodName);
Expand All @@ -134,7 +135,7 @@ void ReadXml (XmlReader reader, List<MamTypeReplacement> typeReplacements, List<

string sourceMethodSignature = reader.GetAttribute ("source-method-signature");
methodReplacements.Add (
new MamMethodReplacement (
new JniRemappingMethodReplacement (
sourceType, sourceMethodName, sourceMethodSignature,
targetType, targetMethodName, isStatic
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void AddEnvironment ()

bool haveRuntimeConfigBlob = !String.IsNullOrEmpty (RuntimeConfigBinFilePath) && File.Exists (RuntimeConfigBinFilePath);
var appConfState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<ApplicationConfigTaskState> (ApplicationConfigTaskState.RegisterTaskObjectKey, RegisteredTaskObjectLifetime.Build);
var mamNativeCodeInfo = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<GenerateMamNativeCode.MamNativeCodeInfo> (GenerateMamNativeCode.MamNativeCodeInfoKey, RegisteredTaskObjectLifetime.Build);
var jniRemappingNativeCodeInfo = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<GenerateJniRemappingNativeCode.JniRemappingNativeCodeInfo> (GenerateJniRemappingNativeCode.JniRemappingNativeCodeInfoKey, RegisteredTaskObjectLifetime.Build);
var appConfigAsmGen = new ApplicationConfigNativeAssemblyGenerator (environmentVariables, systemProperties, Log) {
IsBundledApp = IsBundledApplication,
UsesMonoAOT = usesMonoAOT,
Expand All @@ -430,8 +430,8 @@ void AddEnvironment ()
AndroidRuntimeJNIEnvToken = android_runtime_jnienv_class_token,
JNIEnvInitializeToken = jnienv_initialize_method_token,
JNIEnvRegisterJniNativesToken = jnienv_registerjninatives_method_token,
MAMReplacementTypeCount = mamNativeCodeInfo == null ? 0 : mamNativeCodeInfo.ReplacementTypeCount,
MAMReplacementMethodIndexEntryCount = mamNativeCodeInfo == null ? 0 : mamNativeCodeInfo.ReplacementMethodIndexEntryCount,
JniRemappingReplacementTypeCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementTypeCount,
JniRemappingReplacementMethodIndexEntryCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementMethodIndexEntryCount,
};
appConfigAsmGen.Init ();

Expand Down
6 changes: 3 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Tasks/PrepareAbiItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PrepareAbiItems : AndroidTask
const string TypeMapBase = "typemaps";
const string EnvBase = "environment";
const string CompressedAssembliesBase = "compressed_assemblies";
const string MamBase = "mam_remap";
const string JniRemappingBase = "jni_remap";

public override string TaskPrefix => "PAI";

Expand Down Expand Up @@ -51,8 +51,8 @@ public override bool RunTask ()
baseName = EnvBase;
} else if (String.Compare ("compressed", Mode, StringComparison.OrdinalIgnoreCase) == 0) {
baseName = CompressedAssembliesBase;
} else if (String.Compare ("mamremap", Mode, StringComparison.OrdinalIgnoreCase) == 0) {
baseName = MamBase;
} else if (String.Compare ("jniremap", Mode, StringComparison.OrdinalIgnoreCase) == 0) {
baseName = JniRemappingBase;
} else {
Log.LogError ($"Unknown mode: {Mode}");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public sealed class ApplicationConfig
public uint android_runtime_jnienv_class_token;
public uint jnienv_initialize_method_token;
public uint jnienv_registerjninatives_method_token;
public uint mam_replacement_type_count;
public uint mam_replacement_method_index_entry_count;
public uint jni_remapping_replacement_type_count;
public uint jni_remapping_replacement_method_index_entry_count;
public uint mono_components_mask;
public string android_package_name = String.Empty;
}
Expand Down Expand Up @@ -310,14 +310,14 @@ static ApplicationConfig ReadApplicationConfig (EnvironmentFile envFile)
ret.number_of_dso_cache_entries = ConvertFieldToUInt32 ("jnienv_registerjninatives_method_token", envFile.Path, parser.SourceFilePath, item.LineNumber, field [1]);
break;

case 21: // mam_replacement_type_count: uint32_t / .word | .long
case 21: // jni_remapping_replacement_type_count: uint32_t / .word | .long
Assert.IsTrue (expectedUInt32Types.Contains (field [0]), $"Unexpected uint32_t field type in '{envFile.Path}:{item.LineNumber}': {field [0]}");
ret.mam_replacement_type_count = ConvertFieldToUInt32 ("mam_replacement_type_count", envFile.Path, parser.SourceFilePath, item.LineNumber, field [1]);
ret.jni_remapping_replacement_type_count = ConvertFieldToUInt32 ("jni_remapping_replacement_type_count", envFile.Path, parser.SourceFilePath, item.LineNumber, field [1]);
break;

case 22: // mam_replacement_method_index_entry_count: uint32_t / .word | .long
case 22: // jni_remapping_replacement_method_index_entry_count: uint32_t / .word | .long
Assert.IsTrue (expectedUInt32Types.Contains (field [0]), $"Unexpected uint32_t field type in '{envFile.Path}:{item.LineNumber}': {field [0]}");
ret.mam_replacement_method_index_entry_count = ConvertFieldToUInt32 ("mam_replacement_method_index_entry_count", envFile.Path, parser.SourceFilePath, item.LineNumber, field [1]);
ret.jni_remapping_replacement_method_index_entry_count = ConvertFieldToUInt32 ("jni_remapping_replacement_method_index_entry_count", envFile.Path, parser.SourceFilePath, item.LineNumber, field [1]);
break;

case 23: // mono_components_mask: uint32_t / .word | .long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ sealed class ApplicationConfig
public uint android_runtime_jnienv_class_token;
public uint jnienv_initialize_method_token;
public uint jnienv_registerjninatives_method_token;
public uint mam_replacement_type_count;
public uint mam_replacement_method_index_entry_count;
public uint jni_remapping_replacement_type_count;
public uint jni_remapping_replacement_method_index_entry_count;
public uint mono_components_mask;
public string android_package_name = String.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ sealed class XamarinAndroidBundledAssembly
public int AndroidRuntimeJNIEnvToken { get; set; }
public int JNIEnvInitializeToken { get; set; }
public int JNIEnvRegisterJniNativesToken { get; set; }
public int MAMReplacementTypeCount { get; set; }
public int MAMReplacementMethodIndexEntryCount { get; set; }
public int JniRemappingReplacementTypeCount { get; set; }
public int JniRemappingReplacementMethodIndexEntryCount { get; set; }
public MonoComponent MonoComponents { get; set; }
public PackageNamingPolicy PackageNamingPolicy { get; set; }
public List<ITaskItem> NativeLibraries { get; set; }
Expand Down Expand Up @@ -214,8 +214,8 @@ public override void Init ()
android_runtime_jnienv_class_token = (uint)AndroidRuntimeJNIEnvToken,
jnienv_initialize_method_token = (uint)JNIEnvInitializeToken,
jnienv_registerjninatives_method_token = (uint)JNIEnvRegisterJniNativesToken,
mam_replacement_type_count = (uint)MAMReplacementTypeCount,
mam_replacement_method_index_entry_count = (uint)MAMReplacementMethodIndexEntryCount,
jni_remapping_replacement_type_count = (uint)JniRemappingReplacementTypeCount,
jni_remapping_replacement_method_index_entry_count = (uint)JniRemappingReplacementMethodIndexEntryCount,
mono_components_mask = (uint)MonoComponents,
android_package_name = AndroidPackageName,
};
Expand Down
Loading

0 comments on commit 0044761

Please sign in to comment.