blob: d10031a91b9cf92a910359b074d2d2f5f4346fc9 [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
324 bool IsRegistered() const;
325
326 void RegisterNative(Thread* self, const void* native_method)
327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
328
329 void UnregisterNative(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
330
331 static MemberOffset NativeMethodOffset() {
332 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_);
333 }
334
335 const void* GetNativeMethod() const {
336 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
337 }
338
339 void SetNativeMethod(const void*);
340
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 static MemberOffset GetMethodIndexOffset() {
342 return OFFSET_OF_OBJECT_MEMBER(AbstractMethod, method_index_);
343 }
344
345 uint32_t GetCoreSpillMask() const {
346 return GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, core_spill_mask_), false);
347 }
348
349 void SetCoreSpillMask(uint32_t core_spill_mask) {
350 // Computed during compilation
351 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, core_spill_mask_), core_spill_mask, false);
352 }
353
354 uint32_t GetFpSpillMask() const {
355 return GetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, fp_spill_mask_), false);
356 }
357
358 void SetFpSpillMask(uint32_t fp_spill_mask) {
359 // Computed during compilation
360 SetField32(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, fp_spill_mask_), fp_spill_mask, false);
361 }
362
363 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
364 // conventions for a method of managed code. Returns false for Proxy methods.
365 bool IsRuntimeMethod() const;
366
367 // Is this a hand crafted method used for something like describing callee saves?
368 bool IsCalleeSaveMethod() const;
369
370 bool IsResolutionMethod() const;
371
372 uintptr_t NativePcOffset(const uintptr_t pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
373
374 // Converts a native PC to a dex PC.
375 uint32_t ToDexPc(const uintptr_t pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
376
377 // Converts a dex PC to a native PC.
378 uintptr_t ToNativePc(const uint32_t dex_pc) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
379
380 // Converts a dex PC to the first corresponding safepoint PC.
381 uintptr_t ToFirstNativeSafepointPc(const uint32_t dex_pc)
382 const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
383
384 // Find the catch block for the given exception type and dex_pc
385 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const
386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
387
388 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
389
390 static Class* GetConstructorClass() {
391 return java_lang_reflect_Constructor_;
392 }
393
394 static Class* GetMethodClass() {
395 return java_lang_reflect_Method_;
396 }
397
398 static void ResetClasses();
399
400 protected:
401 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
402 // The class we are a part of
403 Class* declaring_class_;
404
405 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
406 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
407
408 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
409 ObjectArray<AbstractMethod>* dex_cache_resolved_methods_;
410
411 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
412 ObjectArray<Class>* dex_cache_resolved_types_;
413
414 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
415 ObjectArray<String>* dex_cache_strings_;
416
417 // Access flags; low 16 bits are defined by spec.
418 uint32_t access_flags_;
419
420 // Compiled code associated with this method for callers from managed code.
421 // May be compiled managed code or a bridge for invoking a native method.
422 const void* code_;
423
424 // Offset to the CodeItem.
425 uint32_t code_item_offset_;
426
427 // Architecture-dependent register spill mask
428 uint32_t core_spill_mask_;
429
430 // Architecture-dependent register spill mask
431 uint32_t fp_spill_mask_;
432
433 // Total size in bytes of the frame
434 size_t frame_size_in_bytes_;
435
436 // Garbage collection map of native PC offsets to reference bitmaps.
437 const uint8_t* native_gc_map_;
438
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 // Mapping from native pc to dex pc
440 const uint32_t* mapping_table_;
441
442 // Index into method_ids of the dex file associated with this method
443 uint32_t method_dex_index_;
444
445 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
446 //
447 // For abstract methods in an interface class, this is the offset of the method in
448 // "iftable_->Get(n)->GetMethodArray()".
449 //
450 // For static and direct methods this is the index in the direct methods table.
451 uint32_t method_index_;
452
453 // The target native method registered with this method
454 const void* native_method_;
455
456 // When a register is promoted into a register, the spill mask holds which registers hold dex
457 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
458 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
459 const uint16_t* vmap_table_;
460
461 static Class* java_lang_reflect_Constructor_;
462 static Class* java_lang_reflect_Method_;
463
464 friend struct art::AbstractMethodOffsets; // for verifying offset information
465 friend struct art::ConstructorMethodOffsets; // for verifying offset information
466 friend struct art::MethodOffsets; // for verifying offset information
467 DISALLOW_IMPLICIT_CONSTRUCTORS(AbstractMethod);
468};
469
470class MANAGED Method : public AbstractMethod {
471
472};
473
474class MANAGED Constructor : public AbstractMethod {
475
476};
477
478class MANAGED AbstractMethodClass : public Class {
479 private:
480 Object* ORDER_BY_SIGNATURE_;
481 friend struct art::MethodClassOffsets; // for verifying offset information
482 DISALLOW_IMPLICIT_CONSTRUCTORS(AbstractMethodClass);
483};
484
485} // namespace mirror
486} // namespace art
487
488#endif // ART_SRC_MIRROR_METHOD_H_