From a5ca7665cdb316e51842a32f38b345641c5ac27a Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 26 Jan 2021 11:56:58 -0800 Subject: Avoid integer underflow involving maxFreqCount. If a device does not have any policy directories under /sys/devices/system/cpu/cpufreq this would previously lead to the cputimeinstate subsystem being initialized with an empty set of policy frequencies. This would lead to integer underflows in various loops that enumerate the frequencies when subtracting 1 from a maxFreqCount variable calculated as 0, resulting in us spending a significant amount of time in these loops, likely leading to an ANR in system_server since at least the loop in clearUidTimes is executed while holding the BatteryStatsImpl lock. Fix the problem by skipping the initialization of cputimeinstate if there are no policy directories. Bug: 142352330 Bug: 178231152 Change-Id: I2ec1e8de0fe2a40ed100c8f14e6ca3f6d6285b82 --- libs/cputimeinstate/cputimeinstate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libs') diff --git a/libs/cputimeinstate/cputimeinstate.cpp b/libs/cputimeinstate/cputimeinstate.cpp index 4209dc5c6b..22103d5b8d 100644 --- a/libs/cputimeinstate/cputimeinstate.cpp +++ b/libs/cputimeinstate/cputimeinstate.cpp @@ -98,7 +98,7 @@ static bool initGlobals() { struct dirent **dirlist; const char basepath[] = "/sys/devices/system/cpu/cpufreq"; int ret = scandir(basepath, &dirlist, isPolicyFile, comparePolicyFiles); - if (ret == -1) return false; + if (ret == -1 || ret == 0) return false; gNPolicies = ret; std::vector policyFileNames; -- cgit v1.2.3-59-g8ed1b