Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return NULL instead of throwing when the class name is invalid #5284

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions runtime/jcl/common/jcldefine.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,
J9TranslationLocalBuffer localBuffer = {J9_CP_INDEX_NONE, LOAD_LOCATION_UNKNOWN, NULL};

if (vm->dynamicLoadBuffers == NULL) {
throwNewInternalError(env, "Dynamic loader is unavailable");
return NULL;
if (J9_ARE_NO_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
throwNewInternalError(env, "Dynamic loader is unavailable");
}
goto done;
}
dynFuncs = vm->dynamicLoadBuffers;

if (classRep == NULL) {
throwNewNullPointerException(env, NULL);
return NULL;
if (J9_ARE_NO_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
throwNewNullPointerException(env, NULL);
}
goto done;
}

vmFuncs->internalEnterVMFromJNI(currentThread);
Expand All @@ -72,18 +76,22 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,
vmFuncs->internalExitVMToJNI(currentThread);
/* Make a "flat" copy of classRep */
if (length < 0) {
throwNewIndexOutOfBoundsException(env, NULL);
return NULL;
if (J9_ARE_NO_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
throwNewIndexOutOfBoundsException(env, NULL);
}
goto done;
}
classBytes = j9mem_allocate_memory(length, J9MEM_CATEGORY_CLASSES);
if (classBytes == NULL) {
vmFuncs->throwNativeOOMError(env, 0, 0);
return NULL;
if (J9_ARE_NO_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
vmFuncs->throwNativeOOMError(env, 0, 0);
}
goto done;
}
(*env)->GetByteArrayRegion(env, classRep, offset, length, (jbyte *)classBytes);
if ((*env)->ExceptionCheck(env)) {
j9mem_free_memory(classBytes);
return NULL;
goto done;
}
vmFuncs->internalEnterVMFromJNI(currentThread);
}
Expand Down Expand Up @@ -123,16 +131,9 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,
if (vmFuncs->hashClassTableAt(classLoader, utf8Name, utf8Length) != NULL) {
/* Bad, we have already defined this class - fail */
omrthread_monitor_exit(vm->classTableMutex);

if (J9_ARE_ANY_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
/*
* The caller signalled that the name is invalid.
* Don't set a pending exception - the caller will do that.
*/
goto invalid_name;
if (J9_ARE_NO_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGLINKAGEERROR, (UDATA *)*(j9object_t*)className);
}

vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGLINKAGEERROR, (UDATA *)*(j9object_t*)className);
goto done;
}

Expand Down Expand Up @@ -211,18 +212,26 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,
}

done:
if ((clazz == NULL) && (currentThread->currentException == NULL)) {
/* should not get here -- throw the default exception just in case */
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGCLASSFORMATERROR, NULL);
}

result = vmFuncs->j9jni_createLocalRef(env, J9VM_J9CLASS_TO_HEAPCLASS(clazz));
if (NULL == clazz) {
if (J9_ARE_ANY_BITS_SET(options, J9_FINDCLASS_FLAG_NAME_IS_INVALID)) {
/*
* The caller signalled that the name is invalid. Leave the result NULL and
* clear any pending exception; the caller will throw NoClassDefFoundError.
*/
currentThread->currentException = NULL;
currentThread->privateFlags &= ~(UDATA)J9_PRIVATE_FLAGS_REPORT_EXCEPTION_THROW;
} else if (NULL == currentThread->currentException) {
/* should not get here -- throw the default exception just in case */
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGCLASSFORMATERROR, NULL);
}
} else {
result = vmFuncs->j9jni_createLocalRef(env, J9VM_J9CLASS_TO_HEAPCLASS(clazz));

if ((NULL != result) && J9CLASS_IS_EXEMPT_FROM_VALIDATION(clazz)) {
vmFuncs->fixUnsafeMethods(currentThread, result);
if ((NULL != result) && J9CLASS_IS_EXEMPT_FROM_VALIDATION(clazz)) {
vmFuncs->fixUnsafeMethods(currentThread, result);
}
}

invalid_name:
vmFuncs->internalExitVMToJNI(currentThread);

if ((U_8*)utf8NameStackBuffer != utf8Name) {
Expand Down