From a8742d6851aafad9c42aca20ab84cf587226c862 Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Thu, 8 Nov 2018 15:19:23 -0800 Subject: [PATCH] libpower: try detect memory corruption and abort Bug: 117575503 Test: device boots Change-Id: Idf81ad5591c675473624dc4d0a057ae715ed748f --- power.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/power.cpp b/power.cpp index c4f2036..093fd4c 100644 --- a/power.cpp +++ b/power.cpp @@ -18,6 +18,7 @@ #define ATRACE_TAG ATRACE_TAG_POWER #include +#include #include #include #include @@ -36,7 +37,30 @@ static std::mutex gLock; static std::unordered_map> gWakeLockMap; static const sp& getSystemSuspendServiceOnce() { - static sp suspendService = ISystemSuspend::getService(); + using android::system::suspend::V1_0::BpHwSystemSuspend; + static std::once_flag initFlag; + static sp suspendService = nullptr; + + // TODO(b/117575503): We use this buffer to make sure that suspendService pointer and the + // underlying memory are not corrupted before using it. Ideally, memory corruption should be + // fixed. + static constexpr size_t bufSize = sizeof(BpHwSystemSuspend); + static char buf[bufSize]; + + std::call_once(initFlag, []() { + // It's possible for the calling process to not have permissions to + // ISystemSuspend. getService will then return nullptr. + suspendService = ISystemSuspend::getService(); + if (suspendService) { + std::memcpy(buf, static_cast(suspendService.get()), bufSize); + } + }); + if (suspendService) { + if (std::memcmp(buf, static_cast(suspendService.get()), bufSize) != 0) { + LOG(FATAL) << "Memory corrupted. Aborting."; + } + } + return suspendService; }