1 | /*
|
---|
2 | * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
---|
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 | /*
|
---|
27 | * Copyright 2003 Wily Technology, Inc.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <string.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 |
|
---|
33 | #ifdef __EMX__
|
---|
34 | #include <strings.h> // strcasecmp
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include "jni.h"
|
---|
38 |
|
---|
39 | #include "Utilities.h"
|
---|
40 | #include "JPLISAssert.h"
|
---|
41 | #include "JPLISAgent.h"
|
---|
42 | #include "JavaExceptions.h"
|
---|
43 |
|
---|
44 | #include "EncodingSupport.h"
|
---|
45 | #include "FileSystemSupport.h"
|
---|
46 | #include "JarFacade.h"
|
---|
47 | #include "PathCharsValidator.h"
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * This module contains the direct interface points with the JVMTI.
|
---|
51 | * The OnLoad handler is here, along with the various event handlers.
|
---|
52 | */
|
---|
53 |
|
---|
54 | static int
|
---|
55 | appendClassPath(JPLISAgent* agent,
|
---|
56 | const char* jarfile);
|
---|
57 |
|
---|
58 | static void
|
---|
59 | appendBootClassPath(JPLISAgent* agent,
|
---|
60 | const char* jarfile,
|
---|
61 | const char* pathList);
|
---|
62 |
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Parse -javaagent tail, of the form name[=options], into name
|
---|
66 | * and options. Returned values are heap allocated and options maybe
|
---|
67 | * NULL. Returns 0 if parse succeeds, -1 if allocation fails.
|
---|
68 | */
|
---|
69 | static int
|
---|
70 | parseArgumentTail(char* tail, char** name, char** options) {
|
---|
71 | int len;
|
---|
72 | char* pos;
|
---|
73 |
|
---|
74 | pos = strchr(tail, '=');
|
---|
75 | len = (pos == NULL) ? (int)strlen(tail) : (int)(pos - tail);
|
---|
76 |
|
---|
77 | *name = (char*)malloc(len+1);
|
---|
78 | if (*name == NULL) {
|
---|
79 | return -1;
|
---|
80 | }
|
---|
81 | memcpy(*name, tail, len);
|
---|
82 | (*name)[len] = '\0';
|
---|
83 |
|
---|
84 | if (pos == NULL) {
|
---|
85 | *options = NULL;
|
---|
86 | } else {
|
---|
87 | char * str = (char*)malloc( (int)strlen(pos + 1) + 1 );
|
---|
88 | if (str == NULL) {
|
---|
89 | free(*name);
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 | strcpy(str, pos +1);
|
---|
93 | *options = str;
|
---|
94 | }
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Get the value of an attribute in an attribute list. Returns NULL
|
---|
100 | * if attribute not found.
|
---|
101 | */
|
---|
102 | jboolean
|
---|
103 | getBooleanAttribute(const jarAttribute* attributes, const char* name) {
|
---|
104 | char* attributeValue = getAttribute(attributes, name);
|
---|
105 | return attributeValue != NULL && strcasecmp(attributeValue, "true") == 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Parse any capability settings in the JAR manifest and
|
---|
110 | * convert them to JVM TI capabilities.
|
---|
111 | */
|
---|
112 | void
|
---|
113 | convertCapabilityAtrributes(const jarAttribute* attributes, JPLISAgent* agent) {
|
---|
114 | /* set redefineClasses capability */
|
---|
115 | if (getBooleanAttribute(attributes, "Can-Redefine-Classes")) {
|
---|
116 | addRedefineClassesCapability(agent);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* create an environment which has the retransformClasses capability */
|
---|
120 | if (getBooleanAttribute(attributes, "Can-Retransform-Classes")) {
|
---|
121 | retransformableEnvironment(agent);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* set setNativeMethodPrefix capability */
|
---|
125 | if (getBooleanAttribute(attributes, "Can-Set-Native-Method-Prefix")) {
|
---|
126 | addNativeMethodPrefixCapability(agent);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* for retransformClasses testing, set capability to use original method order */
|
---|
130 | if (getBooleanAttribute(attributes, "Can-Maintain-Original-Method-Order")) {
|
---|
131 | addOriginalMethodOrderCapability(agent);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * This will be called once for every -javaagent on the command line.
|
---|
137 | * Each call to Agent_OnLoad will create its own agent and agent data.
|
---|
138 | *
|
---|
139 | * The argument tail string provided to Agent_OnLoad will be of form
|
---|
140 | * <jarfile>[=<options>]. The tail string is split into the jarfile and
|
---|
141 | * options components. The jarfile manifest is parsed and the value of the
|
---|
142 | * Premain-Class attribute will become the agent's premain class. The jar
|
---|
143 | * file is then added to the system class path, and if the Boot-Class-Path
|
---|
144 | * attribute is present then all relative URLs in the value are processed
|
---|
145 | * to create boot class path segments to append to the boot class path.
|
---|
146 | */
|
---|
147 | JNIEXPORT jint JNICALL
|
---|
148 | Agent_OnLoad(JavaVM *vm, char *tail, void * reserved) {
|
---|
149 | JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
|
---|
150 | jint result = JNI_OK;
|
---|
151 | JPLISAgent * agent = NULL;
|
---|
152 |
|
---|
153 | initerror = createNewJPLISAgent(vm, &agent);
|
---|
154 | if ( initerror == JPLIS_INIT_ERROR_NONE ) {
|
---|
155 | int oldLen, newLen;
|
---|
156 | char * jarfile;
|
---|
157 | char * options;
|
---|
158 | jarAttribute* attributes;
|
---|
159 | char * premainClass;
|
---|
160 | char * agentClass;
|
---|
161 | char * bootClassPath;
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Parse <jarfile>[=options] into jarfile and options
|
---|
165 | */
|
---|
166 | if (parseArgumentTail(tail, &jarfile, &options) != 0) {
|
---|
167 | fprintf(stderr, "-javaagent: memory allocation failure.\n");
|
---|
168 | return JNI_ERR;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Agent_OnLoad is specified to provide the agent options
|
---|
173 | * argument tail in modified UTF8. However for 1.5.0 this is
|
---|
174 | * actually in the platform encoding - see 5049313.
|
---|
175 | *
|
---|
176 | * Open zip/jar file and parse archive. If can't be opened or
|
---|
177 | * not a zip file return error. Also if Premain-Class attribute
|
---|
178 | * isn't present we return an error.
|
---|
179 | */
|
---|
180 | attributes = readAttributes(jarfile);
|
---|
181 | if (attributes == NULL) {
|
---|
182 | fprintf(stderr, "Error opening zip file or JAR manifest missing : %s\n", jarfile);
|
---|
183 | free(jarfile);
|
---|
184 | if (options != NULL) free(options);
|
---|
185 | return JNI_ERR;
|
---|
186 | }
|
---|
187 |
|
---|
188 | premainClass = getAttribute(attributes, "Premain-Class");
|
---|
189 | if (premainClass == NULL) {
|
---|
190 | fprintf(stderr, "Failed to find Premain-Class manifest attribute in %s\n",
|
---|
191 | jarfile);
|
---|
192 | free(jarfile);
|
---|
193 | if (options != NULL) free(options);
|
---|
194 | freeAttributes(attributes);
|
---|
195 | return JNI_ERR;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * Add to the jarfile
|
---|
200 | */
|
---|
201 | appendClassPath(agent, jarfile);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * The value of the Premain-Class attribute becomes the agent
|
---|
205 | * class name. The manifest is in UTF8 so need to convert to
|
---|
206 | * modified UTF8 (see JNI spec).
|
---|
207 | */
|
---|
208 | oldLen = (int)strlen(premainClass);
|
---|
209 | newLen = modifiedUtf8LengthOfUtf8(premainClass, oldLen);
|
---|
210 | if (newLen == oldLen) {
|
---|
211 | premainClass = strdup(premainClass);
|
---|
212 | } else {
|
---|
213 | char* str = (char*)malloc( newLen+1 );
|
---|
214 | if (str != NULL) {
|
---|
215 | convertUtf8ToModifiedUtf8(premainClass, oldLen, str, newLen);
|
---|
216 | }
|
---|
217 | premainClass = str;
|
---|
218 | }
|
---|
219 | if (premainClass == NULL) {
|
---|
220 | fprintf(stderr, "-javaagent: memory allocation failed\n");
|
---|
221 | free(jarfile);
|
---|
222 | if (options != NULL) free(options);
|
---|
223 | freeAttributes(attributes);
|
---|
224 | return JNI_ERR;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * If the Boot-Class-Path attribute is specified then we process
|
---|
229 | * each relative URL and add it to the bootclasspath.
|
---|
230 | */
|
---|
231 | bootClassPath = getAttribute(attributes, "Boot-Class-Path");
|
---|
232 | if (bootClassPath != NULL) {
|
---|
233 | appendBootClassPath(agent, jarfile, bootClassPath);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Convert JAR attributes into agent capabilities
|
---|
238 | */
|
---|
239 | convertCapabilityAtrributes(attributes, agent);
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Track (record) the agent class name and options data
|
---|
243 | */
|
---|
244 | initerror = recordCommandLineData(agent, premainClass, options);
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Clean-up
|
---|
248 | */
|
---|
249 | free(jarfile);
|
---|
250 | if (options != NULL) free(options);
|
---|
251 | freeAttributes(attributes);
|
---|
252 | free(premainClass);
|
---|
253 | }
|
---|
254 |
|
---|
255 | switch (initerror) {
|
---|
256 | case JPLIS_INIT_ERROR_NONE:
|
---|
257 | result = JNI_OK;
|
---|
258 | break;
|
---|
259 | case JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT:
|
---|
260 | result = JNI_ERR;
|
---|
261 | fprintf(stderr, "java.lang.instrument/-javaagent: cannot create native agent.\n");
|
---|
262 | break;
|
---|
263 | case JPLIS_INIT_ERROR_FAILURE:
|
---|
264 | result = JNI_ERR;
|
---|
265 | fprintf(stderr, "java.lang.instrument/-javaagent: initialization of native agent failed.\n");
|
---|
266 | break;
|
---|
267 | case JPLIS_INIT_ERROR_ALLOCATION_FAILURE:
|
---|
268 | result = JNI_ERR;
|
---|
269 | fprintf(stderr, "java.lang.instrument/-javaagent: allocation failure.\n");
|
---|
270 | break;
|
---|
271 | case JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED:
|
---|
272 | result = JNI_ERR;
|
---|
273 | fprintf(stderr, "-javaagent: agent class not specified.\n");
|
---|
274 | break;
|
---|
275 | default:
|
---|
276 | result = JNI_ERR;
|
---|
277 | fprintf(stderr, "java.lang.instrument/-javaagent: unknown error\n");
|
---|
278 | break;
|
---|
279 | }
|
---|
280 | return result;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0
|
---|
285 | * indicates an error. To allow the attach mechanism throw an
|
---|
286 | * AgentInitializationException with a reasonable exception message we define
|
---|
287 | * a few specific errors here.
|
---|
288 | */
|
---|
289 | #define AGENT_ERROR_BADJAR ((jint)100) /* Agent JAR not found or no Agent-Class attribute */
|
---|
290 | #define AGENT_ERROR_NOTONCP ((jint)101) /* Unable to add JAR file to system class path */
|
---|
291 | #define AGENT_ERROR_STARTFAIL ((jint)102) /* No agentmain method or agentmain failed */
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * This will be called once each time a tool attaches to the VM and loads
|
---|
295 | * the JPLIS library.
|
---|
296 | */
|
---|
297 | JNIEXPORT jint JNICALL
|
---|
298 | Agent_OnAttach(JavaVM* vm, char *args, void * reserved) {
|
---|
299 | JPLISInitializationError initerror = JPLIS_INIT_ERROR_NONE;
|
---|
300 | jint result = JNI_OK;
|
---|
301 | JPLISAgent * agent = NULL;
|
---|
302 | JNIEnv * jni_env = NULL;
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Need JNIEnv - guaranteed to be called from thread that is already
|
---|
306 | * attached to VM
|
---|
307 | */
|
---|
308 | result = (*vm)->GetEnv(vm, (void**)&jni_env, JNI_VERSION_1_2);
|
---|
309 | jplis_assert(result==JNI_OK);
|
---|
310 |
|
---|
311 | initerror = createNewJPLISAgent(vm, &agent);
|
---|
312 | if ( initerror == JPLIS_INIT_ERROR_NONE ) {
|
---|
313 | int oldLen, newLen;
|
---|
314 | char * jarfile;
|
---|
315 | char * options;
|
---|
316 | jarAttribute* attributes;
|
---|
317 | char * agentClass;
|
---|
318 | char * bootClassPath;
|
---|
319 | jboolean success;
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * Parse <jarfile>[=options] into jarfile and options
|
---|
323 | */
|
---|
324 | if (parseArgumentTail(args, &jarfile, &options) != 0) {
|
---|
325 | return JNI_ENOMEM;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * Open the JAR file and parse the manifest
|
---|
330 | */
|
---|
331 | attributes = readAttributes( jarfile );
|
---|
332 | if (attributes == NULL) {
|
---|
333 | fprintf(stderr, "Error opening zip file or JAR manifest missing: %s\n", jarfile);
|
---|
334 | free(jarfile);
|
---|
335 | if (options != NULL) free(options);
|
---|
336 | return AGENT_ERROR_BADJAR;
|
---|
337 | }
|
---|
338 |
|
---|
339 | agentClass = getAttribute(attributes, "Agent-Class");
|
---|
340 | if (agentClass == NULL) {
|
---|
341 | fprintf(stderr, "Failed to find Agent-Class manifest attribute from %s\n",
|
---|
342 | jarfile);
|
---|
343 | free(jarfile);
|
---|
344 | if (options != NULL) free(options);
|
---|
345 | freeAttributes(attributes);
|
---|
346 | return AGENT_ERROR_BADJAR;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * Add the jarfile to the system class path
|
---|
351 | */
|
---|
352 | if (appendClassPath(agent, jarfile)) {
|
---|
353 | fprintf(stderr, "Unable to add %s to system class path "
|
---|
354 | "- not supported by system class loader or configuration error!\n",
|
---|
355 | jarfile);
|
---|
356 | free(jarfile);
|
---|
357 | if (options != NULL) free(options);
|
---|
358 | freeAttributes(attributes);
|
---|
359 | return AGENT_ERROR_NOTONCP;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * The value of the Agent-Class attribute becomes the agent
|
---|
364 | * class name. The manifest is in UTF8 so need to convert to
|
---|
365 | * modified UTF8 (see JNI spec).
|
---|
366 | */
|
---|
367 | oldLen = strlen(agentClass);
|
---|
368 | newLen = modifiedUtf8LengthOfUtf8(agentClass, oldLen);
|
---|
369 | if (newLen == oldLen) {
|
---|
370 | agentClass = strdup(agentClass);
|
---|
371 | } else {
|
---|
372 | char* str = (char*)malloc( newLen+1 );
|
---|
373 | if (str != NULL) {
|
---|
374 | convertUtf8ToModifiedUtf8(agentClass, oldLen, str, newLen);
|
---|
375 | }
|
---|
376 | agentClass = str;
|
---|
377 | }
|
---|
378 | if (agentClass == NULL) {
|
---|
379 | free(jarfile);
|
---|
380 | if (options != NULL) free(options);
|
---|
381 | freeAttributes(attributes);
|
---|
382 | return JNI_ENOMEM;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * If the Boot-Class-Path attribute is specified then we process
|
---|
387 | * each URL - in the live phase only JAR files will be added.
|
---|
388 | */
|
---|
389 | bootClassPath = getAttribute(attributes, "Boot-Class-Path");
|
---|
390 | if (bootClassPath != NULL) {
|
---|
391 | appendBootClassPath(agent, jarfile, bootClassPath);
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Convert JAR attributes into agent capabilities
|
---|
396 | */
|
---|
397 | convertCapabilityAtrributes(attributes, agent);
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * Create the java.lang.instrument.Instrumentation instance
|
---|
401 | */
|
---|
402 | success = createInstrumentationImpl(jni_env, agent);
|
---|
403 | jplis_assert(success);
|
---|
404 |
|
---|
405 | /*
|
---|
406 | * Turn on the ClassFileLoadHook.
|
---|
407 | */
|
---|
408 | if (success) {
|
---|
409 | success = setLivePhaseEventHandlers(agent);
|
---|
410 | jplis_assert(success);
|
---|
411 | }
|
---|
412 |
|
---|
413 | /*
|
---|
414 | * Start the agent
|
---|
415 | */
|
---|
416 | if (success) {
|
---|
417 | success = startJavaAgent(agent,
|
---|
418 | jni_env,
|
---|
419 | agentClass,
|
---|
420 | options,
|
---|
421 | agent->mAgentmainCaller);
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (!success) {
|
---|
425 | fprintf(stderr, "Agent failed to start!\n");
|
---|
426 | result = AGENT_ERROR_STARTFAIL;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Clean-up
|
---|
431 | */
|
---|
432 | free(jarfile);
|
---|
433 | if (options != NULL) free(options);
|
---|
434 | free(agentClass);
|
---|
435 | freeAttributes(attributes);
|
---|
436 | }
|
---|
437 |
|
---|
438 | return result;
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | JNIEXPORT void JNICALL
|
---|
443 | Agent_OnUnload(JavaVM *vm) {
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * JVMTI callback support
|
---|
449 | *
|
---|
450 | * We have two "stages" of callback support.
|
---|
451 | * At OnLoad time, we install a VMInit handler.
|
---|
452 | * When the VMInit handler runs, we remove the VMInit handler and install a
|
---|
453 | * ClassFileLoadHook handler.
|
---|
454 | */
|
---|
455 |
|
---|
456 | void JNICALL
|
---|
457 | eventHandlerVMInit( jvmtiEnv * jvmtienv,
|
---|
458 | JNIEnv * jnienv,
|
---|
459 | jthread thread) {
|
---|
460 | JPLISEnvironment * environment = NULL;
|
---|
461 | jboolean success = JNI_FALSE;
|
---|
462 |
|
---|
463 | environment = getJPLISEnvironment(jvmtienv);
|
---|
464 |
|
---|
465 | /* process the premain calls on the all the JPL agents */
|
---|
466 | if ( environment != NULL ) {
|
---|
467 | jthrowable outstandingException = preserveThrowable(jnienv);
|
---|
468 | success = processJavaStart( environment->mAgent,
|
---|
469 | jnienv);
|
---|
470 | restoreThrowable(jnienv, outstandingException);
|
---|
471 | }
|
---|
472 |
|
---|
473 | /* if we fail to start cleanly, bring down the JVM */
|
---|
474 | if ( !success ) {
|
---|
475 | abortJVM(jnienv, JPLIS_ERRORMESSAGE_CANNOTSTART);
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | void JNICALL
|
---|
480 | eventHandlerClassFileLoadHook( jvmtiEnv * jvmtienv,
|
---|
481 | JNIEnv * jnienv,
|
---|
482 | jclass class_being_redefined,
|
---|
483 | jobject loader,
|
---|
484 | const char* name,
|
---|
485 | jobject protectionDomain,
|
---|
486 | jint class_data_len,
|
---|
487 | const unsigned char* class_data,
|
---|
488 | jint* new_class_data_len,
|
---|
489 | unsigned char** new_class_data) {
|
---|
490 | JPLISEnvironment * environment = NULL;
|
---|
491 |
|
---|
492 | environment = getJPLISEnvironment(jvmtienv);
|
---|
493 |
|
---|
494 | /* if something is internally inconsistent (no agent), just silently return without touching the buffer */
|
---|
495 | if ( environment != NULL ) {
|
---|
496 | jthrowable outstandingException = preserveThrowable(jnienv);
|
---|
497 | transformClassFile( environment->mAgent,
|
---|
498 | jnienv,
|
---|
499 | loader,
|
---|
500 | name,
|
---|
501 | class_being_redefined,
|
---|
502 | protectionDomain,
|
---|
503 | class_data_len,
|
---|
504 | class_data,
|
---|
505 | new_class_data_len,
|
---|
506 | new_class_data,
|
---|
507 | environment->mIsRetransformer);
|
---|
508 | restoreThrowable(jnienv, outstandingException);
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 |
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * URLs in Boot-Class-Path attributes are separated by one or more spaces.
|
---|
517 | * This function splits the attribute value into a list of path segments.
|
---|
518 | * The attribute value is in UTF8 but cannot contain NUL. Also non US-ASCII
|
---|
519 | * characters must be escaped (URI syntax) so safe to iterate through the
|
---|
520 | * value as a C string.
|
---|
521 | */
|
---|
522 | static void
|
---|
523 | splitPathList(const char* str, int* pathCount, char*** paths) {
|
---|
524 | int count = 0;
|
---|
525 | char** segments = NULL;
|
---|
526 | char* c = (char*) str;
|
---|
527 | while (*c != '\0') {
|
---|
528 | while (*c == ' ') c++; /* skip leading spaces */
|
---|
529 | if (*c == '\0') {
|
---|
530 | break;
|
---|
531 | }
|
---|
532 | if (segments == NULL) {
|
---|
533 | segments = (char**)malloc( sizeof(char**) );
|
---|
534 | } else {
|
---|
535 | segments = (char**)realloc( segments, (count+1)*sizeof(char**) );
|
---|
536 | }
|
---|
537 | jplis_assert(segments != (char**)NULL);
|
---|
538 | segments[count++] = c;
|
---|
539 | c = strchr(c, ' ');
|
---|
540 | if (c == NULL) {
|
---|
541 | break;
|
---|
542 | }
|
---|
543 | *c = '\0';
|
---|
544 | c++;
|
---|
545 | }
|
---|
546 | *pathCount = count;
|
---|
547 | *paths = segments;
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | /* URI path decoding - ported from src/share/classes/java/net/URI.java */
|
---|
552 |
|
---|
553 | static int
|
---|
554 | decodeNibble(char c) {
|
---|
555 | if ((c >= '0') && (c <= '9'))
|
---|
556 | return c - '0';
|
---|
557 | if ((c >= 'a') && (c <= 'f'))
|
---|
558 | return c - 'a' + 10;
|
---|
559 | if ((c >= 'A') && (c <= 'F'))
|
---|
560 | return c - 'A' + 10;
|
---|
561 | return -1;
|
---|
562 | }
|
---|
563 |
|
---|
564 | static int
|
---|
565 | decodeByte(char c1, char c2) {
|
---|
566 | return (((decodeNibble(c1) & 0xf) << 4) | ((decodeNibble(c2) & 0xf) << 0));
|
---|
567 | }
|
---|
568 |
|
---|
569 | /*
|
---|
570 | * Evaluates all escapes in s. Assumes that escapes are well-formed
|
---|
571 | * syntactically, i.e., of the form %XX.
|
---|
572 | * If the path does not require decoding the the original path is
|
---|
573 | * returned. Otherwise the decoded path (heap allocated) is returned,
|
---|
574 | * along with the length of the decoded path. Note that the return
|
---|
575 | * string will not be null terminated after decoding.
|
---|
576 | */
|
---|
577 | static
|
---|
578 | char *decodePath(const char *s, int* decodedLen) {
|
---|
579 | int n;
|
---|
580 | char *result;
|
---|
581 | char *resultp;
|
---|
582 | int c;
|
---|
583 | int i;
|
---|
584 |
|
---|
585 | n = (int)strlen(s);
|
---|
586 | if (n == 0) {
|
---|
587 | *decodedLen = 0;
|
---|
588 | return (char*)s;
|
---|
589 | }
|
---|
590 | if (strchr(s, '%') == NULL) {
|
---|
591 | *decodedLen = n;
|
---|
592 | return (char*)s; /* no escapes, we are done */
|
---|
593 | }
|
---|
594 |
|
---|
595 | resultp = result = calloc(n+1, 1);
|
---|
596 | c = s[0];
|
---|
597 | for (i = 0; i < n;) {
|
---|
598 | if (c != '%') {
|
---|
599 | *resultp++ = c;
|
---|
600 | if (++i >= n)
|
---|
601 | break;
|
---|
602 | c = s[i];
|
---|
603 | continue;
|
---|
604 | }
|
---|
605 | for (;;) {
|
---|
606 | char b1 = s[++i];
|
---|
607 | char b2 = s[++i];
|
---|
608 | int decoded = decodeByte(b1, b2);
|
---|
609 | *resultp++ = decoded;
|
---|
610 | if (++i >= n)
|
---|
611 | break;
|
---|
612 | c = s[i];
|
---|
613 | if (c != '%')
|
---|
614 | break;
|
---|
615 | }
|
---|
616 | }
|
---|
617 | *decodedLen = (int)(resultp - result);
|
---|
618 | return result; // not null terminated.
|
---|
619 | }
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * Append the given jar file to the system class path. This should succeed in the
|
---|
623 | * onload phase but may fail in the live phase if the system class loader doesn't
|
---|
624 | * support appending to the class path.
|
---|
625 | */
|
---|
626 | static int
|
---|
627 | appendClassPath( JPLISAgent* agent,
|
---|
628 | const char* jarfile ) {
|
---|
629 | jvmtiEnv* jvmtienv = jvmti(agent);
|
---|
630 | jvmtiError jvmtierr;
|
---|
631 |
|
---|
632 | jvmtierr = (*jvmtienv)->AddToSystemClassLoaderSearch(jvmtienv, jarfile);
|
---|
633 |
|
---|
634 | if (jvmtierr == JVMTI_ERROR_NONE) {
|
---|
635 | return 0;
|
---|
636 | } else {
|
---|
637 | jvmtiPhase phase;
|
---|
638 | jvmtiError err;
|
---|
639 |
|
---|
640 | err = (*jvmtienv)->GetPhase(jvmtienv, &phase);
|
---|
641 | jplis_assert(err == JVMTI_ERROR_NONE);
|
---|
642 |
|
---|
643 | if (phase == JVMTI_PHASE_LIVE) {
|
---|
644 | switch (jvmtierr) {
|
---|
645 | case JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED :
|
---|
646 | fprintf(stderr, "System class loader does not support adding "
|
---|
647 | "JAR file to system class path during the live phase!\n");
|
---|
648 | break;
|
---|
649 | default:
|
---|
650 | fprintf(stderr, "Unexpected error (%d) returned by "
|
---|
651 | "AddToSystemClassLoaderSearch\n", jvmtierr);
|
---|
652 | break;
|
---|
653 | }
|
---|
654 | return -1;
|
---|
655 | }
|
---|
656 | jplis_assert(0);
|
---|
657 | }
|
---|
658 | return -2;
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * res = func, free'ing the previous value of 'res' if function
|
---|
664 | * returns a new result.
|
---|
665 | */
|
---|
666 | #define TRANSFORM(res,func) { \
|
---|
667 | char* tmp = func; \
|
---|
668 | if (tmp != res) { \
|
---|
669 | free(res); \
|
---|
670 | res = tmp; \
|
---|
671 | } \
|
---|
672 | jplis_assert((void*)res != (void*)NULL); \
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 | /*
|
---|
677 | * This function takes the value of the Boot-Class-Path attribute,
|
---|
678 | * splits it into the individual path segments, and then combines it
|
---|
679 | * with the path to the jar file to create the path to be added
|
---|
680 | * to the bootclasspath.
|
---|
681 | *
|
---|
682 | * Each individual path segment starts out as a UTF8 string. Additionally
|
---|
683 | * as the path is specified to use URI path syntax all non US-ASCII
|
---|
684 | * characters are escaped. Once the URI path is decoded we get a UTF8
|
---|
685 | * string which must then be converted to the platform encoding (as it
|
---|
686 | * will be combined with the platform path of the jar file). Once
|
---|
687 | * converted it is then normalized (remove duplicate slashes, etc.).
|
---|
688 | * If the resulting path is an absolute path (starts with a slash for
|
---|
689 | * example) then the path will be added to the bootclasspath. Otherwise
|
---|
690 | * if it's not absolute then we get the canoncial path of the agent jar
|
---|
691 | * file and then resolve the path in the context of the base path of
|
---|
692 | * the agent jar.
|
---|
693 | */
|
---|
694 | static void
|
---|
695 | appendBootClassPath( JPLISAgent* agent,
|
---|
696 | const char* jarfile,
|
---|
697 | const char* pathList ) {
|
---|
698 | char canonicalPath[MAXPATHLEN];
|
---|
699 | char *parent = NULL;
|
---|
700 | int haveBasePath = 0;
|
---|
701 |
|
---|
702 | int count, i;
|
---|
703 | char **paths;
|
---|
704 | jvmtiEnv* jvmtienv = jvmti(agent);
|
---|
705 | jvmtiError jvmtierr;
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * Split the attribute value into the individual path segments
|
---|
709 | * and process each in sequence
|
---|
710 | */
|
---|
711 | splitPathList(pathList, &count, &paths);
|
---|
712 |
|
---|
713 | for (i=0; i<count; i++) {
|
---|
714 | int len;
|
---|
715 | char* path;
|
---|
716 | char* pos;
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * The path segment at this point is a pointer into the attribute
|
---|
720 | * value. As it will go through a number of transformation (tossing away
|
---|
721 | * the previous results as we go along) it make it easier if the path
|
---|
722 | * starts out as a heap allocated string.
|
---|
723 | */
|
---|
724 | path = strdup(paths[i]);
|
---|
725 | jplis_assert(path != (char*)NULL);
|
---|
726 |
|
---|
727 | /*
|
---|
728 | * The attribute is specified to be a list of relative URIs so in theory
|
---|
729 | * there could be a query component - if so, get rid of it.
|
---|
730 | */
|
---|
731 | pos = strchr(path, '?');
|
---|
732 | if (pos != NULL) {
|
---|
733 | *pos = '\0';
|
---|
734 | }
|
---|
735 |
|
---|
736 | /*
|
---|
737 | * Check for characters that are not allowed in the path component of
|
---|
738 | * a URI.
|
---|
739 | */
|
---|
740 | if (validatePathChars(path)) {
|
---|
741 | fprintf(stderr, "WARNING: illegal character in Boot-Class-Path value: %s\n",
|
---|
742 | path);
|
---|
743 | free(path);
|
---|
744 | continue;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | /*
|
---|
749 | * Next decode any escaped characters. The result is a UTF8 string.
|
---|
750 | */
|
---|
751 | TRANSFORM(path, decodePath(path,&len));
|
---|
752 |
|
---|
753 | /*
|
---|
754 | * Convert to the platform encoding
|
---|
755 | */
|
---|
756 | {
|
---|
757 | char platform[MAXPATHLEN];
|
---|
758 | int new_len = convertUft8ToPlatformString(path, len, platform, MAXPATHLEN);
|
---|
759 | free(path);
|
---|
760 | if (new_len < 0) {
|
---|
761 | /* bogus value - exceeds maximum path size or unable to convert */
|
---|
762 | continue;
|
---|
763 | }
|
---|
764 | path = strdup(platform);
|
---|
765 | jplis_assert(path != (char*)NULL);
|
---|
766 | }
|
---|
767 |
|
---|
768 | /*
|
---|
769 | * Post-process the URI path - needed on Windows to transform
|
---|
770 | * /c:/foo to c:/foo.
|
---|
771 | */
|
---|
772 | TRANSFORM(path, fromURIPath(path));
|
---|
773 |
|
---|
774 | /*
|
---|
775 | * Normalize the path - no duplicate slashes (except UNCs on Windows), trailing
|
---|
776 | * slash removed.
|
---|
777 | */
|
---|
778 | TRANSFORM(path, normalize(path));
|
---|
779 |
|
---|
780 | /*
|
---|
781 | * If the path is an absolute path then add to the bootclassloader
|
---|
782 | * search path. Otherwise we get the canonical path of the agent jar
|
---|
783 | * and then use its base path (directory) to resolve the given path
|
---|
784 | * segment.
|
---|
785 | *
|
---|
786 | * NOTE: JVMTI is specified to use modified UTF8 strings (like JNI).
|
---|
787 | * In 1.5.0 the AddToBootstrapClassLoaderSearch takes a platform string
|
---|
788 | * - see 5049313.
|
---|
789 | */
|
---|
790 | if (isAbsolute(path)) {
|
---|
791 | jvmtierr = (*jvmtienv)->AddToBootstrapClassLoaderSearch(jvmtienv, path);
|
---|
792 | } else {
|
---|
793 | char* resolved;
|
---|
794 |
|
---|
795 | if (!haveBasePath) {
|
---|
796 | if (canonicalize((char*)jarfile, canonicalPath, sizeof(canonicalPath)) != 0) {
|
---|
797 | fprintf(stderr, "WARNING: unable to canonicalize %s\n", jarfile);
|
---|
798 | free(path);
|
---|
799 | continue;
|
---|
800 | }
|
---|
801 | parent = basePath(canonicalPath);
|
---|
802 | jplis_assert(parent != (char*)NULL);
|
---|
803 | haveBasePath = 1;
|
---|
804 | }
|
---|
805 |
|
---|
806 | resolved = resolve(parent, path);
|
---|
807 | jvmtierr = (*jvmtienv)->AddToBootstrapClassLoaderSearch(jvmtienv, resolved);
|
---|
808 | }
|
---|
809 |
|
---|
810 | /* print warning if boot class path not updated */
|
---|
811 | if (jvmtierr != JVMTI_ERROR_NONE) {
|
---|
812 | fprintf(stderr, "WARNING: %s not added to bootstrap class loader search: ", path);
|
---|
813 | switch (jvmtierr) {
|
---|
814 | case JVMTI_ERROR_ILLEGAL_ARGUMENT :
|
---|
815 | fprintf(stderr, "Illegal argument or not JAR file\n");
|
---|
816 | break;
|
---|
817 | default:
|
---|
818 | fprintf(stderr, "Unexpected error: %d\n", jvmtierr);
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | /* finished with the path */
|
---|
823 | free(path);
|
---|
824 | }
|
---|
825 |
|
---|
826 |
|
---|
827 | /* clean-up */
|
---|
828 | if (haveBasePath && parent != canonicalPath) {
|
---|
829 | free(parent);
|
---|
830 | }
|
---|
831 | }
|
---|