/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Copyright 2003 Wily Technology, Inc.
*/
#include <string.h>
#include <stdlib.h>
#include "jni.h"
#include "Utilities.h"
#include "JPLISAssert.h"
#include "JPLISAgent.h"
#include "JavaExceptions.h"
#include "EncodingSupport.h"
#include "FileSystemSupport.h"
#include "JarFacade.h"
#include "PathCharsValidator.h"
/**
* This module contains the direct interface points with the JVMTI.
* The OnLoad handler is here, along with the various event handlers.
*/
static int
const char* jarfile);
static void
const char* jarfile,
const char* pathList);
/*
* Parse -javaagent tail, of the form name[=options], into name
* and options. Returned values are heap allocated and options maybe
* NULL. Returns 0 if parse succeeds, -1 if allocation fails.
*/
static int
int len;
char* pos;
return -1;
}
} else {
return -1;
}
}
return 0;
}
/*
* Get the value of an attribute in an attribute list. Returns NULL
* if attribute not found.
*/
}
/*
* Parse any capability settings in the JAR manifest and
* convert them to JVM TI capabilities.
*/
void
/* set redefineClasses capability */
}
/* create an environment which has the retransformClasses capability */
}
/* set setNativeMethodPrefix capability */
}
/* for retransformClasses testing, set capability to use original method order */
}
}
/*
* This will be called once for every -javaagent on the command line.
* Each call to Agent_OnLoad will create its own agent and agent data.
*
* The argument tail string provided to Agent_OnLoad will be of form
* <jarfile>[=<options>]. The tail string is split into the jarfile and
* options components. The jarfile manifest is parsed and the value of the
* Premain-Class attribute will become the agent's premain class. The jar
* file is then added to the system class path, and if the Boot-Class-Path
* attribute is present then all relative URLs in the value are processed
* to create boot class path segments to append to the boot class path.
*/
if ( initerror == JPLIS_INIT_ERROR_NONE ) {
char * jarfile;
char * options;
char * premainClass;
char * agentClass;
char * bootClassPath;
/*
* Parse <jarfile>[=options] into jarfile and options
*/
return JNI_ERR;
}
/*
* Agent_OnLoad is specified to provide the agent options
* argument tail in modified UTF8. However for 1.5.0 this is
* actually in the platform encoding - see 5049313.
*
* not a zip file return error. Also if Premain-Class attribute
* isn't present we return an error.
*/
if (attributes == NULL) {
return JNI_ERR;
}
if (premainClass == NULL) {
jarfile);
return JNI_ERR;
}
/*
* Add to the jarfile
*/
/*
* The value of the Premain-Class attribute becomes the agent
* class name. The manifest is in UTF8 so need to convert to
* modified UTF8 (see JNI spec).
*/
} else {
}
premainClass = str;
}
if (premainClass == NULL) {
return JNI_ERR;
}
/*
* If the Boot-Class-Path attribute is specified then we process
* each relative URL and add it to the bootclasspath.
*/
if (bootClassPath != NULL) {
}
/*
* Convert JAR attributes into agent capabilities
*/
/*
* Track (record) the agent class name and options data
*/
/*
* Clean-up
*/
}
switch (initerror) {
case JPLIS_INIT_ERROR_NONE:
break;
break;
case JPLIS_INIT_ERROR_FAILURE:
break;
break;
break;
default:
break;
}
return result;
}
/*
* Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0
* indicates an error. To allow the attach mechanism throw an
* AgentInitializationException with a reasonable exception message we define
* a few specific errors here.
*/
/*
* This will be called once each time a tool attaches to the VM and loads
* the JPLIS library.
*/
/*
* Need JNIEnv - guaranteed to be called from thread that is already
* attached to VM
*/
if ( initerror == JPLIS_INIT_ERROR_NONE ) {
char * jarfile;
char * options;
char * agentClass;
char * bootClassPath;
/*
* Parse <jarfile>[=options] into jarfile and options
*/
return JNI_ENOMEM;
}
/*
* Open the JAR file and parse the manifest
*/
if (attributes == NULL) {
return AGENT_ERROR_BADJAR;
}
if (agentClass == NULL) {
jarfile);
return AGENT_ERROR_BADJAR;
}
/*
* Add the jarfile to the system class path
*/
"- not supported by system class loader or configuration error!\n",
jarfile);
return AGENT_ERROR_NOTONCP;
}
/*
* The value of the Agent-Class attribute becomes the agent
* class name. The manifest is in UTF8 so need to convert to
* modified UTF8 (see JNI spec).
*/
} else {
}
agentClass = str;
}
if (agentClass == NULL) {
return JNI_ENOMEM;
}
/*
* If the Boot-Class-Path attribute is specified then we process
* each URL - in the live phase only JAR files will be added.
*/
if (bootClassPath != NULL) {
}
/*
* Convert JAR attributes into agent capabilities
*/
/*
* Create the java.lang.instrument.Instrumentation instance
*/
/*
* Turn on the ClassFileLoadHook.
*/
if (success) {
}
/*
* Start the agent
*/
if (success) {
}
if (!success) {
}
/*
* Clean-up
*/
}
return result;
}
}
/*
* JVMTI callback support
*
* We have two "stages" of callback support.
* At OnLoad time, we install a VMInit handler.
* When the VMInit handler runs, we remove the VMInit handler and install a
* ClassFileLoadHook handler.
*/
void JNICALL
/* process the premain calls on the all the JPL agents */
if ( environment != NULL ) {
jnienv);
}
/* if we fail to start cleanly, bring down the JVM */
if ( !success ) {
}
}
void JNICALL
const char* name,
const unsigned char* class_data,
unsigned char** new_class_data) {
/* if something is internally inconsistent (no agent), just silently return without touching the buffer */
if ( environment != NULL ) {
name,
}
}
/*
* URLs in Boot-Class-Path attributes are separated by one or more spaces.
* This function splits the attribute value into a list of path segments.
* The attribute value is in UTF8 but cannot contain NUL. Also non US-ASCII
* characters must be escaped (URI syntax) so safe to iterate through the
* value as a C string.
*/
static void
int count = 0;
char* c = (char*) str;
while (*c != '\0') {
while (*c == ' ') c++; /* skip leading spaces */
if (*c == '\0') {
break;
}
} else {
}
c = strchr(c, ' ');
if (c == NULL) {
break;
}
*c = '\0';
c++;
}
}
static int
decodeNibble(char c) {
if ((c >= '0') && (c <= '9'))
return c - '0';
if ((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
if ((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
return -1;
}
static int
}
/*
* Evaluates all escapes in s. Assumes that escapes are well-formed
* syntactically, i.e., of the form %XX.
* If the path does not require decoding the the original path is
* returned. Otherwise the decoded path (heap allocated) is returned,
* along with the length of the decoded path. Note that the return
* string will not be null terminated after decoding.
*/
static
int n;
char *result;
char *resultp;
int c;
int i;
n = (int)strlen(s);
if (n == 0) {
*decodedLen = 0;
return (char*)s;
}
*decodedLen = n;
return (char*)s; /* no escapes, we are done */
}
c = s[0];
for (i = 0; i < n;) {
if (c != '%') {
*resultp++ = c;
if (++i >= n)
break;
c = s[i];
continue;
}
for (;;) {
char b1 = s[++i];
char b2 = s[++i];
if (++i >= n)
break;
c = s[i];
if (c != '%')
break;
}
}
return result; // not null terminated.
}
/*
* Append the given jar file to the system class path. This should succeed in the
* onload phase but may fail in the live phase if the system class loader doesn't
* support appending to the class path.
*/
static int
const char* jarfile ) {
if (jvmtierr == JVMTI_ERROR_NONE) {
return 0;
} else {
/* can be called from any phase */
if (phase == JVMTI_PHASE_LIVE) {
switch (jvmtierr) {
"JAR file to system class path during the live phase!\n");
break;
default:
"AddToSystemClassLoaderSearch\n", jvmtierr);
break;
}
return -1;
}
jplis_assert(0);
}
return -2;
}
/*
* res = func, free'ing the previous value of 'res' if function
* returns a new result.
*/
} \
}
/*
* This function takes the value of the Boot-Class-Path attribute,
* splits it into the individual path segments, and then combines it
* with the path to the jar file to create the path to be added
* to the bootclasspath.
*
* Each individual path segment starts out as a UTF8 string. Additionally
* as the path is specified to use URI path syntax all non US-ASCII
* characters are escaped. Once the URI path is decoded we get a UTF8
* string which must then be converted to the platform encoding (as it
* will be combined with the platform path of the jar file). Once
* converted it is then normalized (remove duplicate slashes, etc.).
* If the resulting path is an absolute path (starts with a slash for
* example) then the path will be added to the bootclasspath. Otherwise
* if it's not absolute then we get the canoncial path of the agent jar
* file and then resolve the path in the context of the base path of
* the agent jar.
*/
static void
const char* jarfile,
const char* pathList ) {
int haveBasePath = 0;
int count, i;
char **paths;
/*
* Split the attribute value into the individual path segments
* and process each in sequence
*/
for (i=0; i<count; i++) {
int len;
char* path;
char* pos;
/*
* The path segment at this point is a pointer into the attribute
* value. As it will go through a number of transformation (tossing away
* the previous results as we go along) it make it easier if the path
* starts out as a heap allocated string.
*/
/*
* The attribute is specified to be a list of relative URIs so in theory
* there could be a query component - if so, get rid of it.
*/
*pos = '\0';
}
/*
* Check for characters that are not allowed in the path component of
* a URI.
*/
if (validatePathChars(path)) {
path);
continue;
}
/*
* Next decode any escaped characters. The result is a UTF8 string.
*/
/*
* Convert to the platform encoding
*/
{
if (new_len < 0) {
/* bogus value - exceeds maximum path size or unable to convert */
continue;
}
}
/*
* Post-process the URI path - needed on Windows to transform
* /c:/foo to c:/foo.
*/
/*
* Normalize the path - no duplicate slashes (except UNCs on Windows), trailing
* slash removed.
*/
/*
* If the path is an absolute path then add to the bootclassloader
* search path. Otherwise we get the canonical path of the agent jar
* and then use its base path (directory) to resolve the given path
* segment.
*
* NOTE: JVMTI is specified to use modified UTF8 strings (like JNI).
* In 1.5.0 the AddToBootstrapClassLoaderSearch takes a platform string
* - see 5049313.
*/
if (isAbsolute(path)) {
} else {
char* resolved;
if (!haveBasePath) {
continue;
}
haveBasePath = 1;
}
}
/* print warning if boot class path not updated */
if (jvmtierr != JVMTI_ERROR_NONE) {
switch (jvmtierr) {
case JVMTI_ERROR_ILLEGAL_ARGUMENT :
break;
default:
}
}
/* finished with the path */
}
/* clean-up */
}
}