From 340d9c0a3b4d6c70fb29de28c0dfa46febaaf5ac Mon Sep 17 00:00:00 2001 From: Theresa Mammarella Date: Wed, 31 Jul 2024 10:58:33 -0400 Subject: [PATCH] Value type null restricted array support - create a null restricted array field for every RAM class that could support a flattened field (implicitly constructed) - implement JVM_IsNullRestrictedArray and JVM_NewNullRestrictedArray - Update exception thrown when null is set to a null restricted array from NullPointerException to ArrayStoreException as described in https://openjdk.org/jeps/8316779 Signed-off-by: Theresa Mammarella --- runtime/codert_vm/cnathelp.cpp | 32 +++++----- .../ClassArrayClassSlotIterator.cpp | 17 ++++++ .../ClassArrayClassSlotIterator.hpp | 3 + .../gc_structs/ClassLoaderClassesIterator.cpp | 12 ++++ runtime/j9vm/javanextvmi.cpp | 57 +++++++++++++++-- runtime/oti/j9.h | 6 +- runtime/oti/j9consts.h | 1 + runtime/oti/j9nonbuilder.h | 12 ++++ runtime/oti/vm_api.h | 12 ++++ runtime/vm/BytecodeInterpreter.hpp | 4 +- runtime/vm/ValueTypeHelpers.hpp | 5 +- runtime/vm/classsupport.c | 13 ++-- runtime/vm/createramclass.cpp | 61 ++++++++++++++----- runtime/vm/intfunc.c | 1 + test/functional/Valhalla/playlist.xml | 21 ++----- .../test/lworld/ValueTypeArrayTests.java | 37 +++++++++-- .../openj9/test/lworld/ValueTypeTests.java | 16 ----- 17 files changed, 226 insertions(+), 84 deletions(-) diff --git a/runtime/codert_vm/cnathelp.cpp b/runtime/codert_vm/cnathelp.cpp index a87590919fe..f8640d63b4c 100644 --- a/runtime/codert_vm/cnathelp.cpp +++ b/runtime/codert_vm/cnathelp.cpp @@ -40,7 +40,7 @@ extern "C" { void* J9FASTCALL -old_slow_jitThrowNullPointerException(J9VMThread *currentThread); +old_slow_jitThrowArrayStoreException(J9VMThread *currentThread); J9_EXTERN_BUILDER_SYMBOL(throwCurrentExceptionFromJIT); J9_EXTERN_BUILDER_SYMBOL(handlePopFramesFromJIT); @@ -1001,7 +1001,7 @@ old_fast_jitLoadFlattenableArrayElement(J9VMThread *currentThread) value = (j9object_t) currentThread->javaVM->internalVMFunctions->loadFlattenableArrayElement(currentThread, arrayObject, index, true); if (NULL == value) { J9ArrayClass *arrayObjectClass = (J9ArrayClass *)J9OBJECT_CLAZZ(currentThread, arrayObject); - if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(arrayObjectClass->componentType)) { + if (J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(arrayObjectClass->componentType)) { goto slow; } } @@ -1068,7 +1068,7 @@ old_fast_jitStoreFlattenableArrayElement(J9VMThread *currentThread) goto slow; } arrayrefClass = (J9ArrayClass *) J9OBJECT_CLAZZ(currentThread, arrayref); - if ((J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(arrayrefClass->componentType)) && (NULL == value)) { + if ((J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(arrayrefClass->componentType)) && (NULL == value)) { goto slow; } currentThread->javaVM->internalVMFunctions->storeFlattenableArrayElement(currentThread, arrayref, index, value); @@ -1474,11 +1474,10 @@ old_fast_jitCheckCast(J9VMThread *currentThread) slowPath = (void*)old_slow_jitCheckCast; } } -#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - else if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(castClass)) { - slowPath = (void*)old_slow_jitThrowNullPointerException; - } -#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ + /* In the future Valhalla checkcast needs to throw exception on + * null restricted checkedType if obj is null, + * see issue https://github.com/eclipse-openj9/openj9/issues/19764 + */ return slowPath; } @@ -1505,8 +1504,8 @@ old_fast_jitCheckCastForArrayStore(J9VMThread *currentThread) } } #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - else if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(castClass)) { - slowPath = (void*)old_slow_jitThrowNullPointerException; + else if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(castClass)) { + slowPath = (void*)old_slow_jitThrowArrayStoreException; } #endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ return slowPath; @@ -3610,11 +3609,10 @@ fast_jitCheckCast(J9VMThread *currentThread, J9Class *castClass, j9object_t obje slowPath = (void*)old_slow_jitCheckCast; } } -#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - else if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(castClass)) { - slowPath = (void*)old_slow_jitThrowNullPointerException; - } -#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ + /* In the future Valhalla checkcast needs to throw exception on + * null restricted checkedType if obj is null, + * see issue https://github.com/eclipse-openj9/openj9/issues/19764 + */ return slowPath; } @@ -3638,8 +3636,8 @@ fast_jitCheckCastForArrayStore(J9VMThread *currentThread, J9Class *castClass, j9 } } #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - else if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(castClass)) { - slowPath = (void*)old_slow_jitThrowNullPointerException; + else if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(castClass)) { + slowPath = (void*)old_slow_jitThrowArrayStoreException; } #endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ return slowPath; diff --git a/runtime/gc_structs/ClassArrayClassSlotIterator.cpp b/runtime/gc_structs/ClassArrayClassSlotIterator.cpp index 219e70a7a40..e7d7f02c9f4 100644 --- a/runtime/gc_structs/ClassArrayClassSlotIterator.cpp +++ b/runtime/gc_structs/ClassArrayClassSlotIterator.cpp @@ -46,10 +46,27 @@ GC_ClassArrayClassSlotIterator::nextSlot() classPtr = (J9Class *)_iterateClazz->arrayClass; if (!_isArrayClass) { _state = classArrayClassSlotIterator_state_done; + break; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + } else if (NULL == classPtr) { + classPtr = (J9Class *)_iterateClazz->nullRestrictedArrayClass; + _state += classArrayClassSlotIterator_state_componentType; +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ } else { _state += 1; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + if (NULL == ((J9ArrayClass *)classPtr)->companionArray) { + _state += 1; /* skip nullRestrictedArrayClass case */ + } +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ } break; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + case classArrayClassSlotIterator_state_nullRestrictedArrayClass: + classPtr = ((J9ArrayClass *)_iterateClazz)->companionArray; + _state += 1; + break; +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ case classArrayClassSlotIterator_state_componentType: classPtr = ((J9ArrayClass *)_iterateClazz)->componentType; _state += 1; diff --git a/runtime/gc_structs/ClassArrayClassSlotIterator.hpp b/runtime/gc_structs/ClassArrayClassSlotIterator.hpp index f40434acb60..298d44beb54 100644 --- a/runtime/gc_structs/ClassArrayClassSlotIterator.hpp +++ b/runtime/gc_structs/ClassArrayClassSlotIterator.hpp @@ -46,6 +46,9 @@ class GC_ClassArrayClassSlotIterator enum { classArrayClassSlotIterator_state_arrayClass = 0, +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + classArrayClassSlotIterator_state_nullRestrictedArrayClass, +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ classArrayClassSlotIterator_state_componentType, classArrayClassSlotIterator_state_leafComponentType, classArrayClassSlotIterator_state_done diff --git a/runtime/gc_structs/ClassLoaderClassesIterator.cpp b/runtime/gc_structs/ClassLoaderClassesIterator.cpp index e22d9028d5b..deb9748ad60 100644 --- a/runtime/gc_structs/ClassLoaderClassesIterator.cpp +++ b/runtime/gc_structs/ClassLoaderClassesIterator.cpp @@ -111,6 +111,18 @@ GC_ClassLoaderClassesIterator::nextClass() if ( (result->classLoader == _classLoader) && (NULL != result->arrayClass) ) { /* this class is defined in the loader, so follow its array classes */ _nextClass = result->arrayClass; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + } else if ( (result->classLoader == _classLoader) && (NULL != result->nullRestrictedArrayClass) ) { + /* class has a nullRestrictedArrayClass but not an arrayClass*/ + _nextClass = result->nullRestrictedArrayClass; + } else if ( (result->classLoader == _classLoader) + && J9CLASS_IS_ARRAY(result) + && (NULL != ((J9ArrayClass *)result)->companionArray) + && J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(((J9ArrayClass *)result)->companionArray) + ) { + /* class has both an arrayClass and nullRestrictedArrayClass */ + _nextClass = ((J9ArrayClass *)result)->companionArray; +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ } else if (TABLE_CLASSES == _mode) { _nextClass = nextTableClass(); } else { diff --git a/runtime/j9vm/javanextvmi.cpp b/runtime/j9vm/javanextvmi.cpp index 45d6f80b6fc..5ca8fb44288 100644 --- a/runtime/j9vm/javanextvmi.cpp +++ b/runtime/j9vm/javanextvmi.cpp @@ -763,15 +763,62 @@ JVM_IsImplicitlyConstructibleClass(JNIEnv *env, jclass cls) JNIEXPORT jboolean JNICALL JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj) { - // TODO implement this with https://github.com/eclipse-openj9/openj9/issues/19460 - return JNI_FALSE; + jboolean result = JNI_FALSE; + J9VMThread *currentThread = (J9VMThread *)env; + J9InternalVMFunctions *vmFuncs = currentThread->javaVM->internalVMFunctions; + vmFuncs->internalEnterVMFromJNI(currentThread); + if (NULL == obj) { + vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, NULL); + } else { + jclass clazz = env->GetObjectClass(obj); + J9Class *j9clazz = J9VM_J9CLASS_FROM_JCLASS(currentThread, clazz); + if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(j9clazz)) { + result = JNI_TRUE; + } + } + vmFuncs->internalExitVMToJNI(currentThread); + return result; } JNIEXPORT jarray JNICALL -JVM_NewNullRestrictedArray(JNIEnv *env, jclass cls, jint length) +JVM_NewNullRestrictedArray(JNIEnv *env, jclass componentType, jint length) { - assert(!"JVM_NewNullRestrictedArray unimplemented"); - return NULL; + J9VMThread *currentThread = (J9VMThread *)env; + J9JavaVM *vm = currentThread->javaVM; + J9InternalVMFunctions *vmFuncs = currentThread->javaVM->internalVMFunctions; + J9Class *ramClass = NULL; + j9object_t newArray = NULL; + jarray arrayRef = NULL; + + vmFuncs->internalEnterVMFromJNI(currentThread); + ramClass = J9VMJAVALANGCLASS_VMREF(currentThread, J9_JNI_UNWRAP_REFERENCE(componentType)); + + if (!(J9_IS_J9CLASS_VALUETYPE(ramClass) && J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(ramClass))) { + vmFuncs->setCurrentException(currentThread, J9VMCONSTANTPOOL_JAVALANGILLEGALARGUMENTEXCEPTION, NULL); + goto done; + } + + if (NULL == ramClass->nullRestrictedArrayClass) { + J9ROMArrayClass *arrayOfObjectsROMClass = (J9ROMArrayClass*)J9ROMIMAGEHEADER_FIRSTCLASS(vm->arrayROMClasses); + vmFuncs->internalCreateArrayClassWithOptions( + currentThread, arrayOfObjectsROMClass, ramClass, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY); + if (NULL != currentThread->currentException) { + goto done; + } + } + + newArray = vm->memoryManagerFunctions->J9AllocateIndexableObject( + currentThread, ramClass->nullRestrictedArrayClass, length, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE); + + if (NULL == newArray) { + vmFuncs->setHeapOutOfMemoryError(currentThread); + goto done; + } + + arrayRef = (jarray)vmFuncs->j9jni_createLocalRef(env, newArray); +done: + vmFuncs->internalExitVMToJNI(currentThread); + return arrayRef; } #endif /* defined(J9VM_OPT_VALHALLA_VALUE_TYPES) */ diff --git a/runtime/oti/j9.h b/runtime/oti/j9.h index 0a472b748cc..0688f3fde96 100644 --- a/runtime/oti/j9.h +++ b/runtime/oti/j9.h @@ -331,7 +331,8 @@ static const struct { \ #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) #define J9CLASS_UNPADDED_INSTANCE_SIZE(clazz) J9_VALUETYPE_FLATTENED_SIZE(clazz) -/* TODO replace with J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(clazz) J9_ARE_ALL_BITS_SET((clazz)->classFlags, J9ClassAllowsInitialDefaultValue)*/ +#define J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(clazz) J9_ARE_ALL_BITS_SET((clazz)->classFlags, J9ClassAllowsInitialDefaultValue) +/* TODO replace with J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE */ #define J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(clazz) J9_ARE_ALL_BITS_SET((clazz)->classFlags, J9ClassIsPrimitiveValueType) #define J9_IS_J9CLASS_FLATTENED(clazz) J9_ARE_ALL_BITS_SET((clazz)->classFlags, J9ClassIsFlattened) @@ -348,14 +349,17 @@ static const struct { \ J9_IS_J9CLASS_FLATTENED(fieldClazz) && \ (J9_ARE_NO_BITS_SET((romFieldShape)->modifiers, J9AccVolatile) || (J9CLASS_UNPADDED_INSTANCE_SIZE(fieldClazz) <= sizeof(U_64)))) #define J9_VALUETYPE_FLATTENED_SIZE(clazz) (J9CLASS_HAS_4BYTE_PREPADDING((clazz)) ? ((clazz)->totalInstanceSize - sizeof(U_32)) : (clazz)->totalInstanceSize) +#define J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(clazz) J9_ARE_ALL_BITS_SET((clazz)->classFlags, J9ClassArrayIsNullRestricted) #else /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ #define J9CLASS_UNPADDED_INSTANCE_SIZE(clazz) ((clazz)->totalInstanceSize) +#define J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(clazz) FALSE #define J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(clazz) FALSE #define J9_IS_J9CLASS_FLATTENED(clazz) FALSE #define J9ROMFIELD_IS_NULL_RESTRICTED(romField) FALSE #define J9_IS_FIELD_FLATTENED(fieldClazz, romFieldShape) FALSE #define J9_IS_NULL_RESTRICTED_FIELD_FLATTENED(fieldClazz, romFieldShape) FALSE #define J9_VALUETYPE_FLATTENED_SIZE(clazz)((UDATA) 0) /* It is not possible for this macro to be used since we always check J9_IS_J9CLASS_FLATTENED before ever using it. */ +#define J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(clazz) FALSE #endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ #define IS_REF_OR_VAL_SIGNATURE(firstChar) ('L' == (firstChar)) diff --git a/runtime/oti/j9consts.h b/runtime/oti/j9consts.h index e6b60957c3a..2993636e535 100644 --- a/runtime/oti/j9consts.h +++ b/runtime/oti/j9consts.h @@ -449,6 +449,7 @@ extern "C" { #define J9_FINDCLASS_FLAG_DO_NOT_SHARE 0x100000 #define J9_FINDCLASS_FLAG_LAMBDA 0x200000 #define J9_FINDCLASS_FLAG_LAMBDAFORM 0x400000 +#define J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY 0x800000 #define J9_FINDKNOWNCLASS_FLAG_INITIALIZE 0x1 #define J9_FINDKNOWNCLASS_FLAG_EXISTING_ONLY 0x2 diff --git a/runtime/oti/j9nonbuilder.h b/runtime/oti/j9nonbuilder.h index a04bd591a83..ed743815935 100644 --- a/runtime/oti/j9nonbuilder.h +++ b/runtime/oti/j9nonbuilder.h @@ -92,6 +92,7 @@ #define J9ClassAllowsInitialDefaultValue 0x400000 #define J9ClassAllowsNonAtomicCreation 0x800000 #define J9ClassNeedToPruneMemberNames 0x1000000 +#define J9ClassArrayIsNullRestricted 0x2000000 /* @ddr_namespace: map_to_type=J9FieldFlags */ @@ -3396,6 +3397,9 @@ typedef struct J9Class { /* A linked list of weak global references to every resolved MemberName whose clazz is this class. */ J9MemberNameListNode *memberNames; #endif /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */ +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + struct J9Class *nullRestrictedArrayClass; +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ } J9Class; /* Interface classes can never be instantiated, so the following fields in J9Class will not be used: @@ -3492,6 +3496,13 @@ typedef struct J9ArrayClass { /* A linked list of weak global references to every resolved MemberName whose clazz is this class. */ J9MemberNameListNode *memberNames; #endif /* defined(J9VM_OPT_OPENJDK_METHODHANDLE) */ +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + /* + * A nullable J9ArrayClass points to its null restricted companion, if one exists. + * A null restricted J9ArrayClass points to its nullable companion, if one exists. + */ + struct J9Class *companionArray; +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ } J9ArrayClass; @@ -4804,6 +4815,7 @@ typedef struct J9InternalVMFunctions { void (JNICALL *sendInit)(struct J9VMThread *vmContext, j9object_t object, struct J9Class *senderClass, UDATA lookupOptions) ; void ( *internalAcquireVMAccessNoMutex)(struct J9VMThread * vmThread) ; struct J9Class* ( *internalCreateArrayClass)(struct J9VMThread* vmThread, struct J9ROMArrayClass* romClass, struct J9Class* elementClass) ; + struct J9Class* ( *internalCreateArrayClassWithOptions)(struct J9VMThread* vmThread, struct J9ROMArrayClass* romClass, struct J9Class* elementClass, UDATA options) ; IDATA ( *attachSystemDaemonThread)(struct J9JavaVM * vm, struct J9VMThread ** p_env, const char * threadName) ; void ( *internalAcquireVMAccessClearStatus)(struct J9VMThread * vmThread, UDATA flags) ; #if defined(J9VM_OPT_REFLECT) diff --git a/runtime/oti/vm_api.h b/runtime/oti/vm_api.h index e4a8faabc17..0fe552ad62b 100644 --- a/runtime/oti/vm_api.h +++ b/runtime/oti/vm_api.h @@ -357,6 +357,18 @@ peekClassHashTable(J9VMThread* currentThread, J9ClassLoader* classLoader, U_8* c J9Class* internalCreateArrayClass(J9VMThread* vmThread, J9ROMArrayClass* romClass, J9Class* elementClass); +/** +* @brief +* @param vmThread +* @param romClass +* @param elementClass +* @param options +* @return J9Class* +*/ +J9Class* +internalCreateArrayClassWithOptions(J9VMThread* vmThread, J9ROMArrayClass* romClass, J9Class* elementClass, UDATA options); + + /** * Load the class with the specified name in a given module * diff --git a/runtime/vm/BytecodeInterpreter.hpp b/runtime/vm/BytecodeInterpreter.hpp index 8955229cc11..e3d6b8bdd63 100644 --- a/runtime/vm/BytecodeInterpreter.hpp +++ b/runtime/vm/BytecodeInterpreter.hpp @@ -6890,8 +6890,8 @@ class INTERPRETER_CLASS } else { #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) J9ArrayClass *arrayrefClass = (J9ArrayClass *) J9OBJECT_CLAZZ(_currentThread, arrayref); - if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(arrayrefClass->componentType) && (NULL == value)) { - rc = THROW_NPE; + if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(arrayrefClass) && (NULL == value)) { + rc = THROW_ARRAY_STORE; goto done; } #endif /* if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ diff --git a/runtime/vm/ValueTypeHelpers.hpp b/runtime/vm/ValueTypeHelpers.hpp index 32490af20e6..47059f769c9 100644 --- a/runtime/vm/ValueTypeHelpers.hpp +++ b/runtime/vm/ValueTypeHelpers.hpp @@ -512,7 +512,6 @@ class VM_ValueTypeHelpers { I_32 srcEndIndex = srcIndex + lengthInSlots; J9Class *srcClazz = J9OBJECT_CLAZZ(currentThread, srcObject); J9Class *destClazz = J9OBJECT_CLAZZ(currentThread, destObject); - J9Class *destComponentClass = ((J9ArrayClass *)destClazz)->componentType; /* Array elements must be copied backwards if source and destination overlap in memory and source is before destination */ if ((srcObject == destObject) && (srcIndex < destIndex) && ((srcIndex + lengthInSlots) > destIndex)) { @@ -566,8 +565,8 @@ class VM_ValueTypeHelpers { } if (typeChecksRequired) { - if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(destComponentClass) && (NULL == copyObject)) { - /* Null objects cannot be stored in an array of primitive value types */ + if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(destClazz) && (NULL == copyObject)) { + /* Null objects cannot be stored in an array of null-restricted value types */ return -2; } if (!VM_VMHelpers::objectArrayStoreAllowed(currentThread, destObject, copyObject)) { diff --git a/runtime/vm/classsupport.c b/runtime/vm/classsupport.c index 1f99a502ccc..70d4216cef5 100644 --- a/runtime/vm/classsupport.c +++ b/runtime/vm/classsupport.c @@ -196,22 +196,27 @@ findPrimitiveArrayClass(J9JavaVM* vm, jchar sigChar) */ J9Class* internalCreateArrayClass(J9VMThread* vmThread, J9ROMArrayClass* romClass, J9Class* elementClass) +{ + return internalCreateArrayClassWithOptions(vmThread, romClass, elementClass, 0); +} + +J9Class* +internalCreateArrayClassWithOptions(J9VMThread* vmThread, J9ROMArrayClass* romClass, J9Class* elementClass, UDATA options) { J9Class *result = NULL; j9object_t heapClass = J9VM_J9CLASS_TO_HEAPCLASS(elementClass); j9object_t protectionDomain = NULL; J9ROMClass* arrayRomClass = (J9ROMClass*) romClass; J9JavaVM *const javaVM = vmThread->javaVM; - UDATA options = 0; BOOLEAN elementInitSuccess = TRUE; #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - /* When creating an array of valuetype elements, the array elements are initialized to the defaultValue of the - * element type. As a result the element type must be fully initialized (if its a valuetype) before creating an + /* When creating an array of implicitly constructible valuetype elements, the array elements are initialized to + * the defaultValue of the element type. As a result the element type must be fully initialized before creating an * instance of the array. Element class init must be done before the arrayClass is created so that in the case * of an init failure the arrayClass is not temporarily exposed. */ - if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(elementClass)) { + if (J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(elementClass)) { UDATA initStatus = elementClass->initializeStatus; if ((J9ClassInitSucceeded != initStatus) && ((UDATA)vmThread != initStatus)) { initializeClass(vmThread, elementClass); diff --git a/runtime/vm/createramclass.cpp b/runtime/vm/createramclass.cpp index a72ea536024..b8baa5722ce 100644 --- a/runtime/vm/createramclass.cpp +++ b/runtime/vm/createramclass.cpp @@ -197,6 +197,18 @@ static void initializeClassLinks(J9Class *ramClass, J9Class *superclass, J9Memor #define MAGIC_ACCESSOR_IMPL "jdk/internal/reflect/MagicAccessorImpl" #endif /* JAVA_SPEC_VERSION == 8 */ +static inline J9Class * +getArrayClass(J9Class *elementClass, UDATA options) { +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + if (J9_ARE_ALL_BITS_SET(options, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY)) { + return elementClass->nullRestrictedArrayClass; + } else +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ + { + return elementClass->arrayClass; + } +} + /** * Mark all of the interfaces supported by this class, including all interfaces * inherited by superinterfaces. Unmark all interfaces which are inherited from @@ -2175,7 +2187,7 @@ internalCreateRAMClassDone(J9VMThread *vmThread, J9ClassLoader *classLoader, J9C if (elementClass == NULL) { alreadyLoadedClass = hashClassTableAt(classLoader, J9UTF8_DATA(className), J9UTF8_LENGTH(className)); } else { - alreadyLoadedClass = elementClass->arrayClass; + alreadyLoadedClass = getArrayClass(elementClass, options); } if (alreadyLoadedClass != NULL) { /* We are discarding this class */ @@ -2374,7 +2386,7 @@ internalCreateRAMClassDone(J9VMThread *vmThread, J9ClassLoader *classLoader, J9C if (elementClass == NULL) { alreadyLoadedClass = hashClassTableAt(classLoader, J9UTF8_DATA(className), J9UTF8_LENGTH(className)); } else { - alreadyLoadedClass = elementClass->arrayClass; + alreadyLoadedClass = getArrayClass(elementClass, options); } if (alreadyLoadedClass != NULL) { goto alreadyLoaded; @@ -2388,7 +2400,21 @@ internalCreateRAMClassDone(J9VMThread *vmThread, J9ClassLoader *classLoader, J9C } } else { if (J9ROMCLASS_IS_ARRAY(romClass)) { - ((J9ArrayClass *)elementClass)->arrayClass = state->ramClass; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + if (J9_ARE_ALL_BITS_SET(options, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY)) { + elementClass->nullRestrictedArrayClass = state->ramClass; + } else +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ + { + ((J9ArrayClass *)elementClass)->arrayClass = state->ramClass; + } +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) + if ((NULL != elementClass->nullRestrictedArrayClass) && (NULL != elementClass->arrayClass)) { + ((J9ArrayClass *)elementClass->arrayClass)->companionArray = elementClass->nullRestrictedArrayClass; + ((J9ArrayClass *)elementClass->nullRestrictedArrayClass)->companionArray = elementClass->arrayClass; + + } +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ /* Assigning into the arrayClass field creates an implicit reference to the class from its class loader */ javaVM->memoryManagerFunctions->j9gc_objaccess_postStoreClassToClassLoader(vmThread, classLoader, state->ramClass); } @@ -2844,7 +2870,7 @@ internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *clas if (elementClass == NULL) { ramClass = hashClassTableAt(classLoader, J9UTF8_DATA(className), J9UTF8_LENGTH(className)); } else { - ramClass = elementClass->arrayClass; + ramClass = getArrayClass(elementClass, options); } state->ramClass = ramClass; @@ -3189,7 +3215,7 @@ internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *clas * + J9ClassAllowsNonAtomicCreation * * + J9ClassNeedToPruneMemberNames - * + Unused + * + J9ClassArrayIsNullRestricted * + Unused * + Unused * @@ -3419,14 +3445,7 @@ internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *clas arity = elementArrayClass->arity + 1; leafComponentType = elementArrayClass->leafComponentType; } else { - U_32 arrayFlags = J9ClassLargestAlignmentConstraintReference | J9ClassLargestAlignmentConstraintDouble; - - if (J9_ARE_ALL_BITS_SET(javaVM->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_ENABLE_VT_ARRAY_FLATTENING)) { - arrayFlags |= J9ClassIsFlattened; - } - - arity = 1; - leafComponentType = elementClass; +#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) /* For arrays of valueType elements (where componentType is a valuetype), the arrays themselves are not * valuetypes but they should inherit the layout characteristics (ie. flattenable, etc.) * of the valuetype elements. A 2D (or more) array of valuetype elements (where leafComponentType is a Valuetype but @@ -3434,7 +3453,19 @@ internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *clas * properties from the leafComponentType. A 2D array is an array of references so it can never be flattened, however, its * elements may be flattened arrays. */ + U_32 arrayFlags = J9ClassLargestAlignmentConstraintReference | J9ClassLargestAlignmentConstraintDouble; + if (J9_ARE_ALL_BITS_SET(options, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY) + && J9_ARE_ALL_BITS_SET(javaVM->extendedRuntimeFlags2, J9_EXTENDED_RUNTIME2_ENABLE_VT_ARRAY_FLATTENING) + ) { + arrayFlags |= J9ClassIsFlattened; + } ramArrayClass->classFlags |= (elementClass->classFlags & arrayFlags); + if (J9_ARE_ALL_BITS_SET(options, J9_FINDCLASS_FLAG_CLASS_OPTION_NULL_RESTRICTED_ARRAY)) { + ramArrayClass->classFlags |= J9ClassArrayIsNullRestricted; + } +#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ + arity = 1; + leafComponentType = elementClass; } ramArrayClass->classFlags |= J9ClassHasIdentity; ramArrayClass->leafComponentType = leafComponentType; @@ -3458,7 +3489,7 @@ internalCreateRAMClassFromROMClassImpl(J9VMThread *vmThread, J9ClassLoader *clas J9ARRAYCLASS_SET_STRIDE(ramClass, J9_VALUETYPE_FLATTENED_SIZE(elementClass)); } } else { - if (J9_IS_J9CLASS_PRIMITIVE_VALUETYPE(elementClass)) { + if (J9_IS_J9CLASS_ALLOW_DEFAULT_VALUE(elementClass)) { ramArrayClass->classFlags |= J9ClassContainsUnflattenedFlattenables; } J9ARRAYCLASS_SET_STRIDE(ramClass, (((UDATA) 1) << (((J9ROMArrayClass*)romClass)->arrayShape & 0x0000FFFF))); @@ -3651,7 +3682,7 @@ internalCreateRAMClassFromROMClass(J9VMThread *vmThread, J9ClassLoader *classLoa #endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ ) { #if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) - if (flattenedClassCache != (J9FlattenedClassCache *) flattenedClassCacheBuffer) { + if (flattenedClassCache != (J9FlattenedClassCache *)flattenedClassCacheBuffer) { j9mem_free_memory(flattenedClassCache); } #endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */ diff --git a/runtime/vm/intfunc.c b/runtime/vm/intfunc.c index 03853069834..d6b8833857e 100644 --- a/runtime/vm/intfunc.c +++ b/runtime/vm/intfunc.c @@ -80,6 +80,7 @@ J9InternalVMFunctions J9InternalFunctions = { sendInit, internalAcquireVMAccessNoMutex, internalCreateArrayClass, + internalCreateArrayClassWithOptions, attachSystemDaemonThread, internalAcquireVMAccessClearStatus, #if defined(J9VM_OPT_REFLECT) diff --git a/test/functional/Valhalla/playlist.xml b/test/functional/Valhalla/playlist.xml index e6acf1c8c48..4b3e1046a20 100644 --- a/test/functional/Valhalla/playlist.xml +++ b/test/functional/Valhalla/playlist.xml @@ -98,11 +98,6 @@ ValueTypeArrayTests - - - https://github.com/eclipse-openj9/openj9/issues/19460 - - -Xgcpolicy:optthruput -Xgcpolicy:optthruput -XX:ValueTypeFlatteningThreshold=99999 -XX:-EnableArrayFlattening @@ -142,7 +137,7 @@ ValueTypeArrayTestsJIT - https://github.com/eclipse-openj9/openj9/issues/19460 + https://github.com/eclipse-openj9/openj9/issues/19913 @@ -340,16 +335,12 @@ ValueTypeSystemArraycopyTests - - - https://github.com/eclipse-openj9/openj9/issues/19460 - - -Xint -Xint -Xgcpolicy:optthruput -XX:ValueTypeFlatteningThreshold=99999 -Xint -Xgcpolicy:optthruput -XX:ValueTypeFlatteningThreshold=99999 -XX:+EnableArrayFlattening - -Xjit:count=0 + + - -Xjit:count=1,disableAsyncCompilation,initialOptLevel=warm -Xgcpolicy:optthruput + $(JAVA_COMMAND) $(JVM_OPTIONS) \ --enable-preview \ diff --git a/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeArrayTests.java b/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeArrayTests.java index 86849f0a4c9..764d820624a 100644 --- a/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeArrayTests.java +++ b/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeArrayTests.java @@ -23,8 +23,13 @@ import org.testng.Assert; import static org.testng.Assert.*; + +import java.lang.reflect.Array; + import org.testng.annotations.Test; +import jdk.internal.value.CheckedType; +import jdk.internal.value.NormalCheckedType; import jdk.internal.value.NullRestrictedCheckedType; import jdk.internal.value.ValueClass; import jdk.internal.vm.annotation.ImplicitlyConstructible; @@ -228,7 +233,7 @@ static void assignDispatch(Object[] arr, int idx, Object src, int arrKind, int s new Class[] {null, null, null, null, null}, // All values can be assigned to Object[] new Class[] {null, ASE, null, null, null}, // ASE for SomeIface[] = Object new Class[] {null, ASE, ASE, null, ASE}, // ASE for PointV[] = PointPV, SomeIface - new Class[] {NPE, ASE, ASE, ASE, null}, // NPE for PointPV[] = null; ASE for PointPV[] = PointV + new Class[] {ASE, ASE, ASE, ASE, null}, // ASE for PointPV[] = null; ASE for PointPV[] = PointV }; /** @@ -328,7 +333,8 @@ static void runTest(Object[] arr, Object sourceVal, int staticArrayKind, int sta */ @Test(priority=1,invocationCount=2) static public void testValueTypeArrayAssignments() throws Throwable { - Object[][] testArrays = new Object[][] {new Object[2], new SomeIface[2], new PointV[2], new PointPV[2]}; + Object[][] testArrays = new Object[][] {new Object[2], new SomeIface[2], new PointV[2], + ValueClass.newArrayInstance(NullRestrictedCheckedType.of(PointPV.class), 2)}; int[] kinds = {OBJ_TYPE, IFACE_TYPE, VAL_TYPE, PRIM_TYPE}; Object[] vals = new Object[] {null, bogusIfaceObj, new PointV(1.0, 2.0), new PointPV(3.0, 4.0)}; @@ -753,11 +759,11 @@ static public void testStoreNullToNullRestrictedArrayElement1() throws Throwable try { arrayElementStoreNull(dstData, ARRAY_LENGTH/2); - } catch (NullPointerException npe) { + } catch (ArrayStoreException ase) { return; /* pass */ } - Assert.fail("Expect a NullPointerException. No exception or wrong kind of exception thrown"); + Assert.fail("Expect an ArrayStoreException. No exception or wrong kind of exception thrown"); } @Test(priority=1,invocationCount=2) @@ -769,10 +775,29 @@ static public void testStoreNullToNullRestrictedArrayElement2() throws Throwable try { arrayElementStore(dstData, ARRAY_LENGTH/2, obj); - } catch (NullPointerException npe) { + } catch (ArrayStoreException ase) { return; /* pass */ } - Assert.fail("Expect a NullPointerException. No exception or wrong kind of exception thrown"); + Assert.fail("Expect an ArrayStoreException. No exception or wrong kind of exception thrown"); + } + + @ImplicitlyConstructible + public static value class EmptyNullRestricted { + } + + /* This test passes with Xint, disable until all cases are passing. */ + /* Test JVM_IsNullRestrictedArray which is called by ValueClass.componentCheckedType */ + @Test + static public void testJVMIsNullRestrictedArray() { + EmptyNullRestricted[] nrArray = (EmptyNullRestricted[])ValueClass.newArrayInstance( + NullRestrictedCheckedType.of(EmptyNullRestricted.class), 4); + CheckedType nrType = ValueClass.componentCheckedType(nrArray); + assertTrue(nrType instanceof NullRestrictedCheckedType); + + EmptyNullRestricted[] normalArray = (EmptyNullRestricted[])ValueClass.newArrayInstance( + NormalCheckedType.of(EmptyNullRestricted.class), 4); + CheckedType normalType = ValueClass.componentCheckedType(normalArray); + assertTrue(normalType instanceof NormalCheckedType); } } diff --git a/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeTests.java b/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeTests.java index 7e8b125200b..943ed42e8fe 100644 --- a/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeTests.java +++ b/test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeTests.java @@ -572,22 +572,6 @@ static public void testDefaultValueWithNonValueType() throws Throwable { } catch (IncompatibleClassChangeError e) {} } - @Test(priority=4, invocationCount=2) - static public void testNullWritesOnNonNullableArrays() throws Throwable { - Object arrayObject = Array.newInstance(point2DClass, 3); - try { - Array.set(arrayObject, 1, null); - Assert.fail("Should throw NPE. Cant write null to arrays of valuetypes"); - } catch(NullPointerException e) {} - - Object arrayObject2 = Array.newInstance(String.class, 3); - try { - Array.set(arrayObject2, 1, null); - } catch(NullPointerException e) { - Assert.fail("Should not throw NPE. Can write null to arrays of identity types"); - } - } - @Test(priority=2, invocationCount=2) static public void testBasicACMPTestOnIdentityTypes() throws Throwable {