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
17 #include "art_method.h"
18 #include "base/enums.h"
19 #include "jni.h"
20 #include "mirror/array-inl.h"
21 #include "mirror/class-inl.h"
22 #include "mirror/dex_cache-inl.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "stack.h"
25 #include "thread.h"
26
27 namespace art {
28
29 namespace {
30
Java_Main_cloneResolvedMethods(JNIEnv * env,jclass,jclass cls)31 extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env,
32 jclass,
33 jclass cls) {
34 ScopedObjectAccess soa(Thread::Current());
35 mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
36 size_t num_methods = dex_cache->NumResolvedMethods();
37 mirror::MethodDexCacheType* 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(2u * num_methods);
45 } else {
46 array = env->NewLongArray(2u * num_methods);
47 }
48 CHECK(array != nullptr);
49 ObjPtr<mirror::Array> decoded_array = soa.Decode<mirror::Array>(array);
50 for (size_t i = 0; i != num_methods; ++i) {
51 auto pair = mirror::DexCache::GetNativePairPtrSize(methods, i, kRuntimePointerSize);
52 uint32_t index = pair.index;
53 ArtMethod* method = pair.object;
54 if (sizeof(void*) == 4) {
55 ObjPtr<mirror::IntArray> int_array = down_cast<mirror::IntArray*>(decoded_array.Ptr());
56 int_array->Set(2u * i, index);
57 int_array->Set(2u * i + 1u, static_cast<jint>(reinterpret_cast<uintptr_t>(method)));
58 } else {
59 ObjPtr<mirror::LongArray> long_array = down_cast<mirror::LongArray*>(decoded_array.Ptr());
60 long_array->Set(2u * i, index);
61 long_array->Set(2u * i + 1u, reinterpret_cast64<jlong>(method));
62 }
63 }
64 return array;
65 }
66
Java_Main_restoreResolvedMethods(JNIEnv *,jclass,jclass cls,jobject old_cache)67 extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
68 JNIEnv*, jclass, jclass cls, jobject old_cache) {
69 ScopedObjectAccess soa(Thread::Current());
70 mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
71 size_t num_methods = dex_cache->NumResolvedMethods();
72 mirror::MethodDexCacheType* methods =
73 soa.Decode<mirror::Class>(cls)->GetDexCache()->GetResolvedMethods();
74 CHECK_EQ(num_methods != 0u, methods != nullptr);
75 ObjPtr<mirror::Array> old = soa.Decode<mirror::Array>(old_cache);
76 CHECK_EQ(methods != nullptr, old != nullptr);
77 CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength()));
78 for (size_t i = 0; i != num_methods; ++i) {
79 uint32_t index;
80 ArtMethod* method;
81 if (sizeof(void*) == 4) {
82 ObjPtr<mirror::IntArray> int_array = down_cast<mirror::IntArray*>(old.Ptr());
83 index = static_cast<uint32_t>(int_array->Get(2u * i));
84 method = reinterpret_cast<ArtMethod*>(static_cast<uint32_t>(int_array->Get(2u * i + 1u)));
85 } else {
86 ObjPtr<mirror::LongArray> long_array = down_cast<mirror::LongArray*>(old.Ptr());
87 index = dchecked_integral_cast<uint32_t>(long_array->Get(2u * i));
88 method = reinterpret_cast64<ArtMethod*>(long_array->Get(2u * i + 1u));
89 }
90 mirror::MethodDexCachePair pair(method, index);
91 mirror::DexCache::SetNativePairPtrSize(methods, i, pair, kRuntimePointerSize);
92 }
93 }
94
95 } // namespace
96
97 } // namespace art
98