From 451cc239050f097060be927171fe0e46962f3356 Mon Sep 17 00:00:00 2001 From: Adam Sotona Date: Tue, 21 May 2024 07:59:33 +0000 Subject: [PATCH] 8332486: ClassFile API ArrayIndexOutOfBoundsException with label metadata Reviewed-by: psandoz --- .../jdk/internal/classfile/impl/CodeImpl.java | 3 +++ test/jdk/jdk/classfile/LimitsTest.java | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java index 5be0715ec2f92..4eba39989f62c 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java @@ -221,6 +221,9 @@ private int adjustForObjectOrUninitialized(int bci) { } private void inflateLabel(int bci) { + if (bci < 0 || bci > codeLength) + throw new IllegalArgumentException(String.format("Bytecode offset out of range; bci=%d, codeLength=%d", + bci, codeLength)); if (labels[bci] == null) labels[bci] = new LabelImpl(this, bci); } diff --git a/test/jdk/jdk/classfile/LimitsTest.java b/test/jdk/jdk/classfile/LimitsTest.java index ff090278ceb68..a94a56ce33ff3 100644 --- a/test/jdk/jdk/classfile/LimitsTest.java +++ b/test/jdk/jdk/classfile/LimitsTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8320360 8330684 8331320 8331655 8331940 + * @bug 8320360 8330684 8331320 8331655 8331940 8332486 * @summary Testing ClassFile limits. * @run junit LimitsTest */ @@ -37,8 +37,11 @@ import java.lang.classfile.attribute.CodeAttribute; import java.lang.classfile.attribute.LineNumberInfo; import java.lang.classfile.attribute.LineNumberTableAttribute; +import java.lang.classfile.attribute.LocalVariableInfo; +import java.lang.classfile.attribute.LocalVariableTableAttribute; import java.lang.classfile.constantpool.ConstantPoolException; import java.lang.classfile.constantpool.IntegerEntry; +import java.lang.classfile.instruction.LocalVariable; import java.util.List; import jdk.internal.classfile.impl.DirectCodeBuilder; import jdk.internal.classfile.impl.DirectMethodBuilder; @@ -175,4 +178,16 @@ void testLineNumberOutOfBounds() { .writeAttribute(LineNumberTableAttribute.of(List.of(LineNumberInfo.of(500, 0)))) ))).methods().get(0).code().get().elementList()); } + + @Test + void testLocalVariableOutOfBounds() { + assertThrows(IllegalArgumentException.class, () -> + ClassFile.of().parse(ClassFile.of().build(ClassDesc.of("LocalVariableClass"), cb -> cb.withMethodBody( + "localVariableMethod", MethodTypeDesc.of(ConstantDescs.CD_void), 0, cob -> ((DirectCodeBuilder)cob + .return_()) + .writeAttribute(LocalVariableTableAttribute.of(List.of( + new UnboundAttribute.UnboundLocalVariableInfo(0, 200, + cob.constantPool().utf8Entry("a"), cob.constantPool().utf8Entry("A"), 0)))) + ))).methods().get(0).code().get().elementList()); + } }