Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/bcutil/ClassFileParser.cpp
5985 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2014 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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
21
*******************************************************************************/
22
#include "ClassFileParser.hpp"
23
#include "ROMClassCreationContext.hpp"
24
#include "ROMClassVerbosePhase.hpp"
25
#include "bcutil_api.h"
26
#include "ut_j9bcu.h"
27
28
/*
29
* NOTE: Do not use BufferManager for memory allocation in ClassFileParser.
30
* In the error case, the allocated memory is passed outside of the ROMClassBuilder.
31
*/
32
33
BuildResult
34
ClassFileParser::parseClassFile(ROMClassCreationContext *context, UDATA *initialBufferSize, U_8 **classFileBuffer)
35
{
36
BuildResult buildResult = OutOfMemory;
37
ROMClassVerbosePhase v(context, ParseClassFile, &buildResult);
38
PORT_ACCESS_FROM_PORT(_portLibrary);
39
UDATA bufferSize = *initialBufferSize;
40
U_8 * buffer = *classFileBuffer;
41
UDATA romMethodSortThreshold = UDATA_MAX;
42
J9JavaVM *vm = context->javaVM();
43
44
if (NULL != vm) {
45
romMethodSortThreshold = vm->romMethodSortThreshold;
46
}
47
if ( NULL == buffer ) {
48
buffer = (U_8 *)j9mem_allocate_memory(bufferSize, J9MEM_CATEGORY_CLASSES);
49
*classFileBuffer = buffer;
50
if ( NULL == buffer ) {
51
return buildResult;
52
}
53
}
54
55
I_32 result = BCT_ERR_OUT_OF_ROM;
56
while( BCT_ERR_OUT_OF_ROM == result ) {
57
result = j9bcutil_readClassFileBytes(
58
_portLibrary,
59
_verifyClassFunction,
60
context->classFileBytes(), context->classFileSize(),
61
buffer, bufferSize,
62
context->bctFlags(),
63
NULL,
64
context->isVerbose() ? context : NULL,
65
context->findClassFlags(), romMethodSortThreshold);
66
67
if (BCT_ERR_OUT_OF_ROM == result) {
68
context->recordOutOfMemory(bufferSize);
69
/* free old buffer, update bufferSize and alloc new buffer */
70
context->freeClassFileBuffer(buffer);
71
72
UDATA newBufferSize = bufferSize * 2;
73
/* Check for overflow. */
74
if (newBufferSize <= bufferSize) {
75
buffer = NULL;
76
} else {
77
bufferSize = newBufferSize;
78
buffer = (U_8 *)j9mem_allocate_memory(bufferSize, J9MEM_CATEGORY_CLASSES);
79
}
80
81
*classFileBuffer = buffer;
82
if ( NULL == buffer ) {
83
return buildResult;
84
}
85
}
86
}
87
*initialBufferSize = bufferSize;
88
89
if ( BCT_ERR_NO_ERROR == result ) {
90
/* return the buffer */
91
_j9CfrClassFile = (J9CfrClassFile *)buffer;
92
buildResult = BuildResult(result);
93
} else if ( BCT_ERR_GENERIC_ERROR == result ) {
94
/* error structure filled in, located in buffer */
95
context->recordCFRError(buffer);
96
Trc_BCU_createRomClassEndian_Error(result, I_32(ClassRead));
97
buildResult = ClassRead;
98
} else {
99
Trc_BCU_createRomClassEndian_Error(result, I_32(GenericError));
100
buildResult = GenericError;
101
}
102
103
return buildResult;
104
}
105
106
void
107
ClassFileParser::restoreOriginalMethodBytecodes()
108
{
109
J9CfrMethod *end = &_j9CfrClassFile->methods[_j9CfrClassFile->methodsCount];
110
111
for (J9CfrMethod *method = _j9CfrClassFile->methods; method != end; method++) {
112
J9CfrAttributeCode *codeAttribute = method->codeAttribute;
113
if (NULL != codeAttribute) {
114
memcpy(codeAttribute->code, codeAttribute->originalCode, codeAttribute->codeLength);
115
}
116
}
117
}
118
119