blob: 36ec4eb65cb3cef85bd4841a3db903c0a7c63616 [file] [log] [blame]
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +01001/*
2 * Copyright (C) 2015 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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080017#include "art_method.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070018#include "base/enums.h"
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010019#include "jni.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070020#include "mirror/array-inl.h"
21#include "mirror/class-inl.h"
22#include "mirror/dex_cache-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010024#include "stack.h"
25#include "thread.h"
26
27namespace art {
28
29namespace {
30
Vladimir Marko05792b92015-08-03 11:56:49 +010031extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env,
32 jclass,
33 jclass cls) {
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010034 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoc524e9e2019-03-26 10:54:50 +000035 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
Vladimir Marko05792b92015-08-03 11:56:49 +010036 size_t num_methods = dex_cache->NumResolvedMethods();
Vladimir Marko07bfbac2017-07-06 14:55:02 +010037 mirror::MethodDexCacheType* methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +010038 CHECK_EQ(num_methods != 0u, methods != nullptr);
39 if (num_methods == 0u) {
40 return nullptr;
41 }
42 jarray array;
43 if (sizeof(void*) == 4) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +010044 array = env->NewIntArray(2u * num_methods);
Vladimir Marko05792b92015-08-03 11:56:49 +010045 } else {
Vladimir Marko07bfbac2017-07-06 14:55:02 +010046 array = env->NewLongArray(2u * num_methods);
Vladimir Marko05792b92015-08-03 11:56:49 +010047 }
48 CHECK(array != nullptr);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010049 ObjPtr<mirror::Array> decoded_array = soa.Decode<mirror::Array>(array);
Vladimir Marko05792b92015-08-03 11:56:49 +010050 for (size_t i = 0; i != num_methods; ++i) {
David Srbecky5de5efe2021-02-15 21:23:00 +000051 auto pair = mirror::DexCache::GetNativePair(methods, i);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010052 uint32_t index = pair.index;
53 ArtMethod* method = pair.object;
54 if (sizeof(void*) == 4) {
Vladimir Markobcf17522018-06-01 13:14:32 +010055 ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(decoded_array);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010056 int_array->Set(2u * i, index);
Vladimir Markoca8de0a2018-07-04 11:56:08 +010057 int_array->Set(2u * i + 1u, reinterpret_cast32<jint>(method));
Vladimir Marko07bfbac2017-07-06 14:55:02 +010058 } else {
Vladimir Markobcf17522018-06-01 13:14:32 +010059 ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(decoded_array);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010060 long_array->Set(2u * i, index);
61 long_array->Set(2u * i + 1u, reinterpret_cast64<jlong>(method));
62 }
Vladimir Marko05792b92015-08-03 11:56:49 +010063 }
64 return array;
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010065}
66
67extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
68 JNIEnv*, jclass, jclass cls, jobject old_cache) {
69 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoc524e9e2019-03-26 10:54:50 +000070 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
Vladimir Marko05792b92015-08-03 11:56:49 +010071 size_t num_methods = dex_cache->NumResolvedMethods();
Vladimir Markoc524e9e2019-03-26 10:54:50 +000072 mirror::MethodDexCacheType* methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +010073 CHECK_EQ(num_methods != 0u, methods != nullptr);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010074 ObjPtr<mirror::Array> old = soa.Decode<mirror::Array>(old_cache);
Vladimir Marko05792b92015-08-03 11:56:49 +010075 CHECK_EQ(methods != nullptr, old != nullptr);
76 CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength()));
77 for (size_t i = 0; i != num_methods; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +010078 uint32_t index;
79 ArtMethod* method;
80 if (sizeof(void*) == 4) {
Vladimir Marko58412b12019-04-01 13:26:34 +010081 ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(old);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010082 index = static_cast<uint32_t>(int_array->Get(2u * i));
Vladimir Markoca8de0a2018-07-04 11:56:08 +010083 method = reinterpret_cast32<ArtMethod*>(int_array->Get(2u * i + 1u));
Vladimir Marko07bfbac2017-07-06 14:55:02 +010084 } else {
Vladimir Marko58412b12019-04-01 13:26:34 +010085 ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(old);
Vladimir Marko07bfbac2017-07-06 14:55:02 +010086 index = dchecked_integral_cast<uint32_t>(long_array->Get(2u * i));
87 method = reinterpret_cast64<ArtMethod*>(long_array->Get(2u * i + 1u));
88 }
89 mirror::MethodDexCachePair pair(method, index);
David Srbecky5de5efe2021-02-15 21:23:00 +000090 mirror::DexCache::SetNativePair(methods, i, pair);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010091 }
92}
93
94} // namespace
95
96} // namespace art