Skip to content

Commit d2ade69

Browse files
authored
[Xamarin.Android.Build.Tasks] Fix native code generation when marshal methods are disabled (dotnet#7899)
Disabling marshal method generation via `$(AndroidEnableMarshalMethods)`=False turns off a lot of native code generation in the `MarshalMethodsNativeAssemblyGenerator` class, but the class is also responsible for outputting a correctly sized cache area (an array of *X* pointers) to be used by the native runtime to cache pointers to `MonoImage` instances. `libmonodroid.so` trusts that `Xamarin.Android.Build.Tasks.dll` et al will generate correct code, and thus does not verify the size of generated array. This trust, unfortunately, was broken because with marshal methods disabled, the native code generator created an output cache array that was 0 entries in size, thus leading to segfault when attempting to run the application. Fix the issue and also parametrize one of the on-device tests to be built twice, with marshal methods explicitly disabled and explicitly enabled.
1 parent 7da9e23 commit d2ade69

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ void AddEnvironment ()
413413
Log
414414
);
415415
} else {
416-
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (uniqueAssemblyNames);
416+
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (assemblyCount, uniqueAssemblyNames);
417417
}
418418
marshalMethodsAsmGen.Init ();
419419

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class KnownProperties
1212
public const string AndroidUseLatestPlatformSdk = "AndroidUseLatestPlatformSdk";
1313
public const string AndroidUseAapt2 = "AndroidUseAapt2";
1414
public const string AndroidCreatePackagePerAbi = "AndroidCreatePackagePerAbi";
15+
public const string AndroidEnableMarshalMethods = "AndroidEnableMarshalMethods";
1516

1617
public const string AndroidSupportedAbis = "AndroidSupportedAbis";
1718
public const string RuntimeIdentifier = "RuntimeIdentifier";

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ public AndroidLinkMode AndroidLinkModeRelease {
166166
set { SetProperty (ReleaseProperties, KnownProperties.AndroidLinkMode, value.ToString ()); }
167167
}
168168

169+
public bool EnableMarshalMethods {
170+
get { return string.Equals (GetProperty (KnownProperties.AndroidEnableMarshalMethods), "True", StringComparison.OrdinalIgnoreCase); }
171+
set { SetProperty (KnownProperties.AndroidEnableMarshalMethods, value.ToString ()); }
172+
}
173+
169174
public string AndroidManifest { get; set; }
170175
public string LayoutMain { get; set; }
171176
public string MainActivity { get; set; }

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ sealed class MarshalMethodName
204204
/// <summary>
205205
/// Constructor to be used ONLY when marshal methods are DISABLED
206206
/// </summary>
207-
public MarshalMethodsNativeAssemblyGenerator (ICollection<string> uniqueAssemblyNames)
207+
public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames)
208208
{
209+
this.numberOfAssembliesInApk = numberOfAssembliesInApk;
209210
this.uniqueAssemblyNames = uniqueAssemblyNames ?? throw new ArgumentNullException (nameof (uniqueAssemblyNames));
210211
generateEmptyCode = true;
211212
}

tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Teardown ()
2626
}
2727

2828
[Test]
29-
public void NativeAssemblyCacheWithSatelliteAssemblies ()
29+
public void NativeAssemblyCacheWithSatelliteAssemblies ([Values (true, false)] bool enableMarshalMethods)
3030
{
3131
var path = Path.Combine ("temp", TestName);
3232
var lib = new XamarinAndroidLibraryProject {
@@ -49,6 +49,7 @@ public void NativeAssemblyCacheWithSatelliteAssemblies ()
4949

5050
proj = new XamarinAndroidApplicationProject {
5151
IsRelease = true,
52+
EnableMarshalMethods = enableMarshalMethods,
5253
};
5354
proj.References.Add (new BuildItem.ProjectReference ($"..\\{lib.ProjectName}\\{lib.ProjectName}.csproj", lib.ProjectName, lib.ProjectGuid));
5455
proj.SetAndroidSupportedAbis ("armeabi-v7a", "arm64-v8a", "x86", "x86_64");

0 commit comments

Comments
 (0)