-
Notifications
You must be signed in to change notification settings - Fork 748
/
Copy pathClasspathItem.cpp
476 lines (407 loc) · 12 KB
/
ClasspathItem.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
/*******************************************************************************
* Copyright IBM Corp. and others 2001
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "ClasspathItem.hpp"
#include "sharedconsts.h"
#include "ut_j9shr.h"
#include "j9shrnls.h"
#include <stdlib.h>
#include <string.h>
#define CLASSPATHITEM_ERR_TRACE(var) j9nls_printf(PORTLIB, J9NLS_ERROR, var);
#define HEADER_SIZE sizeof(ClasspathItem)
#define CPEI_ARRAY_PTR_FROM_CPI(cpiPtr) (IDATA*)((BlockPtr)cpiPtr + HEADER_SIZE)
#define UPDATE_CPEI_ARRAY_PTR_FROM_CPI(cpiPtr) (IDATA*)(cpiPtr += HEADER_SIZE)
ClasspathEntryItem*
ClasspathEntryItem::newInstance(const char* path, U_16 pathLen, UDATA protocol, ClasspathEntryItem* memForConstructor)
{
ClasspathEntryItem* newCPEI = (ClasspathEntryItem*)memForConstructor;
new(newCPEI) ClasspathEntryItem();
if (newCPEI->initialize(path, pathLen, protocol)==0) {
return newCPEI;
} else {
return NULL;
}
}
IDATA
ClasspathEntryItem::initialize(const char* path_, U_16 pathLen_, UDATA protocol_)
{
flags = 0;
protocol = protocol_;
/* important to initialize timestamp to -1 */
timestamp = -1;
hashValue = 0;
path = (char*)path_;
pathLen = pathLen_;
locationPathLen = pathLen_;
if (protocol == PROTO_JAR) {
if (NULL != path) {
char* jarPath = strstr(path,"!/");
if (NULL == jarPath) {
jarPath = strstr(path,"!\\");
}
if (NULL != jarPath) {
locationPathLen = jarPath - path;
}
}
}
return 0;
}
/* Assume we have enough space */
ClasspathEntryItem::BlockPtr
ClasspathEntryItem::writeToAddress(BlockPtr block)
{
ClasspathEntryItem* cpeiInCache = (ClasspathEntryItem*)block;
IDATA paddedPathSize = SHC_PAD(pathLen, SHC_WORDALIGN);
BlockPtr pathInCache = block + sizeof(ClasspathEntryItem);
memcpy(block, this, sizeof(ClasspathEntryItem));
strncpy(pathInCache, path, pathLen);
/* Mark cache entry so that we know how to read it */
cpeiInCache->flags |= IS_IN_CACHE_FLAG;
return (pathInCache + paddedPathSize);
}
/* If this is view on cache, return correct ptr. Otherwise, return known ptr. */
const char*
ClasspathEntryItem::getPath(U_16* pathLen_) const
{
if (pathLen_) {
*pathLen_ = (U_16)pathLen;
}
if ((flags & IS_IN_CACHE_FLAG)==0) {
return path;
} else {
return (((BlockPtr)this) + sizeof(ClasspathEntryItem));
}
}
U_32
ClasspathEntryItem::getSizeNeeded(void) const
{
return (sizeof(ClasspathEntryItem) + SHC_PAD((U_32)pathLen, SHC_WORDALIGN));
}
UDATA
ClasspathEntryItem::hash(J9InternalVMFunctions* functionTable)
{
U_16 pathLen = 0;
const char* path = getPath(&pathLen);
if (!hashValue) {
return (hashValue = (functionTable->computeHashForUTF8((U_8*)path, pathLen) + protocol));
}
return hashValue;
}
const char*
ClasspathEntryItem::getLocation(U_16* locationPathLen_) const
{
if (locationPathLen_) {
*locationPathLen_ = (U_16)locationPathLen;
}
if ((flags & IS_IN_CACHE_FLAG)==0) {
return path;
} else {
return (((BlockPtr)this) + sizeof(ClasspathEntryItem));
}
}
void
ClasspathEntryItem::cleanup(void)
{
/* Currently no cleanup */
}
ClasspathItem*
ClasspathItem::newInstance(J9JavaVM* vm, IDATA entries_, IDATA helperID_, U_16 cpType_, ClasspathItem* memForConstructor)
{
ClasspathItem* newCPI = (ClasspathItem*)memForConstructor;
new(newCPI) ClasspathItem();
newCPI->initialize(vm, entries_, helperID_, cpType_, ((BlockPtr)memForConstructor + sizeof(ClasspathItem)));
return newCPI;
}
UDATA
ClasspathItem::getRequiredConstrBytes(UDATA entries)
{
UDATA reqBytes = 0;
reqBytes += sizeof(ClasspathItem);
reqBytes += (entries * (sizeof(ClasspathEntryItem) + sizeof(ClasspathEntryItem*)));
return reqBytes;
}
void
ClasspathItem::initialize(J9JavaVM* vm, IDATA entries_, IDATA helperID_, U_16 cpType_, BlockPtr memForConstructor) {
type = cpType_;
flags = 0;
entries = entries_;
portlib = vm->portLibrary;
helperID = helperID_;
itemsAdded = 0;
firstDirIndex = -1;
hashValue = 0;
jarsLockedToIndex = -1;
Trc_SHR_CPI_initialize_Entry(helperID_, entries_, cpType_);
items = (ClasspathEntryItem**)memForConstructor;
for (IDATA i=0; i<entries_; i++) {
items[i] = (ClasspathEntryItem*)(((UDATA)items) + ((entries_ * sizeof(ClasspathEntryItem*))
+ (sizeof(ClasspathEntryItem) * i)));
}
Trc_SHR_CPI_initialize_Exit();
}
/* Returns -1 if addItem fails otherwise total number of items added */
IDATA
ClasspathItem::addItem(J9InternalVMFunctions* functionTable, const char* path, U_16 pathLen, UDATA protocol)
{
ClasspathEntryItem* current = NULL;
Trc_SHR_CPI_addItem_Entry(pathLen, path, protocol);
if (entries==itemsAdded) {
/* Cannot access verbose level, so this is not suppressed by "silent". However, it's a "should never happen" message. */
PORT_ACCESS_FROM_PORT(portlib);
CLASSPATHITEM_ERR_TRACE(J9NLS_SHRC_CPI_TOO_MANY_ITEMS);
Trc_SHR_CPI_addItem_ExitTooMany();
Trc_SHR_Assert_ShouldNeverHappen();
return -1;
}
current = ClasspathEntryItem::newInstance(path, pathLen, protocol, items[itemsAdded]);
if (!current) {
Trc_SHR_CPI_addItem_ExitError();
return -1;
}
if (protocol==PROTO_DIR && firstDirIndex==-1) {
firstDirIndex = itemsAdded;
}
hashValue += current->hash(functionTable);
++itemsAdded;
Trc_SHR_CPI_addItem_Exit(itemsAdded);
return itemsAdded;
}
/* Either knows directly about its items, or calculates using offsets in the cache */
ClasspathEntryItem*
ClasspathItem::itemAt(I_16 i) const
{
Trc_SHR_CPI_itemAt_Entry(i);
if (i>=itemsAdded) {
Trc_SHR_CPI_itemAt_ExitError(itemsAdded);
Trc_SHR_Assert_ShouldNeverHappen();
return NULL;
}
if (!(flags & IS_IN_CACHE_FLAG)) {
if (!items) {
Trc_SHR_CPI_itemAt_NotInitialized();
return NULL; /* Not initialized yet */
} else {
Trc_SHR_CPI_itemAt_ExitLocal();
return items[i];
}
} else {
IDATA* offsets = CPEI_ARRAY_PTR_FROM_CPI(this);
Trc_SHR_CPI_itemAt_ExitInCache();
return (ClasspathEntryItem*)((BlockPtr)this + offsets[i]);
}
}
I_16
ClasspathItem::find(J9InternalVMFunctions* functionTable, ClasspathEntryItem* test) const
{
return find(functionTable, test, -1);
}
/* Does not compare timestamps or flags */
bool /* static */
ClasspathItem::compare(J9InternalVMFunctions* functionTable, ClasspathEntryItem* test, ClasspathEntryItem* compareTo)
{
U_16 testPathLen = 0;
U_16 comparePathLen = 0;
U_8* testPath = NULL;
U_8* comparePath = NULL;
UDATA hash1, hash2;
Trc_SHR_CPI_compare_CPEI_Entry(test, compareTo);
if (test==compareTo) {
Trc_SHR_CPI_compare_CPEI_ExitSame();
return true;
}
if (test==NULL || compareTo==NULL) {
Trc_SHR_CPI_compare_CPEI_ExitNull();
Trc_SHR_Assert_ShouldNeverHappen();
return false;
}
testPath = (U_8*)test->getPath(&testPathLen);
comparePath = (U_8*)compareTo->getPath(&comparePathLen);
Trc_SHR_CPI_compare_CPEI_Paths(testPathLen, testPath, comparePathLen, comparePath);
hash1 = test->hash(functionTable);
hash2 = compareTo->hash(functionTable);
if (hash1 != hash2) {
Trc_SHR_CPI_compare_CPEI_ExitHash(hash1, hash2);
return false;
}
if (test->protocol != compareTo->protocol) {
Trc_SHR_CPI_compare_CPEI_ExitProtocol(test->protocol, compareTo->protocol);
return false;
}
if (!J9UTF8_DATA_EQUALS(testPath, testPathLen, comparePath, comparePathLen)) {
Trc_SHR_CPI_compare_CPEI_ExitCompare();
return false;
}
Trc_SHR_CPI_compare_CPEI_ExitSuccess();
return true;
}
/* NOTE: Should not compare getType(). A 1-element URL should be the same as a 1-element Classpath. */
bool /* static */
ClasspathItem::compare(J9InternalVMFunctions* functionTable, ClasspathItem* test, ClasspathItem* compareTo)
{
Trc_SHR_CPI_compare_Entry(test, compareTo);
if (test==compareTo) {
Trc_SHR_CPI_compare_ExitTrue1();
return true;
}
if (test==NULL || compareTo==NULL) {
Trc_SHR_CPI_compare_ExitNullError();
Trc_SHR_Assert_ShouldNeverHappen();
return false;
}
if (test->getItemsAdded()!=compareTo->getItemsAdded()) {
Trc_SHR_CPI_compare_ExitFalse1();
return false;
}
if (test->getHashCode()!=compareTo->getHashCode()) {
Trc_SHR_CPI_compare_ExitFalse2();
return false;
}
for (I_16 i=0; i<(I_16)test->itemsAdded; i++) {
if (!compare(functionTable, test->itemAt(i), compareTo->itemAt(i))) {
Trc_SHR_CPI_compare_ExitFalse4(i);
return false;
}
}
Trc_SHR_CPI_compare_ExitTrue2();
return true;
}
/* PERFORMANCE: Since most classpath matching will be done comparing the same classpath,
* the quickest patch to compare is to work right to left, only starting at the index
* we're interested in.
*/
I_16
ClasspathItem::find(J9InternalVMFunctions* functionTable, ClasspathEntryItem* test, I_16 stopAtIndex) const
{
I_16 countFrom = 0;
Trc_SHR_CPI_find_Entry(test, stopAtIndex);
/* stopAtIndex should hopefully never be greater than itemsAdded! */
if ((stopAtIndex==-1) || (stopAtIndex >= (I_16)itemsAdded)) {
countFrom = (I_16)itemsAdded - 1;
} else {
countFrom = stopAtIndex;
}
for (I_16 i=countFrom; i>=0; i--) {
if (compare(functionTable, itemAt(i), test)) {
Trc_SHR_CPI_find_ExitFound(i);
return i;
}
}
Trc_SHR_CPI_find_ExitNotFound();
return -1;
}
IDATA
ClasspathItem::getMaxItems(void) const
{
return entries;
}
I_16
ClasspathItem::getItemsAdded(void) const
{
return (I_16)itemsAdded;
}
/* Assume we have enough memory at block to write whole item */
IDATA
ClasspathItem::writeToAddress(BlockPtr block)
{
BlockPtr cursor = block;
IDATA* arrayPtr = NULL; /* Array of offsets */
ClasspathItem* cpiInCache = (ClasspathItem*)block;
Trc_SHR_CPI_writeToAddress_Entry(block);
memcpy(cpiInCache, this, sizeof(ClasspathItem));
arrayPtr = UPDATE_CPEI_ARRAY_PTR_FROM_CPI(cursor);
cursor += itemsAdded * (sizeof(IDATA));
for (I_16 i=0; i<(I_16)itemsAdded; i++) {
/* store the offset */
*(arrayPtr++) = (cursor - block);
cursor = itemAt(i)->writeToAddress(cursor);
}
/* Indicate that this is stored in cache */
cpiInCache->flags |= IS_IN_CACHE_FLAG;
Trc_SHR_CPI_writeToAddress_Exit();
return 0;
}
/* Returns amount of size needed in cache to write out complete ClasspathItem */
U_32
ClasspathItem::getSizeNeeded(void) const
{
U_32 total = HEADER_SIZE + ((U_32)itemsAdded*sizeof(IDATA));
for (I_16 i=0; i<(I_16)itemsAdded; i++) {
total += itemAt(i)->getSizeNeeded();
}
return total;
}
/* Only valid/interesting for ClasspathItems created locally (not in cache) */
IDATA
ClasspathItem::getHelperID(void) const
{
if (!(flags & IS_IN_CACHE_FLAG)) {
return helperID;
} else {
return HELPERID_NOT_APPLICABLE;
}
}
IDATA
ClasspathItem::getFirstDirIndex(void) const
{
return firstDirIndex;
}
UDATA
ClasspathItem::getHashCode(void) const
{
return hashValue;
}
UDATA
ClasspathItem::getType(void) const
{
return type;
}
bool
ClasspathItem::isInCache(void) const
{
return ((flags & IS_IN_CACHE_FLAG) != 0);
}
void
ClasspathItem::setJarsLockedToIndex(I_16 i)
{
if (!(flags & IS_IN_CACHE_FLAG)) {
jarsLockedToIndex = i;
}
}
I_16
ClasspathItem::getJarsLockedToIndex(void) const
{
if (!(flags & IS_IN_CACHE_FLAG)) {
return (I_16)jarsLockedToIndex;
} else {
return -1;
}
}
void
ClasspathItem::cleanup(void)
{
if (!(flags & IS_IN_CACHE_FLAG)) {
if (items) {
for (int i=0; i<itemsAdded; i++) {
items[i]->cleanup();
}
}
}
}