blob: 47865a2a806257e1e5833bbf86d149be9582ea2a [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;
Mathieu Chartierc7853442015-03-27 14:35:38 -070034 class Class;
Ian Rogers848871b2013-08-05 10:56:33 -070035 class Object;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036 class String;
Ian Rogers848871b2013-08-05 10:56:33 -070037} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070038
Mathieu Chartierc7853442015-03-27 14:35:38 -070039class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070040class ArtMethod;
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,
Mathieu Chartiere401d142015-04-22 13:56:20 -070046 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080047 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080062 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700109 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 Chartiere401d142015-04-22 13:56:20 -0700116 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 Chartiere401d142015-04-22 13:56:20 -0700135inline ArtField* FindFieldFromCode(
136 uint32_t field_idx, ArtMethod* referrer, 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>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140inline ArtMethod* FindMethodFromCode(
141 uint32_t method_idx, mirror::Object** this_object, ArtMethod** referrer, Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700143
Ian Rogers08f753d2012-08-24 14:35:25 -0700144// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145inline ArtField* FindFieldFast(
146 uint32_t field_idx, ArtMethod* referrer, FindFieldType type, size_t expected_size)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700148
Ian Rogers08f753d2012-08-24 14:35:25 -0700149// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150inline ArtMethod* FindMethodFast(
151 uint32_t method_idx, mirror::Object* this_object, ArtMethod* referrer, bool access_check,
152 InvokeType type)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700154
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155inline mirror::Class* ResolveVerifyAndClinit(
156 uint32_t type_idx, ArtMethod* referrer, Thread* self, bool can_run_clinit, bool verify_access)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700158
jeffhaod7521322012-11-21 15:38:24 -0800159extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700163
Ian Rogerse5877a12014-07-16 12:06:35 -0700164// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800165inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogerse5877a12014-07-16 12:06:35 -0700166 NO_THREAD_SAFETY_ANALYSIS;
TDYa1273d71d802012-08-15 03:47:03 -0700167
Ian Rogerse5877a12014-07-16 12:06:35 -0700168void CheckReferenceResult(mirror::Object* o, Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa1273d71d802012-08-15 03:47:03 -0700170
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700171JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700172 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800173 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700174 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800175
Ian Rogers832336b2014-10-08 15:35:22 -0700176bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload)
177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
178
Ian Rogers450dcb52013-09-20 17:36:02 -0700179template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800180inline INT_TYPE art_float_to_integral(FLOAT_TYPE f);
Ian Rogers450dcb52013-09-20 17:36:02 -0700181
Shih-wei Liao2d831012011-09-28 22:06:53 -0700182} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700183
Ian Rogers7655f292013-07-29 11:07:13 -0700184#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_