-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathj9ddr.c
581 lines (458 loc) · 15.3 KB
/
j9ddr.c
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
/*******************************************************************************
* Copyright (c) 1991, 2017 IBM Corp. and others
*
* 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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include <string.h>
#include "j9protos.h"
#include "j9port.h"
#include "j9ddr.h"
#include "ddrtable.h"
extern U_8 sizeofBool(void);
typedef struct {
U_32 x1 : 1;
U_32 x2 : 15;
U_32 x3 : 6;
U_32 x4 : 10;
U_32 x5 : 7;
U_32 x6 : 9;
U_32 x7 : 4;
U_32 x8 : 12;
} bitField;
#define DEBUG
#define J9DDR_CORE_FILE_DATA_VERSION 1
typedef struct {
U_32 version;
U_8 sizeofBool;
U_8 sizeofUDATA;
U_8 bitfieldFormat;
U_8 unused;
U_32 structDataSize;
U_32 stringDataSize;
U_32 structureCount;
} J9DDRCoreFileData;
typedef struct {
OMRPortLibrary * portLibrary;
J9DDRCoreFileData * coreFileData;
const J9DDRStructDefinition ** structTables;
UDATA totalSize;
J9HashTable * stringTable;
void * cursor;
char * filename;
} J9DDRTranslationData;
typedef struct {
const char * cString;
U_32 offset;
} J9DDRStringTableEntry;
typedef struct {
U_32 nameOffset;
U_32 superOffset;
U_32 structSize;
U_32 fieldCount;
U_32 constantCount;
} J9DDRCoreFileStruct;
typedef struct {
U_32 nameOffset;
U_32 typeOffset;
U_32 offset;
} J9DDRCoreFileField;
typedef struct {
U_32 nameOffset;
U_32 value[2];
} J9DDRCoreFileConstant;
static jint initializeBitfieldEncoding PROTOTYPE((J9DDRTranslationData * data));
static jint processDDRStructTableList PROTOTYPE((J9DDRTranslationData * data));
static jint writeDataFile PROTOTYPE((J9DDRTranslationData * data));
static jint processDDRStructTable PROTOTYPE((J9DDRTranslationData * data, const J9DDRStructDefinition * ddrStruct));
static U_32 stringTableOffset PROTOTYPE((J9DDRTranslationData * data, const char * cString));
static void copyStringTable PROTOTYPE((J9DDRTranslationData * data));
static jint processDDRField PROTOTYPE((J9DDRTranslationData * data, const J9DDRFieldDeclaration * ddrField, U_32 * size));
static UDATA stringTableHash PROTOTYPE((void *key, void *userData));
static UDATA stringTableEquals PROTOTYPE((void *leftKey, void *rightKey, void *userData));
static jint processDDRConstant PROTOTYPE((J9DDRTranslationData * data, const J9DDRConstantDeclaration * ddrConstant, U_32 * size));
static jint createDDRCoreData PROTOTYPE((J9DDRTranslationData * data));
/* Try to initialize the NLS catalog; otherwise use defaults */
static void
ddr_setNLSCatalog(OMRPortLibrary * portLib, char **argv)
{
char *exename = NULL;
OMRPORT_ACCESS_FROM_OMRPORT(portLib);
/* Look for java.home in the executable's directory */
if ( omrsysinfo_get_executable_name( argv[0], &exename ) == 0 ) {
/* find the last separator and add the "java" string */
omrnls_set_catalog( (const char**)&exename, 1, "java", "properties" );
}
/* Do /not/ delete executable name string; system-owned. */
}
static jint
createDDRCoreData(J9DDRTranslationData * data)
{
OMRPORT_ACCESS_FROM_OMRPORT(data->portLibrary);
J9HashTable * stringTable = NULL;
jint rc = JNI_OK;
J9DDRCoreFileData pass1Header;
J9DDRCoreFileData * coreFileData;
omrtty_printf("Encoding struct data\n");
/* Create an empty string table */
stringTable = hashTableNew(OMRPORTLIB, J9_GET_CALLSITE(), 0, sizeof(J9DDRStringTableEntry), 0, 0, OMRMEM_CATEGORY_VM, stringTableHash, stringTableEquals, NULL, NULL);
if (stringTable == NULL) {
omrtty_printf("ERROR: Unable to allocate string hash table\n");
rc = JNI_ENOMEM;
goto done;
}
/* Run the conversion once without writing to compute the size */
data->stringTable = stringTable;
data->coreFileData = &pass1Header;
memset(&pass1Header, 0, sizeof(pass1Header));
rc = processDDRStructTableList(data);
if (rc != JNI_OK) {
omrtty_printf("ERROR: Pass 1 encoding failed\n");
data->coreFileData = NULL;
rc = JNI_ENOMEM;
goto done;
}
/* Allocate memory for the header, struct data and string table and copy in the header */
data->totalSize = sizeof(pass1Header) + pass1Header.structDataSize + pass1Header.stringDataSize;
coreFileData = omrmem_allocate_memory(data->totalSize, OMRMEM_CATEGORY_VM);
if (coreFileData == NULL) {
omrtty_printf("ERROR: Unable to allocate temp space of size %d for encoded data\n", data->totalSize);
rc = JNI_ENOMEM;
goto done;
}
data->coreFileData = coreFileData;
*coreFileData = pass1Header;
/* Run the conversion again, writing the struct data to the allocated memory */
memset(coreFileData, 0, data->totalSize);
coreFileData->stringDataSize = pass1Header.stringDataSize;
data->cursor = coreFileData + 1;
rc = processDDRStructTableList(data);
if (rc != JNI_OK) {
omrtty_printf("ERROR: Pass 2 encoding failed\n");
rc = JNI_ERR;
goto done;
}
/* Sanity check to make sure the second pass computations match the first pass */
if (memcmp(&pass1Header, coreFileData, sizeof(pass1Header)) != 0) {
omrtty_printf("ERROR: Pass 2 header does not match pass 1\n");
rc = JNI_EINVAL;
goto done;
}
if ((((U_8 *)(coreFileData +1)) + coreFileData->structDataSize) != data->cursor) {
omrtty_printf("ERROR: Pass 2 data size does not match pass 1\n");
rc = JNI_EINVAL;
goto done;
}
/* Write the string table after the struct data */
copyStringTable(data);
/* Sanity check to make sure the second pass computations match the first pass */
if ((((U_8 *)(coreFileData +1)) + coreFileData->structDataSize + coreFileData->stringDataSize) != data->cursor) {
omrtty_printf("ERROR: Pass 2 string table size does not match pass 1\n");
rc = JNI_EINVAL;
goto done;
}
/* Fill in compile-time constants */
coreFileData->version = J9DDR_CORE_FILE_DATA_VERSION;
coreFileData->sizeofBool = sizeofBool();
coreFileData->sizeofUDATA = sizeof(UDATA);
rc = initializeBitfieldEncoding(data);
done:
if (stringTable != NULL) {
hashTableFree(stringTable);
}
return rc;
}
static jint
processDDRStructTableList(J9DDRTranslationData * data)
{
const J9DDRStructDefinition ** list = data->structTables;
/* Process each struct table in the list */
while(NULL != *list) {
jint rc = processDDRStructTable(data, *list++);
if (rc != JNI_OK) {
return rc;
}
}
/* Round the string data size up to U_32 to maintain alignment of anything which comes after it */
data->coreFileData->stringDataSize = (data->coreFileData->stringDataSize + 3) & ~(U_32)3;
return JNI_OK;
}
static void
copyStringTable(J9DDRTranslationData * data)
{
U_8 * stringData = data->cursor;
J9HashTableState state;
J9DDRStringTableEntry * entry;
/* Iterate the table and copy each string to its already-assigned offset */
entry = hashTableStartDo(data->stringTable, &state);
while (entry != NULL) {
J9UTF8 * utf = (J9UTF8 *)(stringData + entry->offset);
const char * cString = entry->cString;
size_t len = strlen(cString);
J9UTF8_SET_LENGTH(utf, (U_16)len);
memcpy(J9UTF8_DATA(utf), cString, len);
entry = hashTableNextDo(&state);
}
/* Update cursor (stringDataSize is U_32 aligned already) */
data->cursor = stringData + data->coreFileData->stringDataSize;
}
static UDATA
stringTableHash(void *key, void *userData)
{
const char *s = ((J9DDRStringTableEntry *)key)->cString;
size_t length = strlen(s);
UDATA hash = 0;
U_8 * data = (U_8 *) s;
const U_8 * end = data + length;
while (data < end) {
U_16 c;
data += decodeUTF8Char(data, &c);
hash = (hash << 5) - hash + c;
}
return hash;
}
static UDATA
stringTableEquals(void *leftKey, void *rightKey, void *userData)
{
const char *left_s = ((J9DDRStringTableEntry *)leftKey)->cString;
const char *right_s = ((J9DDRStringTableEntry *)rightKey)->cString;
return (left_s == right_s) || (0 == strcmp(left_s, right_s));
}
static U_32
stringTableOffset(J9DDRTranslationData * data, const char * cString)
{
J9DDRStringTableEntry exemplar;
J9DDRStringTableEntry * entry;
exemplar.cString = cString;
exemplar.offset = U_32_MAX;
/* Add will return an existing entry if one matches, NULL on allocation failure, or the new node */
entry = hashTableAdd(data->stringTable, &exemplar);
/* NULL means the string was new, but memory allocation failed */
if (entry == NULL) {
OMRPORT_ACCESS_FROM_OMRPORT(data->portLibrary);
omrtty_printf("ERROR: Unable to allocate memory for string table entry: %s", cString);
return U_32_MAX;
}
/* If the offset is U_32_MAX, this indicates that a new entry was added */
if (entry->offset == U_32_MAX) {
entry->offset = data->coreFileData->stringDataSize;
/* Reserve space for the U_16 size, the string data, and the padding byte, if necessary */
data->coreFileData->stringDataSize += (U_32)((sizeof(U_16) + strlen(cString) + 1) & ~(size_t)1);
}
return entry->offset;
}
static jint
processDDRStructTable(J9DDRTranslationData * data, const J9DDRStructDefinition * ddrStruct)
{
/* Process each struct definition in the array (table is terminated with an all-zero entry */
while (ddrStruct->name != NULL) {
J9DDRCoreFileStruct * coreFileStruct = data->cursor;
const J9DDRFieldDeclaration * ddrField;
const J9DDRConstantDeclaration * ddrConstant;
U_32 nameOffset;
U_32 superOffset;
U_32 fieldCount = 0;
U_32 constantCount = 0;
jint rc;
if (data->cursor != NULL) {
data->cursor = coreFileStruct + 1;
}
data->coreFileData->structDataSize += sizeof(J9DDRCoreFileStruct);
/* Process fields */
ddrField = ddrStruct->fields;
if (ddrField != NULL) {
while (ddrField->name != NULL) {
rc = processDDRField(data, ddrField, &(data->coreFileData->structDataSize));
if (rc != JNI_OK) {
return rc;
}
++fieldCount;
++ddrField;
}
}
/* Process constants */
ddrConstant = ddrStruct->constants;
if (ddrConstant != NULL) {
while (ddrConstant->name != NULL) {
rc = processDDRConstant(data, ddrConstant, &(data->coreFileData->structDataSize));
if (rc != JNI_OK) {
return rc;
}
++constantCount;
++ddrConstant;
}
}
/* Create the strings */
nameOffset = stringTableOffset(data, ddrStruct->name);
if (nameOffset == U_32_MAX) {
return JNI_ENOMEM;
}
if (ddrStruct->superName == NULL) {
superOffset = U_32_MAX;
} else {
superOffset = stringTableOffset(data, ddrStruct->superName);
if (superOffset == U_32_MAX) {
return JNI_ENOMEM;
}
}
/* Fill in the header */
if (data->cursor != NULL) {
coreFileStruct->nameOffset = nameOffset;
coreFileStruct->superOffset = superOffset;
coreFileStruct->structSize = ddrStruct->size;
coreFileStruct->fieldCount = fieldCount;
coreFileStruct->constantCount = constantCount;
}
data->coreFileData->structureCount += 1;
++ddrStruct;
}
return JNI_OK;
}
static jint
processDDRField(J9DDRTranslationData * data, const J9DDRFieldDeclaration * ddrField, U_32 * size)
{
U_32 nameOffset;
U_32 typeOffset;
/* Create the strings */
nameOffset = stringTableOffset(data, ddrField->name);
if (nameOffset == U_32_MAX) {
return JNI_ENOMEM;
}
typeOffset = stringTableOffset(data, ddrField->type);
if (typeOffset == U_32_MAX) {
return JNI_ENOMEM;
}
/* Fill in the struct */
if (data->cursor != NULL) {
J9DDRCoreFileField * coreFileField = data->cursor;
coreFileField->nameOffset = nameOffset;
coreFileField->typeOffset = typeOffset;
coreFileField->offset = ddrField->offset;
data->cursor = coreFileField + 1;
}
/* Update required size */
*size += sizeof(J9DDRCoreFileField);
return JNI_OK;
}
static jint
processDDRConstant(J9DDRTranslationData * data, const J9DDRConstantDeclaration * ddrConstant, U_32 * size)
{
U_32 nameOffset;
/* Create the strings */
nameOffset = stringTableOffset(data, ddrConstant->name);
if (nameOffset == U_32_MAX) {
return JNI_ENOMEM;
}
/* Fill in the struct */
if (data->cursor != NULL) {
J9DDRCoreFileConstant * coreFileConstant = data->cursor;
memcpy(coreFileConstant->value, &ddrConstant->value, sizeof(U_64));
coreFileConstant->nameOffset = nameOffset;
data->cursor = coreFileConstant + 1;
}
/* Update required size */
*size += sizeof(J9DDRCoreFileConstant);
return JNI_OK;
}
UDATA
ddrSignalProtectedMain(struct OMRPortLibrary *portLibrary, void *arg)
{
J9DDRCmdlineOptions *startupOptions = arg;
int argc = startupOptions->argc;
char **argv = startupOptions->argv;
char **envp = startupOptions->envp;
OMRPORT_ACCESS_FROM_OMRPORT(startupOptions->portLibrary);
J9DDRTranslationData data;
UDATA rc = JNI_OK;
memset(&data, 0, sizeof(data));
data.portLibrary = OMRPORTLIB;
data.filename = J9DDR_DAT_FILE;
ddr_setNLSCatalog(OMRPORTLIB, argv);
/* Initialize the thread library -- Zip code now uses monitors. */
omrthread_attach_ex(NULL, J9THREAD_ATTR_DEFAULT);
data.structTables = initializeDDRComponents(data.portLibrary);
if (NULL == data.structTables) {
rc = JNI_ENOMEM;
goto done;
}
rc = createDDRCoreData(&data);
if (rc != JNI_OK) {
goto done;
}
rc = writeDataFile(&data);
if (rc != JNI_OK) {
goto done;
}
omrtty_printf("Completed with no errors\n");
done:
omrmem_free_memory(data.coreFileData);
omrmem_free_memory((void*) data.structTables);
return (UDATA) rc;
}
static jint
writeDataFile(J9DDRTranslationData * data)
{
OMRPORT_ACCESS_FROM_OMRPORT(data->portLibrary);
IDATA fd;
IDATA length = data->totalSize;
omrtty_printf("Creating output file %s of size %i\n", data->filename, length);
fd = omrfile_open(data->filename, EsOpenWrite | EsOpenCreate | EsOpenTruncate, 0666);
if (fd == -1) {
omrtty_printf("ERROR: Failed to open output file\n");
return JNI_ERR;
}
if (omrfile_write(fd, data->coreFileData, length) != length) {
omrtty_printf("ERROR: Failed to write to output file\n");
omrfile_close(fd);
return JNI_ERR;
}
if (omrfile_close(fd) != 0) {
omrtty_printf("ERROR: Failed to close output file\n");
return JNI_ERR;
}
return JNI_OK;
}
static jint
initializeBitfieldEncoding(J9DDRTranslationData * data)
{
bitField bf;
jint rc = JNI_OK;
U_32 slot0;
U_32 slot1;
memset(&bf, 0, sizeof(bf));
bf.x1 = 1; /* 1 */
bf.x2 = 31768; /* 111110000011000 */
bf.x3 = 3; /* 000011 */
bf.x4 = 777; /* 1100001001 */
bf.x5 = 43; /* 0101011 */
bf.x6 = 298; /* 100101010 */
bf.x7 = 12; /* 1100 */
bf.x8 = 1654; /* 011001110110 */
slot0 = ((U_32*)&bf)[0];
slot1 = ((U_32*)&bf)[1];
if ((slot0 ==0xC243F831) && (slot1 == 0x676C952B)) {
data->coreFileData->bitfieldFormat = 1;
} else if ((slot0 ==0xFC180F09) && (slot1 == 0x572AC676)) {
data->coreFileData->bitfieldFormat = 2;
} else {
OMRPORT_ACCESS_FROM_OMRPORT(data->portLibrary);
omrtty_printf("ERROR: Unable to determine bitfield format from %08X %08X\n", slot0, slot1);
rc = JNI_ERR;
}
return rc;
}