xref: /aosp_15_r20/art/test/497-inlining-and-class-loader/clear_dex_cache.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "art_method.h"
18*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
19*795d594fSAndroid Build Coastguard Worker #include "jni.h"
20*795d594fSAndroid Build Coastguard Worker #include "mirror/array-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
22*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "stack.h"
25*795d594fSAndroid Build Coastguard Worker #include "thread.h"
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker namespace art {
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace {
30*795d594fSAndroid Build Coastguard Worker 
Java_Main_cloneResolvedMethods(JNIEnv * env,jclass,jclass cls)31*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env,
32*795d594fSAndroid Build Coastguard Worker                                                                     jclass,
33*795d594fSAndroid Build Coastguard Worker                                                                     jclass cls) {
34*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
35*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
36*795d594fSAndroid Build Coastguard Worker   size_t num_methods = dex_cache->NumResolvedMethods();
37*795d594fSAndroid Build Coastguard Worker   auto* methods = dex_cache->GetResolvedMethods();
38*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(num_methods != 0u, methods != nullptr);
39*795d594fSAndroid Build Coastguard Worker   if (num_methods == 0u) {
40*795d594fSAndroid Build Coastguard Worker     return nullptr;
41*795d594fSAndroid Build Coastguard Worker   }
42*795d594fSAndroid Build Coastguard Worker   jarray array;
43*795d594fSAndroid Build Coastguard Worker   if (sizeof(void*) == 4) {
44*795d594fSAndroid Build Coastguard Worker     array = env->NewIntArray(2u * num_methods);
45*795d594fSAndroid Build Coastguard Worker   } else {
46*795d594fSAndroid Build Coastguard Worker     array = env->NewLongArray(2u * num_methods);
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker   CHECK(array != nullptr);
49*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Array> decoded_array = soa.Decode<mirror::Array>(array);
50*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i != num_methods; ++i) {
51*795d594fSAndroid Build Coastguard Worker     auto pair = methods->GetNativePair(i);
52*795d594fSAndroid Build Coastguard Worker     uint32_t index = pair.index;
53*795d594fSAndroid Build Coastguard Worker     ArtMethod* method = pair.object;
54*795d594fSAndroid Build Coastguard Worker     if (sizeof(void*) == 4) {
55*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(decoded_array);
56*795d594fSAndroid Build Coastguard Worker       int_array->Set(2u * i, index);
57*795d594fSAndroid Build Coastguard Worker       int_array->Set(2u * i + 1u, reinterpret_cast32<jint>(method));
58*795d594fSAndroid Build Coastguard Worker     } else {
59*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(decoded_array);
60*795d594fSAndroid Build Coastguard Worker       long_array->Set(2u * i, index);
61*795d594fSAndroid Build Coastguard Worker       long_array->Set(2u * i + 1u, reinterpret_cast64<jlong>(method));
62*795d594fSAndroid Build Coastguard Worker     }
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker   return array;
65*795d594fSAndroid Build Coastguard Worker }
66*795d594fSAndroid Build Coastguard Worker 
Java_Main_restoreResolvedMethods(JNIEnv *,jclass,jclass cls,jobject old_cache)67*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
68*795d594fSAndroid Build Coastguard Worker     JNIEnv*, jclass, jclass cls, jobject old_cache) {
69*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
70*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
71*795d594fSAndroid Build Coastguard Worker   size_t num_methods = dex_cache->NumResolvedMethods();
72*795d594fSAndroid Build Coastguard Worker   auto* methods = dex_cache->GetResolvedMethods();
73*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(num_methods != 0u, methods != nullptr);
74*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Array> old = soa.Decode<mirror::Array>(old_cache);
75*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(methods != nullptr, old != nullptr);
76*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength()));
77*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i != num_methods; ++i) {
78*795d594fSAndroid Build Coastguard Worker     uint32_t index;
79*795d594fSAndroid Build Coastguard Worker     ArtMethod* method;
80*795d594fSAndroid Build Coastguard Worker     if (sizeof(void*) == 4) {
81*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::IntArray> int_array = ObjPtr<mirror::IntArray>::DownCast(old);
82*795d594fSAndroid Build Coastguard Worker       index = static_cast<uint32_t>(int_array->Get(2u * i));
83*795d594fSAndroid Build Coastguard Worker       method = reinterpret_cast32<ArtMethod*>(int_array->Get(2u * i + 1u));
84*795d594fSAndroid Build Coastguard Worker     } else {
85*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::LongArray> long_array = ObjPtr<mirror::LongArray>::DownCast(old);
86*795d594fSAndroid Build Coastguard Worker       index = dchecked_integral_cast<uint32_t>(long_array->Get(2u * i));
87*795d594fSAndroid Build Coastguard Worker       method = reinterpret_cast64<ArtMethod*>(long_array->Get(2u * i + 1u));
88*795d594fSAndroid Build Coastguard Worker     }
89*795d594fSAndroid Build Coastguard Worker     mirror::NativeDexCachePair<ArtMethod> pair(method, index);
90*795d594fSAndroid Build Coastguard Worker     methods->SetNativePair(i, pair);
91*795d594fSAndroid Build Coastguard Worker   }
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker }  // namespace
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker }  // namespace art
97