summaryrefslogtreecommitdiff
path: root/ravenwood/runtime-common-src
diff options
context:
space:
mode:
author John Wu <topjohnwu@google.com> 2024-07-31 22:46:33 +0000
committer Cherrypicker Worker <android-build-cherrypicker-worker@google.com> 2024-08-01 18:17:21 +0000
commit4ac7fb181a44e5bf484339db21109f76148ebe84 (patch)
tree353f29bfe23eaa22f83f371f1d92e37da35948a9 /ravenwood/runtime-common-src
parentf01b502933344c8bdf84c25c22fef259323332f9 (diff)
[Ravenwood] Enable PFD.getStatSize and cleanup implementation
Bug: 353521759 Flag: EXEMPT host side test change only Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:be940db4aacbb14f0abc35197b224bf197ddac4f) Merged-In: I011f14bdefb041d6f75a5d4c1d729dc368528d09 Change-Id: I011f14bdefb041d6f75a5d4c1d729dc368528d09
Diffstat (limited to 'ravenwood/runtime-common-src')
-rw-r--r--ravenwood/runtime-common-src/com/android/ravenwood/common/JvmWorkaround.java11
-rw-r--r--ravenwood/runtime-common-src/com/android/ravenwood/common/OpenJdkWorkaround.java17
-rw-r--r--ravenwood/runtime-common-src/com/android/ravenwood/common/SneakyThrow.java33
3 files changed, 61 insertions, 0 deletions
diff --git a/ravenwood/runtime-common-src/com/android/ravenwood/common/JvmWorkaround.java b/ravenwood/runtime-common-src/com/android/ravenwood/common/JvmWorkaround.java
index ee280991216a..0238baa2dcbf 100644
--- a/ravenwood/runtime-common-src/com/android/ravenwood/common/JvmWorkaround.java
+++ b/ravenwood/runtime-common-src/com/android/ravenwood/common/JvmWorkaround.java
@@ -16,6 +16,7 @@
package com.android.ravenwood.common;
import java.io.FileDescriptor;
+import java.io.IOException;
/**
* Collection of methods to workaround limitation in the hostside JVM.
@@ -44,6 +45,11 @@ public abstract class JvmWorkaround {
public abstract int getFdInt(FileDescriptor fd);
/**
+ * Equivalent to Android's Os.close(fd).
+ */
+ public abstract void closeFd(FileDescriptor fd) throws IOException;
+
+ /**
* Placeholder implementation for the host side.
*
* Even on the host side, we don't want to throw just because the class is loaded,
@@ -64,5 +70,10 @@ public abstract class JvmWorkaround {
public int getFdInt(FileDescriptor fd) {
throw calledOnHostside();
}
+
+ @Override
+ public void closeFd(FileDescriptor fd) {
+ throw calledOnHostside();
+ }
}
}
diff --git a/ravenwood/runtime-common-src/com/android/ravenwood/common/OpenJdkWorkaround.java b/ravenwood/runtime-common-src/com/android/ravenwood/common/OpenJdkWorkaround.java
index 9aedaab5b911..a260147654cd 100644
--- a/ravenwood/runtime-common-src/com/android/ravenwood/common/OpenJdkWorkaround.java
+++ b/ravenwood/runtime-common-src/com/android/ravenwood/common/OpenJdkWorkaround.java
@@ -16,6 +16,8 @@
package com.android.ravenwood.common;
import java.io.FileDescriptor;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
class OpenJdkWorkaround extends JvmWorkaround {
@Override
@@ -43,4 +45,19 @@ class OpenJdkWorkaround extends JvmWorkaround {
+ " perhaps JRE has changed?", e);
}
}
+
+ @Override
+ public void closeFd(FileDescriptor fd) throws IOException {
+ try {
+ final Object obj = Class.forName("jdk.internal.access.SharedSecrets").getMethod(
+ "getJavaIOFileDescriptorAccess").invoke(null);
+ Class.forName("jdk.internal.access.JavaIOFileDescriptorAccess").getMethod(
+ "close", FileDescriptor.class).invoke(obj, fd);
+ } catch (InvocationTargetException e) {
+ SneakyThrow.sneakyThrow(e.getTargetException());
+ } catch (ReflectiveOperationException e) {
+ throw new RuntimeException("Failed to interact with raw FileDescriptor internals;"
+ + " perhaps JRE has changed?", e);
+ }
+ }
}
diff --git a/ravenwood/runtime-common-src/com/android/ravenwood/common/SneakyThrow.java b/ravenwood/runtime-common-src/com/android/ravenwood/common/SneakyThrow.java
new file mode 100644
index 000000000000..0dbf7df12bce
--- /dev/null
+++ b/ravenwood/runtime-common-src/com/android/ravenwood/common/SneakyThrow.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2024 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.ravenwood.common;
+
+public class SneakyThrow {
+
+ private SneakyThrow() {
+ }
+
+ /**
+ * Throw checked exceptions without the need to declare in method signature
+ */
+ public static void sneakyThrow(Throwable t) {
+ SneakyThrow.<RuntimeException>sneakyThrow_(t);
+ }
+
+ private static <T extends Throwable> void sneakyThrow_(Throwable t) throws T {
+ throw (T) t;
+ }
+}