Skip to content

Commit

Permalink
Merge pull request #20315 from theresa-m/fix_20305
Browse files Browse the repository at this point in the history
Nullable array class cannot be cast to null-restricted
  • Loading branch information
hangshao0 authored Oct 9, 2024
2 parents 70f99fd + a5105d6 commit 6187621
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
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

0 comments on commit 6187621

Please sign in to comment.