50113
|
1 |
/*
|
58678
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.event.io;
|
|
27 |
|
|
28 |
import java.io.File;
|
|
29 |
import java.io.FileInputStream;
|
|
30 |
import java.io.FileOutputStream;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.OutputStream;
|
|
34 |
import java.io.RandomAccessFile;
|
|
35 |
import java.lang.instrument.ClassFileTransformer;
|
|
36 |
import java.lang.instrument.Instrumentation;
|
|
37 |
import java.net.ServerSocket;
|
|
38 |
import java.net.Socket;
|
|
39 |
import java.security.ProtectionDomain;
|
|
40 |
import java.util.concurrent.CountDownLatch;
|
58678
|
41 |
import jdk.test.lib.Utils;
|
50113
|
42 |
|
|
43 |
|
|
44 |
/**
|
|
45 |
* @test
|
|
46 |
* @key jfr
|
|
47 |
* @summary This test runs JFR with a javaagent that reads/writes files and
|
|
48 |
* sockets during every class definition. This is to verify that the i/o
|
|
49 |
* instrumentation in JFR does not interfere with javaagents.
|
51214
|
50 |
* @requires vm.hasJFR
|
50113
|
51 |
*
|
|
52 |
* @library /test/lib /test/jdk
|
|
53 |
* @modules java.instrument
|
|
54 |
*
|
58678
|
55 |
* @build jdk.jfr.event.io.EvilInstrument
|
50113
|
56 |
* @run shell MakeJAR.sh EvilInstrument 'Can-Redefine-Classes: true'
|
|
57 |
* @run main/othervm -javaagent:EvilInstrument.jar jdk.jfr.event.io.EvilInstrument
|
|
58 |
*/
|
|
59 |
|
|
60 |
public class EvilInstrument {
|
|
61 |
|
|
62 |
CountDownLatch socketEchoReady = new CountDownLatch(1);
|
|
63 |
ServerSocket ss;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Thread that echos everything from a socket.
|
|
67 |
*/
|
|
68 |
class SocketEcho extends Thread
|
|
69 |
{
|
|
70 |
public SocketEcho() {
|
|
71 |
setDaemon(true);
|
|
72 |
}
|
|
73 |
|
|
74 |
public void run() {
|
|
75 |
try {
|
|
76 |
Socket s = ss.accept();
|
|
77 |
OutputStream os = s.getOutputStream();
|
|
78 |
InputStream is = s.getInputStream();
|
|
79 |
socketEchoReady.countDown();
|
|
80 |
for(;;) {
|
|
81 |
int b = is.read();
|
|
82 |
os.write(b);
|
|
83 |
}
|
|
84 |
} catch(Exception ex) {
|
|
85 |
ex.printStackTrace();
|
|
86 |
System.exit(1);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
public static File createScratchFile() throws IOException {
|
58678
|
92 |
return Utils.createTempFile("EvilTransformer", null).toFile();
|
50113
|
93 |
}
|
|
94 |
|
|
95 |
class EvilTransformer implements ClassFileTransformer {
|
|
96 |
File scratch;
|
|
97 |
Socket s;
|
|
98 |
volatile boolean inited = false;
|
|
99 |
|
|
100 |
public EvilTransformer() throws Exception {
|
|
101 |
scratch = createScratchFile();
|
|
102 |
ss = new ServerSocket(0);
|
|
103 |
new SocketEcho().start();
|
|
104 |
s = new Socket(ss.getInetAddress(), ss.getLocalPort());
|
|
105 |
socketEchoReady.await();
|
|
106 |
inited = true;
|
|
107 |
}
|
|
108 |
|
|
109 |
public byte[] transform(ClassLoader loader, String className,
|
|
110 |
Class<?> classBeingRedefined,
|
|
111 |
ProtectionDomain protectionDomain,
|
|
112 |
byte[] classfileBuffer)
|
|
113 |
{
|
|
114 |
if (!inited) {
|
|
115 |
return null;
|
|
116 |
}
|
|
117 |
// Do i/o operations during every transform call.
|
|
118 |
try {
|
|
119 |
FileOutputStream fos = new FileOutputStream(scratch);
|
|
120 |
fos.write(31);
|
|
121 |
fos.close();
|
|
122 |
|
|
123 |
FileInputStream fis = new FileInputStream(scratch);
|
|
124 |
fis.read();
|
|
125 |
fis.close();
|
|
126 |
|
|
127 |
RandomAccessFile raf = new RandomAccessFile(scratch, "rw");
|
|
128 |
raf.read();
|
|
129 |
raf.write(31);
|
|
130 |
raf.close();
|
|
131 |
|
|
132 |
s.getOutputStream().write(31);
|
|
133 |
s.getInputStream().read();
|
|
134 |
|
|
135 |
} catch(Exception ex) {
|
|
136 |
ex.printStackTrace();
|
|
137 |
System.exit(1);
|
|
138 |
}
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
public static void premain(String agentArgs, Instrumentation inst) {
|
|
144 |
new EvilInstrument().addTransformer(inst);
|
|
145 |
}
|
|
146 |
|
|
147 |
private void addTransformer(Instrumentation inst) {
|
|
148 |
try {
|
|
149 |
inst.addTransformer(new EvilTransformer());
|
|
150 |
} catch(Exception ex) {
|
|
151 |
ex.printStackTrace();
|
|
152 |
System.exit(1);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
public static void main(String... args) throws Exception {
|
|
157 |
System.out.println("Hello");
|
|
158 |
}
|
|
159 |
|
|
160 |
}
|