|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* $Id$ |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* Intel - Initial API and implementation |
| 11 |
**********************************************************************/ |
| 12 |
|
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <map> |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include "BCIEng.h" |
| 20 |
#include "JClassBuilder.h" |
| 21 |
#include "ModuleJ.h" |
| 22 |
|
| 23 |
#include "JMemStream.h" |
| 24 |
#include "class_inerface_int.h" |
| 25 |
|
| 26 |
|
| 27 |
#include <jni.h> |
| 28 |
|
| 29 |
typedef unsigned long intptr; |
| 30 |
|
| 31 |
static JNIEnv *(*m_get_vm_pointer)(void **); |
| 32 |
static jclass m_j_l_class; |
| 33 |
static jclass m_j_l_classloader; |
| 34 |
static jmethodID m_j_l_class_getName; /*()Ljava/lang/String;*/ |
| 35 |
static jmethodID m_j_l_class_isInterface;/* ()Z */ |
| 36 |
static jmethodID m_j_l_class_getClassLoader; /*()Ljava/lang/ClassLoader;*/ |
| 37 |
static jmethodID m_j_l_classloader_findLoadedClass; /*(Ljava/lang/String;)Ljava/lang/Class;*/ |
| 38 |
static jmethodID m_j_l_classloader_getSystemClassLoader; /*()Ljava/lang/ClassLoader;*/ |
| 39 |
static jobject m_system_classloader; |
| 40 |
/** |
| 41 |
* Define class handler |
| 42 |
*/ |
| 43 |
/*[0xdead0001 - simplefied, 0xdead0002 - instrumented, 0xdead0000 - none]*/ |
| 44 |
#define CLASS_HANDLER_MASK 0xdeadffff |
| 45 |
#define CLASS_HANDLER_SIMPLEFIED 0xdead0001 |
| 46 |
#define CLASS_HANDLER_INSTRUMENTED 0xdead0002 |
| 47 |
#define CLASS_HANDLER_NONE 0xdead0000 |
| 48 |
#define IS_CLASS_INSTRUMENTED(x) (((x)->magic & CLASS_HANDLER_MASK) == CLASS_HANDLER_INSTRUMENTED) |
| 49 |
struct Class{ |
| 50 |
int magic; |
| 51 |
struct { |
| 52 |
class CModuleJ *instrumented_class; |
| 53 |
void *simplified_class; |
| 54 |
} X; /*XXX: possible union here will be more applieble */ |
| 55 |
int loaded; |
| 56 |
const char *class_name; /*used if loaded = 0*/ |
| 57 |
struct Class *super_class; |
| 58 |
}; |
| 59 |
|
| 60 |
typedef std::map<std::string,class_handler> name2ch_t; |
| 61 |
static name2ch_t m_name2ch; |
| 62 |
|
| 63 |
#define MEHTOD_HANDLER_MASK 0xbabeffff |
| 64 |
#define METHOD_HANDLER_SELF 0xbabe0001 |
| 65 |
#define METHOD_HANDLER_EXT 0xbabe0002 |
| 66 |
#define METHOD_HANDLER_NONE 0xbabe0000 |
| 67 |
struct Method { |
| 68 |
int magic; |
| 69 |
struct { |
| 70 |
class CMethodJ *instrumented_method; |
| 71 |
void *simplefied_method; |
| 72 |
} X; /*XXX: possible union here will be more applieble */ |
| 73 |
class_handler class_owner; |
| 74 |
const char *method_name; /*used for METHOD_HANDLER_EXT only*/ |
| 75 |
const char *method_descriptor; /*used for METHOD_HANDLER_EXT only*/ |
| 76 |
}; |
| 77 |
|
| 78 |
static struct Class *m_j_l_object_ch; |
| 79 |
/** |
| 80 |
* Define field handler |
| 81 |
*/ |
| 82 |
typedef struct Field_ * field_handler; |
| 83 |
|
| 84 |
/** |
| 85 |
* Define method handler |
| 86 |
*/ |
| 87 |
//typedef struct Method_ * method_handler; |
| 88 |
|
| 89 |
/** |
| 90 |
* Define class loader handler |
| 91 |
*/ |
| 92 |
typedef struct ClassLoader_ * classloader_handler; |
| 93 |
|
| 94 |
static int |
| 95 |
create_class_handler(class_handler *pch, int flag, const char *class_name, int loaded, void *dsk) |
| 96 |
{ |
| 97 |
class_handler ch0 = m_name2ch[class_name]; /*for debug*/ |
| 98 |
if (m_name2ch[class_name] != NULL |
| 99 |
&& m_name2ch[class_name]->magic == flag) { |
| 100 |
*pch = m_name2ch[class_name]; |
| 101 |
return (0); |
| 102 |
} |
| 103 |
class_handler ch = (class_handler)malloc(sizeof(struct Class)); |
| 104 |
memset(ch, 0, sizeof(struct Class)); |
| 105 |
assert(ch); |
| 106 |
ch->loaded = loaded; |
| 107 |
if (loaded == 0) { |
| 108 |
ch->class_name = class_name; |
| 109 |
} |
| 110 |
ch->magic = flag; |
| 111 |
switch(flag) { |
| 112 |
case (CLASS_HANDLER_SIMPLEFIED): |
| 113 |
ch->X.simplified_class = dsk; |
| 114 |
if (loaded == 0) { |
| 115 |
ch->class_name = class_name; |
| 116 |
} |
| 117 |
ch->loaded = loaded; |
| 118 |
break; |
| 119 |
case (CLASS_HANDLER_INSTRUMENTED): |
| 120 |
ch->X.instrumented_class = (CModuleJ *)dsk; |
| 121 |
break; |
| 122 |
default: |
| 123 |
assert(!"shouldn't be here!!!"); |
| 124 |
} |
| 125 |
*pch = ch; |
| 126 |
m_name2ch[class_name] = ch; |
| 127 |
return (0); |
| 128 |
} |
| 129 |
|
| 130 |
extern "C" int |
| 131 |
create_method_handler(method_handler *pmh, class_handler ch, int flag, void *dsk) |
| 132 |
{ |
| 133 |
method_handler mh = (method_handler)malloc(sizeof(struct Method)); |
| 134 |
assert(mh); |
| 135 |
memset(mh, 0, sizeof(struct Method)); |
| 136 |
switch (flag) { |
| 137 |
case METHOD_HANDLER_SELF: |
| 138 |
mh->X.instrumented_method = (class CMethodJ *)dsk; |
| 139 |
break; |
| 140 |
case METHOD_HANDLER_EXT: |
| 141 |
mh->X.simplefied_method = dsk; |
| 142 |
break; |
| 143 |
default: |
| 144 |
assert(!"shouldn't enter here"); |
| 145 |
} |
| 146 |
mh->magic = (IS_CLASS_INSTRUMENTED(ch))?METHOD_HANDLER_SELF:flag; |
| 147 |
mh->class_owner = ch; |
| 148 |
*pmh = mh; |
| 149 |
return (0); |
| 150 |
} |
| 151 |
/** |
| 152 |
* Class interface |
| 153 |
*/ |
| 154 |
|
| 155 |
extern "C" unsigned short |
| 156 |
class_get_version( class_handler klass ) { |
| 157 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 158 |
return klass->X.instrumented_class->GetClassBuilder().GetMajorVersion(); |
| 159 |
} else { |
| 160 |
assert (!"not implemented yet!"); |
| 161 |
} |
| 162 |
return (0); |
| 163 |
} |
| 164 |
|
| 165 |
extern "C" const char * |
| 166 |
class_get_name( class_handler klass ) |
| 167 |
{ |
| 168 |
JNIEnv *env; |
| 169 |
jstring class_name; |
| 170 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 171 |
return klass->X.instrumented_class->GetName(); |
| 172 |
} |
| 173 |
else { |
| 174 |
if (klass->loaded == 0) { |
| 175 |
return(klass->class_name); |
| 176 |
} |
| 177 |
env = m_get_vm_pointer((void **)&env); |
| 178 |
class_name = (jstring)env->CallObjectMethod((jobject)klass->X.simplified_class, m_j_l_class_getName); |
| 179 |
if (class_name == NULL) |
| 180 |
return (NULL); |
| 181 |
char *class_name_simple = (char *)env->GetStringUTFChars(class_name, NULL); |
| 182 |
char *ch = strchr(class_name_simple, '.'); |
| 183 |
while (ch != NULL) { |
| 184 |
*ch = '/'; |
| 185 |
ch = strchr(ch, '.'); |
| 186 |
} |
| 187 |
return(class_name_simple); |
| 188 |
|
| 189 |
} |
| 190 |
return (NULL); |
| 191 |
} |
| 192 |
|
| 193 |
extern "C" classloader_handler |
| 194 |
class_get_class_loader( class_handler klass ) |
| 195 |
{ |
| 196 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 197 |
return (0); |
| 198 |
} else { |
| 199 |
assert(!"simplefied"); |
| 200 |
} |
| 201 |
return (NULL); |
| 202 |
} |
| 203 |
|
| 204 |
extern "C" class_handler |
| 205 |
class_get_super_class( class_handler klass ) |
| 206 |
{ |
| 207 |
|
| 208 |
const char *class_name; |
| 209 |
const char *super_class_name = NULL; |
| 210 |
jobject this_class = NULL; |
| 211 |
jobject super_class; |
| 212 |
JNIEnv *env = NULL; |
| 213 |
env = m_get_vm_pointer((void **)&env); |
| 214 |
assert(env); |
| 215 |
int loaded = 1; |
| 216 |
if (klass->loaded == 0 && strcmp(klass->class_name,"java/lang/Object") == 0) return (NULL); |
| 217 |
if (klass->super_class != NULL) return (klass->super_class); |
| 218 |
|
| 219 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 220 |
class_name = klass->X.instrumented_class->GetName(); |
| 221 |
CConstPool* cpool = klass->X.instrumented_class->GetClass().GetConstPool(); |
| 222 |
CCPUtf8Info* utfinfo = cpool->GetClass(klass->X.instrumented_class->GetClass().GetSuperClass()); |
| 223 |
super_class_name = (const char *)utfinfo->GetBytes(); |
| 224 |
assert(super_class_name); |
| 225 |
super_class = (jclass)env->FindClass(super_class_name); |
| 226 |
} |
| 227 |
else { |
| 228 |
if (klass->loaded == 0) { |
| 229 |
assert(klass->class_name); |
| 230 |
this_class = (jclass)env->FindClass(klass->class_name); |
| 231 |
if (this_class == NULL && env->ExceptionCheck()) { |
| 232 |
//env->ExceptionDescribe(); |
| 233 |
env->ExceptionClear(); |
| 234 |
//XXX: class wasn't found or loaded |
| 235 |
return (m_j_l_object_ch); |
| 236 |
//return (NULL); |
| 237 |
} |
| 238 |
} |
| 239 |
this_class = this_class?this_class:(jobject)klass->X.simplified_class; |
| 240 |
super_class = (jobject)env->GetSuperclass((jclass)this_class); |
| 241 |
} |
| 242 |
|
| 243 |
if (super_class == NULL) { |
| 244 |
if (env->ExceptionCheck()) { |
| 245 |
//env->ExceptionDescribe(); |
| 246 |
env->ExceptionClear(); |
| 247 |
//XXX: class wasn't found or loaded |
| 248 |
loaded = 0; |
| 249 |
//return (NULL); |
| 250 |
} |
| 251 |
else { |
| 252 |
klass->super_class = m_j_l_object_ch; |
| 253 |
return (m_j_l_object_ch); |
| 254 |
//return (NULL); /*it happends when klass is java.lang.Object*/ |
| 255 |
} |
| 256 |
} |
| 257 |
else { |
| 258 |
jstring jclass_name = (jstring)env->CallObjectMethod(super_class, m_j_l_class_getName); |
| 259 |
if (jclass_name == NULL) { |
| 260 |
return (m_j_l_object_ch); |
| 261 |
} |
| 262 |
char *class_name_simple = (char *)env->GetStringUTFChars(jclass_name, NULL); |
| 263 |
assert (class_name_simple); |
| 264 |
char *ch = strchr(class_name_simple, '.'); |
| 265 |
while (ch != NULL) { |
| 266 |
*ch = '/'; |
| 267 |
ch = strchr(ch, '.'); |
| 268 |
} |
| 269 |
super_class_name = class_name_simple; |
| 270 |
} |
| 271 |
|
| 272 |
//assert(super_class); |
| 273 |
class_handler ch; |
| 274 |
jobject super_class_ref = env->NewGlobalRef(super_class); |
| 275 |
env->DeleteLocalRef(super_class); |
| 276 |
int status = create_class_handler(&ch, CLASS_HANDLER_SIMPLEFIED, super_class_name, loaded, super_class_ref); |
| 277 |
assert(status == 0); |
| 278 |
return (ch); |
| 279 |
} |
| 280 |
|
| 281 |
extern "C" unsigned |
| 282 |
class_is_same_class( class_handler klass1, class_handler klass2 ) |
| 283 |
{ |
| 284 |
assert(!"verifier entered!!"); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static char* |
| 289 |
package_name(const char* kname) |
| 290 |
{ |
| 291 |
char *tokp; |
| 292 |
char *tok0; |
| 293 |
tokp = tok0 = (char *)&kname[0]; |
| 294 |
tok0 = strchr(kname, '/'); |
| 295 |
while (tok0 != NULL) { |
| 296 |
tokp = tok0; |
| 297 |
tok0 = strchr(tok0 + 1, '/'); |
| 298 |
} |
| 299 |
return (tokp); |
| 300 |
} |
| 301 |
|
| 302 |
extern "C" unsigned |
| 303 |
class_is_same_package( class_handler klass1, class_handler klass2 ) |
| 304 |
{ |
| 305 |
if (klass1 == klass2) { |
| 306 |
return (1); |
| 307 |
} |
| 308 |
else { |
| 309 |
const char *klass1_name = class_get_name(klass1); |
| 310 |
const char *klass2_name = class_get_name(klass2); |
| 311 |
char* package1 = package_name(klass1_name); |
| 312 |
char* package2 = package_name(klass2_name); |
| 313 |
int package1_len = (package1 - klass1_name); |
| 314 |
int package2_len = (package2 - klass2_name); |
| 315 |
if (package1_len != package2_len) |
| 316 |
return (0); |
| 317 |
return(strncmp(klass1_name, klass2_name, package2_len) == 0); |
| 318 |
assert(!"verifier entered!!"); |
| 319 |
} |
| 320 |
return (0); |
| 321 |
} |
| 322 |
|
| 323 |
extern "C" unsigned |
| 324 |
class_is_interface( class_handler klass ) |
| 325 |
{ |
| 326 |
JNIEnv *env; |
| 327 |
env = m_get_vm_pointer((void **)&env); |
| 328 |
assert(env); |
| 329 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 330 |
return (0); |
| 331 |
} |
| 332 |
else { |
| 333 |
return (unsigned)(env->CallBooleanMethod((jobject)klass->X.simplified_class, m_j_l_class_isInterface)); |
| 334 |
} |
| 335 |
|
| 336 |
return (0); |
| 337 |
} |
| 338 |
|
| 339 |
extern "C" unsigned |
| 340 |
class_is_array( class_handler klass ) |
| 341 |
{ |
| 342 |
assert(!"verifier entered!!"); |
| 343 |
return (0); |
| 344 |
} |
| 345 |
|
| 346 |
extern "C" unsigned |
| 347 |
class_is_final_( class_handler klass ) |
| 348 |
{ |
| 349 |
assert(!"verifier entered!!"); |
| 350 |
return (0); |
| 351 |
} |
| 352 |
|
| 353 |
extern "C" unsigned short |
| 354 |
class_get_superinterface_number( class_handler klass ) |
| 355 |
{ |
| 356 |
assert(!"verifier entered!!"); |
| 357 |
return (0); |
| 358 |
} |
| 359 |
|
| 360 |
extern "C" class_handler |
| 361 |
class_get_superinterface( class_handler klass, unsigned short index ) |
| 362 |
{ |
| 363 |
assert(!"verifier entered!!"); |
| 364 |
return (NULL); |
| 365 |
} |
| 366 |
|
| 367 |
extern "C" class_handler |
| 368 |
class_get_array_element_class( class_handler klass ) |
| 369 |
{ |
| 370 |
assert(!"verifier entered!!"); |
| 371 |
return (NULL); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
extern "C" class_handler |
| 376 |
class_get_extended_class( class_handler klass, const char *super_name ) |
| 377 |
{ |
| 378 |
return (NULL); |
| 379 |
} |
| 380 |
|
| 381 |
extern "C" unsigned short |
| 382 |
class_get_method_number( class_handler klass ) |
| 383 |
{ |
| 384 |
assert(!"verifier entered!!"); |
| 385 |
return (0); |
| 386 |
} |
| 387 |
|
| 388 |
extern "C" method_handler |
| 389 |
class_get_method( class_handler klass, unsigned short index ) |
| 390 |
{ |
| 391 |
assert(!"verifier entered!!"); |
| 392 |
return (NULL); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
/** |
| 397 |
* Constant pool inteface |
| 398 |
*/ |
| 399 |
|
| 400 |
extern "C" unsigned short |
| 401 |
class_cp_get_size( class_handler klass ) |
| 402 |
{ |
| 403 |
assert(klass); |
| 404 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 405 |
assert(klass->X.instrumented_class); |
| 406 |
assert(klass->X.instrumented_class->GetClassBuilder().GetConstPool()); |
| 407 |
return klass->X.instrumented_class->GetClassBuilder().GetConstPool()->GetSize(); |
| 408 |
} else { |
| 409 |
assert (!"not implemented yet!"); |
| 410 |
} |
| 411 |
assert(!"verifier entered!!"); |
| 412 |
return (0); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
extern "C" unsigned char |
| 417 |
class_cp_get_tag( class_handler klass, unsigned short index ) |
| 418 |
{ |
| 419 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 420 |
return (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]->GetTag(); |
| 421 |
} else { |
| 422 |
assert (!"not implemented yet!"); |
| 423 |
} |
| 424 |
return (0); |
| 425 |
} |
| 426 |
|
| 427 |
extern "C" unsigned short |
| 428 |
class_cp_get_class_name_index( class_handler klass, unsigned short index ) |
| 429 |
{ |
| 430 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 431 |
CCPInfo * ccinfo = (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]; |
| 432 |
assert(ccinfo->GetTag() == CONSTANT_Class); |
| 433 |
return ((CCPClassInfo *)ccinfo)->GetClassInd(); |
| 434 |
} else { |
| 435 |
assert (!"not implemented yet!"); |
| 436 |
} |
| 437 |
return (0); |
| 438 |
} |
| 439 |
|
| 440 |
extern "C" unsigned short |
| 441 |
class_cp_get_ref_class_index( class_handler klass, unsigned short index ) |
| 442 |
{ |
| 443 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 444 |
CCPInfo * ccinfo = (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]; |
| 445 |
unsigned char tag = ccinfo->GetTag(); |
| 446 |
switch(tag) { |
| 447 |
case CONSTANT_Fieldref: |
| 448 |
return ((CCPFieldrefInfo *)ccinfo)->GetClassInd(); |
| 449 |
break; |
| 450 |
case CONSTANT_Methodref: |
| 451 |
return ((CCPMethodrefInfo *)ccinfo)->GetClassInd(); |
| 452 |
break; |
| 453 |
case CONSTANT_InterfaceMethodref: |
| 454 |
return ((CCPInterfaceMethodrefInfo *)ccinfo)->GetClassInd(); |
| 455 |
break; |
| 456 |
default: |
| 457 |
assert(!"unsupported tag"); |
| 458 |
} |
| 459 |
} else { |
| 460 |
assert (!"not implemented yet!"); |
| 461 |
} |
| 462 |
return (0); |
| 463 |
} |
| 464 |
|
| 465 |
extern "C" unsigned short |
| 466 |
class_cp_get_ref_name_and_type_index( class_handler klass, unsigned short index ) |
| 467 |
{ |
| 468 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 469 |
CCPInfo * ccinfo = (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]; |
| 470 |
unsigned char tag = ccinfo->GetTag(); |
| 471 |
switch(tag) { |
| 472 |
case CONSTANT_Fieldref: |
| 473 |
return ((CCPFieldrefInfo *)ccinfo)->GetNameAndTypeInd(); |
| 474 |
break; |
| 475 |
case CONSTANT_Methodref: |
| 476 |
return ((CCPMethodrefInfo *)ccinfo)->GetNameAndTypeInd(); |
| 477 |
break; |
| 478 |
case CONSTANT_InterfaceMethodref: |
| 479 |
return ((CCPInterfaceMethodrefInfo *)ccinfo)->GetNameAndTypeInd(); |
| 480 |
break; |
| 481 |
default: |
| 482 |
assert(!"unsupported tag"); |
| 483 |
} |
| 484 |
} else { |
| 485 |
assert (!"not implemented yet!"); |
| 486 |
} |
| 487 |
return (0); |
| 488 |
} |
| 489 |
|
| 490 |
extern "C" unsigned short |
| 491 |
class_cp_get_string_index( class_handler klass, unsigned short index ) |
| 492 |
{ |
| 493 |
assert(!"verifier entered!!"); |
| 494 |
return (0); |
| 495 |
} |
| 496 |
|
| 497 |
extern "C" unsigned short |
| 498 |
class_cp_get_name_index( class_handler klass, unsigned short index ) |
| 499 |
{ |
| 500 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 501 |
CCPInfo * ccinfo = (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]; |
| 502 |
unsigned char tag = ccinfo->GetTag(); |
| 503 |
assert(tag == CONSTANT_NameAndType); |
| 504 |
return ((CCPNameAndTypeInfo *)ccinfo)->GetNameInd(); |
| 505 |
} else { |
| 506 |
assert (!"not implemented yet!"); |
| 507 |
} |
| 508 |
return (0); |
| 509 |
} |
| 510 |
|
| 511 |
extern "C" unsigned short |
| 512 |
class_cp_get_descriptor_index( class_handler klass, unsigned short index ) |
| 513 |
{ |
| 514 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 515 |
CCPInfo * ccinfo = (*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index]; |
| 516 |
unsigned char tag = ccinfo->GetTag(); |
| 517 |
assert(tag == CONSTANT_NameAndType); |
| 518 |
return ((CCPNameAndTypeInfo *)ccinfo)->GetDescriptorInd(); |
| 519 |
} else { |
| 520 |
assert (!"not implemented yet!"); |
| 521 |
} |
| 522 |
return (0); |
| 523 |
} |
| 524 |
|
| 525 |
extern "C" const char * |
| 526 |
class_cp_get_utf8_bytes( class_handler klass, unsigned short index ) |
| 527 |
{ |
| 528 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 529 |
return (const char *)((CCPUtf8Info *)(*klass->X.instrumented_class->GetClassBuilder().GetConstPool())[index])->GetBytes(); |
| 530 |
} else { |
| 531 |
assert (!"not implemented yet!"); |
| 532 |
} |
| 533 |
return (0); |
| 534 |
} |
| 535 |
|
| 536 |
extern "C" void |
| 537 |
class_set_verify_data_ptr( class_handler klass, void *data ) |
| 538 |
{ |
| 539 |
assert(!"verifier entered!!"); |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
extern "C" void * |
| 544 |
class_get_verify_data_ptr( class_handler klass ) |
| 545 |
{ |
| 546 |
assert(!"verifier entered!!"); |
| 547 |
return (NULL); |
| 548 |
} |
| 549 |
|
| 550 |
extern "C" method_handler |
| 551 |
class_resolve_method( class_handler klass, unsigned short index ) |
| 552 |
{ |
| 553 |
assert(!"verifier entered!!"); |
| 554 |
return (NULL); |
| 555 |
} |
| 556 |
|
| 557 |
extern "C" field_handler |
| 558 |
class_resolve_nonstatic_field( class_handler klass, unsigned short index ) |
| 559 |
{ |
| 560 |
assert(!"verifier entered!!"); |
| 561 |
return (NULL); |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Method interface |
| 566 |
*/ |
| 567 |
|
| 568 |
extern "C" class_handler |
| 569 |
method_get_class( method_handler hmethod ) |
| 570 |
{ |
| 571 |
assert(hmethod); |
| 572 |
return hmethod->class_owner; |
| 573 |
} |
| 574 |
|
| 575 |
extern "C" const char * |
| 576 |
method_get_name( method_handler method ) |
| 577 |
{ |
| 578 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 579 |
return (method->X.instrumented_method->GetName()); |
| 580 |
} |
| 581 |
else { |
| 582 |
assert(!"verifier entered!!"); |
| 583 |
} |
| 584 |
return (NULL); |
| 585 |
} |
| 586 |
|
| 587 |
extern "C" const char * |
| 588 |
method_get_descriptor( method_handler method ) |
| 589 |
{ |
| 590 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 591 |
return (method->X.instrumented_method->GetSignature()); |
| 592 |
} |
| 593 |
else { |
| 594 |
assert(!"verifier entered!!"); |
| 595 |
} |
| 596 |
return (NULL); |
| 597 |
} |
| 598 |
|
| 599 |
extern "C" unsigned |
| 600 |
method_get_bytecode_length( method_handler method ) |
| 601 |
{ |
| 602 |
/*insert check about instrumentation*/ |
| 603 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 604 |
unsigned size = 0; |
| 605 |
CInstructions::const_iterator insIterator; |
| 606 |
if (method->X.instrumented_method->GetCodeAttribute() == NULL) return (0); |
| 607 |
return (method->X.instrumented_method->GetCodeAttribute()->GetCodeLength()); |
| 608 |
} |
| 609 |
else { |
| 610 |
assert(!"verifier entered!!"); |
| 611 |
} |
| 612 |
return (0); |
| 613 |
} |
| 614 |
|
| 615 |
extern "C" unsigned char * |
| 616 |
method_get_bytecode( method_handler method ) |
| 617 |
{ |
| 618 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 619 |
return (method->X.instrumented_method->GetCodeAttribute()->GetCode()); |
| 620 |
} |
| 621 |
else { |
| 622 |
assert(!"verifier entered!!"); |
| 623 |
} |
| 624 |
return (NULL); |
| 625 |
} |
| 626 |
|
| 627 |
extern "C" unsigned |
| 628 |
method_get_max_locals( method_handler method ) |
| 629 |
{ |
| 630 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 631 |
/*XXX: add check for instrumented method*/ |
| 632 |
CMethodJ * jmethod = (CMethodJ *)(method->X.instrumented_method); |
| 633 |
return (jmethod->GetCodeAttribute()->GetMaxLocals()); |
| 634 |
} |
| 635 |
else { |
| 636 |
assert(!"verifier entered!!"); |
| 637 |
} |
| 638 |
return (0); |
| 639 |
|
| 640 |
} |
| 641 |
|
| 642 |
extern "C" unsigned |
| 643 |
method_get_max_stack( method_handler method ) |
| 644 |
{ |
| 645 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 646 |
CMethodJ * jmethod = (CMethodJ *)(method->X.instrumented_method); |
| 647 |
return (jmethod->GetCodeAttribute()->GetMaxStack()); |
| 648 |
} |
| 649 |
else { |
| 650 |
assert(!"verifier entered!!"); |
| 651 |
} |
| 652 |
return (0); |
| 653 |
} |
| 654 |
|
| 655 |
extern "C" unsigned |
| 656 |
method_is_static( method_handler method ) |
| 657 |
{ |
| 658 |
if (method->magic == METHOD_HANDLER_SELF) { |
| 659 |
return (method->X.instrumented_method->GetAccessFlags() & ACC_STATIC); |
| 660 |
} |
| 661 |
else { |
| 662 |
//assert(!"verifier entered!!"); |
| 663 |
return (0); /*we're assuming that this function called after class_resolve_method and couldn't return static methods*/ |
| 664 |
} |
| 665 |
return (0); |
| 666 |
|
| 667 |
} |
| 668 |
|
| 669 |
extern "C" unsigned |
| 670 |
method_is_protected( method_handler method ) |
| 671 |
{ |
| 672 |
assert(!"verifier entered!!"); |
| 673 |
return (0); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Method exception handler |
| 678 |
*/ |
| 679 |
|
| 680 |
extern "C" unsigned short |
| 681 |
method_get_exc_handler_number( method_handler method ) |
| 682 |
{ |
| 683 |
return (method->X.instrumented_method->GetExTable()->size()); |
| 684 |
} |
| 685 |
|
| 686 |
extern "C" void |
| 687 |
method_get_exc_handler_info( method_handler method, unsigned short index, |
| 688 |
unsigned short *start_pc, unsigned short *end_pc, |
| 689 |
unsigned short *handler_pc, unsigned short *catch_type ) |
| 690 |
{ |
| 691 |
class CExTable& exc = method->X.instrumented_method->GetCodeAttribute()->GetExTable(); |
| 692 |
*start_pc = exc[index].GetStartPC(); |
| 693 |
*end_pc = exc[index].GetEndPC(); |
| 694 |
*handler_pc = exc[index].GetHandlerPC(); |
| 695 |
*catch_type = exc[index].GetCatchtype(); |
| 696 |
|
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
extern "C" unsigned short |
| 701 |
method_get_number_exc_method_can_throw( method_handler hmethod ) |
| 702 |
{ |
| 703 |
assert(!"verifier entered!!"); |
| 704 |
return (0); |
| 705 |
} |
| 706 |
|
| 707 |
extern "C" const char * |
| 708 |
method_get_exc_method_can_throw( method_handler hmethod, unsigned short index ) |
| 709 |
{ |
| 710 |
assert(!"verifier entered!!"); |
| 711 |
return (NULL); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
extern "C" unsigned char * |
| 716 |
method_get_stackmaptable( method_handler hmethod ) |
| 717 |
{ |
| 718 |
|
| 719 |
CStackMapTableAttribute *attr = hmethod->X.instrumented_method->GetCodeAttribute()->GetStackMaps(); |
| 720 |
if (attr == NULL) |
| 721 |
return (NULL); |
| 722 |
u4 length = attr->GetLength(); |
| 723 |
u1 *stackmap = (u1 *)malloc(length); |
| 724 |
CJMemStream mem_stream; |
| 725 |
mem_stream.Open(stackmap, length); |
| 726 |
CJStream stream(&mem_stream); |
| 727 |
attr->Write(stream); |
| 728 |
//return (attr->GetInfo()); |
| 729 |
return (stackmap); |
| 730 |
} |
| 731 |
|
| 732 |
extern "C" void |
| 733 |
method_modify_exc_handler_info(method_handler method, |
| 734 |
unsigned short idx, |
| 735 |
unsigned short start_pc, |
| 736 |
unsigned short end_pc, |
| 737 |
unsigned short handler_pc, |
| 738 |
unsigned short handler_cp_index ) |
| 739 |
{ |
| 740 |
|
| 741 |
class CExTable& exc = method->X.instrumented_method->GetCodeAttribute()->GetExTable(); |
| 742 |
exc[idx].SetStartPC(start_pc); |
| 743 |
exc[idx].SetEndPC(end_pc); |
| 744 |
exc[idx].SetHandlerPC(handler_pc); |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
extern "C" void |
| 749 |
method_remove_exc_handler( method_handler method, unsigned short idx ) |
| 750 |
{ |
| 751 |
assert(!"verifier entered!!"); |
| 752 |
return; |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Class loader interface |
| 757 |
*/ |
| 758 |
|
| 759 |
extern "C" void |
| 760 |
class_loader_set_verifier_data_ptr( classloader_handler classloader, void *data ) |
| 761 |
{ |
| 762 |
assert(!"verifier entered!!"); |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
extern "C" void * |
| 767 |
class_loader_get_verifier_data_ptr( classloader_handler classloader ) |
| 768 |
{ |
| 769 |
assert(!"verifier entered!!"); |
| 770 |
return (NULL); |
| 771 |
} |
| 772 |
|
| 773 |
extern "C" void |
| 774 |
class_loader_lock( classloader_handler classloader ) |
| 775 |
{ |
| 776 |
assert(!"verifier entered!!"); |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
extern "C" void |
| 781 |
class_loader_unlock( classloader_handler classloader ) |
| 782 |
{ |
| 783 |
assert(!"verifier entered!!"); |
| 784 |
return; |
| 785 |
} |
| 786 |
|
| 787 |
extern "C" class_handler |
| 788 |
class_loader_lookup_class( classloader_handler classloader, const char *name ) |
| 789 |
{ |
| 790 |
if (m_name2ch[name] != NULL && IS_CLASS_INSTRUMENTED(m_name2ch[name])) |
| 791 |
return (m_name2ch[name]); |
| 792 |
else |
| 793 |
return (NULL); |
| 794 |
} |
| 795 |
|
| 796 |
extern "C" class_handler |
| 797 |
class_loader_load_class( classloader_handler classloader, const char *name ) |
| 798 |
{ |
| 799 |
assert(!"verifier entered!!"); |
| 800 |
return (NULL); |
| 801 |
} |
| 802 |
|
| 803 |
extern "C" unsigned |
| 804 |
field_is_protected( field_handler field ) |
| 805 |
{ |
| 806 |
assert(!"verifier entered!!"); |
| 807 |
return (0); |
| 808 |
} |
| 809 |
|
| 810 |
class_handler |
| 811 |
get_class_handler_from_builder(class CModuleJ *module) |
| 812 |
{ |
| 813 |
class_handler ch; |
| 814 |
|
| 815 |
m_name2ch.clear(); |
| 816 |
int status = create_class_handler(&m_j_l_object_ch, CLASS_HANDLER_SIMPLEFIED, "java/lang/Object", 0, NULL); |
| 817 |
assert(!status); |
| 818 |
|
| 819 |
status = create_class_handler(&ch, CLASS_HANDLER_INSTRUMENTED, module->GetName(), 1, module); |
| 820 |
assert(!status); |
| 821 |
return (ch); |
| 822 |
} |
| 823 |
|
| 824 |
method_handler |
| 825 |
get_method_handler_for_cmethod(class CMethod *method) |
| 826 |
{ |
| 827 |
class_handler ch = NULL; |
| 828 |
method_handler mh = NULL; |
| 829 |
int status; |
| 830 |
status = create_class_handler(&ch, CLASS_HANDLER_INSTRUMENTED, (const char *)method->GetModule()->GetName(), 1, method->GetModule()); |
| 831 |
assert(!status); |
| 832 |
status = create_method_handler(&mh, ch, METHOD_HANDLER_SELF, method); |
| 833 |
assert(!status); |
| 834 |
return (mh); |
| 835 |
} |
| 836 |
|
| 837 |
extern "C" unsigned short |
| 838 |
class_cp_get_class_entry(class_handler klass, const char* name) |
| 839 |
{ |
| 840 |
|
| 841 |
|
| 842 |
CCPClassInfo *cinfo; |
| 843 |
if (IS_CLASS_INSTRUMENTED(klass)) { |
| 844 |
/*for this class we'll have entry somewhere in some future */ |
| 845 |
if (strcmp(klass->X.instrumented_class->GetName(), name) == 0) { |
| 846 |
return (klass->X.instrumented_class->GetClass().GetThisClass()); |
| 847 |
} |
| 848 |
assert(name); |
| 849 |
cinfo = (klass->X.instrumented_class->GetClassBuilder().FindClass(name)); |
| 850 |
if (cinfo == NULL) { |
| 851 |
/*should be created new class antry */ |
| 852 |
cinfo = *klass->X.instrumented_class->GetClassBuilder().CreateClassConstant(name); |
| 853 |
assert(cinfo); |
| 854 |
} |
| 855 |
return(cinfo->GetCpIndex()); |
| 856 |
} else { |
| 857 |
assert (!"not implemented yet!"); |
| 858 |
} |
| 859 |
return (0); |
| 860 |
} |
| 861 |
|
| 862 |
int |
| 863 |
initialize_dynamic(get_vm_pointer_t a_get_vm_pointer) |
| 864 |
{ |
| 865 |
m_get_vm_pointer = (JNIEnv *(*)(void **))a_get_vm_pointer; |
| 866 |
JNIEnv *env; |
| 867 |
|
| 868 |
env = m_get_vm_pointer((void **)&env); |
| 869 |
assert(env); |
| 870 |
|
| 871 |
if (m_j_l_class) return (0); |
| 872 |
|
| 873 |
m_j_l_class = env->NewGlobalRef(env->FindClass("java/lang/Class")); |
| 874 |
assert(m_j_l_class); |
| 875 |
|
| 876 |
m_j_l_class_getName = env->GetMethodID(m_j_l_class, "getName", "()Ljava/lang/String;"); |
| 877 |
assert(m_j_l_class_getName); |
| 878 |
|
| 879 |
m_j_l_class_getClassLoader = env->GetMethodID(m_j_l_class, "getClassLoader", "()Ljava/lang/ClassLoader;"); |
| 880 |
assert(m_j_l_class_getClassLoader); |
| 881 |
|
| 882 |
m_j_l_class_isInterface = env->GetMethodID(m_j_l_class, "isInterface", "()Z"); |
| 883 |
assert(m_j_l_class_isInterface); |
| 884 |
|
| 885 |
m_j_l_classloader = env->NewGlobalRef(env->FindClass("java/lang/ClassLoader")); |
| 886 |
assert(m_j_l_classloader); |
| 887 |
|
| 888 |
m_j_l_classloader_findLoadedClass = env->GetMethodID(m_j_l_classloader, "findLoadedClass", "(Ljava/lang/String;)Ljava/lang/Class;"); |
| 889 |
assert(m_j_l_classloader_findLoadedClass); |
| 890 |
|
| 891 |
return (0); |
| 892 |
} |
| 893 |
|
| 894 |
void |
| 895 |
class_destroy_handler(class_handler ch) |
| 896 |
{ |
| 897 |
if (ch == NULL) return; |
| 898 |
if (!IS_CLASS_INSTRUMENTED(ch) && ch->loaded == 1) { |
| 899 |
JNIEnv *env; |
| 900 |
env = m_get_vm_pointer((void **)&env); |
| 901 |
assert(env); |
| 902 |
env->DeleteGlobalRef((jobject)ch->X.simplified_class); |
| 903 |
|
| 904 |
} |
| 905 |
free(ch); |
| 906 |
ch = NULL; |
| 907 |
} |