Skip to content

Commit 4244682

Browse files
committed
8339190: Parameter arrays that are capped during annotation processing report incorrect length
Reviewed-by: vromero
1 parent 7620b12 commit 4244682

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class ClassFile {
103103
public static final int MAX_STACK = 0xffff;
104104

105105
public static final int PREVIEW_MINOR_VERSION = 0xffff;
106+
public static final int MAX_ANNOTATIONS = 0xffff;
106107

107108
public enum Version {
108109
V45_3(45, 3), // base level for all attributes

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,19 @@ void writeCompoundAttribute(Attribute.Compound c) {
649649
databuf.appendChar(poolWriter.putDescriptor(c.type));
650650
databuf.appendChar(c.values.length());
651651
for (Pair<Symbol.MethodSymbol,Attribute> p : c.values) {
652+
checkAnnotationArraySizeInternal(p);
652653
databuf.appendChar(poolWriter.putName(p.fst.name));
653654
p.snd.accept(awriter);
654655
}
655656
}
656657

658+
private void checkAnnotationArraySizeInternal(Pair<Symbol.MethodSymbol, Attribute> p) {
659+
if (p.snd instanceof Attribute.Array arrAttr &&
660+
arrAttr.values.length > ClassFile.MAX_ANNOTATIONS) {
661+
log.error(Errors.AnnotationArrayTooLarge(p.fst.owner));
662+
}
663+
}
664+
657665
void writeTypeAnnotation(Attribute.TypeCompound c) {
658666
writePosition(c.position);
659667
writeCompoundAttribute(c);

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,10 @@ compiler.err.limit.stack=\
887887
compiler.err.limit.string=\
888888
constant string too long
889889

890+
# 0: symbol
891+
compiler.err.annotation.array.too.large=\
892+
Annotation array element too large in \"{0}\"
893+
890894
# 0: string
891895
compiler.err.limit.string.overflow=\
892896
UTF8 representation for string \"{0}...\" is too long for the constant pool
@@ -2235,6 +2239,7 @@ compiler.warn.proc.duplicate.option.name=\
22352239
compiler.warn.proc.duplicate.supported.annotation=\
22362240
Duplicate supported annotation interface ''{0}'' returned by annotation processor ''{1}''
22372241

2242+
22382243
# 0: string
22392244
compiler.warn.proc.redundant.types.with.wildcard=\
22402245
Annotation processor ''{0}'' redundantly supports both ''*'' and other annotation interfaces
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright (c) 2024, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @summary Check if error is thrown if annotation array exceeds limit
27+
* @library /tools/lib
28+
* @run main ParameterArrayLimit
29+
*/
30+
31+
import java.io.BufferedWriter;
32+
import java.io.IOException;
33+
import java.net.URI;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.text.MessageFormat;
38+
import java.util.Collections;
39+
import java.util.List;
40+
import javax.tools.*;
41+
42+
import com.sun.source.util.JavacTask;
43+
44+
public class ParameterArrayLimit {
45+
46+
public static void main(String[] args) throws IOException {
47+
48+
int[] values = new int[]{65536, 65537, 512000};
49+
String[] retPolicies = {"RUNTIME", "CLASS"};
50+
51+
for (var value : values) {
52+
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
53+
54+
for (String retPolicy : retPolicies) {
55+
String className = MessageFormat.format("ClassAnnotationWithLength_{0,number,#}_{1}.java",
56+
value,
57+
retPolicy);
58+
Path out = tmpDir.resolve(className);
59+
createAnnotationFile(out, value, retPolicy, false);
60+
checkParamArrayWarning(className, out);
61+
}
62+
63+
for (String retPolicy : retPolicies) {
64+
String className = MessageFormat.format("TypeAnnotationWithLength_{0,number,#}_{1}.java",
65+
value,
66+
retPolicy);
67+
Path out = tmpDir.resolve(className);
68+
createAnnotationFile(out, value, retPolicy, true);
69+
checkParamArrayWarning(className, out);
70+
}
71+
}
72+
}
73+
74+
private static void checkParamArrayWarning(String className, Path out) throws IOException {
75+
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
76+
DiagnosticCollector<JavaFileObject> d = new DiagnosticCollector<>();
77+
JavacTask task = (JavacTask) javaCompiler.getTask(
78+
null,
79+
null,
80+
d,
81+
null,
82+
null,
83+
Collections.singletonList(
84+
SimpleJavaFileObject.forSource(
85+
URI.create("myfo:/" + className),
86+
Files.readString(out)
87+
)));
88+
task.call();
89+
90+
List<Diagnostic<? extends JavaFileObject>> diagnosticList = d.getDiagnostics();
91+
if (diagnosticList.isEmpty()) {
92+
throw new RuntimeException("No diagnostic found");
93+
}
94+
95+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
96+
if (!(diagnostic.getKind() == Diagnostic.Kind.ERROR
97+
&& diagnostic.getCode()
98+
.equals("compiler.err.annotation.array.too.large"))) {
99+
throw new RuntimeException("Unexpected diagnostic: " + diagnostic.getMessage(null));
100+
}
101+
}
102+
}
103+
104+
private static void createAnnotationFile(Path out, int value, String retPolicy, boolean isTypeAnnotation) throws IOException {
105+
StringBuilder sb = new StringBuilder();
106+
107+
if (isTypeAnnotation) {
108+
sb.append(MessageFormat.format("""
109+
import java.lang.annotation.*;
110+
@Retention(RetentionPolicy.{0})
111+
@Target(ElementType.TYPE_USE)
112+
@interface TypeAnno '{'
113+
long[] arr();
114+
'}'
115+
""", retPolicy));
116+
sb.append(MessageFormat.format("""
117+
public class TypeAnnotationWithLength_{0,number,#}_{1}'{'
118+
@TypeAnno(arr = '{'
119+
""", value, retPolicy));
120+
} else {
121+
sb.append(MessageFormat.format("""
122+
import java.lang.annotation.*;
123+
@Retention(RetentionPolicy.{0})
124+
@interface MyCustomAnno '{'
125+
String value() default "default value";
126+
long[] arr();
127+
int count() default 0;
128+
'}'
129+
""", retPolicy));
130+
sb.append(MessageFormat.format("""
131+
public class ClassAnnotationWithLength_{0,number,#}_{1}'{'
132+
@MyCustomAnno(value = "custom", count = 42, arr = '{'
133+
""", value, retPolicy));
134+
}
135+
136+
sb.append("-1,".repeat(Math.max(0, value - 1)));
137+
sb.append("-1})");
138+
139+
sb.append("""
140+
static int x = 3;
141+
142+
public void myAnnotatedMethod() { }
143+
}
144+
""");
145+
146+
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(out)) {
147+
bufferedWriter.write(sb.toString());
148+
}
149+
}
150+
}

test/langtools/tools/javac/diags/examples.not-yet.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ compiler.err.limit.code # Code
1616
compiler.err.limit.code.too.large.for.try.stmt # Gen
1717
compiler.err.limit.dimensions # Gen
1818
compiler.err.limit.locals # Code
19+
compiler.err.annotation.array.too.large # Code
1920
compiler.err.limit.parameters # Gen
2021
compiler.err.limit.pool # Gen,JavaCompiler
2122
compiler.err.limit.pool.in.class # UNUSED?

0 commit comments

Comments
 (0)