Commit bd449e19 authored by Lubomir Bulej's avatar Lubomir Bulej

DiSL-Agent: added jvmti_alloc_copy() to duplicate buffers into JVM memory.

parent ab60654e
......@@ -238,18 +238,10 @@ jvmti_callback_class_file_load (
);
if (class_changed) {
unsigned char * jvm_class_bytes;
jvmtiError error = (*jvmti)->Allocate (
jvmti, (jlong) class_def.class_byte_count, &jvm_class_bytes
unsigned char * jvm_class_bytes = jvmti_alloc_copy (
jvmti, class_def.class_bytes, class_def.class_byte_count
);
check_jvmti_error (
jvmti, error,
"failed to allocate memory for the instrumented class"
);
//
memcpy (jvm_class_bytes, class_def.class_bytes, class_def.class_byte_count);
free ((void *) class_def.class_bytes);
*new_class_byte_count = class_def.class_byte_count;
......@@ -270,7 +262,7 @@ jvmti_callback_class_file_load (
// ****************************************************************************
// JVMTI EVENT: VM START
// JVMTI EVENT: VM INIT
// ****************************************************************************
static void JNICALL
......
......@@ -17,6 +17,24 @@
#endif
/**
* Allocates JVM memory for the given buffer and copies the buffer
* into JVM memory.
*/
unsigned char *
jvmti_alloc_copy (jvmtiEnv * jvmti, const void * src, size_t size) {
assert (jvmti != NULL);
assert (src != NULL);
unsigned char * jvm_dst;
jvmtiError error = (*jvmti)->Allocate (jvmti, (jlong) size, &jvm_dst);
check_jvmti_error (jvmti, error, "failed create a JVM copy of a buffer");
memcpy (jvm_dst, src, size);
return jvm_dst;
}
/**
* Redefines a class given name and (partial) class definition.
* The class to be redefined is first looked up using JNI to
......@@ -40,9 +58,14 @@ jvmti_redefine_class (
}
//
jvmtiClassDefinition new_classdef = * class_def;
new_classdef.klass = class;
jvmtiClassDefinition new_classdef = {
.klass = class,
.class_byte_count = class_def->class_byte_count,
.class_bytes = jvmti_alloc_copy (
jvmti, class_def->class_bytes, class_def->class_byte_count
),
};
jvmtiError error = (*jvmti)->RedefineClasses (jvmti, 1, &new_classdef);
check_jvmti_error (jvmti, error, "failed to redefine class");
......
......@@ -8,6 +8,8 @@
void report_jvmti_error (jvmtiEnv * jvmti, jvmtiError error, const char * message);
unsigned char * jvmti_alloc_copy (jvmtiEnv * jvmti, const void * src, size_t size);
bool jvmti_redefine_class (
jvmtiEnv * jvmti, JNIEnv * jni,
const char * name, const jvmtiClassDefinition * definition
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment