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-inl.h"
18 #include "jni.h"
19 #include "scoped_thread_state_change.h"
20 #include "stack.h"
21 #include "thread.h"
22 
23 namespace art {
24 
25 namespace {
26 
Java_Main_cloneResolvedMethods(JNIEnv * env,jclass,jclass cls)27 extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env,
28                                                                     jclass,
29                                                                     jclass cls) {
30   ScopedObjectAccess soa(Thread::Current());
31   mirror::DexCache* dex_cache = soa.Decode<mirror::Class*>(cls)->GetDexCache();
32   size_t num_methods = dex_cache->NumResolvedMethods();
33   ArtMethod** methods = dex_cache->GetResolvedMethods();
34   CHECK_EQ(num_methods != 0u, methods != nullptr);
35   if (num_methods == 0u) {
36     return nullptr;
37   }
38   jarray array;
39   if (sizeof(void*) == 4) {
40     array = env->NewIntArray(num_methods);
41   } else {
42     array = env->NewLongArray(num_methods);
43   }
44   CHECK(array != nullptr);
45   mirror::PointerArray* pointer_array = soa.Decode<mirror::PointerArray*>(array);
46   for (size_t i = 0; i != num_methods; ++i) {
47     ArtMethod* method = mirror::DexCache::GetElementPtrSize(methods, i, sizeof(void*));
48     pointer_array->SetElementPtrSize(i, method, sizeof(void*));
49   }
50   return array;
51 }
52 
Java_Main_restoreResolvedMethods(JNIEnv *,jclass,jclass cls,jobject old_cache)53 extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
54     JNIEnv*, jclass, jclass cls, jobject old_cache) {
55   ScopedObjectAccess soa(Thread::Current());
56   mirror::DexCache* dex_cache = soa.Decode<mirror::Class*>(cls)->GetDexCache();
57   size_t num_methods = dex_cache->NumResolvedMethods();
58   ArtMethod** methods = soa.Decode<mirror::Class*>(cls)->GetDexCache()->GetResolvedMethods();
59   CHECK_EQ(num_methods != 0u, methods != nullptr);
60   mirror::PointerArray* old = soa.Decode<mirror::PointerArray*>(old_cache);
61   CHECK_EQ(methods != nullptr, old != nullptr);
62   CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength()));
63   for (size_t i = 0; i != num_methods; ++i) {
64     ArtMethod* method = old->GetElementPtrSize<ArtMethod*>(i, sizeof(void*));
65     mirror::DexCache::SetElementPtrSize(methods, i, method, sizeof(void*));
66   }
67 }
68 
69 }  // namespace
70 
71 }  // namespace art
72