xref: /aosp_15_r20/art/test/596-app-images/app_images.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright 2016 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 <pthread.h>
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <cstdio>
20*795d594fSAndroid Build Coastguard Worker #include <iostream>
21*795d594fSAndroid Build Coastguard Worker #include <vector>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "jni.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
26*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
27*795d594fSAndroid Build Coastguard Worker #include "gc/space/space-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "mirror/class.h"
29*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_utf_chars.h"
30*795d594fSAndroid Build Coastguard Worker #include "oat/image.h"
31*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file.h"
32*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
33*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker namespace art {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace {
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker // Returns whether the extensionless basename of `location` is equal to name.
40*795d594fSAndroid Build Coastguard Worker // E.g. check_name("/foo/bar/baz.odex", "baz") == true,
41*795d594fSAndroid Build Coastguard Worker //      check_name("/foo/bar/baz.odex", "bar") == false
check_name(const std::string & location,const std::string & name)42*795d594fSAndroid Build Coastguard Worker static bool check_name(const std::string& location, const std::string& name) {
43*795d594fSAndroid Build Coastguard Worker   std::string loc_name = location;
44*795d594fSAndroid Build Coastguard Worker   size_t idx = loc_name.rfind('/');
45*795d594fSAndroid Build Coastguard Worker   if (idx != std::string::npos) {
46*795d594fSAndroid Build Coastguard Worker     loc_name = loc_name.substr(idx + 1);
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker   idx = loc_name.rfind('.');
49*795d594fSAndroid Build Coastguard Worker   if (idx != std::string::npos) {
50*795d594fSAndroid Build Coastguard Worker     loc_name = loc_name.substr(0, idx);
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker   return loc_name == name;
53*795d594fSAndroid Build Coastguard Worker }
54*795d594fSAndroid Build Coastguard Worker 
Java_Main_checkAppImageLoaded(JNIEnv * env,jclass,jstring jimage_name)55*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkAppImageLoaded(JNIEnv* env,
56*795d594fSAndroid Build Coastguard Worker                                                                     jclass,
57*795d594fSAndroid Build Coastguard Worker                                                                     jstring jimage_name) {
58*795d594fSAndroid Build Coastguard Worker   ScopedUtfChars image_name(env, jimage_name);
59*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
60*795d594fSAndroid Build Coastguard Worker   for (auto* space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
61*795d594fSAndroid Build Coastguard Worker     if (space->IsImageSpace()) {
62*795d594fSAndroid Build Coastguard Worker       auto* image_space = space->AsImageSpace();
63*795d594fSAndroid Build Coastguard Worker       const auto& image_header = image_space->GetImageHeader();
64*795d594fSAndroid Build Coastguard Worker       // Check that this is an app image associated with the dex file named
65*795d594fSAndroid Build Coastguard Worker       // `jname` by verifying the extensionless basename of the odex file
66*795d594fSAndroid Build Coastguard Worker       // location is equal to `jname`.
67*795d594fSAndroid Build Coastguard Worker       if (image_header.IsAppImage() &&
68*795d594fSAndroid Build Coastguard Worker           check_name(image_space->GetOatFile()->GetLocation(), image_name.c_str())) {
69*795d594fSAndroid Build Coastguard Worker         return JNI_TRUE;
70*795d594fSAndroid Build Coastguard Worker       }
71*795d594fSAndroid Build Coastguard Worker     }
72*795d594fSAndroid Build Coastguard Worker   }
73*795d594fSAndroid Build Coastguard Worker   return JNI_FALSE;
74*795d594fSAndroid Build Coastguard Worker }
75*795d594fSAndroid Build Coastguard Worker 
Java_Main_checkAppImageContains(JNIEnv *,jclass,jclass c)76*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkAppImageContains(JNIEnv*, jclass, jclass c) {
77*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
78*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass_ptr = soa.Decode<mirror::Class>(c);
79*795d594fSAndroid Build Coastguard Worker   for (auto* space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
80*795d594fSAndroid Build Coastguard Worker     if (space->IsImageSpace()) {
81*795d594fSAndroid Build Coastguard Worker       auto* image_space = space->AsImageSpace();
82*795d594fSAndroid Build Coastguard Worker       const auto& image_header = image_space->GetImageHeader();
83*795d594fSAndroid Build Coastguard Worker       if (image_header.IsAppImage()) {
84*795d594fSAndroid Build Coastguard Worker         if (image_space->HasAddress(klass_ptr.Ptr())) {
85*795d594fSAndroid Build Coastguard Worker           return JNI_TRUE;
86*795d594fSAndroid Build Coastguard Worker         }
87*795d594fSAndroid Build Coastguard Worker       }
88*795d594fSAndroid Build Coastguard Worker     }
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker   return JNI_FALSE;
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker 
Java_Main_checkInitialized(JNIEnv *,jclass,jclass c)93*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkInitialized(JNIEnv*, jclass, jclass c) {
94*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
95*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass_ptr = soa.Decode<mirror::Class>(c);
96*795d594fSAndroid Build Coastguard Worker   return klass_ptr->IsInitialized();
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker }  // namespace
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker }  // namespace art
102