-
Notifications
You must be signed in to change notification settings - Fork 566
[release/10.0.1xx] Support for exempting native libraries from JNI preload #10879
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
jonathanpeppers
merged 6 commits into
release/10.0.1xx
from
dev/peppers/jnipreload-net10
Mar 5, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3b9ec2
Support for exempting native libraries from JNI preload (#10787)
grendello 8a683ec
Update Documentation/docs-mobile/building-apps/build-items.md
jonathanpeppers 4fbaec9
Remove GetMinimumApiLevel - XABuildConfig.ArchToApiLevel/ArchToApiLev…
jonathanpeppers 6c46e19
Remove IgnoreUnsupportedConfiguration call - method doesn't exist on …
jonathanpeppers 1d554bb
[docs] Fix typos and documentation wording from review (#10881)
Copilot f725e77
Run JNI preload tests on MonoVM only
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
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
269 changes: 269 additions & 0 deletions
269
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest3.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,269 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
| using Xamarin.Android.Tools; | ||
| using Xamarin.ProjectTools; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests; | ||
|
|
||
| [Parallelizable (ParallelScope.Children)] | ||
| public partial class BuildTest3 : BaseTest | ||
| { | ||
| const int ExpectedJniPreloadIndexStride = 4; | ||
| const string JniPreloadSourceLibraryName = "libtest-jni-library.so"; | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_NoDuplicates () | ||
| { | ||
| const string MyLibKeep1 = "libMyStuffKeep.so"; | ||
| const string MyLibKeep2 = "libMyStuffKeep.so"; | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, MyLibKeep1, MyLibKeep2); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| NativeLibraryJniPreload_VerifyLibs (allPreloads, new List<string> { MyLibKeep1 }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_IncludeCustomLibraries () | ||
| { | ||
| const string MyLib = "libMyStuff.so"; | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, MyLib); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| NativeLibraryJniPreload_VerifyLibs (allPreloads, new List<string> { MyLib }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_ExcludeSomeCustomLibraries () | ||
| { | ||
| const string MyLibKeep = "libMyStuffKeep.so"; | ||
| const string MyLibExempt = "libMyStuffExempt.so"; | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, MyLibKeep, MyLibExempt); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibraryNoJniPreload (MyLibExempt) | ||
| ); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| NativeLibraryJniPreload_VerifyLibs (allPreloads, new List<string> { MyLibKeep }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_ExcludeAllCustomLibraries () | ||
| { | ||
| const string MyLibExempt1 = "libMyStuffExempt1.so"; | ||
| const string MyLibExempt2 = "libMyStuffExempt2.so"; | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, MyLibExempt1, MyLibExempt2); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibraryNoJniPreload (MyLibExempt1) | ||
| ); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibraryNoJniPreload (MyLibExempt2) | ||
| ); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| NativeLibraryJniPreload_VerifyDefaults (allPreloads); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_AddSomeCustomLibrariesAndIgnoreAll () | ||
| { | ||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, "libMyStuffOne.so", "libMyStuffTwo.so"); | ||
| proj.SetProperty ("AndroidIgnoreAllJniPreload", "true"); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| // With `$(AndroidIgnoreAllJniPreload)=true` we still must have the defaults in the generated code. | ||
| NativeLibraryJniPreload_VerifyDefaults (allPreloads); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_AddSomeCustomLibrariesAndIgnoreAllByName () | ||
| { | ||
| const string MyLibExemptOne = "libMyStuffExemptOne.so"; | ||
| const string MyLibExemptTwo = "libMyStuffExemptTwo.so"; | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| NativeLibraryJniPreload_AddNativeLibraries (proj, supportedArches, MyLibExemptOne, MyLibExemptTwo); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibraryNoJniPreload (MyLibExemptOne) | ||
| ); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibraryNoJniPreload (MyLibExemptTwo) | ||
| ); | ||
| } | ||
| ); | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| // With all custom libraries ignored, we still must have the defaults in the generated code. | ||
| NativeLibraryJniPreload_VerifyDefaults (allPreloads); | ||
| } | ||
|
|
||
| void NativeLibraryJniPreload_AddNativeLibraries (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches, string libName, params string[]? moreLibNames) | ||
| { | ||
| var libNames = new List<string> { | ||
| libName, | ||
| }; | ||
| if (moreLibNames != null && moreLibNames.Length > 0) { | ||
| libNames.AddRange (moreLibNames); | ||
| } | ||
|
|
||
| foreach (AndroidTargetArch arch in supportedArches) { | ||
| string libPath = Path.Combine (XABuildPaths.TestAssemblyOutputDirectory, MonoAndroidHelper.ArchToRid (arch), JniPreloadSourceLibraryName); | ||
| Assert.IsTrue (File.Exists (libPath), $"Native library '{libPath}' does not exist."); | ||
|
|
||
| foreach (string lib in libNames) { | ||
| string abi = MonoAndroidHelper.ArchToAbi (arch); | ||
| proj.OtherBuildItems.Add ( | ||
| new AndroidItem.AndroidNativeLibrary ($"native/{abi}/{lib}") { | ||
| BinaryContent = () => File.ReadAllBytes (libPath), | ||
| MetadataValues = $"Link={abi}/{lib}", | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_IgnoreAll_PreservesRequired () | ||
| { | ||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads ( | ||
| (XamarinAndroidApplicationProject proj, AndroidTargetArch[] supportedArches) => { | ||
| proj.SetProperty ("AndroidIgnoreAllJniPreload", "true"); | ||
| } | ||
| ); | ||
|
|
||
| // With `$(AndroidIgnoreAllJniPreload)=true` we still must have the defaults in the generated code. | ||
| NativeLibraryJniPreload_VerifyDefaults (allPreloads); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NativeLibraryJniPreload_DefaultsWork () | ||
| { | ||
| List<EnvironmentHelper.JniPreloads>? allPreloads = NativeLibraryJniPreload_CommonInitAndGetPreloads (); | ||
| NativeLibraryJniPreload_VerifyDefaults (allPreloads); | ||
| } | ||
|
|
||
| void NativeLibraryJniPreload_VerifyDefaults (List<EnvironmentHelper.JniPreloads>? allPreloads) | ||
| { | ||
| NativeLibraryJniPreload_VerifyLibs (allPreloads, additionalLibs: null); | ||
| } | ||
|
|
||
| void NativeLibraryJniPreload_VerifyLibs (List<EnvironmentHelper.JniPreloads>? allPreloads, List<string>? additionalLibs) | ||
| { | ||
| if (allPreloads == null) { | ||
| return; | ||
| } | ||
|
|
||
| int numberOfLibs = 1; | ||
| if (additionalLibs != null) { | ||
| numberOfLibs += additionalLibs.Count; | ||
| } | ||
|
|
||
| int ExpectedEntryCount = ExpectedJniPreloadIndexStride * numberOfLibs; | ||
| foreach (EnvironmentHelper.JniPreloads preloads in allPreloads) { | ||
| Assert.IsTrue (preloads.IndexStride == (uint)ExpectedJniPreloadIndexStride, $"JNI preloads index stride should be {ExpectedJniPreloadIndexStride}, was {preloads.IndexStride} instead. Source file: {preloads.SourceFile}"); | ||
| Assert.IsTrue (preloads.Entries.Count == ExpectedEntryCount, $"JNI preloads index entry count should be {ExpectedEntryCount}, was {preloads.Entries.Count} instead. Source file: {preloads.SourceFile}"); | ||
|
|
||
| // DSO cache entries are sorted based on their **mutated name's** 64-bit xxHash, which | ||
| // won't change but builds may add/remove libraries and, thus, change the indexes after | ||
| // sorting. For that reason we don't verify the index values and use them just for reporting. | ||
| // | ||
| // Also, all the entries will point to the same library name. Name variations aren't | ||
| // stored directly in the DSO cache, just their hashes which are used for lookup at run time. | ||
| // | ||
| // We use a Dictionary<> here because there might be more libraries to preload in the future. | ||
| var expectedLibNames = new Dictionary<string, uint> (StringComparer.Ordinal) { | ||
| { "libSystem.Security.Cryptography.Native.Android.so", 0 }, | ||
| }; | ||
|
|
||
| if (additionalLibs != null) { | ||
| foreach (string extraLib in additionalLibs) { | ||
| expectedLibNames.Add (extraLib, 0); | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < preloads.Entries.Count; i++) { | ||
| EnvironmentHelper.JniPreloadsEntry entry = preloads.Entries[i]; | ||
| Assert.IsFalse (entry.LibraryName == "libmonodroid.so", $"JNI preloads entry at index {i} refers to the .NET for Android native runtime. It must never be preloaded. Source file: {preloads.SourceFile}"); | ||
| Assert.IsTrue (expectedLibNames.ContainsKey (entry.LibraryName), $"JNI preloads entry at index {i}, referring to library at DSO cache index {entry.Index} has unexpected name '{entry.LibraryName}'; Source file: {preloads.SourceFile}"); | ||
| expectedLibNames[entry.LibraryName]++; | ||
| } | ||
|
|
||
| foreach (var kvp in expectedLibNames) { | ||
| Assert.IsTrue (kvp.Value == ExpectedJniPreloadIndexStride, $"JNI preloads entry '{kvp.Key}' should have {ExpectedJniPreloadIndexStride} instances, it had {kvp.Value} instead. Source file: {preloads.SourceFile}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| List<EnvironmentHelper.JniPreloads>? NativeLibraryJniPreload_CommonInitAndGetPreloads (Action<XamarinAndroidApplicationProject, AndroidTargetArch[]>? configureProject = null) | ||
| { | ||
| const bool isRelease = true; | ||
| const AndroidRuntime runtime = AndroidRuntime.MonoVM; | ||
|
|
||
| AndroidTargetArch[] supportedArches = new [] { | ||
| AndroidTargetArch.Arm64, | ||
| AndroidTargetArch.X86_64, | ||
| }; | ||
|
|
||
| var proj = new XamarinAndroidApplicationProject { | ||
| IsRelease = isRelease, | ||
| }; | ||
| proj.SetRuntime (runtime); | ||
| proj.SetRuntimeIdentifiers (supportedArches); | ||
| configureProject?.Invoke (proj, supportedArches); | ||
|
|
||
| using var builder = CreateApkBuilder (); | ||
| Assert.IsTrue (builder.Build (proj), "Build should have succeeded."); | ||
|
|
||
| string objDirPath = Path.Combine (Root, builder.ProjectDirectory, proj.IntermediateOutputPath); | ||
| List<EnvironmentHelper.EnvironmentFile> envFiles = EnvironmentHelper.GatherEnvironmentFiles ( | ||
| objDirPath, | ||
| String.Join (";", supportedArches.Select (arch => MonoAndroidHelper.ArchToAbi (arch))), | ||
| true | ||
| ); | ||
|
|
||
| EnvironmentHelper.ApplicationConfig app_config = EnvironmentHelper.ReadApplicationConfig (envFiles); | ||
| uint numberOfDsoCacheEntries = app_config.number_of_dso_cache_entries; | ||
|
|
||
| return EnvironmentHelper.ReadJniPreloads (envFiles, numberOfDsoCacheEntries, runtime); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.