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

8273650: [lworld] Interpreter fails with fatal error: DEBUG MESSAGE: klass not initialized #553

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,190 +22,187 @@
*/
package runtime.valhalla.inlinetypes;


import jdk.test.lib.Asserts;

/*
* @test
* @summary Test several scenarios of class initialization failures
* @library /test/lib
* @run main/othervm runtime.valhalla.inlinetypes.ClassInitializationFailuresTest
* @run main runtime.valhalla.inlinetypes.ClassInitializationFailuresTest

*/
public class ClassInitializationFailuresTest {

static boolean failingInitialization = true;
static Object bo = null;

static primitive class BadOne {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
static boolean failingInitialization = true;
static Object bo = null;

static primitive class BadOne {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
}
}
}

static primitive class TestClass1 {
BadOne badField = new BadOne();
}

// Test handling of errors during the initialization of a primitive class
// Initialization of TestClass1 triggers the initialization of classes
// of all its primitive class typed fields, in this case BadOne
// Static initializer of BadOne throws an exception, so BadOne's initialization
// fails, which must caused the initialization of TestClass1 to fail too
// First attempt to instantiate TestClass1 must fail with an ExceptionInInitializerError
// because an exception has been thrown during the initialization process
// Second attempt to instantiate TestClass1 must fail with a NoClassDefFoundError
// because TestClass1 must already be in a faile initialization state (so no new
// attempt to initialize the class)
static void testClassInitialization() {
Throwable e = null;
try {
TestClass1 t1 = new TestClass1();
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Exception should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
Asserts.assertTrue(e.getCause().getClass() == RuntimeException.class, "Must be the exception thown in the static initializer of BadOne");
// Second attempt because it doesn't fail the same way
e = null;
try {
TestClass1 t1 = new TestClass1();
} catch(Throwable t) {
e = t;
static primitive class TestClass1 {
BadOne badField = new BadOne();
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}

static primitive class BadTwo {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
// Test handling of errors during the initialization of a primitive class
// Initialization of TestClass1 triggers the initialization of classes
// of all its primitive class typed fields, in this case BadOne
// Static initializer of BadOne throws an exception, so BadOne's initialization
// fails, which must caused the initialization of TestClass1 to fail too
// First attempt to instantiate TestClass1 must fail with an ExceptionInInitializerError
// because an exception has been thrown during the initialization process
// Second attempt to instantiate TestClass1 must fail with a NoClassDefFoundError
// because TestClass1 must already be in a failed initialization state (so no new
// attempt to initialize the class)
static void testClassInitialization() {
Throwable e = null;
try {
TestClass1 t1 = new TestClass1();
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Exception should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
Asserts.assertTrue(e.getCause().getClass() == RuntimeException.class, "Must be the exception thown in the static initializer of BadOne");
// Second attempt because it doesn't fail the same way
e = null;
try {
TestClass1 t1 = new TestClass1();
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}
}

static primitive class BadThree {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
static primitive class BadTwo {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
}
}
}

// Same test as above, but for arrays of primitive objects
static void testArrayInitialization() {
// Testing anewarray when the primitive element class fails to initialize properly
Throwable e = null;
try {
BadTwo[] array = new BadTwo[10];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
// Second attempt because it doesn't fail the same way
try {
BadTwo[] array = new BadTwo[10];
} catch(Throwable t) {
e = t;
static primitive class BadThree {
int i = 0;
static {
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
}
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
// Testing multianewarray when the primitive element class fails to initialize properly
try {
BadThree[][] array = new BadThree[10][20];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
// Second attempt because it doesn't fail the same way
try {
BadThree[][][] array = new BadThree[10][30][10];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}

static primitive class BadFour {
int i = 0;
static BadFour[] array;
static {
array = new BadFour[10];
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
// Same test as above, but for arrays of primitive objects
static void testArrayInitialization() {
// Testing anewarray when the primitive element class fails to initialize properly
Throwable e = null;
try {
BadTwo[] array = new BadTwo[10];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
// Second attempt because it doesn't fail the same way
try {
BadTwo[] array = new BadTwo[10];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
// Testing multianewarray when the primitive element class fails to initialize properly
try {
BadThree[][] array = new BadThree[10][20];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
// Second attempt because it doesn't fail the same way
try {
BadThree[][][] array = new BadThree[10][30][10];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}
}

// Even if a primitive class fails to initialize property, some instances
// of this class can escape and be accessible. The JVM must be able to
// deal with those instances without crashes. The test below checks that
// escaped values stored in an array are handled correctly
static void testEscapedValueInArray() {
Throwable e = null;
try {
BadFour bt = new BadFour();
} catch (Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error must have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
e = null;
try {
BadFour t = BadFour.array[0];
} catch(Throwable t) {
e = t;
static primitive class BadFour {
int i = 0;
static BadFour[] array;
static {
array = new BadFour[10];
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
}
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}

static primitive class BadFive {
int i = 0;
static {
ClassInitializationFailuresTest.bo = new BadSix();
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
// Even if a primitive class fails to initialize properly, some instances
// of this class can escape and be accessible. The JVM must be able to
// deal with those instances without crashes. The test below checks that
// escaped values stored in an array are handled correctly
static void testEscapedValueInArray() {
Throwable e = null;
try {
BadFour bt = new BadFour();
} catch (Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error must have been thrown");
Asserts.assertTrue(e.getClass() == ExceptionInInitializerError.class, " Must be an ExceptionInInitializerError");
e = null;
try {
BadFour t = BadFour.array[0];
} catch(Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error should have been thrown");
Asserts.assertTrue(e.getClass() == NoClassDefFoundError.class, "Must be a NoClassDefFoundError");
Asserts.assertTrue(e.getCause().getClass() == ExceptionInInitializerError.class, "Must be an ExceptionInInitializerError");
}
}

static class BadSix {
BadFive bf = new BadFive();
}
static primitive class BadFive {
int i = 0;
static {
ClassInitializationFailuresTest.bo = new BadSix();
if (ClassInitializationFailuresTest.failingInitialization) {
throw new RuntimeException("Failing initialization");
}
}
}

// Same test as above, but escaped values are stored in an object
static void testEscapedValueInObject() {
Throwable e = null;
try {
BadSix bt = new BadSix();
} catch (Throwable t) {
e = t;
static class BadSix {
BadFive bf = new BadFive();
}
Asserts.assertNotNull(e, "Error must have been thrown");
Asserts.assertNotNull(ClassInitializationFailuresTest.bo, "bo object should have been set");
BadFive bf = ((BadSix)ClassInitializationFailuresTest.bo).bf;
}

public static void main(String[] args) {
testClassInitialization();
testArrayInitialization();
testEscapedValueInArray();
testEscapedValueInObject();
}
// Same test as above, but escaped values are stored in an object
static void testEscapedValueInObject() {
Throwable e = null;
try {
BadSix bt = new BadSix();
} catch (Throwable t) {
e = t;
}
Asserts.assertNotNull(e, "Error must have been thrown");
Asserts.assertNotNull(ClassInitializationFailuresTest.bo, "bo object should have been set");
BadFive bf = ((BadSix)ClassInitializationFailuresTest.bo).bf;
}

public static void main(String[] args) {
testClassInitialization();
testArrayInitialization();
testEscapedValueInArray();
testEscapedValueInObject();
}
}