Skip to content

Commit

Permalink
8338661: StackMapTable is invalid if frames appear in dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
asotona committed Aug 20, 2024
1 parent 54123c8 commit a5989c9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void writeTo(BufWriterImpl buf) {
Util.writeAttributes(buf, attributes);
}

@SuppressWarnings("unchecked")
<A> A get(AttributeMapper<A> am) {
for (Attribute<?> a : attributes)
if (a.attributeMapper() == am)
return (A)a;
return null;
}

boolean isPresent(AttributeMapper<?> am) {
for (Attribute<?> a : attributes)
if (a.attributeMapper() == am)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@
*/
package jdk.internal.classfile.impl;

import java.lang.classfile.Attributes;
import java.lang.classfile.TypeKind;
import java.lang.classfile.attribute.StackMapFrameInfo;
import java.lang.classfile.attribute.StackMapTableAttribute;
import java.lang.classfile.constantpool.ConstantDynamicEntry;
import java.lang.classfile.constantpool.DynamicConstantPoolEntry;
import java.lang.classfile.constantpool.MemberRefEntry;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.BitSet;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

import static java.lang.classfile.ClassFile.*;
import java.lang.constant.ClassDesc;
import java.util.stream.Collectors;

public final class StackCounter {

Expand All @@ -47,6 +50,7 @@ private record Target(int bci, int stack) {}
static StackCounter of(DirectCodeBuilder dcb, BufWriterImpl buf) {
return new StackCounter(
dcb,
dcb.attributes.get(Attributes.stackMapTable()),
buf.thisClass().asSymbol(),
dcb.methodInfo.methodName().stringValue(),
dcb.methodInfo.methodTypeSymbol(),
Expand Down Expand Up @@ -97,6 +101,7 @@ private boolean next() {
}

public StackCounter(LabelContext labelContext,
StackMapTableAttribute smta,
ClassDesc thisClass,
String methodName,
MethodTypeDesc methodDesc,
Expand All @@ -111,8 +116,20 @@ public StackCounter(LabelContext labelContext,
this.bytecode = bytecode;
this.cp = cp;
targets = new ArrayDeque<>();
maxStack = stack = rets = 0;
stack = rets = 0;
maxStack = handlers.isEmpty() ? 0 : 1;
for (var h : handlers) targets.add(new Target(labelContext.labelToBci(h.handler), 1));
if (smta != null) {
for (var smfi : smta.entries()) {
int frameStack = smfi.stack().size();
for (var vti : smfi.stack()) {
if (vti == StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG
|| vti == StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_DOUBLE) frameStack++;
}
if (maxStack < frameStack) maxStack = frameStack;
targets.add(new Target(labelContext.labelToBci(smfi.target()), frameStack));
}
}
maxLocals = isStatic ? 0 : 1;
maxLocals += Util.parameterSlots(methodDesc);
bcs = new RawBytecodeHelper(bytecode);
Expand Down
40 changes: 34 additions & 6 deletions test/jdk/jdk/classfile/StackMapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@
/*
* @test
* @summary Testing Classfile stack maps generator.
* @bug 8305990 8320222 8320618 8335475 8338623
* @bug 8305990 8320222 8320618 8335475 8338623 8338661
* @build testdata.*
* @run junit StackMapsTest
*/

import java.lang.classfile.*;
import java.lang.classfile.attribute.CodeAttribute;
import java.lang.classfile.attribute.StackMapFrameInfo;
import java.lang.classfile.attribute.StackMapTableAttribute;
import java.lang.classfile.components.ClassPrinter;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import java.lang.reflect.AccessFlag;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
Expand All @@ -45,11 +52,6 @@
import static java.lang.classfile.ClassFile.ACC_STATIC;
import static java.lang.constant.ConstantDescs.*;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import java.lang.reflect.AccessFlag;

/**
* StackMapsTest
*/
Expand Down Expand Up @@ -368,4 +370,30 @@ void testInvocationCounters(ClassFile.StackMapsOption option) {
assertEquals(1, code.maxStack());
}
}

@Test
void testDeadCodeCountersWithCustomSMTA() {
ClassDesc bar = ClassDesc.of("Bar");
byte[] bytes = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS).build(bar, clb -> clb
.withMethodBody(
"foo", MethodTypeDesc.of(ConstantDescs.CD_long), ACC_STATIC, cob -> {
cob.lconst_0().lreturn();
Label f2 = cob.newBoundLabel();
cob.lstore(0);
Label f3 = cob.newBoundLabel();
cob.lload(0).lreturn().with(
StackMapTableAttribute.of(List.of(
StackMapFrameInfo.of(f2,
List.of(),
List.of(StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG)),
StackMapFrameInfo.of(f3,
List.of(StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG),
List.of()))));
}
));
assertEmpty(ClassFile.of().verify(bytes));
var code = (CodeAttribute) ClassFile.of().parse(bytes).methods().getFirst().code().orElseThrow();
assertEquals(2, code.maxLocals());
assertEquals(2, code.maxStack());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void benchmarkStackMapsGenerator(Blackhole bh) {
public void benchmarkStackCounter(Blackhole bh) {
for (var d : data) bh.consume(new StackCounter(
d.labelContext(),
null,
d.thisClass(),
d.methodName(),
d.methodDesc(),
Expand Down

0 comments on commit a5989c9

Please sign in to comment.