author | sspitsyn |
Sat, 09 Jul 2016 21:41:14 -0700 | |
changeset 39739 | 5d058d6bd245 |
parent 36511 | 9d0388c6b336 |
child 40818 | 9c2079cba606 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36511 | 2 |
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* Copyright 2003 Wily Technology, Inc. |
|
28 |
*/ |
|
29 |
||
30 |
#include <jni.h> |
|
36511 | 31 |
#include <jvm.h> |
2 | 32 |
#include <jvmti.h> |
33 |
#include <stdlib.h> |
|
34 |
#include <string.h> |
|
35 |
#include "JPLISAgent.h" |
|
36 |
#include "JPLISAssert.h" |
|
37 |
#include "Utilities.h" |
|
38 |
#include "Reentrancy.h" |
|
39 |
#include "JavaExceptions.h" |
|
40 |
||
41 |
#include "EncodingSupport.h" |
|
1090
c5805b1672a6
6732421: Removed old javavm and Classic VM files from the jdk7 sources
ohair
parents:
715
diff
changeset
|
42 |
#include "FileSystemSupport.h" /* For MAXPATHLEN & uintptr_t */ |
2 | 43 |
|
44 |
#include "sun_instrument_InstrumentationImpl.h" |
|
45 |
||
46 |
/* |
|
47 |
* The JPLISAgent manages the initialization all of the Java programming language Agents. |
|
48 |
* It also supports the native method bridge between the JPLIS and the JVMTI. |
|
49 |
* It maintains a single JVMTI Env that all JPL agents share. |
|
50 |
* It parses command line requests and creates individual Java agents. |
|
51 |
*/ |
|
52 |
||
53 |
||
54 |
/* |
|
55 |
* private prototypes |
|
56 |
*/ |
|
57 |
||
58 |
/* Allocates an unformatted JPLIS agent data structure. Returns NULL if allocation fails. */ |
|
59 |
JPLISAgent * |
|
60 |
allocateJPLISAgent(jvmtiEnv * jvmtiEnv); |
|
61 |
||
62 |
/* Initializes an already-allocated JPLIS agent data structure. */ |
|
63 |
JPLISInitializationError |
|
64 |
initializeJPLISAgent( JPLISAgent * agent, |
|
65 |
JavaVM * vm, |
|
66 |
jvmtiEnv * jvmtienv); |
|
67 |
/* De-allocates a JPLIS agent data structure. Only used in partial-failure cases at startup; |
|
68 |
* in normal usage the JPLIS agent lives forever |
|
69 |
*/ |
|
70 |
void |
|
71 |
deallocateJPLISAgent( jvmtiEnv * jvmtienv, |
|
72 |
JPLISAgent * agent); |
|
73 |
||
74 |
/* Does one-time work to interrogate the JVM about capabilities and cache the answers. */ |
|
75 |
void |
|
76 |
checkCapabilities(JPLISAgent * agent); |
|
77 |
||
78 |
/* Takes the elements of the command string (agent class name and options string) and |
|
79 |
* create java strings for them. |
|
80 |
* Returns true if a classname was found. Makes no promises beyond the textual; says nothing about whether |
|
81 |
* the class exists or can be loaded. |
|
82 |
* If return value is true, sets outputClassname to a non-NULL local JNI reference. |
|
83 |
* If return value is true, sets outputOptionsString either to NULL or to a non-NULL local JNI reference. |
|
84 |
* If return value is false, neither output parameter is set. |
|
85 |
*/ |
|
86 |
jboolean |
|
87 |
commandStringIntoJavaStrings( JNIEnv * jnienv, |
|
88 |
const char * classname, |
|
89 |
const char * optionsString, |
|
90 |
jstring * outputClassname, |
|
91 |
jstring * outputOptionsString); |
|
92 |
||
93 |
/* Start one Java agent from the supplied parameters. |
|
94 |
* Most of the logic lives in a helper function that lives over in Java code-- |
|
95 |
* we pass parameters out to Java and use our own Java helper to actually |
|
96 |
* load the agent and call the premain. |
|
97 |
* Returns true if the Java agent class is loaded and the premain/agentmain method completes |
|
98 |
* with no exceptions, false otherwise. |
|
99 |
*/ |
|
100 |
jboolean |
|
101 |
invokeJavaAgentMainMethod( JNIEnv * jnienv, |
|
102 |
jobject instrumentationImpl, |
|
103 |
jmethodID agentMainMethod, |
|
104 |
jstring className, |
|
105 |
jstring optionsString); |
|
106 |
||
107 |
/* Once we have loaded the Java agent and called the premain, |
|
108 |
* we can release the copies we have been keeping of the command line |
|
109 |
* data (agent class name and option strings). |
|
110 |
*/ |
|
111 |
void |
|
112 |
deallocateCommandLineData(JPLISAgent * agent); |
|
113 |
||
114 |
/* |
|
115 |
* Common support for various class list fetchers. |
|
116 |
*/ |
|
117 |
typedef jvmtiError (*ClassListFetcher) |
|
118 |
( jvmtiEnv * jvmtiEnv, |
|
119 |
jobject classLoader, |
|
120 |
jint * classCount, |
|
121 |
jclass ** classes); |
|
122 |
||
123 |
/* Fetcher that ignores the class loader parameter, and uses the JVMTI to get a list of all classes. |
|
124 |
* Returns a jvmtiError according to the underlying JVMTI service. |
|
125 |
*/ |
|
126 |
jvmtiError |
|
127 |
getAllLoadedClassesClassListFetcher( jvmtiEnv * jvmtiEnv, |
|
128 |
jobject classLoader, |
|
129 |
jint * classCount, |
|
130 |
jclass ** classes); |
|
131 |
||
132 |
/* Fetcher that uses the class loader parameter, and uses the JVMTI to get a list of all classes |
|
133 |
* for which the supplied loader is the initiating loader. |
|
134 |
* Returns a jvmtiError according to the underlying JVMTI service. |
|
135 |
*/ |
|
136 |
jvmtiError |
|
137 |
getInitiatedClassesClassListFetcher( jvmtiEnv * jvmtiEnv, |
|
138 |
jobject classLoader, |
|
139 |
jint * classCount, |
|
140 |
jclass ** classes); |
|
141 |
||
142 |
/* |
|
143 |
* Common guts for two native methods, which are the same except for the policy for fetching |
|
144 |
* the list of classes. |
|
145 |
* Either returns a local JNI reference to an array of references to java.lang.Class. |
|
146 |
* Can throw, if it does will alter the JNIEnv with an outstanding exception. |
|
147 |
*/ |
|
148 |
jobjectArray |
|
149 |
commonGetClassList( JNIEnv * jnienv, |
|
150 |
JPLISAgent * agent, |
|
151 |
jobject classLoader, |
|
152 |
ClassListFetcher fetcher); |
|
153 |
||
154 |
||
155 |
/* |
|
156 |
* Misc. utilities. |
|
157 |
*/ |
|
158 |
||
159 |
/* Checked exception mapper used by the redefine classes implementation. |
|
160 |
* Allows ClassNotFoundException or UnmodifiableClassException; maps others |
|
161 |
* to InternalError. Can return NULL in an error case. |
|
162 |
*/ |
|
163 |
jthrowable |
|
164 |
redefineClassMapper( JNIEnv * jnienv, |
|
165 |
jthrowable throwableToMap); |
|
166 |
||
167 |
/* Turns a buffer of jclass * into a Java array whose elements are java.lang.Class. |
|
168 |
* Can throw, if it does will alter the JNIEnv with an outstanding exception. |
|
169 |
*/ |
|
170 |
jobjectArray |
|
171 |
getObjectArrayFromClasses(JNIEnv* jnienv, jclass* classes, jint classCount); |
|
172 |
||
173 |
||
174 |
JPLISEnvironment * |
|
175 |
getJPLISEnvironment(jvmtiEnv * jvmtienv) { |
|
176 |
JPLISEnvironment * environment = NULL; |
|
177 |
jvmtiError jvmtierror = JVMTI_ERROR_NONE; |
|
178 |
||
179 |
jvmtierror = (*jvmtienv)->GetEnvironmentLocalStorage( |
|
180 |
jvmtienv, |
|
181 |
(void**)&environment); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
182 |
/* can be called from any phase */ |
2 | 183 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
184 |
||
185 |
if (jvmtierror == JVMTI_ERROR_NONE) { |
|
186 |
jplis_assert(environment != NULL); |
|
187 |
jplis_assert(environment->mJVMTIEnv == jvmtienv); |
|
188 |
} else { |
|
189 |
environment = NULL; |
|
190 |
} |
|
191 |
return environment; |
|
192 |
} |
|
193 |
||
194 |
/* |
|
195 |
* OnLoad processing code. |
|
196 |
*/ |
|
197 |
||
198 |
/* |
|
199 |
* Creates a new JPLISAgent. |
|
200 |
* Returns error if the agent cannot be created and initialized. |
|
201 |
* The JPLISAgent* pointed to by agent_ptr is set to the new broker, |
|
202 |
* or NULL if an error has occurred. |
|
203 |
*/ |
|
204 |
JPLISInitializationError |
|
205 |
createNewJPLISAgent(JavaVM * vm, JPLISAgent **agent_ptr) { |
|
206 |
JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE; |
|
207 |
jvmtiEnv * jvmtienv = NULL; |
|
208 |
jint jnierror = JNI_OK; |
|
209 |
||
210 |
*agent_ptr = NULL; |
|
211 |
jnierror = (*vm)->GetEnv( vm, |
|
212 |
(void **) &jvmtienv, |
|
7978
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
213 |
JVMTI_VERSION_1_1); |
2 | 214 |
if ( jnierror != JNI_OK ) { |
215 |
initerror = JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT; |
|
216 |
} else { |
|
217 |
JPLISAgent * agent = allocateJPLISAgent(jvmtienv); |
|
218 |
if ( agent == NULL ) { |
|
219 |
initerror = JPLIS_INIT_ERROR_ALLOCATION_FAILURE; |
|
220 |
} else { |
|
221 |
initerror = initializeJPLISAgent( agent, |
|
222 |
vm, |
|
223 |
jvmtienv); |
|
224 |
if ( initerror == JPLIS_INIT_ERROR_NONE ) { |
|
225 |
*agent_ptr = agent; |
|
226 |
} else { |
|
227 |
deallocateJPLISAgent(jvmtienv, agent); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
/* don't leak envs */ |
|
232 |
if ( initerror != JPLIS_INIT_ERROR_NONE ) { |
|
233 |
jvmtiError jvmtierror = (*jvmtienv)->DisposeEnvironment(jvmtienv); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
234 |
/* can be called from any phase */ |
2 | 235 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
236 |
} |
|
237 |
} |
|
238 |
||
239 |
return initerror; |
|
240 |
} |
|
241 |
||
242 |
/* |
|
243 |
* Allocates a JPLISAgent. Returns NULL if it cannot be allocated |
|
244 |
*/ |
|
245 |
JPLISAgent * |
|
246 |
allocateJPLISAgent(jvmtiEnv * jvmtienv) { |
|
247 |
return (JPLISAgent *) allocate( jvmtienv, |
|
248 |
sizeof(JPLISAgent)); |
|
249 |
} |
|
250 |
||
251 |
JPLISInitializationError |
|
252 |
initializeJPLISAgent( JPLISAgent * agent, |
|
253 |
JavaVM * vm, |
|
254 |
jvmtiEnv * jvmtienv) { |
|
255 |
jvmtiError jvmtierror = JVMTI_ERROR_NONE; |
|
256 |
jvmtiPhase phase; |
|
257 |
||
258 |
agent->mJVM = vm; |
|
259 |
agent->mNormalEnvironment.mJVMTIEnv = jvmtienv; |
|
260 |
agent->mNormalEnvironment.mAgent = agent; |
|
261 |
agent->mNormalEnvironment.mIsRetransformer = JNI_FALSE; |
|
262 |
agent->mRetransformEnvironment.mJVMTIEnv = NULL; /* NULL until needed */ |
|
263 |
agent->mRetransformEnvironment.mAgent = agent; |
|
279
e0911cc0a5d1
6547358: 2/2 j.l.i.: manifest attribute 'Can-Retransform-Classes' is ignored by isRetransformClassesSupported()
dcubed
parents:
2
diff
changeset
|
264 |
agent->mRetransformEnvironment.mIsRetransformer = JNI_FALSE; /* JNI_FALSE until mJVMTIEnv is set */ |
2 | 265 |
agent->mAgentmainCaller = NULL; |
266 |
agent->mInstrumentationImpl = NULL; |
|
267 |
agent->mPremainCaller = NULL; |
|
268 |
agent->mTransform = NULL; |
|
269 |
agent->mRedefineAvailable = JNI_FALSE; /* assume no for now */ |
|
270 |
agent->mRedefineAdded = JNI_FALSE; |
|
271 |
agent->mNativeMethodPrefixAvailable = JNI_FALSE; /* assume no for now */ |
|
272 |
agent->mNativeMethodPrefixAdded = JNI_FALSE; |
|
273 |
agent->mAgentClassName = NULL; |
|
274 |
agent->mOptionsString = NULL; |
|
275 |
||
276 |
/* make sure we can recover either handle in either direction. |
|
277 |
* the agent has a ref to the jvmti; make it mutual |
|
278 |
*/ |
|
279 |
jvmtierror = (*jvmtienv)->SetEnvironmentLocalStorage( |
|
280 |
jvmtienv, |
|
281 |
&(agent->mNormalEnvironment)); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
282 |
/* can be called from any phase */ |
2 | 283 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
284 |
||
285 |
/* check what capabilities are available */ |
|
286 |
checkCapabilities(agent); |
|
287 |
||
288 |
/* check phase - if live phase then we don't need the VMInit event */ |
|
281
3da74b83e387
6642405: 4/4 src/share/instrument/JPLISAgent.c line 286: "==" found where assignment "=" expected
dcubed
parents:
280
diff
changeset
|
289 |
jvmtierror = (*jvmtienv)->GetPhase(jvmtienv, &phase); |
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
290 |
/* can be called from any phase */ |
2 | 291 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
292 |
if (phase == JVMTI_PHASE_LIVE) { |
|
293 |
return JPLIS_INIT_ERROR_NONE; |
|
294 |
} |
|
295 |
||
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
296 |
if (phase != JVMTI_PHASE_ONLOAD) { |
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
297 |
/* called too early or called too late; either way bail out */ |
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
298 |
return JPLIS_INIT_ERROR_FAILURE; |
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
299 |
} |
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
300 |
|
2 | 301 |
/* now turn on the VMInit event */ |
302 |
if ( jvmtierror == JVMTI_ERROR_NONE ) { |
|
303 |
jvmtiEventCallbacks callbacks; |
|
304 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
305 |
callbacks.VMInit = &eventHandlerVMInit; |
|
306 |
||
307 |
jvmtierror = (*jvmtienv)->SetEventCallbacks( jvmtienv, |
|
308 |
&callbacks, |
|
309 |
sizeof(callbacks)); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
310 |
check_phase_ret_blob(jvmtierror, JPLIS_INIT_ERROR_FAILURE); |
2 | 311 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
312 |
} |
|
313 |
||
314 |
if ( jvmtierror == JVMTI_ERROR_NONE ) { |
|
315 |
jvmtierror = (*jvmtienv)->SetEventNotificationMode( |
|
316 |
jvmtienv, |
|
317 |
JVMTI_ENABLE, |
|
318 |
JVMTI_EVENT_VM_INIT, |
|
319 |
NULL /* all threads */); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
320 |
check_phase_ret_blob(jvmtierror, JPLIS_INIT_ERROR_FAILURE); |
2 | 321 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
322 |
} |
|
323 |
||
324 |
return (jvmtierror == JVMTI_ERROR_NONE)? JPLIS_INIT_ERROR_NONE : JPLIS_INIT_ERROR_FAILURE; |
|
325 |
} |
|
326 |
||
327 |
void |
|
328 |
deallocateJPLISAgent(jvmtiEnv * jvmtienv, JPLISAgent * agent) { |
|
329 |
deallocate(jvmtienv, agent); |
|
330 |
} |
|
331 |
||
332 |
||
333 |
JPLISInitializationError |
|
334 |
recordCommandLineData( JPLISAgent * agent, |
|
335 |
const char * agentClassName, |
|
336 |
const char * optionsString ) { |
|
337 |
JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE; |
|
338 |
char * ourCopyOfAgentClassName = NULL; |
|
339 |
char * ourCopyOfOptionsString = NULL; |
|
340 |
||
341 |
/* if no actual params, bail out now */ |
|
342 |
if ((agentClassName == NULL) || (*agentClassName == 0)) { |
|
343 |
initerror = JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED; |
|
344 |
} else { |
|
345 |
ourCopyOfAgentClassName = allocate(jvmti(agent), strlen(agentClassName)+1); |
|
346 |
if (ourCopyOfAgentClassName == NULL) { |
|
347 |
initerror = JPLIS_INIT_ERROR_ALLOCATION_FAILURE; |
|
348 |
} else { |
|
349 |
if (optionsString != NULL) { |
|
350 |
ourCopyOfOptionsString = allocate(jvmti(agent), strlen(optionsString)+1); |
|
351 |
if (ourCopyOfOptionsString == NULL) { |
|
352 |
deallocate(jvmti(agent), ourCopyOfAgentClassName); |
|
353 |
initerror = JPLIS_INIT_ERROR_ALLOCATION_FAILURE; |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
if (initerror == JPLIS_INIT_ERROR_NONE) { |
|
360 |
strcpy(ourCopyOfAgentClassName, agentClassName); |
|
361 |
if (optionsString != NULL) { |
|
362 |
strcpy(ourCopyOfOptionsString, optionsString); |
|
363 |
} |
|
364 |
agent->mAgentClassName = ourCopyOfAgentClassName; |
|
365 |
agent->mOptionsString = ourCopyOfOptionsString; |
|
366 |
} |
|
367 |
||
368 |
return initerror; |
|
369 |
} |
|
370 |
||
371 |
/* |
|
372 |
* VMInit processing code. |
|
373 |
*/ |
|
374 |
||
375 |
||
376 |
/* |
|
377 |
* If this call fails, the JVM launch will ultimately be aborted, |
|
378 |
* so we don't have to be super-careful to clean up in partial failure |
|
379 |
* cases. |
|
380 |
*/ |
|
381 |
jboolean |
|
382 |
processJavaStart( JPLISAgent * agent, |
|
383 |
JNIEnv * jnienv) { |
|
384 |
jboolean result; |
|
385 |
||
386 |
/* |
|
387 |
* OK, Java is up now. We can start everything that needs Java. |
|
388 |
*/ |
|
389 |
||
390 |
/* |
|
391 |
* First make our emergency fallback InternalError throwable. |
|
392 |
*/ |
|
393 |
result = initializeFallbackError(jnienv); |
|
394 |
jplis_assert(result); |
|
395 |
||
396 |
/* |
|
397 |
* Now make the InstrumentationImpl instance. |
|
398 |
*/ |
|
399 |
if ( result ) { |
|
400 |
result = createInstrumentationImpl(jnienv, agent); |
|
401 |
jplis_assert(result); |
|
402 |
} |
|
403 |
||
404 |
||
405 |
/* |
|
406 |
* Then turn off the VMInit handler and turn on the ClassFileLoadHook. |
|
407 |
* This way it is on before anyone registers a transformer. |
|
408 |
*/ |
|
409 |
if ( result ) { |
|
410 |
result = setLivePhaseEventHandlers(agent); |
|
411 |
jplis_assert(result); |
|
412 |
} |
|
413 |
||
414 |
/* |
|
415 |
* Load the Java agent, and call the premain. |
|
416 |
*/ |
|
417 |
if ( result ) { |
|
418 |
result = startJavaAgent(agent, jnienv, |
|
419 |
agent->mAgentClassName, agent->mOptionsString, |
|
420 |
agent->mPremainCaller); |
|
421 |
} |
|
422 |
||
423 |
/* |
|
424 |
* Finally surrender all of the tracking data that we don't need any more. |
|
425 |
* If something is wrong, skip it, we will be aborting the JVM anyway. |
|
426 |
*/ |
|
427 |
if ( result ) { |
|
428 |
deallocateCommandLineData(agent); |
|
429 |
} |
|
430 |
||
431 |
return result; |
|
432 |
} |
|
433 |
||
434 |
jboolean |
|
435 |
startJavaAgent( JPLISAgent * agent, |
|
436 |
JNIEnv * jnienv, |
|
437 |
const char * classname, |
|
438 |
const char * optionsString, |
|
439 |
jmethodID agentMainMethod) { |
|
440 |
jboolean success = JNI_FALSE; |
|
441 |
jstring classNameObject = NULL; |
|
442 |
jstring optionsStringObject = NULL; |
|
443 |
||
444 |
success = commandStringIntoJavaStrings( jnienv, |
|
445 |
classname, |
|
446 |
optionsString, |
|
447 |
&classNameObject, |
|
448 |
&optionsStringObject); |
|
449 |
||
450 |
if (success) { |
|
451 |
success = invokeJavaAgentMainMethod( jnienv, |
|
452 |
agent->mInstrumentationImpl, |
|
453 |
agentMainMethod, |
|
454 |
classNameObject, |
|
455 |
optionsStringObject); |
|
456 |
} |
|
457 |
||
458 |
return success; |
|
459 |
} |
|
460 |
||
461 |
void |
|
462 |
deallocateCommandLineData( JPLISAgent * agent) { |
|
463 |
deallocate(jvmti(agent), (void*)agent->mAgentClassName); |
|
464 |
deallocate(jvmti(agent), (void*)agent->mOptionsString); |
|
465 |
||
466 |
/* zero things out so it is easier to see what is going on */ |
|
467 |
agent->mAgentClassName = NULL; |
|
468 |
agent->mOptionsString = NULL; |
|
469 |
} |
|
470 |
||
471 |
/* |
|
472 |
* Create the java.lang.instrument.Instrumentation instance |
|
473 |
* and access information for it (method IDs, etc) |
|
474 |
*/ |
|
475 |
jboolean |
|
476 |
createInstrumentationImpl( JNIEnv * jnienv, |
|
477 |
JPLISAgent * agent) { |
|
478 |
jclass implClass = NULL; |
|
479 |
jboolean errorOutstanding = JNI_FALSE; |
|
480 |
jobject resultImpl = NULL; |
|
481 |
jmethodID premainCallerMethodID = NULL; |
|
482 |
jmethodID agentmainCallerMethodID = NULL; |
|
483 |
jmethodID transformMethodID = NULL; |
|
484 |
jmethodID constructorID = NULL; |
|
485 |
jobject localReference = NULL; |
|
486 |
||
487 |
/* First find the class of our implementation */ |
|
488 |
implClass = (*jnienv)->FindClass( jnienv, |
|
489 |
JPLIS_INSTRUMENTIMPL_CLASSNAME); |
|
490 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
491 |
errorOutstanding = errorOutstanding || (implClass == NULL); |
|
492 |
jplis_assert_msg(!errorOutstanding, "find class on InstrumentationImpl failed"); |
|
493 |
||
494 |
if ( !errorOutstanding ) { |
|
495 |
constructorID = (*jnienv)->GetMethodID( jnienv, |
|
496 |
implClass, |
|
497 |
JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODNAME, |
|
498 |
JPLIS_INSTRUMENTIMPL_CONSTRUCTOR_METHODSIGNATURE); |
|
499 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
500 |
errorOutstanding = errorOutstanding || (constructorID == NULL); |
|
501 |
jplis_assert_msg(!errorOutstanding, "find constructor on InstrumentationImpl failed"); |
|
502 |
} |
|
503 |
||
504 |
if ( !errorOutstanding ) { |
|
505 |
jlong peerReferenceAsScalar = (jlong)(intptr_t) agent; |
|
506 |
localReference = (*jnienv)->NewObject( jnienv, |
|
507 |
implClass, |
|
508 |
constructorID, |
|
509 |
peerReferenceAsScalar, |
|
510 |
agent->mRedefineAdded, |
|
511 |
agent->mNativeMethodPrefixAdded); |
|
512 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
513 |
errorOutstanding = errorOutstanding || (localReference == NULL); |
|
514 |
jplis_assert_msg(!errorOutstanding, "call constructor on InstrumentationImpl failed"); |
|
515 |
} |
|
516 |
||
517 |
if ( !errorOutstanding ) { |
|
518 |
resultImpl = (*jnienv)->NewGlobalRef(jnienv, localReference); |
|
519 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
520 |
jplis_assert_msg(!errorOutstanding, "copy local ref to global ref"); |
|
521 |
} |
|
522 |
||
523 |
/* Now look up the method ID for the pre-main caller (we will need this more than once) */ |
|
524 |
if ( !errorOutstanding ) { |
|
525 |
premainCallerMethodID = (*jnienv)->GetMethodID( jnienv, |
|
526 |
implClass, |
|
527 |
JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODNAME, |
|
528 |
JPLIS_INSTRUMENTIMPL_PREMAININVOKER_METHODSIGNATURE); |
|
529 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
530 |
errorOutstanding = errorOutstanding || (premainCallerMethodID == NULL); |
|
531 |
jplis_assert_msg(!errorOutstanding, "can't find premain invoker methodID"); |
|
532 |
} |
|
533 |
||
534 |
/* Now look up the method ID for the agent-main caller */ |
|
535 |
if ( !errorOutstanding ) { |
|
536 |
agentmainCallerMethodID = (*jnienv)->GetMethodID( jnienv, |
|
537 |
implClass, |
|
538 |
JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODNAME, |
|
539 |
JPLIS_INSTRUMENTIMPL_AGENTMAININVOKER_METHODSIGNATURE); |
|
540 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
541 |
errorOutstanding = errorOutstanding || (agentmainCallerMethodID == NULL); |
|
542 |
jplis_assert_msg(!errorOutstanding, "can't find agentmain invoker methodID"); |
|
543 |
} |
|
544 |
||
545 |
/* Now look up the method ID for the transform method (we will need this constantly) */ |
|
546 |
if ( !errorOutstanding ) { |
|
547 |
transformMethodID = (*jnienv)->GetMethodID( jnienv, |
|
548 |
implClass, |
|
549 |
JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODNAME, |
|
550 |
JPLIS_INSTRUMENTIMPL_TRANSFORM_METHODSIGNATURE); |
|
551 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
552 |
errorOutstanding = errorOutstanding || (transformMethodID == NULL); |
|
553 |
jplis_assert_msg(!errorOutstanding, "can't find transform methodID"); |
|
554 |
} |
|
555 |
||
556 |
if ( !errorOutstanding ) { |
|
557 |
agent->mInstrumentationImpl = resultImpl; |
|
558 |
agent->mPremainCaller = premainCallerMethodID; |
|
559 |
agent->mAgentmainCaller = agentmainCallerMethodID; |
|
560 |
agent->mTransform = transformMethodID; |
|
561 |
} |
|
562 |
||
563 |
return !errorOutstanding; |
|
564 |
} |
|
565 |
||
566 |
jboolean |
|
567 |
commandStringIntoJavaStrings( JNIEnv * jnienv, |
|
568 |
const char * classname, |
|
569 |
const char * optionsString, |
|
570 |
jstring * outputClassname, |
|
571 |
jstring * outputOptionsString) { |
|
572 |
jstring classnameJavaString = NULL; |
|
573 |
jstring optionsJavaString = NULL; |
|
574 |
jboolean errorOutstanding = JNI_TRUE; |
|
575 |
||
576 |
classnameJavaString = (*jnienv)->NewStringUTF(jnienv, classname); |
|
577 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
578 |
jplis_assert_msg(!errorOutstanding, "can't create class name java string"); |
|
579 |
||
580 |
if ( !errorOutstanding ) { |
|
581 |
if ( optionsString != NULL) { |
|
582 |
optionsJavaString = (*jnienv)->NewStringUTF(jnienv, optionsString); |
|
583 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
584 |
jplis_assert_msg(!errorOutstanding, "can't create options java string"); |
|
585 |
} |
|
586 |
||
587 |
if ( !errorOutstanding ) { |
|
588 |
*outputClassname = classnameJavaString; |
|
589 |
*outputOptionsString = optionsJavaString; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
return !errorOutstanding; |
|
594 |
} |
|
595 |
||
596 |
||
597 |
jboolean |
|
598 |
invokeJavaAgentMainMethod( JNIEnv * jnienv, |
|
599 |
jobject instrumentationImpl, |
|
600 |
jmethodID mainCallingMethod, |
|
601 |
jstring className, |
|
602 |
jstring optionsString) { |
|
603 |
jboolean errorOutstanding = JNI_FALSE; |
|
604 |
||
605 |
jplis_assert(mainCallingMethod != NULL); |
|
606 |
if ( mainCallingMethod != NULL ) { |
|
607 |
(*jnienv)->CallVoidMethod( jnienv, |
|
608 |
instrumentationImpl, |
|
609 |
mainCallingMethod, |
|
610 |
className, |
|
611 |
optionsString); |
|
612 |
errorOutstanding = checkForThrowable(jnienv); |
|
613 |
if ( errorOutstanding ) { |
|
614 |
logThrowable(jnienv); |
|
615 |
} |
|
616 |
checkForAndClearThrowable(jnienv); |
|
617 |
} |
|
618 |
return !errorOutstanding; |
|
619 |
} |
|
620 |
||
621 |
jboolean |
|
622 |
setLivePhaseEventHandlers( JPLISAgent * agent) { |
|
623 |
jvmtiEventCallbacks callbacks; |
|
624 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
625 |
jvmtiError jvmtierror; |
|
626 |
||
627 |
/* first swap out the handlers (switch from the VMInit handler, which we do not need, |
|
628 |
* to the ClassFileLoadHook handler, which is what the agents need from now on) |
|
629 |
*/ |
|
630 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
631 |
callbacks.ClassFileLoadHook = &eventHandlerClassFileLoadHook; |
|
632 |
||
633 |
jvmtierror = (*jvmtienv)->SetEventCallbacks( jvmtienv, |
|
634 |
&callbacks, |
|
635 |
sizeof(callbacks)); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
636 |
check_phase_ret_false(jvmtierror); |
2 | 637 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
638 |
||
639 |
||
640 |
if ( jvmtierror == JVMTI_ERROR_NONE ) { |
|
641 |
/* turn off VMInit */ |
|
642 |
jvmtierror = (*jvmtienv)->SetEventNotificationMode( |
|
643 |
jvmtienv, |
|
644 |
JVMTI_DISABLE, |
|
645 |
JVMTI_EVENT_VM_INIT, |
|
646 |
NULL /* all threads */); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
647 |
check_phase_ret_false(jvmtierror); |
2 | 648 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
649 |
} |
|
650 |
||
651 |
if ( jvmtierror == JVMTI_ERROR_NONE ) { |
|
652 |
/* turn on ClassFileLoadHook */ |
|
653 |
jvmtierror = (*jvmtienv)->SetEventNotificationMode( |
|
654 |
jvmtienv, |
|
655 |
JVMTI_ENABLE, |
|
656 |
JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
|
657 |
NULL /* all threads */); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
658 |
check_phase_ret_false(jvmtierror); |
2 | 659 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
660 |
} |
|
661 |
||
662 |
return (jvmtierror == JVMTI_ERROR_NONE); |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* Check if the can_redefine_classes capability is available. |
|
667 |
*/ |
|
668 |
void |
|
669 |
checkCapabilities(JPLISAgent * agent) { |
|
670 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
671 |
jvmtiCapabilities potentialCapabilities; |
|
672 |
jvmtiError jvmtierror; |
|
673 |
||
674 |
memset(&potentialCapabilities, 0, sizeof(potentialCapabilities)); |
|
675 |
||
676 |
jvmtierror = (*jvmtienv)->GetPotentialCapabilities(jvmtienv, &potentialCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
677 |
check_phase_ret(jvmtierror); |
2 | 678 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
679 |
||
680 |
if ( jvmtierror == JVMTI_ERROR_NONE ) { |
|
681 |
if ( potentialCapabilities.can_redefine_classes == 1 ) { |
|
682 |
agent->mRedefineAvailable = JNI_TRUE; |
|
683 |
} |
|
684 |
if ( potentialCapabilities.can_set_native_method_prefix == 1 ) { |
|
685 |
agent->mNativeMethodPrefixAvailable = JNI_TRUE; |
|
686 |
} |
|
687 |
} |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Enable native method prefix in one JVM TI environment |
|
692 |
*/ |
|
693 |
void |
|
694 |
enableNativeMethodPrefixCapability(jvmtiEnv * jvmtienv) { |
|
695 |
jvmtiCapabilities desiredCapabilities; |
|
696 |
jvmtiError jvmtierror; |
|
697 |
||
698 |
jvmtierror = (*jvmtienv)->GetCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
699 |
/* can be called from any phase */ |
2 | 700 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
701 |
desiredCapabilities.can_set_native_method_prefix = 1; |
|
702 |
jvmtierror = (*jvmtienv)->AddCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
703 |
check_phase_ret(jvmtierror); |
2 | 704 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
705 |
} |
|
706 |
||
707 |
||
708 |
/** |
|
709 |
* Add the can_set_native_method_prefix capability |
|
710 |
*/ |
|
711 |
void |
|
712 |
addNativeMethodPrefixCapability(JPLISAgent * agent) { |
|
713 |
if (agent->mNativeMethodPrefixAvailable && !agent->mNativeMethodPrefixAdded) { |
|
714 |
jvmtiEnv * jvmtienv = agent->mNormalEnvironment.mJVMTIEnv; |
|
715 |
enableNativeMethodPrefixCapability(jvmtienv); |
|
716 |
||
717 |
jvmtienv = agent->mRetransformEnvironment.mJVMTIEnv; |
|
718 |
if (jvmtienv != NULL) { |
|
719 |
enableNativeMethodPrefixCapability(jvmtienv); |
|
720 |
} |
|
721 |
agent->mNativeMethodPrefixAdded = JNI_TRUE; |
|
722 |
} |
|
723 |
} |
|
724 |
||
725 |
/** |
|
726 |
* Add the can_maintain_original_method_order capability (for testing) |
|
727 |
*/ |
|
728 |
void |
|
729 |
addOriginalMethodOrderCapability(JPLISAgent * agent) { |
|
730 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
731 |
jvmtiCapabilities desiredCapabilities; |
|
732 |
jvmtiError jvmtierror; |
|
733 |
||
734 |
jvmtierror = (*jvmtienv)->GetCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
735 |
/* can be called from any phase */ |
2 | 736 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
737 |
desiredCapabilities.can_maintain_original_method_order = 1; |
|
738 |
jvmtierror = (*jvmtienv)->AddCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
739 |
check_phase_ret(jvmtierror); |
2 | 740 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
741 |
} |
|
742 |
||
743 |
/** |
|
744 |
* Add the can_redefine_classes capability |
|
745 |
*/ |
|
746 |
void |
|
747 |
addRedefineClassesCapability(JPLISAgent * agent) { |
|
748 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
749 |
jvmtiCapabilities desiredCapabilities; |
|
750 |
jvmtiError jvmtierror; |
|
751 |
||
752 |
if (agent->mRedefineAvailable && !agent->mRedefineAdded) { |
|
753 |
jvmtierror = (*jvmtienv)->GetCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
754 |
/* can be called from any phase */ |
2 | 755 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
756 |
desiredCapabilities.can_redefine_classes = 1; |
|
757 |
jvmtierror = (*jvmtienv)->AddCapabilities(jvmtienv, &desiredCapabilities); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
758 |
check_phase_ret(jvmtierror); |
2 | 759 |
|
760 |
/* |
|
761 |
* With mixed premain/agentmain agents then it's possible that the |
|
762 |
* capability was potentially available in the onload phase but |
|
763 |
* subsequently unavailable in the live phase. |
|
764 |
*/ |
|
765 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE || |
|
766 |
jvmtierror == JVMTI_ERROR_NOT_AVAILABLE); |
|
767 |
if (jvmtierror == JVMTI_ERROR_NONE) { |
|
768 |
agent->mRedefineAdded = JNI_TRUE; |
|
769 |
} |
|
770 |
} |
|
771 |
} |
|
772 |
||
36511 | 773 |
static jobject |
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
774 |
getModuleObject(jvmtiEnv* jvmti, |
36511 | 775 |
jobject loaderObject, |
776 |
const char* cname) { |
|
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
777 |
jvmtiError err = JVMTI_ERROR_NONE; |
36511 | 778 |
jobject moduleObject = NULL; |
779 |
||
780 |
/* find last slash in the class name */ |
|
781 |
char* last_slash = (cname == NULL) ? NULL : strrchr(cname, '/'); |
|
782 |
int len = (last_slash == NULL) ? 0 : (int)(last_slash - cname); |
|
783 |
char* pkg_name_buf = (char*)malloc(len + 1); |
|
784 |
||
785 |
jplis_assert_msg(pkg_name_buf != NULL, "OOM error in native tmp buffer allocation"); |
|
786 |
if (last_slash != NULL) { |
|
787 |
strncpy(pkg_name_buf, cname, len); |
|
788 |
} |
|
789 |
pkg_name_buf[len] = '\0'; |
|
790 |
||
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
791 |
err = (*jvmti)->GetNamedModule(jvmti, loaderObject, pkg_name_buf, &moduleObject); |
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
792 |
jplis_assert_msg(err == JVMTI_ERROR_NONE, "error in the JVMTI GetNamedModule"); |
36511 | 793 |
|
794 |
free((void*)pkg_name_buf); |
|
795 |
return moduleObject; |
|
796 |
} |
|
2 | 797 |
|
798 |
/* |
|
799 |
* Support for the JVMTI callbacks |
|
800 |
*/ |
|
801 |
||
802 |
void |
|
803 |
transformClassFile( JPLISAgent * agent, |
|
804 |
JNIEnv * jnienv, |
|
805 |
jobject loaderObject, |
|
806 |
const char* name, |
|
807 |
jclass classBeingRedefined, |
|
808 |
jobject protectionDomain, |
|
809 |
jint class_data_len, |
|
810 |
const unsigned char* class_data, |
|
811 |
jint* new_class_data_len, |
|
812 |
unsigned char** new_class_data, |
|
813 |
jboolean is_retransformer) { |
|
814 |
jboolean errorOutstanding = JNI_FALSE; |
|
815 |
jstring classNameStringObject = NULL; |
|
816 |
jarray classFileBufferObject = NULL; |
|
817 |
jarray transformedBufferObject = NULL; |
|
818 |
jsize transformedBufferSize = 0; |
|
819 |
unsigned char * resultBuffer = NULL; |
|
820 |
jboolean shouldRun = JNI_FALSE; |
|
821 |
||
822 |
/* only do this if we aren't already in the middle of processing a class on this thread */ |
|
823 |
shouldRun = tryToAcquireReentrancyToken( |
|
824 |
jvmti(agent), |
|
825 |
NULL); /* this thread */ |
|
826 |
||
827 |
if ( shouldRun ) { |
|
828 |
/* first marshall all the parameters */ |
|
829 |
classNameStringObject = (*jnienv)->NewStringUTF(jnienv, |
|
830 |
name); |
|
831 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
832 |
jplis_assert_msg(!errorOutstanding, "can't create name string"); |
|
833 |
||
834 |
if ( !errorOutstanding ) { |
|
835 |
classFileBufferObject = (*jnienv)->NewByteArray(jnienv, |
|
836 |
class_data_len); |
|
837 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
36511 | 838 |
jplis_assert_msg(!errorOutstanding, "can't create byte array"); |
2 | 839 |
} |
840 |
||
841 |
if ( !errorOutstanding ) { |
|
842 |
jbyte * typedBuffer = (jbyte *) class_data; /* nasty cast, dumb JNI interface, const missing */ |
|
843 |
/* The sign cast is safe. The const cast is dumb. */ |
|
844 |
(*jnienv)->SetByteArrayRegion( jnienv, |
|
845 |
classFileBufferObject, |
|
846 |
0, |
|
847 |
class_data_len, |
|
848 |
typedBuffer); |
|
849 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
850 |
jplis_assert_msg(!errorOutstanding, "can't set byte array region"); |
|
851 |
} |
|
852 |
||
853 |
/* now call the JPL agents to do the transforming */ |
|
854 |
/* potential future optimization: may want to skip this if there are none */ |
|
855 |
if ( !errorOutstanding ) { |
|
36511 | 856 |
jobject moduleObject = NULL; |
857 |
||
858 |
if (classBeingRedefined == NULL) { |
|
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
859 |
moduleObject = getModuleObject(jvmti(agent), loaderObject, name); |
36511 | 860 |
} else { |
861 |
// Redefine or retransform, InstrumentationImpl.transform() will use |
|
862 |
// classBeingRedefined.getModule() to get the module. |
|
863 |
} |
|
2 | 864 |
jplis_assert(agent->mInstrumentationImpl != NULL); |
865 |
jplis_assert(agent->mTransform != NULL); |
|
866 |
transformedBufferObject = (*jnienv)->CallObjectMethod( |
|
867 |
jnienv, |
|
868 |
agent->mInstrumentationImpl, |
|
869 |
agent->mTransform, |
|
39739
5d058d6bd245
8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents:
36511
diff
changeset
|
870 |
moduleObject, |
2 | 871 |
loaderObject, |
872 |
classNameStringObject, |
|
873 |
classBeingRedefined, |
|
874 |
protectionDomain, |
|
875 |
classFileBufferObject, |
|
876 |
is_retransformer); |
|
877 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
878 |
jplis_assert_msg(!errorOutstanding, "transform method call failed"); |
|
879 |
} |
|
880 |
||
881 |
/* Finally, unmarshall the parameters (if someone touched the buffer, tell the JVM) */ |
|
882 |
if ( !errorOutstanding ) { |
|
883 |
if ( transformedBufferObject != NULL ) { |
|
884 |
transformedBufferSize = (*jnienv)->GetArrayLength( jnienv, |
|
885 |
transformedBufferObject); |
|
886 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
887 |
jplis_assert_msg(!errorOutstanding, "can't get array length"); |
|
888 |
||
889 |
if ( !errorOutstanding ) { |
|
890 |
/* allocate the response buffer with the JVMTI allocate call. |
|
891 |
* This is what the JVMTI spec says to do for Class File Load hook responses |
|
892 |
*/ |
|
893 |
jvmtiError allocError = (*(jvmti(agent)))->Allocate(jvmti(agent), |
|
894 |
transformedBufferSize, |
|
895 |
&resultBuffer); |
|
896 |
errorOutstanding = (allocError != JVMTI_ERROR_NONE); |
|
897 |
jplis_assert_msg(!errorOutstanding, "can't allocate result buffer"); |
|
898 |
} |
|
899 |
||
900 |
if ( !errorOutstanding ) { |
|
901 |
(*jnienv)->GetByteArrayRegion( jnienv, |
|
902 |
transformedBufferObject, |
|
903 |
0, |
|
904 |
transformedBufferSize, |
|
905 |
(jbyte *) resultBuffer); |
|
906 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
907 |
jplis_assert_msg(!errorOutstanding, "can't get byte array region"); |
|
908 |
||
909 |
/* in this case, we will not return the buffer to the JVMTI, |
|
910 |
* so we need to deallocate it ourselves |
|
911 |
*/ |
|
912 |
if ( errorOutstanding ) { |
|
913 |
deallocate( jvmti(agent), |
|
914 |
(void*)resultBuffer); |
|
915 |
} |
|
916 |
} |
|
917 |
||
918 |
if ( !errorOutstanding ) { |
|
919 |
*new_class_data_len = (transformedBufferSize); |
|
920 |
*new_class_data = resultBuffer; |
|
921 |
} |
|
922 |
} |
|
923 |
} |
|
924 |
||
925 |
/* release the token */ |
|
926 |
releaseReentrancyToken( jvmti(agent), |
|
927 |
NULL); /* this thread */ |
|
928 |
||
929 |
} |
|
930 |
||
931 |
return; |
|
932 |
} |
|
933 |
||
934 |
/* |
|
935 |
* Misc. internal utilities. |
|
936 |
*/ |
|
937 |
||
938 |
/* |
|
939 |
* The only checked exceptions we can throw are ClassNotFoundException and |
|
940 |
* UnmodifiableClassException. All others map to InternalError. |
|
941 |
*/ |
|
942 |
jthrowable |
|
943 |
redefineClassMapper( JNIEnv * jnienv, |
|
944 |
jthrowable throwableToMap) { |
|
945 |
jthrowable mappedThrowable = NULL; |
|
946 |
||
947 |
jplis_assert(isSafeForJNICalls(jnienv)); |
|
948 |
jplis_assert(!isUnchecked(jnienv, throwableToMap)); |
|
949 |
||
950 |
if ( isInstanceofClassName( jnienv, |
|
951 |
throwableToMap, |
|
952 |
"java/lang/ClassNotFoundException") ) { |
|
953 |
mappedThrowable = throwableToMap; |
|
954 |
} else { |
|
955 |
if ( isInstanceofClassName( jnienv, |
|
956 |
throwableToMap, |
|
957 |
"java/lang/instrument/UnmodifiableClassException")) { |
|
958 |
mappedThrowable = throwableToMap; |
|
959 |
} else { |
|
960 |
jstring message = NULL; |
|
961 |
||
962 |
message = getMessageFromThrowable(jnienv, throwableToMap); |
|
963 |
mappedThrowable = createInternalError(jnienv, message); |
|
964 |
} |
|
965 |
} |
|
966 |
||
967 |
jplis_assert(isSafeForJNICalls(jnienv)); |
|
968 |
return mappedThrowable; |
|
969 |
} |
|
970 |
||
971 |
jobjectArray |
|
972 |
getObjectArrayFromClasses(JNIEnv* jnienv, jclass* classes, jint classCount) { |
|
973 |
jclass classArrayClass = NULL; |
|
974 |
jobjectArray localArray = NULL; |
|
975 |
jint classIndex = 0; |
|
976 |
jboolean errorOccurred = JNI_FALSE; |
|
977 |
||
978 |
/* get the class array class */ |
|
979 |
classArrayClass = (*jnienv)->FindClass(jnienv, "java/lang/Class"); |
|
980 |
errorOccurred = checkForThrowable(jnienv); |
|
981 |
||
982 |
if (!errorOccurred) { |
|
983 |
jplis_assert_msg(classArrayClass != NULL, "FindClass returned null class"); |
|
984 |
||
985 |
/* create the array for the classes */ |
|
986 |
localArray = (*jnienv)->NewObjectArray(jnienv, classCount, classArrayClass, NULL); |
|
987 |
errorOccurred = checkForThrowable(jnienv); |
|
988 |
||
989 |
if (!errorOccurred) { |
|
990 |
jplis_assert_msg(localArray != NULL, "NewObjectArray returned null array"); |
|
991 |
||
992 |
/* now copy refs to all the classes and put them into the array */ |
|
993 |
for (classIndex = 0; classIndex < classCount; classIndex++) { |
|
994 |
/* put class into array */ |
|
995 |
(*jnienv)->SetObjectArrayElement(jnienv, localArray, classIndex, classes[classIndex]); |
|
996 |
errorOccurred = checkForThrowable(jnienv); |
|
997 |
||
998 |
if (errorOccurred) { |
|
999 |
localArray = NULL; |
|
1000 |
break; |
|
1001 |
} |
|
1002 |
} |
|
1003 |
} |
|
1004 |
} |
|
1005 |
||
1006 |
return localArray; |
|
1007 |
} |
|
1008 |
||
1009 |
||
1010 |
/* Return the environment with the retransformation capability. |
|
1011 |
* Create it if it doesn't exist. |
|
1012 |
* Return NULL if it can't be created. |
|
1013 |
*/ |
|
1014 |
jvmtiEnv * |
|
1015 |
retransformableEnvironment(JPLISAgent * agent) { |
|
1016 |
jvmtiEnv * retransformerEnv = NULL; |
|
1017 |
jint jnierror = JNI_OK; |
|
1018 |
jvmtiCapabilities desiredCapabilities; |
|
1019 |
jvmtiEventCallbacks callbacks; |
|
1020 |
jvmtiError jvmtierror; |
|
1021 |
||
1022 |
if (agent->mRetransformEnvironment.mJVMTIEnv != NULL) { |
|
1023 |
return agent->mRetransformEnvironment.mJVMTIEnv; |
|
1024 |
} |
|
1025 |
jnierror = (*agent->mJVM)->GetEnv( agent->mJVM, |
|
1026 |
(void **) &retransformerEnv, |
|
7978
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
1027 |
JVMTI_VERSION_1_1); |
2 | 1028 |
if ( jnierror != JNI_OK ) { |
1029 |
return NULL; |
|
1030 |
} |
|
1031 |
jvmtierror = (*retransformerEnv)->GetCapabilities(retransformerEnv, &desiredCapabilities); |
|
1032 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
|
1033 |
desiredCapabilities.can_retransform_classes = 1; |
|
1034 |
if (agent->mNativeMethodPrefixAdded) { |
|
1035 |
desiredCapabilities.can_set_native_method_prefix = 1; |
|
1036 |
} |
|
1037 |
||
1038 |
jvmtierror = (*retransformerEnv)->AddCapabilities(retransformerEnv, &desiredCapabilities); |
|
1039 |
if (jvmtierror != JVMTI_ERROR_NONE) { |
|
1040 |
/* cannot get the capability, dispose of the retransforming environment */ |
|
1041 |
jvmtierror = (*retransformerEnv)->DisposeEnvironment(retransformerEnv); |
|
1042 |
jplis_assert(jvmtierror == JVMTI_ERROR_NOT_AVAILABLE); |
|
1043 |
return NULL; |
|
1044 |
} |
|
1045 |
memset(&callbacks, 0, sizeof(callbacks)); |
|
1046 |
callbacks.ClassFileLoadHook = &eventHandlerClassFileLoadHook; |
|
1047 |
||
1048 |
jvmtierror = (*retransformerEnv)->SetEventCallbacks(retransformerEnv, |
|
1049 |
&callbacks, |
|
1050 |
sizeof(callbacks)); |
|
1051 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
|
1052 |
if (jvmtierror == JVMTI_ERROR_NONE) { |
|
1053 |
// install the retransforming environment |
|
1054 |
agent->mRetransformEnvironment.mJVMTIEnv = retransformerEnv; |
|
279
e0911cc0a5d1
6547358: 2/2 j.l.i.: manifest attribute 'Can-Retransform-Classes' is ignored by isRetransformClassesSupported()
dcubed
parents:
2
diff
changeset
|
1055 |
agent->mRetransformEnvironment.mIsRetransformer = JNI_TRUE; |
2 | 1056 |
|
1057 |
// Make it for ClassFileLoadHook handling |
|
1058 |
jvmtierror = (*retransformerEnv)->SetEnvironmentLocalStorage( |
|
1059 |
retransformerEnv, |
|
1060 |
&(agent->mRetransformEnvironment)); |
|
1061 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
|
1062 |
if (jvmtierror == JVMTI_ERROR_NONE) { |
|
1063 |
return retransformerEnv; |
|
1064 |
} |
|
1065 |
} |
|
1066 |
return NULL; |
|
1067 |
} |
|
1068 |
||
1069 |
||
1070 |
/* |
|
1071 |
* Underpinnings for native methods |
|
1072 |
*/ |
|
1073 |
||
1074 |
jboolean |
|
1075 |
isModifiableClass(JNIEnv * jnienv, JPLISAgent * agent, jclass clazz) { |
|
1076 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
1077 |
jvmtiError jvmtierror; |
|
1078 |
jboolean is_modifiable = JNI_FALSE; |
|
1079 |
||
1080 |
jvmtierror = (*jvmtienv)->IsModifiableClass( jvmtienv, |
|
1081 |
clazz, |
|
1082 |
&is_modifiable); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
1083 |
check_phase_ret_false(jvmtierror); |
2 | 1084 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
1085 |
||
1086 |
return is_modifiable; |
|
1087 |
} |
|
1088 |
||
1089 |
jboolean |
|
1090 |
isRetransformClassesSupported(JNIEnv * jnienv, JPLISAgent * agent) { |
|
279
e0911cc0a5d1
6547358: 2/2 j.l.i.: manifest attribute 'Can-Retransform-Classes' is ignored by isRetransformClassesSupported()
dcubed
parents:
2
diff
changeset
|
1091 |
return agent->mRetransformEnvironment.mIsRetransformer; |
2 | 1092 |
} |
1093 |
||
1094 |
void |
|
1095 |
setHasRetransformableTransformers(JNIEnv * jnienv, JPLISAgent * agent, jboolean has) { |
|
1096 |
jvmtiEnv * retransformerEnv = retransformableEnvironment(agent); |
|
1097 |
jvmtiError jvmtierror; |
|
1098 |
||
1099 |
jplis_assert(retransformerEnv != NULL); |
|
1100 |
jvmtierror = (*retransformerEnv)->SetEventNotificationMode( |
|
1101 |
retransformerEnv, |
|
1102 |
has? JVMTI_ENABLE : JVMTI_DISABLE, |
|
1103 |
JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, |
|
1104 |
NULL /* all threads */); |
|
1105 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
|
1106 |
} |
|
1107 |
||
1108 |
void |
|
1109 |
retransformClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classes) { |
|
1110 |
jvmtiEnv * retransformerEnv = retransformableEnvironment(agent); |
|
1111 |
jboolean errorOccurred = JNI_FALSE; |
|
1112 |
jvmtiError errorCode = JVMTI_ERROR_NONE; |
|
1113 |
jsize numClasses = 0; |
|
1114 |
jclass * classArray = NULL; |
|
1115 |
||
1116 |
/* This is supposed to be checked by caller, but just to be sure */ |
|
1117 |
if (retransformerEnv == NULL) { |
|
1118 |
jplis_assert(retransformerEnv != NULL); |
|
1119 |
errorOccurred = JNI_TRUE; |
|
1120 |
errorCode = JVMTI_ERROR_MUST_POSSESS_CAPABILITY; |
|
1121 |
} |
|
1122 |
||
1123 |
/* This was supposed to be checked by caller too */ |
|
1124 |
if (!errorOccurred && classes == NULL) { |
|
1125 |
jplis_assert(classes != NULL); |
|
1126 |
errorOccurred = JNI_TRUE; |
|
1127 |
errorCode = JVMTI_ERROR_NULL_POINTER; |
|
1128 |
} |
|
1129 |
||
1130 |
if (!errorOccurred) { |
|
1131 |
numClasses = (*jnienv)->GetArrayLength(jnienv, classes); |
|
1132 |
errorOccurred = checkForThrowable(jnienv); |
|
1133 |
jplis_assert(!errorOccurred); |
|
280
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1134 |
|
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1135 |
if (!errorOccurred && numClasses == 0) { |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1136 |
jplis_assert(numClasses != 0); |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1137 |
errorOccurred = JNI_TRUE; |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1138 |
errorCode = JVMTI_ERROR_NULL_POINTER; |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1139 |
} |
2 | 1140 |
} |
1141 |
||
1142 |
if (!errorOccurred) { |
|
1143 |
classArray = (jclass *) allocate(retransformerEnv, |
|
1144 |
numClasses * sizeof(jclass)); |
|
1145 |
errorOccurred = (classArray == NULL); |
|
1146 |
jplis_assert(!errorOccurred); |
|
1147 |
if (errorOccurred) { |
|
1148 |
errorCode = JVMTI_ERROR_OUT_OF_MEMORY; |
|
1149 |
} |
|
1150 |
} |
|
1151 |
||
1152 |
if (!errorOccurred) { |
|
1153 |
jint index; |
|
1154 |
for (index = 0; index < numClasses; index++) { |
|
1155 |
classArray[index] = (*jnienv)->GetObjectArrayElement(jnienv, classes, index); |
|
1156 |
errorOccurred = checkForThrowable(jnienv); |
|
1157 |
jplis_assert(!errorOccurred); |
|
1158 |
if (errorOccurred) { |
|
1159 |
break; |
|
1160 |
} |
|
280
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1161 |
|
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1162 |
if (classArray[index] == NULL) { |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1163 |
jplis_assert(classArray[index] != NULL); |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1164 |
errorOccurred = JNI_TRUE; |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1165 |
errorCode = JVMTI_ERROR_NULL_POINTER; |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1166 |
break; |
988fe6228d49
6547500: 2/2 j.l.i.: .retransformClasses throws unexpected InternalError
dcubed
parents:
279
diff
changeset
|
1167 |
} |
2 | 1168 |
} |
1169 |
} |
|
1170 |
||
1171 |
if (!errorOccurred) { |
|
1172 |
errorCode = (*retransformerEnv)->RetransformClasses(retransformerEnv, |
|
1173 |
numClasses, classArray); |
|
1174 |
errorOccurred = (errorCode != JVMTI_ERROR_NONE); |
|
1175 |
} |
|
1176 |
||
1177 |
/* Give back the buffer if we allocated it. Throw any exceptions after. |
|
1178 |
*/ |
|
1179 |
if (classArray != NULL) { |
|
1180 |
deallocate(retransformerEnv, (void*)classArray); |
|
1181 |
} |
|
1182 |
||
1183 |
if (errorCode != JVMTI_ERROR_NONE) { |
|
1184 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, errorCode); |
|
1185 |
} |
|
1186 |
||
1187 |
mapThrownThrowableIfNecessary(jnienv, redefineClassMapper); |
|
1188 |
} |
|
1189 |
||
1190 |
/* |
|
1191 |
* Java code must not call this with a null list or a zero-length list. |
|
1192 |
*/ |
|
1193 |
void |
|
1194 |
redefineClasses(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray classDefinitions) { |
|
1195 |
jvmtiEnv* jvmtienv = jvmti(agent); |
|
1196 |
jboolean errorOccurred = JNI_FALSE; |
|
1197 |
jclass classDefClass = NULL; |
|
1198 |
jmethodID getDefinitionClassMethodID = NULL; |
|
1199 |
jmethodID getDefinitionClassFileMethodID = NULL; |
|
1200 |
jvmtiClassDefinition* classDefs = NULL; |
|
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1201 |
jbyteArray* targetFiles = NULL; |
2 | 1202 |
jsize numDefs = 0; |
1203 |
||
1204 |
jplis_assert(classDefinitions != NULL); |
|
1205 |
||
1206 |
numDefs = (*jnienv)->GetArrayLength(jnienv, classDefinitions); |
|
1207 |
errorOccurred = checkForThrowable(jnienv); |
|
1208 |
jplis_assert(!errorOccurred); |
|
1209 |
||
1210 |
if (!errorOccurred) { |
|
1211 |
jplis_assert(numDefs > 0); |
|
1212 |
/* get method IDs for methods to call on class definitions */ |
|
1213 |
classDefClass = (*jnienv)->FindClass(jnienv, "java/lang/instrument/ClassDefinition"); |
|
1214 |
errorOccurred = checkForThrowable(jnienv); |
|
1215 |
jplis_assert(!errorOccurred); |
|
1216 |
} |
|
1217 |
||
1218 |
if (!errorOccurred) { |
|
1219 |
getDefinitionClassMethodID = (*jnienv)->GetMethodID( jnienv, |
|
1220 |
classDefClass, |
|
1221 |
"getDefinitionClass", |
|
1222 |
"()Ljava/lang/Class;"); |
|
1223 |
errorOccurred = checkForThrowable(jnienv); |
|
1224 |
jplis_assert(!errorOccurred); |
|
1225 |
} |
|
1226 |
||
1227 |
if (!errorOccurred) { |
|
1228 |
getDefinitionClassFileMethodID = (*jnienv)->GetMethodID( jnienv, |
|
1229 |
classDefClass, |
|
1230 |
"getDefinitionClassFile", |
|
1231 |
"()[B"); |
|
1232 |
errorOccurred = checkForThrowable(jnienv); |
|
1233 |
jplis_assert(!errorOccurred); |
|
1234 |
} |
|
1235 |
||
1236 |
if (!errorOccurred) { |
|
1237 |
classDefs = (jvmtiClassDefinition *) allocate( |
|
1238 |
jvmtienv, |
|
1239 |
numDefs * sizeof(jvmtiClassDefinition)); |
|
1240 |
errorOccurred = (classDefs == NULL); |
|
1241 |
jplis_assert(!errorOccurred); |
|
1242 |
if ( errorOccurred ) { |
|
1243 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, JVMTI_ERROR_OUT_OF_MEMORY); |
|
1244 |
} |
|
1245 |
||
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1246 |
else { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1247 |
/* |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1248 |
* We have to save the targetFile values that we compute so |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1249 |
* that we can release the class_bytes arrays that are |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1250 |
* returned by GetByteArrayElements(). In case of a JNI |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1251 |
* error, we can't (easily) recompute the targetFile values |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1252 |
* and we still want to free any memory we allocated. |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1253 |
*/ |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1254 |
targetFiles = (jbyteArray *) allocate(jvmtienv, |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1255 |
numDefs * sizeof(jbyteArray)); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1256 |
errorOccurred = (targetFiles == NULL); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1257 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1258 |
if ( errorOccurred ) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1259 |
deallocate(jvmtienv, (void*)classDefs); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1260 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1261 |
JVMTI_ERROR_OUT_OF_MEMORY); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1262 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1263 |
else { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1264 |
jint i, j; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1265 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1266 |
// clear classDefs so we can correctly free memory during errors |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1267 |
memset(classDefs, 0, numDefs * sizeof(jvmtiClassDefinition)); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1268 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1269 |
for (i = 0; i < numDefs; i++) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1270 |
jclass classDef = NULL; |
2 | 1271 |
|
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1272 |
classDef = (*jnienv)->GetObjectArrayElement(jnienv, classDefinitions, i); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1273 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1274 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1275 |
if (errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1276 |
break; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1277 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1278 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1279 |
classDefs[i].klass = (*jnienv)->CallObjectMethod(jnienv, classDef, getDefinitionClassMethodID); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1280 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1281 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1282 |
if (errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1283 |
break; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1284 |
} |
2 | 1285 |
|
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1286 |
targetFiles[i] = (*jnienv)->CallObjectMethod(jnienv, classDef, getDefinitionClassFileMethodID); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1287 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1288 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1289 |
if (errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1290 |
break; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1291 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1292 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1293 |
classDefs[i].class_byte_count = (*jnienv)->GetArrayLength(jnienv, targetFiles[i]); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1294 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1295 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1296 |
if (errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1297 |
break; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1298 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1299 |
|
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1300 |
/* |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1301 |
* Allocate class_bytes last so we don't have to free |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1302 |
* memory on a partial row error. |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1303 |
*/ |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1304 |
classDefs[i].class_bytes = (unsigned char*)(*jnienv)->GetByteArrayElements(jnienv, targetFiles[i], NULL); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1305 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1306 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1307 |
if (errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1308 |
break; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1309 |
} |
2 | 1310 |
} |
1311 |
||
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1312 |
if (!errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1313 |
jvmtiError errorCode = JVMTI_ERROR_NONE; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1314 |
errorCode = (*jvmtienv)->RedefineClasses(jvmtienv, numDefs, classDefs); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1315 |
if (errorCode == JVMTI_ERROR_WRONG_PHASE) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1316 |
/* insulate caller from the wrong phase error */ |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1317 |
errorCode = JVMTI_ERROR_NONE; |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1318 |
} else { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1319 |
errorOccurred = (errorCode != JVMTI_ERROR_NONE); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1320 |
if ( errorOccurred ) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1321 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, errorCode); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1322 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1323 |
} |
2 | 1324 |
} |
1325 |
||
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1326 |
/* |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1327 |
* Cleanup memory that we allocated above. If we had a |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1328 |
* JNI error, a JVM/TI error or no errors, index 'i' |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1329 |
* tracks how far we got in processing the classDefs |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1330 |
* array. Note: ReleaseByteArrayElements() is safe to |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1331 |
* call with a JNI exception pending. |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1332 |
*/ |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1333 |
for (j = 0; j < i; j++) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1334 |
if ((jbyte *)classDefs[j].class_bytes != NULL) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1335 |
(*jnienv)->ReleaseByteArrayElements(jnienv, |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1336 |
targetFiles[j], (jbyte *)classDefs[j].class_bytes, |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1337 |
0 /* copy back and free */); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1338 |
/* |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1339 |
* Only check for error if we didn't already have one |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1340 |
* so we don't overwrite errorOccurred. |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1341 |
*/ |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1342 |
if (!errorOccurred) { |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1343 |
errorOccurred = checkForThrowable(jnienv); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1344 |
jplis_assert(!errorOccurred); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1345 |
} |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1346 |
} |
2 | 1347 |
} |
11359
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1348 |
deallocate(jvmtienv, (void*)targetFiles); |
9e53808b2128
7121600: Instrumentation.redefineClasses() leaks class bytes
dcubed
parents:
9035
diff
changeset
|
1349 |
deallocate(jvmtienv, (void*)classDefs); |
2 | 1350 |
} |
1351 |
} |
|
1352 |
} |
|
1353 |
||
1354 |
mapThrownThrowableIfNecessary(jnienv, redefineClassMapper); |
|
1355 |
} |
|
1356 |
||
1357 |
/* Cheesy sharing. ClassLoader may be null. */ |
|
1358 |
jobjectArray |
|
1359 |
commonGetClassList( JNIEnv * jnienv, |
|
1360 |
JPLISAgent * agent, |
|
1361 |
jobject classLoader, |
|
1362 |
ClassListFetcher fetcher) { |
|
1363 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
1364 |
jboolean errorOccurred = JNI_FALSE; |
|
1365 |
jvmtiError jvmtierror = JVMTI_ERROR_NONE; |
|
1366 |
jint classCount = 0; |
|
1367 |
jclass * classes = NULL; |
|
1368 |
jobjectArray localArray = NULL; |
|
1369 |
||
1370 |
/* retrieve the classes from the JVMTI agent */ |
|
1371 |
jvmtierror = (*fetcher)( jvmtienv, |
|
1372 |
classLoader, |
|
1373 |
&classCount, |
|
1374 |
&classes); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
1375 |
check_phase_ret_blob(jvmtierror, localArray); |
2 | 1376 |
errorOccurred = (jvmtierror != JVMTI_ERROR_NONE); |
1377 |
jplis_assert(!errorOccurred); |
|
1378 |
||
1379 |
if ( errorOccurred ) { |
|
1380 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, jvmtierror); |
|
1381 |
} else { |
|
1382 |
localArray = getObjectArrayFromClasses( jnienv, |
|
1383 |
classes, |
|
1384 |
classCount); |
|
1385 |
errorOccurred = checkForThrowable(jnienv); |
|
1386 |
jplis_assert(!errorOccurred); |
|
1387 |
||
1388 |
/* do this whether or not we saw a problem */ |
|
1389 |
deallocate(jvmtienv, (void*)classes); |
|
1390 |
} |
|
1391 |
||
1392 |
mapThrownThrowableIfNecessary(jnienv, mapAllCheckedToInternalErrorMapper); |
|
1393 |
return localArray; |
|
1394 |
||
1395 |
} |
|
1396 |
||
1397 |
jvmtiError |
|
1398 |
getAllLoadedClassesClassListFetcher( jvmtiEnv * jvmtienv, |
|
1399 |
jobject classLoader, |
|
1400 |
jint * classCount, |
|
1401 |
jclass ** classes) { |
|
1402 |
return (*jvmtienv)->GetLoadedClasses(jvmtienv, classCount, classes); |
|
1403 |
} |
|
1404 |
||
1405 |
jobjectArray |
|
1406 |
getAllLoadedClasses(JNIEnv * jnienv, JPLISAgent * agent) { |
|
1407 |
return commonGetClassList( jnienv, |
|
1408 |
agent, |
|
1409 |
NULL, |
|
1410 |
getAllLoadedClassesClassListFetcher); |
|
1411 |
} |
|
1412 |
||
1413 |
jvmtiError |
|
1414 |
getInitiatedClassesClassListFetcher( jvmtiEnv * jvmtienv, |
|
1415 |
jobject classLoader, |
|
1416 |
jint * classCount, |
|
1417 |
jclass ** classes) { |
|
1418 |
return (*jvmtienv)->GetClassLoaderClasses(jvmtienv, classLoader, classCount, classes); |
|
1419 |
} |
|
1420 |
||
1421 |
||
1422 |
jobjectArray |
|
1423 |
getInitiatedClasses(JNIEnv * jnienv, JPLISAgent * agent, jobject classLoader) { |
|
1424 |
return commonGetClassList( jnienv, |
|
1425 |
agent, |
|
1426 |
classLoader, |
|
1427 |
getInitiatedClassesClassListFetcher); |
|
1428 |
} |
|
1429 |
||
1430 |
jlong |
|
1431 |
getObjectSize(JNIEnv * jnienv, JPLISAgent * agent, jobject objectToSize) { |
|
1432 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
1433 |
jlong objectSize = -1; |
|
1434 |
jvmtiError jvmtierror = JVMTI_ERROR_NONE; |
|
1435 |
||
1436 |
jvmtierror = (*jvmtienv)->GetObjectSize(jvmtienv, objectToSize, &objectSize); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
1437 |
check_phase_ret_0(jvmtierror); |
2 | 1438 |
jplis_assert(jvmtierror == JVMTI_ERROR_NONE); |
1439 |
if ( jvmtierror != JVMTI_ERROR_NONE ) { |
|
1440 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, jvmtierror); |
|
1441 |
} |
|
1442 |
||
1443 |
mapThrownThrowableIfNecessary(jnienv, mapAllCheckedToInternalErrorMapper); |
|
1444 |
return objectSize; |
|
1445 |
} |
|
1446 |
||
1447 |
void |
|
1448 |
appendToClassLoaderSearch(JNIEnv * jnienv, JPLISAgent * agent, jstring jarFile, jboolean isBootLoader) |
|
1449 |
{ |
|
1450 |
jvmtiEnv * jvmtienv = jvmti(agent); |
|
1451 |
jboolean errorOutstanding; |
|
1452 |
jvmtiError jvmtierror; |
|
1453 |
const char* utf8Chars; |
|
1454 |
jsize utf8Len; |
|
1455 |
jboolean isCopy; |
|
1456 |
char platformChars[MAXPATHLEN]; |
|
1457 |
int platformLen; |
|
1458 |
||
1459 |
utf8Len = (*jnienv)->GetStringUTFLength(jnienv, jarFile); |
|
1460 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
1461 |
||
1462 |
if (!errorOutstanding) { |
|
1463 |
utf8Chars = (*jnienv)->GetStringUTFChars(jnienv, jarFile, &isCopy); |
|
1464 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
1465 |
||
1466 |
if (!errorOutstanding && utf8Chars != NULL) { |
|
1467 |
/* |
|
1468 |
* JVMTI spec'ed to use modified UTF8. At this time this is not implemented |
|
1469 |
* the platform encoding is used. |
|
1470 |
*/ |
|
1471 |
platformLen = convertUft8ToPlatformString((char*)utf8Chars, utf8Len, platformChars, MAXPATHLEN); |
|
1472 |
if (platformLen < 0) { |
|
1473 |
createAndThrowInternalError(jnienv); |
|
1474 |
return; |
|
1475 |
} |
|
1476 |
||
1477 |
(*jnienv)->ReleaseStringUTFChars(jnienv, jarFile, utf8Chars); |
|
1478 |
errorOutstanding = checkForAndClearThrowable(jnienv); |
|
1479 |
||
1480 |
if (!errorOutstanding) { |
|
1481 |
||
1482 |
if (isBootLoader) { |
|
1483 |
jvmtierror = (*jvmtienv)->AddToBootstrapClassLoaderSearch(jvmtienv, platformChars); |
|
1484 |
} else { |
|
1485 |
jvmtierror = (*jvmtienv)->AddToSystemClassLoaderSearch(jvmtienv, platformChars); |
|
1486 |
} |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
1487 |
check_phase_ret(jvmtierror); |
2 | 1488 |
|
1489 |
if ( jvmtierror != JVMTI_ERROR_NONE ) { |
|
1490 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, jvmtierror); |
|
1491 |
} |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} |
|
1495 |
||
1496 |
mapThrownThrowableIfNecessary(jnienv, mapAllCheckedToInternalErrorMapper); |
|
1497 |
} |
|
1498 |
||
1499 |
/* |
|
1500 |
* Set the prefixes used to wrap native methods (so they can be instrumented). |
|
1501 |
* Each transform can set a prefix, any that have been set come in as prefixArray. |
|
1502 |
* Convert them in native strings in a native array then call JVM TI. |
|
1503 |
* One a given call, this function handles either the prefixes for retransformable |
|
1504 |
* transforms or for normal transforms. |
|
1505 |
*/ |
|
1506 |
void |
|
1507 |
setNativeMethodPrefixes(JNIEnv * jnienv, JPLISAgent * agent, jobjectArray prefixArray, |
|
1508 |
jboolean isRetransformable) { |
|
1509 |
jvmtiEnv* jvmtienv; |
|
1510 |
jvmtiError err = JVMTI_ERROR_NONE; |
|
1511 |
jsize arraySize; |
|
1512 |
jboolean errorOccurred = JNI_FALSE; |
|
1513 |
||
1514 |
jplis_assert(prefixArray != NULL); |
|
1515 |
||
1516 |
if (isRetransformable) { |
|
1517 |
jvmtienv = agent->mRetransformEnvironment.mJVMTIEnv; |
|
1518 |
} else { |
|
1519 |
jvmtienv = agent->mNormalEnvironment.mJVMTIEnv; |
|
1520 |
} |
|
1521 |
arraySize = (*jnienv)->GetArrayLength(jnienv, prefixArray); |
|
1522 |
errorOccurred = checkForThrowable(jnienv); |
|
1523 |
jplis_assert(!errorOccurred); |
|
1524 |
||
1525 |
if (!errorOccurred) { |
|
1526 |
/* allocate the native to hold the native prefixes */ |
|
1527 |
const char** prefixes = (const char**) allocate(jvmtienv, |
|
1528 |
arraySize * sizeof(char*)); |
|
1529 |
/* since JNI ReleaseStringUTFChars needs the jstring from which the native |
|
1530 |
* string was allocated, we store them in a parallel array */ |
|
1531 |
jstring* originForRelease = (jstring*) allocate(jvmtienv, |
|
1532 |
arraySize * sizeof(jstring)); |
|
1533 |
errorOccurred = (prefixes == NULL || originForRelease == NULL); |
|
1534 |
jplis_assert(!errorOccurred); |
|
1535 |
if ( errorOccurred ) { |
|
1536 |
createAndThrowThrowableFromJVMTIErrorCode(jnienv, JVMTI_ERROR_OUT_OF_MEMORY); |
|
1537 |
} |
|
1538 |
else { |
|
1539 |
jint inx = 0; |
|
1540 |
jint i; |
|
1541 |
for (i = 0; i < arraySize; i++) { |
|
1542 |
jstring prefixStr = NULL; |
|
1543 |
const char* prefix; |
|
1544 |
jsize prefixLen; |
|
1545 |
jboolean isCopy; |
|
1546 |
||
1547 |
prefixStr = (jstring) ((*jnienv)->GetObjectArrayElement(jnienv, |
|
1548 |
prefixArray, i)); |
|
1549 |
errorOccurred = checkForThrowable(jnienv); |
|
1550 |
jplis_assert(!errorOccurred); |
|
1551 |
if (errorOccurred) { |
|
1552 |
break; |
|
1553 |
} |
|
1554 |
if (prefixStr == NULL) { |
|
1555 |
continue; |
|
1556 |
} |
|
1557 |
||
1558 |
prefixLen = (*jnienv)->GetStringUTFLength(jnienv, prefixStr); |
|
1559 |
errorOccurred = checkForThrowable(jnienv); |
|
1560 |
jplis_assert(!errorOccurred); |
|
1561 |
if (errorOccurred) { |
|
1562 |
break; |
|
1563 |
} |
|
1564 |
||
1565 |
if (prefixLen > 0) { |
|
1566 |
prefix = (*jnienv)->GetStringUTFChars(jnienv, prefixStr, &isCopy); |
|
1567 |
errorOccurred = checkForThrowable(jnienv); |
|
1568 |
jplis_assert(!errorOccurred); |
|
1569 |
if (!errorOccurred && prefix != NULL) { |
|
1570 |
prefixes[inx] = prefix; |
|
1571 |
originForRelease[inx] = prefixStr; |
|
1572 |
++inx; |
|
1573 |
} |
|
1574 |
} |
|
1575 |
} |
|
1576 |
||
1577 |
err = (*jvmtienv)->SetNativeMethodPrefixes(jvmtienv, inx, (char**)prefixes); |
|
282
bca3e5a71df1
6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents:
281
diff
changeset
|
1578 |
/* can be called from any phase */ |
2 | 1579 |
jplis_assert(err == JVMTI_ERROR_NONE); |
1580 |
||
1581 |
for (i = 0; i < inx; i++) { |
|
1582 |
(*jnienv)->ReleaseStringUTFChars(jnienv, originForRelease[i], prefixes[i]); |
|
1583 |
} |
|
1584 |
} |
|
1585 |
deallocate(jvmtienv, (void*)prefixes); |
|
1586 |
deallocate(jvmtienv, (void*)originForRelease); |
|
1587 |
} |
|
1588 |
} |