Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions src/native/clr/include/runtime-base/monodroid-dl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ namespace xamarin::android
log_debugf (LOG_ASSEMBLY, "monodroid_dlopen: hash match %sfound, DSO name is '%.*s'", dso == nullptr ? "not " : "", static_cast<int>(dso_name_for_log.length ()), dso_name_for_log.data ());

if (dso == nullptr) {
// DSO not known at build time, try to load it. Since we don't know whether or not the library uses
// JNI, we're going to assume it does and thus use System.loadLibrary eventually.
return DsoLoader::load (name, flags, true /* is_jni */);
// Packaged libraries have build-time JNI metadata in the DSO cache. A cache miss is therefore
// assumed to be a platform or other non-JNI library, avoiding a synchronous System.loadLibrary
// dispatch to the main thread.
constexpr bool IsJniLibrary = false;
constexpr bool SkipExistsCheck = true;
return DsoLoader::load<SkipExistsCheck> (name, flags, IsJniLibrary);
} else if (dso->handle != nullptr) {
log_debugf (LOG_ASSEMBLY, "monodroid_dlopen: library %.*s already loaded, returning handle %p", static_cast<int>(name.length ()), name.data (), dso->handle);
return dso->handle;
Expand Down
62 changes: 62 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/NativeLibraryLoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,68 @@ namespace Xamarin.Android.Build.Tests;
[Category ("UsesDevice")]
public class NativeLibraryLoadTests : DeviceTest
{
[Test]
public void UnknownSystemLibraryLoadsWithoutMainThreadDispatch ()
{
if (IgnoreUnsupportedConfiguration (AndroidRuntime.CoreCLR, release: false)) {
return;
}

const string completedMessage = "UNKNOWN_SYSTEM_LIBRARY_LOAD_COMPLETED";
const string timeoutMessage = "UNKNOWN_SYSTEM_LIBRARY_LOAD_TIMED_OUT";
string packageName = PackageUtils.MakePackageName (AndroidRuntime.CoreCLR, "unknownsystemlibrary");
var proj = new XamarinAndroidApplicationProject (packageName: packageName) {
EmbedAssembliesIntoApk = true,
ProjectName = "UnknownSystemLibrary",
};
proj.SetRuntime (AndroidRuntime.CoreCLR);
proj.SetRuntimeIdentifiers ([DeviceAbi]);
proj.SetDefaultTargetDevice ();
proj.MainActivity = proj.DefaultMainActivity
.Replace ("//${USINGS}", "using System;\nusing System.Runtime.InteropServices;\nusing System.Threading.Tasks;")
.Replace (
"//${AFTER_ONCREATE}",
$$"""
// Blocking the main thread here verifies CoreCLR directly loads libraries missing from the
// build-time DSO cache instead of dispatching System.loadLibrary to the main thread.
var loadTask = Task.Run (NativeMethods.GetError);
if (!loadTask.Wait (TimeSpan.FromSeconds (2))) {
Android.Util.Log.Error ("NativeLibraryLoadTest", "{{timeoutMessage}}");
return;
}
Android.Util.Log.Info ("NativeLibraryLoadTest", "{{completedMessage}}");
"""
)
.Replace (
"//${AFTER_MAINACTIVITY}",
"""
static class NativeMethods
{
[DllImport ("libGLESv2", EntryPoint = "glGetError")]
public static extern uint GetError ();
}
"""
);

using var builder = CreateApkBuilder (packageName: packageName);
Assert.IsTrue (builder.Install (proj), "Project should have installed.");

ClearAdbLogcat ();
bool loadTimedOut = false;
bool appCompleted = MonitorAdbLogcat (
line => {
loadTimedOut |= line.Contains (timeoutMessage, StringComparison.Ordinal);
return loadTimedOut || line.Contains (completedMessage, StringComparison.Ordinal);
},
Path.Combine (Root, builder.ProjectDirectory, "unknown-system-library-load.log"),
timeout: 30,
onMonitoringStarted: () => StartActivityAndAssert (proj)
);

Assert.IsFalse (loadTimedOut, "The worker P/Invoke waited for the blocked Android main thread.");
Assert.IsTrue (appCompleted, $"Output did not contain {completedMessage}.");
}

[Test]
public void MissingNativeLibraryHasUsefulErrorMessage ([Values (AndroidRuntime.CoreCLR, AndroidRuntime.NativeAOT)] AndroidRuntime runtime)
{
Expand Down
Loading