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

Commit ad5b51fe authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 9432410 from 015ae27a to udc-release

Change-Id: Ie59f6dd519cd62256f3fe0fd7f42e0b628e7b020
parents 7a42f911 015ae27a
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 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.
-->

<!--
     This is the feature indicating that the device has support for updating
     source and destination addresses of IPsec tunnels
-->

<permissions>
    <feature name="android.software.ipsec_tunnel_migration" />
</permissions>
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */

#pragma once

#include <android-base/thread_annotations.h>
#include <android/sysprop/InputProperties.sysprop.h>
#include <input/Input.h>

namespace android {

static inline bool isMotionPredictionEnabled() {
    return sysprop::InputProperties::enable_motion_prediction().value_or(true);
}

/**
 * Given a set of MotionEvents for the current gesture, predict the motion. The returned MotionEvent
 * contains a set of samples in the future, up to "presentation time + offset".
 *
 * The typical usage is like this:
 *
 * MotionPredictor predictor(offset = MY_OFFSET);
 * predictor.setExpectedPresentationTimeNanos(NEXT_PRESENT_TIME);
 * predictor.record(DOWN_MOTION_EVENT);
 * predictor.record(MOVE_MOTION_EVENT);
 * prediction = predictor.predict();
 *
 * The presentation time should be set some time before calling .predict(). It could be set before
 * or after the recorded motion events. Must be done on every frame.
 *
 * The resulting motion event will have eventTime <= (NEXT_PRESENT_TIME + MY_OFFSET). It might
 * contain historical data, which are additional samples from the latest recorded MotionEvent's
 * eventTime to the NEXT_PRESENT_TIME + MY_OFFSET.
 *
 * The offset is used to provide additional flexibility to the caller, in case the default present
 * time (typically provided by the choreographer) does not account for some delays, or to simply
 * reduce the aggressiveness of the prediction. Offset can be both positive and negative.
 */
class MotionPredictor {
public:
    /**
     * Parameters:
     * predictionTimestampOffsetNanos: additional, constant shift to apply to the target
     * presentation time. The prediction will target the time t=(presentationTime +
     * predictionTimestampOffsetNanos).
     *
     * checkEnableMotionPredition: the function to check whether the prediction should run. Used to
     * provide an additional way of turning prediction on and off. Can be toggled at runtime.
     */
    MotionPredictor(nsecs_t predictionTimestampOffsetNanos,
                    std::function<bool()> checkEnableMotionPrediction = isMotionPredictionEnabled);
    void record(const MotionEvent& event);
    std::vector<std::unique_ptr<MotionEvent>> predict();
    bool isPredictionAvailable(int32_t deviceId, int32_t source);
    void setExpectedPresentationTimeNanos(int64_t expectedPresentationTimeNanos);

private:
    std::mutex mLock;
    int64_t mExpectedPresentationTimeNanos GUARDED_BY(mLock) = 0;
    int64_t getExpectedPresentationTimeNanos();
    std::vector<MotionEvent> mEvents;
    const nsecs_t mPredictionTimestampOffsetNanos;
    const std::function<bool()> mCheckMotionPredictionEnabled;
};

} // namespace android
+8 −9
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ std::atomic_bool BpBinder::sCountByUidEnabled(false);
binder_proxy_limit_callback BpBinder::sLimitCallback;
bool BpBinder::sBinderProxyThrottleCreate = false;

static StaticString16 kDescriptorUninit(u"<uninit descriptor>");

// Arbitrarily high value that probably distinguishes a bad behaving app
uint32_t BpBinder::sBinderProxyCountHighWatermark = 2500;
// Another arbitrary value a binder count needs to drop below before another callback will be called
@@ -211,6 +213,7 @@ BpBinder::BpBinder(Handle&& handle)
        mAlive(true),
        mObitsSent(false),
        mObituaries(nullptr),
        mDescriptorCache(kDescriptorUninit),
        mTrackedUid(-1) {
    extendObjectLifetime(OBJECT_LIFETIME_WEAK);
}
@@ -258,12 +261,12 @@ std::optional<int32_t> BpBinder::getDebugBinderHandle() const {

bool BpBinder::isDescriptorCached() const {
    Mutex::Autolock _l(mLock);
    return mDescriptorCache.size() ? true : false;
    return mDescriptorCache.string() != kDescriptorUninit.string();
}

const String16& BpBinder::getInterfaceDescriptor() const
{
    if (isDescriptorCached() == false) {
    if (!isDescriptorCached()) {
        sp<BpBinder> thiz = sp<BpBinder>::fromExisting(const_cast<BpBinder*>(this));

        Parcel data;
@@ -276,8 +279,7 @@ const String16& BpBinder::getInterfaceDescriptor() const
            Mutex::Autolock _l(mLock);
            // mDescriptorCache could have been assigned while the lock was
            // released.
            if (mDescriptorCache.size() == 0)
                mDescriptorCache = res;
            if (mDescriptorCache.string() == kDescriptorUninit.string()) mDescriptorCache = res;
        }
    }

@@ -369,10 +371,7 @@ status_t BpBinder::transact(
        if (data.dataSize() > LOG_TRANSACTIONS_OVER_SIZE) {
            Mutex::Autolock _l(mLock);
            ALOGW("Large outgoing transaction of %zu bytes, interface descriptor %s, code %d",
                  data.dataSize(),
                  mDescriptorCache.size() ? String8(mDescriptorCache).c_str()
                                          : "<uncached descriptor>",
                  code);
                  data.dataSize(), String8(mDescriptorCache).c_str(), code);
        }

        if (status == DEAD_OBJECT) mAlive = 0;
@@ -647,7 +646,7 @@ void BpBinder::onLastStrongRef(const void* /*id*/) {
    if(obits != nullptr) {
        if (!obits->isEmpty()) {
            ALOGI("onLastStrongRef automatically unlinking death recipients: %s",
                  mDescriptorCache.size() ? String8(mDescriptorCache).c_str() : "<uncached descriptor>");
                  String8(mDescriptorCache).c_str());
        }

        if (ipc) ipc->clearDeathNotification(binderHandle(), this);
+59 −35
Original line number Diff line number Diff line
@@ -75,12 +75,48 @@ void clean(const void* id, void* obj, void* cookie) {
AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
AIBinder::~AIBinder() {}

std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
                                                     const String16& newDescriptor, bool set) {
// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
static std::string SanitizeString(const String16& str) {
    std::string sanitized{String8(str)};
    for (auto& c : sanitized) {
        if (!isprint(c)) {
            c = '?';
        }
    }
    return sanitized;
}

bool AIBinder::associateClass(const AIBinder_Class* clazz) {
    if (clazz == nullptr) return false;

    // If mClazz is non-null, this must have been called and cached
    // already. So, we can safely call this first. Due to the implementation
    // of getInterfaceDescriptor (at time of writing), two simultaneous calls
    // may lead to extra binder transactions, but this is expected to be
    // exceedingly rare. Once we have a binder, when we get it again later,
    // we won't make another binder transaction here.
    const String16& descriptor = getBinder()->getInterfaceDescriptor();
    const String16& newDescriptor = clazz->getInterfaceDescriptor();

    std::lock_guard<std::mutex> lock(mClazzMutex);
    if (mClazz == clazz) return true;

    if (mClazz != nullptr) {
    // If this is an ABpBinder, the first class object becomes the canonical one. The implication
    // of this is that no API can require a proxy information to get information on how to behave.
    // from the class itself - which should only store the interface descriptor. The functionality
    // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by
    // setting things on AIBinder_Class which get transferred along with the binder, so that they
    // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in
    // onTransact).
    //
    // While this check is required to support linkernamespaces, one downside of it is that
    // you may parcel code to communicate between things in the same process. However, comms
    // between linkernamespaces like this already happen for cross-language calls like Java<->C++
    // or Rust<->Java, and there are good stability guarantees here. This interacts with
    // binder Stability checks exactly like any other in-process call. The stability is known
    // to the IBinder object, so that it doesn't matter if a class object comes from
    // a different stability level.
    if (mClazz != nullptr && !asABpBinder()) {
        const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
        if (newDescriptor == currentDescriptor) {
            LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
@@ -97,37 +133,10 @@ std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz
        return false;
    }

    if (set) {
        // if this is a local object, it's not one known to libbinder_ndk
        mClazz = clazz;
        return true;
    }

    return {};
}

// b/175635923 libcxx causes "implicit-conversion" with a string with invalid char
static std::string SanitizeString(const String16& str) {
    std::string sanitized{String8(str)};
    for (auto& c : sanitized) {
        if (!isprint(c)) {
            c = '?';
        }
    }
    return sanitized;
}

bool AIBinder::associateClass(const AIBinder_Class* clazz) {
    if (clazz == nullptr) return false;

    const String16& newDescriptor = clazz->getInterfaceDescriptor();

    auto result = associateClassInternal(clazz, newDescriptor, false);
    if (result.has_value()) return *result;

    CHECK(asABpBinder() != nullptr);  // ABBinder always has a descriptor

    const String16& descriptor = getBinder()->getInterfaceDescriptor();
    // This will always be an O(n) comparison, but it's expected to be extremely rare.
    // since it's an error condition. Do the comparison after we take the lock and
    // check the pointer equality fast path. By always taking the lock, it's also
    // more flake-proof. However, the check is not dependent on the lock.
    if (descriptor != newDescriptor) {
        if (getBinder()->isBinderAlive()) {
            LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
@@ -141,7 +150,14 @@ bool AIBinder::associateClass(const AIBinder_Class* clazz) {
        return false;
    }

    return associateClassInternal(clazz, newDescriptor, true).value();
    // A local binder being set for the first time OR
    // ignoring a proxy binder which is set multiple time, by considering the first
    // associated class as the canonical one.
    if (mClazz == nullptr) {
        mClazz = clazz;
    }

    return true;
}

ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
@@ -325,6 +341,10 @@ bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
    return lhs->binder < rhs->binder;
}

// WARNING: When multiple classes exist with the same interface descriptor in different
// linkernamespaces, the first one to be associated with mClazz becomes the canonical one
// and the only requirement on this is that the interface descriptors match. If this
// is an ABpBinder, no other state can be referenced from mClazz.
AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
                               AIBinder_Class_onDestroy onDestroy,
                               AIBinder_Class_onTransact onTransact)
@@ -632,6 +652,10 @@ binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
    (*in)->get()->markForBinder(binder->getBinder());

    status_t status = android::OK;

    // note - this is the only read of a value in clazz, and it comes with a warning
    // on the API itself. Do not copy this design. Instead, attach data in a new
    // version of the prepareTransaction function.
    if (clazz->writeHeader) {
        status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
    }
+5 −3
Original line number Diff line number Diff line
@@ -53,12 +53,14 @@ struct AIBinder : public virtual ::android::RefBase {
    }

   private:
    std::optional<bool> associateClassInternal(const AIBinder_Class* clazz,
                                               const ::android::String16& newDescriptor, bool set);

    // AIBinder instance is instance of this class for a local object. In order to transact on a
    // remote object, this also must be set for simplicity (although right now, only the
    // interfaceDescriptor from it is used).
    //
    // WARNING: When multiple classes exist with the same interface descriptor in different
    // linkernamespaces, the first one to be associated with mClazz becomes the canonical one
    // and the only requirement on this is that the interface descriptors match. If this
    // is an ABpBinder, no other state can be referenced from mClazz.
    const AIBinder_Class* mClazz;
    std::mutex mClazzMutex;
};
Loading