blob: 9ba05bc706f6000f8c6945515be369c7bb11921d [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());
Mathieu Chartier0795f232016-09-27 18:43:30 -070035 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();
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 Chartier1cc62e42016-10-03 18:01:28 -070049 mirror::PointerArray* pointer_array = soa.Decode<mirror::PointerArray>(array).Ptr();
Vladimir Marko05792b92015-08-03 11:56:49 +010050 for (size_t i = 0; i != num_methods; ++i) {
Andreas Gampe542451c2016-07-26 09:02:02 -070051 ArtMethod* method = mirror::DexCache::GetElementPtrSize(methods, i, kRuntimePointerSize);
52 pointer_array->SetElementPtrSize(i, method, kRuntimePointerSize);
Vladimir Marko05792b92015-08-03 11:56:49 +010053 }
54 return array;
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010055}
56
57extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
58 JNIEnv*, jclass, jclass cls, jobject old_cache) {
59 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -070060 mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
Vladimir Marko05792b92015-08-03 11:56:49 +010061 size_t num_methods = dex_cache->NumResolvedMethods();
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 ArtMethod** methods = soa.Decode<mirror::Class>(cls)->GetDexCache()->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +010063 CHECK_EQ(num_methods != 0u, methods != nullptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070064 ObjPtr<mirror::PointerArray> old = soa.Decode<mirror::PointerArray>(old_cache);
Vladimir Marko05792b92015-08-03 11:56:49 +010065 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 Gampe542451c2016-07-26 09:02:02 -070068 ArtMethod* method = old->GetElementPtrSize<ArtMethod*>(i, kRuntimePointerSize);
69 mirror::DexCache::SetElementPtrSize(methods, i, method, kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010070 }
71}
72
73} // namespace
74
75} // namespace art