Skip to content

Commit f55ef1e

Browse files
Aditya MandaleekaRealCLanger
authored andcommitted
8210495: compiler crashes because of illegal signature in otherwise legal code
Disable strict verification of compiler signatures when they do not affect generated bytecode Reviewed-by: clanger Backport-of: c0d51dc
1 parent e6d0e9d commit f55ef1e

File tree

3 files changed

+114
-11
lines changed

3 files changed

+114
-11
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,6 +5025,10 @@ protected SignatureGenerator(Types types) {
50255025
this.types = types;
50265026
}
50275027

5028+
protected void reportIllegalSignature(Type t) {
5029+
throw new InvalidSignatureException(t);
5030+
}
5031+
50285032
/**
50295033
* Assemble signature of given type in string buffer.
50305034
*/
@@ -5059,7 +5063,7 @@ public void assembleSig(Type type) {
50595063
break;
50605064
case CLASS:
50615065
if (type.isCompound()) {
5062-
throw new InvalidSignatureException(type);
5066+
reportIllegalSignature(type);
50635067
}
50645068
append('L');
50655069
assembleClassSig(type);
@@ -5104,7 +5108,7 @@ public void assembleSig(Type type) {
51045108
}
51055109
case TYPEVAR:
51065110
if (((TypeVar)type).isCaptured()) {
5107-
throw new InvalidSignatureException(type);
5111+
reportIllegalSignature(type);
51085112
}
51095113
append('T');
51105114
append(type.tsym.name);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
package com.sun.tools.javac.comp;
2727

28+
import com.sun.tools.javac.code.Types.SignatureGenerator.InvalidSignatureException;
29+
import com.sun.tools.javac.resources.CompilerProperties.Errors;
30+
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
2831
import com.sun.tools.javac.tree.*;
2932
import com.sun.tools.javac.tree.JCTree.*;
3033
import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
@@ -2030,7 +2033,7 @@ private String serializedLambdaDisambiguation() {
20302033
owner.type != null ||
20312034
directlyEnclosingLambda() != null);
20322035
if (owner.type != null) {
2033-
buf.append(typeSig(owner.type));
2036+
buf.append(typeSig(owner.type, true));
20342037
buf.append(":");
20352038
}
20362039

@@ -2046,7 +2049,7 @@ private String serializedLambdaDisambiguation() {
20462049
//add captured locals info: type, name, order
20472050
for (Symbol fv : getSymbolMap(CAPTURED_VAR).keySet()) {
20482051
if (fv != self) {
2049-
buf.append(typeSig(fv.type));
2052+
buf.append(typeSig(fv.type, true));
20502053
buf.append(" ");
20512054
buf.append(fv.flatName());
20522055
buf.append(",");
@@ -2435,15 +2438,31 @@ boolean propagateAnnotations() {
24352438
*/
24362439

24372440
private String typeSig(Type type) {
2438-
L2MSignatureGenerator sg = new L2MSignatureGenerator();
2439-
sg.assembleSig(type);
2440-
return sg.toString();
2441+
return typeSig(type, false);
2442+
}
2443+
2444+
private String typeSig(Type type, boolean allowIllegalSignature) {
2445+
try {
2446+
L2MSignatureGenerator sg = new L2MSignatureGenerator(allowIllegalSignature);
2447+
sg.assembleSig(type);
2448+
return sg.toString();
2449+
} catch (InvalidSignatureException ex) {
2450+
Symbol c = attrEnv.enclClass.sym;
2451+
log.error(Errors.CannotGenerateClass(c, Fragments.IllegalSignature(c, ex.type())));
2452+
return "<ERRONEOUS>";
2453+
}
24412454
}
24422455

24432456
private String classSig(Type type) {
2444-
L2MSignatureGenerator sg = new L2MSignatureGenerator();
2445-
sg.assembleClassSig(type);
2446-
return sg.toString();
2457+
try {
2458+
L2MSignatureGenerator sg = new L2MSignatureGenerator(false);
2459+
sg.assembleClassSig(type);
2460+
return sg.toString();
2461+
} catch (InvalidSignatureException ex) {
2462+
Symbol c = attrEnv.enclClass.sym;
2463+
log.error(Errors.CannotGenerateClass(c, Fragments.IllegalSignature(c, ex.type())));
2464+
return "<ERRONEOUS>";
2465+
}
24472466
}
24482467

24492468
/**
@@ -2456,8 +2475,22 @@ private class L2MSignatureGenerator extends Types.SignatureGenerator {
24562475
*/
24572476
StringBuilder sb = new StringBuilder();
24582477

2459-
L2MSignatureGenerator() {
2478+
/**
2479+
* Are signatures incompatible with JVM spec allowed?
2480+
* Used by {@link LambdaTranslationContext#serializedLambdaDisambiguation()}.
2481+
*/
2482+
boolean allowIllegalSignatures;
2483+
2484+
L2MSignatureGenerator(boolean allowIllegalSignatures) {
24602485
super(types);
2486+
this.allowIllegalSignatures = allowIllegalSignatures;
2487+
}
2488+
2489+
@Override
2490+
protected void reportIllegalSignature(Type t) {
2491+
if (!allowIllegalSignatures) {
2492+
super.reportIllegalSignature(t);
2493+
}
24612494
}
24622495

24632496
@Override
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
/*
27+
* @test
28+
* @bug 8210495
29+
* @summary compiler crashes because of illegal signature in otherwise legal code
30+
* @compile T8210495.java
31+
*/
32+
33+
import java.awt.*;
34+
import java.awt.event.ActionListener;
35+
import java.util.List;
36+
37+
class T8210495 {
38+
interface IFilter {
39+
Component getComponent();
40+
}
41+
42+
static class Filter implements IFilter {
43+
@Override
44+
public Component getComponent() {
45+
return null;
46+
}
47+
48+
}
49+
50+
public Component buildFilter(List<? extends Filter> l, Dialog dialog) {
51+
Panel c = new Panel();
52+
l.stream()
53+
.map(f -> {
54+
Button btn = (Button)f.getComponent();
55+
btn.addActionListener((java.io.Serializable & ActionListener)evt -> {
56+
applyFilter(f);
57+
dialog.setVisible(false);
58+
});
59+
return btn;
60+
})
61+
.forEach(c::add);
62+
return c;
63+
}
64+
65+
private void applyFilter(IFilter f) { }
66+
}

0 commit comments

Comments
 (0)