forked from derfsss/java-os4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamvm-amiga-openjdk.patch
More file actions
3691 lines (3478 loc) · 140 KB
/
Copy pathjamvm-amiga-openjdk.patch
File metadata and controls
3691 lines (3478 loc) · 140 KB
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
diff --git a/src/alloc.c b/src/alloc.c
index 536c720..eb601f0 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -191,6 +191,12 @@ static VMLock registered_refs_lock;
static VMWaitLock run_finaliser_lock;
static VMWaitLock reference_lock;
+/* Cooperative VM-thread shutdown (AmigaOS/clib4 can't async-stop threads via
+ signals). Set TRUE by terminateVMThreads(); the finalizer / reference-handler
+ loops check it after each condvar wait and exit, so clib4 exit() -- which waits
+ for all threads -- can complete instead of hanging on the never-ending daemons. */
+volatile int vm_exiting = FALSE;
+
/* A pointer to the finalizer thread. */
static Thread *finalizer_thread;
@@ -286,9 +292,40 @@ void clearMarkBits() {
memset(markbits, 0, markbit_size * sizeof(*markbits));
}
+#ifdef __amigaos4__
+#include <proto/exec.h>
+#endif
+
int initialiseAlloc(InitArgs *args) {
char *mem = (char*)mmap(0, args->max_heap, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANON, -1, 0);
+
+#ifdef __amigaos4__
+ /* The whole max_heap is mapped here, once, and the heap only ever grows
+ INSIDE it -- so max_heap is really "how big a single MAP_ANON can we
+ get". clib4 refuses large ones, and the VM used to just abort, which is
+ why nativePhysicalMemory() reported a fake 64MB and left every AmigaOS
+ user with a 16MB heap that could not grow.
+
+ Halve and retry instead. The default can then be sized from real memory
+ and simply settles on whatever this machine can actually map, rather than
+ everyone paying for the worst case. Stop at MIN_MIN_HEAP: below that the
+ VM cannot run anyway, and the original error is the right one to report. */
+ while(mem == MAP_FAILED && args->max_heap > MIN_MIN_HEAP) {
+ args->max_heap /= 2;
+
+ if(args->min_heap > args->max_heap)
+ args->min_heap = args->max_heap;
+
+ mem = (char*)mmap(0, args->max_heap, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE|MAP_ANON, -1, 0);
+ if(mem != MAP_FAILED)
+ jam_fprintf(stderr, "Note: could not map the default heap; "
+ "settled on %ldMB (-Xmx overrides)\n",
+ (long)(args->max_heap / MB));
+ }
+#endif
+
if(mem == MAP_FAILED) {
perror("Couldn't allocate the heap; try reducing the max "
"heap size (-Xmx)");
@@ -309,6 +346,24 @@ int initialiseAlloc(InitArgs *args) {
freelist->next = NULL;
TRACE_GC("Alloced heap size %p\n", heaplimit-heapbase);
+
+#ifdef __amigaos4__
+ /*
+ * The heap's address range, on the serial line, once.
+ *
+ * A DSI exception gives a faulting address and nothing else, and the first
+ * question about it is always the same: is that address a Java object at
+ * all? Answering it has twice meant reasoning backwards from a register
+ * dump. With this line the answer is a comparison -- inside [heapbase,
+ * heapmax) means the object is real and something about its LAYOUT is
+ * wrong; outside means the pointer never belonged to the heap and the
+ * fault is a stale or corrupted reference. Those two lead opposite ways.
+ */
+ DebugPrintF("[HEAP] base=%p limit=%p max=%p (map %p..%p, %ldMB)\n",
+ heapbase, heaplimit, heapmax, mem, mem + args->max_heap,
+ (long)(args->max_heap / MB));
+#endif
+
allocMarkBits();
/* Initialise GC locks */
@@ -1788,6 +1843,13 @@ void asyncGCThreadLoop(Thread *self) {
for(;;) { \
waitVMWaitLock(list##_lock, self); \
\
+ /* VM is shutting down: exit the loop so the thread terminates \
+ (cooperative shutdown -- see vm_exiting / terminateVMThreads). */ \
+ if(vm_exiting) { \
+ unlockVMWaitLock(list##_lock, self); \
+ break; \
+ } \
+ \
if((list##_start == list##_size) && (list##_end == 0)) \
continue; \
\
@@ -1846,6 +1908,28 @@ void referenceHandlerThreadLoop(Thread *self) {
"<GC: enqueuing %d references>\n", self, &self);
}
+/* Cooperative VM-thread shutdown. Signal the finalizer and reference-handler
+ daemon threads to exit their loops (they wait on a condvar, so a normal notify
+ wakes them -- no signals needed), letting clib4 exit() complete. Called from
+ the exit hook before the process terminates. */
+void terminateVMThreads() {
+ Thread *self = threadSelf();
+
+ vm_exiting = TRUE;
+
+ /* If main is blocked in mainThreadWaitToExitVM(), wake it so it can
+ observe vm_exiting and take the process down from the main thread. */
+ requestExitVMWaitBreak();
+
+ lockVMWaitLock(run_finaliser_lock, self);
+ notifyAllVMWaitLock(run_finaliser_lock, self);
+ unlockVMWaitLock(run_finaliser_lock, self);
+
+ lockVMWaitLock(reference_lock, self);
+ notifyAllVMWaitLock(reference_lock, self);
+ unlockVMWaitLock(reference_lock, self);
+}
+
int initialiseGC(InitArgs *args) {
/* Pre-allocate an OutOfMemoryError exception object - we throw it
* when we're really low on heap space, and can create FA... */
@@ -2023,7 +2107,8 @@ void *gcMalloc(int len) {
state = throw_oom;
unlockVMLock(heap_lock, self);
- signalException(java_lang_OutOfMemoryError, NULL);
+ /* the genuinely-out-of-heap case; say which one this is */
+ signalException(java_lang_OutOfMemoryError, "Java heap space");
return NULL;
break;
@@ -2075,8 +2160,14 @@ got_it:
Thread *self; \
disableSuspend(self = threadSelf()); \
lockVMLock(has_fnlzr_lock, self); \
- TRACE_FNLZ(("Object @%p type %s has a finalize method...\n", \
- ob, CLASS_CB(ob->class)->name)); \
+ /* C99 variadic macro, so NO outer parentheses: with them the whole thing
+ is one comma expression whose value is the last operand, and this
+ degrades to jam_printf(CLASS_CB(ob->class)->name) -- the class name used
+ as the format string, printed bare with no newline. That is what
+ flooded the serial line with "sun/java2d/SunGraphics2D" on every draw,
+ and it would read junk varargs the day a name contained a '%'. */ \
+ TRACE_FNLZ("Object @%p type %s has a finalize method...\n", \
+ ob, CLASS_CB(ob->class)->name); \
if(has_finaliser_count == has_finaliser_size) { \
has_finaliser_size += LIST_INCREMENT; \
has_finaliser_list = sysRealloc(has_finaliser_list, \
@@ -2116,9 +2207,17 @@ Object *allocObject(Class *class) {
Object *allocArray(Class *class, int size, int el_size) {
Object *ob;
- /* Special check to protect against integer overflow */
+ /* Special check to protect against integer overflow. Say so: this is NOT
+ heap exhaustion -- no GC has even been attempted -- but a request for an
+ impossible array length, and a bare OutOfMemoryError with no message is
+ indistinguishable from the real thing. That cost a long hunt once, when
+ a bad `new Node[n]` length out of ConcurrentHashMap's constructor looked
+ exactly like a heap too small to hold java.util.Locale. */
if(size > (INT_MAX - sizeof(uintptr_t) - sizeof(Object)) / el_size) {
- signalException(java_lang_OutOfMemoryError, NULL);
+ char buff[64];
+
+ snprintf(buff, sizeof(buff), "array length %d too large", size);
+ signalException(java_lang_OutOfMemoryError, buff);
return NULL;
}
@@ -2309,7 +2408,32 @@ void *gcMemRealloc(void *addr, int size) {
uintptr_t old_size = *--mem;
uintptr_t new_size = size + sizeof(uintptr_t);
+ /*
+ * Grow in place only when the block demonstrably already holds the new
+ * size.
+ *
+ * The test below this one is "do both sizes land in the same page?",
+ * which is sound only where mmap rounds an allocation UP to a whole
+ * page -- as Linux does -- so that a block of 100 bytes really owns
+ * 4096 and can be relabelled as 4000 for free.
+ *
+ * clib4's mmap does not round. It allocates MMAP_PAGE_SIZE + len and
+ * spends that extra page on alignment and its own header, so a 100-byte
+ * request owns 100 usable bytes plus whatever alignment slack happens
+ * to be left. Relabelling it 4000 hands the caller a pointer to memory
+ * it does not own, and the fault comes later, from whoever reads the
+ * far end -- which is why this presented as an intermittent DSI inside
+ * findHashedUtf8 during class loading, at an entry index near the top
+ * of a hash table that resizeHash had just "grown".
+ *
+ * Shrinking is always safe: the block is at least old_size already.
+ * Growing takes the allocate-copy-free path below, every time.
+ */
+#ifdef __amigaos4__
+ if(new_size <= old_size) {
+#else
if(old_size/sys_page_size == new_size/sys_page_size) {
+#endif
*mem = new_size;
return addr;
} else {
diff --git a/src/class.c b/src/class.c
index af81530..0523294 100644
--- a/src/class.c
+++ b/src/class.c
@@ -260,6 +260,27 @@ Class *parseClass(char *classname, char *data, int offset, int len,
READ_U2(minor_version, ptr, len);
READ_U2(major_version, ptr, len);
+ /* Java 8 (class-file major 52) is the newest bytecode this VM's class
+ library supports. Reject anything newer up front with a clear
+ UnsupportedClassVersionError -- otherwise a Java 9+ class loads here and
+ dies far away with a mysterious BootstrapMethodError the first time it
+ executes an indy string-concat (java.lang.invoke.StringConcatFactory is
+ absent from the Java 8 rt.jar), which is exactly the kind of failure
+ that reads as "it just freezes". (minor_version only flags preview
+ features -- ignore it.) */
+ if(major_version > 52) {
+ char msg[256];
+ snprintf(msg, sizeof(msg),
+ "%s has been compiled by a more recent version of the Java "
+ "Runtime (class file version %d.0); this Java 8 runtime only "
+ "recognizes class file versions up to 52.0 -- recompile with "
+ "javac --release 8", classname ? classname : "A class",
+ major_version);
+ signalChainedExceptionName("java/lang/UnsupportedClassVersionError",
+ msg, NULL);
+ return NULL;
+ }
+
if((class = allocClass()) == NULL)
return NULL;
@@ -1480,6 +1501,10 @@ unlock:
objectUnlock(class);
}
+#ifdef __amigaos4__
+#include <proto/exec.h>
+#endif
+
Class *initClass(Class *class) {
ClassBlock *cb = CLASS_CB(class);
ConstantPool *cp = &cb->constant_pool;
@@ -1549,8 +1574,51 @@ Class *initClass(Class *class) {
if((excep = exceptionOccurred())) {
Class *error;
+#ifdef __amigaos4__
+ {
+ /* detailMessage/vmState are inherited from java.lang.Throwable, so use
+ lookupField (searches superclasses), NOT findField (own fields only). */
+ FieldBlock *msgfb = lookupField(excep->class, newUtf8("detailMessage"),
+ newUtf8("Ljava/lang/String;"));
+ Object *msg = msgfb ? INST_DATA(excep, Object*, msgfb->u.offset)
+ : NULL;
+ if(msg != NULL) {
+ char *c = String2Cstr(msg);
+ DebugPrintF("[CLINIT] %s <clinit> threw %s: %s\n",
+ CLASS_CB(class)->name,
+ CLASS_CB(excep->class)->name, c);
+ sysFree(c);
+ } else
+ DebugPrintF("[CLINIT] %s <clinit> threw %s (no message)\n",
+ CLASS_CB(class)->name, CLASS_CB(excep->class)->name);
- clearException();
+ /* Dump the stack trace: Throwable.vmState (VMThrowable) -> backtrace,
+ an int[] of (MethodBlock*, pc) pairs (see classlib .../excep.c). */
+ {
+ FieldBlock *vmsfb = lookupField(excep->class, newUtf8("vmState"),
+ newUtf8("Ljava/lang/VMThrowable;"));
+ Object *vmt = vmsfb ? INST_DATA(excep, Object*, vmsfb->u.offset)
+ : NULL;
+ FieldBlock *btfb = vmt ? findField(vmt->class, SYMBOL(backtrace),
+ newUtf8("[I")) : NULL;
+ Object *bt = btfb ? INST_DATA(vmt, Object*, btfb->u.offset) : NULL;
+ if(bt != NULL) {
+ uintptr_t *d = ARRAY_DATA(bt, uintptr_t);
+ int i, len = ARRAY_LEN(bt);
+ for(i = 0; i < len && i < 24; i += 2) {
+ MethodBlock *m = (MethodBlock*)d[i];
+ if(m == NULL) break;
+ DebugPrintF("[CLINIT] at %s.%s\n",
+ CLASS_CB(m->class)->name, m->name);
+ }
+ } else
+ DebugPrintF("[CLINIT] (no backtrace: vmt=%p btfb=%p)\n",
+ vmt, btfb);
+ }
+ }
+#endif
+
+ clearException();
/* Don't wrap exceptions of type java.lang.Error... */
error = findSystemClass0(SYMBOL(java_lang_Error));
@@ -1592,6 +1660,10 @@ char *findFileEntry(char *path, int *file_len) {
char *data;
FILE *fd;
+#ifdef __amigaos4__
+ path = (char*)amigaPath(path);
+#endif
+
if((fd = fopen(path, "r")) == NULL)
return NULL;
@@ -2135,21 +2207,38 @@ void freeClassLoaderData(Object *class_loader) {
}
}
+#ifdef __amigaos4__
+#include <proto/exec.h>
+
+/* ':' is the AmigaDOS volume separator as well as the path-list separator
+ everyone types, so deciding where one entry ends is not a character test --
+ see amigaIsPathListSep() in os/amiga/classpath.c. */
+#define IS_PATH_LIST_SEP(list, start, i, seen_vol) \
+ amigaIsPathListSep(list, start, i, seen_vol)
+#else
+#define IS_PATH_LIST_SEP(list, start, i, seen_vol) ((list)[i] == ':')
+#endif
+
void parseBootClassPath() {
char *cp, *pntr, *start;
int i, j, len, max = 0;
+ int seen_vol = FALSE;
struct stat info;
cp = sysMalloc(strlen(bootpath)+1);
strcpy(cp, bootpath);
+#ifdef __amigaos4__
+ DebugPrintF("[BCP] bootpath='%s'\n", bootpath);
+#endif
for(i = 0, start = pntr = cp; *pntr; pntr++) {
- if(*pntr == ':') {
+ if(IS_PATH_LIST_SEP(cp, start - cp, pntr - cp, &seen_vol)) {
if(start != pntr) {
*pntr = '\0';
i++;
}
start = pntr+1;
+ seen_vol = FALSE;
}
}
if(start != pntr)
@@ -2158,32 +2247,113 @@ void parseBootClassPath() {
bootclasspath = sysMalloc(sizeof(BCPEntry)*i);
for(j = 0, pntr = cp; i > 0; i--) {
+#ifdef __amigaos4__
+ while(*pntr == ':' || *pntr == ';')
+#else
while(*pntr == ':')
+#endif
pntr++;
start = pntr;
pntr += (len = strlen(pntr))+1;
- if(stat(start, &info) == 0) {
+ {
+#ifdef __amigaos4__
+ int _sr = stat(amigaPath(start), &info);
+ DebugPrintF("[BCP] entry='%s' stat=%d\n", start, _sr);
+#else
+ int _sr = stat(start, &info);
+#endif
+ if(_sr == 0) {
if(S_ISDIR(info.st_mode)) {
bootclasspath[j].zip = NULL;
if(len > max)
max = len;
} else
- if((bootclasspath[j].zip = processArchive(start)) == NULL)
+ if((bootclasspath[j].zip = processArchive(start)) == NULL) {
+#ifdef __amigaos4__
+ DebugPrintF("[BCP] processArchive FAILED '%s'\n", start);
+#endif
continue;
+ }
bootclasspath[j++].path = start;
}
+ }
}
max_cp_element_len = max;
bcp_entries = j;
}
+#ifdef __amigaos4__
+/* Build the Java-facing value of the "sun.boot.class.path" property from the
+ C-level boot path. Two things have to change on the way out:
+
+ - the separator. path.separator is ';' on AmigaOS (':' is the volume
+ separator), and sun.misc.Launcher.getBootstrapClassPath() splits this
+ property on File.pathSeparator. Hand it a ':'-joined list and Java reads
+ the whole thing as ONE entry whose leading "niopatch.zip:" AmigaDOS takes
+ for a missing volume -- the "Please insert volume niopatch.zip" requester,
+ after which every getBootstrapResource() fails. (That is what used to
+ break sun.reflect.misc.MethodUtil: unable to read Trampoline.class through
+ the boot URLClassPath, loadClass fell through to the bootstrap loader,
+ which defined it with the null loader, and Trampoline's initialiser threw
+ "must not be defined by the bootstrap class loader".)
+
+ - the leading '/'. java.io.File and sun.nio.fs only treat a path as
+ absolute when it starts with one; amigaPath() strips it again at the
+ native chokepoints.
+
+ Entries arrive already java.home-absolute (classlibDefaultBootClassPath), so
+ this only re-spells them. Separate string: it does NOT touch the global
+ bootpath the VM's own class search uses. */
+char *amigaBootClassPathProperty(void) {
+ char *bcp = getBootClassPath();
+ char *out;
+ int n, len = 0, start, seen_vol = FALSE, i;
+
+ if(bcp == NULL)
+ return NULL;
+
+ n = strlen(bcp);
+ out = sysMalloc(n * 2 + 2); /* worst case: a '/' added per entry */
+
+ for(start = 0, i = 0; i <= n; i++) {
+ if(i != n && !amigaIsPathListSep(bcp, start, i, &seen_vol))
+ continue;
+
+ if(i != start) {
+ if(len != 0)
+ out[len++] = ';';
+ if(bcp[start] != '/')
+ out[len++] = '/';
+ memcpy(out + len, &bcp[start], i - start);
+ len += i - start;
+ }
+
+ start = i + 1;
+ seen_vol = FALSE;
+ }
+
+ out[len] = '\0';
+ return out;
+}
+#endif
+
void setClassPath(InitArgs *args) {
char *env;
- classpath = args->classpath ? args->classpath :
+#ifdef __amigaos4__
+ /* Rewrite the -cp: ':' or ';' separated, wildcards expanded, entries made
+ launch-anchored absolute Unix paths -- see os/amiga/classpath.c. Empty
+ when none given (on AmigaOS "." canonicalises to a volume path the
+ URLClassPath mis-handles). */
+ char *raw = args->classpath ? args->classpath :
+ ((env = getenv("CLASSPATH")) ? env : NULL);
+ classpath = raw != NULL ? amigaClassPath(raw) : "";
+#else
+ classpath = args->classpath ? args->classpath :
((env = getenv("CLASSPATH")) ? env : ".");
+#endif
}
char *getClassPath() {
diff --git a/src/classlib/gnuclasspath/natives.c b/src/classlib/gnuclasspath/natives.c
index ca95e00..f99f293 100644
--- a/src/classlib/gnuclasspath/natives.c
+++ b/src/classlib/gnuclasspath/natives.c
@@ -39,6 +39,10 @@
#include "reflect.h"
#include "gnuclasspath.h"
+#ifdef __amigaos4__
+#include <proto/exec.h>
+#endif
+
static int pd_offset;
int classlibInitialiseNatives() {
diff --git a/src/classlib/openjdk/class.c b/src/classlib/openjdk/class.c
index 3206ed0..c22e5f7 100644
--- a/src/classlib/openjdk/class.c
+++ b/src/classlib/openjdk/class.c
@@ -117,6 +117,60 @@ Object *classlibSkipReflectionLoader(Object *loader) {
return loader;
}
+#ifdef __amigaos4__
+/* Amiga runtime layout is FLAT (jars beside the VM) and JamVM's boot-classpath
+ parser splits on ':' which collides with "Volume:" -- so the default is a
+ RELATIVE flat list (no volume colons), resolved against the CWD, which the
+ standard launch sets to the install dir. niopatch.zip FIRST: it prepends the
+ patched sun.nio.fs.DefaultFileSystemProvider needed for -classpath. */
+/* The Amiga runtime layout is FLAT -- the boot jars sit beside the VM, not in a
+ Unix <java.home>/lib tree. Entries are java.home-ABSOLUTE and ';'-separated:
+ they used to be bare relative names resolved against the current directory,
+ which forced the VM to chdir into its own install drawer at startup, and that
+ in turn made every relative path an application handed to a native call
+ resolve in the WRONG place -- e.g. new ZipFile("Invoicex.jar") looked in JAVA:
+ instead of the drawer the user ran java from, because java.util.zip passes
+ File.getPath() (not getAbsolutePath()) straight to open(). With the boot path
+ absolute the chdir is gone and the process cwd stays where the user is.
+
+ ';' rather than ':' as the separator: parseBootClassPath() resolves ':'
+ volume-aware (amigaIsPathListSep), so ':' would work too, but the entries here
+ all carry a volume and ';' leaves nothing to infer. This is the C-level path,
+ in plain AmigaDOS form; amigaBootClassPathProperty() derives the Java-facing
+ sun.boot.class.path from it. */
+char *classlibDefaultBootClassPath() {
+ static char *entries[] = {"niopatch.zip",
+ "resources.jar",
+ "rt.jar",
+ "sunrsasign.jar",
+ "jsse.jar",
+ "jce.jar",
+ "charsets.jar",
+ "amigatoolkit.zip",
+ "classes",
+ NULL};
+ char *java_home = getJavaHome();
+ const char *sep;
+ char *path, *pntr;
+ int i, len = 0;
+
+ if(java_home == NULL)
+ java_home = "";
+ /* "Volume:" IS the directory -- a '/' after it means its PARENT */
+ sep = (*java_home != '\0' && java_home[strlen(java_home) - 1] != ':') ? "/" : "";
+
+ for(i = 0; entries[i] != NULL; i++)
+ len += strlen(java_home) + strlen(sep) + strlen(entries[i]) + 1;
+
+ pntr = path = sysMalloc(len + 1);
+
+ for(i = 0; entries[i] != NULL; i++)
+ pntr += sprintf(pntr, "%s%s%s%s", i != 0 ? ";" : "",
+ java_home, sep, entries[i]);
+
+ return path;
+}
+#else
char *classlibDefaultBootClassPath() {
static char *entries[] = {"lib/resources.jar",
"lib/rt.jar",
@@ -130,6 +184,10 @@ char *classlibDefaultBootClassPath() {
char *path, *pntr;
int i, j, len = 0;
+ /* Pre-init (java -version), getJavaHome() is NULL -- avoid sprintf("%s",NULL). */
+ if(java_home == NULL)
+ return NULL;
+
for(i = 0; entries[i] != NULL; i++)
len += strlen(entries[i]);
@@ -142,19 +200,58 @@ char *classlibDefaultBootClassPath() {
pntr += sprintf(pntr, "%s/%s:", java_home, entries[j]);
sprintf(pntr, "%s/%s", java_home, entries[j]);
-
+
return path;
}
+#endif
+
+#ifdef __amigaos4__
+/* "/<java.home>/<tail>": java.home is an AmigaDOS path ("SYS:Java") and the
+ leading '/' is Java's absolute marker, stripped again by amigaPath() at the
+ native chokepoints. "Volume:" IS the directory, so no '/' follows a trailing
+ ':'. These lists used to be relative (resolved against user.dir = the install
+ drawer); now that user.dir is the directory the user ran `java` from they must
+ be absolute. The embedded ':' is safe: path.separator is ';' on AmigaOS. */
+char *amigaJavaHomePath(const char *tail) {
+ char *home = getJavaHome();
+ int hlen, tlen = strlen(tail);
+ char *out;
+
+ if(home == NULL)
+ home = "";
+ hlen = strlen(home);
+
+ out = sysMalloc(hlen + tlen + 3);
+ out[0] = '/';
+ memcpy(out + 1, home, hlen);
+ hlen++;
+
+ if(tlen != 0 && hlen > 1 && out[hlen - 1] != ':')
+ out[hlen++] = '/';
+ strcpy(out + hlen, tail);
+
+ return out;
+}
+char *classlibDefaultExtDirs() {
+ return amigaJavaHomePath("lib/ext");
+}
+#else
char *classlibDefaultExtDirs() {
char *java_home = getJavaHome();
- char *ext_dirs = sysMalloc(strlen(java_home) +
+ char *ext_dirs = sysMalloc(strlen(java_home) +
sizeof("/lib/ext:/usr/java/packages/lib/ext"));
return strcat(strcpy(ext_dirs, java_home),
"/lib/ext:/usr/java/packages/lib/ext");
}
+#endif
+#ifdef __amigaos4__
+char *classlibDefaultEndorsedDirs() {
+ return amigaJavaHomePath("lib/endorsed");
+}
+#else
char *classlibDefaultEndorsedDirs() {
char *java_home = getJavaHome();
char *endorsed_dirs = sysMalloc(strlen(java_home) +
@@ -162,6 +259,7 @@ char *classlibDefaultEndorsedDirs() {
return strcat(strcpy(endorsed_dirs, java_home), "/lib/endorsed");
}
+#endif
char *classlibExternalClassName(Class *class) {
ClassBlock *cb = CLASS_CB(class);
diff --git a/src/classlib/openjdk/classlib-defs.h b/src/classlib/openjdk/classlib-defs.h
index 5433ff3..a8f7048 100644
--- a/src/classlib/openjdk/classlib-defs.h
+++ b/src/classlib/openjdk/classlib-defs.h
@@ -42,7 +42,20 @@
Object *class_loader; \
char pad2[3*sizeof(Object*)+1*sizeof(int)];
#elif OPENJDK_VERSION == 8
-#define CLASSLIB_CLASS_PAD char pad[11*sizeof(Object*)+1*sizeof(int)];
+/* Temurin/late-8u backported the VM-injected java.lang.Class.classLoader
+ field (getClassLoader0() reads the FIELD; no longer a native). Shadow it
+ in the ClassBlock padding at its Java-field offset, like the OpenJDK 9
+ support above. Temurin 8 Class field order: cachedConstructor,
+ newInstanceCallerCache, name, classLoader, reflectionData,
+ classRedefinedCount(int), genericInfo, enumConstants,
+ enumConstantDirectory, annotationData, annotationType, classValueMap
+ (11 refs + 1 int = the original pad size). JamVM lays the reference
+ fields out in REVERSE declaration order from the object base (verified
+ empirically: classLoader = object byte-offset 36 = ClassBlock word 7). */
+#define CLASSLIB_CLASS_PAD \
+ char pad1[7*sizeof(Object*)]; \
+ Object *class_loader; \
+ char pad2[3*sizeof(Object*)+1*sizeof(int)];
#elif OPENJDK_VERSION == 7
#define CLASSLIB_CLASS_PAD char pad[18*sizeof(Object*)+2*sizeof(int)];
#else
@@ -61,15 +74,15 @@
#define CLASSLIB_ARRAY_CLASS_EXTRA_FIELDS
#define CLASSLIB_CLASSBLOCK_ARRAY_REFS_DO(action, cb, ...)
#else
+/* NOTE: for OpenJDK 8 (Temurin) class_loader lives in the CLASS_PAD
+ shadow above; only the openjdk-8 build consumes this branch. */
#define CLASSLIB_CLASS_EXTRA_FIELDS \
Object *protection_domain; \
- Object *class_loader; \
Object *host_class; \
Object *signers;
#define CLASSLIB_CLASSBLOCK_REFS_DO(action, cb, ...) \
action(cb, protection_domain, ## __VA_ARGS__); \
- action(cb, class_loader, ## __VA_ARGS__); \
action(cb, host_class, ## __VA_ARGS__); \
action(cb, signers, ## __VA_ARGS__)
diff --git a/src/classlib/openjdk/classlib.h b/src/classlib/openjdk/classlib.h
index 054afe4..6d0f58c 100644
--- a/src/classlib/openjdk/classlib.h
+++ b/src/classlib/openjdk/classlib.h
@@ -35,7 +35,12 @@ extern Object *classlibMarkThreadTerminated(Object *jThread);
#define classlibThreadIdName() SYMBOL(tid)
#define classlibAddThreadName() SYMBOL(add)
+#if OPENJDK_VERSION >= 8
+/* OpenJDK 8 changed java.lang.Thread.name from char[] to String. */
+#define classlibThreadNameType() SYMBOL(sig_java_lang_String)
+#else
#define classlibThreadNameType() SYMBOL(array_C)
+#endif
#define classlibRemoveThreadName() SYMBOL(removeThreadName)
#define classlibExceptionHandlerName() SYMBOL(uncaughtExceptionHandler)
@@ -46,6 +51,7 @@ extern void classlibThreadName2Buff(Object *jThread, char *buffer,
extern int classlibInitialiseSignals();
extern void classlibSignalThread(Thread *self);
+extern void classlibSignalThreadExit(void);
/* Class */
@@ -109,8 +115,28 @@ extern Object *classlibCheckIfOnLoad(Frame *last);
/* Properties */
+#ifdef __amigaos4__
+/* sun.nio.fs.chdirAllowed=true makes UnixFileSystem's constructor skip the
+ UnixNativeDispatcher.getcwd() native, so the (niopatch.zip-supplied) default
+ NIO.2 provider constructs without libnio -- required for URLClassLoader /
+ -classpath. A command-line -D still overrides (added after defaults). */
+#define classlibAddDefaultProperties(properties) \
+ do { \
+ setProperty(properties, "sun.nio.fs.chdirAllowed", "true"); \
+ setProperty(properties, "awt.toolkit", \
+ "sun.awt.amiga.AmigaToolkit"); \
+ setProperty(properties, "java.awt.graphicsenv", \
+ "sun.awt.amiga.AmigaGraphicsEnvironment"); \
+ setProperty(properties, "sun.java2d.renderer", \
+ "sun.java2d.marlin.MarlinRenderingEngine"); \
+ setProperty(properties, "awt.useSystemAAFontSettings", "on"); \
+ setProperty(properties, "swing.aatext", "true"); \
+ setProperty(properties, "sun.java2d.pmoffscreen", "false"); \
+ } while(0)
+#else
#define classlibAddDefaultProperties(properties) \
/* NOTHING TO DO */
+#endif
extern char *classlibDefaultJavaHome();
diff --git a/src/classlib/openjdk/dll.c b/src/classlib/openjdk/dll.c
index 1d9cc9c..c05537e 100644
--- a/src/classlib/openjdk/dll.c
+++ b/src/classlib/openjdk/dll.c
@@ -41,12 +41,35 @@ int classlibInitialiseDll() {
return TRUE;
}
+#ifdef __amigaos4__
+/* The native .so files live beside the VM (flat layout), so the boot library
+ path is just java.home, absolute: "/SYS:Java". It used to be "." -- resolved
+ by java.lang.ClassLoader against user.dir, which had to be forced to the
+ install drawer for it to work. user.dir is now the directory the user ran
+ `java` from (so an application's own relative paths behave), hence the boot
+ library path has to stand on its own. The embedded volume ':' is safe:
+ path.separator is ';' on AmigaOS, and amigaPath() strips the leading '/'
+ before dlopen. */
+char *classlibDefaultBootDllPath() {
+ extern char *amigaJavaHomePath(const char *tail);
+
+ return amigaJavaHomePath("");
+}
+#else
char *classlibDefaultBootDllPath() {
char *java_home = getJavaHome();
- char *dll_path = sysMalloc(strlen(java_home) + sizeof("/lib/"OS_ARCH));
+ char *dll_path;
+
+ /* java.home is only set during initVM; showVersionAndCopyright() (java
+ -version) calls this pre-init, when getJavaHome() returns NULL. Return
+ NULL rather than strlen(NULL)-crashing -- the caller handles it. */
+ if(java_home == NULL)
+ return NULL;
+ dll_path = sysMalloc(strlen(java_home) + sizeof("/lib/"OS_ARCH));
return strcat(strcpy(dll_path, java_home), "/lib/"OS_ARCH);
}
+#endif
void *classlibLookupLoadedDlls(char *name, Object *loader) {
Object *name_string;
diff --git a/src/classlib/openjdk/jvm.c b/src/classlib/openjdk/jvm.c
index 971ac75..63a5373 100644
--- a/src/classlib/openjdk/jvm.c
+++ b/src/classlib/openjdk/jvm.c
@@ -1525,6 +1525,75 @@ jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2) {
}
+/* JVM_CopySwapMemory -- backs java.nio.Bits.copySwapMemory0, which the
+ ByteBuffer bulk get/put of short/int/long arrays uses whenever the buffer's
+ byte order differs from the platform's. JamVM never defined it, so libjava.so
+ carried an unresolved reference; the AmigaOS ELF loader binds lazily, which
+ means that did not break the build or startup -- it would have killed the
+ process the first time such a buffer was used.
+
+ Object+offset pairs are resolved exactly as Unsafe does here (base pointer
+ plus raw byte offset, base NULL for an off-heap address) -- see
+ copyMemoryOpenJDK7() in natives.c. Elements are staged through a local buffer
+ so an in-place swap (src == dst) is safe, and overlapping regions are walked
+ in the direction that memmove would. */
+
+static void swapElement(char *dst, const char *src, int size) {
+ char buff[8];
+ int i;
+
+ for(i = 0; i < size; i++)
+ buff[i] = src[size - 1 - i];
+
+ memcpy(dst, buff, size);
+}
+
+void JVM_CopySwapMemory(JNIEnv *env, jobject srcObj, jlong srcOffset,
+ jobject dstObj, jlong dstOffset, jlong size,
+ jlong elemSize) {
+
+ char *src = (char *)srcObj + srcOffset;
+ char *dst = (char *)dstObj + dstOffset;
+ jlong i;
+
+ TRACE("JVM_CopySwapMemory(size=%lld, elemSize=%lld)", size, elemSize);
+
+ if(elemSize != 2 && elemSize != 4 && elemSize != 8) {
+ signalException(java_lang_IllegalArgumentException,
+ "wrong element size");
+ return;
+ }
+
+ if(size < 0 || (size % elemSize) != 0) {
+ signalException(java_lang_IllegalArgumentException,
+ "size must be a multiple of the element size");
+ return;
+ }
+
+ if(dst > src && dst < src + size)
+ for(i = size - elemSize; i >= 0; i -= elemSize)
+ swapElement(dst + i, src + i, (int)elemSize);
+ else
+ for(i = 0; i < size; i += elemSize)
+ swapElement(dst + i, src + i, (int)elemSize);
+}
+
+
+/* JVM_GetTemporaryDirectory -- sun.misc.VMSupport.getVMTemporaryDirectory().
+ HotSpot returns the OS temp directory, not the java.io.tmpdir property; keep
+ the two in step (see addDefaultProperties() in properties.c). */
+
+jstring JVM_GetTemporaryDirectory(JNIEnv *env) {
+ TRACE("JVM_GetTemporaryDirectory(env=%p)", env);
+
+#ifdef __amigaos4__
+ return (jstring)createString("/T:");
+#else
+ return (jstring)createString("/tmp");
+#endif
+}
+
+
/* JVM_Open */
#define JVM_EEXIST -100
@@ -1533,6 +1602,12 @@ jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2) {
jint JVM_Open(const char *fname, jint flags, jint mode) {
int result;
+#ifdef __amigaos4__
+ /* ZipFile/JarFile and friends open through JVM_Open, so this is the central
+ Java-form -> AmigaDOS-form conversion for the VM's own file access. */
+ fname = amigaPath(fname);
+#endif
+
TRACE("JVM_Open(fname=%s, flags=%d, mode=%d)", fname, flags, mode);
/* O_DELETE is a special classlib flag which indicates that
@@ -3034,7 +3109,28 @@ jint JVM_FindSignal(const char *name) {
TRACE("JVM_FindSignal(name=%s)", name);
- for(i = 0; signals[i].name != NULL && strcmp(signals[i].name, name); i++);
+ for(i = 0; signals[i].name != NULL && strcmp(signals[i].name, name); i++);
return signals[i].number;
}
+
+/* Class-Data-Sharing (CDS) shared class-path lookup cache. JamVM has no CDS, so
+ report "no cache": getLookupCacheURLs returning NULL makes sun.misc.URLClassPath
+ disable the cache cleanly (URLClassPath.java:322->331). These JVM_* were missing
+ from the classlib -- libjava.so's sun/misc/URLClassPath.c calls them, so the
+ unresolved symbols crashed sun.misc.Launcher init on AmigaOS. */
+jobjectArray JVM_GetResourceLookupCacheURLs(JNIEnv *env, jobject loader) {
+ TRACE("JVM_GetResourceLookupCacheURLs(loader=%p)", loader);
+ return NULL;
+}
+
+jintArray JVM_GetResourceLookupCache(JNIEnv *env, jobject loader,
+ const char *resource_name) {
+ TRACE("JVM_GetResourceLookupCache(loader=%p, name=%s)", loader, resource_name);
+ return NULL;
+}
+
+jboolean JVM_KnownToNotExist(JNIEnv *env, jobject loader, const char *classname) {
+ TRACE("JVM_KnownToNotExist(loader=%p, name=%s)", loader, classname);
+ return JNI_FALSE;
+}
diff --git a/src/classlib/openjdk/management.c b/src/classlib/openjdk/management.c
index 822f41e..b24c039 100644
--- a/src/classlib/openjdk/management.c
+++ b/src/classlib/openjdk/management.c
@@ -54,9 +54,50 @@ jobject jmm_GetInputArguments(JNIEnv *env) {
return NULL;
}
+/*
+ * The arguments the VM was started with -- what RuntimeMXBean.getInputArguments()
+ * reports. sun.management.VMManagementImpl.getVmArguments0() calls straight
+ * through to here.
+ *
+ * This used to be UNIMPLEMENTED, which returned NULL after printing its own name
+ * on stdout. Neither half was harmless: RuntimeImpl wraps the result in a List
+ * without checking, so a caller got a null it could not tell from "started with
+ * no arguments", and the trace landed in the middle of the application's own
+ * output. It only became reachable when libmanagement.so started being built --
+ * before that, getRuntimeMXBean() died earlier, with UnsatisfiedLinkError.
+ *
+ * An EMPTY array when nothing was recorded, never NULL: a VM created through
+ * JNI_CreateJavaVM does not go through parseCommandLine, and "no arguments" is
+ * the truthful answer there rather than a failure.
+ */
jobjectArray jmm_GetInputArgumentArray(JNIEnv *env) {
- UNIMPLEMENTED("jmm_GetInputArgumentArray");
- return NULL;
+ extern char **vm_input_args;
+ extern int vm_input_args_count;
+
+ Class *array_class = findArrayClass(SYMBOL(array_java_lang_String));
+ int count = vm_input_args == NULL ? 0 : vm_input_args_count;
+ Object *array;
+ int i;
+
+ if(array_class == NULL)
+ return NULL;
+
+ array = allocArray(array_class, count, sizeof(Object*));
+ if(array == NULL)
+ return NULL;
+
+ for(i = 0; i < count; i++) {
+ Object *str = createString(vm_input_args[i]);
+
+ if(str == NULL)
+ return NULL;
+
+ /* The body is addressed through `array` on each pass rather than cached
+ in a local, since createString allocates in between. */
+ ARRAY_DATA(array, Object*)[i] = str;
+ }
+
+ return array;
}
jobjectArray jmm_GetMemoryPools(JNIEnv *env, jobject obj) {
diff --git a/src/classlib/openjdk/properties.c b/src/classlib/openjdk/properties.c
index 2391e61..fc8877e 100644
--- a/src/classlib/openjdk/properties.c
+++ b/src/classlib/openjdk/properties.c
@@ -21,6 +21,44 @@
#include <string.h>
#include "jam.h"
+#ifdef __amigaos4__
+/* On AmigaOS the runtime layout is FLAT -- libjvm.so, libjava.so and the boot
+ jars all live in one directory (e.g. SYS:Test/), not the Unix
+ <java.home>/lib/<arch>/<vmtype>/libjvm.so tree. So java.home is simply the
+ directory containing libjvm.so (strip the trailing filename after the last
+ '/' or ':' volume separator). Boot class/library paths are supplied
+ explicitly via -Xbootclasspath and -Dsun.boot.library.path, so they don't
+ depend on the (Unix-shaped) java.home/lib/... derivation. */
+char *classlibDefaultJavaHome() {
+ /* java.home = the VM's program directory (PROGDIR:, e.g. "SYS:Java"), where
+ libjvm.so + the boot jars + lib/ all live (flat layout). Resolve it from
+ PROGDIR: so it is independent of the current dir (the launcher no longer
+ CDs there). Fall back to deriving it from libjvm.so's own path. */
+ extern char *amigaProgramDir(void);
+ char *pd = amigaProgramDir();
+ char *path, *pntr, *home;
+
+ if(pd != NULL)
+ return strcpy(sysMalloc(strlen(pd) + 1), pd);
+
+ path = nativeJVMPath();
+ if(path == NULL)
+ return NULL;
+
+ pntr = path + strlen(path);
+ while(pntr > path && pntr[-1] != '/' && pntr[-1] != ':')
+ pntr--;
+ if(pntr > path && pntr[-1] == '/') /* keep volume ':' but drop dir '/' */
+ pntr--;
+
+ home = sysMalloc(pntr - path + 1);
+ memcpy(home, path, pntr - path);
+ home[pntr - path] = '\0';
+ sysFree(path);
+
+ return home;
+}
+#else
char *classlibDefaultJavaHome() {
char *path = nativeJVMPath();
char *pntr = path + strlen(path);
@@ -42,4 +80,5 @@ char *classlibDefaultJavaHome() {