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

Commit 38e04d4e authored by Atneya Nair's avatar Atneya Nair
Browse files

Add binder unhandled exception notification

Exceptions which are not parcelled by binder are dropped on the service
side. For oneway txns, all exceptions are dropped.

Add an extension point to Java binder interface implementations to
generically handle an exception which will be dropped, by
logging/crashing or some other facility.

Bug: 150808347
Test: atest BinderUncaughtExceptionHandlerTest
Test: Manual (handled vs unhandled exception in subsequent CL)
Flag: EXEMPT safe addition, no api
Change-Id: I6aa97c363ccf21af262ad95efc20611c78796e32
parent 2ca3236b
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -1110,6 +1110,21 @@ public class Binder implements IBinder {
            @Nullable String[] args) {
    }

    /**
     * Called whenever the stub implementation throws an exception which isn't propagated to the
     * remote caller by the binder. If this method isn't overridden, this exception is swallowed,
     * and some default return values are propagated to the caller.
     *
     * <br> <b> This should not throw. </b> Doing so would defeat the purpose of this handler, and
     * suppress the exception it is handling.
     *
     * @param code The transaction code being handled
     * @param e The exception which was thrown.
     * @hide
     */
    protected void onUnhandledException(int code, int flags, Exception e) {
    }

    /**
     * @param in The raw file descriptor that an input data stream can be read from.
     * @param out The raw file descriptor that normal command messages should be written to.
@@ -1408,10 +1423,15 @@ public class Binder implements IBinder {
                } else {
                    Log.w(TAG, "Caught a RuntimeException from the binder stub implementation.", e);
                }
                onUnhandledException(code, flags, e);
            } else {
                // Clear the parcel before writing the exception.
                reply.setDataSize(0);
                reply.setDataPosition(0);
                // The writeException below won't do anything useful if this is the case.
                if (Parcel.getExceptionCode(e) == 0) {
                    onUnhandledException(code, flags, e);
                }
                reply.writeException(e);
            }
            res = true;
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ filegroup {
        "BinderProxyCountingTestApp/src/**/*.java",
        "BinderProxyCountingTestService/src/**/*.java",
        "BinderDeathRecipientHelperApp/src/**/*.java",
        "AppThatCallsBinderMethods/src/**/*.kt",
    ],
    visibility: ["//visibility:private"],
}
@@ -143,6 +144,7 @@ android_test {
        ":BinderProxyCountingTestApp",
        ":BinderProxyCountingTestService",
        ":AppThatUsesAppOps",
        ":AppThatCallsBinderMethods",
    ],
}

+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
        <option name="test-file-name" value="BinderProxyCountingTestApp.apk" />
        <option name="test-file-name" value="BinderProxyCountingTestService.apk" />
        <option name="test-file-name" value="AppThatUsesAppOps.apk" />
        <option name="test-file-name" value="AppThatCallsBinderMethods.apk" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+20 −0
Original line number Diff line number Diff line
// 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.

android_test_helper_app {
    name: "AppThatCallsBinderMethods",
    srcs: ["src/**/*.kt"],
    platform_apis: true,
    static_libs: ["coretests-aidl"],
}
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>

<!--
  ~ 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.
  -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.android.frameworks.coretests.methodcallerhelperapp">
    <application>
        <receiver android:name="com.android.frameworks.coretests.methodcallerhelperapp.CallMethodsReceiver"
                  android:exported="true"/>
    </application>

</manifest>
Loading