Path: blob/master/runtime/bcutil/ClassFileParser.cpp
5985 views
/*******************************************************************************1* Copyright (c) 2001, 2014 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21#include "ClassFileParser.hpp"22#include "ROMClassCreationContext.hpp"23#include "ROMClassVerbosePhase.hpp"24#include "bcutil_api.h"25#include "ut_j9bcu.h"2627/*28* NOTE: Do not use BufferManager for memory allocation in ClassFileParser.29* In the error case, the allocated memory is passed outside of the ROMClassBuilder.30*/3132BuildResult33ClassFileParser::parseClassFile(ROMClassCreationContext *context, UDATA *initialBufferSize, U_8 **classFileBuffer)34{35BuildResult buildResult = OutOfMemory;36ROMClassVerbosePhase v(context, ParseClassFile, &buildResult);37PORT_ACCESS_FROM_PORT(_portLibrary);38UDATA bufferSize = *initialBufferSize;39U_8 * buffer = *classFileBuffer;40UDATA romMethodSortThreshold = UDATA_MAX;41J9JavaVM *vm = context->javaVM();4243if (NULL != vm) {44romMethodSortThreshold = vm->romMethodSortThreshold;45}46if ( NULL == buffer ) {47buffer = (U_8 *)j9mem_allocate_memory(bufferSize, J9MEM_CATEGORY_CLASSES);48*classFileBuffer = buffer;49if ( NULL == buffer ) {50return buildResult;51}52}5354I_32 result = BCT_ERR_OUT_OF_ROM;55while( BCT_ERR_OUT_OF_ROM == result ) {56result = j9bcutil_readClassFileBytes(57_portLibrary,58_verifyClassFunction,59context->classFileBytes(), context->classFileSize(),60buffer, bufferSize,61context->bctFlags(),62NULL,63context->isVerbose() ? context : NULL,64context->findClassFlags(), romMethodSortThreshold);6566if (BCT_ERR_OUT_OF_ROM == result) {67context->recordOutOfMemory(bufferSize);68/* free old buffer, update bufferSize and alloc new buffer */69context->freeClassFileBuffer(buffer);7071UDATA newBufferSize = bufferSize * 2;72/* Check for overflow. */73if (newBufferSize <= bufferSize) {74buffer = NULL;75} else {76bufferSize = newBufferSize;77buffer = (U_8 *)j9mem_allocate_memory(bufferSize, J9MEM_CATEGORY_CLASSES);78}7980*classFileBuffer = buffer;81if ( NULL == buffer ) {82return buildResult;83}84}85}86*initialBufferSize = bufferSize;8788if ( BCT_ERR_NO_ERROR == result ) {89/* return the buffer */90_j9CfrClassFile = (J9CfrClassFile *)buffer;91buildResult = BuildResult(result);92} else if ( BCT_ERR_GENERIC_ERROR == result ) {93/* error structure filled in, located in buffer */94context->recordCFRError(buffer);95Trc_BCU_createRomClassEndian_Error(result, I_32(ClassRead));96buildResult = ClassRead;97} else {98Trc_BCU_createRomClassEndian_Error(result, I_32(GenericError));99buildResult = GenericError;100}101102return buildResult;103}104105void106ClassFileParser::restoreOriginalMethodBytecodes()107{108J9CfrMethod *end = &_j9CfrClassFile->methods[_j9CfrClassFile->methodsCount];109110for (J9CfrMethod *method = _j9CfrClassFile->methods; method != end; method++) {111J9CfrAttributeCode *codeAttribute = method->codeAttribute;112if (NULL != codeAttribute) {113memcpy(codeAttribute->code, codeAttribute->originalCode, codeAttribute->codeLength);114}115}116}117118119