Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_DexCache.h" |
| 18 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 19 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 20 | #include "dex_file_types.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "mirror/class-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 23 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 24 | #include "mirror/object-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_fast_native_object_access-inl.h" |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 26 | #include "well_known_classes.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static jobject DexCache_getDexNative(JNIEnv* env, jobject javaDexCache) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 31 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 32 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 33 | // Should only be called while holding the lock on the dex cache. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 34 | DCHECK_EQ(dex_cache->GetLockOwnerThreadId(), soa.Self()->GetThreadId()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 35 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 36 | if (dex_file == nullptr) { |
| 37 | return nullptr; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 38 | } |
| 39 | void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin())); |
| 40 | jobject byte_buffer = env->NewDirectByteBuffer(address, dex_file->Size()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | if (byte_buffer == nullptr) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 42 | DCHECK(soa.Self()->IsExceptionPending()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 43 | return nullptr; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | jvalue args[1]; |
| 47 | args[0].l = byte_buffer; |
| 48 | return env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex, |
| 49 | WellKnownClasses::com_android_dex_Dex_create, |
| 50 | args); |
| 51 | } |
| 52 | |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 53 | static jobject DexCache_getResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index) { |
| 54 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 56 | CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 57 | return soa.AddLocalReference<jobject>(dex_cache->GetResolvedType(dex::TypeIndex(type_index))); |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static jobject DexCache_getResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index) { |
| 61 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 62 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 63 | CHECK_LT(static_cast<size_t>(string_index), dex_cache->GetDexFile()->NumStringIds()); |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 64 | return soa.AddLocalReference<jobject>(dex_cache->GetResolvedString(string_index)); |
| 65 | } |
| 66 | |
| 67 | static void DexCache_setResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index, |
| 68 | jobject type) { |
| 69 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 70 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 71 | CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame^] | 72 | dex_cache->SetResolvedType(dex::TypeIndex(type_index), soa.Decode<mirror::Class>(type)); |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void DexCache_setResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index, |
| 76 | jobject string) { |
| 77 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 78 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 79 | CHECK_LT(static_cast<size_t>(string_index), dex_cache->GetDexFile()->NumStringIds()); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 80 | dex_cache->SetResolvedString(string_index, soa.Decode<mirror::String>(string)); |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 83 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 84 | NATIVE_METHOD(DexCache, getDexNative, "!()Lcom/android/dex/Dex;"), |
Vladimir Marko | c136312 | 2015-04-09 14:13:13 +0100 | [diff] [blame] | 85 | NATIVE_METHOD(DexCache, getResolvedType, "!(I)Ljava/lang/Class;"), |
| 86 | NATIVE_METHOD(DexCache, getResolvedString, "!(I)Ljava/lang/String;"), |
| 87 | NATIVE_METHOD(DexCache, setResolvedType, "!(ILjava/lang/Class;)V"), |
| 88 | NATIVE_METHOD(DexCache, setResolvedString, "!(ILjava/lang/String;)V"), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | void register_java_lang_DexCache(JNIEnv* env) { |
| 92 | REGISTER_NATIVE_METHODS("java/lang/DexCache"); |
| 93 | } |
| 94 | |
| 95 | } // namespace art |