Skip to content

Instantly share code, notes, and snippets.

@marchof
Created October 11, 2018 14:20
Show Gist options
  • Save marchof/6561c2344c4b284582dc4c778590ca72 to your computer and use it in GitHub Desktop.
Save marchof/6561c2344c4b284582dc4c778590ca72 to your computer and use it in GitHub Desktop.
Reproducer for jacoco/jacoco#767
/*******************************************************************************
* Copyright (c) 2009, 2018 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:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.core.internal.instr;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.IRuntime;
import org.jacoco.core.runtime.RuntimeData;
import org.jacoco.core.runtime.SystemPropertiesRuntime;
import org.jacoco.core.test.TargetLoader;
import org.junit.Test;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
* Reproducer for the case where the last local variable slot used by method
* parameters is overwritten with a two slot variable. This damages the next
* variable containing the probe array.
*/
public class MethodParameterOverwriteTest {
@Test
public void run_original() throws Exception {
byte[] def = generateMethod();
TargetLoader loader = new TargetLoader();
loader.add("Sample", def).newInstance();
}
@Test
public void run_instrumented() throws Exception {
IRuntime rt = new SystemPropertiesRuntime();
rt.startup(new RuntimeData());
Instrumenter instr = new Instrumenter(rt);
byte[] def = instr.instrument(generateMethod(), "Sample");
TargetLoader loader = new TargetLoader();
loader.add("Sample", def).newInstance();
}
private byte[] generateMethod() throws Exception {
final ClassWriter writer = new ClassWriter(0);
writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null,
"java/lang/Object", new String[0]);
MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
"()V", null, new String[0]);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>",
"()V", false);
// Put a long value (2 slots) on position 0, overwriting 'this'
mv.visitLdcInsn(Long.valueOf(42));
mv.visitVarInsn(Opcodes.LSTORE, 0);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
writer.visitEnd();
return writer.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment