Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 17 | #include "art_method.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 18 | #include "base/enums.h" |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 19 | #include "jni.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 20 | #include "mirror/array-inl.h" |
| 21 | #include "mirror/class-inl.h" |
| 22 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 24 | #include "stack.h" |
| 25 | #include "thread.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace { |
| 30 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 31 | extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env, |
| 32 | jclass, |
| 33 | jclass cls) { |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 34 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 35 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 36 | size_t num_methods = dex_cache->NumResolvedMethods(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 37 | mirror::MethodDexCacheType* methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 38 | 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 Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 44 | array = env->NewIntArray(2u * num_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 45 | } else { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 46 | array = env->NewLongArray(2u * num_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 47 | } |
| 48 | CHECK(array != nullptr); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 49 | ObjPtr<mirror::Array> decoded_array = soa.Decode<mirror::Array>(array); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 50 | for (size_t i = 0; i != num_methods; ++i) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 51 | auto pair = mirror::DexCache::GetNativePair(methods, i); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 52 | uint32_t index = pair.index; |
| 53 | ArtMethod* method = pair.object; |
| 54 | if (sizeof(void*) == 4) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 55 | ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(decoded_array); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 56 | int_array->Set(2u * i, index); |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 57 | int_array->Set(2u * i + 1u, reinterpret_cast32<jint>(method)); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 58 | } else { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 59 | ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(decoded_array); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 60 | long_array->Set(2u * i, index); |
| 61 | long_array->Set(2u * i + 1u, reinterpret_cast64<jlong>(method)); |
| 62 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 63 | } |
| 64 | return array; |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods( |
| 68 | JNIEnv*, jclass, jclass cls, jobject old_cache) { |
| 69 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 70 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 71 | size_t num_methods = dex_cache->NumResolvedMethods(); |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 72 | mirror::MethodDexCacheType* methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 73 | CHECK_EQ(num_methods != 0u, methods != nullptr); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 74 | ObjPtr<mirror::Array> old = soa.Decode<mirror::Array>(old_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 75 | 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 Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 78 | uint32_t index; |
| 79 | ArtMethod* method; |
| 80 | if (sizeof(void*) == 4) { |
Vladimir Marko | 58412b1 | 2019-04-01 13:26:34 +0100 | [diff] [blame] | 81 | ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(old); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 82 | index = static_cast<uint32_t>(int_array->Get(2u * i)); |
Vladimir Marko | ca8de0a | 2018-07-04 11:56:08 +0100 | [diff] [blame] | 83 | method = reinterpret_cast32<ArtMethod*>(int_array->Get(2u * i + 1u)); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 84 | } else { |
Vladimir Marko | 58412b1 | 2019-04-01 13:26:34 +0100 | [diff] [blame] | 85 | ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(old); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 86 | 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 Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 90 | mirror::DexCache::SetNativePair(methods, i, pair); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | } // namespace art |