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()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 35 | 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(); |
| 37 | ArtMethod** methods = dex_cache->GetResolvedMethods(); |
| 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) { |
| 44 | array = env->NewIntArray(num_methods); |
| 45 | } else { |
| 46 | array = env->NewLongArray(num_methods); |
| 47 | } |
| 48 | CHECK(array != nullptr); |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 49 | mirror::PointerArray* pointer_array = soa.Decode<mirror::PointerArray>(array).Ptr(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 50 | for (size_t i = 0; i != num_methods; ++i) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 51 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(methods, i, kRuntimePointerSize); |
| 52 | pointer_array->SetElementPtrSize(i, method, kRuntimePointerSize); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 53 | } |
| 54 | return array; |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods( |
| 58 | JNIEnv*, jclass, jclass cls, jobject old_cache) { |
| 59 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 60 | mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 61 | size_t num_methods = dex_cache->NumResolvedMethods(); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 62 | ArtMethod** methods = soa.Decode<mirror::Class>(cls)->GetDexCache()->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 63 | CHECK_EQ(num_methods != 0u, methods != nullptr); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 64 | ObjPtr<mirror::PointerArray> old = soa.Decode<mirror::PointerArray>(old_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 65 | CHECK_EQ(methods != nullptr, old != nullptr); |
| 66 | CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength())); |
| 67 | for (size_t i = 0; i != num_methods; ++i) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 68 | ArtMethod* method = old->GetElementPtrSize<ArtMethod*>(i, kRuntimePointerSize); |
| 69 | mirror::DexCache::SetElementPtrSize(methods, i, method, kRuntimePointerSize); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | } // namespace art |