Merge changes I12c9448e,I928175a3 into nyc-dev

* changes:
  Create linker namespace for the system server classloader
  Extract pathclassloader initialization to a separate class
This commit is contained in:
Dimitry Ivanov
2016-04-27 20:32:24 +00:00
committed by Android (Google) Code Review
6 changed files with 98 additions and 28 deletions

View File

@@ -18,6 +18,7 @@ package android.app;
import android.os.Trace; import android.os.Trace;
import android.util.ArrayMap; import android.util.ArrayMap;
import com.android.internal.os.PathClassLoaderFactory;
import dalvik.system.PathClassLoader; import dalvik.system.PathClassLoader;
class ApplicationLoaders { class ApplicationLoaders {
@@ -54,21 +55,16 @@ class ApplicationLoaders {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip); Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, zip);
PathClassLoader pathClassloader = PathClassLoader pathClassloader = PathClassLoaderFactory.createClassLoader(
new PathClassLoader(zip, librarySearchPath, parent); zip,
librarySearchPath,
libraryPermittedPath,
parent,
targetSdkVersion,
isBundled);
String errorMessage = createClassloaderNamespace(pathClassloader,
targetSdkVersion,
librarySearchPath,
libraryPermittedPath,
isBundled);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
if (errorMessage != null) {
throw new UnsatisfiedLinkError("Unable to create namespace for the classloader " +
pathClassloader + ": " + errorMessage);
}
mLoaders.put(zip, pathClassloader); mLoaders.put(zip, pathClassloader);
return pathClassloader; return pathClassloader;
} }
@@ -80,12 +76,6 @@ class ApplicationLoaders {
} }
} }
private static native String createClassloaderNamespace(ClassLoader classLoader,
int targetSdkVersion,
String librarySearchPath,
String libraryPermittedPath,
boolean isShared);
/** /**
* Adds a new path the classpath of the given loader. * Adds a new path the classpath of the given loader.
* @throws IllegalStateException if the provided class loader is not a {@link PathClassLoader}. * @throws IllegalStateException if the provided class loader is not a {@link PathClassLoader}.
@@ -100,6 +90,5 @@ class ApplicationLoaders {
private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>(); private final ArrayMap<String, ClassLoader> mLoaders = new ArrayMap<String, ClassLoader>();
private static final ApplicationLoaders gApplicationLoaders private static final ApplicationLoaders gApplicationLoaders = new ApplicationLoaders();
= new ApplicationLoaders();
} }

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.os;
import dalvik.system.PathClassLoader;
/**
* Creates path class loaders.
*
* @hide
*/
public class PathClassLoaderFactory {
// Unconstructable
private PathClassLoaderFactory() {}
/**
* Create a PathClassLoader and initialize a linker-namespace for it.
*
* @hide
*/
public static PathClassLoader createClassLoader(String dexPath,
String librarySearchPath,
String libraryPermittedPath,
ClassLoader parent,
int targetSdkVersion,
boolean isNamespaceShared) {
PathClassLoader pathClassloader = new PathClassLoader(dexPath, librarySearchPath, parent);
String errorMessage = createClassloaderNamespace(pathClassloader,
targetSdkVersion,
librarySearchPath,
libraryPermittedPath,
isNamespaceShared);
if (errorMessage != null) {
throw new UnsatisfiedLinkError("Unable to create namespace for the classloader " +
pathClassloader + ": " + errorMessage);
}
return pathClassloader;
}
private static native String createClassloaderNamespace(ClassLoader classLoader,
int targetSdkVersion,
String librarySearchPath,
String libraryPermittedPath,
boolean isNamespaceShared);
}

View File

@@ -475,7 +475,9 @@ public class ZygoteInit {
} else { } else {
ClassLoader cl = null; ClassLoader cl = null;
if (systemServerClasspath != null) { if (systemServerClasspath != null) {
cl = new PathClassLoader(systemServerClasspath, ClassLoader.getSystemClassLoader()); cl = createSystemServerClassLoader(systemServerClasspath,
parsedArgs.targetSdkVersion);
Thread.currentThread().setContextClassLoader(cl); Thread.currentThread().setContextClassLoader(cl);
} }
@@ -488,6 +490,23 @@ public class ZygoteInit {
/* should never reach here */ /* should never reach here */
} }
/**
* Creates a PathClassLoader for the system server. It also creates
* a shared namespace associated with the classloader to let it access
* platform-private native libraries.
*/
private static PathClassLoader createSystemServerClassLoader(String systemServerClasspath,
int targetSdkVersion) {
String librarySearchPath = System.getProperty("java.library.path");
return PathClassLoaderFactory.createClassLoader(systemServerClasspath,
librarySearchPath,
null /* libraryPermittedPath */,
ClassLoader.getSystemClassLoader(),
targetSdkVersion,
true /* isNamespaceShared */);
}
/** /**
* Performs dex-opt on the elements of {@code classPath}, if needed. We * Performs dex-opt on the elements of {@code classPath}, if needed. We
* choose the instruction set of the current runtime. * choose the instruction set of the current runtime.

View File

@@ -34,7 +34,6 @@ LOCAL_SRC_FILES:= \
com_google_android_gles_jni_EGLImpl.cpp \ com_google_android_gles_jni_EGLImpl.cpp \
com_google_android_gles_jni_GLImpl.cpp.arm \ com_google_android_gles_jni_GLImpl.cpp.arm \
android_app_Activity.cpp \ android_app_Activity.cpp \
android_app_ApplicationLoaders.cpp \
android_app_NativeActivity.cpp \ android_app_NativeActivity.cpp \
android_app_admin_SecurityLog.cpp \ android_app_admin_SecurityLog.cpp \
android_opengl_EGL14.cpp \ android_opengl_EGL14.cpp \
@@ -174,6 +173,7 @@ LOCAL_SRC_FILES:= \
android_content_res_Configuration.cpp \ android_content_res_Configuration.cpp \
android_animation_PropertyValuesHolder.cpp \ android_animation_PropertyValuesHolder.cpp \
com_android_internal_net_NetworkStatsFactory.cpp \ com_android_internal_net_NetworkStatsFactory.cpp \
com_android_internal_os_PathClassLoaderFactory.cpp \
com_android_internal_os_Zygote.cpp \ com_android_internal_os_Zygote.cpp \
com_android_internal_util_VirtualRefBasePtr.cpp \ com_android_internal_util_VirtualRefBasePtr.cpp \
com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp com_android_internal_view_animation_NativeInterpolatorFactoryHelper.cpp

View File

@@ -179,7 +179,6 @@ extern int register_android_backup_BackupDataOutput(JNIEnv *env);
extern int register_android_backup_FileBackupHelperBase(JNIEnv *env); extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env); extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
extern int register_android_app_backup_FullBackup(JNIEnv *env); extern int register_android_app_backup_FullBackup(JNIEnv *env);
extern int register_android_app_ApplicationLoaders(JNIEnv* env);
extern int register_android_app_Activity(JNIEnv *env); extern int register_android_app_Activity(JNIEnv *env);
extern int register_android_app_ActivityThread(JNIEnv *env); extern int register_android_app_ActivityThread(JNIEnv *env);
extern int register_android_app_NativeActivity(JNIEnv *env); extern int register_android_app_NativeActivity(JNIEnv *env);
@@ -200,6 +199,7 @@ extern int register_android_content_res_Configuration(JNIEnv* env);
extern int register_android_animation_PropertyValuesHolder(JNIEnv *env); extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env); extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env); extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
extern int register_com_android_internal_os_PathClassLoaderFactory(JNIEnv* env);
extern int register_com_android_internal_os_Zygote(JNIEnv *env); extern int register_com_android_internal_os_Zygote(JNIEnv *env);
extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env); extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
@@ -1355,6 +1355,7 @@ static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_net_NetworkUtils), REG_JNI(register_android_net_NetworkUtils),
REG_JNI(register_android_net_TrafficStats), REG_JNI(register_android_net_TrafficStats),
REG_JNI(register_android_os_MemoryFile), REG_JNI(register_android_os_MemoryFile),
REG_JNI(register_com_android_internal_os_PathClassLoaderFactory),
REG_JNI(register_com_android_internal_os_Zygote), REG_JNI(register_com_android_internal_os_Zygote),
REG_JNI(register_com_android_internal_util_VirtualRefBasePtr), REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
REG_JNI(register_android_hardware_Camera), REG_JNI(register_android_hardware_Camera),
@@ -1386,7 +1387,6 @@ static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_backup_FileBackupHelperBase), REG_JNI(register_android_backup_FileBackupHelperBase),
REG_JNI(register_android_backup_BackupHelperDispatcher), REG_JNI(register_android_backup_BackupHelperDispatcher),
REG_JNI(register_android_app_backup_FullBackup), REG_JNI(register_android_app_backup_FullBackup),
REG_JNI(register_android_app_ApplicationLoaders),
REG_JNI(register_android_app_Activity), REG_JNI(register_android_app_Activity),
REG_JNI(register_android_app_ActivityThread), REG_JNI(register_android_app_ActivityThread),
REG_JNI(register_android_app_NativeActivity), REG_JNI(register_android_app_NativeActivity),

View File

@@ -39,13 +39,13 @@ static const JNINativeMethod g_methods[] = {
reinterpret_cast<void*>(createClassloaderNamespace_native) }, reinterpret_cast<void*>(createClassloaderNamespace_native) },
}; };
static const char* const kApplicationLoadersPathName = "android/app/ApplicationLoaders"; static const char* const kPathClassLoaderFactoryPathName = "com/android/internal/os/PathClassLoaderFactory";
namespace android namespace android
{ {
int register_android_app_ApplicationLoaders(JNIEnv* env) { int register_com_android_internal_os_PathClassLoaderFactory(JNIEnv* env) {
return RegisterMethodsOrDie(env, kApplicationLoadersPathName, g_methods, NELEM(g_methods)); return RegisterMethodsOrDie(env, kPathClassLoaderFactoryPathName, g_methods, NELEM(g_methods));
} }
} // namespace android } // namespace android