summaryrefslogtreecommitdiff
path: root/ravenwood/runtime-helper-src
diff options
context:
space:
mode:
author John Wu <topjohnwu@google.com> 2024-09-25 00:43:28 +0000
committer John Wu <topjohnwu@google.com> 2024-09-26 22:59:51 +0000
commit5e13e25d51685f4cf1ae4c9188ae19577538c6f7 (patch)
treebc8bf33dad91ab85b2dbe311a1dedea7542ac4f6 /ravenwood/runtime-helper-src
parent983461633b96db0bc58205a657edeffad3ce4080 (diff)
[Ravenwood] Use native system property implementation
System properties are not only used by Java code, but also native code. To make the property values consistent across Java and native code, make the "source of truth" of property values on the native side. In order to achieve this, we have to introduce a new native library "libravenwood_sysprops" that does NOT link against libbase, and load that library first. By doing so, we can override the low-level sysprop function symbols with our own implementation. Once that is done, all existing native code accessing system properties, regardless whether they use the libbase/libcutils wrappers or the raw sysprop functions will go through Ravenwood's implementation. Other than improving system properties, this provides the infrastructure to override/implement C functions that is used in native code. Bug: 292141694 Flag: EXEMPT host test change only Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh Merged-in: I14678e2ac52ace0b23bd53df7b6092a1cbb881b3 Change-Id: I14678e2ac52ace0b23bd53df7b6092a1cbb881b3
Diffstat (limited to 'ravenwood/runtime-helper-src')
-rw-r--r--ravenwood/runtime-helper-src/framework/android/os/SystemProperties_host.java209
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/android/system/OsConstants.java4
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java14
3 files changed, 8 insertions, 219 deletions
diff --git a/ravenwood/runtime-helper-src/framework/android/os/SystemProperties_host.java b/ravenwood/runtime-helper-src/framework/android/os/SystemProperties_host.java
deleted file mode 100644
index b09bf3119cfa..000000000000
--- a/ravenwood/runtime-helper-src/framework/android/os/SystemProperties_host.java
+++ /dev/null
@@ -1,209 +0,0 @@
-/*
- * Copyright (C) 2023 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 android.os;
-
-import android.util.SparseArray;
-
-import com.android.internal.annotations.GuardedBy;
-import com.android.internal.util.Preconditions;
-
-import java.util.Map;
-import java.util.Objects;
-import java.util.function.Predicate;
-
-public class SystemProperties_host {
- private static final Object sLock = new Object();
-
- /** Active system property values */
- @GuardedBy("sLock")
- private static Map<String, String> sValues;
- /** Predicate tested to determine if a given key can be read. */
- @GuardedBy("sLock")
- private static Predicate<String> sKeyReadablePredicate;
- /** Predicate tested to determine if a given key can be written. */
- @GuardedBy("sLock")
- private static Predicate<String> sKeyWritablePredicate;
-
- /**
- * Reverse mapping that provides a way back to an original key from the
- * {@link System#identityHashCode(Object)} of {@link String#intern}.
- */
- @GuardedBy("sLock")
- private static SparseArray<String> sKeyHandles = new SparseArray<>();
-
- /**
- * Basically the same as {@link #init$ravenwood}, but it'll only run if no values are
- * set yet.
- */
- public static void initializeIfNeeded(Map<String, String> values,
- Predicate<String> keyReadablePredicate, Predicate<String> keyWritablePredicate) {
- synchronized (sLock) {
- if (sValues != null) {
- return; // Already initialized.
- }
- init$ravenwood(values, keyReadablePredicate, keyWritablePredicate);
- }
- }
-
- public static void init$ravenwood(Map<String, String> values,
- Predicate<String> keyReadablePredicate, Predicate<String> keyWritablePredicate) {
- synchronized (sLock) {
- sValues = Objects.requireNonNull(values);
- sKeyReadablePredicate = Objects.requireNonNull(keyReadablePredicate);
- sKeyWritablePredicate = Objects.requireNonNull(keyWritablePredicate);
- sKeyHandles.clear();
- synchronized (SystemProperties.sChangeCallbacks) {
- SystemProperties.sChangeCallbacks.clear();
- }
- }
- }
-
- public static void reset$ravenwood() {
- synchronized (sLock) {
- sValues = null;
- sKeyReadablePredicate = null;
- sKeyWritablePredicate = null;
- sKeyHandles.clear();
- synchronized (SystemProperties.sChangeCallbacks) {
- SystemProperties.sChangeCallbacks.clear();
- }
- }
- }
-
- public static void native_set(String key, String val) {
- synchronized (sLock) {
- Objects.requireNonNull(key);
- Preconditions.requireNonNullViaRavenwoodRule(sValues);
- if (!sKeyWritablePredicate.test(key)) {
- throw new IllegalArgumentException(
- "Write access to system property '" + key + "' denied via RavenwoodRule");
- }
- if (key.startsWith("ro.") && sValues.containsKey(key)) {
- throw new IllegalArgumentException(
- "System property '" + key + "' already defined once; cannot redefine");
- }
- if ((val == null) || val.isEmpty()) {
- sValues.remove(key);
- } else {
- sValues.put(key, val);
- }
- SystemProperties.callChangeCallbacks();
- }
- }
-
- public static String native_get(String key, String def) {
- synchronized (sLock) {
- Objects.requireNonNull(key);
- Preconditions.requireNonNullViaRavenwoodRule(sValues);
- if (!sKeyReadablePredicate.test(key)) {
- throw new IllegalArgumentException(
- "Read access to system property '" + key + "' denied via RavenwoodRule");
- }
- return sValues.getOrDefault(key, def);
- }
- }
-
- public static int native_get_int(String key, int def) {
- try {
- return Integer.parseInt(native_get(key, ""));
- } catch (NumberFormatException ignored) {
- return def;
- }
- }
-
- public static long native_get_long(String key, long def) {
- try {
- return Long.parseLong(native_get(key, ""));
- } catch (NumberFormatException ignored) {
- return def;
- }
- }
-
- public static boolean native_get_boolean(String key, boolean def) {
- return parseBoolean(native_get(key, ""), def);
- }
-
- public static long native_find(String name) {
- synchronized (sLock) {
- Preconditions.requireNonNullViaRavenwoodRule(sValues);
- if (sValues.containsKey(name)) {
- name = name.intern();
- final int handle = System.identityHashCode(name);
- sKeyHandles.put(handle, name);
- return handle;
- } else {
- return 0;
- }
- }
- }
-
- public static String native_get(long handle) {
- synchronized (sLock) {
- return native_get(sKeyHandles.get((int) handle), "");
- }
- }
-
- public static int native_get_int(long handle, int def) {
- synchronized (sLock) {
- return native_get_int(sKeyHandles.get((int) handle), def);
- }
- }
-
- public static long native_get_long(long handle, long def) {
- synchronized (sLock) {
- return native_get_long(sKeyHandles.get((int) handle), def);
- }
- }
-
- public static boolean native_get_boolean(long handle, boolean def) {
- synchronized (sLock) {
- return native_get_boolean(sKeyHandles.get((int) handle), def);
- }
- }
-
- public static void native_add_change_callback() {
- // Ignored; callback always registered via init above
- }
-
- public static void native_report_sysprop_change() {
- // Report through callback always registered via init above
- synchronized (sLock) {
- Preconditions.requireNonNullViaRavenwoodRule(sValues);
- SystemProperties.callChangeCallbacks();
- }
- }
-
- private static boolean parseBoolean(String val, boolean def) {
- // Matches system/libbase/include/android-base/parsebool.h
- if (val == null) return def;
- switch (val) {
- case "1":
- case "on":
- case "true":
- case "y":
- case "yes":
- return true;
- case "0":
- case "false":
- case "n":
- case "no":
- case "off":
- return false;
- default:
- return def;
- }
- }
-}
diff --git a/ravenwood/runtime-helper-src/libcore-fake/android/system/OsConstants.java b/ravenwood/runtime-helper-src/libcore-fake/android/system/OsConstants.java
index c56ec8a60f00..3fedc1a450b9 100644
--- a/ravenwood/runtime-helper-src/libcore-fake/android/system/OsConstants.java
+++ b/ravenwood/runtime-helper-src/libcore-fake/android/system/OsConstants.java
@@ -15,8 +15,6 @@
*/
package android.system;
-import com.android.ravenwood.common.RavenwoodCommonUtils;
-
/**
* Copied from libcore's version, with the local changes:
* - All the imports are removed. (they're only used in javadoc)
@@ -1252,8 +1250,6 @@ public class OsConstants {
private static int placeholder() { return 0; }
// ...because we want to initialize them at runtime.
static {
- // [ravenwood-change] Load the JNI lib.
- RavenwoodCommonUtils.loadRavenwoodNativeRuntime();
Native.initConstants();
}
}
diff --git a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java
index ad80d92686ab..f13189f6f8be 100644
--- a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java
+++ b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java
@@ -19,7 +19,6 @@ import android.system.ErrnoException;
import android.system.StructStat;
import com.android.ravenwood.common.JvmWorkaround;
-import com.android.ravenwood.common.RavenwoodCommonUtils;
import java.io.FileDescriptor;
@@ -30,11 +29,6 @@ public class RavenwoodRuntimeNative {
private RavenwoodRuntimeNative() {
}
- static {
- RavenwoodCommonUtils.ensureOnRavenwood();
- RavenwoodCommonUtils.loadRavenwoodNativeRuntime();
- }
-
public static native void applyFreeFunction(long freeFunction, long nativePtr);
private static native long nLseek(int fd, long offset, int whence) throws ErrnoException;
@@ -56,6 +50,14 @@ public class RavenwoodRuntimeNative {
public static native void setenv(String name, String value, boolean overwrite)
throws ErrnoException;
+ public static native void reloadNativeLibrary(String libFile);
+
+ public static native String getSystemProperty(String key);
+
+ public static native boolean setSystemProperty(String key, String value);
+
+ public static native void clearSystemProperties();
+
public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException {
return nLseek(JvmWorkaround.getInstance().getFdInt(fd), offset, whence);
}