Path: blob/master/test/functional/cmdLineTests/doProtectedAccessCheck/src/javaagent/ProtectedClassFileTransformer.java
6004 views
/*******************************************************************************1* Copyright (c) 2016, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package javaagent;2223import java.lang.instrument.ClassFileTransformer;24import java.lang.instrument.IllegalClassFormatException;25import java.lang.instrument.UnmodifiableClassException;26import java.security.ProtectionDomain;27import java.util.Random;2829import org.objectweb.asm.ClassWriter;30import org.objectweb.asm.Label;31import org.objectweb.asm.MethodVisitor;32import org.objectweb.asm.Opcodes;3334public class ProtectedClassFileTransformer implements ClassFileTransformer,35Opcodes {3637@Override38public byte[] transform(ClassLoader loader, String className,39Class<?> classBeingRedefined, ProtectionDomain protectionDomain,40byte[] classfileBuffer) throws IllegalClassFormatException {4142/*43* When multiple classes are retransformed at the same time via JVMTI44* the verifier will not see all new class data while each new class is45* being verified. So if we redefine A and B, and verifying A required46* looking at B, we'll see the old B.47*48* This test retransforms 2 classes (A and B) in the same call, but49* modifies A such that B should fail verification.50*51* See JAZZ103 40748 and CMVC 19346952*/5354if (className.equals("b/B")) {55JavaAgent.transformCount += 1;56}5758if (JavaAgent.transformCount > 1) {5960/*61* This is an asmified version of62* VM_Test/VM/cmdLineTester_doProtectedAccessCheck/src/a/A.java63* except the foo method is changed from public to protected.64*/65if (className.equals("a/A")) {66ClassWriter cw = new ClassWriter(0);67MethodVisitor mv;6869cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "a/A", null,70"java/lang/Object", null);7172{73mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);74mv.visitCode();75mv.visitVarInsn(ALOAD, 0);76mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",77"<init>", "()V");78mv.visitInsn(RETURN);79mv.visitMaxs(1, 1);80mv.visitEnd();81}82{83mv = cw.visitMethod(ACC_PROTECTED, "foo", "()V", null, null);84mv.visitCode();85mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",86"Ljava/io/PrintStream;");87mv.visitLdcInsn("retransformed A");88mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",89"println", "(Ljava/lang/String;)V");90mv.visitInsn(RETURN);91mv.visitMaxs(2, 1);92mv.visitEnd();9394}95cw.visitEnd();9697return cw.toByteArray();98}99100/*101* This is an asmified version of102* VM_Test/VM/cmdLineTester_doProtectedAccessCheck/src/b/B.java103*/104if (className.equals("b/B")) {105ClassWriter cw = new ClassWriter(0);106MethodVisitor mv;107108cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "b/B", null, "a/A", null);109110{111mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);112mv.visitCode();113mv.visitVarInsn(ALOAD, 0);114mv.visitMethodInsn(INVOKESPECIAL, "a/A", "<init>", "()V");115mv.visitInsn(RETURN);116mv.visitMaxs(1, 1);117mv.visitEnd();118}119{120mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main",121"([Ljava/lang/String;)V", null, null);122mv.visitCode();123mv.visitTypeInsn(NEW, "a/A");124mv.visitInsn(DUP);125mv.visitMethodInsn(INVOKESPECIAL, "a/A", "<init>", "()V");126mv.visitVarInsn(ASTORE, 1);127mv.visitVarInsn(ALOAD, 1);128mv.visitMethodInsn(INVOKEVIRTUAL, "a/A", "foo", "()V");129mv.visitInsn(RETURN);130mv.visitMaxs(2, 2);131mv.visitEnd();132}133134cw.visitEnd();135136return cw.toByteArray();137}138}139140return classfileBuffer;141}142}143144145