Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/doProtectedAccessCheck/src/javaagent/ProtectedClassFileTransformer.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2016, 2020 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
package javaagent;
23
24
import java.lang.instrument.ClassFileTransformer;
25
import java.lang.instrument.IllegalClassFormatException;
26
import java.lang.instrument.UnmodifiableClassException;
27
import java.security.ProtectionDomain;
28
import java.util.Random;
29
30
import org.objectweb.asm.ClassWriter;
31
import org.objectweb.asm.Label;
32
import org.objectweb.asm.MethodVisitor;
33
import org.objectweb.asm.Opcodes;
34
35
public class ProtectedClassFileTransformer implements ClassFileTransformer,
36
Opcodes {
37
38
@Override
39
public byte[] transform(ClassLoader loader, String className,
40
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
41
byte[] classfileBuffer) throws IllegalClassFormatException {
42
43
/*
44
* When multiple classes are retransformed at the same time via JVMTI
45
* the verifier will not see all new class data while each new class is
46
* being verified. So if we redefine A and B, and verifying A required
47
* looking at B, we'll see the old B.
48
*
49
* This test retransforms 2 classes (A and B) in the same call, but
50
* modifies A such that B should fail verification.
51
*
52
* See JAZZ103 40748 and CMVC 193469
53
*/
54
55
if (className.equals("b/B")) {
56
JavaAgent.transformCount += 1;
57
}
58
59
if (JavaAgent.transformCount > 1) {
60
61
/*
62
* This is an asmified version of
63
* VM_Test/VM/cmdLineTester_doProtectedAccessCheck/src/a/A.java
64
* except the foo method is changed from public to protected.
65
*/
66
if (className.equals("a/A")) {
67
ClassWriter cw = new ClassWriter(0);
68
MethodVisitor mv;
69
70
cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "a/A", null,
71
"java/lang/Object", null);
72
73
{
74
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
75
mv.visitCode();
76
mv.visitVarInsn(ALOAD, 0);
77
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",
78
"<init>", "()V");
79
mv.visitInsn(RETURN);
80
mv.visitMaxs(1, 1);
81
mv.visitEnd();
82
}
83
{
84
mv = cw.visitMethod(ACC_PROTECTED, "foo", "()V", null, null);
85
mv.visitCode();
86
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
87
"Ljava/io/PrintStream;");
88
mv.visitLdcInsn("retransformed A");
89
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
90
"println", "(Ljava/lang/String;)V");
91
mv.visitInsn(RETURN);
92
mv.visitMaxs(2, 1);
93
mv.visitEnd();
94
95
}
96
cw.visitEnd();
97
98
return cw.toByteArray();
99
}
100
101
/*
102
* This is an asmified version of
103
* VM_Test/VM/cmdLineTester_doProtectedAccessCheck/src/b/B.java
104
*/
105
if (className.equals("b/B")) {
106
ClassWriter cw = new ClassWriter(0);
107
MethodVisitor mv;
108
109
cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "b/B", null, "a/A", null);
110
111
{
112
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
113
mv.visitCode();
114
mv.visitVarInsn(ALOAD, 0);
115
mv.visitMethodInsn(INVOKESPECIAL, "a/A", "<init>", "()V");
116
mv.visitInsn(RETURN);
117
mv.visitMaxs(1, 1);
118
mv.visitEnd();
119
}
120
{
121
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main",
122
"([Ljava/lang/String;)V", null, null);
123
mv.visitCode();
124
mv.visitTypeInsn(NEW, "a/A");
125
mv.visitInsn(DUP);
126
mv.visitMethodInsn(INVOKESPECIAL, "a/A", "<init>", "()V");
127
mv.visitVarInsn(ASTORE, 1);
128
mv.visitVarInsn(ALOAD, 1);
129
mv.visitMethodInsn(INVOKEVIRTUAL, "a/A", "foo", "()V");
130
mv.visitInsn(RETURN);
131
mv.visitMaxs(2, 2);
132
mv.visitEnd();
133
}
134
135
cw.visitEnd();
136
137
return cw.toByteArray();
138
}
139
}
140
141
return classfileBuffer;
142
}
143
}
144
145