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

Nullable array class cannot be cast to null-restricted #20315

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 15 additions & 6 deletions runtime/oti/VMHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,22 @@ class VM_VMHelpers
/* check the [[O -> [[O case. Don't allow [[I -> [[O */
if (instanceArity == castArity) {
J9Class *instanceClassLeafComponent = ((J9ArrayClass*)instanceClass)->leafComponentType;
if (J9CLASS_IS_MIXED(instanceClassLeafComponent)) {
/* we know arities are the same, so skip directly to the terminal case */
instanceClass = instanceClassLeafComponent;
castClass = castClassLeafComponent;
didRetry = true;
goto retry;
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
if (J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(instanceClass)
|| !J9_IS_J9ARRAYCLASS_NULL_RESTRICTED(castClass)
) {
#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */
if (J9CLASS_IS_MIXED(instanceClassLeafComponent)) {
/* we know arities are the same, so skip directly to the terminal case */
instanceClass = instanceClassLeafComponent;
castClass = castClassLeafComponent;
didRetry = true;
goto retry;
}
#if defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES)
}
/* else fail since a nullable array class cannot be cast to a null-restricted class */
#endif /* defined(J9VM_OPT_VALHALLA_FLATTENABLE_VALUE_TYPES) */
}
/* else the arity of the instance wasn't high enough, so we fail */
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,17 @@ static public void testCheckCastNullableTypeOnNull() throws Throwable {
checkCastRefClassOnNull.invoke();
}

@Test(priority=1)
static public void testClassIsInstanceNullableArrays() throws Throwable {
ValueTypePoint2D[] nonNullableArray = (ValueTypePoint2D[]) ValueClass.newNullRestrictedArray(ValueTypePoint2D.class, 1);
ValueTypePoint2D[] nullableArray = new ValueTypePoint2D[1];

assertTrue(nonNullableArray.getClass().isInstance(nonNullableArray));
assertTrue(nullableArray.getClass().isInstance(nullableArray));
assertFalse(nonNullableArray.getClass().isInstance(nullableArray));
assertTrue(nullableArray.getClass().isInstance(nonNullableArray));
}

/*
* Maintain a buffer of flattened arrays with long-aligned valuetypes while keeping a certain amount of classes alive at any
* single time. This forces the GC to unload the classes.
Expand Down