-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathjavaClasses.cpp
4586 lines (3873 loc) · 170 KB
/
javaClasses.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) 1997, 2018, 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/altHashing.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/stringTable.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/debugInfo.hpp"
#include "code/dependencyContext.hpp"
#include "code/pcDesc.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/linkResolver.hpp"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/oopFactory.hpp"
#include "memory/metaspaceShared.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/fieldStreams.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/instanceMirrorKlass.hpp"
#include "oops/klass.hpp"
#include "oops/method.inline.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/symbol.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "prims/resolvedMethodTable.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/safepoint.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/vframe.inline.hpp"
#include "utilities/align.hpp"
#include "utilities/preserveException.hpp"
#if INCLUDE_JVMCI
#include "jvmci/jvmciJavaClasses.hpp"
#endif
#define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \
klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
#if INCLUDE_CDS
#define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \
f->do_u4((u4*)&_##name##_offset);
#endif
#define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \
{ SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },
InjectedField JavaClasses::_injected_fields[] = {
ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
};
int JavaClasses::compute_injected_offset(InjectedFieldID id) {
return _injected_fields[id].compute_offset();
}
InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) {
*field_count = 0;
vmSymbols::SID sid = vmSymbols::find_sid(class_name);
if (sid == vmSymbols::NO_SID) {
// Only well known classes can inject fields
return NULL;
}
int count = 0;
int start = -1;
#define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \
if (sid == vmSymbols::VM_SYMBOL_ENUM_NAME(klass)) { \
count++; \
if (start == -1) start = klass##_##name##_enum; \
}
ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD);
#undef LOOKUP_INJECTED_FIELD
if (start != -1) {
*field_count = count;
return _injected_fields + start;
}
return NULL;
}
// Helpful routine for computing field offsets at run time rather than hardcoding them
// Finds local fields only, including static fields. Static field offsets are from the
// beginning of the mirror.
static void compute_offset(int &dest_offset,
InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol,
bool is_static = false) {
fieldDescriptor fd;
if (ik == NULL) {
ResourceMark rm;
log_error(class)("Mismatch JDK version for field: %s type: %s", name_symbol->as_C_string(), signature_symbol->as_C_string());
vm_exit_during_initialization("Invalid layout of preloaded class");
}
if (!ik->find_local_field(name_symbol, signature_symbol, &fd) || fd.is_static() != is_static) {
ResourceMark rm;
log_error(class)("Invalid layout of %s field: %s type: %s", ik->external_name(),
name_symbol->as_C_string(), signature_symbol->as_C_string());
#ifndef PRODUCT
// Prints all fields and offsets
Log(class) lt;
LogStream ls(lt.error());
ik->print_on(&ls);
#endif //PRODUCT
vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
}
dest_offset = fd.offset();
}
// Overloading to pass name as a string.
static void compute_offset(int& dest_offset, InstanceKlass* ik,
const char* name_string, Symbol* signature_symbol,
bool is_static = false) {
TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
if (name == NULL) {
ResourceMark rm;
log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
vm_exit_during_initialization("Invalid layout of preloaded class", ik->external_name());
}
compute_offset(dest_offset, ik, name, signature_symbol, is_static);
}
// Same as above but for "optional" offsets that might not be present in certain JDK versions
// Old versions should be cleaned out since Hotspot only supports the current JDK, and this
// function should be removed.
static void
compute_optional_offset(int& dest_offset,
InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol) {
fieldDescriptor fd;
if (ik->find_local_field(name_symbol, signature_symbol, &fd)) {
dest_offset = fd.offset();
}
}
int java_lang_String::value_offset = 0;
int java_lang_String::hash_offset = 0;
int java_lang_String::coder_offset = 0;
bool java_lang_String::initialized = false;
bool java_lang_String::is_instance(oop obj) {
return is_instance_inlined(obj);
}
#if INCLUDE_CDS
#define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
f->do_u4((u4*)&offset)
#define FIELD_SERIALIZE_OFFSET_OPTIONAL(offset, klass, name, signature) \
f->do_u4((u4*)&offset)
#endif
#define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
#define FIELD_COMPUTE_OFFSET_OPTIONAL(offset, klass, name, signature) \
compute_optional_offset(offset, klass, name, vmSymbols::signature())
#define STRING_FIELDS_DO(macro) \
macro(value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \
macro(hash_offset, k, "hash", int_signature, false); \
macro(coder_offset, k, "coder", byte_signature, false)
void java_lang_String::compute_offsets() {
if (initialized) {
return;
}
InstanceKlass* k = SystemDictionary::String_klass();
STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
initialized = true;
}
#if INCLUDE_CDS
void java_lang_String::serialize_offsets(SerializeClosure* f) {
STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
f->do_u4((u4*)&initialized);
}
#endif
class CompactStringsFixup : public FieldClosure {
private:
bool _value;
public:
CompactStringsFixup(bool value) : _value(value) {}
void do_field(fieldDescriptor* fd) {
if (fd->name() == vmSymbols::compact_strings_name()) {
oop mirror = fd->field_holder()->java_mirror();
assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String");
assert(mirror != NULL, "String must have mirror already");
mirror->bool_field_put(fd->offset(), _value);
}
}
};
void java_lang_String::set_compact_strings(bool value) {
CompactStringsFixup fix(value);
InstanceKlass::cast(SystemDictionary::String_klass())->do_local_static_fields(&fix);
}
Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
assert(initialized, "Must be initialized");
assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
// Create the String object first, so there's a chance that the String
// and the char array it points to end up in the same cache line.
oop obj;
obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH);
// Create the char array. The String object must be handlized here
// because GC can happen as a result of the allocation attempt.
Handle h_obj(THREAD, obj);
int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
// Point the String at the char array
obj = h_obj();
set_value(obj, buffer);
// No need to zero the offset, allocation zero'ed the entire String object
set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16);
return h_obj;
}
Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) {
bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length);
Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
typeArrayOop buffer = value(h_obj());
assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]");
if (is_latin1) {
for (int index = 0; index < length; index++) {
buffer->byte_at_put(index, (jbyte)unicode[index]);
}
} else {
for (int index = 0; index < length; index++) {
buffer->char_at_put(index, unicode[index]);
}
}
#ifdef ASSERT
{
ResourceMark rm;
char* expected = UNICODE::as_utf8(unicode, length);
char* actual = as_utf8_string(h_obj());
if (strcmp(expected, actual) != 0) {
tty->print_cr("Unicode conversion failure: %s --> %s", expected, actual);
ShouldNotReachHere();
}
}
#endif
return h_obj;
}
oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
return h_obj();
}
Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
if (utf8_str == NULL) {
return Handle();
}
bool has_multibyte, is_latin1;
int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
if (!CompactStrings) {
has_multibyte = true;
is_latin1 = false;
}
Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
if (length > 0) {
if (!has_multibyte) {
const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str);
ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
} else if (is_latin1) {
UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
} else {
UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
}
}
#ifdef ASSERT
// This check is too strict because the input string is not necessarily valid UTF8.
// For example, it may be created with arbitrary content via jni_NewStringUTF.
/*
{
ResourceMark rm;
const char* expected = utf8_str;
char* actual = as_utf8_string(h_obj());
if (strcmp(expected, actual) != 0) {
tty->print_cr("String conversion failure: %s --> %s", expected, actual);
ShouldNotReachHere();
}
}
*/
#endif
return h_obj;
}
oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
Handle h_obj = create_from_str(utf8_str, CHECK_0);
return h_obj();
}
Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
const char* utf8_str = (char*)symbol->bytes();
int utf8_len = symbol->utf8_length();
bool has_multibyte, is_latin1;
int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
if (!CompactStrings) {
has_multibyte = true;
is_latin1 = false;
}
Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
if (length > 0) {
if (!has_multibyte) {
const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str);
ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
} else if (is_latin1) {
UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
} else {
UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
}
}
#ifdef ASSERT
{
ResourceMark rm;
const char* expected = symbol->as_utf8();
char* actual = as_utf8_string(h_obj());
if (strncmp(expected, actual, utf8_len) != 0) {
tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual);
ShouldNotReachHere();
}
}
#endif
return h_obj;
}
// Converts a C string to a Java String based on current encoding
Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) {
assert(str != NULL, "bad arguments");
typedef jstring (*to_java_string_fn_t)(JNIEnv*, const char *);
static to_java_string_fn_t _to_java_string_fn = NULL;
if (_to_java_string_fn == NULL) {
void *lib_handle = os::native_java_library();
_to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "NewStringPlatform"));
if (_to_java_string_fn == NULL) {
fatal("NewStringPlatform missing");
}
}
jstring js = NULL;
{ JavaThread* thread = (JavaThread*)THREAD;
assert(thread->is_Java_thread(), "must be java thread");
HandleMark hm(thread);
ThreadToNativeFromVM ttn(thread);
js = (_to_java_string_fn)(thread->jni_environment(), str);
}
return Handle(THREAD, JNIHandles::resolve(js));
}
// Converts a Java String to a native C string that can be used for
// native OS calls.
char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) {
typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*);
static to_platform_string_fn_t _to_platform_string_fn = NULL;
if (_to_platform_string_fn == NULL) {
void *lib_handle = os::native_java_library();
_to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars"));
if (_to_platform_string_fn == NULL) {
fatal("GetStringPlatformChars missing");
}
}
char *native_platform_string;
{ JavaThread* thread = (JavaThread*)THREAD;
assert(thread->is_Java_thread(), "must be java thread");
JNIEnv *env = thread->jni_environment();
jstring js = (jstring) JNIHandles::make_local(env, java_string());
bool is_copy;
HandleMark hm(thread);
ThreadToNativeFromVM ttn(thread);
native_platform_string = (_to_platform_string_fn)(env, js, &is_copy);
assert(is_copy == JNI_TRUE, "is_copy value changed");
JNIHandles::destroy_local(js);
}
return native_platform_string;
}
Handle java_lang_String::char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS) {
oop obj = java_string();
// Typical usage is to convert all '/' to '.' in string.
typeArrayOop value = java_lang_String::value(obj);
int length = java_lang_String::length(obj);
bool is_latin1 = java_lang_String::is_latin1(obj);
// First check if any from_char exist
int index; // Declared outside, used later
for (index = 0; index < length; index++) {
jchar c = !is_latin1 ? value->char_at(index) :
((jchar) value->byte_at(index)) & 0xff;
if (c == from_char) {
break;
}
}
if (index == length) {
// No from_char, so do not copy.
return java_string;
}
// Check if result string will be latin1
bool to_is_latin1 = false;
// Replacement char must be latin1
if (CompactStrings && UNICODE::is_latin1(to_char)) {
if (is_latin1) {
// Source string is latin1 as well
to_is_latin1 = true;
} else if (!UNICODE::is_latin1(from_char)) {
// We are replacing an UTF16 char. Scan string to
// check if result can be latin1 encoded.
to_is_latin1 = true;
for (index = 0; index < length; index++) {
jchar c = value->char_at(index);
if (c != from_char && !UNICODE::is_latin1(c)) {
to_is_latin1 = false;
break;
}
}
}
}
// Create new UNICODE (or byte) buffer. Must handlize value because GC
// may happen during String and char array creation.
typeArrayHandle h_value(THREAD, value);
Handle string = basic_create(length, to_is_latin1, CHECK_NH);
typeArrayOop from_buffer = h_value();
typeArrayOop to_buffer = java_lang_String::value(string());
// Copy contents
for (index = 0; index < length; index++) {
jchar c = (!is_latin1) ? from_buffer->char_at(index) :
((jchar) from_buffer->byte_at(index)) & 0xff;
if (c == from_char) {
c = to_char;
}
if (!to_is_latin1) {
to_buffer->char_at_put(index, c);
} else {
to_buffer->byte_at_put(index, (jbyte) c);
}
}
return string;
}
jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) {
typeArrayOop value = java_lang_String::value(java_string);
length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
if (result != NULL) {
if (!is_latin1) {
for (int index = 0; index < length; index++) {
result[index] = value->char_at(index);
}
} else {
for (int index = 0; index < length; index++) {
result[index] = ((jchar) value->byte_at(index)) & 0xff;
}
}
} else {
THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
}
return result;
}
unsigned int java_lang_String::hash_code(oop java_string) {
int length = java_lang_String::length(java_string);
// Zero length string will hash to zero with String.hashCode() function.
if (length == 0) return 0;
typeArrayOop value = java_lang_String::value(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (is_latin1) {
return java_lang_String::hash_code(value->byte_at_addr(0), length);
} else {
return java_lang_String::hash_code(value->char_at_addr(0), length);
}
}
char* java_lang_String::as_quoted_ascii(oop java_string) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (length == 0) return NULL;
char* result;
int result_length;
if (!is_latin1) {
jchar* base = value->char_at_addr(0);
result_length = UNICODE::quoted_ascii_length(base, length) + 1;
result = NEW_RESOURCE_ARRAY(char, result_length);
UNICODE::as_quoted_ascii(base, length, result, result_length);
} else {
jbyte* base = value->byte_at_addr(0);
result_length = UNICODE::quoted_ascii_length(base, length) + 1;
result = NEW_RESOURCE_ARRAY(char, result_length);
UNICODE::as_quoted_ascii(base, length, result, result_length);
}
assert(result_length >= length + 1, "must not be shorter");
assert(result_length == (int)strlen(result) + 1, "must match");
return result;
}
Symbol* java_lang_String::as_symbol(oop java_string, TRAPS) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD);
return sym;
} else {
ResourceMark rm;
jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
const char* base = UNICODE::as_utf8(position, length);
Symbol* sym = SymbolTable::lookup(base, length, THREAD);
return sym;
}
}
Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
return SymbolTable::probe_unicode(base, length);
} else {
ResourceMark rm;
jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
const char* base = UNICODE::as_utf8(position, length);
return SymbolTable::probe(base, length);
}
}
int java_lang_String::utf8_length(oop java_string) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (length == 0) {
return 0;
}
if (!is_latin1) {
return UNICODE::utf8_length(value->char_at_addr(0), length);
} else {
return UNICODE::utf8_length(value->byte_at_addr(0), length);
}
}
char* java_lang_String::as_utf8_string(oop java_string) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
return UNICODE::as_utf8(position, length);
} else {
jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
return UNICODE::as_utf8(position, length);
}
}
char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
return UNICODE::as_utf8(position, length, buf, buflen);
} else {
jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
return UNICODE::as_utf8(position, length, buf, buflen);
}
}
char* java_lang_String::as_utf8_string(oop java_string, int start, int len) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
assert(start + len <= length, "just checking");
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* position = value->char_at_addr(start);
return UNICODE::as_utf8(position, len);
} else {
jbyte* position = value->byte_at_addr(start);
return UNICODE::as_utf8(position, len);
}
}
char* java_lang_String::as_utf8_string(oop java_string, int start, int len, char* buf, int buflen) {
typeArrayOop value = java_lang_String::value(java_string);
int length = java_lang_String::length(java_string);
assert(start + len <= length, "just checking");
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
jchar* position = value->char_at_addr(start);
return UNICODE::as_utf8(position, len, buf, buflen);
} else {
jbyte* position = value->byte_at_addr(start);
return UNICODE::as_utf8(position, len, buf, buflen);
}
}
bool java_lang_String::equals(oop java_string, jchar* chars, int len) {
assert(java_string->klass() == SystemDictionary::String_klass(),
"must be java_string");
typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
int length = java_lang_String::length(java_string);
if (length != len) {
return false;
}
bool is_latin1 = java_lang_String::is_latin1(java_string);
if (!is_latin1) {
for (int i = 0; i < len; i++) {
if (value->char_at(i) != chars[i]) {
return false;
}
}
} else {
for (int i = 0; i < len; i++) {
if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) {
return false;
}
}
}
return true;
}
bool java_lang_String::equals(oop str1, oop str2) {
assert(str1->klass() == SystemDictionary::String_klass(),
"must be java String");
assert(str2->klass() == SystemDictionary::String_klass(),
"must be java String");
typeArrayOop value1 = java_lang_String::value_no_keepalive(str1);
int length1 = java_lang_String::length(str1);
bool is_latin1 = java_lang_String::is_latin1(str1);
typeArrayOop value2 = java_lang_String::value_no_keepalive(str2);
int length2 = java_lang_String::length(str2);
bool is_latin2 = java_lang_String::is_latin1(str2);
if ((length1 != length2) || (is_latin1 != is_latin2)) {
// Strings of different size or with different
// coders are never equal.
return false;
}
int blength1 = value1->length();
for (int i = 0; i < blength1; i++) {
if (value1->byte_at(i) != value2->byte_at(i)) {
return false;
}
}
return true;
}
void java_lang_String::print(oop java_string, outputStream* st) {
assert(java_string->klass() == SystemDictionary::String_klass(), "must be java_string");
typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
if (value == NULL) {
// This can happen if, e.g., printing a String
// object before its initializer has been called
st->print("NULL");
return;
}
int length = java_lang_String::length(java_string);
bool is_latin1 = java_lang_String::is_latin1(java_string);
st->print("\"");
for (int index = 0; index < length; index++) {
st->print("%c", (!is_latin1) ? value->char_at(index) :
((jchar) value->byte_at(index)) & 0xff );
}
st->print("\"");
}
static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
assert(mirror.not_null() && fd->is_static(), "just checking");
if (fd->has_initial_value()) {
BasicType t = fd->field_type();
switch (t) {
case T_BYTE:
mirror()->byte_field_put(fd->offset(), fd->int_initial_value());
break;
case T_BOOLEAN:
mirror()->bool_field_put(fd->offset(), fd->int_initial_value());
break;
case T_CHAR:
mirror()->char_field_put(fd->offset(), fd->int_initial_value());
break;
case T_SHORT:
mirror()->short_field_put(fd->offset(), fd->int_initial_value());
break;
case T_INT:
mirror()->int_field_put(fd->offset(), fd->int_initial_value());
break;
case T_FLOAT:
mirror()->float_field_put(fd->offset(), fd->float_initial_value());
break;
case T_DOUBLE:
mirror()->double_field_put(fd->offset(), fd->double_initial_value());
break;
case T_LONG:
mirror()->long_field_put(fd->offset(), fd->long_initial_value());
break;
case T_OBJECT:
{
assert(fd->signature() == vmSymbols::string_signature(),
"just checking");
if (DumpSharedSpaces && MetaspaceShared::is_archive_object(mirror())) {
// Archive the String field and update the pointer.
oop s = mirror()->obj_field(fd->offset());
oop archived_s = StringTable::create_archived_string(s, CHECK);
mirror()->obj_field_put(fd->offset(), archived_s);
} else {
oop string = fd->string_initial_value(CHECK);
mirror()->obj_field_put(fd->offset(), string);
}
}
break;
default:
THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
"Illegal ConstantValue attribute in class file");
}
}
}
void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
// If the offset was read from the shared archive, it was fixed up already
if (!k->is_shared()) {
if (k->is_instance_klass()) {
// During bootstrap, java.lang.Class wasn't loaded so static field
// offsets were computed without the size added it. Go back and
// update all the static field offsets to included the size.
for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
if (fs.access_flags().is_static()) {
int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
fs.set_offset(real_offset);
}
}
}
}
if (k->is_shared() && k->has_raw_archived_mirror()) {
if (MetaspaceShared::open_archive_heap_region_mapped()) {
bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
assert(present, "Missing archived mirror for %s", k->external_name());
return;
} else {
k->set_java_mirror_handle(NULL);
k->clear_has_raw_archived_mirror();
}
}
create_mirror(k, Handle(), Handle(), Handle(), CHECK);
}
void java_lang_Class::initialize_mirror_fields(Klass* k,
Handle mirror,
Handle protection_domain,
TRAPS) {
// Allocate a simple java object for a lock.
// This needs to be a java object because during class initialization
// it can be held across a java call.
typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
set_init_lock(mirror(), r);
// Set protection domain also
set_protection_domain(mirror(), protection_domain());
// Initialize static fields
InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
}
// Set the java.lang.Module module field in the java_lang_Class mirror
void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) {
if (module.is_null()) {
// During startup, the module may be NULL only if java.base has not been defined yet.
// Put the class on the fixup_module_list to patch later when the java.lang.Module
// for java.base is known.
assert(!Universe::is_module_initialized(), "Incorrect java.lang.Module pre module system initialization");
bool javabase_was_defined = false;
{
MutexLocker m1(Module_lock, THREAD);
// Keep list of classes needing java.base module fixup
if (!ModuleEntryTable::javabase_defined()) {
assert(k->java_mirror() != NULL, "Class's mirror is null");
k->class_loader_data()->inc_keep_alive();
assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
fixup_module_field_list()->push(k);
} else {
javabase_was_defined = true;
}
}
// If java.base was already defined then patch this particular class with java.base.
if (javabase_was_defined) {
ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry();
assert(javabase_entry != NULL && javabase_entry->module() != NULL,
"Setting class module field, " JAVA_BASE_NAME " should be defined");
Handle javabase_handle(THREAD, javabase_entry->module());
set_module(mirror(), javabase_handle());
}
} else {
assert(Universe::is_module_initialized() ||
(ModuleEntryTable::javabase_defined() &&
(oopDesc::equals(module(), ModuleEntryTable::javabase_moduleEntry()->module()))),
"Incorrect java.lang.Module specification while creating mirror");
set_module(mirror(), module());
}
}
// Statically allocate fixup lists because they always get created.
void java_lang_Class::allocate_fixup_lists() {
GrowableArray<Klass*>* mirror_list =
new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
set_fixup_mirror_list(mirror_list);
GrowableArray<Klass*>* module_list =
new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
set_fixup_module_field_list(module_list);
}
void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
Handle module, Handle protection_domain, TRAPS) {
assert(k != NULL, "Use create_basic_type_mirror for primitive types");
assert(k->java_mirror() == NULL, "should only assign mirror once");
// Use this moment of initialization to cache modifier_flags also,
// to support Class.getModifiers(). Instance classes recalculate
// the cached flags after the class file is parsed, but before the
// class is put into the system dictionary.
int computed_modifiers = k->compute_modifier_flags(CHECK);
k->set_modifier_flags(computed_modifiers);
// Class_klass has to be loaded because it is used to allocate
// the mirror.
if (SystemDictionary::Class_klass_loaded()) {
// Allocate mirror (java.lang.Class instance)
oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
Handle mirror(THREAD, mirror_oop);
Handle comp_mirror;
// Setup indirection from mirror->klass
java_lang_Class::set_klass(mirror(), k);
InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
// It might also have a component mirror. This mirror must already exist.
if (k->is_array_klass()) {
if (k->is_typeArray_klass()) {
BasicType type = TypeArrayKlass::cast(k)->element_type();
comp_mirror = Handle(THREAD, Universe::java_mirror(type));
} else {
assert(k->is_objArray_klass(), "Must be");
Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
assert(element_klass != NULL, "Must have an element klass");
comp_mirror = Handle(THREAD, element_klass->java_mirror());
}
assert(comp_mirror() != NULL, "must have a mirror");
// Two-way link between the array klass and its component mirror:
// (array_klass) k -> mirror -> component_mirror -> array_klass -> k
set_component_mirror(mirror(), comp_mirror());
// See below for ordering dependencies between field array_klass in component mirror
// and java_mirror in this klass.
} else {
assert(k->is_instance_klass(), "Must be");
initialize_mirror_fields(k, mirror, protection_domain, THREAD);
if (HAS_PENDING_EXCEPTION) {
// If any of the fields throws an exception like OOM remove the klass field
// from the mirror so GC doesn't follow it after the klass has been deallocated.
// This mirror looks like a primitive type, which logically it is because it
// it represents no class.
java_lang_Class::set_klass(mirror(), NULL);
return;
}
}
// set the classLoader field in the java_lang_Class instance
assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same");
set_class_loader(mirror(), class_loader());
// Setup indirection from klass->mirror
// after any exceptions can happen during allocations.
k->set_java_mirror(mirror);
// Set the module field in the java_lang_Class instance. This must be done
// after the mirror is set.
set_mirror_module_field(k, mirror, module, THREAD);
if (comp_mirror() != NULL) {
// Set after k->java_mirror() is published, because compiled code running
// concurrently doesn't expect a k to have a null java_mirror.
release_set_array_klass(comp_mirror(), k);
}
} else {
assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
fixup_mirror_list()->push(k);
}
}
#if INCLUDE_CDS_JAVA_HEAP
// Clears mirror fields. Static final fields with initial values are reloaded
// from constant pool. The object identity hash is in the object header and is
// not affected.
class ResetMirrorField: public FieldClosure {
private:
Handle _m;
public:
ResetMirrorField(Handle mirror) : _m(mirror) {}
void do_field(fieldDescriptor* fd) {
assert(DumpSharedSpaces, "dump time only");
assert(_m.not_null(), "Mirror cannot be NULL");
if (fd->is_static() && fd->has_initial_value()) {
initialize_static_field(fd, _m, Thread::current());
return;
}
BasicType ft = fd->field_type();
switch (ft) {
case T_BYTE:
_m()->byte_field_put(fd->offset(), 0);
break;
case T_CHAR:
_m()->char_field_put(fd->offset(), 0);
break;
case T_DOUBLE:
_m()->double_field_put(fd->offset(), 0);