-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathclasspath-openjdk.cpp
6193 lines (5123 loc) · 171 KB
/
classpath-openjdk.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) 2008-2015, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#include "avian/machine.h"
#include "avian/classpath-common.h"
#include "avian/util.h"
#include "avian/process.h"
#ifdef PLATFORM_WINDOWS
#include <windows.h>
#include <io.h>
#include <direct.h>
#include <share.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#undef interface
#define CLOSE _close
#define READ _read
#define WRITE _write
#define FSTAT _fstat
#define STAT _stat
#define LSEEK _lseek
#define S_ISSOCK(x) false
#ifdef _MSC_VER
#define S_ISREG(x) ((x) | _S_IFREG)
#define S_ISDIR(x) ((x) | _S_IFDIR)
#define S_IRUSR _S_IREAD
#define S_IWUSR _S_IWRITE
#else
#define OPEN _open
#endif
#define O_RDONLY _O_RDONLY
#if (defined AVIAN_OPENJDK_SRC) \
|| ((defined __x86_64__) && (defined __MINGW32__))
#define EXPORT(x) x
#else
#define EXPORT(x) _##x
#endif
typedef int socklen_t;
#define RTLD_DEFAULT 0
#else // not PLATFORM_WINDOWS
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <sched.h>
#include <dlfcn.h>
#define OPEN open
#define CLOSE close
#define READ read
#define WRITE write
#define STAT stat
#define FSTAT fstat
#define LSEEK lseek
#define EXPORT(x) x
#endif // not PLATFORM_WINDOWS
#define JVM_EEXIST -100
using namespace vm;
namespace {
#ifdef _MSC_VER
inline int OPEN(string_t path, int mask, int mode)
{
int fd;
if (_wsopen_s(&fd, path, mask, _SH_DENYNO, mode) == 0) {
return fd;
} else {
return -1;
}
}
#endif
namespace local {
const int JMM_VERSION_1_0 = 0x20010000;
struct jmmOptionalSupport {
unsigned isLowMemoryDetectionSupported : 1;
unsigned isCompilationTimeMonitoringSupported : 1;
unsigned isThreadContentionMonitoringSupported : 1;
unsigned isCurrentThreadCpuTimeSupported : 1;
unsigned isOtherThreadCpuTimeSupported : 1;
unsigned isBootClassPathSupported : 1;
unsigned isObjectMonitorUsageSupported : 1;
unsigned isSynchronizerUsageSupported : 1;
};
typedef unsigned jmmLongAttribute;
typedef unsigned jmmBoolAttribute;
typedef unsigned jmmStatisticType;
typedef unsigned jmmThresholdType;
typedef unsigned jmmVMGlobalType;
typedef unsigned jmmVMGlobalOrigin;
struct jmmVMGlobal {
jstring name;
jvalue value;
jmmVMGlobalType type;
jmmVMGlobalOrigin origin;
unsigned writeable : 1;
unsigned external : 1;
unsigned reserved : 30;
void* reserved1;
void* reserved2;
};
struct jmmExtAttributeInfo {
const char* name;
char type;
const char* description;
};
struct jmmGCStat {
jlong gc_index;
jlong start_time;
jlong end_time;
jobjectArray usage_before_gc;
jobjectArray usage_after_gc;
jint gc_ext_attribute_values_size;
jvalue* gc_ext_attribute_values;
jint num_gc_ext_attributes;
};
struct JmmInterface {
void* reserved1;
void* reserved2;
jint(JNICALL* GetVersion)(JNIEnv*);
jint(JNICALL* GetOptionalSupport)(JNIEnv*, jmmOptionalSupport*);
jobject(JNICALL* GetInputArguments)(JNIEnv*);
jint(JNICALL* GetThreadInfo)(JNIEnv*, jlongArray, jint, jobjectArray);
jobjectArray(JNICALL* GetInputArgumentArray)(JNIEnv*);
jobjectArray(JNICALL* GetMemoryPools)(JNIEnv*, jobject);
jobjectArray(JNICALL* GetMemoryManagers)(JNIEnv*, jobject);
jobject(JNICALL* GetMemoryPoolUsage)(JNIEnv*, jobject);
jobject(JNICALL* GetPeakMemoryPoolUsage)(JNIEnv*, jobject);
void* reserved4;
jobject(JNICALL* GetMemoryUsage)(JNIEnv*, jboolean);
jlong(JNICALL* GetLongAttribute)(JNIEnv*, jobject, jmmLongAttribute);
jboolean(JNICALL* GetBoolAttribute)(JNIEnv*, jmmBoolAttribute);
jboolean(JNICALL* SetBoolAttribute)(JNIEnv*, jmmBoolAttribute, jboolean);
jint(JNICALL* GetLongAttributes)(JNIEnv*,
jobject,
jmmLongAttribute*,
jint,
jlong*);
jobjectArray(JNICALL* FindCircularBlockedThreads)(JNIEnv*);
jlong(JNICALL* GetThreadCpuTime)(JNIEnv*, jlong);
jobjectArray(JNICALL* GetVMGlobalNames)(JNIEnv*);
jint(JNICALL* GetVMGlobals)(JNIEnv*, jobjectArray, jmmVMGlobal*, jint);
jint(JNICALL* GetInternalThreadTimes)(JNIEnv*, jobjectArray, jlongArray);
jboolean(JNICALL* ResetStatistic)(JNIEnv*, jvalue, jmmStatisticType);
void(JNICALL* SetPoolSensor)(JNIEnv*, jobject, jmmThresholdType, jobject);
jlong(JNICALL* SetPoolThreshold)(JNIEnv*, jobject, jmmThresholdType, jlong);
jobject(JNICALL* GetPoolCollectionUsage)(JNIEnv*, jobject);
jint(JNICALL* GetGCExtAttributeInfo)(JNIEnv*,
jobject,
jmmExtAttributeInfo*,
jint);
void(JNICALL* GetLastGCStat)(JNIEnv*, jobject, jmmGCStat*);
jlong(JNICALL* GetThreadCpuTimeWithKind)(JNIEnv*, jlong, jboolean);
void* reserved5;
jint(JNICALL* DumpHeap0)(JNIEnv*, jstring, jboolean);
jobjectArray(JNICALL* FindDeadlocks)(JNIEnv*, jboolean);
void(JNICALL* SetVMGlobal)(JNIEnv*, jstring, jvalue);
void* reserved6;
jobjectArray(JNICALL* DumpThreads)(JNIEnv*, jlongArray, jboolean, jboolean);
};
const unsigned InterfaceVersion = 4;
const unsigned PageSize = 4 * 1024;
#ifdef AVIAN_OPENJDK_SRC
const int VirtualFileBase = 1000000000;
#endif
Machine* globalMachine;
const char* primitiveName(Thread* t, GcClass* c)
{
if (c == primitiveClass(t, 'V')) {
return "void";
} else if (c == primitiveClass(t, 'Z')) {
return "boolean";
} else if (c == primitiveClass(t, 'B')) {
return "byte";
} else if (c == primitiveClass(t, 'C')) {
return "char";
} else if (c == primitiveClass(t, 'S')) {
return "short";
} else if (c == primitiveClass(t, 'I')) {
return "int";
} else if (c == primitiveClass(t, 'F')) {
return "float";
} else if (c == primitiveClass(t, 'J')) {
return "long";
} else if (c == primitiveClass(t, 'D')) {
return "double";
} else {
abort(t);
}
}
GcByteArray* getClassName(Thread* t, GcClass* c)
{
if (c->name() == 0) {
if (c->vmFlags() & PrimitiveFlag) {
PROTECT(t, c);
GcByteArray* name = makeByteArray(t, primitiveName(t, c));
c->setName(t, name);
} else {
abort(t);
}
}
return c->name();
}
GcString* makeClassNameString(Thread* t, GcByteArray* name)
{
THREAD_RUNTIME_ARRAY(t, char, s, name->length());
replace('/',
'.',
RUNTIME_ARRAY_BODY(s),
reinterpret_cast<char*>(name->body().begin()));
return makeString(t, "%s", RUNTIME_ARRAY_BODY(s));
}
object makeJmethod(Thread* t, GcMethod* vmMethod, int index = -1);
object makeJconstructor(Thread* t, GcMethod* vmMethod, int index = -1);
object makeJfield(Thread* t, GcField* vmField, int index = -1);
static uint64_t uncaughtExceptionHandler(Thread*, uintptr_t*);
#ifdef AVIAN_OPENJDK_SRC
void interceptFileOperations(Thread*, bool);
#endif
class MyClasspath : public Classpath {
public:
MyClasspath(System* s,
Allocator* allocator,
const char* javaHome,
const char* embedPrefix)
: allocator(allocator), ranNetOnLoad(0), ranManagementOnLoad(0)
{
class StringBuilder {
public:
StringBuilder(System* s, Allocator* allocator)
: s(s),
allocator(allocator),
bufferSize(1024),
buffer(static_cast<char*>(allocator->allocate(bufferSize))),
offset(0)
{
}
void ensure(unsigned capacity)
{
if (capacity > bufferSize) {
unsigned size = max(bufferSize * 2, capacity);
char* b = static_cast<char*>(allocator->allocate(size));
if (offset) {
memcpy(b, buffer, offset);
}
allocator->free(buffer, bufferSize);
buffer = b;
bufferSize = size;
}
}
void append(const char* append)
{
unsigned length = strlen(append);
ensure(offset + length + 1);
strncpy(buffer + offset, append, length + 1);
offset += length;
}
void append(char c)
{
ensure(2);
buffer[offset] = c;
buffer[offset + 1] = 0;
++offset;
}
System* s;
Allocator* allocator;
unsigned bufferSize;
char* buffer;
unsigned offset;
} sb(s, allocator);
unsigned javaHomeOffset = sb.offset;
sb.append(javaHome);
sb.append('\0');
unsigned classpathOffset = sb.offset;
sb.append(AVIAN_CLASSPATH);
sb.append(s->pathSeparator());
sb.append(javaHome);
sb.append("/lib/rt.jar");
sb.append(s->pathSeparator());
sb.append(javaHome);
sb.append("/lib/jsse.jar");
sb.append(s->pathSeparator());
sb.append(javaHome);
sb.append("/lib/jce.jar");
sb.append(s->pathSeparator());
sb.append(javaHome);
sb.append("/lib/ext/sunjce_provider.jar");
sb.append(s->pathSeparator());
sb.append(javaHome);
sb.append("/lib/resources.jar");
sb.append('\0');
unsigned libraryPathOffset = sb.offset;
sb.append(javaHome);
#ifdef PLATFORM_WINDOWS
#define LIB_DIR "/bin"
#elif defined __APPLE__
#define LIB_DIR "/lib"
#elif defined ARCH_x86_64
#define LIB_DIR "/lib/amd64"
#elif defined ARCH_arm
#define LIB_DIR "/lib/arm"
#else
// todo: handle other architectures
#define LIB_DIR "/lib/i386"
#endif
#ifdef PLATFORM_WINDOWS
sb.append(LIB_DIR);
#else
sb.append(LIB_DIR ":");
sb.append(javaHome);
sb.append(LIB_DIR "/xawt");
#endif
sb.append('\0');
unsigned tzMappingsOffset = sb.offset;
sb.append(javaHome);
sb.append("/lib/tzmappings");
this->tzMappingsLength = sb.offset - tzMappingsOffset;
sb.append('\0');
unsigned embedPrefixOffset = sb.offset;
sb.append(embedPrefix);
this->embedPrefixLength = sb.offset - embedPrefixOffset;
this->javaHome = sb.buffer + javaHomeOffset;
this->classpath = sb.buffer + classpathOffset;
this->libraryPath = sb.buffer + libraryPathOffset;
this->tzMappings = sb.buffer + tzMappingsOffset;
this->embedPrefix = sb.buffer + embedPrefixOffset;
this->buffer = sb.buffer;
this->bufferSize = sb.bufferSize;
}
virtual GcJclass* makeJclass(Thread* t, GcClass* class_)
{
PROTECT(t, class_);
GcString* name = makeClassNameString(t, getClassName(t, class_));
PROTECT(t, name);
GcJclass* c
= reinterpret_cast<GcJclass*>(allocate(t, GcJclass::FixedSize, true));
setObjectClass(t, c, type(t, GcJclass::Type));
c->setName(t, name);
c->setVmClass(t, class_);
#ifdef HAVE_JclassClassLoader
if (class_->loader() != roots(t)->bootLoader()) {
c->setClassLoader(t, class_->loader());
}
#endif
return c;
}
virtual GcString* makeString(Thread* t,
object oarray,
int32_t offset,
int32_t length)
{
if (objectClass(t, oarray) == type(t, GcByteArray::Type)) {
GcByteArray* array = cast<GcByteArray>(t, oarray);
PROTECT(t, array);
GcCharArray* charArray = makeCharArray(t, length);
for (int i = 0; i < length; ++i) {
if (array->body()[offset + i] & 0x80) {
GcMethod* constructor = resolveMethod(t,
type(t, GcString::Type),
"<init>",
"([BIILjava/lang/String;)V");
PROTECT(t, constructor);
GcString* utf8 = vm::makeString(t, "UTF8");
PROTECT(t, utf8);
object s = makeNew(t, type(t, GcString::Type));
PROTECT(t, s);
t->m->processor->invoke(
t, constructor, s, array, offset, length, utf8);
return cast<GcString>(t, s);
}
charArray->body()[i] = array->body()[offset + i];
}
oarray = charArray;
offset = 0;
} else {
expect(t, objectClass(t, oarray) == type(t, GcCharArray::Type));
}
return vm::makeString(t, oarray, offset, length, 0);
}
virtual GcThread* makeThread(Thread* t, Thread* parent)
{
const unsigned MaxPriority = 10;
const unsigned NormalPriority = 5;
GcThreadGroup* group;
if (parent) {
group = parent->javaThread->group();
} else {
group = reinterpret_cast<GcThreadGroup*>(
allocate(t, GcThreadGroup::FixedSize, true));
setObjectClass(t, group, type(t, GcThreadGroup::Type));
group->maxPriority() = MaxPriority;
}
PROTECT(t, group);
GcThread* thread
= reinterpret_cast<GcThread*>(allocate(t, GcThread::FixedSize, true));
setObjectClass(t, thread, type(t, GcThread::Type));
thread->priority() = NormalPriority;
thread->setGroup(t, group);
thread->setContextClassLoader(t, roots(t)->appLoader());
PROTECT(t, thread);
GcJobject* blockerLock = makeJobject(t);
thread->setBlockerLock(t, blockerLock);
#if HAVE_ThreadName_Ljava_lang_String_
GcString* name = vm::makeString(t, "Thread-%p", thread);
#else
const unsigned BufferSize = 256;
char buffer[BufferSize];
unsigned length = vm::snprintf(buffer, BufferSize, "Thread-%p", thread);
GcCharArray* name = makeCharArray(t, length);
for (unsigned i = 0; i < length; ++i) {
name->body()[i] = buffer[i];
}
#endif
thread->setName(t, name);
return thread;
}
virtual object makeJMethod(Thread* t, GcMethod* vmMethod)
{
PROTECT(t, vmMethod);
return vmMethod->name()->body()[0] == '<' ? makeJconstructor(t, vmMethod)
: makeJmethod(t, vmMethod);
}
virtual GcMethod* getVMMethod(Thread* t, object jmethod)
{
return cast<GcMethod>(
t,
objectClass(t, jmethod) == type(t, GcJmethod::Type)
? cast<GcArray>(t,
cast<GcJmethod>(t, jmethod)
->clazz()
->vmClass()
->methodTable())
->body()[cast<GcJmethod>(t, jmethod)->slot()]
: cast<GcArray>(t,
cast<GcJconstructor>(t, jmethod)
->clazz()
->vmClass()
->methodTable())
->body()[cast<GcJconstructor>(t, jmethod)->slot()]);
}
virtual object makeJField(Thread* t, GcField* vmField)
{
return makeJfield(t, vmField);
}
virtual GcField* getVMField(Thread* t, GcJfield* jfield)
{
return cast<GcField>(
t,
cast<GcArray>(t, jfield->clazz()->vmClass()->fieldTable())
->body()[jfield->slot()]);
}
virtual void clearInterrupted(Thread* t)
{
vm::clearInterrupted(t);
}
virtual void runThread(Thread* t)
{
// force monitor creation so we don't get an OutOfMemory error
// later when we try to acquire it:
objectMonitor(t, t->javaThread, true);
THREAD_RESOURCE0(t, {
GcThrowable* e = t->exception;
if (e != NULL) {
t->exception = NULL;
// Prevent any exceptions thrown from the uncaught
// exception handler from unwinding the stack past
// this point. This allows us to continue cleaning
// up this resource.
uintptr_t argument = reinterpret_cast<uintptr_t>(e);
runRaw(t, uncaughtExceptionHandler, &argument);
}
vm::acquire(t, t->javaThread);
t->clearFlag(Thread::ActiveFlag);
vm::notifyAll(t, t->javaThread);
vm::release(t, t->javaThread);
t->m->processor->invoke(t,
cast<GcMethod>(t, roots(t)->threadTerminated()),
t->javaThread->group(),
t->javaThread);
});
GcMethod* method = resolveMethod(
t, roots(t)->bootLoader(), "java/lang/Thread", "run", "()V");
t->m->processor->invoke(t, method, t->javaThread);
}
virtual void resolveNative(Thread* t, GcMethod* method)
{
if (strcmp(reinterpret_cast<const int8_t*>("sun/font/SunFontManager"),
method->class_()->name()->body().begin()) == 0
and strcmp(reinterpret_cast<const int8_t*>("initIDs"),
method->name()->body().begin()) == 0
and strcmp(reinterpret_cast<const int8_t*>("()V"),
method->spec()->body().begin()) == 0) {
PROTECT(t, method);
expect(t, loadLibrary(t, libraryPath, "fontmanager", true, true));
}
vm::resolveNative(t, method);
}
virtual void interceptMethods(Thread* t UNUSED)
{
#ifdef AVIAN_OPENJDK_SRC
interceptFileOperations(t, false);
#endif
}
virtual void preBoot(Thread*)
{
// ignore
}
virtual bool mayInitClasses()
{
return true;
}
virtual void boot(Thread* t)
{
globalMachine = t->m;
resolveSystemClass(
t, roots(t)->bootLoader(), type(t, GcClassLoader::Type)->name());
GcMethod* method = resolveMethod(t,
roots(t)->bootLoader(),
"java/lang/ThreadGroup",
"threadTerminated",
"(Ljava/lang/Thread;)V");
// sequence point, for gc (don't recombine statements)
roots(t)->setThreadTerminated(t, method);
#ifdef AVIAN_OPENJDK_SRC
interceptFileOperations(t, true);
#else // not AVIAN_OPENJDK_SRC
# ifdef PLATFORM_WINDOWS
expect(t, loadLibrary(t, libraryPath, "msvcr100", true, true));
# endif
// necessary for using OpenJDK builds from
// https://github.com/ojdkbuild/ojdkbuild:
loadLibrary(t, libraryPath, "ojdkbuild_zlib", true, true, false);
expect(t, loadLibrary(t, libraryPath, "verify", true, true));
expect(t, loadLibrary(t, libraryPath, "java", true, true));
#endif // not AVIAN_OPENJDK_SRC
{
GcField* assertionLock = resolveField(t,
type(t, GcClassLoader::Type),
"assertionLock",
"Ljava/lang/Object;");
setField(t,
roots(t)->bootLoader(),
assertionLock->offset(),
roots(t)->bootLoader());
}
{
GcClass* class_ = resolveClass(t,
roots(t)->bootLoader(),
"java/util/Properties",
true,
GcNoClassDefFoundError::Type);
PROTECT(t, class_);
object instance = makeNew(t, class_);
PROTECT(t, instance);
GcMethod* constructor = resolveMethod(t, class_, "<init>", "()V");
t->m->processor->invoke(t, constructor, instance);
t->m->processor->invoke(t,
roots(t)->bootLoader(),
"java/lang/System",
"setProperties",
"(Ljava/util/Properties;)V",
0,
instance);
}
{
GcMethod* constructor = resolveMethod(t,
type(t, GcClassLoader::Type),
"<init>",
"(Ljava/lang/ClassLoader;)V");
PROTECT(t, constructor);
t->m->processor->invoke(t, constructor, roots(t)->bootLoader(), 0);
t->m->processor->invoke(
t, constructor, roots(t)->appLoader(), roots(t)->bootLoader());
}
{
GcField* scl = resolveField(
t, type(t, GcClassLoader::Type), "scl", "Ljava/lang/ClassLoader;");
PROTECT(t, scl);
GcField* sclSet
= resolveField(t, type(t, GcClassLoader::Type), "sclSet", "Z");
setField(t,
type(t, GcClassLoader::Type)->staticTable(),
scl->offset(),
roots(t)->appLoader());
fieldAtOffset<uint8_t>(type(t, GcClassLoader::Type)->staticTable(),
sclSet->offset()) = true;
}
t->m->processor->invoke(t,
roots(t)->bootLoader(),
"java/lang/System",
"initializeSystemClass",
"()V",
0);
t->m->processor->invoke(t,
roots(t)->bootLoader(),
"sun/misc/Launcher",
"getLauncher",
"()Lsun/misc/Launcher;",
0);
t->javaThread->setContextClassLoader(t, roots(t)->appLoader());
}
virtual const char* bootClasspath()
{
return classpath;
}
virtual object makeDirectByteBuffer(Thread* t, void* p, jlong capacity)
{
GcClass* c
= resolveClass(t, roots(t)->bootLoader(), "java/nio/DirectByteBuffer");
PROTECT(t, c);
object instance = makeNew(t, c);
PROTECT(t, instance);
GcMethod* constructor = resolveMethod(t, c, "<init>", "(JI)V");
t->m->processor->invoke(t,
constructor,
instance,
reinterpret_cast<int64_t>(p),
static_cast<int32_t>(capacity));
return instance;
}
virtual void* getDirectBufferAddress(Thread* t, object b)
{
PROTECT(t, b);
GcField* field = resolveField(t, objectClass(t, b), "address", "J");
return reinterpret_cast<void*>(fieldAtOffset<int64_t>(b, field->offset()));
}
virtual int64_t getDirectBufferCapacity(Thread* t, object b)
{
PROTECT(t, b);
GcField* field = resolveField(t, objectClass(t, b), "capacity", "I");
return fieldAtOffset<int32_t>(b, field->offset());
}
virtual bool canTailCall(Thread* t UNUSED,
GcMethod*,
GcByteArray* calleeClassName,
GcByteArray* calleeMethodName,
GcByteArray*)
{
// we can't tail call System.loadLibrary or Runtime.loadLibrary
// due to their use of System.getCallerClass, which gets confused
// if we elide stack frames.
return (strcmp("loadLibrary",
reinterpret_cast<char*>(calleeMethodName->body().begin()))
or (strcmp("java/lang/System",
reinterpret_cast<char*>(calleeClassName->body().begin()))
and strcmp("java/lang/Runtime",
reinterpret_cast<char*>(
calleeClassName->body().begin()))))
// and we can't tail call Reflection.getCallerClass because the
// number of stack frames will be wrong
and (strcmp(
"getCallerClass",
reinterpret_cast<char*>(calleeMethodName->body().begin()))
or strcmp("sun/reflect/Reflection",
reinterpret_cast<char*>(
calleeClassName->body().begin())));
}
virtual GcClassLoader* libraryClassLoader(Thread* t, GcMethod* caller)
{
#ifdef AVIAN_OPENJDK_SRC
return (caller->class_() == type(t, GcClassLoader::Type)
and t->libraryLoadStack)
? t->libraryLoadStack->classLoader
#else
return strcmp("java/lang/ClassLoader$NativeLibrary",
reinterpret_cast<char*>(
caller->class_()->name()->body().begin())) == 0
? cast<GcClass>(
t,
cast<GcJclass>(t,
t->m->processor->invoke(
t,
resolveMethod(t,
caller->class_(),
"getFromClass",
"()Ljava/lang/Class;"),
0))->vmClass())->loader()
#endif
: caller->class_()->loader();
}
virtual void shutDown(Thread* t)
{
GcClass* c
= resolveClass(t, roots(t)->bootLoader(), "java/lang/Shutdown", false);
if (c) {
GcMethod* m = findMethodOrNull(t, c, "shutdown", "()V");
if (m) {
t->m->processor->invoke(t, m, 0);
}
}
}
virtual void dispose()
{
allocator->free(buffer, bufferSize);
allocator->free(this, sizeof(*this));
}
Allocator* allocator;
const char* javaHome;
const char* classpath;
const char* libraryPath;
const char* tzMappings;
const char* embedPrefix;
char* buffer;
unsigned bufferSize;
unsigned tzMappingsLength;
unsigned embedPrefixLength;
unsigned filePathField;
unsigned fileDescriptorFdField;
unsigned fileInputStreamFdField;
unsigned zipFileJzfileField;
unsigned zipEntryNameField;
unsigned zipEntryTimeField;
unsigned zipEntryCrcField;
unsigned zipEntrySizeField;
unsigned zipEntryCsizeField;
unsigned zipEntryMethodField;
bool ranNetOnLoad;
bool ranManagementOnLoad;
JmmInterface jmmInterface;
};
struct JVM_ExceptionTableEntryType {
jint start_pc;
jint end_pc;
jint handler_pc;
jint catchType;
};
struct jvm_version_info {
unsigned jvm_version;
unsigned update_version : 8;
unsigned special_update_version : 8;
unsigned reserved1 : 16;
unsigned reserved2;
unsigned is_attach_supported : 1;
unsigned is_kernel_jvm : 1;
unsigned : 30;
unsigned : 32;
unsigned : 32;
};
bool pathEqual(const char* a, const char* b, unsigned length)
{
#ifdef PLATFORM_WINDOWS
return strncasecmp(a, b, length) == 0;
#else
return strncmp(a, b, length) == 0;
#endif
}
class EmbeddedFile {
public:
EmbeddedFile(MyClasspath* cp, const char* path, unsigned pathLength)
{
if (pathEqual(cp->embedPrefix, path, cp->embedPrefixLength)) {
const char* p = path + cp->embedPrefixLength;
while (*p == '/')
++p;
this->jar = p;
if (*p == 0) {
this->jarLength = 0;
this->path = 0;
this->pathLength = 0;
return;
}
while (*p and *p != '/')
++p;
this->jarLength = p - this->jar;
while (*p == '/')
++p;
this->path = p;
this->pathLength = pathLength - (p - path);
} else {
this->jar = 0;
this->jarLength = 0;
this->path = 0;
this->pathLength = 0;
}
}
const char* jar;
const char* path;
unsigned jarLength;
unsigned pathLength;
};
static uint64_t uncaughtExceptionHandler(Thread* t, uintptr_t* arguments)
{
GcThrowable* exception = cast<GcThrowable>(t, reinterpret_cast<object>(arguments[0]));
PROTECT(t, exception);
GcMethod* dispatch = resolveMethod(t,
roots(t)->bootLoader(),
"java/lang/Thread",
"dispatchUncaughtException",
"(Ljava/lang/Throwable;)V");
if (dispatch != NULL) {
t->m->processor->invoke(t, dispatch, t->javaThread, exception);
}
return 0;
}