From 88b85e1f9b2fb3cf1ab9da1967e1d454be2d5be0 Mon Sep 17 00:00:00 2001 From: Robert Carr Date: Mon, 21 Mar 2022 15:47:35 -0700 Subject: SurfaceFlinger: Detect Rel Z loops Telemetry indicates we are sometimes seeing crashes due to infinite recursion in isHiddenByPolicy. We detect this condition (relative Z loop) and add a helpful log. The performance impact should be minimal as relative Z is rare, and the traversal isn't doing any heavy work or sp<> promotion. Bug: 213420016 Bug: 195298154 Bug: 219774897 Test: Existing tests pass Change-Id: Ia11854654e99288c5846fe7c62ebabf37daeb139 --- services/surfaceflinger/Layer.cpp | 31 +++++++++++++++++++++++++++++++ services/surfaceflinger/Layer.h | 1 + 2 files changed, 32 insertions(+) diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp index aeaf1e1a14..8159150f85 100644 --- a/services/surfaceflinger/Layer.cpp +++ b/services/surfaceflinger/Layer.cpp @@ -833,6 +833,14 @@ bool Layer::setRelativeLayer(const sp& relativeToHandle, int32_t relati return false; } + if (CC_UNLIKELY(relative->usingRelativeZ(LayerVector::StateSet::Drawing)) && + (relative->mDrawingState.zOrderRelativeOf == this)) { + ALOGE("Detected relative layer loop between %s and %s", + mName.c_str(), relative->mName.c_str()); + ALOGE("Ignoring new call to set relative layer"); + return false; + } + mFlinger->mSomeChildrenChanged = true; mDrawingState.sequence++; @@ -1990,6 +1998,18 @@ void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster, } } +bool Layer::findInHierarchy(const sp& l) { + if (l == this) { + return true; + } + for (auto& child : mDrawingChildren) { + if (child->findInHierarchy(l)) { + return true; + } + } + return false; +} + void Layer::commitChildList() { for (size_t i = 0; i < mCurrentChildren.size(); i++) { const auto& child = mCurrentChildren[i]; @@ -1997,6 +2017,17 @@ void Layer::commitChildList() { } mDrawingChildren = mCurrentChildren; mDrawingParent = mCurrentParent; + if (CC_UNLIKELY(usingRelativeZ(LayerVector::StateSet::Drawing))) { + auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote(); + if (zOrderRelativeOf == nullptr) return; + if (findInHierarchy(zOrderRelativeOf)) { + ALOGE("Detected Z ordering loop between %s and %s", mName.c_str(), + zOrderRelativeOf->mName.c_str()); + ALOGE("Severing rel Z loop, potentially dangerous"); + mDrawingState.isRelativeOf = false; + zOrderRelativeOf->removeZOrderRelative(this); + } + } } diff --git a/services/surfaceflinger/Layer.h b/services/surfaceflinger/Layer.h index 48a9bc50c4..846460d4b1 100644 --- a/services/surfaceflinger/Layer.h +++ b/services/surfaceflinger/Layer.h @@ -1138,6 +1138,7 @@ private: bool mIsAtRoot = false; uint32_t mLayerCreationFlags; + bool findInHierarchy(const sp&); }; std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate); -- cgit v1.2.3-59-g8ed1b