Support Parcel, Bundle under Ravenwood, with CTS.

We have baseline implementations of the Parcel and Bundle, so we're
ready to support them under the Ravenwood environment.  Brings along
the relevant CTS to prove that it works consistently on both devices
and Ravenwood.  Make a few test-driven bug fixes to Parcel code.

Currently does not support FDs or Binders.

Add new `@IgnoreUnderRavenwood` annotation that is respected by a
new `RavenwoodRule`, letting the same test code be shared to run
under both devices and Ravenwood.  Currently packaged under our own
area of the tree, but named to match layout of `platform_testing`
for future migration.

Add "ThrowClass" policy.

Bug: 292141694
Test: atest-dev CtsOsTestCasesRavenwood CtsOsTestCases:ParcelTest CtsOsTestCases:BundleTest
Change-Id: I88d19afff47fbea6557e922a10e8555ee0f70ba3
diff --git a/Ravenwood.bp b/Ravenwood.bp
index 1582266..3310898 100644
--- a/Ravenwood.bp
+++ b/Ravenwood.bp
@@ -77,10 +77,17 @@
         "framework-minus-apex.ravenwood",
         "hoststubgen-helper-runtime.ravenwood",
         "hoststubgen-helper-framework-runtime.ravenwood",
+        "junit",
+        "truth",
+        "ravenwood-junit",
     ],
 }
 
 android_ravenwood_libgroup {
     name: "ravenwood-utils",
-    libs: [],
+    libs: [
+        "junit",
+        "truth",
+        "ravenwood-junit",
+    ],
 }
diff --git a/core/java/android/os/Binder.java b/core/java/android/os/Binder.java
index 3db1cb0..9956220 100644
--- a/core/java/android/os/Binder.java
+++ b/core/java/android/os/Binder.java
@@ -333,6 +333,12 @@
     @CriticalNative
     public static final native boolean isDirectlyHandlingTransactionNative();
 
+    /** @hide */
+    public static final boolean isDirectlyHandlingTransactionNative$ravenwood() {
+        // Ravenwood doesn't support IPC
+        return false;
+    }
+
     private static boolean sIsHandlingBinderTransaction = false;
 
     /**
@@ -715,7 +721,9 @@
      */
     public Binder(@Nullable String descriptor) {
         mObject = getNativeBBinderHolder();
-        NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject);
+        if (mObject != 0L) {
+            NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject);
+        }
 
         if (FIND_POTENTIAL_LEAKS) {
             final Class<? extends Binder> klass = getClass();
@@ -1277,6 +1285,10 @@
 
     private static native long getNativeBBinderHolder();
 
+    private static long getNativeBBinderHolder$ravenwood() {
+        return 0L;
+    }
+
     /**
      * By default, we use the calling UID since we can always trust it.
      */
diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java
index 352c9d2..86628d9 100644
--- a/core/java/android/os/Parcel.java
+++ b/core/java/android/os/Parcel.java
@@ -1558,7 +1558,7 @@
         ensureWithinMemoryLimit(typeSize, totalObjects);
     }
 
-    private void ensureWithinMemoryLimit(int typeSize, @NonNull int length) {
+    private void ensureWithinMemoryLimit(int typeSize, int length) {
         int estimatedAllocationSize = 0;
         try {
             estimatedAllocationSize = Math.multiplyExact(typeSize, length);
@@ -2957,6 +2957,14 @@
     }
 
     /** @hide */
+    public final void writeException$ravenwood(@NonNull Exception e) {
+        // Ravenwood doesn't support IPC, no transaction headers needed
+        writeInt(getExceptionCode(e));
+        writeString(e.getMessage());
+        writeInt(0);
+    }
+
+    /** @hide */
     public static int getExceptionCode(@NonNull Throwable e) {
         int code = 0;
         if (e instanceof Parcelable
@@ -3039,6 +3047,12 @@
         }
     }
 
+    /** @hide */
+    public final void writeNoException$ravenwood() {
+        // Ravenwood doesn't support IPC, no transaction headers needed
+        writeInt(0);
+    }
+
     /**
      * Special function for reading an exception result from the header of
      * a parcel, to be used after receiving the result of a transaction.  This
diff --git a/framework-minus-apex-ravenwood-policies.txt b/framework-minus-apex-ravenwood-policies.txt
index 8e76fd2..48c0a2d 100644
--- a/framework-minus-apex-ravenwood-policies.txt
+++ b/framework-minus-apex-ravenwood-policies.txt
@@ -71,8 +71,9 @@
 # Misc
 class android.util.Dumpable stubclass
 class android.util.DebugUtils stubclass
-class android.util.UtilConfig stubclass
+class android.util.MathUtils stubclass
 class android.util.Patterns stubclass
+class android.util.UtilConfig stubclass
 
 # Internals
 class com.android.internal.util.ArrayUtils stubclass
@@ -89,3 +90,26 @@
 class com.android.internal.util.LineBreakBufferedWriter stubclass
 class com.android.internal.util.Preconditions stubclass
 class com.android.internal.util.StringPool stubclass
+
+# Parcel
+class android.os.Parcel stubclass
+    method writeException (Ljava/lang/Exception;)V @writeException$ravenwood
+    method writeNoException ()V @writeNoException$ravenwood
+class android.os.Parcel !com.android.hoststubgen.nativesubstitution.Parcel_host
+
+class android.os.Parcelable stubclass
+class android.os.ParcelFormatException stubclass
+class android.os.BadParcelableException stubclass
+class android.os.BadTypeParcelableException stubclass
+
+# Binder: just enough to construct, no further functionality
+class android.os.Binder stub
+    method <init> ()V stub
+    method <init> (Ljava/lang/String;)V stub
+    method isDirectlyHandlingTransaction ()Z stub
+    method isDirectlyHandlingTransactionNative ()Z @isDirectlyHandlingTransactionNative$ravenwood
+    method getNativeBBinderHolder ()J @getNativeBBinderHolder$ravenwood
+
+# Containers
+class android.os.BaseBundle stubclass
+class android.os.Bundle stubclass
diff --git a/ravenwood/Android.bp b/ravenwood/Android.bp
index d0e442e..5c9bf18 100644
--- a/ravenwood/Android.bp
+++ b/ravenwood/Android.bp
@@ -32,3 +32,14 @@
     host_supported: true,
     visibility: ["//visibility:public"],
 }
+
+java_library {
+    name: "ravenwood-junit",
+    srcs: ["junit-src/**/*.java"],
+    libs: [
+        "junit",
+    ],
+    sdk_version: "core_current",
+    host_supported: true,
+    visibility: ["//visibility:public"],
+}
diff --git a/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java b/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java
new file mode 100644
index 0000000..0aac084
--- /dev/null
+++ b/ravenwood/junit-src/android/platform/test/annotations/IgnoreUnderRavenwood.java
@@ -0,0 +1,33 @@
+/*
+ * 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.platform.test.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * THIS ANNOTATION IS EXPERIMENTAL. REACH OUT TO g/ravenwood BEFORE USING IT, OR YOU HAVE ANY
+ * QUESTIONS ABOUT IT.
+ *
+ * @hide
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface IgnoreUnderRavenwood {
+}
diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java
new file mode 100644
index 0000000..a6b3f66
--- /dev/null
+++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java
@@ -0,0 +1,50 @@
+/*
+ * 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.platform.test.ravenwood;
+
+import android.platform.test.annotations.IgnoreUnderRavenwood;
+
+import org.junit.Assume;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * THIS RULE IS EXPERIMENTAL. REACH OUT TO g/ravenwood BEFORE USING IT, OR YOU HAVE ANY
+ * QUESTIONS ABOUT IT.
+ *
+ * @hide
+ */
+public class RavenwoodRule implements TestRule {
+    public boolean isUnderRavenwood() {
+        // TODO: give ourselves a better environment signal
+        return System.getProperty("java.class.path").contains("ravenwood");
+    }
+
+    @Override
+    public Statement apply(Statement base, Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                if (description.getAnnotation(IgnoreUnderRavenwood.class) != null) {
+                    Assume.assumeFalse(isUnderRavenwood());
+                }
+                base.evaluate();
+            }
+        };
+    }
+}
diff --git a/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/framework/com/android/hoststubgen/nativesubstitution/Parcel_host.java b/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/framework/com/android/hoststubgen/nativesubstitution/Parcel_host.java
index d749f07..12c7841 100644
--- a/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/framework/com/android/hoststubgen/nativesubstitution/Parcel_host.java
+++ b/tools/hoststubgen/hoststubgen/helper-framework-runtime-src/framework/com/android/hoststubgen/nativesubstitution/Parcel_host.java
@@ -20,6 +20,7 @@
 import java.io.FileDescriptor;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicLong;
@@ -197,9 +198,9 @@
         if (b == null) {
             nativeWriteInt(nativePtr, -1);
         } else {
-            final var alignedSize = align4(b.length);
+            final var alignedSize = align4(len);
 
-            nativeWriteInt(nativePtr, b.length);
+            nativeWriteInt(nativePtr, len);
 
             p.ensureMoreCapacity(alignedSize);
 
@@ -280,6 +281,7 @@
                     + data.length + " given=" + destLen);
             return false;
         }
+        System.arraycopy(data, 0, dest, 0, data.length);
         return true;
     }
 
@@ -289,7 +291,12 @@
             return null;
         }
         var p = getInstance(nativePtr);
-        p.ensureDataAvailable(size);
+        try {
+            p.ensureDataAvailable(align4(size));
+        } catch (Exception e) {
+            System.err.println(e.toString());
+            return null;
+        }
 
         var bytes = new byte[size];
         System.arraycopy(p.mBuffer, p.mPos, bytes, 0, size);
@@ -301,7 +308,10 @@
     public static int nativeReadInt(long nativePtr) {
         var p = getInstance(nativePtr);
 
-        p.ensureDataAvailable(Integer.BYTES);
+        if (p.mSize - p.mPos < 4) {
+            // Match native impl that returns "0" when not enough data
+            return 0;
+        }
 
         var ret = (((p.mBuffer[p.mPos++] & 0xff) << 24)
                 | ((p.mBuffer[p.mPos++] & 0xff) << 16)
@@ -341,11 +351,16 @@
     }
 
     public static byte[] nativeMarshall(long nativePtr) {
-        throw new RuntimeException("Not implemented yet");
+        var p = getInstance(nativePtr);
+        return Arrays.copyOf(p.mBuffer, p.mSize);
     }
     public static void nativeUnmarshall(
             long nativePtr, byte[] data, int offset, int length) {
-        throw new RuntimeException("Not implemented yet");
+        var p = getInstance(nativePtr);
+        p.ensureMoreCapacity(length);
+        System.arraycopy(data, offset, p.mBuffer, p.mPos, length);
+        p.mPos += length;
+        p.updateSize();
     }
     public static int nativeCompareData(long thisNativePtr, long otherNativePtr) {
         throw new RuntimeException("Not implemented yet");