Skip to content

Commit

Permalink
Verify name is in class memory segment for copying
Browse files Browse the repository at this point in the history
Signed-off-by: Theresa Mammarella <[email protected]>
  • Loading branch information
theresa-m committed Nov 28, 2024
1 parent 752c652 commit 74b5d5e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
28 changes: 18 additions & 10 deletions runtime/bcverify/clconstraints.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@

static J9ClassLoadingConstraint* findClassLoadingConstraint (J9VMThread* vmThread, J9ClassLoader* loader, U_8* name, UDATA length);
static J9ClassLoadingConstraint* registerClassLoadingConstraint (J9VMThread* vmThread, J9ClassLoader* loader, U_8* name, UDATA length, UDATA copyName);
static void validateArgs (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length);
static void validateArgs (J9VMThread *vmThread, J9ClassLoader *loader1, J9ClassLoader *loader2, U_8 *name1, U_8 *name2, UDATA length, UDATA *copyUTF1, UDATA *copyUTF2);
static void constrainList (J9ClassLoadingConstraint* constraint, J9Class* clazz);
static UDATA constraintHashFn(void *key, void *userData);
static UDATA constraintHashEqualFn(void *leftKey, void *rightKey, void *userData);


/* This is a helper function used by Assert_RTV_validateClassLoadingConstraints */
static void
validateArgs (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length)
validateArgs (J9VMThread *vmThread, J9ClassLoader *loader1, J9ClassLoader *loader2, U_8 *name1, U_8 *name2, UDATA length, UDATA *copyUTF1, UDATA *copyUTF2)
{
J9MemorySegment *seg;

Expand All @@ -55,9 +55,15 @@ validateArgs (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loade
while (seg) {
if (seg->heapBase <= name1 && seg->heapTop >= name1) {
Assert_RTV_true( (seg->classLoader == loader1) || (seg->classLoader->flags & J9CLASSLOADER_INVARIANTS_SHARABLE) );
if (NULL != copyUTF1) {
*copyUTF1 = FALSE;
}
}
if (seg->heapBase <= name2 && seg->heapTop >= name2) {
Assert_RTV_true( (seg->classLoader == loader2) || (seg->classLoader->flags & J9CLASSLOADER_INVARIANTS_SHARABLE) );
if (NULL != copyUTF2) {
*copyUTF2 = FALSE;
}
}
seg = seg->nextSegment;
}
Expand All @@ -81,7 +87,7 @@ j9bcv_checkClassLoadingConstraintsForSignature (J9VMThread* vmThread, J9ClassLoa

Trc_RTV_checkClassLoadingConstraintsForSignature_Entry(vmThread, loader1, loader2, sig1, sig2, J9UTF8_LENGTH(sig1), J9UTF8_DATA(sig1));
Assert_RTV_true(J9UTF8_LENGTH(sig1) == J9UTF8_LENGTH(sig2));
Assert_RTV_validateClassLoadingConstraints(vmThread, loader1, loader2, J9UTF8_DATA(sig1), J9UTF8_DATA(sig2), J9UTF8_LENGTH(sig1));
Assert_RTV_validateClassLoadingConstraints(vmThread, loader1, loader2, J9UTF8_DATA(sig1), J9UTF8_DATA(sig2), J9UTF8_LENGTH(sig1), NULL, NULL);

omrthread_monitor_enter(javaVM->classTableMutex);
for (;;) {
Expand All @@ -102,7 +108,7 @@ j9bcv_checkClassLoadingConstraintsForSignature (J9VMThread* vmThread, J9ClassLoa
endIndex++;
}

rc = j9bcv_checkClassLoadingConstraintForName (vmThread, loader1, loader2, &J9UTF8_DATA(sig1)[index], &J9UTF8_DATA(sig2)[index], endIndex - index, FALSE);
rc = j9bcv_checkClassLoadingConstraintForName (vmThread, loader1, loader2, &J9UTF8_DATA(sig1)[index], &J9UTF8_DATA(sig2)[index], endIndex - index);
if (rc) {
break;
}
Expand All @@ -120,16 +126,18 @@ j9bcv_checkClassLoadingConstraintsForSignature (J9VMThread* vmThread, J9ClassLoa
/* NOTE: the current thread must own the class table mutex */

UDATA
j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length, UDATA copyUTFs)
j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length)
{
J9Class *class1;
J9Class *class2;
J9ClassLoadingConstraint *const1 = NULL;
J9ClassLoadingConstraint *const2 = NULL;
J9InternalVMFunctions const *vmFuncs = vmThread->javaVM->internalVMFunctions;
UDATA copyUTF1 = TRUE;
UDATA copyUTF2 = TRUE;

Trc_RTV_checkClassLoadingConstraintForName(vmThread, loader1, loader2, length, name1);
Assert_RTV_validateClassLoadingConstraints(vmThread, loader1, loader2, name1, name2, length);
Assert_RTV_validateClassLoadingConstraints(vmThread, loader1, loader2, name1, name2, length, &copyUTF1, &copyUTF2);

/* peek at the class tables to see if the class has been loaded yet */
class1 = vmFuncs->hashClassTableAt (loader1, name1, length);
Expand All @@ -140,7 +148,7 @@ j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* l
return 1;
}
} else if (class1 == NULL && class2 != NULL) {
const1 = registerClassLoadingConstraint (vmThread, loader1, name1, length, copyUTFs);
const1 = registerClassLoadingConstraint (vmThread, loader1, name1, length, copyUTF1);
if (const1 == NULL) return 1;
if (const1->clazz != NULL) {
if (const1->clazz != class2) {
Expand All @@ -151,7 +159,7 @@ j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* l
const1->clazz = class2;
}
} else if (class2 == NULL && class1 != NULL) {
const2 = registerClassLoadingConstraint (vmThread, loader2, name2, length, copyUTFs);
const2 = registerClassLoadingConstraint (vmThread, loader2, name2, length, copyUTF2);
if (const2->clazz != NULL) {
if (const2->clazz != class1) {
return 1;
Expand All @@ -164,11 +172,11 @@ j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* l
J9ClassLoadingConstraint *tempNext;
J9ClassLoadingConstraint *tempPrevious;

const1 = registerClassLoadingConstraint (vmThread, loader1, name1, length, copyUTFs);
const1 = registerClassLoadingConstraint (vmThread, loader1, name1, length, copyUTF1);
if (const1 == NULL) {
return 1;
}
const2 = registerClassLoadingConstraint (vmThread, loader2, name2, length, copyUTFs);
const2 = registerClassLoadingConstraint (vmThread, loader2, name2, length, copyUTF2);
if (const2 == NULL) {
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/bcverify/j9bcverify.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TraceExit=Trc_RTV_unlinkClassLoadingConstraints_Exit NoEnv Overhead=1 Level=1 Te

TraceEvent=Trc_RTV_checkClassLoadingConstraintForName Overhead=1 Level=3 Template="checkClassLoadingConstraintForName - Checking constraints between %p and %p for %.*s"

TraceAssert=Assert_RTV_validateClassLoadingConstraints Overhead=2 Level=5 NoEnv Assert="validateArgs(P1, P2, P3, P4, P5, P6), 1"
TraceAssert=Assert_RTV_validateClassLoadingConstraints Overhead=2 Level=5 NoEnv Assert="validateArgs(P1, P2, P3, P4, P5, P6, P7, P8), 1"
TraceAssert=Assert_RTV_true Overhead=1 Level=1 NoEnv Assert="P1"
TraceAssert=Assert_RTV_notEqual Overhead=1 Level=1 NoEnv Assert="P1 != P2"

Expand Down
6 changes: 3 additions & 3 deletions runtime/jcl/common/java_dyn_methodhandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ accessCheckFieldSignature(J9VMThread *currentThread, J9Class* lookupClass, UDATA
U_32 sigLength = J9UTF8_LENGTH(lookupSig) - sigOffset - 1;

omrthread_monitor_enter(vm->classTableMutex);
if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, ramClass->classLoader, &lookupSigData[sigOffset], &lookupSigData[sigOffset], sigLength, TRUE) != 0) {
if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, ramClass->classLoader, &lookupSigData[sigOffset], &lookupSigData[sigOffset], sigLength) != 0) {
result = FALSE;
}
omrthread_monitor_exit(vm->classTableMutex);
Expand Down Expand Up @@ -445,7 +445,7 @@ accessCheckMethodSignature(J9VMThread *currentThread, J9Method *method, j9object

/* Check if we really need to check this classloader constraint */
if (argumentRamClass->classLoader != targetClassloader) {
if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, argumentRamClass->classLoader, &J9UTF8_DATA(targetSig)[index], &lookupSigData[index], endIndex - index, TRUE) != 0) {
if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, argumentRamClass->classLoader, &J9UTF8_DATA(targetSig)[index], &lookupSigData[index], endIndex - index) != 0) {
result = FALSE;
goto releaseMutexAndReturn;
}
Expand Down Expand Up @@ -477,7 +477,7 @@ accessCheckMethodSignature(J9VMThread *currentThread, J9Method *method, j9object
endIndex++;
}

if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, returnRamClass->classLoader, &J9UTF8_DATA(targetSig)[index], &lookupSigData[index], endIndex - index, TRUE) != 0) {
if(verifyData->checkClassLoadingConstraintForNameFunction(currentThread, targetClassloader, returnRamClass->classLoader, &J9UTF8_DATA(targetSig)[index], &lookupSigData[index], endIndex - index) != 0) {
result = FALSE;
goto releaseMutexAndReturn;
}
Expand Down
3 changes: 1 addition & 2 deletions runtime/jcl/common/java_lang_invoke_MethodHandleNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,8 +1322,7 @@ Java_java_lang_invoke_MethodHandleNatives_resolve(
callerClass->classLoader,
J9UTF8_DATA(signature) + sigOffset,
J9UTF8_DATA(signature) + sigOffset,
J9UTF8_LENGTH(signature) - sigOffset - 1, /* -1 to remove the trailing ;*/
true);
J9UTF8_LENGTH(signature) - sigOffset - 1 /* -1 to remove the trailing ;*/);
omrthread_monitor_exit(vm->classTableMutex);
if (0 != clConstraintResult) {
vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGLINKAGEERROR, NULL);
Expand Down
3 changes: 1 addition & 2 deletions runtime/jcl/common/java_lang_invoke_VarHandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ accessCheckFieldType(J9VMThread *currentThread, J9Class* lookupClass, J9Class* t
typeClassloader,
&lookupSigData[1],
&lookupSigData[1],
J9UTF8_LENGTH(lookupSig) - 2,
TRUE) != 0) {
J9UTF8_LENGTH(lookupSig) - 2) != 0) {
result = FALSE;
}
j9thread_monitor_exit(vm->classTableMutex);
Expand Down
2 changes: 1 addition & 1 deletion runtime/oti/bcverify_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ j9bcv_checkClassLoadingConstraintsForSignature (J9VMThread* vmThread, J9ClassLoa
* @return UDATA
*/
UDATA
j9bcv_checkClassLoadingConstraintForName (J9VMThread* vmThread, J9ClassLoader* loader1, J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length, UDATA copyUTFs);
j9bcv_checkClassLoadingConstraintForName (J9VMThread *vmThread, J9ClassLoader *loader1, J9ClassLoader *loader2, U_8 *name1, U_8 *name2, UDATA length);

/**
* @brief
Expand Down
2 changes: 1 addition & 1 deletion runtime/oti/j9nonbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ typedef struct J9TranslationBufferSet {

typedef struct J9BytecodeVerificationData {
IDATA ( *verifyBytecodesFunction)(struct J9PortLibrary *portLib, struct J9Class *ramClass, struct J9ROMClass *romClass, struct J9BytecodeVerificationData *verifyData) ;
UDATA ( *checkClassLoadingConstraintForNameFunction)(struct J9VMThread* vmThread, struct J9ClassLoader* loader1, struct J9ClassLoader* loader2, U_8* name1, U_8* name2, UDATA length, UDATA copyUTFs) ;
UDATA ( *checkClassLoadingConstraintForNameFunction)(struct J9VMThread *vmThread, struct J9ClassLoader *loader1, struct J9ClassLoader *loader2, U_8 *name1, U_8 *name2, UDATA length) ;
struct J9UTF8** classNameList;
struct J9UTF8** classNameListEnd;
U_8* classNameSegment;
Expand Down

0 comments on commit 74b5d5e

Please sign in to comment.