Skip to content

Commit c25c489

Browse files
committed
8333812: ClassFile.verify() can throw exceptions instead of returning VerifyErrors
Reviewed-by: liach
1 parent 6e9fcc2 commit c25c489

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ public static final class BoundLocalVariableTableAttribute
286286

287287
public BoundLocalVariableTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTableAttribute> mapper, int pos) {
288288
super(cf, mapper, pos);
289-
codeAttribute = (CodeImpl) enclosing;
289+
if (enclosing instanceof CodeImpl ci) {
290+
this.codeAttribute = ci;
291+
} else {
292+
throw new IllegalArgumentException("Invalid LocalVariableTable attribute location");
293+
}
290294
}
291295

292296
@Override
@@ -313,7 +317,11 @@ public static final class BoundLocalVariableTypeTableAttribute
313317

314318
public BoundLocalVariableTypeTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTypeTableAttribute> mapper, int pos) {
315319
super(cf, mapper, pos);
316-
this.codeAttribute = (CodeImpl) enclosing;
320+
if (enclosing instanceof CodeImpl ci) {
321+
this.codeAttribute = ci;
322+
} else {
323+
throw new IllegalArgumentException("Invalid LocalVariableTypeTable attribute location");
324+
}
317325
}
318326

319327
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/ClassFileImpl.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ public void accept(ClassBuilder builder) {
132132

133133
@Override
134134
public List<VerifyError> verify(ClassModel model) {
135-
return VerifierImpl.verify(model, classHierarchyResolverOption().classHierarchyResolver(), null);
135+
try {
136+
return VerifierImpl.verify(model, classHierarchyResolverOption().classHierarchyResolver(), null);
137+
} catch (IllegalArgumentException verifierInitializationError) {
138+
return List.of(new VerifyError(verifierInitializationError.getMessage()));
139+
}
136140
}
137141

138142
@Override

src/java.base/share/classes/jdk/internal/classfile/impl/verifier/VerifierImpl.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ public static List<VerifyError> verify(ClassModel classModel, Consumer<String> l
114114
}
115115

116116
public static List<VerifyError> verify(ClassModel classModel, ClassHierarchyResolver classHierarchyResolver, Consumer<String> logger) {
117-
var klass = new VerificationWrapper(classModel);
118-
log_info(logger, "Start class verification for: %s", klass.thisClassName());
117+
String clsName = classModel.thisClass().asInternalName();
118+
log_info(logger, "Start class verification for: %s", clsName);
119119
try {
120+
var klass = new VerificationWrapper(classModel);
120121
var errors = new ArrayList<VerifyError>();
121122
errors.addAll(new ParserVerifier(classModel).verify());
122123
if (is_eligible_for_verification(klass)) {
@@ -134,7 +135,7 @@ public static List<VerifyError> verify(ClassModel classModel, ClassHierarchyReso
134135
}
135136
return errors;
136137
} finally {
137-
log_info(logger, "End class verification for: %s", klass.thisClassName());
138+
log_info(logger, "End class verification for: %s", clsName);
138139
}
139140
}
140141

test/jdk/jdk/classfile/VerifierSelfTest.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @summary Testing ClassFile Verifier.
27+
* @bug 8333812
2728
* @enablePreview
2829
* @run junit VerifierSelfTest
2930
*/
@@ -46,8 +47,10 @@
4647
import java.lang.classfile.attribute.*;
4748
import java.lang.classfile.components.ClassPrinter;
4849
import java.lang.constant.ModuleDesc;
50+
import jdk.internal.classfile.impl.DirectClassBuilder;
51+
import jdk.internal.classfile.impl.UnboundAttribute;
4952
import org.junit.jupiter.api.Test;
50-
import static org.junit.jupiter.api.Assertions.fail;
53+
import static org.junit.jupiter.api.Assertions.*;
5154

5255
class VerifierSelfTest {
5356

@@ -94,6 +97,27 @@ void testFailed() throws IOException {
9497
}
9598
}
9699

100+
@Test
101+
void testInvalidAttrLocation() {
102+
var cc = ClassFile.of();
103+
var bytes = cc.build(ClassDesc.of("InvalidAttrLocationClass"), cb ->
104+
((DirectClassBuilder)cb).writeAttribute(new UnboundAttribute.AdHocAttribute<LocalVariableTableAttribute>(Attributes.localVariableTable()) {
105+
@Override
106+
public void writeBody(BufWriter b) {
107+
b.writeU2(0);
108+
}
109+
}));
110+
assertTrue(cc.verify(bytes).stream().anyMatch(e -> e.getMessage().contains("Invalid LocalVariableTable attribute location")));
111+
}
112+
113+
@Test
114+
void testInvalidClassNameEntry() {
115+
var cc = ClassFile.of();
116+
var bytes = cc.parse(new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE,
117+
0, 0, 0, 0, 0, 2, ClassFile.TAG_INTEGER, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
118+
assertTrue(cc.verify(bytes).stream().anyMatch(e -> e.getMessage().contains("expected ClassEntry")));
119+
}
120+
97121
@Test
98122
void testParserVerification() {
99123
var cc = ClassFile.of();

0 commit comments

Comments
 (0)