Path: blob/master/runtime/jcl/common/jdk_internal_misc_ScopedMemoryAccess.cpp
6000 views
/*******************************************************************************1* Copyright (c) 2020, 2021 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*******************************************************************************/2122#include "jni.h"23#include "jcl.h"24#include "jclglob.h"25#include "jclprots.h"26#include "jcl_internal.h"27#include "rommeth.h"28#include "omrlinkedlist.h"2930extern "C" {3132#if JAVA_SPEC_VERSION >= 1633void JNICALL34Java_jdk_internal_misc_ScopedMemoryAccess_registerNatives(JNIEnv *env, jclass clazz)35{36}3738static UDATA39closeScope0FrameWalkFunction(J9VMThread *vmThread, J9StackWalkState *walkState)40{41if (JNI_FALSE == *(jboolean *)walkState->userData2) {42/* scope has been found */43return J9_STACKWALK_STOP_ITERATING;44}45return J9_STACKWALK_KEEP_ITERATING;46}4748static void49closeScope0OSlotWalkFunction(J9VMThread *vmThread, J9StackWalkState *walkState, j9object_t *slot, const void *stackLocation)50{51J9Method *method = walkState->method;52if (NULL != method) {53J9ROMMethod *romMethod = J9_ROM_METHOD_FROM_RAM_METHOD(method);54if (NULL != romMethod && J9ROMMETHOD_HAS_EXTENDED_MODIFIERS(romMethod)) {55U_32 extraModifiers = getExtendedModifiersDataFromROMMethod(romMethod);56if (J9ROMMETHOD_HAS_SCOPED_ANNOTATION(extraModifiers)) {57if (*slot == J9_JNI_UNWRAP_REFERENCE(walkState->userData1)) {58*(jboolean *)walkState->userData2 = JNI_FALSE;59}60}61}62}63}6465/**66* For each thread, walk Java stack and look for the scope instance. Methods that can take and access a scope67* instance are marked with the "@Scoped" extended modifier. If the scope instance is found in a method, that68* method is accessing the memory segment associated with the scope and thus closing the scope will fail.69*/70jboolean JNICALL71Java_jdk_internal_misc_ScopedMemoryAccess_closeScope0(JNIEnv *env, jobject instance, jobject scope, jobject exception)72{73J9VMThread *currentThread = (J9VMThread *)env;74J9JavaVM *vm = currentThread->javaVM;75const J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;76jboolean scopeNotFound = JNI_TRUE;7778vmFuncs->internalEnterVMFromJNI(currentThread);79vmFuncs->acquireExclusiveVMAccess(currentThread);8081if (NULL == scope) {82vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, NULL);83} else {84J9VMThread *walkThread = J9_LINKED_LIST_START_DO(vm->mainThread);85while (NULL != walkThread) {86J9StackWalkState walkState;87walkState.walkThread = walkThread;88walkState.flags = J9_STACKWALK_ITERATE_FRAMES | J9_STACKWALK_ITERATE_O_SLOTS;89walkState.skipCount = 0;90walkState.userData1 = (void *)scope;91walkState.userData2 = (void *)&scopeNotFound;92walkState.frameWalkFunction = closeScope0FrameWalkFunction;93walkState.objectSlotWalkFunction = closeScope0OSlotWalkFunction;9495vm->walkStackFrames(walkThread, &walkState);96if (JNI_FALSE == *(jboolean *)walkState.userData2) {97/* scope found */98break;99}100101walkThread = J9_LINKED_LIST_NEXT_DO(vm->mainThread, walkThread);102}103}104105vmFuncs->releaseExclusiveVMAccess(currentThread);106vmFuncs->internalExitVMToJNI(currentThread);107return scopeNotFound;108}109#endif /* JAVA_SPEC_VERSION >= 16 */110111} /* extern "C" */112113114