8333812: ClassFile.verify() can throw exceptions instead of returning VerifyErrors

Reviewed-by: liach
This commit is contained in:
Adam Sotona 2024-07-19 13:09:45 +00:00
parent 6e9fcc2d80
commit c25c4896ad
4 changed files with 44 additions and 7 deletions
src/java.base/share/classes/jdk/internal/classfile/impl
test/jdk/jdk/classfile

@ -286,7 +286,11 @@ public abstract sealed class BoundAttribute<T extends Attribute<T>>
public BoundLocalVariableTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTableAttribute> mapper, int pos) {
super(cf, mapper, pos);
codeAttribute = (CodeImpl) enclosing;
if (enclosing instanceof CodeImpl ci) {
this.codeAttribute = ci;
} else {
throw new IllegalArgumentException("Invalid LocalVariableTable attribute location");
}
}
@Override
@ -313,7 +317,11 @@ public abstract sealed class BoundAttribute<T extends Attribute<T>>
public BoundLocalVariableTypeTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTypeTableAttribute> mapper, int pos) {
super(cf, mapper, pos);
this.codeAttribute = (CodeImpl) enclosing;
if (enclosing instanceof CodeImpl ci) {
this.codeAttribute = ci;
} else {
throw new IllegalArgumentException("Invalid LocalVariableTypeTable attribute location");
}
}
@Override

@ -132,7 +132,11 @@ public record ClassFileImpl(StackMapsOption stackMapsOption,
@Override
public List<VerifyError> verify(ClassModel model) {
return VerifierImpl.verify(model, classHierarchyResolverOption().classHierarchyResolver(), null);
try {
return VerifierImpl.verify(model, classHierarchyResolverOption().classHierarchyResolver(), null);
} catch (IllegalArgumentException verifierInitializationError) {
return List.of(new VerifyError(verifierInitializationError.getMessage()));
}
}
@Override

@ -114,9 +114,10 @@ public final class VerifierImpl {
}
public static List<VerifyError> verify(ClassModel classModel, ClassHierarchyResolver classHierarchyResolver, Consumer<String> logger) {
var klass = new VerificationWrapper(classModel);
log_info(logger, "Start class verification for: %s", klass.thisClassName());
String clsName = classModel.thisClass().asInternalName();
log_info(logger, "Start class verification for: %s", clsName);
try {
var klass = new VerificationWrapper(classModel);
var errors = new ArrayList<VerifyError>();
errors.addAll(new ParserVerifier(classModel).verify());
if (is_eligible_for_verification(klass)) {
@ -134,7 +135,7 @@ public final class VerifierImpl {
}
return errors;
} finally {
log_info(logger, "End class verification for: %s", klass.thisClassName());
log_info(logger, "End class verification for: %s", clsName);
}
}

@ -24,6 +24,7 @@
/*
* @test
* @summary Testing ClassFile Verifier.
* @bug 8333812
* @enablePreview
* @run junit VerifierSelfTest
*/
@ -46,8 +47,10 @@ import java.lang.classfile.*;
import java.lang.classfile.attribute.*;
import java.lang.classfile.components.ClassPrinter;
import java.lang.constant.ModuleDesc;
import jdk.internal.classfile.impl.DirectClassBuilder;
import jdk.internal.classfile.impl.UnboundAttribute;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
class VerifierSelfTest {
@ -94,6 +97,27 @@ class VerifierSelfTest {
}
}
@Test
void testInvalidAttrLocation() {
var cc = ClassFile.of();
var bytes = cc.build(ClassDesc.of("InvalidAttrLocationClass"), cb ->
((DirectClassBuilder)cb).writeAttribute(new UnboundAttribute.AdHocAttribute<LocalVariableTableAttribute>(Attributes.localVariableTable()) {
@Override
public void writeBody(BufWriter b) {
b.writeU2(0);
}
}));
assertTrue(cc.verify(bytes).stream().anyMatch(e -> e.getMessage().contains("Invalid LocalVariableTable attribute location")));
}
@Test
void testInvalidClassNameEntry() {
var cc = ClassFile.of();
var bytes = cc.parse(new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE,
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});
assertTrue(cc.verify(bytes).stream().anyMatch(e -> e.getMessage().contains("expected ClassEntry")));
}
@Test
void testParserVerification() {
var cc = ClassFile.of();