blob: 8d419f8a7bb25b56262a707ad9a17035e504e05b [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -07001/*
2 * Copyright (C) 2012 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 */
buzbee54330722011-08-23 16:46:55 -070016
Ian Rogers7655f292013-07-29 11:07:13 -070017#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
Ian Rogers450dcb52013-09-20 17:36:02 -070019
Mingyao Yang98d1cc82014-05-15 17:02:16 -070020#include <jni.h>
21#include <stdint.h>
22
Ian Rogers450dcb52013-09-20 17:36:02 -070023#include "base/macros.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "base/mutex.h"
Ian Rogers832336b2014-10-08 15:35:22 -070025#include "dex_instruction.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "gc/allocator_type.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070027#include "invoke_type.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "jvalue.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070029
Shih-wei Liao2d831012011-09-28 22:06:53 -070030namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070031
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033 class Array;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034 class ArtMethod;
Mathieu Chartierc7853442015-03-27 14:35:38 -070035 class Class;
Ian Rogers848871b2013-08-05 10:56:33 -070036 class Object;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037 class String;
Ian Rogers848871b2013-08-05 10:56:33 -070038} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070039
Mathieu Chartierc7853442015-03-27 14:35:38 -070040class ArtField;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041class ScopedObjectAccessAlreadyRunnable;
42class Thread;
43
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080044template <const bool kAccessCheck>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080045ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
46 mirror::ArtMethod* method,
47 Thread* self, bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070048 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070049
Andreas Gampe9f612ff2014-11-24 13:42:22 -080050ALWAYS_INLINE inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
51 Thread* self,
52 bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070053 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080054
Ian Rogers57b86d42012-03-27 16:05:41 -070055// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
56// cannot be resolved, throw an error. If it can, use it to create an instance.
57// When verification/compiler hasn't been able to verify access, optionally perform an access
58// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080059template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080060ALWAYS_INLINE inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
61 mirror::ArtMethod* method,
62 Thread* self,
63 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070065
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080066// Given the context of a calling Method and a resolved class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080067template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080068ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
69 Thread* self,
70 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080072
73// Given the context of a calling Method and an initialized class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080074template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080075ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
76 Thread* self,
77 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080079
80
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080081template <bool kAccessCheck>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080082ALWAYS_INLINE inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080083 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080084 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080085 bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070087
88// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
89// it cannot be resolved, throw an error. If it can, use it to create an array.
90// When verification/compiler hasn't been able to verify access, optionally perform an access
91// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080092template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080093ALWAYS_INLINE inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080094 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080095 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080096 Thread* self,
97 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070099
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800100template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800101ALWAYS_INLINE inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800102 int32_t component_count,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800103 mirror::ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800104 Thread* self,
105 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -0700106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800107
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800108extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, int32_t component_count,
109 mirror::ArtMethod* method, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800110 bool access_check,
111 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700113
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800114extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800115 int32_t component_count,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800116 mirror::ArtMethod* method,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800117 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800118 bool access_check,
119 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
Ian Rogers08f753d2012-08-24 14:35:25 -0700122// Type of find field operation for fast and slow case.
123enum FindFieldType {
124 InstanceObjectRead,
125 InstanceObjectWrite,
126 InstancePrimitiveRead,
127 InstancePrimitiveWrite,
128 StaticObjectRead,
129 StaticObjectWrite,
130 StaticPrimitiveRead,
131 StaticPrimitiveWrite,
132};
133
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200134template<FindFieldType type, bool access_check>
Mathieu Chartierc7853442015-03-27 14:35:38 -0700135inline ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800136 Thread* self, size_t expected_size)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200138
139template<InvokeType type, bool access_check>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800140inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
141 mirror::Object** this_object,
142 mirror::ArtMethod** referrer, Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700144
Ian Rogers08f753d2012-08-24 14:35:25 -0700145// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700146inline ArtField* FindFieldFast(uint32_t field_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800147 mirror::ArtMethod* referrer,
148 FindFieldType type, size_t expected_size)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700150
Ian Rogers08f753d2012-08-24 14:35:25 -0700151// Fast path method resolution that can't throw exceptions.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800152inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
153 mirror::Object* this_object,
154 mirror::ArtMethod* referrer,
155 bool access_check, InvokeType type)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700157
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800158inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
159 mirror::ArtMethod* referrer,
160 Thread* self, bool can_run_clinit,
161 bool verify_access)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700163
jeffhaod7521322012-11-21 15:38:24 -0800164extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800166inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer, uint32_t string_idx)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700168
Ian Rogerse5877a12014-07-16 12:06:35 -0700169// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800170inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogerse5877a12014-07-16 12:06:35 -0700171 NO_THREAD_SAFETY_ANALYSIS;
TDYa1273d71d802012-08-15 03:47:03 -0700172
Ian Rogerse5877a12014-07-16 12:06:35 -0700173void CheckReferenceResult(mirror::Object* o, Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa1273d71d802012-08-15 03:47:03 -0700175
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700176JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700177 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800178 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800180
Ian Rogers832336b2014-10-08 15:35:22 -0700181bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload)
182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
183
Ian Rogers450dcb52013-09-20 17:36:02 -0700184template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800185inline INT_TYPE art_float_to_integral(FLOAT_TYPE f);
Ian Rogers450dcb52013-09-20 17:36:02 -0700186
Shih-wei Liao2d831012011-09-28 22:06:53 -0700187} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700188
Ian Rogers7655f292013-07-29 11:07:13 -0700189#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_