blob: 4217cab6979f0294c1bd92b6652a6a051fe48586 [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"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000029#include "runtime.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070030
Shih-wei Liao2d831012011-09-28 22:06:53 -070031namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070032
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034 class Array;
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;
Mathieu Chartiere401d142015-04-22 13:56:20 -070041class ArtMethod;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042class ScopedObjectAccessAlreadyRunnable;
43class Thread;
44
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080045template <const bool kAccessCheck>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080046ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -070047 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080048 Thread* self, bool* slow_path)
Mathieu Chartier90443472015-07-16 20:32:27 -070049 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070050
Andreas Gampe9f612ff2014-11-24 13:42:22 -080051ALWAYS_INLINE inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
52 Thread* self,
53 bool* slow_path)
Mathieu Chartier90443472015-07-16 20:32:27 -070054 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080055
Ian Rogers57b86d42012-03-27 16:05:41 -070056// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
57// cannot be resolved, throw an error. If it can, use it to create an instance.
58// When verification/compiler hasn't been able to verify access, optionally perform an access
59// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080060template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080061ALWAYS_INLINE inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080063 Thread* self,
64 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -070065 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070066
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080067// Given the context of a calling Method and a resolved class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080068template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080069ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
70 Thread* self,
71 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080073
74// Given the context of a calling Method and an initialized class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080075template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080076ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
77 Thread* self,
78 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -070079 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080080
81
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080082template <bool kAccessCheck>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080083ALWAYS_INLINE inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080084 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080086 bool* slow_path)
Mathieu Chartier90443472015-07-16 20:32:27 -070087 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070088
89// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
90// it cannot be resolved, throw an error. If it can, use it to create an array.
91// When verification/compiler hasn't been able to verify access, optionally perform an access
92// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080093template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080094ALWAYS_INLINE inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080095 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070096 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080097 Thread* self,
98 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700100
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800101template <bool kAccessCheck, bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800102ALWAYS_INLINE inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800103 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800105 Thread* self,
106 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800108
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800109extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 ArtMethod* method, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800111 bool access_check,
112 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800115extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800116 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 ArtMethod* method,
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800118 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800119 bool access_check,
120 gc::AllocatorType allocator_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700121 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700122
Ian Rogers08f753d2012-08-24 14:35:25 -0700123// Type of find field operation for fast and slow case.
124enum FindFieldType {
125 InstanceObjectRead,
126 InstanceObjectWrite,
127 InstancePrimitiveRead,
128 InstancePrimitiveWrite,
129 StaticObjectRead,
130 StaticObjectWrite,
131 StaticPrimitiveRead,
132 StaticPrimitiveWrite,
133};
134
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200135template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136inline ArtField* FindFieldFromCode(
137 uint32_t field_idx, ArtMethod* referrer, Thread* self, size_t expected_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700138 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200139
140template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700141inline ArtMethod* FindMethodFromCode(
Andreas Gampe3a357142015-08-07 17:20:11 -0700142 uint32_t method_idx, mirror::Object** this_object, ArtMethod* referrer, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700143 SHARED_REQUIRES(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 Chartiere401d142015-04-22 13:56:20 -0700146inline ArtField* FindFieldFast(
147 uint32_t field_idx, ArtMethod* referrer, FindFieldType type, size_t expected_size)
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700149
Ian Rogers08f753d2012-08-24 14:35:25 -0700150// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700151inline ArtMethod* FindMethodFast(
152 uint32_t method_idx, mirror::Object* this_object, ArtMethod* referrer, bool access_check,
153 InvokeType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700154 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700155
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156inline mirror::Class* ResolveVerifyAndClinit(
157 uint32_t type_idx, ArtMethod* referrer, Thread* self, bool can_run_clinit, bool verify_access)
Mathieu Chartier90443472015-07-16 20:32:27 -0700158 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700159
Mathieu Chartier90443472015-07-16 20:32:27 -0700160extern void ThrowStackOverflowError(Thread* self) SHARED_REQUIRES(Locks::mutator_lock_);
jeffhaod7521322012-11-21 15:38:24 -0800161
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700163 SHARED_REQUIRES(Locks::mutator_lock_);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700164
Ian Rogerse5877a12014-07-16 12:06:35 -0700165// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800166inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogerse5877a12014-07-16 12:06:35 -0700167 NO_THREAD_SAFETY_ANALYSIS;
TDYa1273d71d802012-08-15 03:47:03 -0700168
Ian Rogerse5877a12014-07-16 12:06:35 -0700169void CheckReferenceResult(mirror::Object* o, Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700170 SHARED_REQUIRES(Locks::mutator_lock_);
TDYa1273d71d802012-08-15 03:47:03 -0700171
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700172JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700173 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800174 std::vector<jvalue>& args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700175 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800176
Ian Rogers832336b2014-10-08 15:35:22 -0700177bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload)
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers832336b2014-10-08 15:35:22 -0700179
Ian Rogers450dcb52013-09-20 17:36:02 -0700180template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800181inline INT_TYPE art_float_to_integral(FLOAT_TYPE f);
Ian Rogers450dcb52013-09-20 17:36:02 -0700182
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000183ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
184 Runtime::CalleeSaveType type,
185 bool do_caller_check = false);
186
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_