Skip to content

Commit

Permalink
Merge pull request #15322 from dnakamura/linkage_error
Browse files Browse the repository at this point in the history
Check for duplicate class definitions when creating rom class
  • Loading branch information
tajila committed Jun 24, 2022
2 parents 7b717f7 + 27a1982 commit da77f9f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
3 changes: 2 additions & 1 deletion runtime/bcutil/BuildResult.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2021 IBM Corp. and others
* Copyright (c) 2001, 2022 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
Expand Down Expand Up @@ -52,6 +52,7 @@ enum BuildResult {
#if defined(J9VM_OPT_VALHALLA_VALUE_TYPES)
InvalidValueType = BCT_ERR_INVALID_VALUE_TYPE,
#endif /* defined(J9VM_OPT_VALHALLA_VALUE_TYPES) */
DuplicateName = BCT_ERR_DUPLICATE_NAME,
};

#endif /* BUILDRESULT_HPP_ */
36 changes: 25 additions & 11 deletions runtime/bcutil/ROMClassCreationContext.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2021 IBM Corp. and others
* Copyright (c) 2001, 2022 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
Expand Down Expand Up @@ -452,16 +452,30 @@ class ROMClassCreationContext
return ClassNameMismatch;
#undef J9WRONGNAME
}
} else if (shouldCheckPackageName() /* classname is null */
&& (classNameLength >= 5)
&& (0 == memcmp(className, "java/", 5))
&& !isClassAnon()
) {
/*
* Non-bootstrap classloaders may not load nto the "java" package.
* if classname is not null, the JCL or JNI has already checked it
*/
return IllegalPackageName;
} else { /* classname is null */
/* If a name was not provided with the load data, now is our first chance to check for a duplicate definition */
if ((NULL != _javaVM) && (NULL != J9_VM_FUNCTION_VIA_JAVAVM(_javaVM, hashClassTableAt)(_classLoader, className, classNameLength))) {
PORT_ACCESS_FROM_PORT(_portLibrary);
U_8 *errorUTF = (U_8 *) j9mem_allocate_memory(classNameLength + 1, J9MEM_CATEGORY_CLASSES);
if (NULL != errorUTF) {
memcpy(errorUTF, className, classNameLength);
errorUTF[classNameLength] = '\0';
}
recordCFRError(errorUTF);
return DuplicateName;
}

if (shouldCheckPackageName()
&& (classNameLength >= 5)
&& (0 == memcmp(className, "java/", 5))
&& !isClassAnon()
) {
/*
* Non-bootstrap classloaders may not load nto the "java" package.
* if classname is not null, the JCL or JNI has already checked it
*/
return IllegalPackageName;
}
}

}
Expand Down
14 changes: 13 additions & 1 deletion runtime/bcutil/defineclass.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ createROMClassFromClassFile(J9VMThread *currentThread, J9LoadROMClassData *loadD
break;
}

case BCT_ERR_DUPLICATE_NAME:
/* This case is handled below */
break;

/*
* Error messages are contents of vm->dynamicLoadBuffers->classFileError if anything is assigned
* otherwise just the classname.
Expand Down Expand Up @@ -928,7 +932,15 @@ createROMClassFromClassFile(J9VMThread *currentThread, J9LoadROMClassData *loadD
J9_VM_FUNCTION(currentThread, setCurrentException)(currentThread, exceptionNumber, NULL);
} else {
PORT_ACCESS_FROM_JAVAVM(vm);
J9_VM_FUNCTION(currentThread, setCurrentExceptionUTF)(currentThread, exceptionNumber, (const char*)errorUTF);
if (BCT_ERR_DUPLICATE_NAME == result) {
size_t nameLength = 0;
if (NULL != errorUTF ){
nameLength = strlen(errorUTF);
}
J9_VM_FUNCTION(currentThread, setCurrentExceptionNLSWithArgs)(currentThread, J9NLS_JCL_DUPLICATE_CLASS_DEFINITION, J9VMCONSTANTPOOL_JAVALANGLINKAGEERROR, (const char*)errorUTF, nameLength);
} else {
J9_VM_FUNCTION(currentThread, setCurrentExceptionUTF)(currentThread, exceptionNumber, (const char*)errorUTF);
}
j9mem_free_memory(errorUTF);
}
}
Expand Down
15 changes: 12 additions & 3 deletions runtime/cfdumper/stubs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2019 IBM Corp. and others
* Copyright (c) 2019, 2022 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
Expand Down Expand Up @@ -45,6 +45,8 @@
* "_setCurrentException", referenced from:
* _internalDefineClass in libj9dyn.a(defineclass.o)
* "_setCurrentExceptionUTF", referenced from:
* _internalDefineClass in libj9dyn.a(defineclass.o)
* "setCurrentExceptionNLSWithArgs", referenced from:
* _internalDefineClass in libj9dyn.a(defineclass.o)
*/

Expand All @@ -64,16 +66,23 @@ internalCreateRAMClassFromROMClass(J9VMThread *vmThread, J9ClassLoader *classLoa

}

void
void
setCurrentExceptionUTF(J9VMThread * vmThread, UDATA exceptionNumber, const char * detailUTF)
{
printf("setCurrentExceptionUTF stub called!\n");
return;
}

void
void
setCurrentException(J9VMThread *currentThread, UDATA exceptionNumber, UDATA *detailMessage)
{
printf("setCurrentException stub called!\n");
return;
}

void
setCurrentExceptionNLSWithArgs(J9VMThread * vmThread, U_32 nlsModule, U_32 nlsID, UDATA exceptionIndex, ...)
{
printf("setCurrentExceptionNLSWithArgs stub called!\n");
return;
}
9 changes: 5 additions & 4 deletions runtime/jcl/common/jcldefine.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1998, 2021 IBM Corp. and others
* Copyright (c) 1998, 2022 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
Expand Down Expand Up @@ -133,8 +133,9 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,

if (isContiguousClassBytes) {
/* For ARRAYLETS case, we get free range checking from GetByteArrayRegion JNI call */
if ((offset < 0) || (length < 0) ||
(((U_32)offset + (U_32)length) > J9INDEXABLEOBJECT_SIZE(currentThread, *(J9IndexableObject **)classRep))) {
if ((offset < 0) || (length < 0)
|| (((U_32)offset + (U_32)length) > J9INDEXABLEOBJECT_SIZE(currentThread, *(J9IndexableObject **)classRep))
) {
vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGINDEXOUTOFBOUNDSEXCEPTION, NULL);
goto done;
}
Expand All @@ -153,7 +154,7 @@ defineClassCommon(JNIEnv *env, jobject classLoaderObject,

omrthread_monitor_enter(vm->classTableMutex);
/* Hidden class is never added into the hash table */
if (J9_ARE_NO_BITS_SET(*options, J9_FINDCLASS_FLAG_HIDDEN)) {
if ((NULL != utf8Name) && J9_ARE_NO_BITS_SET(*options, J9_FINDCLASS_FLAG_HIDDEN)) {
if (NULL != vmFuncs->hashClassTableAt(classLoader, utf8Name, utf8Length)) {
/* Bad, we have already defined this class - fail */
omrthread_monitor_exit(vm->classTableMutex);
Expand Down
3 changes: 2 additions & 1 deletion runtime/oti/bcutil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2021 IBM Corp. and others
* Copyright (c) 2011, 2022 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
Expand Down Expand Up @@ -43,4 +43,5 @@
#if defined(J9VM_OPT_VALHALLA_VALUE_TYPES)
#define BCT_ERR_INVALID_VALUE_TYPE -20
#endif /* defined(J9VM_OPT_VALHALLA_VALUE_TYPES) */
#define BCT_ERR_DUPLICATE_NAME -21

0 comments on commit da77f9f

Please sign in to comment.