| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /* |
| 17 | * Declaration of the fundamental Object type and refinements thereof, plus |
| 18 | * some functions for manipulating them. |
| 19 | */ |
| 20 | #ifndef _DALVIK_OO_OBJECT |
| 21 | #define _DALVIK_OO_OBJECT |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | /* fwd decl */ |
| 26 | struct DataObject; |
| 27 | struct ClassObject; |
| 28 | struct StringObject; |
| 29 | struct ArrayObject; |
| 30 | struct Method; |
| 31 | struct ExceptionEntry; |
| 32 | struct LineNumEntry; |
| 33 | struct StaticField; |
| 34 | struct InstField; |
| 35 | struct Field; |
| 36 | struct RegisterMap; |
| 37 | typedef struct DataObject DataObject; |
| 38 | typedef struct ClassObject ClassObject; |
| 39 | typedef struct StringObject StringObject; |
| 40 | typedef struct ArrayObject ArrayObject; |
| 41 | typedef struct Method Method; |
| 42 | typedef struct ExceptionEntry ExceptionEntry; |
| 43 | typedef struct LineNumEntry LineNumEntry; |
| 44 | typedef struct StaticField StaticField; |
| 45 | typedef struct InstField InstField; |
| 46 | typedef struct Field Field; |
| 47 | typedef struct RegisterMap RegisterMap; |
| 48 | |
| 49 | /* |
| 50 | * Native function pointer type. |
| 51 | * |
| 52 | * "args[0]" holds the "this" pointer for virtual methods. |
| 53 | * |
| 54 | * The "Bridge" form is a super-set of the "Native" form; in many places |
| 55 | * they are used interchangeably. Currently, all functions have all |
| 56 | * arguments passed in, but some functions only care about the first two. |
| 57 | * Passing extra arguments to a C function is (mostly) harmless. |
| 58 | */ |
| 59 | typedef void (*DalvikBridgeFunc)(const u4* args, JValue* pResult, |
| 60 | const Method* method, struct Thread* self); |
| 61 | typedef void (*DalvikNativeFunc)(const u4* args, JValue* pResult); |
| 62 | |
| 63 | |
| 64 | /* vm-internal access flags and related definitions */ |
| 65 | typedef enum AccessFlags { |
| 66 | ACC_MIRANDA = 0x8000, // method (internal to VM) |
| 67 | JAVA_FLAGS_MASK = 0xffff, // bits set from Java sources (low 16) |
| 68 | } AccessFlags; |
| 69 | |
| 70 | /* Use the top 16 bits of the access flags field for |
| 71 | * other class flags. Code should use the *CLASS_FLAG*() |
| 72 | * macros to set/get these flags. |
| 73 | */ |
| 74 | typedef enum ClassFlags { |
| 75 | CLASS_ISFINALIZABLE = (1<<31), // class/ancestor overrides finalize() |
| 76 | CLASS_ISARRAY = (1<<30), // class is a "[*" |
| 77 | CLASS_ISOBJECTARRAY = (1<<29), // class is a "[L*" or "[[*" |
| 78 | CLASS_ISREFERENCE = (1<<28), // class is a soft/weak/phantom ref |
| 79 | // only ISREFERENCE is set --> soft |
| 80 | CLASS_ISWEAKREFERENCE = (1<<27), // class is a weak reference |
| 81 | CLASS_ISPHANTOMREFERENCE = (1<<26), // class is a phantom reference |
| 82 | |
| 83 | CLASS_MULTIPLE_DEFS = (1<<25), // DEX verifier: defs in multiple DEXs |
| 84 | |
| 85 | /* unlike the others, these can be present in the optimized DEX file */ |
| 86 | CLASS_ISOPTIMIZED = (1<<17), // class may contain opt instrs |
| 87 | CLASS_ISPREVERIFIED = (1<<16), // class has been pre-verified |
| 88 | } ClassFlags; |
| 89 | |
| 90 | /* bits we can reasonably expect to see set in a DEX access flags field */ |
| 91 | #define EXPECTED_FILE_FLAGS \ |
| 92 | (ACC_CLASS_MASK | CLASS_ISPREVERIFIED | CLASS_ISOPTIMIZED) |
| 93 | |
| 94 | /* current state of the class, increasing as we progress */ |
| 95 | typedef enum ClassStatus { |
| 96 | CLASS_ERROR = -1, |
| 97 | |
| 98 | CLASS_NOTREADY = 0, |
| 99 | CLASS_LOADED = 1, |
| 100 | CLASS_PREPARED = 2, /* part of linking */ |
| 101 | CLASS_RESOLVED = 3, /* part of linking */ |
| 102 | CLASS_VERIFYING = 4, /* in the process of being verified */ |
| 103 | CLASS_VERIFIED = 5, /* logically part of linking; done pre-init */ |
| 104 | CLASS_INITIALIZING = 6, /* class init in progress */ |
| 105 | CLASS_INITIALIZED = 7, /* ready to go */ |
| 106 | } ClassStatus; |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * Primitive type identifiers. We use these values as indexes into an |
| 111 | * array of synthesized classes, so these start at zero and count up. |
| 112 | * The order is arbitrary (mimics table in doc for newarray opcode), |
| 113 | * but can't be changed without shuffling some reflection tables. |
| 114 | * |
| 115 | * PRIM_VOID can't be used as an array type, but we include it here for |
| 116 | * other uses (e.g. Void.TYPE). |
| 117 | */ |
| 118 | typedef enum PrimitiveType { |
| 119 | PRIM_NOT = -1, /* value is not a primitive type */ |
| 120 | PRIM_BOOLEAN = 0, |
| 121 | PRIM_CHAR = 1, |
| 122 | PRIM_FLOAT = 2, |
| 123 | PRIM_DOUBLE = 3, |
| 124 | PRIM_BYTE = 4, |
| 125 | PRIM_SHORT = 5, |
| 126 | PRIM_INT = 6, |
| 127 | PRIM_LONG = 7, |
| 128 | PRIM_VOID = 8, |
| 129 | |
| 130 | PRIM_MAX |
| 131 | } PrimitiveType; |
| 132 | #define PRIM_TYPE_TO_LETTER "ZCFDBSIJV" /* must match order in enum */ |
| 133 | |
| 134 | /* |
| 135 | * This defines the amount of space we leave for field slots in the |
| 136 | * java.lang.Class definition. If we alter the class to have more than |
| 137 | * this many fields, the VM will abort at startup. |
| 138 | */ |
| 139 | #define CLASS_FIELD_SLOTS 4 |
| 140 | |
| 141 | |
| 142 | /* |
| 143 | * Used for iftable in ClassObject. |
| 144 | */ |
| 145 | typedef struct InterfaceEntry { |
| 146 | /* pointer to interface class */ |
| 147 | ClassObject* clazz; |
| 148 | |
| 149 | /* |
| 150 | * Index into array of vtable offsets. This points into the ifviPool, |
| 151 | * which holds the vtables for all interfaces declared by this class. |
| 152 | */ |
| 153 | int* methodIndexArray; |
| 154 | } InterfaceEntry; |
| 155 | |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | * There are three types of objects: |
| 160 | * Class objects - an instance of java.lang.Class |
| 161 | * Array objects - an object created with a "new array" instruction |
| 162 | * Data objects - an object that is neither of the above |
| 163 | * |
| 164 | * We also define String objects. At present they're equivalent to |
| 165 | * DataObject, but that may change. (Either way, they make some of the |
| 166 | * code more obvious.) |
| 167 | * |
| 168 | * All objects have an Object header followed by type-specific data. |
| 169 | */ |
| 170 | typedef struct Object { |
| 171 | /* ptr to class object */ |
| 172 | ClassObject* clazz; |
| 173 | |
| 174 | /* thin lock or "fat" monitor */ |
| 175 | Lock lock; |
| 176 | } Object; |
| 177 | |
| 178 | /* |
| 179 | * Properly initialize an Object. |
| 180 | * void DVM_OBJECT_INIT(Object *obj, ClassObject *clazz_) |
| 181 | */ |
| 182 | #define DVM_OBJECT_INIT(obj, clazz_) \ |
| 183 | do { (obj)->clazz = (clazz_); DVM_LOCK_INIT(&(obj)->lock); } while (0) |
| 184 | |
| 185 | /* |
| 186 | * Get/set class flags. |
| 187 | */ |
| 188 | #define SET_CLASS_FLAG(clazz, flag) \ |
| 189 | do { (clazz)->accessFlags |= (flag); } while (0) |
| 190 | |
| 191 | #define CLEAR_CLASS_FLAG(clazz, flag) \ |
| 192 | do { (clazz)->accessFlags &= ~(flag); } while (0) |
| 193 | |
| 194 | #define IS_CLASS_FLAG_SET(clazz, flag) \ |
| 195 | (((clazz)->accessFlags & (flag)) != 0) |
| 196 | |
| 197 | #define GET_CLASS_FLAG_GROUP(clazz, flags) \ |
| 198 | ((u4)((clazz)->accessFlags & (flags))) |
| 199 | |
| 200 | /* |
| 201 | * Data objects have an Object header followed by their instance data. |
| 202 | */ |
| 203 | struct DataObject { |
| 204 | Object obj; /* MUST be first item */ |
| 205 | |
| 206 | /* variable #of u4 slots; u8 uses 2 slots */ |
| 207 | u4 instanceData[1]; |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * Strings are used frequently enough that we may want to give them their |
| 212 | * own unique type. |
| 213 | * |
| 214 | * Using a dedicated type object to access the instance data provides a |
| 215 | * performance advantage but makes the java/lang/String.java implementation |
| 216 | * fragile. |
| 217 | * |
| 218 | * Currently this is just equal to DataObject, and we pull the fields out |
| 219 | * like we do for any other object. |
| 220 | */ |
| 221 | struct StringObject { |
| 222 | Object obj; /* MUST be first item */ |
| 223 | |
| 224 | /* variable #of u4 slots; u8 uses 2 slots */ |
| 225 | u4 instanceData[1]; |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | /* |
| 230 | * Array objects have these additional fields. |
| 231 | * |
| 232 | * We don't currently store the size of each element. Usually it's implied |
| 233 | * by the instruction. If necessary, the width can be derived from |
| 234 | * the first char of obj->clazz->name. |
| 235 | */ |
| 236 | struct ArrayObject { |
| 237 | Object obj; /* MUST be first item */ |
| 238 | |
| 239 | /* number of elements; immutable after init */ |
| 240 | u4 length; |
| 241 | |
| 242 | /* |
| 243 | * Array contents; actual size is (length * sizeof(type)). This is |
| 244 | * declared as u8 so that the compiler inserts any necessary padding |
| 245 | * (e.g. for EABI); the actual allocation may be smaller than 8 bytes. |
| 246 | */ |
| 247 | u8 contents[1]; |
| 248 | }; |
| 249 | |
| 250 | /* |
| 251 | * Class objects have many additional fields. This is used for both |
| 252 | * classes and interfaces, including synthesized classes (arrays and |
| 253 | * primitive types). |
| 254 | * |
| 255 | * Class objects are unusual in that they have some fields allocated with |
| 256 | * the system malloc (or LinearAlloc), rather than on the GC heap. This is |
| 257 | * handy during initialization, but does require special handling when |
| 258 | * discarding java.lang.Class objects. |
| 259 | * |
| 260 | * The separation of methods (direct vs. virtual) and fields (class vs. |
| 261 | * instance) used in Dalvik works out pretty well. The only time it's |
| 262 | * annoying is when enumerating or searching for things with reflection. |
| 263 | */ |
| 264 | struct ClassObject { |
| 265 | Object obj; /* MUST be first item */ |
| 266 | |
| 267 | /* leave space for instance data; we could access fields directly if we |
| 268 | freeze the definition of java/lang/Class */ |
| 269 | u4 instanceData[CLASS_FIELD_SLOTS]; |
| 270 | |
| 271 | /* UTF-8 descriptor for the class; from constant pool, or on heap |
| 272 | if generated ("[C") */ |
| 273 | const char* descriptor; |
| 274 | char* descriptorAlloc; |
| 275 | |
| 276 | /* access flags; low 16 bits are defined by VM spec */ |
| 277 | u4 accessFlags; |
| 278 | |
| 279 | /* VM-unique class serial number, nonzero, set very early */ |
| 280 | u4 serialNumber; |
| 281 | |
| 282 | /* DexFile from which we came; needed to resolve constant pool entries */ |
| 283 | /* (will be NULL for VM-generated, e.g. arrays and primitive classes) */ |
| 284 | DvmDex* pDvmDex; |
| 285 | |
| 286 | /* state of class initialization */ |
| 287 | ClassStatus status; |
| 288 | |
| 289 | /* if class verify fails, we must return same error on subsequent tries */ |
| 290 | ClassObject* verifyErrorClass; |
| 291 | |
| 292 | /* threadId, used to check for recursive <clinit> invocation */ |
| 293 | u4 initThreadId; |
| 294 | |
| 295 | /* |
| 296 | * Total object size; used when allocating storage on gc heap. (For |
| 297 | * interfaces and abstract classes this will be zero.) |
| 298 | */ |
| 299 | size_t objectSize; |
| 300 | |
| 301 | /* arrays only: class object for base element, for instanceof/checkcast |
| 302 | (for String[][][], this will be String) */ |
| 303 | ClassObject* elementClass; |
| 304 | |
| 305 | /* class object representing an array of this class; set on first use */ |
| 306 | ClassObject* arrayClass; |
| 307 | |
| 308 | /* arrays only: number of dimensions, e.g. int[][] is 2 */ |
| 309 | int arrayDim; |
| 310 | |
| 311 | /* primitive type index, or PRIM_NOT (-1); set for generated prim classes */ |
| 312 | PrimitiveType primitiveType; |
| 313 | |
| 314 | /* superclass, or NULL if this is java.lang.Object */ |
| 315 | ClassObject* super; |
| 316 | |
| 317 | /* defining class loader, or NULL for the "bootstrap" system loader */ |
| 318 | Object* classLoader; |
| 319 | |
| 320 | /* initiating class loader list */ |
| 321 | Object** initiatingLoaders; |
| 322 | int initiatingLoaderCount; |
| 323 | |
| 324 | /* array of interfaces this class implements directly */ |
| 325 | int interfaceCount; |
| 326 | ClassObject** interfaces; |
| 327 | |
| 328 | /* static, private, and <init> methods */ |
| 329 | int directMethodCount; |
| 330 | Method* directMethods; |
| 331 | |
| 332 | /* virtual methods defined in this class; invoked through vtable */ |
| 333 | int virtualMethodCount; |
| 334 | Method* virtualMethods; |
| 335 | |
| 336 | /* |
| 337 | * Virtual method table (vtable), for use by "invoke-virtual". The |
| 338 | * vtable from the superclass is copied in, and virtual methods from |
| 339 | * our class either replace those from the super or are appended. |
| 340 | */ |
| 341 | int vtableCount; |
| 342 | Method** vtable; |
| 343 | |
| 344 | /* |
| 345 | * Interface table (iftable), one entry per interface supported by |
| 346 | * this class. That means one entry for each interface we support |
| 347 | * directly, indirectly via superclass, or indirectly via |
| 348 | * superinterface. This will be null if neither we nor our superclass |
| 349 | * implement any interfaces. |
| 350 | * |
| 351 | * Why we need this: given "class Foo implements Face", declare |
| 352 | * "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" is |
| 353 | * part of the Face interface. We can't easily use a single vtable. |
| 354 | * |
| 355 | * For every interface a concrete class implements, we create a list of |
| 356 | * virtualMethod indices for the methods in the interface. |
| 357 | */ |
| 358 | int iftableCount; |
| 359 | InterfaceEntry* iftable; |
| 360 | |
| 361 | /* |
| 362 | * The interface vtable indices for iftable get stored here. By placing |
| 363 | * them all in a single pool for each class that implements interfaces, |
| 364 | * we decrease the number of allocations. |
| 365 | */ |
| 366 | int ifviPoolCount; |
| 367 | int* ifviPool; |
| 368 | |
| 369 | /* static fields */ |
| 370 | int sfieldCount; |
| 371 | StaticField* sfields; |
| 372 | |
| 373 | /* instance fields |
| 374 | * |
| 375 | * These describe the layout of the contents of a DataObject-compatible |
| 376 | * Object. Note that only the fields directly defined by this class |
| 377 | * are listed in ifields; fields defined by a superclass are listed |
| 378 | * in the superclass's ClassObject.ifields. |
| 379 | * |
| 380 | * All instance fields that refer to objects are guaranteed to be |
| 381 | * at the beginning of the field list. ifieldRefCount specifies |
| 382 | * the number of reference fields. |
| 383 | */ |
| 384 | int ifieldCount; |
| 385 | int ifieldRefCount; // number of fields that are object refs |
| 386 | InstField* ifields; |
| 387 | |
| 388 | /* source file name, if known */ |
| 389 | const char* sourceFile; |
| 390 | }; |
| 391 | |
| 392 | /* |
| 393 | * A method. We create one of these for every method in every class |
| 394 | * we load, so try to keep the size to a minimum. |
| 395 | * |
| 396 | * Much of this comes from and could be accessed in the data held in shared |
| 397 | * memory. We hold it all together here for speed. Everything but the |
| 398 | * pointers could be held in a shared table generated by the optimizer; |
| 399 | * if we're willing to convert them to offsets and take the performance |
| 400 | * hit (e.g. "meth->insns" becomes "baseAddr + meth->insnsOffset") we |
| 401 | * could move everything but "nativeFunc". |
| 402 | */ |
| 403 | struct Method { |
| 404 | /* the class we are a part of */ |
| 405 | ClassObject* clazz; |
| 406 | |
| 407 | /* access flags; low 16 bits are defined by spec (could be u2?) */ |
| 408 | u4 accessFlags; |
| 409 | |
| 410 | /* |
| 411 | * For concrete virtual methods, this is the offset of the method |
| 412 | * in "vtable". |
| 413 | * |
| 414 | * For abstract methods in an interface class, this is the offset |
| 415 | * of the method in "iftable[n]->methodIndexArray". |
| 416 | */ |
| 417 | u2 methodIndex; |
| 418 | |
| 419 | /* |
| 420 | * Method bounds; not needed for an abstract method. |
| 421 | * |
| 422 | * For a native method, we compute the size of the argument list, and |
| 423 | * set "insSize" and "registerSize" equal to it. |
| 424 | */ |
| 425 | u2 registersSize; /* ins + locals */ |
| 426 | u2 outsSize; |
| 427 | u2 insSize; |
| 428 | |
| 429 | /* method name, e.g. "<init>" or "eatLunch" */ |
| 430 | const char* name; |
| 431 | |
| 432 | /* |
| 433 | * Method prototype descriptor string (return and argument types). |
| 434 | * |
| 435 | * TODO: This currently must specify the DexFile as well as the proto_ids |
| 436 | * index, because generated Proxy classes don't have a DexFile. We can |
| 437 | * remove the DexFile* and reduce the size of this struct if we generate |
| 438 | * a DEX for proxies. |
| 439 | */ |
| 440 | DexProto prototype; |
| 441 | |
| 442 | /* short-form method descriptor string */ |
| 443 | const char* shorty; |
| 444 | |
| 445 | /* |
| 446 | * The remaining items are not used for abstract or native methods. |
| 447 | * (JNI is currently hijacking "insns" as a function pointer, set |
| 448 | * after the first call. For internal-native this stays null.) |
| 449 | */ |
| 450 | |
| 451 | /* the actual code */ |
| 452 | const u2* insns; /* instructions, in memory-mapped .dex */ |
| 453 | |
| 454 | /* cached JNI argument and return-type hints */ |
| 455 | int jniArgInfo; |
| 456 | |
| 457 | /* |
| 458 | * Native method ptr; could be actual function or a JNI bridge. We |
| 459 | * don't currently discriminate between DalvikBridgeFunc and |
| 460 | * DalvikNativeFunc; the former takes an argument superset (i.e. two |
| 461 | * extra args) which will be ignored. If necessary we can use |
| 462 | * insns==NULL to detect JNI bridge vs. internal native. |
| 463 | */ |
| 464 | DalvikBridgeFunc nativeFunc; |
| 465 | |
| 466 | /* |
| 467 | * Register map data, if available. This will point into the DEX file |
| 468 | * if the data was computed during pre-verification, or into the |
| 469 | * linear alloc area if not. |
| 470 | */ |
| 471 | const RegisterMap* registerMap; |
| 472 | |
| 473 | #ifdef WITH_PROFILER |
| 474 | bool inProfile; |
| 475 | #endif |
| 476 | #ifdef WITH_DEBUGGER |
| 477 | short debugBreakpointCount; |
| 478 | #endif |
| 479 | }; |
| 480 | |
| 481 | /* |
| 482 | * Generic field header. We pass this around when we want a generic Field |
| 483 | * pointer (e.g. for reflection stuff). Testing the accessFlags for |
| 484 | * ACC_STATIC allows a proper up-cast. |
| 485 | */ |
| 486 | struct Field { |
| 487 | ClassObject* clazz; /* class in which the field is declared */ |
| 488 | const char* name; |
| 489 | const char* signature; /* e.g. "I", "[C", "Landroid/os/Debug;" */ |
| 490 | u4 accessFlags; |
| 491 | #ifdef PROFILE_FIELD_ACCESS |
| 492 | u4 gets; |
| 493 | u4 puts; |
| 494 | #endif |
| 495 | }; |
| 496 | |
| 497 | /* |
| 498 | * Static field. |
| 499 | */ |
| 500 | struct StaticField { |
| 501 | Field field; /* MUST be first item */ |
| 502 | JValue value; /* initially set from DEX for primitives */ |
| 503 | }; |
| 504 | |
| 505 | /* |
| 506 | * Instance field. |
| 507 | */ |
| 508 | struct InstField { |
| 509 | Field field; /* MUST be first item */ |
| 510 | |
| 511 | /* |
| 512 | * This field indicates the byte offset from the beginning of the |
| 513 | * (Object *) to the actual instance data; e.g., byteOffset==0 is |
| 514 | * the same as the object pointer (bug!), and byteOffset==4 is 4 |
| 515 | * bytes farther. |
| 516 | */ |
| 517 | int byteOffset; |
| 518 | }; |
| 519 | |
| 520 | |
| 521 | /* |
| 522 | * Find a method within a class. The superclass is not searched. |
| 523 | */ |
| 524 | Method* dvmFindDirectMethodByDescriptor(const ClassObject* clazz, |
| 525 | const char* methodName, const char* signature); |
| 526 | Method* dvmFindVirtualMethodByDescriptor(const ClassObject* clazz, |
| 527 | const char* methodName, const char* signature); |
| 528 | Method* dvmFindVirtualMethodByName(const ClassObject* clazz, |
| 529 | const char* methodName); |
| 530 | Method* dvmFindDirectMethod(const ClassObject* clazz, const char* methodName, |
| 531 | const DexProto* proto); |
| 532 | Method* dvmFindVirtualMethod(const ClassObject* clazz, const char* methodName, |
| 533 | const DexProto* proto); |
| 534 | |
| 535 | |
| 536 | /* |
| 537 | * Find a method within a class hierarchy. |
| 538 | */ |
| 539 | Method* dvmFindDirectMethodHierByDescriptor(const ClassObject* clazz, |
| 540 | const char* methodName, const char* descriptor); |
| 541 | Method* dvmFindVirtualMethodHierByDescriptor(const ClassObject* clazz, |
| 542 | const char* methodName, const char* signature); |
| 543 | Method* dvmFindDirectMethodHier(const ClassObject* clazz, |
| 544 | const char* methodName, const DexProto* proto); |
| 545 | Method* dvmFindVirtualMethodHier(const ClassObject* clazz, |
| 546 | const char* methodName, const DexProto* proto); |
| 547 | |
| 548 | /* |
| 549 | * Find the implementation of "meth" in "clazz". |
| 550 | * |
| 551 | * Returns NULL and throws an exception if not found. |
| 552 | */ |
| 553 | const Method* dvmGetVirtualizedMethod(const ClassObject* clazz, |
| 554 | const Method* meth); |
| 555 | |
| 556 | /* |
| 557 | * Get the source file associated with a method. |
| 558 | */ |
| 559 | const char* dvmGetMethodSourceFile(const Method* meth); |
| 560 | |
| 561 | /* |
| 562 | * Find a field within a class. The superclass is not searched. |
| 563 | */ |
| 564 | InstField* dvmFindInstanceField(const ClassObject* clazz, |
| 565 | const char* fieldName, const char* signature); |
| 566 | StaticField* dvmFindStaticField(const ClassObject* clazz, |
| 567 | const char* fieldName, const char* signature); |
| 568 | |
| 569 | /* |
| 570 | * Find a field in a class/interface hierarchy. |
| 571 | */ |
| 572 | InstField* dvmFindInstanceFieldHier(const ClassObject* clazz, |
| 573 | const char* fieldName, const char* signature); |
| 574 | StaticField* dvmFindStaticFieldHier(const ClassObject* clazz, |
| 575 | const char* fieldName, const char* signature); |
| 576 | |
| 577 | /* |
| 578 | * Find a field and return the byte offset from the object pointer. Only |
| 579 | * searches the specified class, not the superclass. |
| 580 | * |
| 581 | * Returns -1 on failure. |
| 582 | */ |
| 583 | INLINE int dvmFindFieldOffset(const ClassObject* clazz, |
| 584 | const char* fieldName, const char* signature) |
| 585 | { |
| 586 | InstField* pField = dvmFindInstanceField(clazz, fieldName, signature); |
| 587 | if (pField == NULL) |
| 588 | return -1; |
| 589 | else |
| 590 | return pField->byteOffset; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Field access functions. Pass in the word offset from Field->byteOffset. |
| 595 | * |
| 596 | * We guarantee that long/double field data is 64-bit aligned, so it's safe |
| 597 | * to access them with ldrd/strd on ARM. |
| 598 | * |
| 599 | * The VM treats all fields as 32 or 64 bits, so the field set functions |
| 600 | * write 32 bits even if the underlying type is smaller. |
| 601 | */ |
| 602 | #define BYTE_OFFSET(_ptr, _offset) ((void*) (((u1*)(_ptr)) + (_offset))) |
| 603 | |
| 604 | INLINE JValue* dvmFieldPtr(const Object* obj, int offset) { |
| 605 | return ((JValue*)BYTE_OFFSET(obj, offset)); |
| 606 | } |
| 607 | |
| 608 | INLINE bool dvmGetFieldBoolean(const Object* obj, int offset) { |
| 609 | return ((JValue*)BYTE_OFFSET(obj, offset))->z; |
| 610 | } |
| 611 | INLINE s1 dvmGetFieldByte(const Object* obj, int offset) { |
| 612 | return ((JValue*)BYTE_OFFSET(obj, offset))->b; |
| 613 | } |
| 614 | INLINE s2 dvmGetFieldShort(const Object* obj, int offset) { |
| 615 | return ((JValue*)BYTE_OFFSET(obj, offset))->s; |
| 616 | } |
| 617 | INLINE u2 dvmGetFieldChar(const Object* obj, int offset) { |
| 618 | return ((JValue*)BYTE_OFFSET(obj, offset))->c; |
| 619 | } |
| 620 | INLINE s4 dvmGetFieldInt(const Object* obj, int offset) { |
| 621 | return ((JValue*)BYTE_OFFSET(obj, offset))->i; |
| 622 | } |
| 623 | INLINE s8 dvmGetFieldLong(const Object* obj, int offset) { |
| 624 | return ((JValue*)BYTE_OFFSET(obj, offset))->j; |
| 625 | } |
| 626 | INLINE float dvmGetFieldFloat(const Object* obj, int offset) { |
| 627 | return ((JValue*)BYTE_OFFSET(obj, offset))->f; |
| 628 | } |
| 629 | INLINE double dvmGetFieldDouble(const Object* obj, int offset) { |
| 630 | return ((JValue*)BYTE_OFFSET(obj, offset))->d; |
| 631 | } |
| 632 | INLINE Object* dvmGetFieldObject(const Object* obj, int offset) { |
| 633 | return ((JValue*)BYTE_OFFSET(obj, offset))->l; |
| 634 | } |
| 635 | |
| 636 | INLINE void dvmSetFieldBoolean(Object* obj, int offset, bool val) { |
| 637 | ((JValue*)BYTE_OFFSET(obj, offset))->i = val; |
| 638 | } |
| 639 | INLINE void dvmSetFieldByte(Object* obj, int offset, s1 val) { |
| 640 | ((JValue*)BYTE_OFFSET(obj, offset))->i = val; |
| 641 | } |
| 642 | INLINE void dvmSetFieldShort(Object* obj, int offset, s2 val) { |
| 643 | ((JValue*)BYTE_OFFSET(obj, offset))->i = val; |
| 644 | } |
| 645 | INLINE void dvmSetFieldChar(Object* obj, int offset, u2 val) { |
| 646 | ((JValue*)BYTE_OFFSET(obj, offset))->i = val; |
| 647 | } |
| 648 | INLINE void dvmSetFieldInt(Object* obj, int offset, s4 val) { |
| 649 | ((JValue*)BYTE_OFFSET(obj, offset))->i = val; |
| 650 | } |
| 651 | INLINE void dvmSetFieldLong(Object* obj, int offset, s8 val) { |
| 652 | ((JValue*)BYTE_OFFSET(obj, offset))->j = val; |
| 653 | } |
| 654 | INLINE void dvmSetFieldFloat(Object* obj, int offset, float val) { |
| 655 | ((JValue*)BYTE_OFFSET(obj, offset))->f = val; |
| 656 | } |
| 657 | INLINE void dvmSetFieldDouble(Object* obj, int offset, double val) { |
| 658 | ((JValue*)BYTE_OFFSET(obj, offset))->d = val; |
| 659 | } |
| 660 | INLINE void dvmSetFieldObject(Object* obj, int offset, Object* val) { |
| 661 | ((JValue*)BYTE_OFFSET(obj, offset))->l = val; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Static field access functions. |
| 666 | */ |
| 667 | INLINE JValue* dvmStaticFieldPtr(const StaticField* sfield) { |
| 668 | return (JValue*)&sfield->value; |
| 669 | } |
| 670 | |
| 671 | INLINE bool dvmGetStaticFieldBoolean(const StaticField* sfield) { |
| 672 | return sfield->value.z; |
| 673 | } |
| 674 | INLINE s1 dvmGetStaticFieldByte(const StaticField* sfield) { |
| 675 | return sfield->value.b; |
| 676 | } |
| 677 | INLINE s2 dvmGetStaticFieldShort(const StaticField* sfield) { |
| 678 | return sfield->value.s; |
| 679 | } |
| 680 | INLINE u2 dvmGetStaticFieldChar(const StaticField* sfield) { |
| 681 | return sfield->value.c; |
| 682 | } |
| 683 | INLINE s4 dvmGetStaticFieldInt(const StaticField* sfield) { |
| 684 | return sfield->value.i; |
| 685 | } |
| 686 | INLINE s8 dvmGetStaticFieldLong(const StaticField* sfield) { |
| 687 | return sfield->value.j; |
| 688 | } |
| 689 | INLINE float dvmGetStaticFieldFloat(const StaticField* sfield) { |
| 690 | return sfield->value.f; |
| 691 | } |
| 692 | INLINE double dvmGetStaticFieldDouble(const StaticField* sfield) { |
| 693 | return sfield->value.d; |
| 694 | } |
| 695 | INLINE Object* dvmGetStaticFieldObject(const StaticField* sfield) { |
| 696 | return sfield->value.l; |
| 697 | } |
| 698 | |
| 699 | INLINE void dvmSetStaticFieldBoolean(StaticField* sfield, bool val) { |
| 700 | sfield->value.i = val; |
| 701 | } |
| 702 | INLINE void dvmSetStaticFieldByte(StaticField* sfield, s1 val) { |
| 703 | sfield->value.i = val; |
| 704 | } |
| 705 | INLINE void dvmSetStaticFieldShort(StaticField* sfield, s2 val) { |
| 706 | sfield->value.i = val; |
| 707 | } |
| 708 | INLINE void dvmSetStaticFieldChar(StaticField* sfield, u2 val) { |
| 709 | sfield->value.i = val; |
| 710 | } |
| 711 | INLINE void dvmSetStaticFieldInt(StaticField* sfield, s4 val) { |
| 712 | sfield->value.i = val; |
| 713 | } |
| 714 | INLINE void dvmSetStaticFieldLong(StaticField* sfield, s8 val) { |
| 715 | sfield->value.j = val; |
| 716 | } |
| 717 | INLINE void dvmSetStaticFieldFloat(StaticField* sfield, float val) { |
| 718 | sfield->value.f = val; |
| 719 | } |
| 720 | INLINE void dvmSetStaticFieldDouble(StaticField* sfield, double val) { |
| 721 | sfield->value.d = val; |
| 722 | } |
| 723 | INLINE void dvmSetStaticFieldObject(StaticField* sfield, Object* val) { |
| 724 | sfield->value.l = val; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Helpers. |
| 729 | */ |
| 730 | INLINE bool dvmIsPublicMethod(const Method* method) { |
| 731 | return (method->accessFlags & ACC_PUBLIC) != 0; |
| 732 | } |
| 733 | INLINE bool dvmIsPrivateMethod(const Method* method) { |
| 734 | return (method->accessFlags & ACC_PRIVATE) != 0; |
| 735 | } |
| 736 | INLINE bool dvmIsStaticMethod(const Method* method) { |
| 737 | return (method->accessFlags & ACC_STATIC) != 0; |
| 738 | } |
| 739 | INLINE bool dvmIsSynchronizedMethod(const Method* method) { |
| 740 | return (method->accessFlags & ACC_SYNCHRONIZED) != 0; |
| 741 | } |
| 742 | INLINE bool dvmIsDeclaredSynchronizedMethod(const Method* method) { |
| 743 | return (method->accessFlags & ACC_DECLARED_SYNCHRONIZED) != 0; |
| 744 | } |
| 745 | INLINE bool dvmIsFinalMethod(const Method* method) { |
| 746 | return (method->accessFlags & ACC_FINAL) != 0; |
| 747 | } |
| 748 | INLINE bool dvmIsNativeMethod(const Method* method) { |
| 749 | return (method->accessFlags & ACC_NATIVE) != 0; |
| 750 | } |
| 751 | INLINE bool dvmIsAbstractMethod(const Method* method) { |
| 752 | return (method->accessFlags & ACC_ABSTRACT) != 0; |
| 753 | } |
| 754 | INLINE bool dvmIsMirandaMethod(const Method* method) { |
| 755 | return (method->accessFlags & ACC_MIRANDA) != 0; |
| 756 | } |
| 757 | INLINE bool dvmIsConstructorMethod(const Method* method) { |
| 758 | return *method->name == '<'; |
| 759 | } |
| 760 | /* Dalvik puts private, static, and constructors into non-virtual table */ |
| 761 | INLINE bool dvmIsDirectMethod(const Method* method) { |
| 762 | return dvmIsPrivateMethod(method) || |
| 763 | dvmIsStaticMethod(method) || |
| 764 | dvmIsConstructorMethod(method); |
| 765 | } |
| 766 | /* Get whether the given method has associated bytecode. This is the |
| 767 | * case for methods which are neither native nor abstract. */ |
| 768 | INLINE bool dvmIsBytecodeMethod(const Method* method) { |
| 769 | return (method->accessFlags & (ACC_NATIVE | ACC_ABSTRACT)) == 0; |
| 770 | } |
| 771 | |
| 772 | INLINE bool dvmIsProtectedField(const Field* field) { |
| 773 | return (field->accessFlags & ACC_PROTECTED) != 0; |
| 774 | } |
| 775 | INLINE bool dvmIsStaticField(const Field* field) { |
| 776 | return (field->accessFlags & ACC_STATIC) != 0; |
| 777 | } |
| 778 | INLINE bool dvmIsFinalField(const Field* field) { |
| 779 | return (field->accessFlags & ACC_FINAL) != 0; |
| 780 | } |
| 781 | |
| 782 | INLINE bool dvmIsInterfaceClass(const ClassObject* clazz) { |
| 783 | return (clazz->accessFlags & ACC_INTERFACE) != 0; |
| 784 | } |
| 785 | INLINE bool dvmIsPublicClass(const ClassObject* clazz) { |
| 786 | return (clazz->accessFlags & ACC_PUBLIC) != 0; |
| 787 | } |
| 788 | INLINE bool dvmIsFinalClass(const ClassObject* clazz) { |
| 789 | return (clazz->accessFlags & ACC_FINAL) != 0; |
| 790 | } |
| 791 | INLINE bool dvmIsAbstractClass(const ClassObject* clazz) { |
| 792 | return (clazz->accessFlags & ACC_ABSTRACT) != 0; |
| 793 | } |
| 794 | INLINE bool dvmIsAnnotationClass(const ClassObject* clazz) { |
| 795 | return (clazz->accessFlags & ACC_ANNOTATION) != 0; |
| 796 | } |
| 797 | INLINE bool dvmIsPrimitiveClass(const ClassObject* clazz) { |
| 798 | return clazz->primitiveType != PRIM_NOT; |
| 799 | } |
| 800 | |
| 801 | /* linked, here meaning prepared and resolved */ |
| 802 | INLINE bool dvmIsClassLinked(const ClassObject* clazz) { |
| 803 | return clazz->status >= CLASS_RESOLVED; |
| 804 | } |
| 805 | /* has class been verified? */ |
| 806 | INLINE bool dvmIsClassVerified(const ClassObject* clazz) { |
| 807 | return clazz->status >= CLASS_VERIFIED; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Get the associated code struct for a method. This returns NULL |
| 812 | * for non-bytecode methods. |
| 813 | */ |
| 814 | INLINE const DexCode* dvmGetMethodCode(const Method* meth) { |
| 815 | if (dvmIsBytecodeMethod(meth)) { |
| 816 | /* |
| 817 | * The insns field for a bytecode method actually points at |
| 818 | * &(DexCode.insns), so we can subtract back to get at the |
| 819 | * DexCode in front. |
| 820 | */ |
| 821 | return (const DexCode*) |
| 822 | (((const u1*) meth->insns) - offsetof(DexCode, insns)); |
| 823 | } else { |
| 824 | return NULL; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | * Get the size of the insns associated with a method. This returns 0 |
| 830 | * for non-bytecode methods. |
| 831 | */ |
| 832 | INLINE u4 dvmGetMethodInsnsSize(const Method* meth) { |
| 833 | const DexCode* pCode = dvmGetMethodCode(meth); |
| 834 | return (pCode == NULL) ? 0 : pCode->insnsSize; |
| 835 | } |
| 836 | |
| 837 | /* debugging */ |
| 838 | void dvmDumpObject(const Object* obj); |
| 839 | |
| 840 | #endif /*_DALVIK_OO_OBJECT*/ |