summaryrefslogtreecommitdiff
path: root/ravenwood/runtime-helper-src
diff options
context:
space:
mode:
author John Wu <topjohnwu@google.com> 2025-02-24 22:46:54 -0800
committer John Wu <topjohnwu@google.com> 2025-02-25 00:39:31 -0800
commitc1d81ac35e0f136bfd34759ddc36046e754970bb (patch)
tree3df55e7376b719b9f20f1457352aaadbd071abf3 /ravenwood/runtime-helper-src
parent4cc6f84a25884109d2e1488e7170afa28fe0015d (diff)
[Ravenwood] Sync internal main to AOSP
Bug: 354011961 Test: f/b/r/scripts/run-ravenwood-tests.sh Change-Id: Iaad01f6f69e96763bda113639bd286d3f186b186 Merged-In: I90a79a92987870d494ed927eb6ba02747e247929
Diffstat (limited to 'ravenwood/runtime-helper-src')
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodJdkPatch.java19
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodRuntimeNative.java2
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/dalvik/system/CloseGuard.java198
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/libcore/io/IoBridge.java63
-rw-r--r--ravenwood/runtime-helper-src/libcore-fake/libcore/util/NativeAllocationRegistry.java63
5 files changed, 340 insertions, 5 deletions
diff --git a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodJdkPatch.java b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodJdkPatch.java
index 96aed4b3401d..d5a96ddc3a98 100644
--- a/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodJdkPatch.java
+++ b/ravenwood/runtime-helper-src/libcore-fake/com/android/ravenwood/RavenwoodJdkPatch.java
@@ -20,6 +20,7 @@ import com.android.ravenwood.common.JvmWorkaround;
import java.io.FileDescriptor;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.regex.Pattern;
/**
* Class to host APIs that exist in libcore, but not in standard JRE.
@@ -46,4 +47,22 @@ public class RavenwoodJdkPatch {
final var it = map.entrySet().iterator();
return it.hasNext() ? it.next() : null;
}
+
+ /**
+ * Implements Pattern.compile(String)
+ *
+ * ART always assumes UNICODE_CHARACTER_CLASS is set.
+ */
+ public static Pattern compilePattern(String regex) {
+ return Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
+ }
+
+ /**
+ * Implements Pattern.compile(String, int)
+ *
+ * ART always assumes UNICODE_CHARACTER_CLASS is set.
+ */
+ public static Pattern compilePattern(String regex, int flag) {
+ return Pattern.compile(regex, flag | Pattern.UNICODE_CHARACTER_CLASS);
+ }
}
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 acbcdf1926db..0d82a8691881 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
@@ -66,6 +66,8 @@ public class RavenwoodRuntimeNative {
public static native int gettid();
+ public static native String getIcuDataName();
+
public static long lseek(FileDescriptor fd, long offset, int whence) throws ErrnoException {
return nLseek(JvmWorkaround.getInstance().getFdInt(fd), offset, whence);
}
diff --git a/ravenwood/runtime-helper-src/libcore-fake/dalvik/system/CloseGuard.java b/ravenwood/runtime-helper-src/libcore-fake/dalvik/system/CloseGuard.java
new file mode 100644
index 000000000000..82bab64f22f3
--- /dev/null
+++ b/ravenwood/runtime-helper-src/libcore-fake/dalvik/system/CloseGuard.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2010 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 dalvik.system;
+
+/**
+ * A no-op copy of libcore/dalvik/src/main/java/dalvik/system/CloseGuard.java
+ */
+public final class CloseGuard {
+
+ /**
+ * Returns a CloseGuard instance. {@code #open(String)} can be used to set
+ * up the instance to warn on failure to close.
+ *
+ * @return {@link CloseGuard} instance.
+ *
+ * @hide
+ */
+ public static CloseGuard get() {
+ return new CloseGuard();
+ }
+
+ /**
+ * Enables/disables stack capture and tracking. A call stack is captured
+ * during open(), and open/close events are reported to the Tracker, only
+ * if enabled is true. If a stack trace was captured, the {@link
+ * #getReporter() reporter} is informed of unclosed resources; otherwise a
+ * one-line warning is logged.
+ *
+ * @param enabled whether stack capture and tracking is enabled.
+ *
+ * @hide
+ */
+ public static void setEnabled(boolean enabled) {
+ }
+
+ /**
+ * True if CloseGuard stack capture and tracking are enabled.
+ *
+ * @hide
+ */
+ public static boolean isEnabled() {
+ return false;
+ }
+
+ /**
+ * Used to replace default Reporter used to warn of CloseGuard
+ * violations when stack tracking is enabled. Must be non-null.
+ *
+ * @param rep replacement for default Reporter.
+ *
+ * @hide
+ */
+ public static void setReporter(Reporter rep) {
+ if (rep == null) {
+ throw new NullPointerException("reporter == null");
+ }
+ }
+
+ /**
+ * Returns non-null CloseGuard.Reporter.
+ *
+ * @return CloseGuard's Reporter.
+ *
+ * @hide
+ */
+ public static Reporter getReporter() {
+ return null;
+ }
+
+ /**
+ * Sets the {@link Tracker} that is notified when resources are allocated and released.
+ * The Tracker is invoked only if CloseGuard {@link #isEnabled()} held when {@link #open()}
+ * was called. A null argument disables tracking.
+ *
+ * <p>This is only intended for use by {@code dalvik.system.CloseGuardSupport} class and so
+ * MUST NOT be used for any other purposes.
+ *
+ * @hide
+ */
+ public static void setTracker(Tracker tracker) {
+ }
+
+ /**
+ * Returns {@link #setTracker(Tracker) last Tracker that was set}, or null to indicate
+ * there is none.
+ *
+ * <p>This is only intended for use by {@code dalvik.system.CloseGuardSupport} class and so
+ * MUST NOT be used for any other purposes.
+ *
+ * @hide
+ */
+ public static Tracker getTracker() {
+ return null;
+ }
+
+ private CloseGuard() {}
+
+ /**
+ * {@code open} initializes the instance with a warning that the caller
+ * should have explicitly called the {@code closer} method instead of
+ * relying on finalization.
+ *
+ * @param closer non-null name of explicit termination method. Printed by warnIfOpen.
+ * @throws NullPointerException if closer is null.
+ *
+ * @hide
+ */
+ public void open(String closer) {
+ openWithCallSite(closer, null /* callsite */);
+ }
+
+ /**
+ * Like {@link #open(String)}, but with explicit callsite string being passed in for better
+ * performance.
+ * <p>
+ * This only has better performance than {@link #open(String)} if {@link #isEnabled()} returns {@code true}, which
+ * usually shouldn't happen on release builds.
+ *
+ * @param closer Non-null name of explicit termination method. Printed by warnIfOpen.
+ * @param callsite Non-null string uniquely identifying the callsite.
+ *
+ * @hide
+ */
+ public void openWithCallSite(String closer, String callsite) {
+ }
+
+ // We keep either an allocation stack containing the closer String or, when
+ // in disabled state, just the closer String.
+ // We keep them in a single field only to minimize overhead.
+ private Object /* String or Throwable */ closerNameOrAllocationInfo;
+
+ /**
+ * Marks this CloseGuard instance as closed to avoid warnings on
+ * finalization.
+ *
+ * @hide
+ */
+ public void close() {
+ }
+
+ /**
+ * Logs a warning if the caller did not properly cleanup by calling an
+ * explicit close method before finalization. If CloseGuard was enabled
+ * when the CloseGuard was created, passes the stacktrace associated with
+ * the allocation to the current reporter. If it was not enabled, it just
+ * directly logs a brief message.
+ *
+ * @hide
+ */
+ public void warnIfOpen() {
+ }
+
+
+ /**
+ * Interface to allow customization of tracking behaviour.
+ *
+ * <p>This is only intended for use by {@code dalvik.system.CloseGuardSupport} class and so
+ * MUST NOT be used for any other purposes.
+ *
+ * @hide
+ */
+ public interface Tracker {
+ void open(Throwable allocationSite);
+ void close(Throwable allocationSite);
+ }
+
+ /**
+ * Interface to allow customization of reporting behavior.
+ * @hide
+ */
+ public interface Reporter {
+ /**
+ *
+ * @hide
+ */
+ void report(String message, Throwable allocationSite);
+
+ /**
+ *
+ * @hide
+ */
+ default void report(String message) {}
+ }
+}
diff --git a/ravenwood/runtime-helper-src/libcore-fake/libcore/io/IoBridge.java b/ravenwood/runtime-helper-src/libcore-fake/libcore/io/IoBridge.java
new file mode 100644
index 000000000000..2a1ee2542982
--- /dev/null
+++ b/ravenwood/runtime-helper-src/libcore-fake/libcore/io/IoBridge.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2025 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 libcore.io;
+
+import android.system.ErrnoException;
+import android.system.Os;
+import android.system.OsConstants;
+
+import java.io.FileDescriptor;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+public class IoBridge {
+
+ public static void closeAndSignalBlockedThreads(FileDescriptor fd) throws IOException {
+ if (fd == null) {
+ return;
+ }
+ try {
+ Os.close(fd);
+ } catch (ErrnoException errnoException) {
+ throw errnoException.rethrowAsIOException();
+ }
+ }
+
+ public static FileDescriptor open(String path, int flags) throws FileNotFoundException {
+ FileDescriptor fd = null;
+ try {
+ fd = Os.open(path, flags, 0666);
+ // Posix open(2) fails with EISDIR only if you ask for write permission.
+ // Java disallows reading directories too.f
+ if (OsConstants.S_ISDIR(Os.fstat(fd).st_mode)) {
+ throw new ErrnoException("open", OsConstants.EISDIR);
+ }
+ return fd;
+ } catch (ErrnoException errnoException) {
+ try {
+ if (fd != null) {
+ closeAndSignalBlockedThreads(fd);
+ }
+ } catch (IOException ignored) {
+ }
+ FileNotFoundException ex = new FileNotFoundException(path + ": "
+ + errnoException.getMessage());
+ ex.initCause(errnoException);
+ throw ex;
+ }
+ }
+}
diff --git a/ravenwood/runtime-helper-src/libcore-fake/libcore/util/NativeAllocationRegistry.java b/ravenwood/runtime-helper-src/libcore-fake/libcore/util/NativeAllocationRegistry.java
index 48ee7433c52b..985e00e8641d 100644
--- a/ravenwood/runtime-helper-src/libcore-fake/libcore/util/NativeAllocationRegistry.java
+++ b/ravenwood/runtime-helper-src/libcore-fake/libcore/util/NativeAllocationRegistry.java
@@ -35,6 +35,11 @@ public class NativeAllocationRegistry {
return new NativeAllocationRegistry(classLoader, freeFunction, size);
}
+ public static NativeAllocationRegistry createNonmalloced(
+ Class clazz, long freeFunction, long size) {
+ return new NativeAllocationRegistry(clazz.getClassLoader(), freeFunction, size);
+ }
+
public static NativeAllocationRegistry createMalloced(
ClassLoader classLoader, long freeFunction, long size) {
return new NativeAllocationRegistry(classLoader, freeFunction, size);
@@ -45,6 +50,11 @@ public class NativeAllocationRegistry {
return new NativeAllocationRegistry(classLoader, freeFunction, 0);
}
+ public static NativeAllocationRegistry createMalloced(
+ Class clazz, long freeFunction, long size) {
+ return new NativeAllocationRegistry(clazz.getClassLoader(), freeFunction, size);
+ }
+
public NativeAllocationRegistry(ClassLoader classLoader, long freeFunction, long size) {
if (size < 0) {
throw new IllegalArgumentException("Invalid native allocation size: " + size);
@@ -52,6 +62,37 @@ public class NativeAllocationRegistry {
mFreeFunction = freeFunction;
}
+ private class CleanerThunk implements Runnable {
+ private long nativePtr;
+
+ public CleanerThunk() {
+ nativePtr = 0;
+ }
+
+ public void setNativePtr(long ptr) {
+ nativePtr = ptr;
+ }
+
+ @Override
+ public void run() {
+ if (nativePtr != 0) {
+ applyFreeFunction(mFreeFunction, nativePtr);
+ }
+ }
+ }
+
+ private static class CleanableRunner implements Runnable {
+ private final Cleaner.Cleanable mCleanable;
+
+ public CleanableRunner(Cleaner.Cleanable cleanable) {
+ mCleanable = cleanable;
+ }
+
+ public void run() {
+ mCleanable.clean();
+ }
+ }
+
public Runnable registerNativeAllocation(Object referent, long nativePtr) {
if (referent == null) {
throw new IllegalArgumentException("referent is null");
@@ -63,13 +104,25 @@ public class NativeAllocationRegistry {
throw new IllegalArgumentException("nativePtr is null");
}
- final Runnable releaser = () -> {
- RavenwoodRuntimeNative.applyFreeFunction(mFreeFunction, nativePtr);
- };
- sCleaner.register(referent, releaser);
+ final CleanerThunk thunk;
+ final CleanableRunner result;
+ try {
+ thunk = new CleanerThunk();
+ final var cleanable = sCleaner.register(referent, thunk);
+ result = new CleanableRunner(cleanable);
+ } catch (VirtualMachineError vme /* probably OutOfMemoryError */) {
+ applyFreeFunction(mFreeFunction, nativePtr);
+ throw vme;
+ }
+ // Enable the cleaner only after we can no longer throw anything, including OOME.
+ thunk.setNativePtr(nativePtr);
// Ensure that cleaner doesn't get invoked before we enable it.
Reference.reachabilityFence(referent);
- return releaser;
+ return result;
+ }
+
+ public static void applyFreeFunction(long freeFunction, long nativePtr) {
+ RavenwoodRuntimeNative.applyFreeFunction(freeFunction, nativePtr);
}
}