summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Moreland <smoreland@google.com>2020-10-13 23:02:42 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2021-02-09 20:19:41 +0000
commit56c311eca40a5832ff885ed01ec3acafd9d9e471 (patch)
treebc4c7669081692fa94ee8301d193065c953390ac
parent3eec5b9bee837e8a5454682902978ecfe5c1866d (diff)
downloadlibhidl-56c311eca40a5832ff885ed01ec3acafd9d9e471.tar.gz
getProcessAgeMs: better error messages
There function is jumping to abort on specific boots in two processes on the device repeatedly. Most likely, it is failing because of ubsan, but it isn't clear where exactly it is failing, so checking all assumptions and dumping errors. Bug: 170456142 Bug: 173264759 Test: adb logcat *:E | grep HidlServiceManagement # no errors on ToT Test: adb logcat | grep HidlServiceManagement # see registration logs Merged-In: Ib5c390dd93a9e4f78b42e7172cff74603a89f6cf Change-Id: Ib5c390dd93a9e4f78b42e7172cff74603a89f6cf (cherry picked from commit adee24d077f082bc8e946e1cd81a2c3830570c56)
-rw-r--r--transport/ServiceManagement.cpp50
1 files changed, 39 insertions, 11 deletions
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 7de5c78..5f92dc1 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -160,22 +160,50 @@ namespace details {
__attribute__((noinline)) static long getProcessAgeMs() {
constexpr const int PROCFS_STAT_STARTTIME_INDEX = 21;
std::string content;
- android::base::ReadFileToString("/proc/self/stat", &content, false);
- auto stats = android::base::Split(content, " ");
- if (stats.size() <= PROCFS_STAT_STARTTIME_INDEX) {
- LOG(INFO) << "Could not read starttime from /proc/self/stat";
+ if (!android::base::ReadFileToString("/proc/self/stat", &content, false)) {
+ LOG(ERROR) << "Process age: Could not read /proc/self/stat";
return -1;
}
- const std::string& startTimeString = stats[PROCFS_STAT_STARTTIME_INDEX];
- static const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
- const int64_t uptime = android::uptimeMillis();
+ std::vector<std::string> stats = android::base::Split(content, " ");
+ if (PROCFS_STAT_STARTTIME_INDEX >= stats.size()) {
+ LOG(ERROR) << "Process age: Could not read starttime from /proc/self/stat";
+ return -1;
+ }
+
+ const std::string& startTimeString = stats.at(PROCFS_STAT_STARTTIME_INDEX);
unsigned long long startTimeInClockTicks = 0;
- if (android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
- long startTimeMs = 1000ULL * startTimeInClockTicks / ticksPerSecond;
- return uptime - startTimeMs;
+ if (!android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
+ LOG(ERROR) << "Process age: Could not parse start time: " << startTimeString;
+ return -1;
}
- return -1;
+
+ const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
+ if (ticksPerSecond <= 0) {
+ LOG(ERROR) << "Process age: Invalid _SC_CLK_TCK: " << ticksPerSecond;
+ return -1;
+ }
+
+ const int64_t uptime = android::uptimeMillis();
+ if (uptime < 0) {
+ LOG(ERROR) << "Process age: Invalid uptime: " << uptime;
+ return -1;
+ }
+
+ unsigned long long startTimeTicks;
+ if (__builtin_umulll_overflow(1000ULL, startTimeInClockTicks, &startTimeTicks)) {
+ LOG(ERROR) << "Process age: Too many ticks, overflow: " << startTimeInClockTicks;
+ return -1;
+ }
+
+ long startTimeMs = startTimeTicks / ticksPerSecond;
+ if (startTimeMs >= uptime) {
+ LOG(ERROR) << "Process age: process started in future: " << startTimeMs << " after "
+ << uptime;
+ return -1;
+ }
+
+ return uptime - startTimeMs;
}
static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {