Skip to content

Commit b2bff06

Browse files
committed
Enable the JNI remapping p/invokes
1 parent 525ff1f commit b2bff06

File tree

7 files changed

+140
-5
lines changed

7 files changed

+140
-5
lines changed

src/native/clr/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(XAMARIN_MONODROID_SOURCES
3333
host-jni.cc
3434
host-util.cc
3535
internal-pinvokes.cc
36+
jni-remapping.cc
3637
os-bridge.cc
3738
pinvoke-override.cc
3839
typemap.cc

src/native/clr/host/generate-pinvoke-tables.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const std::vector<std::string> internal_pinvoke_names = {
5858
"_monodroid_gref_log_new",
5959
"monodroid_log",
6060
// "monodroid_log_traces",
61-
// "_monodroid_lookup_replacement_type",
62-
// "_monodroid_lookup_replacement_method_info",
61+
"_monodroid_lookup_replacement_type",
62+
"_monodroid_lookup_replacement_method_info",
6363
// "_monodroid_lref_log_delete",
6464
// "_monodroid_lref_log_new",
6565
// "_monodroid_max_gref_get",

src/native/clr/host/internal-pinvokes.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <host/os-bridge.hh>
33
#include <host/typemap.hh>
44
#include <runtime-base/internal-pinvokes.hh>
5+
#include <runtime-base/jni-remapping.hh>
56

67
using namespace xamarin::android;
78

@@ -75,3 +76,15 @@ void monodroid_free (void *ptr) noexcept
7576
{
7677
free (ptr);
7778
}
79+
80+
const char*
81+
_monodroid_lookup_replacement_type (const char *jniSimpleReference)
82+
{
83+
return JniRemapping::lookup_replacement_type (jniSimpleReference);
84+
}
85+
86+
const JniRemappingReplacementMethod*
87+
_monodroid_lookup_replacement_method_info (const char *jniSourceType, const char *jniMethodName, const char *jniMethodSignature)
88+
{
89+
return JniRemapping::lookup_replacement_method_info (jniSourceType, jniMethodName, jniMethodSignature);
90+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstring>
2+
3+
#include <runtime-base/logger.hh>
4+
#include <runtime-base/jni-remapping.hh>
5+
6+
#include "xamarin-app.hh"
7+
8+
using namespace xamarin::android;
9+
10+
[[gnu::always_inline]]
11+
auto JniRemapping::equal (JniRemappingString const& left, const char *right, size_t right_len) noexcept -> bool
12+
{
13+
if (left.length != static_cast<uint32_t>(right_len) || left.str[0] != *right) {
14+
return false;
15+
}
16+
17+
if (memcmp (left.str, right, right_len) == 0) {
18+
return true;
19+
}
20+
21+
return false;
22+
}
23+
24+
auto JniRemapping::lookup_replacement_type (const char *jniSimpleReference) noexcept -> const char*
25+
{
26+
if (application_config.jni_remapping_replacement_type_count == 0 || jniSimpleReference == nullptr || *jniSimpleReference == '\0') {
27+
return nullptr;
28+
}
29+
30+
size_t ref_len = strlen (jniSimpleReference);
31+
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_type_count; i++) {
32+
JniRemappingTypeReplacementEntry const& entry = jni_remapping_type_replacements[i];
33+
34+
if (equal (entry.name, jniSimpleReference, ref_len)) {
35+
return entry.replacement;
36+
}
37+
}
38+
39+
return nullptr;
40+
}
41+
42+
auto JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const char *jniMethodName, const char *jniMethodSignature) noexcept -> const JniRemappingReplacementMethod*
43+
{
44+
if (application_config.jni_remapping_replacement_method_index_entry_count == 0 ||
45+
jniSourceType == nullptr || *jniSourceType == '\0' ||
46+
jniMethodName == nullptr || *jniMethodName == '\0') {
47+
return nullptr;
48+
}
49+
50+
size_t source_type_len = strlen (jniSourceType);
51+
52+
const JniRemappingIndexTypeEntry *type = nullptr;
53+
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_method_index_entry_count; i++) {
54+
JniRemappingIndexTypeEntry const& entry = jni_remapping_method_replacement_index[i];
55+
56+
if (!equal (entry.name, jniSourceType, source_type_len)) {
57+
continue;
58+
}
59+
60+
type = &jni_remapping_method_replacement_index[i];
61+
break;
62+
}
63+
64+
if (type == nullptr || type->method_count == 0 || type->methods == nullptr) {
65+
return nullptr;
66+
}
67+
68+
size_t method_name_len = strlen (jniMethodName);
69+
size_t signature_len = jniMethodSignature == nullptr ? 0uz : strlen (jniMethodSignature);
70+
71+
for (size_t i = 0uz; i < type->method_count; i++) {
72+
JniRemappingIndexMethodEntry const& entry = type->methods[i];
73+
74+
if (!equal (entry.name, jniMethodName, method_name_len)) {
75+
continue;
76+
}
77+
78+
if (entry.signature.length == 0 || equal (entry.signature, jniMethodSignature, signature_len)) {
79+
return &type->methods[i].replacement;
80+
}
81+
82+
const char *sig_end = jniMethodSignature + signature_len;
83+
if (*sig_end == ')') {
84+
continue;
85+
}
86+
87+
while (sig_end != jniMethodSignature && *sig_end != ')') {
88+
sig_end--;
89+
}
90+
91+
if (equal (entry.signature, jniMethodSignature, static_cast<size_t>(sig_end - jniMethodSignature) + 1uz)) {
92+
return &type->methods[i].replacement;
93+
}
94+
}
95+
96+
return nullptr;
97+
}

src/native/clr/host/pinvoke-tables.include

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
namespace {
1212
#if INTPTR_MAX == INT64_MAX
1313
//64-bit internal p/invoke table
14-
std::array<PinvokeEntry, 11> internal_pinvokes {{
14+
std::array<PinvokeEntry, 13> internal_pinvokes {{
15+
{0x423c8f539a2c56d2, "_monodroid_lookup_replacement_type", reinterpret_cast<void*>(&_monodroid_lookup_replacement_type)},
1516
{0x4310c1531ddddc14, "__android_log_print", reinterpret_cast<void*>(&__android_log_print)},
1617
{0x4b1956138764939a, "_monodroid_gref_log_new", reinterpret_cast<void*>(&_monodroid_gref_log_new)},
1718
{0x9187e6bc6294cacf, "clr_typemap_managed_to_java", reinterpret_cast<void*>(&clr_typemap_managed_to_java)},
@@ -20,6 +21,7 @@ namespace {
2021
{0xae3df96dda0143bd, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
2122
{0xb8306f71b963cd3d, "monodroid_log", reinterpret_cast<void*>(&monodroid_log)},
2223
{0xb9bae9c43fb05089, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
24+
{0xc2a21d3f6c8ccc24, "_monodroid_lookup_replacement_method_info", reinterpret_cast<void*>(&_monodroid_lookup_replacement_method_info)},
2325
{0xd1e121b94ea63f2e, "_monodroid_gref_get", reinterpret_cast<void*>(&_monodroid_gref_get)},
2426
{0xd5151b00eb33d85e, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
2527
{0xf41c48df6f9be476, "monodroid_free", reinterpret_cast<void*>(&monodroid_free)},
@@ -516,14 +518,16 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x18
516518
constexpr hash_t system_globalization_native_library_hash = 0x28b5c8fca080abd5;
517519
#else
518520
//32-bit internal p/invoke table
519-
std::array<PinvokeEntry, 11> internal_pinvokes {{
521+
std::array<PinvokeEntry, 13> internal_pinvokes {{
520522
{0xb7a486a, "monodroid_TypeManager_get_java_class_name", reinterpret_cast<void*>(&monodroid_TypeManager_get_java_class_name)},
523+
{0x333d4835, "_monodroid_lookup_replacement_method_info", reinterpret_cast<void*>(&_monodroid_lookup_replacement_method_info)},
521524
{0x39e5b5d4, "__android_log_print", reinterpret_cast<void*>(&__android_log_print)},
522525
{0x656e00bd, "clr_typemap_managed_to_java", reinterpret_cast<void*>(&clr_typemap_managed_to_java)},
523526
{0xa04e5d1c, "monodroid_free", reinterpret_cast<void*>(&monodroid_free)},
524527
{0xb02468aa, "_monodroid_gref_get", reinterpret_cast<void*>(&_monodroid_gref_get)},
525528
{0xb6431f9a, "clr_typemap_java_to_managed", reinterpret_cast<void*>(&clr_typemap_java_to_managed)},
526529
{0xbe8d7701, "_monodroid_gref_log_new", reinterpret_cast<void*>(&_monodroid_gref_log_new)},
530+
{0xc439b5d7, "_monodroid_lookup_replacement_type", reinterpret_cast<void*>(&_monodroid_lookup_replacement_type)},
527531
{0xc5146c54, "_monodroid_gref_log_delete", reinterpret_cast<void*>(&_monodroid_gref_log_delete)},
528532
{0xe7e77ca5, "_monodroid_gref_log", reinterpret_cast<void*>(&_monodroid_gref_log)},
529533
{0xeac7f6e3, "xamarin_app_init", reinterpret_cast<void*>(&xamarin_app_init)},
@@ -1021,6 +1025,6 @@ constexpr hash_t system_security_cryptography_native_android_library_hash = 0x93
10211025
constexpr hash_t system_globalization_native_library_hash = 0xa66f1e5a;
10221026
#endif
10231027

1024-
constexpr size_t internal_pinvokes_count = 11;
1028+
constexpr size_t internal_pinvokes_count = 13;
10251029
constexpr size_t dotnet_pinvokes_count = 477;
10261030
} // end of anonymous namespace

src/native/clr/include/runtime-base/internal-pinvokes.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <jni.h>
44

5+
#include <xamarin-app.hh>
56
#include "logger.hh"
67

78
int _monodroid_gref_get () noexcept;
@@ -13,3 +14,5 @@ bool clr_typemap_java_to_managed (const char *java_type_name, char const** assem
1314
void monodroid_log (xamarin::android::LogLevel level, LogCategories category, const char *message) noexcept;
1415
char* monodroid_TypeManager_get_java_class_name (jclass klass) noexcept;
1516
void monodroid_free (void *ptr) noexcept;
17+
const char* _monodroid_lookup_replacement_type (const char *jniSimpleReference);
18+
const JniRemappingReplacementMethod* _monodroid_lookup_replacement_method_info (const char *jniSourceType, const char *jniMethodName, const char *jniMethodSignature);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "xamarin-app.hh"
4+
5+
namespace xamarin::android
6+
{
7+
class JniRemapping final
8+
{
9+
public:
10+
static auto lookup_replacement_type (const char *jniSimpleReference) noexcept -> const char*;
11+
static auto lookup_replacement_method_info (const char *jniSourceType, const char *jniMethodName, const char *jniMethodSignature) noexcept -> const JniRemappingReplacementMethod*;
12+
13+
private:
14+
[[gnu::nonnull (2)]]
15+
static auto equal (JniRemappingString const& left, const char *right, size_t right_len) noexcept -> bool;
16+
};
17+
}

0 commit comments

Comments
 (0)