/
systemDictionaryShared.cpp
2410 lines (2165 loc) · 91.8 KB
/
systemDictionaryShared.cpp
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classListParser.hpp"
#include "classfile/classLoader.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/classLoaderExt.hpp"
#include "classfile/dictionary.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/systemDictionaryShared.hpp"
#include "classfile/verificationType.hpp"
#include "classfile/vmClasses.hpp"
#include "classfile/vmSymbols.hpp"
#include "interpreter/bootstrapInfo.hpp"
#include "jfr/jfrEvents.hpp"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/allocation.hpp"
#include "memory/archiveUtils.hpp"
#include "memory/archiveBuilder.hpp"
#include "memory/dynamicArchive.hpp"
#include "memory/filemap.hpp"
#include "memory/heapShared.hpp"
#include "memory/metadataFactory.hpp"
#include "memory/metaspaceClosure.hpp"
#include "memory/metaspaceShared.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/klass.inline.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oopHandle.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "runtime/arguments.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/mutexLocker.hpp"
#include "utilities/hashtable.inline.hpp"
#include "utilities/resourceHash.hpp"
#include "utilities/stringUtils.hpp"
OopHandle SystemDictionaryShared::_shared_protection_domains;
OopHandle SystemDictionaryShared::_shared_jar_urls;
OopHandle SystemDictionaryShared::_shared_jar_manifests;
DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;)
bool SystemDictionaryShared::_dump_in_progress = false;
class DumpTimeSharedClassInfo: public CHeapObj<mtClass> {
bool _excluded;
bool _is_early_klass;
public:
struct DTLoaderConstraint {
Symbol* _name;
char _loader_type1;
char _loader_type2;
DTLoaderConstraint(Symbol* name, char l1, char l2) : _name(name), _loader_type1(l1), _loader_type2(l2) {
_name->increment_refcount();
}
DTLoaderConstraint() : _name(NULL), _loader_type1('0'), _loader_type2('0') {}
bool equals(const DTLoaderConstraint& t) {
return t._name == _name &&
((t._loader_type1 == _loader_type1 && t._loader_type2 == _loader_type2) ||
(t._loader_type2 == _loader_type1 && t._loader_type1 == _loader_type2));
}
};
struct DTVerifierConstraint {
Symbol* _name;
Symbol* _from_name;
DTVerifierConstraint() : _name(NULL), _from_name(NULL) {}
DTVerifierConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) {
_name->increment_refcount();
_from_name->increment_refcount();
}
};
InstanceKlass* _klass;
InstanceKlass* _nest_host;
bool _failed_verification;
bool _is_archived_lambda_proxy;
int _id;
int _clsfile_size;
int _clsfile_crc32;
GrowableArray<DTVerifierConstraint>* _verifier_constraints;
GrowableArray<char>* _verifier_constraint_flags;
GrowableArray<DTLoaderConstraint>* _loader_constraints;
DumpTimeSharedClassInfo() {
_klass = NULL;
_nest_host = NULL;
_failed_verification = false;
_is_archived_lambda_proxy = false;
_id = -1;
_clsfile_size = -1;
_clsfile_crc32 = -1;
_excluded = false;
_is_early_klass = JvmtiExport::is_early_phase();
_verifier_constraints = NULL;
_verifier_constraint_flags = NULL;
_loader_constraints = NULL;
}
void add_verification_constraint(InstanceKlass* k, Symbol* name,
Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object);
void record_linking_constraint(Symbol* name, Handle loader1, Handle loader2);
bool is_builtin() {
return SystemDictionaryShared::is_builtin(_klass);
}
int num_verifier_constraints() {
if (_verifier_constraint_flags != NULL) {
return _verifier_constraint_flags->length();
} else {
return 0;
}
}
int num_loader_constraints() {
if (_loader_constraints != NULL) {
return _loader_constraints->length();
} else {
return 0;
}
}
void metaspace_pointers_do(MetaspaceClosure* it) {
it->push(&_klass);
it->push(&_nest_host);
if (_verifier_constraints != NULL) {
for (int i = 0; i < _verifier_constraints->length(); i++) {
DTVerifierConstraint* cons = _verifier_constraints->adr_at(i);
it->push(&cons->_name);
it->push(&cons->_from_name);
}
}
if (_loader_constraints != NULL) {
for (int i = 0; i < _loader_constraints->length(); i++) {
DTLoaderConstraint* lc = _loader_constraints->adr_at(i);
it->push(&lc->_name);
}
}
}
void set_excluded() {
_excluded = true;
}
bool is_excluded() {
// _klass may become NULL due to DynamicArchiveBuilder::set_to_null
return _excluded || _failed_verification || _klass == NULL;
}
// Was this class loaded while JvmtiExport::is_early_phase()==true
bool is_early_klass() {
return _is_early_klass;
}
void set_failed_verification() {
_failed_verification = true;
}
bool failed_verification() {
return _failed_verification;
}
void set_nest_host(InstanceKlass* nest_host) {
_nest_host = nest_host;
}
InstanceKlass* nest_host() {
return _nest_host;
}
};
inline unsigned DumpTimeSharedClassTable_hash(InstanceKlass* const& k) {
if (DumpSharedSpaces) {
// Deterministic archive contents
uintx delta = k->name() - MetaspaceShared::symbol_rs_base();
return primitive_hash<uintx>(delta);
} else {
// Deterministic archive is not possible because classes can be loaded
// in multiple threads.
return primitive_hash<InstanceKlass*>(k);
}
}
class DumpTimeSharedClassTable: public ResourceHashtable<
InstanceKlass*,
DumpTimeSharedClassInfo,
&DumpTimeSharedClassTable_hash,
primitive_equals<InstanceKlass*>,
15889, // prime number
ResourceObj::C_HEAP>
{
int _builtin_count;
int _unregistered_count;
public:
DumpTimeSharedClassInfo* find_or_allocate_info_for(InstanceKlass* k, bool dump_in_progress) {
bool created = false;
DumpTimeSharedClassInfo* p;
if (!dump_in_progress) {
p = put_if_absent(k, &created);
} else {
p = get(k);
}
if (created) {
assert(!SystemDictionaryShared::no_class_loading_should_happen(),
"no new classes can be loaded while dumping archive");
p->_klass = k;
} else {
if (!dump_in_progress) {
assert(p->_klass == k, "Sanity");
}
}
return p;
}
class CountClassByCategory : StackObj {
DumpTimeSharedClassTable* _table;
public:
CountClassByCategory(DumpTimeSharedClassTable* table) : _table(table) {}
bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
if (!info.is_excluded()) {
if (info.is_builtin()) {
++ _table->_builtin_count;
} else {
++ _table->_unregistered_count;
}
}
return true; // keep on iterating
}
};
void update_counts() {
_builtin_count = 0;
_unregistered_count = 0;
CountClassByCategory counter(this);
iterate(&counter);
}
int count_of(bool is_builtin) const {
if (is_builtin) {
return _builtin_count;
} else {
return _unregistered_count;
}
}
};
class LambdaProxyClassKey {
InstanceKlass* _caller_ik;
Symbol* _invoked_name;
Symbol* _invoked_type;
Symbol* _method_type;
Method* _member_method;
Symbol* _instantiated_method_type;
public:
LambdaProxyClassKey(InstanceKlass* caller_ik,
Symbol* invoked_name,
Symbol* invoked_type,
Symbol* method_type,
Method* member_method,
Symbol* instantiated_method_type) :
_caller_ik(caller_ik),
_invoked_name(invoked_name),
_invoked_type(invoked_type),
_method_type(method_type),
_member_method(member_method),
_instantiated_method_type(instantiated_method_type) {}
void metaspace_pointers_do(MetaspaceClosure* it) {
it->push(&_caller_ik);
it->push(&_invoked_name);
it->push(&_invoked_type);
it->push(&_method_type);
it->push(&_member_method);
it->push(&_instantiated_method_type);
}
void mark_pointers() {
ArchivePtrMarker::mark_pointer(&_caller_ik);
ArchivePtrMarker::mark_pointer(&_instantiated_method_type);
ArchivePtrMarker::mark_pointer(&_invoked_name);
ArchivePtrMarker::mark_pointer(&_invoked_type);
ArchivePtrMarker::mark_pointer(&_member_method);
ArchivePtrMarker::mark_pointer(&_method_type);
}
bool equals(LambdaProxyClassKey const& other) const {
return _caller_ik == other._caller_ik &&
_invoked_name == other._invoked_name &&
_invoked_type == other._invoked_type &&
_method_type == other._method_type &&
_member_method == other._member_method &&
_instantiated_method_type == other._instantiated_method_type;
}
unsigned int hash() const {
return SystemDictionaryShared::hash_for_shared_dictionary((address)_caller_ik) +
SystemDictionaryShared::hash_for_shared_dictionary((address)_invoked_name) +
SystemDictionaryShared::hash_for_shared_dictionary((address)_invoked_type) +
SystemDictionaryShared::hash_for_shared_dictionary((address)_method_type) +
SystemDictionaryShared::hash_for_shared_dictionary((address)_instantiated_method_type);
}
static unsigned int dumptime_hash(Symbol* sym) {
if (sym == NULL) {
// _invoked_name maybe NULL
return 0;
}
return java_lang_String::hash_code((const jbyte*)sym->bytes(), sym->utf8_length());
}
unsigned int dumptime_hash() const {
return dumptime_hash(_caller_ik->name()) +
dumptime_hash(_invoked_name) +
dumptime_hash(_invoked_type) +
dumptime_hash(_method_type) +
dumptime_hash(_instantiated_method_type);
}
static inline unsigned int DUMPTIME_HASH(LambdaProxyClassKey const& key) {
return (key.dumptime_hash());
}
static inline bool DUMPTIME_EQUALS(
LambdaProxyClassKey const& k1, LambdaProxyClassKey const& k2) {
return (k1.equals(k2));
}
};
class DumpTimeLambdaProxyClassInfo {
public:
GrowableArray<InstanceKlass*>* _proxy_klasses;
DumpTimeLambdaProxyClassInfo() : _proxy_klasses(NULL) {}
void add_proxy_klass(InstanceKlass* proxy_klass) {
if (_proxy_klasses == NULL) {
_proxy_klasses = new (ResourceObj::C_HEAP, mtClassShared)GrowableArray<InstanceKlass*>(5, mtClassShared);
}
assert(_proxy_klasses != NULL, "sanity");
_proxy_klasses->append(proxy_klass);
}
void metaspace_pointers_do(MetaspaceClosure* it) {
for (int i=0; i<_proxy_klasses->length(); i++) {
it->push(_proxy_klasses->adr_at(i));
}
}
};
class RunTimeLambdaProxyClassInfo {
LambdaProxyClassKey _key;
InstanceKlass* _proxy_klass_head;
public:
RunTimeLambdaProxyClassInfo(LambdaProxyClassKey key, InstanceKlass* proxy_klass_head) :
_key(key), _proxy_klass_head(proxy_klass_head) {}
InstanceKlass* proxy_klass_head() const { return _proxy_klass_head; }
// Used by LambdaProxyClassDictionary to implement OffsetCompactHashtable::EQUALS
static inline bool EQUALS(
const RunTimeLambdaProxyClassInfo* value, LambdaProxyClassKey* key, int len_unused) {
return (value->_key.equals(*key));
}
void init(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
_key = key;
_key.mark_pointers();
_proxy_klass_head = info._proxy_klasses->at(0);
ArchivePtrMarker::mark_pointer(&_proxy_klass_head);
}
unsigned int hash() const {
return _key.hash();
}
LambdaProxyClassKey key() const {
return _key;
}
};
class LambdaProxyClassDictionary : public OffsetCompactHashtable<
LambdaProxyClassKey*,
const RunTimeLambdaProxyClassInfo*,
RunTimeLambdaProxyClassInfo::EQUALS> {};
LambdaProxyClassDictionary _lambda_proxy_class_dictionary;
LambdaProxyClassDictionary _dynamic_lambda_proxy_class_dictionary;
class DumpTimeLambdaProxyClassDictionary
: public ResourceHashtable<LambdaProxyClassKey,
DumpTimeLambdaProxyClassInfo,
LambdaProxyClassKey::DUMPTIME_HASH,
LambdaProxyClassKey::DUMPTIME_EQUALS,
137, // prime number
ResourceObj::C_HEAP> {
public:
int _count;
};
DumpTimeLambdaProxyClassDictionary* _dumptime_lambda_proxy_class_dictionary = NULL;
static void add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey key,
InstanceKlass* proxy_klass) {
assert(DumpTimeTable_lock->owned_by_self(), "sanity");
if (_dumptime_lambda_proxy_class_dictionary == NULL) {
_dumptime_lambda_proxy_class_dictionary =
new (ResourceObj::C_HEAP, mtClass)DumpTimeLambdaProxyClassDictionary();
}
DumpTimeLambdaProxyClassInfo* lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
if (lambda_info == NULL) {
DumpTimeLambdaProxyClassInfo info;
info.add_proxy_klass(proxy_klass);
_dumptime_lambda_proxy_class_dictionary->put(key, info);
//lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
//assert(lambda_info->_proxy_klass == proxy_klass, "must be"); // debug only -- remove
++_dumptime_lambda_proxy_class_dictionary->_count;
} else {
lambda_info->add_proxy_klass(proxy_klass);
}
}
class RunTimeSharedClassInfo {
public:
struct CrcInfo {
int _clsfile_size;
int _clsfile_crc32;
};
// This is different than DumpTimeSharedClassInfo::DTVerifierConstraint. We use
// u4 instead of Symbol* to save space on 64-bit CPU.
struct RTVerifierConstraint {
u4 _name;
u4 _from_name;
Symbol* name() { return (Symbol*)(SharedBaseAddress + _name);}
Symbol* from_name() { return (Symbol*)(SharedBaseAddress + _from_name); }
};
struct RTLoaderConstraint {
u4 _name;
char _loader_type1;
char _loader_type2;
Symbol* constraint_name() {
return (Symbol*)(SharedBaseAddress + _name);
}
};
InstanceKlass* _klass;
int _num_verifier_constraints;
int _num_loader_constraints;
// optional CrcInfo _crc; (only for UNREGISTERED classes)
// optional InstanceKlass* _nest_host
// optional RTLoaderConstraint _loader_constraint_types[_num_loader_constraints]
// optional RTVerifierConstraint _verifier_constraints[_num_verifier_constraints]
// optional char _verifier_constraint_flags[_num_verifier_constraints]
private:
static size_t header_size_size() {
return sizeof(RunTimeSharedClassInfo);
}
static size_t crc_size(InstanceKlass* klass) {
if (!SystemDictionaryShared::is_builtin(klass)) {
return sizeof(CrcInfo);
} else {
return 0;
}
}
static size_t verifier_constraints_size(int num_verifier_constraints) {
return sizeof(RTVerifierConstraint) * num_verifier_constraints;
}
static size_t verifier_constraint_flags_size(int num_verifier_constraints) {
return sizeof(char) * num_verifier_constraints;
}
static size_t loader_constraints_size(int num_loader_constraints) {
return sizeof(RTLoaderConstraint) * num_loader_constraints;
}
static size_t nest_host_size(InstanceKlass* klass) {
if (klass->is_hidden()) {
return sizeof(InstanceKlass*);
} else {
return 0;
}
}
public:
static size_t byte_size(InstanceKlass* klass, int num_verifier_constraints, int num_loader_constraints) {
return header_size_size() +
crc_size(klass) +
nest_host_size(klass) +
loader_constraints_size(num_loader_constraints) +
verifier_constraints_size(num_verifier_constraints) +
verifier_constraint_flags_size(num_verifier_constraints);
}
private:
size_t crc_offset() const {
return header_size_size();
}
size_t nest_host_offset() const {
return crc_offset() + crc_size(_klass);
}
size_t loader_constraints_offset() const {
return nest_host_offset() + nest_host_size(_klass);
}
size_t verifier_constraints_offset() const {
return loader_constraints_offset() + loader_constraints_size(_num_loader_constraints);
}
size_t verifier_constraint_flags_offset() const {
return verifier_constraints_offset() + verifier_constraints_size(_num_verifier_constraints);
}
void check_verifier_constraint_offset(int i) const {
assert(0 <= i && i < _num_verifier_constraints, "sanity");
}
void check_loader_constraint_offset(int i) const {
assert(0 <= i && i < _num_loader_constraints, "sanity");
}
public:
CrcInfo* crc() const {
assert(crc_size(_klass) > 0, "must be");
return (CrcInfo*)(address(this) + crc_offset());
}
RTVerifierConstraint* verifier_constraints() {
assert(_num_verifier_constraints > 0, "sanity");
return (RTVerifierConstraint*)(address(this) + verifier_constraints_offset());
}
RTVerifierConstraint* verifier_constraint_at(int i) {
check_verifier_constraint_offset(i);
return verifier_constraints() + i;
}
char* verifier_constraint_flags() {
assert(_num_verifier_constraints > 0, "sanity");
return (char*)(address(this) + verifier_constraint_flags_offset());
}
InstanceKlass** nest_host_addr() {
assert(_klass->is_hidden(), "sanity");
return (InstanceKlass**)(address(this) + nest_host_offset());
}
InstanceKlass* nest_host() {
return *nest_host_addr();
}
void set_nest_host(InstanceKlass* k) {
*nest_host_addr() = k;
ArchivePtrMarker::mark_pointer((address*)nest_host_addr());
}
RTLoaderConstraint* loader_constraints() {
assert(_num_loader_constraints > 0, "sanity");
return (RTLoaderConstraint*)(address(this) + loader_constraints_offset());
}
RTLoaderConstraint* loader_constraint_at(int i) {
check_loader_constraint_offset(i);
return loader_constraints() + i;
}
void init(DumpTimeSharedClassInfo& info) {
ArchiveBuilder* builder = ArchiveBuilder::current();
assert(builder->is_in_buffer_space(info._klass), "must be");
_klass = info._klass;
if (!SystemDictionaryShared::is_builtin(_klass)) {
CrcInfo* c = crc();
c->_clsfile_size = info._clsfile_size;
c->_clsfile_crc32 = info._clsfile_crc32;
}
_num_verifier_constraints = info.num_verifier_constraints();
_num_loader_constraints = info.num_loader_constraints();
int i;
if (_num_verifier_constraints > 0) {
RTVerifierConstraint* vf_constraints = verifier_constraints();
char* flags = verifier_constraint_flags();
for (i = 0; i < _num_verifier_constraints; i++) {
vf_constraints[i]._name = builder->any_to_offset_u4(info._verifier_constraints->at(i)._name);
vf_constraints[i]._from_name = builder->any_to_offset_u4(info._verifier_constraints->at(i)._from_name);
}
for (i = 0; i < _num_verifier_constraints; i++) {
flags[i] = info._verifier_constraint_flags->at(i);
}
}
if (_num_loader_constraints > 0) {
RTLoaderConstraint* ld_constraints = loader_constraints();
for (i = 0; i < _num_loader_constraints; i++) {
ld_constraints[i]._name = builder->any_to_offset_u4(info._loader_constraints->at(i)._name);
ld_constraints[i]._loader_type1 = info._loader_constraints->at(i)._loader_type1;
ld_constraints[i]._loader_type2 = info._loader_constraints->at(i)._loader_type2;
}
}
if (_klass->is_hidden()) {
InstanceKlass* n_h = info.nest_host();
set_nest_host(n_h);
}
ArchivePtrMarker::mark_pointer(&_klass);
}
bool matches(int clsfile_size, int clsfile_crc32) const {
return crc()->_clsfile_size == clsfile_size &&
crc()->_clsfile_crc32 == clsfile_crc32;
}
char verifier_constraint_flag(int i) {
check_verifier_constraint_offset(i);
return verifier_constraint_flags()[i];
}
private:
// ArchiveBuilder::make_shallow_copy() has reserved a pointer immediately
// before archived InstanceKlasses. We can use this slot to do a quick
// lookup of InstanceKlass* -> RunTimeSharedClassInfo* without
// building a new hashtable.
//
// info_pointer_addr(klass) --> 0x0100 RunTimeSharedClassInfo*
// InstanceKlass* klass --> 0x0108 <C++ vtbl>
// 0x0110 fields from Klass ...
static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) {
return &((RunTimeSharedClassInfo**)klass)[-1];
}
public:
static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) {
assert(klass->is_shared(), "don't call for non-shared class");
return *info_pointer_addr(klass);
}
static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) {
assert(ArchiveBuilder::current()->is_in_buffer_space(klass), "must be");
assert(ArchiveBuilder::current()->is_in_buffer_space(record), "must be");
*info_pointer_addr(klass) = record;
ArchivePtrMarker::mark_pointer(info_pointer_addr(klass));
}
// Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS
static inline bool EQUALS(
const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) {
return (value->_klass->name() == key);
}
};
class RunTimeSharedDictionary : public OffsetCompactHashtable<
Symbol*,
const RunTimeSharedClassInfo*,
RunTimeSharedClassInfo::EQUALS> {};
static DumpTimeSharedClassTable* _dumptime_table = NULL;
// SystemDictionaries in the base layer static archive
static RunTimeSharedDictionary _builtin_dictionary;
static RunTimeSharedDictionary _unregistered_dictionary;
// SystemDictionaries in the top layer dynamic archive
static RunTimeSharedDictionary _dynamic_builtin_dictionary;
static RunTimeSharedDictionary _dynamic_unregistered_dictionary;
void SystemDictionaryShared::atomic_set_array_index(OopHandle array, int index, oop o) {
// Benign race condition: array.obj_at(index) may already be filled in.
// The important thing here is that all threads pick up the same result.
// It doesn't matter which racing thread wins, as long as only one
// result is used by all threads, and all future queries.
((objArrayOop)array.resolve())->atomic_compare_exchange_oop(index, o, NULL);
}
Handle SystemDictionaryShared::create_jar_manifest(const char* manifest_chars, size_t size, TRAPS) {
typeArrayOop buf = oopFactory::new_byteArray((int)size, CHECK_NH);
typeArrayHandle bufhandle(THREAD, buf);
ArrayAccess<>::arraycopy_from_native(reinterpret_cast<const jbyte*>(manifest_chars),
buf, typeArrayOopDesc::element_offset<jbyte>(0), size);
Handle bais = JavaCalls::construct_new_instance(vmClasses::ByteArrayInputStream_klass(),
vmSymbols::byte_array_void_signature(),
bufhandle, CHECK_NH);
// manifest = new Manifest(ByteArrayInputStream)
Handle manifest = JavaCalls::construct_new_instance(vmClasses::Jar_Manifest_klass(),
vmSymbols::input_stream_void_signature(),
bais, CHECK_NH);
return manifest;
}
oop SystemDictionaryShared::shared_protection_domain(int index) {
return ((objArrayOop)_shared_protection_domains.resolve())->obj_at(index);
}
oop SystemDictionaryShared::shared_jar_url(int index) {
return ((objArrayOop)_shared_jar_urls.resolve())->obj_at(index);
}
oop SystemDictionaryShared::shared_jar_manifest(int index) {
return ((objArrayOop)_shared_jar_manifests.resolve())->obj_at(index);
}
Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) {
Handle manifest ;
if (shared_jar_manifest(shared_path_index) == NULL) {
SharedClassPathEntry* ent = FileMapInfo::shared_path(shared_path_index);
size_t size = (size_t)ent->manifest_size();
if (size == 0) {
return Handle();
}
// ByteArrayInputStream bais = new ByteArrayInputStream(buf);
const char* src = ent->manifest();
assert(src != NULL, "No Manifest data");
manifest = create_jar_manifest(src, size, CHECK_NH);
atomic_set_shared_jar_manifest(shared_path_index, manifest());
}
manifest = Handle(THREAD, shared_jar_manifest(shared_path_index));
assert(manifest.not_null(), "sanity");
return manifest;
}
Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) {
Handle url_h;
if (shared_jar_url(shared_path_index) == NULL) {
JavaValue result(T_OBJECT);
const char* path = FileMapInfo::shared_path_name(shared_path_index);
Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h));
Klass* classLoaders_klass =
vmClasses::jdk_internal_loader_ClassLoaders_klass();
JavaCalls::call_static(&result, classLoaders_klass,
vmSymbols::toFileURL_name(),
vmSymbols::toFileURL_signature(),
path_string, CHECK_(url_h));
atomic_set_shared_jar_url(shared_path_index, result.get_oop());
}
url_h = Handle(THREAD, shared_jar_url(shared_path_index));
assert(url_h.not_null(), "sanity");
return url_h;
}
Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) {
ResourceMark rm(THREAD);
Handle pkgname_string;
TempNewSymbol pkg = ClassLoader::package_from_class_name(class_name);
if (pkg != NULL) { // Package prefix found
const char* pkgname = pkg->as_klass_external_name();
pkgname_string = java_lang_String::create_from_str(pkgname,
CHECK_(pkgname_string));
}
return pkgname_string;
}
// Define Package for shared app classes from JAR file and also checks for
// package sealing (all done in Java code)
// See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html
void SystemDictionaryShared::define_shared_package(Symbol* class_name,
Handle class_loader,
Handle manifest,
Handle url,
TRAPS) {
assert(SystemDictionary::is_system_class_loader(class_loader()), "unexpected class loader");
// get_package_name() returns a NULL handle if the class is in unnamed package
Handle pkgname_string = get_package_name(class_name, CHECK);
if (pkgname_string.not_null()) {
Klass* app_classLoader_klass = vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass();
JavaValue result(T_OBJECT);
JavaCallArguments args(3);
args.set_receiver(class_loader);
args.push_oop(pkgname_string);
args.push_oop(manifest);
args.push_oop(url);
JavaCalls::call_virtual(&result, app_classLoader_klass,
vmSymbols::defineOrCheckPackage_name(),
vmSymbols::defineOrCheckPackage_signature(),
&args,
CHECK);
}
}
// Get the ProtectionDomain associated with the CodeSource from the classloader.
Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader,
Handle url, TRAPS) {
// CodeSource cs = new CodeSource(url, null);
Handle cs = JavaCalls::construct_new_instance(vmClasses::CodeSource_klass(),
vmSymbols::url_code_signer_array_void_signature(),
url, Handle(), CHECK_NH);
// protection_domain = SecureClassLoader.getProtectionDomain(cs);
Klass* secureClassLoader_klass = vmClasses::SecureClassLoader_klass();
JavaValue obj_result(T_OBJECT);
JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass,
vmSymbols::getProtectionDomain_name(),
vmSymbols::getProtectionDomain_signature(),
cs, CHECK_NH);
return Handle(THREAD, obj_result.get_oop());
}
// Returns the ProtectionDomain associated with the JAR file identified by the url.
Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
int shared_path_index,
Handle url,
TRAPS) {
Handle protection_domain;
if (shared_protection_domain(shared_path_index) == NULL) {
Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD);
atomic_set_shared_protection_domain(shared_path_index, pd());
}
// Acquire from the cache because if another thread beats the current one to
// set the shared protection_domain and the atomic_set fails, the current thread
// needs to get the updated protection_domain from the cache.
protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index));
assert(protection_domain.not_null(), "sanity");
return protection_domain;
}
// Returns the ProtectionDomain associated with the moduleEntry.
Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
ModuleEntry* mod, TRAPS) {
ClassLoaderData *loader_data = mod->loader_data();
if (mod->shared_protection_domain() == NULL) {
Symbol* location = mod->location();
if (location != NULL) {
Handle location_string = java_lang_String::create_from_symbol(
location, CHECK_NH);
Handle url;
JavaValue result(T_OBJECT);
if (location->starts_with("jrt:/")) {
url = JavaCalls::construct_new_instance(vmClasses::URL_klass(),
vmSymbols::string_void_signature(),
location_string, CHECK_NH);
} else {
Klass* classLoaders_klass =
vmClasses::jdk_internal_loader_ClassLoaders_klass();
JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(),
vmSymbols::toFileURL_signature(),
location_string, CHECK_NH);
url = Handle(THREAD, result.get_oop());
}
Handle pd = get_protection_domain_from_classloader(class_loader, url,
CHECK_NH);
mod->set_shared_protection_domain(loader_data, pd);
}
}
Handle protection_domain(THREAD, mod->shared_protection_domain());
assert(protection_domain.not_null(), "sanity");
return protection_domain;
}
// Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with
// the given InstanceKlass.
// Returns the ProtectionDomain for the InstanceKlass.
Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS) {
Handle pd;
if (ik != NULL) {
int index = ik->shared_classpath_index();
assert(index >= 0, "Sanity");
SharedClassPathEntry* ent = FileMapInfo::shared_path(index);
Symbol* class_name = ik->name();
if (ent->is_modules_image()) {
// For shared app/platform classes originated from the run-time image:
// The ProtectionDomains are cached in the corresponding ModuleEntries
// for fast access by the VM.
// all packages from module image are already created during VM bootstrap in
// Modules::define_module().
assert(pkg_entry != NULL, "archived class in module image cannot be from unnamed package");
ModuleEntry* mod_entry = pkg_entry->module();
pd = get_shared_protection_domain(class_loader, mod_entry, CHECK_(pd));
} else {
// For shared app/platform classes originated from JAR files on the class path:
// Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length
// as the shared classpath table in the shared archive (see
// FileMap::_shared_path_table in filemap.hpp for details).
//
// If a shared InstanceKlass k is loaded from the class path, let
//
// index = k->shared_classpath_index():
//
// FileMap::_shared_path_table[index] identifies the JAR file that contains k.
//
// k's protection domain is:
//
// ProtectionDomain pd = _shared_protection_domains[index];
//
// and k's Package is initialized using
//
// manifest = _shared_jar_manifests[index];
// url = _shared_jar_urls[index];
// define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
//
// Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by
// the corresponding SystemDictionaryShared::get_shared_xxx() function.
Handle manifest = get_shared_jar_manifest(index, CHECK_(pd));
Handle url = get_shared_jar_url(index, CHECK_(pd));
int index_offset = index - ClassLoaderExt::app_class_paths_start_index();
if (index_offset < PackageEntry::max_index_for_defined_in_class_path()) {
if (pkg_entry == NULL || !pkg_entry->is_defined_by_cds_in_class_path(index_offset)) {
// define_shared_package only needs to be called once for each package in a jar specified
// in the shared class path.
define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
if (pkg_entry != NULL) {
pkg_entry->set_defined_by_cds_in_class_path(index_offset);
}
}
} else {
define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
}
pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd));
}
}
return pd;
}
bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
oop class_loader = loader_data->class_loader();
return (class_loader == NULL ||
SystemDictionary::is_system_class_loader(class_loader) ||
SystemDictionary::is_platform_class_loader(class_loader));
}
bool SystemDictionaryShared::has_platform_or_app_classes() {
if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
return true;
}
if (DynamicArchive::is_mapped() &&
FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
return true;
}
return false;
}
// The following stack shows how this code is reached:
//
// [0] SystemDictionaryShared::find_or_load_shared_class()
// [1] JVM_FindLoadedClass
// [2] java.lang.ClassLoader.findLoadedClass0()
// [3] java.lang.ClassLoader.findLoadedClass()
// [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull()
// [5] jdk.internal.loader.BuiltinClassLoader.loadClass()
// [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or
// jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass()
//
// AppCDS supports fast class loading for these 2 built-in class loaders:
// jdk.internal.loader.ClassLoaders$PlatformClassLoader
// jdk.internal.loader.ClassLoaders$AppClassLoader
// with the following assumptions (based on the JDK core library source code):
//
// [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to
// load the named class.
// [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name).
// [c] At this point, if we can find the named class inside the
// shared_dictionary, we can perform further checks (see
// SystemDictionary::is_shared_class_visible) to ensure that this class
// was loaded by the same class loader during dump time.
//
// Given these assumptions, we intercept the findLoadedClass() call to invoke
// SystemDictionaryShared::find_or_load_shared_class() to load the shared class from
// the archive for the 2 built-in class loaders. This way,
// we can improve start-up because we avoid decoding the classfile,
// and avoid delegating to the parent loader.
//
// NOTE: there's a lot of assumption about the Java code. If any of that change, this
// needs to be redesigned.
InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
Symbol* name, Handle class_loader, TRAPS) {
InstanceKlass* k = NULL;
if (UseSharedSpaces) {