-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-49481] Remove explicit null check exceptions during generation of…
… checkcast and instanceof. PullRequest: graal/16355
- Loading branch information
Showing
3 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
...r/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/CheckCastWithNullTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright (c) 2023, 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 | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
package jdk.graal.compiler.core.test; | ||
|
||
import org.junit.Test; | ||
import jdk.graal.compiler.nodes.java.InstanceOfNode; | ||
import jdk.vm.ci.code.InstalledCode; | ||
import jdk.vm.ci.code.InvalidInstalledCodeException; | ||
|
||
/** | ||
* Tests the correct behavior of (optimized) type casts with {@code null} values. Check casts use | ||
* {@code instanceof} guards. While casts of null values always succeed, {@code instanceof} yields | ||
* {@code false} for {@code null} values. {@link InstanceOfNode#createAllowNull} would be needed to | ||
* create {@code instanceof} checks which yield {@code true} for {@code null} values. Explicit | ||
* {@code null} checks are not allowed, as they can result in wrongly thrown | ||
* {@code NullPointerExceptions} in user code. | ||
*/ | ||
public class CheckCastWithNullTest extends GraalCompilerTest { | ||
|
||
public static class A { | ||
} | ||
|
||
static class B extends A { | ||
} | ||
|
||
static class C { | ||
} | ||
|
||
public static final A NULL = null; | ||
|
||
/* | ||
* Snippets are duplicated to have different profiles. | ||
*/ | ||
public static int snippetNoProfile(Object o) { | ||
A a = (A) o; | ||
return a == null ? 1 : 2; | ||
} | ||
|
||
public static int snippetWithSingleTypeProfile(Object o) { | ||
A a = (A) o; | ||
return a == null ? 1 : 2; | ||
} | ||
|
||
public static int snippetWithMultipleTypeProfile(Object o) { | ||
A a = (A) o; | ||
return a == null ? 1 : 2; | ||
} | ||
|
||
public static int snippetNullSeen(Object o) { | ||
A a = (A) o; | ||
return a == null ? 1 : 2; | ||
} | ||
|
||
public static int snippetExceptionSeen(Object o) { | ||
A a = (A) o; | ||
return a == null ? 1 : 2; | ||
} | ||
|
||
@Test | ||
public void testNoProfile() throws InvalidInstalledCodeException { | ||
resetCache(); | ||
// compile without type profile | ||
InstalledCode c = getCode(getResolvedJavaMethod("snippetNoProfile"), null, true, false, getInitialOptions()); | ||
// null not seen until now | ||
assert c.executeVarargs(NULL).equals(1); | ||
assert c.isValid(); | ||
} | ||
|
||
@Test | ||
public void testWithSingleTypeProfile() throws InvalidInstalledCodeException { | ||
resetCache(); | ||
for (int i = 0; i < 10000; i++) { | ||
test("snippetWithSingleTypeProfile", new B()); | ||
} | ||
// compiles with single type profile | ||
InstalledCode c = getCode(getResolvedJavaMethod("snippetWithSingleTypeProfile"), null, true, false, getInitialOptions()); | ||
// null not seen until now | ||
assert c.executeVarargs(NULL).equals(1); | ||
assert !c.isValid(); | ||
} | ||
|
||
@Test | ||
public void testWithMultipleTypeProfile() throws InvalidInstalledCodeException { | ||
resetCache(); | ||
for (int i = 0; i < 10000; i++) { | ||
test("snippetWithMultipleTypeProfile", new B()); | ||
test("snippetWithMultipleTypeProfile", new A()); | ||
} | ||
// compiles with multiple type profile | ||
InstalledCode c = getCode(getResolvedJavaMethod("snippetWithMultipleTypeProfile"), null, true, false, getInitialOptions()); | ||
// null not seen until now | ||
assert c.executeVarargs(NULL).equals(1); | ||
assert !c.isValid(); | ||
} | ||
|
||
@Test | ||
public void testNullSeen() throws InvalidInstalledCodeException { | ||
resetCache(); | ||
for (int i = 0; i < 10000; i++) { | ||
test("snippetNullSeen", new B()); | ||
test("snippetNullSeen", NULL); | ||
} | ||
// compiles with null seen in profile | ||
InstalledCode c = getCode(getResolvedJavaMethod("snippetNullSeen"), null, true, false, getInitialOptions()); | ||
// null not seen until now | ||
assert c.executeVarargs(NULL).equals(1); | ||
assert c.isValid(); | ||
} | ||
|
||
@Test | ||
public void testExceptionSeen() throws InvalidInstalledCodeException { | ||
resetCache(); | ||
for (int i = 0; i < 10000; i++) { | ||
test("snippetExceptionSeen", new B()); | ||
} | ||
// trigger deopt because of ClassCastException for C | ||
test("snippetExceptionSeen", new C()); | ||
// compiles with exception seen | ||
InstalledCode c = getCode(getResolvedJavaMethod("snippetExceptionSeen"), null, true, false, getInitialOptions()); | ||
// null not seen until now | ||
assert c.executeVarargs(NULL).equals(1); | ||
assert !c.isValid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters