-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use condy for probes array in Java 11+ class files (#845)
- Loading branch information
Showing
7 changed files
with
268 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
org.jacoco.core.test/src/org/jacoco/core/internal/instr/CondyProbeArrayStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.instr; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.objectweb.asm.ConstantDynamic; | ||
import org.objectweb.asm.Handle; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.LdcInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import org.objectweb.asm.tree.TypeInsnNode; | ||
import org.objectweb.asm.tree.VarInsnNode; | ||
|
||
public class CondyProbeArrayStrategyTest { | ||
|
||
private CondyProbeArrayStrategy strategy; | ||
|
||
@Before | ||
public void setup() { | ||
strategy = new CondyProbeArrayStrategy("ClassName", true, 1L, | ||
new OfflineInstrumentationAccessGenerator()); | ||
} | ||
|
||
@Test | ||
public void should_store_instance_using_condy_and_checkcast() { | ||
final MethodNode m = new MethodNode(); | ||
final int maxStack = strategy.storeInstance(m, false, 1); | ||
|
||
assertEquals(1, maxStack); | ||
|
||
final ConstantDynamic constantDynamic = (ConstantDynamic) ((LdcInsnNode) m.instructions | ||
.get(0)).cst; | ||
assertEquals("$jacocoData", constantDynamic.getName()); | ||
assertEquals("Ljava/lang/Object;", constantDynamic.getDescriptor()); | ||
|
||
final Handle bootstrapMethod = constantDynamic.getBootstrapMethod(); | ||
assertEquals(Opcodes.H_INVOKESTATIC, bootstrapMethod.getTag()); | ||
assertEquals("ClassName", bootstrapMethod.getOwner()); | ||
assertEquals("$jacocoInit", bootstrapMethod.getName()); | ||
assertEquals( | ||
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", | ||
bootstrapMethod.getDesc()); | ||
assertTrue(bootstrapMethod.isInterface()); | ||
|
||
final TypeInsnNode castInstruction = (TypeInsnNode) m.instructions | ||
.get(1); | ||
assertEquals(Opcodes.CHECKCAST, castInstruction.getOpcode()); | ||
assertEquals("[Z", castInstruction.desc); | ||
|
||
final VarInsnNode storeInstruction = (VarInsnNode) m.instructions | ||
.get(2); | ||
assertEquals(Opcodes.ASTORE, storeInstruction.getOpcode()); | ||
assertEquals(1, storeInstruction.var); | ||
|
||
assertEquals(3, m.instructions.size()); | ||
} | ||
|
||
@Test | ||
public void should_not_add_fields() { | ||
final ClassNode c = new ClassNode(); | ||
strategy.addMembers(c, 1); | ||
|
||
assertEquals(0, c.fields.size()); | ||
} | ||
|
||
@Test | ||
public void should_add_bootstrap_method() { | ||
final ClassNode c = new ClassNode(); | ||
strategy.addMembers(c, 1); | ||
|
||
assertEquals(1, c.methods.size()); | ||
|
||
final MethodNode m = c.methods.get(0); | ||
assertEquals(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | ||
| Opcodes.ACC_STATIC, m.access); | ||
assertEquals("$jacocoInit", m.name); | ||
assertEquals( | ||
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", | ||
m.desc); | ||
|
||
assertEquals(4, m.maxStack); | ||
assertEquals(3, m.maxLocals); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
org.jacoco.core/src/org/jacoco/core/internal/instr/CondyProbeArrayStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.instr; | ||
|
||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ConstantDynamic; | ||
import org.jacoco.core.runtime.IExecutionDataAccessorGenerator; | ||
import org.objectweb.asm.Handle; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
/** | ||
* This strategy for Java 11+ class files uses {@link ConstantDynamic} to hold | ||
* the probe array and adds bootstrap method requesting the probe array from the | ||
* runtime. | ||
*/ | ||
public class CondyProbeArrayStrategy implements IProbeArrayStrategy { | ||
|
||
/** | ||
* Descriptor of the bootstrap method. | ||
*/ | ||
public static final String B_DESC = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z"; | ||
|
||
private final String className; | ||
|
||
private final boolean isInterface; | ||
|
||
private final long classId; | ||
|
||
private final IExecutionDataAccessorGenerator accessorGenerator; | ||
|
||
CondyProbeArrayStrategy(final String className, final boolean isInterface, | ||
final long classId, | ||
final IExecutionDataAccessorGenerator accessorGenerator) { | ||
this.className = className; | ||
this.isInterface = isInterface; | ||
this.classId = classId; | ||
this.accessorGenerator = accessorGenerator; | ||
} | ||
|
||
public int storeInstance(final MethodVisitor mv, final boolean clinit, | ||
final int variable) { | ||
final Handle bootstrapMethod = new Handle(Opcodes.H_INVOKESTATIC, | ||
className, InstrSupport.INITMETHOD_NAME, B_DESC, isInterface); | ||
// As a workaround for https://bugs.openjdk.java.net/browse/JDK-8216970 | ||
// constant should have type Object | ||
mv.visitLdcInsn(new ConstantDynamic(InstrSupport.DATAFIELD_NAME, | ||
"Ljava/lang/Object;", bootstrapMethod)); | ||
mv.visitTypeInsn(Opcodes.CHECKCAST, "[Z"); | ||
mv.visitVarInsn(Opcodes.ASTORE, variable); | ||
return 1; | ||
} | ||
|
||
public void addMembers(final ClassVisitor cv, final int probeCount) { | ||
final MethodVisitor mv = cv.visitMethod(InstrSupport.INITMETHOD_ACC, | ||
InstrSupport.INITMETHOD_NAME, B_DESC, null, null); | ||
final int maxStack = accessorGenerator.generateDataAccessor(classId, | ||
className, probeCount, mv); | ||
mv.visitInsn(Opcodes.ARETURN); | ||
mv.visitMaxs(maxStack, 3); | ||
mv.visitEnd(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters