blob: 944091573192b7feb1ad53aede5d3980cd240946 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 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#ifndef ART_SRC_MIRROR_METHOD_H_
18#define ART_SRC_MIRROR_METHOD_H_
19
20#include "class.h"
21#include "invoke_type.h"
22#include "locks.h"
23#include "modifiers.h"
24#include "object.h"
25
26namespace art {
27
28struct AbstractMethodOffsets;
29struct ConstructorMethodOffsets;
30union JValue;
31struct MethodClassOffsets;
32struct MethodOffsets;
33class StringPiece;
34
35namespace mirror {
36
37class StaticStorageBase;
38
39// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
40class MANAGED AbstractMethod : public Object {
41 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 Class* GetDeclaringClass() const;
43
44 void SetDeclaringClass(Class *new_declaring_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
45
46 static MemberOffset DeclaringClassOffset() {
47 return MemberOffset(OFFSETOF_MEMBER(AbstractMethod, declaring_class_));
48 }
49
Jeff Hao5d917302013-02-27 17:57:33 -080050 static MemberOffset CodeOffset() {
51 return MemberOffset(OFFSETOF_MEMBER(AbstractMethod, code_));
52 }
53
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 uint32_t GetAccessFlags() const;
55
56 void SetAccessFlags(uint32_t new_access_flags) {
57 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, access_flags_), new_access_flags, false);
58 }
59
60 // Approximate what kind of method call would be used for this method.
61 InvokeType GetInvokeType() const;
62
63 // Returns true if the method is declared public.
64 bool IsPublic() const {
65 return (GetAccessFlags() & kAccPublic) != 0;
66 }
67
68 // Returns true if the method is declared private.
69 bool IsPrivate() const {
70 return (GetAccessFlags() & kAccPrivate) != 0;
71 }
72
73 // Returns true if the method is declared static.
74 bool IsStatic() const {
75 return (GetAccessFlags() & kAccStatic) != 0;
76 }
77
78 // Returns true if the method is a constructor.
79 bool IsConstructor() const {
80 return (GetAccessFlags() & kAccConstructor) != 0;
81 }
82
83 // Returns true if the method is static, private, or a constructor.
84 bool IsDirect() const {
85 return IsDirect(GetAccessFlags());
86 }
87
88 static bool IsDirect(uint32_t access_flags) {
89 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
90 }
91
92 // Returns true if the method is declared synchronized.
93 bool IsSynchronized() const {
94 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
95 return (GetAccessFlags() & synchonized) != 0;
96 }
97
98 bool IsFinal() const {
99 return (GetAccessFlags() & kAccFinal) != 0;
100 }
101
102 bool IsMiranda() const {
103 return (GetAccessFlags() & kAccMiranda) != 0;
104 }
105
106 bool IsNative() const {
107 return (GetAccessFlags() & kAccNative) != 0;
108 }
109
110 bool IsAbstract() const {
111 return (GetAccessFlags() & kAccAbstract) != 0;
112 }
113
114 bool IsSynthetic() const {
115 return (GetAccessFlags() & kAccSynthetic) != 0;
116 }
117
118 bool IsProxyMethod() const;
119
120 bool CheckIncompatibleClassChange(InvokeType type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
122 uint16_t GetMethodIndex() const;
123
124 size_t GetVtableIndex() const {
125 return GetMethodIndex();
126 }
127
128 void SetMethodIndex(uint16_t new_method_index) {
129 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, method_index_), new_method_index, false);
130 }
131
132 static MemberOffset MethodIndexOffset() {
133 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, method_index_);
134 }
135
136 uint32_t GetCodeItemOffset() const {
137 return GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, code_item_offset_), false);
138 }
139
140 void SetCodeItemOffset(uint32_t new_code_off) {
141 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, code_item_offset_), new_code_off, false);
142 }
143
144 // Number of 32bit registers that would be required to hold all the arguments
145 static size_t NumArgRegisters(const StringPiece& shorty);
146
147 uint32_t GetDexMethodIndex() const;
148
149 void SetDexMethodIndex(uint32_t new_idx) {
150 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, method_dex_index_), new_idx, false);
151 }
152
153 ObjectArray<String>* GetDexCacheStrings() const;
154 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings)
155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156
157 static MemberOffset DexCacheStringsOffset() {
158 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_);
159 }
160
161 static MemberOffset DexCacheResolvedMethodsOffset() {
162 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_);
163 }
164
165 static MemberOffset DexCacheResolvedTypesOffset() {
166 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_);
167 }
168
169 static MemberOffset DexCacheInitializedStaticStorageOffset() {
170 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod,
171 dex_cache_initialized_static_storage_);
172 }
173
174 ObjectArray<AbstractMethod>* GetDexCacheResolvedMethods() const;
175 void SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods)
176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177
178 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
179 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types)
180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
181
182 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
183 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value)
184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
185
186 // Find the method that this method overrides
187 AbstractMethod* FindOverriddenMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188
Jeff Hao6474d192013-03-26 14:08:09 -0700189 void Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result, char result_type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191
192 const void* GetCode() const {
193 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, code_), false);
194 }
195
196 void SetCode(const void* code) {
197 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, code_), code, false);
198 }
199
200 uint32_t GetCodeSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
201
202 bool IsWithinCode(uintptr_t pc) const
203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
204 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
205 if (code == 0) {
206 return pc == 0;
207 }
208 /*
209 * During a stack walk, a return PC may point to the end of the code + 1
210 * (in the case that the last instruction is a call that isn't expected to
211 * return. Thus, we check <= code + GetCodeSize().
212 */
213 return (code <= pc && pc <= code + GetCodeSize());
214 }
215
216 void AssertPcIsWithinCode(uintptr_t pc) const
217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218
219 uint32_t GetOatCodeOffset() const;
220
221 void SetOatCodeOffset(uint32_t code_offset);
222
223 static MemberOffset GetCodeOffset() {
224 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, code_);
225 }
226
227 const uint32_t* GetMappingTable() const {
228 const uint32_t* map = GetMappingTableRaw();
229 if (map == NULL) {
230 return map;
231 }
232 return map + 1;
233 }
234
235 uint32_t GetPcToDexMappingTableLength() const {
236 const uint32_t* map = GetMappingTableRaw();
237 if (map == NULL) {
238 return 0;
239 }
240 return map[2];
241 }
242
243 const uint32_t* GetPcToDexMappingTable() const {
244 const uint32_t* map = GetMappingTableRaw();
245 if (map == NULL) {
246 return map;
247 }
248 return map + 3;
249 }
250
251
252 uint32_t GetDexToPcMappingTableLength() const {
253 const uint32_t* map = GetMappingTableRaw();
254 if (map == NULL) {
255 return 0;
256 }
257 return map[1] - map[2];
258 }
259
260 const uint32_t* GetDexToPcMappingTable() const {
261 const uint32_t* map = GetMappingTableRaw();
262 if (map == NULL) {
263 return map;
264 }
265 return map + 3 + map[2];
266 }
267
268
269 const uint32_t* GetMappingTableRaw() const {
270 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, mapping_table_), false);
271 }
272
273 void SetMappingTable(const uint32_t* mapping_table) {
274 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, mapping_table_),
275 mapping_table, false);
276 }
277
278 uint32_t GetOatMappingTableOffset() const;
279
280 void SetOatMappingTableOffset(uint32_t mapping_table_offset);
281
282 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
283 const uint16_t* GetVmapTableRaw() const {
284 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, vmap_table_), false);
285 }
286
287 void SetVmapTable(const uint16_t* vmap_table) {
288 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, vmap_table_), vmap_table, false);
289 }
290
291 uint32_t GetOatVmapTableOffset() const;
292
293 void SetOatVmapTableOffset(uint32_t vmap_table_offset);
294
295 const uint8_t* GetNativeGcMap() const {
296 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_), false);
297 }
298 void SetNativeGcMap(const uint8_t* data) {
299 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_), data,
300 false);
301 }
302
303 // When building the oat need a convenient place to stuff the offset of the native GC map.
304 void SetOatNativeGcMapOffset(uint32_t gc_map_offset);
305 uint32_t GetOatNativeGcMapOffset() const;
306
307 size_t GetFrameSizeInBytes() const {
308 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
309 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, frame_size_in_bytes_), false);
310 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
311 return result;
312 }
313
314 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
315 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
316 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, frame_size_in_bytes_),
317 new_frame_size_in_bytes, false);
318 }
319
320 size_t GetReturnPcOffsetInBytes() const {
321 return GetFrameSizeInBytes() - kPointerSize;
322 }
323
Ian Rogers62d6c772013-02-27 08:32:07 -0800324 size_t GetSirtOffsetInBytes() const {
325 CHECK(IsNative());
326 return kPointerSize;
327 }
328
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 bool IsRegistered() const;
330
331 void RegisterNative(Thread* self, const void* native_method)
332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
333
334 void UnregisterNative(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
335
336 static MemberOffset NativeMethodOffset() {
337 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_);
338 }
339
340 const void* GetNativeMethod() const {
341 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
342 }
343
344 void SetNativeMethod(const void*);
345
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346 static MemberOffset GetMethodIndexOffset() {
347 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, method_index_);
348 }
349
350 uint32_t GetCoreSpillMask() const {
351 return GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, core_spill_mask_), false);
352 }
353
354 void SetCoreSpillMask(uint32_t core_spill_mask) {
355 // Computed during compilation
356 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, core_spill_mask_), core_spill_mask, false);
357 }
358
359 uint32_t GetFpSpillMask() const {
360 return GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, fp_spill_mask_), false);
361 }
362
363 void SetFpSpillMask(uint32_t fp_spill_mask) {
364 // Computed during compilation
365 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, fp_spill_mask_), fp_spill_mask, false);
366 }
367
368 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
369 // conventions for a method of managed code. Returns false for Proxy methods.
370 bool IsRuntimeMethod() const;
371
372 // Is this a hand crafted method used for something like describing callee saves?
373 bool IsCalleeSaveMethod() const;
374
375 bool IsResolutionMethod() const;
376
377 uintptr_t NativePcOffset(const uintptr_t pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
378
379 // Converts a native PC to a dex PC.
380 uint32_t ToDexPc(const uintptr_t pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
381
382 // Converts a dex PC to a native PC.
383 uintptr_t ToNativePc(const uint32_t dex_pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
384
385 // Converts a dex PC to the first corresponding safepoint PC.
386 uintptr_t ToFirstNativeSafepointPc(const uint32_t dex_pc)
387 const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
388
389 // Find the catch block for the given exception type and dex_pc
390 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const
391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
392
393 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
394
395 static Class* GetConstructorClass() {
396 return java_lang_reflect_Constructor_;
397 }
398
399 static Class* GetMethodClass() {
400 return java_lang_reflect_Method_;
401 }
402
403 static void ResetClasses();
404
405 protected:
406 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
407 // The class we are a part of
408 Class* declaring_class_;
409
410 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
411 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
412
413 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
414 ObjectArray<AbstractMethod>* dex_cache_resolved_methods_;
415
416 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
417 ObjectArray<Class>* dex_cache_resolved_types_;
418
419 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
420 ObjectArray<String>* dex_cache_strings_;
421
422 // Access flags; low 16 bits are defined by spec.
423 uint32_t access_flags_;
424
425 // Compiled code associated with this method for callers from managed code.
426 // May be compiled managed code or a bridge for invoking a native method.
427 const void* code_;
428
429 // Offset to the CodeItem.
430 uint32_t code_item_offset_;
431
432 // Architecture-dependent register spill mask
433 uint32_t core_spill_mask_;
434
435 // Architecture-dependent register spill mask
436 uint32_t fp_spill_mask_;
437
438 // Total size in bytes of the frame
439 size_t frame_size_in_bytes_;
440
441 // Garbage collection map of native PC offsets to reference bitmaps.
442 const uint8_t* native_gc_map_;
443
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 // Mapping from native pc to dex pc
445 const uint32_t* mapping_table_;
446
447 // Index into method_ids of the dex file associated with this method
448 uint32_t method_dex_index_;
449
450 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
451 //
452 // For abstract methods in an interface class, this is the offset of the method in
453 // "iftable_->Get(n)->GetMethodArray()".
454 //
455 // For static and direct methods this is the index in the direct methods table.
456 uint32_t method_index_;
457
458 // The target native method registered with this method
459 const void* native_method_;
460
461 // When a register is promoted into a register, the spill mask holds which registers hold dex
462 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
463 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
464 const uint16_t* vmap_table_;
465
466 static Class* java_lang_reflect_Constructor_;
467 static Class* java_lang_reflect_Method_;
468
469 friend struct art::AbstractMethodOffsets; // for verifying offset information
470 friend struct art::ConstructorMethodOffsets; // for verifying offset information
471 friend struct art::MethodOffsets; // for verifying offset information
472 DISALLOW_IMPLICIT_CONSTRUCTORS(AbstractMethod);
473};
474
475class MANAGED Method : public AbstractMethod {
476
477};
478
479class MANAGED Constructor : public AbstractMethod {
480
481};
482
483class MANAGED AbstractMethodClass : public Class {
484 private:
485 Object* ORDER_BY_SIGNATURE_;
486 friend struct art::MethodClassOffsets; // for verifying offset information
487 DISALLOW_IMPLICIT_CONSTRUCTORS(AbstractMethodClass);
488};
489
490} // namespace mirror
491} // namespace art
492
493#endif // ART_SRC_MIRROR_METHOD_H_