• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 "dalvik_system_DexFile.h"
18 
19 #include <memory>
20 
21 #include "class_loader_context.h"
22 #include "class_root-inl.h"
23 #include "mirror/object_array-alloc-inl.h"
24 #include "native_util.h"
25 #include "nativehelper/jni_macros.h"
26 #include "thread-inl.h"
27 
28 namespace art {
29 
append_string(Thread * self,Handle<mirror::ObjectArray<mirror::String>> array,uint32_t & i,const std::string & string)30 static bool append_string(Thread* self,
31                           Handle<mirror::ObjectArray<mirror::String>> array,
32                           uint32_t& i,
33                           const std::string& string) REQUIRES_SHARED(Locks::mutator_lock_) {
34   ObjPtr<mirror::String> ostring = mirror::String::AllocFromModifiedUtf8(self, string.c_str());
35   if (ostring == nullptr) {
36     DCHECK(self->IsExceptionPending());
37     return false;
38   }
39   // We're initializing a newly allocated array object, so we do not need to record that under
40   // a transaction. If the transaction is aborted, the whole object shall be unreachable.
41   array->SetWithoutChecks</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(i, ostring);
42   ++i;
43   return true;
44 }
45 
BaseDexClassLoader_computeClassLoaderContextsNative(JNIEnv * env,jobject class_loader)46 static jobjectArray BaseDexClassLoader_computeClassLoaderContextsNative(JNIEnv* env,
47                                                                         jobject class_loader) {
48   CHECK(class_loader != nullptr);
49   std::map<std::string, std::string> context_map =
50       ClassLoaderContext::EncodeClassPathContextsForClassLoader(class_loader);
51   Thread* self = Thread::ForEnv(env);
52   ScopedObjectAccess soa(self);
53   StackHandleScope<1u> hs(self);
54   Handle<mirror::ObjectArray<mirror::String>> array = hs.NewHandle(
55       mirror::ObjectArray<mirror::String>::Alloc(
56           self, GetClassRoot<mirror::ObjectArray<mirror::String>>(), 2 * context_map.size()));
57   if (array == nullptr) {
58     DCHECK(self->IsExceptionPending());
59     return nullptr;
60   }
61   uint32_t i = 0;
62   for (const auto& classpath_to_context : context_map) {
63     const std::string& classpath = classpath_to_context.first;
64     const std::string& context = classpath_to_context.second;
65     if (!append_string(self, array, i, classpath) || !append_string(self, array, i, context)) {
66       return nullptr;
67     }
68   }
69   return soa.AddLocalReference<jobjectArray>(array.Get());
70 }
71 
72 static JNINativeMethod gMethods[] = {
73   NATIVE_METHOD(BaseDexClassLoader, computeClassLoaderContextsNative,
74                 "()[Ljava/lang/String;"),
75 };
76 
register_dalvik_system_BaseDexClassLoader(JNIEnv * env)77 void register_dalvik_system_BaseDexClassLoader(JNIEnv* env) {
78   REGISTER_NATIVE_METHODS("dalvik/system/BaseDexClassLoader");
79 }
80 
81 }  // namespace art
82