Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8233019: java.lang.Class.isPrimitive() (C1) returns wrong result if K…
…lass* is aligned to 32bit

Reviewed-by: phh
Backport-of: b67ca938f37f952e53f73d2e0b8ebcaf96139fda
  • Loading branch information
Dongbo He authored and RealFYang committed Jul 11, 2022
1 parent e005185 commit 972112e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions hotspot/src/cpu/aarch64/vm/c1_LIRAssembler_aarch64.cpp
Expand Up @@ -1980,6 +1980,9 @@ void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2,
case T_ADDRESS:
imm = opr2->as_constant_ptr()->as_jint();
break;
case T_METADATA:
imm = (intptr_t)(opr2->as_constant_ptr()->as_metadata());
break;
case T_OBJECT:
case T_ARRAY:
imm = jlong(opr2->as_constant_ptr()->as_jobject());
Expand Down
12 changes: 12 additions & 0 deletions hotspot/src/cpu/sparc/vm/c1_LIRAssembler_sparc.cpp
Expand Up @@ -1681,6 +1681,18 @@ void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2,
}
break;

case T_METADATA:
// We only need, for now, comparison with NULL for metadata.
{ assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
Metadata* m = opr2->as_constant_ptr()->as_metadata();
if (m == NULL) {
__ cmp(opr1->as_register(), 0);
} else {
ShouldNotReachHere();
}
}
break;

default:
ShouldNotReachHere();
break;
Expand Down
9 changes: 9 additions & 0 deletions hotspot/src/cpu/x86/vm/c1_LIRAssembler_x86.cpp
Expand Up @@ -2718,6 +2718,15 @@ void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2,
LIR_Const* c = opr2->as_constant_ptr();
if (c->type() == T_INT) {
__ cmpl(reg1, c->as_jint());
} else if (c->type() == T_METADATA) {
// All we need for now is a comparison with NULL for equality.
assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
Metadata* m = c->as_metadata();
if (m == NULL) {
__ cmpptr(reg1, (int32_t)0);
} else {
ShouldNotReachHere();
}
} else if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
// In 64bit oops are single register
jobject o = c->as_jobject();
Expand Down
2 changes: 1 addition & 1 deletion hotspot/src/share/vm/c1/c1_LIRGenerator.cpp
Expand Up @@ -1305,7 +1305,7 @@ void LIRGenerator::do_isPrimitive(Intrinsic* x) {
}

__ move(new LIR_Address(rcvr.result(), java_lang_Class::klass_offset_in_bytes(), T_ADDRESS), temp, info);
__ cmp(lir_cond_notEqual, temp, LIR_OprFact::intConst(0));
__ cmp(lir_cond_notEqual, temp, LIR_OprFact::metadataConst(0));
__ cmove(lir_cond_notEqual, LIR_OprFact::intConst(0), LIR_OprFact::intConst(1), result, T_BOOLEAN);
}

Expand Down
12 changes: 11 additions & 1 deletion hotspot/test/compiler/intrinsics/klass/TestIsPrimitive.java
Expand Up @@ -24,10 +24,12 @@
/*
* @test
* @bug 8150669
* @bug 8233019
* @summary C1 intrinsic for Class.isPrimitive
*
* @run main/othervm -ea -Diters=200 -Xint TestIsPrimitive
* @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=1 TestIsPrimitive
* @run main/othervm -ea -Diters=30000 -XX:-UseSharedSpaces
-XX:TieredStopAtLevel=1 TestIsPrimitive
* @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=4 TestIsPrimitive
*/
import java.lang.reflect.Field;
Expand All @@ -48,6 +50,7 @@ public static void main(String... args) throws Exception {
testOK(true, InlineConstants::testDouble);
testOK(false, InlineConstants::testObject);
testOK(false, InlineConstants::testArray);
testOK(false, InlineConstants::testBooleanArray);

testOK(true, StaticConstants::testBoolean);
testOK(true, StaticConstants::testByte);
Expand All @@ -59,6 +62,7 @@ public static void main(String... args) throws Exception {
testOK(true, StaticConstants::testDouble);
testOK(false, StaticConstants::testObject);
testOK(false, StaticConstants::testArray);
testOK(false, StaticConstants::testBooleanArray);
testNPE( StaticConstants::testNull);

testOK(true, NoConstants::testBoolean);
Expand All @@ -71,6 +75,7 @@ public static void main(String... args) throws Exception {
testOK(true, NoConstants::testDouble);
testOK(false, NoConstants::testObject);
testOK(false, NoConstants::testArray);
testOK(false, NoConstants::testBooleanArray);
testNPE( NoConstants::testNull);
}

Expand Down Expand Up @@ -107,6 +112,7 @@ public static void testNPE(Callable<Object> test) throws Exception {
static volatile Class<?> classObject = Object.class;
static volatile Class<?> classArray = Object[].class;
static volatile Class<?> classNull = null;
static volatile Class<?> classBooleanArray = boolean[].class;

static final Class<?> staticClassBoolean = boolean.class;
static final Class<?> staticClassByte = byte.class;
Expand All @@ -119,6 +125,7 @@ public static void testNPE(Callable<Object> test) throws Exception {
static final Class<?> staticClassObject = Object.class;
static final Class<?> staticClassArray = Object[].class;
static final Class<?> staticClassNull = null;
static final Class<?> staticClassBooleanArray = boolean[].class;

static class InlineConstants {
static boolean testBoolean() { return boolean.class.isPrimitive(); }
Expand All @@ -131,6 +138,7 @@ static class InlineConstants {
static boolean testDouble() { return double.class.isPrimitive(); }
static boolean testObject() { return Object.class.isPrimitive(); }
static boolean testArray() { return Object[].class.isPrimitive(); }
static boolean testBooleanArray() { return boolean[].class.isPrimitive(); }
}

static class StaticConstants {
Expand All @@ -145,6 +153,7 @@ static class StaticConstants {
static boolean testObject() { return staticClassObject.isPrimitive(); }
static boolean testArray() { return staticClassArray.isPrimitive(); }
static boolean testNull() { return staticClassNull.isPrimitive(); }
static boolean testBooleanArray() { return staticClassBooleanArray.isPrimitive(); }
}

static class NoConstants {
Expand All @@ -159,6 +168,7 @@ static class NoConstants {
static boolean testObject() { return classObject.isPrimitive(); }
static boolean testArray() { return classArray.isPrimitive(); }
static boolean testNull() { return classNull.isPrimitive(); }
static boolean testBooleanArray() { return classBooleanArray.isPrimitive(); }
}

}
Expand Down

1 comment on commit 972112e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.