-
Notifications
You must be signed in to change notification settings - Fork 530
/
embedded-assemblies.cc
979 lines (820 loc) · 32.7 KB
/
embedded-assemblies.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#include <host-config.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <ctype.h>
#include <libgen.h>
#include <errno.h>
#include <unistd.h>
#include <climits>
#if defined (HAVE_LZ4)
#include <lz4.h>
#endif
#include <mono/metadata/assembly.h>
#include <mono/metadata/image.h>
#include <mono/metadata/mono-config.h>
#include "java-interop-util.h"
#include "monodroid.h"
#include "util.hh"
#include "embedded-assemblies.hh"
#include "globals.hh"
#include "monodroid-glue.hh"
#include "xamarin-app.hh"
#include "cpp-util.hh"
using namespace xamarin::android;
using namespace xamarin::android::internal;
// A utility class which allows us to manage memory allocated by `mono_guid_to_string` in an elegant way. We can create
// temporary instances of this class in calls to e.g. `log_debug` which are executed ONLY when debug logging is enabled
class MonoGuidString
{
public:
explicit MonoGuidString (const uint8_t *id) noexcept
{
guid = mono_guid_to_string (id);
}
~MonoGuidString ()
{
::free (guid);
}
const char* get () const noexcept
{
return guid;
}
private:
char *guid = nullptr;
};
void EmbeddedAssemblies::set_assemblies_prefix (const char *prefix)
{
if (assemblies_prefix_override != nullptr)
delete[] assemblies_prefix_override;
assemblies_prefix_override = prefix != nullptr ? utils.strdup_new (prefix) : nullptr;
}
force_inline void
EmbeddedAssemblies::get_assembly_data (const MonoBundledAssembly *e, char*& assembly_data, uint32_t& assembly_data_size)
{
#if defined (ANDROID) && defined (HAVE_LZ4) && defined (RELEASE)
auto header = reinterpret_cast<const CompressedAssemblyHeader*>(e->data);
if (header->magic == COMPRESSED_DATA_MAGIC) {
if (XA_UNLIKELY (compressed_assemblies.descriptors == nullptr)) {
log_fatal (LOG_ASSEMBLY, "Compressed assembly found but no descriptor defined");
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
if (XA_UNLIKELY (header->descriptor_index >= compressed_assemblies.count)) {
log_fatal (LOG_ASSEMBLY, "Invalid compressed assembly descriptor index %u", header->descriptor_index);
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
CompressedAssemblyDescriptor &cad = compressed_assemblies.descriptors[header->descriptor_index];
assembly_data_size = e->size - sizeof(CompressedAssemblyHeader);
if (!cad.loaded) {
if (XA_UNLIKELY (cad.data == nullptr)) {
log_fatal (LOG_ASSEMBLY, "Invalid compressed assembly descriptor at %u: no data", header->descriptor_index);
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
timing_period decompress_time;
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
decompress_time.mark_start ();
}
if (header->uncompressed_length != cad.uncompressed_file_size) {
if (header->uncompressed_length > cad.uncompressed_file_size) {
log_fatal (LOG_ASSEMBLY, "Compressed assembly '%s' is larger than when the application was built (expected at most %u, got %u). Assemblies don't grow just like that!", e->name, cad.uncompressed_file_size, header->uncompressed_length);
exit (FATAL_EXIT_MISSING_ASSEMBLY);
} else {
log_debug (LOG_ASSEMBLY, "Compressed assembly '%s' is smaller than when the application was built. Adjusting accordingly.", e->name);
}
cad.uncompressed_file_size = header->uncompressed_length;
}
const char *data_start = reinterpret_cast<const char*>(e->data + sizeof(CompressedAssemblyHeader));
int ret = LZ4_decompress_safe (data_start, reinterpret_cast<char*>(cad.data), static_cast<int>(assembly_data_size), static_cast<int>(cad.uncompressed_file_size));
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
decompress_time.mark_end ();
TIMING_LOG_INFO (decompress_time, "%s LZ4 decompression time", e->name);
}
if (ret < 0) {
log_fatal (LOG_ASSEMBLY, "Decompression of assembly %s failed with code %d", e->name, ret);
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
if (static_cast<uint64_t>(ret) != cad.uncompressed_file_size) {
log_debug (LOG_ASSEMBLY, "Decompression of assembly %s yielded a different size (expected %lu, got %u)", e->name, cad.uncompressed_file_size, static_cast<uint32_t>(ret));
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
cad.loaded = true;
}
assembly_data = reinterpret_cast<char*>(cad.data);
assembly_data_size = cad.uncompressed_file_size;
} else
#endif
{
assembly_data = reinterpret_cast<char*>(const_cast<unsigned char*>(e->data));
assembly_data_size = e->size;
}
}
MonoAssembly*
#if defined (NET6) && defined (NET6_ALC_WORKS)
EmbeddedAssemblies::open_from_bundles (MonoAssemblyName* aname, MonoAssemblyLoadContextGCHandle alc_gchandle, MonoError *error)
#else // !(def NET6 && def NET6_ALC_WORKS)
EmbeddedAssemblies::open_from_bundles (MonoAssemblyName* aname, bool ref_only)
#endif // def NET6 && def NET6_ALC_WORKS
{
#if defined (NET6)
// NET6 doesn't support reference-only loads, define the variable here to minimize ifdefs in the code below
#if defined (NET6_ALC_WORKS)
constexpr bool ref_only = false;
#else // def NET6_ALC_WORKS
ref_only = false;
#endif // ndef NET6_ALC_WORKS
#endif // def NET6
const char *culture = mono_assembly_name_get_culture (aname);
const char *asmname = mono_assembly_name_get_name (aname);
dynamic_local_string<SENSIBLE_PATH_MAX> name;
if (culture != nullptr && *culture != '\0') {
name.append (culture);
name.append ("/");
}
name.append (asmname);
if (!utils.ends_with (name.get (), ".dll")) {
name.append (".dll");
}
MonoAssembly *a = nullptr;
MonoBundledAssembly **p;
log_info (LOG_ASSEMBLY, "open_from_bundles: looking for bundled name: '%s'", name.get ());
dynamic_local_string<SENSIBLE_PATH_MAX> abi_name;
abi_name
.assign (BasicAndroidSystem::get_built_for_abi_name ())
.append ("/")
.append (name.get ());
for (p = bundled_assemblies; p != nullptr && *p; ++p) {
MonoImage *image = nullptr;
MonoImageOpenStatus status;
const MonoBundledAssembly *e = *p;
char *assembly_data = nullptr;
uint32_t assembly_data_size;
if (strcmp (e->name, name.get ()) != 0) {
if (strcmp (e->name, abi_name.get ()) != 0) {
continue;
} else {
log_info (LOG_ASSEMBLY, "open_from_bundles: found architecture-specific: '%s'", abi_name.get ());
}
}
get_assembly_data (e, assembly_data, assembly_data_size);
#if defined (NET6) && defined (NET6_ALC_WORKS)
image = mono_image_open_from_data_alc (
alc_gchandle,
assembly_data,
assembly_data_size,
0 /* need_copy */,
nullptr /* status */,
name.get ()
);
#else // (def NET6 && def NET6_ALC_WORKS)
image = mono_image_open_from_data_with_name (assembly_data, assembly_data_size, 0, nullptr, ref_only, name.get ());
#endif // !(def NET6 && def NET6_ALC_WORKS)
if (image == nullptr) {
continue;
}
a = mono_assembly_load_from_full (image, name.get (), &status, ref_only);
if (a == nullptr) {
continue;
}
mono_config_for_assembly (image);
break;
}
if (a != nullptr && utils.should_log (LOG_ASSEMBLY)) {
log_info_nocheck (LOG_ASSEMBLY, "open_from_bundles: loaded assembly: %p\n", a);
}
#if defined (NET6) && defined (NET6_ALC_WORKS)
if (error != nullptr) {
error->error_code = a == nullptr ? MONO_ERROR_NONE : MONO_ERROR_FILE_NOT_FOUND;
}
#endif // def NET6 && def NET6_ALC_WORKS
return a;
}
#if defined (NET6) && defined (NET6_ALC_WORKS)
MonoAssembly*
EmbeddedAssemblies::open_from_bundles (MonoAssemblyLoadContextGCHandle alc_gchandle, MonoAssemblyName *aname, [[maybe_unused]] char **assemblies_path, [[maybe_unused]] void *user_data, MonoError *error)
{
log_warn (LOG_DEFAULT, __PRETTY_FUNCTION__);
return embeddedAssemblies.open_from_bundles (aname, alc_gchandle, error);
}
#else // def NET6 && def NET6_ALC_WORKS
MonoAssembly*
EmbeddedAssemblies::open_from_bundles_full (MonoAssemblyName *aname, UNUSED_ARG char **assemblies_path, UNUSED_ARG void *user_data)
{
return embeddedAssemblies.open_from_bundles (aname, false);
}
MonoAssembly*
EmbeddedAssemblies::open_from_bundles_refonly (MonoAssemblyName *aname, UNUSED_ARG char **assemblies_path, UNUSED_ARG void *user_data)
{
return embeddedAssemblies.open_from_bundles (aname, true);
}
#endif // !(def NET6 && def NET6_ALC_WORKS)
void
EmbeddedAssemblies::install_preload_hooks ()
{
#if defined (NET6) && defined (NET6_ALC_WORKS)
mono_install_assembly_preload_hook_v3 (
open_from_bundles,
nullptr /* user_data */,
0 /* append */
);
#else // def NET6 && def NET6_ALC_WORKS
mono_install_assembly_preload_hook (open_from_bundles_full, nullptr);
#if !defined (NET6) // Reference-only loads don't exist in NET6
mono_install_assembly_refonly_preload_hook (open_from_bundles_refonly, nullptr);
#endif // !def NET6
#endif // !(def NET6 && def NET6_ALC_WORKS)
}
template<typename Key, typename Entry, int (*compare)(const Key*, const Entry*), bool use_extra_size>
const Entry*
EmbeddedAssemblies::binary_search (const Key *key, const Entry *base, size_t nmemb, [[maybe_unused]] size_t extra_size)
{
static_assert (compare != nullptr, "compare is a required template parameter");
// This comes from the user code, so let's be civil
if (key == nullptr) {
log_warn (LOG_ASSEMBLY, "Key passed to binary_search must not be nullptr");
return nullptr;
}
// This is a coding error on our part, crash!
if (base == nullptr) {
log_fatal (LOG_ASSEMBLY, "Map address not passed to binary_search");
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
constexpr size_t size = sizeof(Entry);
while (nmemb > 0) {
const Entry *ret;
if constexpr (use_extra_size) {
ret = reinterpret_cast<const Entry*>(reinterpret_cast<const uint8_t*>(base) + ((size + extra_size) * (nmemb / 2)));
} else {
ret = base + (nmemb / 2);
}
int result = compare (key, ret);
if (result < 0) {
nmemb /= 2;
} else if (result > 0) {
if constexpr (use_extra_size) {
base = reinterpret_cast<const Entry*>(reinterpret_cast<const uint8_t*>(ret) + size + extra_size);
} else {
base = ret + 1;
}
nmemb -= nmemb / 2 + 1;
} else {
return ret;
}
}
return nullptr;
}
#if defined (DEBUG) || !defined (ANDROID)
int
EmbeddedAssemblies::compare_type_name (const char *type_name, const TypeMapEntry *entry)
{
if (entry == nullptr)
return 1;
return strcmp (type_name, entry->from);
}
MonoReflectionType*
EmbeddedAssemblies::typemap_java_to_managed (const char *java_type_name)
{
const TypeMapEntry *entry = nullptr;
if (application_config.instant_run_enabled) {
TypeMap *module;
for (size_t i = 0; i < type_map_count; i++) {
module = &type_maps[i];
entry = binary_search<const char, TypeMapEntry, compare_type_name, false> (java_type_name, module->java_to_managed, module->entry_count);
if (entry != nullptr)
break;
}
} else {
entry = binary_search<const char, TypeMapEntry, compare_type_name, false> (java_type_name, type_map.java_to_managed, type_map.entry_count);
}
if (XA_UNLIKELY (entry == nullptr)) {
log_info (LOG_ASSEMBLY, "typemap: unable to find mapping to a managed type from Java type '%s'", java_type_name);
return nullptr;
}
const char *managed_type_name = entry->to;
if (managed_type_name == nullptr) {
log_debug (LOG_ASSEMBLY, "typemap: Java type '%s' maps either to an open generic type or an interface type.", java_type_name);
return nullptr;
}
log_debug (LOG_DEFAULT, "typemap: Java type '%s' corresponds to managed type '%s'", java_type_name, managed_type_name);
MonoType *type = mono_reflection_type_from_name (const_cast<char*>(managed_type_name), nullptr);
if (XA_UNLIKELY (type == nullptr)) {
log_info (LOG_ASSEMBLY, "typemap: managed type '%s' (mapped from Java type '%s') could not be loaded", managed_type_name, java_type_name);
return nullptr;
}
MonoReflectionType *ret = mono_type_get_object (mono_domain_get (), type);
if (XA_UNLIKELY (ret == nullptr)) {
log_warn (LOG_ASSEMBLY, "typemap: unable to instantiate managed type '%s'", managed_type_name);
return nullptr;
}
return ret;
}
#else
MonoReflectionType*
EmbeddedAssemblies::typemap_java_to_managed (const char *java_type_name)
{
TypeMapModule *module;
const TypeMapJava *java_entry = binary_search<const char, TypeMapJava, compare_java_name, true> (java_type_name, map_java, java_type_count, java_name_width);
if (java_entry == nullptr) {
log_info (LOG_ASSEMBLY, "typemap: unable to find mapping to a managed type from Java type '%s'", java_type_name);
return nullptr;
}
if (java_entry->module_index >= map_module_count) {
log_warn (LOG_ASSEMBLY, "typemap: mapping from Java type '%s' to managed type has invalid module index", java_type_name);
return nullptr;
}
module = const_cast<TypeMapModule*>(&map_modules[java_entry->module_index]);
const TypeMapModuleEntry *entry = binary_search <uint32_t, TypeMapModuleEntry, compare_type_token> (&java_entry->type_token_id, module->map, module->entry_count);
if (entry == nullptr) {
log_info (LOG_ASSEMBLY, "typemap: unable to find mapping from Java type '%s' to managed type with token ID %u in module [%s]", java_type_name, java_entry->type_token_id, MonoGuidString (module->module_uuid).get ());
return nullptr;
}
uint32_t type_token_id = java_entry->type_token_id;
if (module->image == nullptr) {
module->image = mono_image_loaded (module->assembly_name);
if (module->image == nullptr) {
// TODO: load
log_error (LOG_ASSEMBLY, "typemap: assembly '%s' not loaded yet!", module->assembly_name);
}
if (module->image == nullptr) {
log_error (LOG_ASSEMBLY, "typemap: unable to load assembly '%s' when looking up managed type corresponding to Java type '%s'", module->assembly_name, java_type_name);
return nullptr;
}
}
log_debug (LOG_ASSEMBLY, "typemap: java type '%s' corresponds to managed token id %u (0x%x)", java_type_name, type_token_id, type_token_id);
MonoClass *klass = mono_class_get (module->image, static_cast<uint32_t>(type_token_id));
if (klass == nullptr) {
log_error (LOG_ASSEMBLY, "typemap: unable to find managed type with token ID %u in assembly '%s', corresponding to Java type '%s'", type_token_id, module->assembly_name, java_type_name);
return nullptr;
}
MonoReflectionType *ret = mono_type_get_object (mono_domain_get (), mono_class_get_type (klass));
if (ret == nullptr) {
log_warn (LOG_ASSEMBLY, "typemap: unable to instantiate managed type with token ID %u in assembly '%s', corresponding to Java type '%s'", type_token_id, module->assembly_name, java_type_name);
return nullptr;
}
return ret;
}
int
EmbeddedAssemblies::compare_java_name (const char *java_name, const TypeMapJava *entry)
{
if (entry == nullptr || entry->java_name[0] == '\0') {
return -1;
}
return strcmp (java_name, reinterpret_cast<const char*>(entry->java_name));
}
#endif
MonoReflectionType*
EmbeddedAssemblies::typemap_java_to_managed (MonoString *java_type)
{
timing_period total_time;
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
timing = new Timing ();
total_time.mark_start ();
}
if (XA_UNLIKELY (java_type == nullptr)) {
log_warn (LOG_ASSEMBLY, "typemap: null 'java_type' passed to 'typemap_java_to_managed'");
return nullptr;
}
simple_pointer_guard<char[], false> java_type_name (mono_string_to_utf8 (java_type));
if (XA_UNLIKELY (!java_type_name || *java_type_name == '\0')) {
log_warn (LOG_ASSEMBLY, "typemap: empty Java type name passed to 'typemap_java_to_managed'");
return nullptr;
}
MonoReflectionType *ret = typemap_java_to_managed (java_type_name.get ());
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
total_time.mark_end ();
Timing::info (total_time, "Typemap.java_to_managed: end, total time");
}
return ret;
}
#if defined (DEBUG) || !defined (ANDROID)
inline const TypeMapEntry*
EmbeddedAssemblies::typemap_managed_to_java (const char *managed_type_name)
{
const TypeMapEntry *entry = nullptr;
if (application_config.instant_run_enabled) {
TypeMap *module;
for (size_t i = 0; i < type_map_count; i++) {
module = &type_maps[i];
entry = binary_search<const char, TypeMapEntry, compare_type_name, false> (managed_type_name, module->managed_to_java, module->entry_count);
if (entry != nullptr)
break;
}
} else {
entry = binary_search<const char, TypeMapEntry, compare_type_name, false> (managed_type_name, type_map.managed_to_java, type_map.entry_count);
}
return entry;
}
inline const char*
EmbeddedAssemblies::typemap_managed_to_java ([[maybe_unused]] MonoType *type, MonoClass *klass, [[maybe_unused]] const uint8_t *mvid)
{
simple_pointer_guard<char[], false> type_name (mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_FULL_NAME));
MonoImage *image = mono_class_get_image (klass);
const char *image_name = mono_image_get_name (image);
size_t type_name_len = strlen (type_name.get ());
size_t image_name_len = strlen (image_name);
dynamic_local_string<SENSIBLE_PATH_MAX> full_name;
full_name
.append (type_name.get (), type_name_len)
.append (", ")
.append (image_name, image_name_len);
const TypeMapEntry *entry = typemap_managed_to_java (full_name.get ());
if (XA_UNLIKELY (entry == nullptr)) {
log_info (LOG_ASSEMBLY, "typemap: unable to find mapping to a Java type from managed type '%s'", full_name.get ());
return nullptr;
}
return entry->to;
}
#else
inline int
EmbeddedAssemblies::compare_type_token (const uint32_t *token, const TypeMapModuleEntry *entry)
{
if (entry == nullptr) {
log_fatal (LOG_ASSEMBLY, "typemap: compare_type_token: entry is nullptr");
exit (FATAL_EXIT_MISSING_ASSEMBLY);
}
if (*token < entry->type_token_id)
return -1;
if (*token > entry->type_token_id)
return 1;
return 0;
}
inline int
EmbeddedAssemblies::compare_mvid (const uint8_t *mvid, const TypeMapModule *module)
{
return memcmp (mvid, module->module_uuid, sizeof(module->module_uuid));
}
inline const char*
EmbeddedAssemblies::typemap_managed_to_java ([[maybe_unused]] MonoType *type, MonoClass *klass, const uint8_t *mvid)
{
if (mvid == nullptr) {
log_warn (LOG_ASSEMBLY, "typemap: no mvid specified in call to typemap_managed_to_java");
return nullptr;
}
uint32_t token = mono_class_get_type_token (klass);
const TypeMapModule *map;
size_t map_entry_count;
map = map_modules;
map_entry_count = map_module_count;
const TypeMapModule *match = binary_search<uint8_t, TypeMapModule, compare_mvid> (mvid, map, map_entry_count);
if (match == nullptr) {
log_info (LOG_ASSEMBLY, "typemap: module matching MVID [%s] not found.", MonoGuidString (mvid).get ());
return nullptr;
}
if (match->map == nullptr) {
log_warn (LOG_ASSEMBLY, "typemap: module with MVID [%s] has no associated type map.", MonoGuidString (mvid).get ());
return nullptr;
}
log_debug (LOG_ASSEMBLY, "typemap: MVID [%s] maps to assembly %s, looking for token %d (0x%x), table index %d", MonoGuidString (mvid).get (), match->assembly_name, token, token, token & 0x00FFFFFF);
// Each map entry is a pair of 32-bit integers: [TypeTokenID][JavaMapArrayIndex]
const TypeMapModuleEntry *entry = binary_search <uint32_t, TypeMapModuleEntry, compare_type_token> (&token, match->map, match->entry_count);
if (entry == nullptr) {
if (match->duplicate_count > 0 && match->duplicate_map != nullptr) {
log_debug (LOG_ASSEMBLY, "typemap: searching module [%s] duplicate map for token %u (0x%x)", MonoGuidString (mvid).get (), token, token);
entry = binary_search <uint32_t, TypeMapModuleEntry, compare_type_token> (&token, match->duplicate_map, match->duplicate_count);
}
if (entry == nullptr) {
log_info (LOG_ASSEMBLY, "typemap: type with token %d (0x%x) in module {%s} (%s) not found.", token, token, MonoGuidString (mvid).get (), match->assembly_name);
return nullptr;
}
}
uint32_t java_entry_count;
java_entry_count = java_type_count;
if (entry->java_map_index >= java_entry_count) {
log_warn (LOG_ASSEMBLY, "typemap: type with token %d (0x%x) in module {%s} (%s) has invalid Java type index %u", token, token, MonoGuidString (mvid).get (), match->assembly_name, entry->java_map_index);
return nullptr;
}
const char *ret;
const TypeMapJava *java_entry = reinterpret_cast<const TypeMapJava*> (reinterpret_cast<const uint8_t*>(map_java) + ((sizeof(TypeMapJava) + java_name_width) * entry->java_map_index));
ret = reinterpret_cast<const char*>(reinterpret_cast<const uint8_t*>(java_entry) + 8);
if (XA_UNLIKELY (ret == nullptr)) {
log_warn (LOG_ASSEMBLY, "typemap: empty Java type name returned for entry at index %u", entry->java_map_index);
}
log_debug (
LOG_ASSEMBLY,
"typemap: type with token %d (0x%x) in module {%s} (%s) corresponds to Java type '%s'",
token,
token,
MonoGuidString (mvid).get (),
match->assembly_name,
ret
);
return ret;
}
#endif
const char*
EmbeddedAssemblies::typemap_managed_to_java (MonoReflectionType *reflection_type, const uint8_t *mvid)
{
timing_period total_time;
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
timing = new Timing ();
total_time.mark_start ();
}
MonoType *type = mono_reflection_type_get_type (reflection_type);
if (type == nullptr) {
log_warn (LOG_ASSEMBLY, "Failed to map reflection type to MonoType");
return nullptr;
}
const char *ret = typemap_managed_to_java (type, mono_class_from_mono_type (type), mvid);
if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
total_time.mark_end ();
Timing::info (total_time, "Typemap.managed_to_java: end, total time");
}
return ret;
}
EmbeddedAssemblies::md_mmap_info
EmbeddedAssemblies::md_mmap_apk_file (int fd, uint32_t offset, uint32_t size, const char* filename, const char* apk)
{
md_mmap_info file_info;
md_mmap_info mmap_info;
size_t pageSize = static_cast<size_t>(utils.monodroid_getpagesize ());
uint32_t offsetFromPage = static_cast<uint32_t>(offset % pageSize);
uint32_t offsetPage = offset - offsetFromPage;
uint32_t offsetSize = size + offsetFromPage;
mmap_info.area = mmap (nullptr, offsetSize, PROT_READ, MAP_PRIVATE, fd, static_cast<off_t>(offsetPage));
if (mmap_info.area == MAP_FAILED) {
log_fatal (LOG_DEFAULT, "Could not `mmap` apk `%s` entry `%s`: %s", apk, filename, strerror (errno));
exit (FATAL_EXIT_CANNOT_FIND_APK);
}
mmap_info.size = offsetSize;
file_info.area = (void*)((const char*)mmap_info.area + offsetFromPage);
file_info.size = size;
log_info (LOG_ASSEMBLY, " mmap_start: %08p mmap_end: %08p mmap_len: % 12u file_start: %08p file_end: %08p file_len: % 12u apk: %s file: %s",
mmap_info.area, reinterpret_cast<int*> (mmap_info.area) + mmap_info.size, (unsigned int) mmap_info.size,
file_info.area, reinterpret_cast<int*> (file_info.area) + file_info.size, (unsigned int) file_info.size, apk, filename);
return file_info;
}
bool
EmbeddedAssemblies::register_debug_symbols_for_assembly (const char *entry_name, MonoBundledAssembly *assembly, const mono_byte *debug_contents, int debug_size)
{
const char *entry_basename = strrchr (entry_name, '/') + 1;
// System.dll, System.dll.mdb case
if (strncmp (assembly->name, entry_basename, strlen (assembly->name)) != 0) {
// That failed; try for System.dll, System.pdb case
const char *eb_ext = strrchr (entry_basename, '.');
if (eb_ext == nullptr)
return false;
off_t basename_len = static_cast<off_t>(eb_ext - entry_basename);
assert (basename_len > 0 && "basename must have a length!");
if (strncmp (assembly->name, entry_basename, static_cast<size_t>(basename_len)) != 0)
return false;
}
mono_register_symfile_for_assembly (assembly->name, debug_contents, debug_size);
return true;
}
void
EmbeddedAssemblies::gather_bundled_assemblies_from_apk (const char* apk, monodroid_should_register should_register)
{
int fd;
if ((fd = open (apk, O_RDONLY)) < 0) {
log_error (LOG_DEFAULT, "ERROR: Unable to load application package %s.", apk);
exit (FATAL_EXIT_NO_ASSEMBLIES);
}
zip_load_entries (fd, utils.strdup_new (apk), should_register);
close(fd);
}
#if defined (DEBUG) || !defined (ANDROID)
ssize_t EmbeddedAssemblies::do_read (int fd, void *buf, size_t count)
{
ssize_t ret;
do {
ret = ::read (
fd,
buf,
#if defined (WINDOWS)
static_cast<unsigned int>(count)
#else
count
#endif
);
} while (ret < 0 && errno == EINTR);
return ret;
}
template<typename H>
bool
EmbeddedAssemblies::typemap_read_header ([[maybe_unused]] int dir_fd, const char *file_type, const char *dir_path, const char *file_path, uint32_t expected_magic, H &header, size_t &file_size, int &fd)
{
struct stat sbuf;
int res;
#if __ANDROID_API__ < 21
simple_pointer_guard<char[]> full_file_path = utils.path_combine (dir_path, file_path);
res = stat (full_file_path, &sbuf);
#else
res = fstatat (dir_fd, file_path, &sbuf, 0);
#endif
if (res < 0) {
log_error (LOG_ASSEMBLY, "typemap: failed to stat %s file '%s/%s': %s", file_type, dir_path, file_path, strerror (errno));
return false;
}
file_size = static_cast<size_t>(sbuf.st_size);
if (file_size < sizeof (header)) {
log_error (LOG_ASSEMBLY, "typemap: %s file '%s/%s' is too small (must be at least %u bytes)", file_type, dir_path, file_path, sizeof (header));
return false;
}
#if __ANDROID_API__ < 21
fd = open (full_file_path, O_RDONLY);
#else
fd = openat (dir_fd, file_path, O_RDONLY);
#endif
if (fd < 0) {
log_error (LOG_ASSEMBLY, "typemap: failed to open %s file %s/%s for reading: %s", file_type, dir_path, file_path, strerror (errno));
return false;
}
ssize_t nread = do_read (fd, &header, sizeof (header));
if (nread <= 0) {
if (nread < 0) {
log_error (LOG_ASSEMBLY, "typemap: failed to read %s file header from '%s/%s': %s", file_type, dir_path, file_path, strerror (errno));
} else {
log_error (LOG_ASSEMBLY, "typemap: end of file while reading %s file header from '%s/%s'", file_type, dir_path, file_path);
}
return false;
}
if (header.magic != expected_magic) {
log_error (LOG_ASSEMBLY, "typemap: invalid magic value in the %s file header from '%s/%s': expected 0x%X, got 0x%X", file_type, dir_path, file_path, expected_magic, header.magic);
return false;
}
if (header.version != MODULE_FORMAT_VERSION) {
log_error (LOG_ASSEMBLY, "typemap: incompatible %s format version. This build supports only version %u, file '%s/%s' uses version %u", file_type, MODULE_FORMAT_VERSION, dir_path, file_path, header.version);
return false;
}
return true;
}
uint8_t*
EmbeddedAssemblies::typemap_load_index (TypeMapIndexHeader &header, size_t file_size, int index_fd)
{
size_t entry_size = header.module_file_name_width;
size_t data_size = entry_size * type_map_count;
if (sizeof(header) + data_size > file_size) {
log_error (LOG_ASSEMBLY, "typemap: index file is too small, expected %u, found %u bytes", data_size + sizeof(header), file_size);
return nullptr;
}
auto data = new uint8_t [data_size];
ssize_t nread = do_read (index_fd, data, data_size);
if (nread != static_cast<ssize_t>(data_size)) {
log_error (LOG_ASSEMBLY, "typemap: failed to read %u bytes from index file. %s", data_size, strerror (errno));
return nullptr;
}
uint8_t *p = data;
for (size_t i = 0; i < type_map_count; i++) {
type_maps[i].assembly_name = reinterpret_cast<char*>(p);
p += entry_size;
}
return data;
}
uint8_t*
EmbeddedAssemblies::typemap_load_index (int dir_fd, const char *dir_path, const char *index_path)
{
log_debug (LOG_ASSEMBLY, "typemap: loading TypeMap index file '%s/%s'", dir_path, index_path);
TypeMapIndexHeader header;
size_t file_size;
int fd = -1;
uint8_t *data = nullptr;
if (!typemap_read_header (dir_fd, "TypeMap index", dir_path, index_path, MODULE_INDEX_MAGIC, header, file_size, fd)) {
goto cleanup;
}
type_map_count = header.entry_count;
type_maps = new TypeMap[type_map_count]();
data = typemap_load_index (header, file_size, fd);
cleanup:
if (fd >= 0)
close (fd);
return data;
}
bool
EmbeddedAssemblies::typemap_load_file (BinaryTypeMapHeader &header, const char *dir_path, const char *file_path, int file_fd, TypeMap &module)
{
size_t alloc_size = ADD_WITH_OVERFLOW_CHECK (size_t, header.assembly_name_length, 1);
module.assembly_name = new char[alloc_size];
ssize_t nread = do_read (file_fd, module.assembly_name, header.assembly_name_length);
if (nread != static_cast<ssize_t>(header.assembly_name_length)) {
log_error (LOG_ASSEMBLY, "tyemap: failed to read map assembly name from '%s/%s': %s", dir_path, file_path, strerror (errno));
return false;
}
module.assembly_name [header.assembly_name_length] = 0;
module.entry_count = header.entry_count;
log_debug (
LOG_ASSEMBLY,
"typemap: '%s/%s':: entry count == %u; Java name field width == %u; Managed name width == %u; assembly name length == %u; assembly name == %s",
dir_path, file_path, header.entry_count, header.java_name_width, header.managed_name_width, header.assembly_name_length, module.assembly_name
);
// [name][index]
size_t java_entry_size = header.java_name_width + sizeof(uint32_t);
size_t managed_entry_size = header.managed_name_width + sizeof(uint32_t);
size_t data_size = ADD_WITH_OVERFLOW_CHECK (
size_t,
header.entry_count * java_entry_size,
header.entry_count * managed_entry_size
);
module.data = new uint8_t [data_size];
nread = do_read (file_fd, module.data, data_size);
if (nread != static_cast<ssize_t>(data_size)) {
log_error (LOG_ASSEMBLY, "tyemap: failed to read map data from '%s/%s': %s", dir_path, file_path, strerror (errno));
return false;
}
module.java_to_managed = new TypeMapEntry [module.entry_count];
module.managed_to_java = new TypeMapEntry [module.entry_count];
uint8_t *java_start = module.data;
uint8_t *managed_start = module.data + (module.entry_count * java_entry_size);
uint8_t *java_pos = java_start;
uint8_t *managed_pos = managed_start;
TypeMapEntry *cur;
constexpr uint32_t INVALID_TYPE_INDEX = UINT32_MAX;
for (size_t i = 0; i < module.entry_count; i++) {
cur = &module.java_to_managed[i];
cur->from = reinterpret_cast<char*>(java_pos);
uint32_t idx;
// This might seem slow but it is in fact compiled into a single instruction and is safe when loading the 32-bit
// integer from unaligned memory
memcpy (&idx, java_pos + header.java_name_width, sizeof (idx));
if (idx < INVALID_TYPE_INDEX) {
cur->to = reinterpret_cast<char*>(managed_start + (managed_entry_size * idx));
} else {
// Ignore the type mapping
cur->to = nullptr;
}
java_pos += java_entry_size;
cur = &module.managed_to_java[i];
cur->from = reinterpret_cast<char*>(managed_pos);
memcpy (&idx, managed_pos + header.managed_name_width, sizeof (idx));
cur->to = reinterpret_cast<char*>(java_start + (java_entry_size * idx));
managed_pos += managed_entry_size;
}
return true;
}
bool
EmbeddedAssemblies::typemap_load_file (int dir_fd, const char *dir_path, const char *file_path, TypeMap &module)
{
log_debug (LOG_ASSEMBLY, "typemap: loading TypeMap file '%s/%s'", dir_path, file_path);
bool ret = true;
BinaryTypeMapHeader header;
size_t file_size;
int fd = -1;
module.java_to_managed = nullptr;
module.managed_to_java = nullptr;
if (!typemap_read_header (dir_fd, "TypeMap", dir_path, file_path, MODULE_MAGIC_NAMES, header, file_size, fd)) {
ret = false;
goto cleanup;
}
ret = typemap_load_file (header, dir_path, file_path, fd, module);
cleanup:
if (fd >= 0)
close (fd);
if (!ret) {
delete[] module.java_to_managed;
module.java_to_managed = nullptr;
delete[] module.managed_to_java;
module.managed_to_java = nullptr;
}
return ret;
}
void
EmbeddedAssemblies::try_load_typemaps_from_directory (const char *path)
{
if (!application_config.instant_run_enabled) {
log_info (LOG_ASSEMBLY, "typemap: instant run disabled, not loading type maps from storage");
return;
}
simple_pointer_guard<char[]> dir_path (utils.path_combine (path, "typemaps"));
monodroid_dir_t *dir;
if ((dir = utils.monodroid_opendir (dir_path)) == nullptr) {
log_warn (LOG_ASSEMBLY, "typemap: could not open directory: `%s`", dir_path.get ());
return;
}
int dir_fd;
#if __ANDROID_API__ < 21
dir_fd = -1;
#else
dir_fd = dirfd (dir);
#endif
constexpr char index_name[] = "typemap.index";
// The pointer must be stored here because, after index is loaded, module.assembly_name points into the index data
// and must be valid until after the actual module file is loaded.
simple_pointer_guard<uint8_t[], true> index_data = typemap_load_index (dir_fd, dir_path, index_name);
if (!index_data) {
log_fatal (LOG_ASSEMBLY, "typemap: unable to load TypeMap data index from '%s/%s'", dir_path.get (), index_name);
exit (FATAL_EXIT_NO_ASSEMBLIES); // TODO: use a new error code here
}
for (size_t i = 0; i < type_map_count; i++) {
TypeMap *module = &type_maps[i];
if (!typemap_load_file (dir_fd, dir_path, module->assembly_name, *module)) {
continue;
}
}
utils.monodroid_closedir (dir);
}
#endif
size_t
EmbeddedAssemblies::register_from (const char *apk_file, monodroid_should_register should_register)
{
size_t prev = bundled_assemblies_count;
gather_bundled_assemblies_from_apk (apk_file, should_register);
log_info (LOG_ASSEMBLY, "Package '%s' contains %i assemblies", apk_file, bundled_assemblies_count - prev);
if (bundled_assemblies) {
size_t alloc_size = MULTIPLY_WITH_OVERFLOW_CHECK (size_t, sizeof(void*), bundled_assemblies_count + 1);
bundled_assemblies = reinterpret_cast <MonoBundledAssembly**> (utils.xrealloc (bundled_assemblies, alloc_size));
bundled_assemblies [bundled_assemblies_count] = nullptr;
}
return bundled_assemblies_count;
}