Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
SetSynchronizationContext ();
}

[DllImport ("xamarin-app")]
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
static extern unsafe void xamarin_app_init (IntPtr env, delegate* unmanaged <int, int, int, IntPtr*, void> get_function_pointer);

static void SetSynchronizationContext () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ void AddEnvironment ()
JniRemappingReplacementTypeCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementTypeCount,
JniRemappingReplacementMethodIndexEntryCount = jniRemappingNativeCodeInfo == null ? 0 : jniRemappingNativeCodeInfo.ReplacementMethodIndexEntryCount,
MarshalMethodsEnabled = EnableMarshalMethods,
ManagedMarshalMethodsLookupEnabled = EnableManagedMarshalMethodsLookup,
IgnoreSplitConfigs = ShouldIgnoreSplitConfigs (),
};
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ sealed class ApplicationConfigCLR
public uint jni_remapping_replacement_type_count;
public uint jni_remapping_replacement_method_index_entry_count;
public string android_package_name = String.Empty;
public bool managed_marshal_methods_lookup_enabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ sealed class XamarinAndroidBundledAssembly
public PackageNamingPolicy PackageNamingPolicy { get; set; }
public List<ITaskItem> NativeLibraries { get; set; }
public bool MarshalMethodsEnabled { get; set; }
public bool ManagedMarshalMethodsLookupEnabled { get; set; }
public bool IgnoreSplitConfigs { get; set; }

public ApplicationConfigNativeAssemblyGeneratorCLR (IDictionary<string, string> environmentVariables, IDictionary<string, string> systemProperties,
Expand Down Expand Up @@ -256,6 +257,7 @@ protected override void Construct (LlvmIrModule module)
uses_assembly_preload = UsesAssemblyPreload,
jni_add_native_method_registration_attribute_present = JniAddNativeMethodRegistrationAttributePresent,
marshal_methods_enabled = MarshalMethodsEnabled,
managed_marshal_methods_lookup_enabled = ManagedMarshalMethodsLookupEnabled,
ignore_split_configs = IgnoreSplitConfigs,
number_of_runtime_properties = (uint)(runtimeProperties == null ? 0 : runtimeProperties.Count),
package_naming_policy = (uint)PackageNamingPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Utilities;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Java.Interop.Tools.Cecil;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks
Expand Down Expand Up @@ -82,6 +83,12 @@ public void Rewrite (bool brokenExceptionTransitions)
continue;
}

if (HasUnmanagedCallersOnlyAttribute (method.NativeCallback)) {
log.LogDebugMessage ($"[{targetArch}] Method '{method.NativeCallback.FullName}' does not need a wrapper, it already has UnmanagedCallersOnlyAttribute");
method.NativeCallbackWrapper = method.NativeCallback;
continue;
}

method.NativeCallbackWrapper = GenerateWrapper (method, assemblyImports, brokenExceptionTransitions);
if (method.Connector != null) {
if (method.Connector.IsStatic && method.Connector.IsPrivate) {
Expand Down Expand Up @@ -186,6 +193,17 @@ void RemoveFile (string? path)
log.LogDebugMessage ($"[{targetArch}] {ex.ToString ()}");
}
}

static bool HasUnmanagedCallersOnlyAttribute (MethodDefinition method)
{
foreach (CustomAttribute ca in method.CustomAttributes) {
if (ca.Constructor.DeclaringType.FullName == "System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute") {
return true;
}
}

return false;
}
}

MethodDefinition GenerateWrapper (MarshalMethodEntry method, Dictionary<AssemblyDefinition, AssemblyImports> assemblyImports, bool brokenExceptionTransitions)
Expand Down
1 change: 1 addition & 0 deletions src/native/clr/host/generate-pinvoke-tables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const std::vector<std::string> internal_pinvoke_names = {
// "recv_uninterrupted",
// "send_uninterrupted",
// "set_world_accessable",
"xamarin_app_init",

// We can treat liblog as "internal", since we link against it
"__android_log_print",
Expand Down
3 changes: 3 additions & 0 deletions src/native/clr/host/host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ void Host::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass runtimeCl
init.jniAddNativeMethodRegistrationAttributePresent = application_config.jni_add_native_method_registration_attribute_present ? 1 : 0;
init.jniRemappingInUse = application_config.jni_remapping_replacement_type_count > 0 || application_config.jni_remapping_replacement_method_index_entry_count > 0;
init.marshalMethodsEnabled = application_config.marshal_methods_enabled;
init.managedMarshalMethodsLookupEnabled = application_config.managed_marshal_methods_lookup_enabled;
abort_unless (!init.marshalMethodsEnabled || init.managedMarshalMethodsLookupEnabled,
"Managed marshal methods lookup must be enabled if marshal methods are enabled");

// GC threshold is 90% of the max GREF count
init.grefGcThreshold = static_cast<int>(AndroidSystem::get_gref_gc_threshold ());
Expand Down
4 changes: 4 additions & 0 deletions src/native/clr/host/pinvoke-override.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

using namespace xamarin::android;

#if defined (DEBUG)
void xamarin_app_init ([[maybe_unused]] JNIEnv *env, [[maybe_unused]] get_function_pointer_fn fn) noexcept {}
#endif

#include "pinvoke-tables.include"

[[gnu::flatten]]
Expand Down
8 changes: 5 additions & 3 deletions src/native/clr/host/pinvoke-tables.include
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
namespace {
#if INTPTR_MAX == INT64_MAX
//64-bit internal p/invoke table
std::array<PinvokeEntry, 10> internal_pinvokes {{
std::array<PinvokeEntry, 11> internal_pinvokes {{
{0x4310c1531ddddc14, "__android_log_print", reinterpret_cast<void*>(&__android_log_print)},
{0x4b1956138764939a, "_monodroid_gref_log_new", reinterpret_cast<void*>(&_monodroid_gref_log_new)},
{0x9187e6bc6294cacf, "clr_typemap_managed_to_java", reinterpret_cast<void*>(&clr_typemap_managed_to_java)},
{0x9a946dfe9916a942, "clr_typemap_java_to_managed", reinterpret_cast<void*>(&clr_typemap_java_to_managed)},
{0xa7f58f3ee428cc6b, "_monodroid_gref_log_delete", reinterpret_cast<void*>(&_monodroid_gref_log_delete)},
{0xae3df96dda0143bd, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
{0xb8306f71b963cd3d, "monodroid_log", reinterpret_cast<void*>(&monodroid_log)},
{0xb9bae9c43fb05089, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
{0xd1e121b94ea63f2e, "_monodroid_gref_get", reinterpret_cast<void*>(&_monodroid_gref_get)},
{0xd5151b00eb33d85e, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
{0xf41c48df6f9be476, "monodroid_free", reinterpret_cast<void*>(&monodroid_free)},
Expand Down Expand Up @@ -515,7 +516,7 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x18
constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
#else
//32-bit internal p/invoke table
std::array<PinvokeEntry, 10> internal_pinvokes {{
std::array<PinvokeEntry, 11> internal_pinvokes {{
{0xb7a486a, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
{0x39e5b5d4, "__android_log_print", reinterpret_cast<void*>(&__android_log_print)},
{0x656e00bd, "clr_typemap_managed_to_java", reinterpret_cast<void*>(&clr_typemap_managed_to_java)},
Expand All @@ -525,6 +526,7 @@ constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
{0xbe8d7701, "_monodroid_gref_log_new", reinterpret_cast<void*>(&_monodroid_gref_log_new)},
{0xc5146c54, "_monodroid_gref_log_delete", reinterpret_cast<void*>(&_monodroid_gref_log_delete)},
{0xe7e77ca5, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
{0xeac7f6e3, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
{0xfa4e32ca, "monodroid_log", reinterpret_cast<void*>(&monodroid_log)},
}};

Expand Down Expand Up @@ -1019,6 +1021,6 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x93
constexpr hash_t system_globalization_native_library_hash = 0xa66f1e5a;
#endif

constexpr size_t internal_pinvokes_count = 10;
constexpr size_t internal_pinvokes_count = 11;
constexpr size_t dotnet_pinvokes_count = 477;
} // end of anonymous namespace
1 change: 1 addition & 0 deletions src/native/clr/include/runtime-base/internal-pinvokes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <jni.h>

#include "logger.hh"
#include "xamarin-app.hh"

int _monodroid_gref_get () noexcept;
void _monodroid_gref_log (const char *message) noexcept;
Expand Down
7 changes: 3 additions & 4 deletions src/native/clr/include/xamarin-app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ struct AssemblyStoreSingleAssemblyRuntimeData final
};

// Keep in strict sync with:
// src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfig.cs
// src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigCLR.cs
// src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/EnvironmentHelper.cs
struct ApplicationConfig
{
Expand Down Expand Up @@ -394,8 +394,7 @@ struct MarshalMethodName

[[gnu::visibility("default")]] extern const char* const mm_class_names[];
[[gnu::visibility("default")]] extern const MarshalMethodName mm_method_names[];
#endif // def RELEASE

using get_function_pointer_fn = void(*)(uint32_t mono_image_index, uint32_t class_index, uint32_t method_token, void*& target_ptr);

[[gnu::visibility("default")]] extern void xamarin_app_init (JNIEnv *env, get_function_pointer_fn fn) noexcept;
#endif // def RELEASE
extern "C" [[gnu::visibility("default")]] extern void xamarin_app_init (JNIEnv *env, get_function_pointer_fn fn) noexcept;
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const std::vector<std::string> internal_pinvoke_names = {
"recv_uninterrupted",
"send_uninterrupted",
"set_world_accessable",
"xamarin_app_init",
};

const std::vector<std::string> dotnet_pinvoke_names = {
Expand Down
8 changes: 5 additions & 3 deletions src/native/mono/pinvoke-override/pinvoke-tables.include
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace {
#if INTPTR_MAX == INT64_MAX
//64-bit internal p/invoke table
std::array<PinvokeEntry, 45> internal_pinvokes {{
std::array<PinvokeEntry, 46> internal_pinvokes {{
{0xa50ce5de13bf8b5, "_monodroid_timezone_get_default_id", reinterpret_cast<void*>(&_monodroid_timezone_get_default_id)},
{0x19055d65edfd668e, "_monodroid_get_network_interface_up_state", reinterpret_cast<void*>(&_monodroid_get_network_interface_up_state)},
{0x2b3b0ca1d14076da, "monodroid_get_dylib", reinterpret_cast<void*>(&monodroid_get_dylib)},
Expand All @@ -35,6 +35,7 @@ namespace {
{0xae3df96dda0143bd, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
{0xb6222d90af401865, "_monodroid_weak_gref_get", reinterpret_cast<void*>(&_monodroid_weak_gref_get)},
{0xb8306f71b963cd3d, "monodroid_log", reinterpret_cast<void*>(&monodroid_log)},
{0xb9bae9c43fb05089, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
{0xbc90bafd5ff9c99e, "_monodroid_get_dns_servers", reinterpret_cast<void*>(&_monodroid_get_dns_servers)},
{0xbe5a300beec69c35, "monodroid_get_system_property", reinterpret_cast<void*>(&monodroid_get_system_property)},
{0xbfbb924fbe190616, "monodroid_dylib_mono_free", reinterpret_cast<void*>(&monodroid_dylib_mono_free)},
Expand Down Expand Up @@ -549,7 +550,7 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x18
constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
#else
//32-bit internal p/invoke table
std::array<PinvokeEntry, 45> internal_pinvokes {{
std::array<PinvokeEntry, 46> internal_pinvokes {{
{0xb7a486a, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
{0xf562bd9, "monodroid_embedded_assemblies_set_assemblies_prefix", reinterpret_cast<void*>(&monodroid_embedded_assemblies_set_assemblies_prefix)},
{0x227a2636, "monodroid_get_namespaced_system_property", reinterpret_cast<void*>(&monodroid_get_namespaced_system_property)},
Expand Down Expand Up @@ -588,6 +589,7 @@ constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
{0xe4c3ee19, "monodroid_log_traces", reinterpret_cast<void*>(&monodroid_log_traces)},
{0xe7e77ca5, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
{0xea2184e3, "_monodroid_gc_wait_for_bridge_processing", reinterpret_cast<void*>(&_monodroid_gc_wait_for_bridge_processing)},
{0xeac7f6e3, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
{0xf4079b4a, "monodroid_dylib_mono_new", reinterpret_cast<void*>(&monodroid_dylib_mono_new)},
{0xf5a0ac55, "set_world_accessable", reinterpret_cast<void*>(&set_world_accessable)},
{0xf61941c3, "recv_uninterrupted", reinterpret_cast<void*>(&recv_uninterrupted)},
Expand Down Expand Up @@ -1087,6 +1089,6 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x93
constexpr hash_t system_globalization_native_library_hash = 0xa66f1e5a;
#endif

constexpr size_t internal_pinvokes_count = 45;
constexpr size_t internal_pinvokes_count = 46;
constexpr size_t dotnet_pinvokes_count = 477;
} // end of anonymous namespace
4 changes: 4 additions & 0 deletions src/native/mono/pinvoke-override/precompiled.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

using namespace xamarin::android;

#if defined (DEBUG)
void xamarin_app_init ([[maybe_unused]] JNIEnv *env, [[maybe_unused]] get_function_pointer_fn fn) noexcept {}
#endif

#include "pinvoke-tables.include"

[[gnu::flatten]]
Expand Down
4 changes: 2 additions & 2 deletions src/native/mono/xamarin-app-stub/xamarin-app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ struct MarshalMethodName
MONO_API MONO_API_EXPORT const char* const mm_class_names[];
MONO_API MONO_API_EXPORT const MarshalMethodName mm_method_names[];

using get_function_pointer_fn = void(*)(uint32_t mono_image_index, uint32_t class_index, uint32_t method_token, void*& target_ptr);
#endif // def RELEASE

using get_function_pointer_fn = void(*)(uint32_t mono_image_index, uint32_t class_index, uint32_t method_token, void*& target_ptr);
MONO_API MONO_API_EXPORT void xamarin_app_init (JNIEnv *env, get_function_pointer_fn fn) noexcept;
#endif // def RELEASE

#endif // __XAMARIN_ANDROID_TYPEMAP_H