Merge "Refactor NativeHeapTagging compat feature."

This commit is contained in:
Evgenii Stepanov
2020-02-18 18:47:55 +00:00
committed by Gerrit Code Review
6 changed files with 52 additions and 39 deletions

View File

@@ -19,8 +19,6 @@ package com.android.internal.os;
import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.ApplicationErrorReport;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.type.DefaultMimeMapFactory;
import android.os.Build;
@@ -36,7 +34,6 @@ import android.util.Slog;
import com.android.internal.logging.AndroidConfig;
import com.android.server.NetworkManagementSocketTagger;
import dalvik.annotation.compat.VersionCodes;
import dalvik.system.RuntimeHooks;
import dalvik.system.ThreadPrioritySetter;
import dalvik.system.VMRuntime;
@@ -67,18 +64,8 @@ public class RuntimeInit {
private static volatile boolean mCrashing = false;
/**
* Native heap allocations will now have a non-zero tag in the most significant byte.
* See
* <a href="https://source.android.com/devices/tech/debug/tagged-pointers">https://source.android.com/devices/tech/debug/tagged-pointers</a>.
*/
@ChangeId
@EnabledAfter(targetSdkVersion = VersionCodes.Q)
private static final long NATIVE_HEAP_POINTER_TAGGING = 135754954; // This is a bug id.
private static final native void nativeFinishInit();
private static final native void nativeSetExitWithoutCleanup(boolean exitWithoutCleanup);
private static native void nativeDisableHeapPointerTagging();
private static int Clog_e(String tag, String msg, Throwable tr) {
return Log.printlns(Log.LOG_ID_CRASH, Log.ERROR, tag, msg, tr);
@@ -411,20 +398,6 @@ public class RuntimeInit {
if (DEBUG) Slog.d(TAG, "Leaving RuntimeInit!");
}
private static void maybeDisableHeapPointerTagging(long[] disabledCompatChanges) {
// Heap tagging needs to be disabled before any additional threads are created, but the
// AppCompat framework is not initialized enough at this point.
// Check if the change is enabled manually.
if (disabledCompatChanges != null) {
for (int i = 0; i < disabledCompatChanges.length; i++) {
if (disabledCompatChanges[i] == NATIVE_HEAP_POINTER_TAGGING) {
nativeDisableHeapPointerTagging();
break;
}
}
}
}
protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
String[] argv, ClassLoader classLoader) {
// If the application calls System.exit(), terminate the process
@@ -437,8 +410,6 @@ public class RuntimeInit {
VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges);
maybeDisableHeapPointerTagging(disabledCompatChanges);
final Arguments args = new Arguments(argv);
// The end of of the RuntimeInit event (see #zygoteInit).

View File

@@ -121,6 +121,25 @@ public final class Zygote {
*/
public static final int DISABLE_TEST_API_ENFORCEMENT_POLICY = 1 << 18;
public static final int MEMORY_TAG_LEVEL_MASK = (1 << 19) | (1 << 20);
/**
* Enable pointer tagging in this process.
* Tags are checked during memory deallocation, but not on access.
* TBI stands for Top-Byte-Ignore, an ARM CPU feature.
* {@link https://developer.arm.com/docs/den0024/latest/the-memory-management-unit/translation-table-configuration/virtual-address-tagging}
*/
public static final int MEMORY_TAG_LEVEL_TBI = 1 << 19;
/**
* Enable asynchronous memory tag checks in this process.
*/
public static final int MEMORY_TAG_LEVEL_ASYNC = 2 << 19;
/**
* Enable synchronous memory tag checks in this process.
*/
public static final int MEMORY_TAG_LEVEL_SYNC = 3 << 19;
/** No external storage should be mounted. */
public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE;
/** Default external storage should be mounted. */

View File

@@ -781,6 +781,10 @@ public class ZygoteInit {
Zygote.applyDebuggerSystemProperty(parsedArgs);
Zygote.applyInvokeWithSystemProperty(parsedArgs);
/* Enable pointer tagging in the system server unconditionally. Hardware support for
* this is present in all ARMv8 CPUs; this flag has no effect on other platforms. */
parsedArgs.mRuntimeFlags |= Zygote.MEMORY_TAG_LEVEL_TBI;
if (shouldProfileSystemServer()) {
parsedArgs.mRuntimeFlags |= Zygote.PROFILE_SYSTEM_SERVER;
}

View File

@@ -282,14 +282,6 @@ static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIE
gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
}
static void com_android_internal_os_RuntimeInit_nativeDisableHeapPointerTagging(
JNIEnv* env, jobject clazz) {
HeapTaggingLevel tag_level = M_HEAP_TAGGING_LEVEL_NONE;
if (!android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &tag_level, sizeof(tag_level))) {
ALOGE("ERROR: could not disable heap pointer tagging\n");
}
}
/*
* JNI registration.
*/
@@ -301,8 +293,6 @@ int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
(void*)com_android_internal_os_RuntimeInit_nativeFinishInit},
{"nativeSetExitWithoutCleanup", "(Z)V",
(void*)com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup},
{"nativeDisableHeapPointerTagging", "()V",
(void*)com_android_internal_os_RuntimeInit_nativeDisableHeapPointerTagging},
};
return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
methods, NELEM(methods));

View File

@@ -315,6 +315,8 @@ enum MountExternalKind {
enum RuntimeFlags : uint32_t {
DEBUG_ENABLE_JDWP = 1,
PROFILE_FROM_SHELL = 1 << 15,
MEMORY_TAG_LEVEL_MASK = (1 << 19) | (1 << 20),
MEMORY_TAG_LEVEL_TBI = 1 << 19,
};
enum UnsolicitedZygoteMessageTypes : uint32_t {
@@ -1153,6 +1155,16 @@ static void SpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray gids,
}
}
HeapTaggingLevel heap_tagging_level;
switch (runtime_flags & RuntimeFlags::MEMORY_TAG_LEVEL_MASK) {
case RuntimeFlags::MEMORY_TAG_LEVEL_TBI:
heap_tagging_level = M_HEAP_TAGGING_LEVEL_TBI;
break;
default:
heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
}
android_mallopt(M_SET_HEAP_TAGGING_LEVEL, &heap_tagging_level, sizeof(heap_tagging_level));
if (NeedsNoRandomizeWorkaround()) {
// Work around ARM kernel ASLR lossage (http://b/5817320).
int old_personality = personality(0xffffffff);

View File

@@ -51,6 +51,9 @@ import android.app.ActivityThread;
import android.app.AppGlobals;
import android.app.AppProtoEnums;
import android.app.IApplicationThread;
import android.app.IUidObserver;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -103,6 +106,7 @@ import com.android.server.pm.dex.DexManager;
import com.android.server.wm.ActivityServiceConnectionsHolder;
import com.android.server.wm.WindowManagerService;
import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;
import java.io.File;
@@ -280,6 +284,15 @@ public final class ProcessList {
// lmkd reconnect delay in msecs
private static final long LMKD_RECONNECT_DELAY_MS = 1000;
/**
* Native heap allocations will now have a non-zero tag in the most significant byte.
* @see <a href="https://source.android.com/devices/tech/debug/tagged-pointers">Tagged
* Pointers</a>
*/
@ChangeId
@EnabledAfter(targetSdkVersion = VersionCodes.Q)
private static final long NATIVE_HEAP_POINTER_TAGGING = 135754954; // This is a bug id.
ActivityManagerService mService = null;
// To kill process groups asynchronously
@@ -1653,6 +1666,10 @@ public final class ProcessList {
runtimeFlags |= Zygote.USE_APP_IMAGE_STARTUP_CACHE;
}
if (mPlatformCompat.isChangeEnabled(NATIVE_HEAP_POINTER_TAGGING, app.info)) {
runtimeFlags |= Zygote.MEMORY_TAG_LEVEL_TBI;
}
String invokeWith = null;
if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
// Debuggable apps may include a wrapper script with their library directory.