Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 818789ad authored by John Wu's avatar John Wu Committed by Automerger Merge Worker
Browse files

[Ravenwood] Use native system property implementation am: 5e13e25d

parents def5b9f4 5e13e25d
Loading
Loading
Loading
Loading
+12 −33
Original line number Original line Diff line number Diff line
@@ -21,13 +21,10 @@ import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodRedirect;
import android.ravenwood.annotation.RavenwoodRedirectionClass;
import android.util.Log;
import android.util.Log;
import android.util.MutableInt;
import android.util.MutableInt;


import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.ravenwood.RavenwoodEnvironment;


import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;
import dalvik.annotation.optimization.FastNative;
@@ -40,8 +37,6 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;


/**
/**
 * Gives access to the system properties store.  The system properties
 * Gives access to the system properties store.  The system properties
@@ -58,7 +53,6 @@ import java.util.function.Predicate;
 */
 */
@SystemApi
@SystemApi
@RavenwoodKeepWholeClass
@RavenwoodKeepWholeClass
@RavenwoodRedirectionClass("SystemProperties_host")
public class SystemProperties {
public class SystemProperties {
    private static final String TAG = "SystemProperties";
    private static final String TAG = "SystemProperties";
    private static final boolean TRACK_KEY_ACCESS = false;
    private static final boolean TRACK_KEY_ACCESS = false;
@@ -76,7 +70,7 @@ public class SystemProperties {


    @UnsupportedAppUsage
    @UnsupportedAppUsage
    @GuardedBy("sChangeCallbacks")
    @GuardedBy("sChangeCallbacks")
    static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
    private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();


    @GuardedBy("sRoReads")
    @GuardedBy("sRoReads")
    private static final HashMap<String, MutableInt> sRoReads =
    private static final HashMap<String, MutableInt> sRoReads =
@@ -102,19 +96,6 @@ public class SystemProperties {
        }
        }
    }
    }


    /** @hide */
    @RavenwoodRedirect
    public static void init$ravenwood(Map<String, String> values,
            Predicate<String> keyReadablePredicate, Predicate<String> keyWritablePredicate) {
        throw RavenwoodEnvironment.notSupportedOnDevice();
    }

    /** @hide */
    @RavenwoodRedirect
    public static void reset$ravenwood() {
        throw RavenwoodEnvironment.notSupportedOnDevice();
    }

    // The one-argument version of native_get used to be a regular native function. Nowadays,
    // The one-argument version of native_get used to be a regular native function. Nowadays,
    // we use the two-argument form of native_get all the time, but we can't just delete the
    // we use the two-argument form of native_get all the time, but we can't just delete the
    // one-argument overload: apps use it via reflection, as the UnsupportedAppUsage annotation
    // one-argument overload: apps use it via reflection, as the UnsupportedAppUsage annotation
@@ -126,46 +107,34 @@ public class SystemProperties {


    @FastNative
    @FastNative
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @RavenwoodRedirect
    private static native String native_get(String key, String def);
    private static native String native_get(String key, String def);
    @FastNative
    @FastNative
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @RavenwoodRedirect
    private static native int native_get_int(String key, int def);
    private static native int native_get_int(String key, int def);
    @FastNative
    @FastNative
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    @RavenwoodRedirect
    private static native long native_get_long(String key, long def);
    private static native long native_get_long(String key, long def);
    @FastNative
    @FastNative
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @RavenwoodRedirect
    private static native boolean native_get_boolean(String key, boolean def);
    private static native boolean native_get_boolean(String key, boolean def);


    @FastNative
    @FastNative
    @RavenwoodRedirect
    private static native long native_find(String name);
    private static native long native_find(String name);
    @FastNative
    @FastNative
    @RavenwoodRedirect
    private static native String native_get(long handle);
    private static native String native_get(long handle);
    @CriticalNative
    @CriticalNative
    @RavenwoodRedirect
    private static native int native_get_int(long handle, int def);
    private static native int native_get_int(long handle, int def);
    @CriticalNative
    @CriticalNative
    @RavenwoodRedirect
    private static native long native_get_long(long handle, long def);
    private static native long native_get_long(long handle, long def);
    @CriticalNative
    @CriticalNative
    @RavenwoodRedirect
    private static native boolean native_get_boolean(long handle, boolean def);
    private static native boolean native_get_boolean(long handle, boolean def);


    // _NOT_ FastNative: native_set performs IPC and can block
    // _NOT_ FastNative: native_set performs IPC and can block
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @RavenwoodRedirect
    private static native void native_set(String key, String def);
    private static native void native_set(String key, String def);


    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    @RavenwoodRedirect
    private static native void native_add_change_callback();
    private static native void native_add_change_callback();
    @RavenwoodRedirect
    private static native void native_report_sysprop_change();
    private static native void native_report_sysprop_change();


    /**
    /**
@@ -301,7 +270,7 @@ public class SystemProperties {
    }
    }


    @SuppressWarnings("unused")  // Called from native code.
    @SuppressWarnings("unused")  // Called from native code.
    static void callChangeCallbacks() {
    private static void callChangeCallbacks() {
        ArrayList<Runnable> callbacks = null;
        ArrayList<Runnable> callbacks = null;
        synchronized (sChangeCallbacks) {
        synchronized (sChangeCallbacks) {
            //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
            //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
@@ -326,6 +295,16 @@ public class SystemProperties {
        }
        }
    }
    }


    /**
     * Clear all callback changes.
     * @hide
     */
    public static void clearChangeCallbacksForTest() {
        synchronized (sChangeCallbacks) {
            sChangeCallbacks.clear();
        }
    }

    /**
    /**
     * Notifies listeners that a system property has changed
     * Notifies listeners that a system property has changed
     * @hide
     * @hide
+0 −20
Original line number Original line Diff line number Diff line
@@ -34,8 +34,6 @@


#if defined(__BIONIC__)
#if defined(__BIONIC__)
# include <sys/system_properties.h>
# include <sys/system_properties.h>
#else
struct prop_info;
#endif
#endif


namespace android {
namespace android {
@@ -46,7 +44,6 @@ using android::base::ParseBoolResult;
template<typename Functor>
template<typename Functor>
void ReadProperty(const prop_info* prop, Functor&& functor)
void ReadProperty(const prop_info* prop, Functor&& functor)
{
{
#if defined(__BIONIC__)
    auto thunk = [](void* cookie,
    auto thunk = [](void* cookie,
                    const char* /*name*/,
                    const char* /*name*/,
                    const char* value,
                    const char* value,
@@ -54,9 +51,6 @@ void ReadProperty(const prop_info* prop, Functor&& functor)
        std::forward<Functor>(*static_cast<Functor*>(cookie))(value);
        std::forward<Functor>(*static_cast<Functor*>(cookie))(value);
    };
    };
    __system_property_read_callback(prop, thunk, &functor);
    __system_property_read_callback(prop, thunk, &functor);
#else
    LOG(FATAL) << "fast property access supported only on device";
#endif
}
}


template<typename Functor>
template<typename Functor>
@@ -66,16 +60,11 @@ void ReadProperty(JNIEnv* env, jstring keyJ, Functor&& functor)
    if (!key.c_str()) {
    if (!key.c_str()) {
        return;
        return;
    }
    }
#if defined(__BIONIC__)
    const prop_info* prop = __system_property_find(key.c_str());
    const prop_info* prop = __system_property_find(key.c_str());
    if (!prop) {
    if (!prop) {
        return;
        return;
    }
    }
    ReadProperty(prop, std::forward<Functor>(functor));
    ReadProperty(prop, std::forward<Functor>(functor));
#else
    std::forward<Functor>(functor)(
        android::base::GetProperty(key.c_str(), "").c_str());
#endif
}
}


jstring SystemProperties_getSS(JNIEnv* env, jclass clazz, jstring keyJ,
jstring SystemProperties_getSS(JNIEnv* env, jclass clazz, jstring keyJ,
@@ -132,17 +121,12 @@ jboolean SystemProperties_get_boolean(JNIEnv *env, jclass, jstring keyJ,


jlong SystemProperties_find(JNIEnv* env, jclass, jstring keyJ)
jlong SystemProperties_find(JNIEnv* env, jclass, jstring keyJ)
{
{
#if defined(__BIONIC__)
    ScopedUtfChars key(env, keyJ);
    ScopedUtfChars key(env, keyJ);
    if (!key.c_str()) {
    if (!key.c_str()) {
        return 0;
        return 0;
    }
    }
    const prop_info* prop = __system_property_find(key.c_str());
    const prop_info* prop = __system_property_find(key.c_str());
    return reinterpret_cast<jlong>(prop);
    return reinterpret_cast<jlong>(prop);
#else
    LOG(FATAL) << "fast property access supported only on device";
    __builtin_unreachable();  // Silence warning
#endif
}
}


jstring SystemProperties_getH(JNIEnv* env, jclass clazz, jlong propJ)
jstring SystemProperties_getH(JNIEnv* env, jclass clazz, jlong propJ)
@@ -198,11 +182,7 @@ void SystemProperties_set(JNIEnv *env, jobject clazz, jstring keyJ,
    // request" failures).
    // request" failures).
    errno = 0;
    errno = 0;
    bool success;
    bool success;
#if defined(__BIONIC__)
    success = !__system_property_set(key.c_str(), value_c_str);
    success = !__system_property_set(key.c_str(), value_c_str);
#else
    success = android::base::SetProperty(key.c_str(), value_c_str);
#endif
    if (!success) {
    if (!success) {
        if (errno != 0) {
        if (errno != 0) {
            jniThrowExceptionFmt(env, "java/lang/RuntimeException",
            jniThrowExceptionFmt(env, "java/lang/RuntimeException",
+10 −9
Original line number Original line Diff line number Diff line
@@ -23,11 +23,10 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.fail;


import android.platform.test.ravenwood.RavenwoodRule;
import android.platform.test.ravenwood.RavenwoodConfig;


import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;


import org.junit.Rule;
import org.junit.Test;
import org.junit.Test;


import java.util.Objects;
import java.util.Objects;
@@ -35,17 +34,19 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;


public class SystemPropertiesTest {
public class SystemPropertiesTest {
    @Rule
    private static final String KEY = "sys.testkey";
    public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
    private static final String UNSET_KEY = "Aiw7woh6ie4toh7W";
    private static final String PERSIST_KEY = "persist.sys.testkey";
    private static final String NONEXIST_KEY = "doesnotexist_2341431";

    @RavenwoodConfig.Config
    public static final RavenwoodConfig mRavenwood = new RavenwoodConfig.Builder()
            .setSystemPropertyMutable(KEY, null)
            .setSystemPropertyMutable(KEY, null)
            .setSystemPropertyMutable(UNSET_KEY, null)
            .setSystemPropertyMutable(UNSET_KEY, null)
            .setSystemPropertyMutable(PERSIST_KEY, null)
            .setSystemPropertyMutable(PERSIST_KEY, null)
            .setSystemPropertyImmutable(NONEXIST_KEY, null)
            .build();
            .build();


    private static final String KEY = "sys.testkey";
    private static final String UNSET_KEY = "Aiw7woh6ie4toh7W";
    private static final String PERSIST_KEY = "persist.sys.testkey";

    @Test
    @Test
    @SmallTest
    @SmallTest
    public void testStressPersistPropertyConsistency() throws Exception {
    public void testStressPersistPropertyConsistency() throws Exception {
@@ -117,7 +118,7 @@ public class SystemPropertiesTest {
    @SmallTest
    @SmallTest
    public void testHandle() throws Exception {
    public void testHandle() throws Exception {
        String value;
        String value;
        SystemProperties.Handle handle = SystemProperties.find("doesnotexist_2341431");
        SystemProperties.Handle handle = SystemProperties.find(NONEXIST_KEY);
        assertNull(handle);
        assertNull(handle);
        SystemProperties.set(KEY, "abc");
        SystemProperties.set(KEY, "abc");
        handle = SystemProperties.find(KEY);
        handle = SystemProperties.find(KEY);
+25 −9
Original line number Original line Diff line number Diff line
@@ -267,25 +267,39 @@ filegroup {
    visibility: ["//frameworks/base"],
    visibility: ["//frameworks/base"],
}
}


cc_library_shared {
cc_defaults {
    name: "libravenwood_runtime",
    name: "ravenwood_jni_defaults",
    host_supported: true,

    cflags: [
    cflags: [
        "-Wall",
        "-Wall",
        "-Werror",
        "-Werror",
        "-Wno-unused-parameter",
        "-Wno-unused-parameter",
        "-Wthread-safety",
        "-Wthread-safety",
    ],
    ],
    static_libs: [
        "libnativehelper_jvm",
    ],
    shared_libs: [
        "liblog",
    ],
}

// We need this as a separate library because we need to overload the
// sysprop symbols before libbase is loaded into the process
cc_library_host_shared {
    name: "libravenwood_sysprop",
    defaults: ["ravenwood_jni_defaults"],
    srcs: ["runtime-jni/ravenwood_sysprop.cpp"],
}


cc_library_host_shared {
    name: "libravenwood_runtime",
    defaults: ["ravenwood_jni_defaults"],
    srcs: [
    srcs: [
        "runtime-jni/*.cpp",
        "runtime-jni/ravenwood_runtime.cpp",
        "runtime-jni/ravenwood_os_constants.cpp",
    ],
    ],

    shared_libs: [
    shared_libs: [
        "libbase",
        "libbase",
        "liblog",
        "libnativehelper",
        "libutils",
        "libutils",
        "libcutils",
        "libcutils",
    ],
    ],
@@ -377,8 +391,10 @@ android_ravenwood_libgroup {
        "z00-all-updatable-modules-system-stubs",
        "z00-all-updatable-modules-system-stubs",
    ],
    ],
    jni_libs: [
    jni_libs: [
        "libandroid_runtime",
        // Libraries has to be loaded in the following order
        "libravenwood_sysprop",
        "libravenwood_runtime",
        "libravenwood_runtime",
        "libandroid_runtime",
    ],
    ],
}
}


+1 −0
Original line number Original line Diff line number Diff line
@@ -43,6 +43,7 @@ public final class RavenwoodNativeLoader {
            android.util.Log.class,
            android.util.Log.class,
            android.os.Parcel.class,
            android.os.Parcel.class,
            android.os.Binder.class,
            android.os.Binder.class,
            android.os.SystemProperties.class,
            android.content.res.ApkAssets.class,
            android.content.res.ApkAssets.class,
            android.content.res.AssetManager.class,
            android.content.res.AssetManager.class,
            android.content.res.StringBlock.class,
            android.content.res.StringBlock.class,
Loading