-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathfieldutil.c
331 lines (280 loc) · 9.43 KB
/
fieldutil.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
/*******************************************************************************
* 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
*******************************************************************************/
#if defined(J9VM_OUT_OF_PROCESS)
#include "j9dbgext.h"
#include "dbggen.h"
#endif /* defined(J9VM_OUT_OF_PROCESS) */
#include "j9.h"
#include "j9protos.h"
#include "j9consts.h"
#include "util_internal.h"
#include "ut_j9vmutil.h"
static UDATA getFieldAnnotationsDataOffset(J9ROMFieldShape * field);
static UDATA annotationAttributeSize(U_8 *sectionStart);
/* Field signature translation table
* NOTE: this table is NOT used in this file, but declared as an
* external in util_api.h and used elsewhere in the code.
*
* Shift left by 16 to use as J9FieldType* */
const U_8 fieldModifiersLookupTable[] = {
0x00 /* A */,
(U_8)(J9FieldTypeByte >> 16) /* B */,
(U_8)(J9FieldTypeChar >> 16) /* C */,
(U_8)((J9FieldSizeDouble + J9FieldTypeDouble) >> 16)/* D */,
0x00 /* E */,
(U_8)(J9FieldTypeFloat >> 16) /* F */,
0x00 /* G */,
0x00 /* H */,
(U_8)(J9FieldTypeInt >> 16) /* I */,
(U_8)((J9FieldSizeDouble + J9FieldTypeLong) >> 16) /* J */,
0x00 /* K */,
(U_8)(J9FieldFlagObject >> 16) /* L */,
0x00 /* M */,
0x00 /* N */,
0x00 /* O */,
0x00 /* P */,
0x00 /* Q */,
0x00 /* R */,
(U_8)(J9FieldTypeShort >> 16) /* S */,
0x00 /* T */,
0x00 /* U */,
0x00 /* V */,
0x00 /* W */,
0x00 /* X */,
0x00 /* Y */,
(U_8)(J9FieldTypeBoolean >> 16) /* Z */,
(U_8)(J9FieldFlagObject >> 16) /* [ */
};
J9ROMFieldShape *
romFieldsNextDo(J9ROMFieldWalkState* state)
{
if (state->fieldsLeft == 0) {
return NULL;
}
state->field = (J9ROMFieldShape *) (((U_8 *) state->field) + romFieldSize(state->field));
--(state->fieldsLeft);
return state->field;
}
J9ROMFieldShape *
romFieldsStartDo(J9ROMClass * romClass, J9ROMFieldWalkState* state)
{
state->fieldsLeft = romClass->romFieldCount;
if (state->fieldsLeft == 0) {
return NULL;
}
state->field = J9ROMCLASS_ROMFIELDS(romClass);
--(state->fieldsLeft);
return state->field;
}
void
walkFieldHierarchyDo(J9Class *clazz, J9WalkFieldHierarchyState *state)
{
J9ROMFieldShape *romField = NULL;
J9ROMFieldWalkState walkState;
/* walk class and superclasses */
if (FALSE == J9ROMCLASS_IS_INTERFACE(clazz->romClass)) {
UDATA classDepth = J9CLASS_DEPTH(clazz);
J9Class *currentClass = clazz;
while (NULL != currentClass) {
/* walk currentClass */
memset(&walkState, 0, sizeof(walkState));
romField = romFieldsStartDo(currentClass->romClass, &walkState);
while (NULL != romField) {
if (J9WalkFieldActionStop == state->fieldCallback(romField, currentClass, state->userData)) {
return;
}
romField = romFieldsNextDo(&walkState);
}
/* get the superclass */
if (classDepth >= 1) {
classDepth -= 1;
currentClass = clazz->superclasses[classDepth];
} else {
currentClass = NULL;
}
}
}
/* walk interfaces */
{
J9ITable *currentITable = (J9ITable *)clazz->iTable;
while (NULL != currentITable) {
memset(&walkState, 0, sizeof(walkState));
romField = romFieldsStartDo(currentITable->interfaceClass->romClass, &walkState);
while (NULL != romField) {
if (J9WalkFieldActionStop == state->fieldCallback(romField, currentITable->interfaceClass, state->userData)) {
return;
}
romField = romFieldsNextDo(&walkState);
}
currentITable = currentITable->next;
}
}
}
/**
* Calculate the size of an annotation attribute, including padding to alignment at the end.
* Assumes the sectionStart starts on a 4-byte boundary.
* @param sectionStart pointer to the 4-byte length field preceding the actual annotation data
* @return number of bytes in the attribute, including the length field and trailing padding.
*/
static UDATA
annotationAttributeSize(U_8 *sectionStart)
{
Assert_VMUtil_true(((UDATA)sectionStart % sizeof(U_32)) == 0);
/* skip over the field annotations length bytes, data, and padding */
return ROUND_UP_TO_POWEROF2(sizeof(U_32) + *((U_32 *)sectionStart), sizeof(U_32)); /* length + data + padding */
}
UDATA
romFieldSize(J9ROMFieldShape *romField)
{
UDATA modifiers = romField->modifiers;
UDATA size = sizeof(J9ROMFieldShape);
if (modifiers & J9FieldFlagConstant) {
size += ((modifiers & J9FieldSizeDouble) ? sizeof(U_64) : sizeof(U_32));
}
if (modifiers & J9FieldFlagHasGenericSignature) {
size += sizeof(U_32);
}
if (modifiers & J9FieldFlagHasFieldAnnotations) {
size += annotationAttributeSize((U_8 *)romField + size);
}
if (J9_ARE_ANY_BITS_SET(modifiers, J9FieldFlagHasTypeAnnotations)) {
size += annotationAttributeSize((U_8 *)romField + size);
}
return size;
}
U_32 *
romFieldInitialValueAddress(J9ROMFieldShape * field)
{
if (field->modifiers & J9FieldFlagConstant) {
return (U_32 *) (field + 1);
}
return NULL;
}
J9UTF8 * romFieldGenericSignature(J9ROMFieldShape * field)
{
if (field->modifiers & J9FieldFlagHasGenericSignature) {
U_32 * ptr = (U_32 *) (field + 1);
if (field->modifiers & J9FieldFlagConstant) {
ptr += ((field->modifiers & J9FieldSizeDouble) ? 2 : 1);
}
return NNSRP_PTR_GET(ptr, J9UTF8 *);
}
return NULL;
}
/**
* Find the offset from the beginning of the field to the area containing field annotations.
* If there are no field annotations, this is the start of the field type annotations (if any).
* Skip over any constant value or generic field signature.
* @param field pointer to start of the ROM field
* @return offset in bytes to start of annotations area.
*/
static UDATA
getFieldAnnotationsDataOffset(J9ROMFieldShape * field)
{
UDATA modifiers = field->modifiers;
UDATA size = sizeof(J9ROMFieldShape);
if (modifiers & J9FieldFlagConstant) {
size += ((modifiers & J9FieldSizeDouble) ? sizeof(U_64) : sizeof(U_32));
}
if (modifiers & J9FieldFlagHasGenericSignature) {
size += sizeof(U_32);
}
return size;
}
U_32 *
getFieldAnnotationsDataFromROMField(J9ROMFieldShape * field)
{
U_32 *result = NULL;
if (field->modifiers & J9FieldFlagHasFieldAnnotations) {
UDATA size = getFieldAnnotationsDataOffset(field);
result = (U_32 *)((UDATA)field + size);
}
return result;
}
U_32 *
getFieldTypeAnnotationsDataFromROMField(J9ROMFieldShape * field)
{
U_32 *result = NULL;
if (field->modifiers & J9FieldFlagHasTypeAnnotations) {
U_8 *fieldAnnotationsData = (U_8 *)getFieldAnnotationsDataFromROMField(field);
if (NULL != fieldAnnotationsData) {
result = (U_32 *)(fieldAnnotationsData + annotationAttributeSize(fieldAnnotationsData));
} else {
/* the type annotations are where the field annotations would have been */
result = (U_32 *)((UDATA)field + getFieldAnnotationsDataOffset(field));
}
}
return result;
}
#if defined(J9VM_OUT_OF_PROCESS)
static UDATA
debugRomFieldSize(J9ROMFieldShape *romField)
{
UDATA modifiers = (UDATA)dbgReadSlot((UDATA)&((romField)->modifiers), sizeof((romField)->modifiers));
UDATA size = sizeof(J9ROMFieldShape);
if (modifiers & J9FieldFlagConstant) {
size += ((modifiers & J9FieldSizeDouble) ? sizeof(U_64) : sizeof(U_32));
}
if (modifiers & J9FieldFlagHasGenericSignature) {
size += sizeof(U_32);
}
if (modifiers & J9FieldFlagHasFieldAnnotations) {
UDATA bytesToPad = 0;
U_32 annotationSize = dbgReadU32((U_32 *)((UDATA)romField + size));
/* add the length of annotation data */
size += annotationSize;
/* add the size of the size of the annotation data */
size += sizeof(U_32);
bytesToPad = sizeof(U_32) - annotationSize%sizeof(U_32);
if (sizeof(U_32)==bytesToPad) {
bytesToPad = 0;
}
/* add the padding */
size += bytesToPad;
}
return size;
}
J9ROMFieldShape *
debugRomFieldsStartDo(J9ROMClass *romClass, J9ROMFieldWalkState *state)
{
state->fieldsLeft = (U_32)dbgReadSlot((UDATA)&((romClass)->romFieldCount), sizeof((romClass)->romFieldCount));
if (state->fieldsLeft == 0) {
state->field = NULL;
} else {
J9ROMClass *localRomClass = dbgRead_J9ROMClass(romClass);
state->field = TARGET_NNSRP_GET(localRomClass->romFields, struct J9ROMFieldShape*);
--(state->fieldsLeft);
dbgFree(localRomClass);
}
return state->field;
}
J9ROMFieldShape *
debugRomFieldsNextDo(J9ROMFieldWalkState *state)
{
if (state->fieldsLeft == 0) {
return NULL;
}
state->field = (J9ROMFieldShape *) (((U_8 *) state->field) + debugRomFieldSize(state->field));
--(state->fieldsLeft);
return state->field;
}
#endif /* defined(J9VM_OUT_OF_PROCESS) */