blob: 1bc2c1ef35f61df471e020ed7d280db0f1330344 [file] [log] [blame]
Alex Light2e960a02016-05-03 15:01:06 -07001/*
2 * Copyright 2016 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
Alex Light2e960a02016-05-03 15:01:06 -070017#include <pthread.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
20#include <iostream>
Alex Light2e960a02016-05-03 15:01:06 -070021#include <vector>
22
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "jni.h"
24
Alex Light2e960a02016-05-03 15:01:06 -070025#include "gc/heap.h"
26#include "gc/space/image_space.h"
27#include "gc/space/space-inl.h"
28#include "image.h"
Alex Light2e960a02016-05-03 15:01:06 -070029#include "mirror/class.h"
Andreas Gampe038a1982020-03-11 23:06:42 +000030#include "nativehelper/scoped_utf_chars.h"
31#include "oat_file.h"
Alex Light2e960a02016-05-03 15:01:06 -070032#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.h"
Alex Light2e960a02016-05-03 15:01:06 -070034
35namespace art {
36
37namespace {
38
Andreas Gampe038a1982020-03-11 23:06:42 +000039// Returns whether the extensionless basename of `location` is equal to name.
40// E.g. check_name("/foo/bar/baz.odex", "baz") == true,
41// check_name("/foo/bar/baz.odex", "bar") == false
42static bool check_name(const std::string& location, const std::string& name) {
43 std::string loc_name = location;
44 size_t idx = loc_name.rfind('/');
45 if (idx != std::string::npos) {
46 loc_name = loc_name.substr(idx + 1);
47 }
48 idx = loc_name.rfind('.');
49 if (idx != std::string::npos) {
50 loc_name = loc_name.substr(0, idx);
51 }
52 return loc_name == name;
53}
54
55extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkAppImageLoaded(JNIEnv* env,
56 jclass,
57 jstring jimage_name) {
58 ScopedUtfChars image_name(env, jimage_name);
Alex Light2e960a02016-05-03 15:01:06 -070059 ScopedObjectAccess soa(Thread::Current());
60 for (auto* space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
61 if (space->IsImageSpace()) {
62 auto* image_space = space->AsImageSpace();
63 const auto& image_header = image_space->GetImageHeader();
Andreas Gampe038a1982020-03-11 23:06:42 +000064 // Check that this is an app image associated with the dex file named
65 // `jname` by verifying the extensionless basename of the odex file
66 // location is equal to `jname`.
67 if (image_header.IsAppImage() &&
68 check_name(image_space->GetOatFile()->GetLocation(), image_name.c_str())) {
Alex Light2e960a02016-05-03 15:01:06 -070069 return JNI_TRUE;
70 }
71 }
72 }
73 return JNI_FALSE;
74}
75
76extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkAppImageContains(JNIEnv*, jclass, jclass c) {
77 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0795f232016-09-27 18:43:30 -070078 ObjPtr<mirror::Class> klass_ptr = soa.Decode<mirror::Class>(c);
Alex Light2e960a02016-05-03 15:01:06 -070079 for (auto* space : Runtime::Current()->GetHeap()->GetContinuousSpaces()) {
80 if (space->IsImageSpace()) {
81 auto* image_space = space->AsImageSpace();
82 const auto& image_header = image_space->GetImageHeader();
83 if (image_header.IsAppImage()) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070084 if (image_space->HasAddress(klass_ptr.Ptr())) {
Alex Light2e960a02016-05-03 15:01:06 -070085 return JNI_TRUE;
86 }
87 }
88 }
89 }
90 return JNI_FALSE;
91}
92
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +000093extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkInitialized(JNIEnv*, jclass, jclass c) {
94 ScopedObjectAccess soa(Thread::Current());
95 ObjPtr<mirror::Class> klass_ptr = soa.Decode<mirror::Class>(c);
96 return klass_ptr->IsInitialized();
97}
98
Alex Light2e960a02016-05-03 15:01:06 -070099} // namespace
100
101} // namespace art