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   ObjPtr<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 = ObjPtr<mirror::IntArray>::DownCast(decoded_array);
56       int_array->Set(2u * i, index);
57       int_array->Set(2u * i + 1u, reinterpret_cast32<jint>(method));
58     } else {
59       ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(decoded_array);
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   ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
71   size_t num_methods = dex_cache->NumResolvedMethods();
72   mirror::MethodDexCacheType* methods = dex_cache->GetResolvedMethods();
73   CHECK_EQ(num_methods != 0u, methods != nullptr);
74   ObjPtr<mirror::Array> old = soa.Decode<mirror::Array>(old_cache);
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) {
78     uint32_t index;
79     ArtMethod* method;
80     if (sizeof(void*) == 4) {
81       ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(old);
82       index = static_cast<uint32_t>(int_array->Get(2u * i));
83       method = reinterpret_cast32<ArtMethod*>(int_array->Get(2u * i + 1u));
84     } else {
85       ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(old);
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);
90     mirror::DexCache::SetNativePairPtrSize(methods, i, pair, kRuntimePointerSize);
91   }
92 }
93 
94 }  // namespace
95 
96 }  // namespace art
97