diff options
author | epenner@chromium.org <epenner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-16 18:15:50 +0000 |
---|---|---|
committer | epenner@chromium.org <epenner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-16 18:15:50 +0000 |
commit | 0cea355a02d0bf07f6f16f8732b5922b78598e26 (patch) | |
tree | 01f61e1eff3f085e43538da2af9ce7766d10aa53 /base/sys_info_android.cc | |
parent | 94c30ca27a105548898b46d4d9ff07c998b92282 (diff) | |
download | chromium_src-0cea355a02d0bf07f6f16f8732b5922b78598e26.zip chromium_src-0cea355a02d0bf07f6f16f8732b5922b78598e26.tar.gz chromium_src-0cea355a02d0bf07f6f16f8732b5922b78598e26.tar.bz2 |
This fixes the overflows identified in the parsing
function, and adds the dalvik-heap-limit.
BUG=176201
NOTRY=true
No try since it has passed many times and is taking days in the CQ.
Review URL: https://chromiumcodereview.appspot.com/12223064
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@182986 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/sys_info_android.cc')
-rw-r--r-- | base/sys_info_android.cc | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/base/sys_info_android.cc b/base/sys_info_android.cc index 5380170..5d4eff0 100644 --- a/base/sys_info_android.cc +++ b/base/sys_info_android.cc @@ -44,39 +44,68 @@ void ParseOSVersionNumbers(const char* os_version_str, *bugfix_version = kDefaultAndroidBugfixVersion; } -int ParseHeapSize(const base::StringPiece& str) { +// Parses a system property (specified with unit 'k','m' or 'g'). +// Returns a value in bytes. +// Returns -1 if the string could not be parsed. +int64 ParseSystemPropertyBytes(const base::StringPiece& str) { const int64 KB = 1024; const int64 MB = 1024 * KB; const int64 GB = 1024 * MB; - CHECK_GT(str.size(), 0u); - int64 factor = 1; + if (str.size() == 0u) + return -1; + int64 unit_multiplier = 1; size_t length = str.size(); if (str[length - 1] == 'k') { - factor = KB; + unit_multiplier = KB; length--; } else if (str[length - 1] == 'm') { - factor = MB; + unit_multiplier = MB; length--; } else if (str[length - 1] == 'g') { - factor = GB; + unit_multiplier = GB; length--; - } else { - CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); } int64 result = 0; bool parsed = base::StringToInt64(str.substr(0, length), &result); - CHECK(parsed); - result = result * factor / MB; - // dalvik.vm.heapsize property is writable by user, - // truncate it to reasonable value to avoid overflows later. - result = std::min<int64>(std::max<int64>(32, result), 1024); - return static_cast<int>(result); + bool negative = result <= 0; + bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier; + if (!parsed || negative || overflow) + return -1; + return result * unit_multiplier; } int GetDalvikHeapSizeMB() { char heap_size_str[PROP_VALUE_MAX]; __system_property_get("dalvik.vm.heapsize", heap_size_str); - return ParseHeapSize(heap_size_str); + // dalvik.vm.heapsize property is writable by a root user. + // Clamp it to reasonable range as a sanity check, + // a typical android device will never have less than 48MB. + const int64 MB = 1024 * 1024; + int64 result = ParseSystemPropertyBytes(heap_size_str); + if (result == -1) { + // We should consider not exposing these values if they are not reliable. + LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str; + result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3; + } + result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB; + return static_cast<int>(result); +} + +int GetDalvikHeapGrowthLimitMB() { + char heap_size_str[PROP_VALUE_MAX]; + __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str); + // dalvik.vm.heapgrowthlimit property is writable by a root user. + // Clamp it to reasonable range as a sanity check, + // a typical android device will never have less than 24MB. + const int64 MB = 1024 * 1024; + int64 result = ParseSystemPropertyBytes(heap_size_str); + if (result == -1) { + // We should consider not exposing these values if they are not reliable. + LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str; + result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6; + } + result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB; + return static_cast<int>(result); } } // anonymous namespace @@ -122,4 +151,10 @@ int SysInfo::DalvikHeapSizeMB() { return heap_size; } +int SysInfo::DalvikHeapGrowthLimitMB() { + static int heap_growth_limit = GetDalvikHeapGrowthLimitMB(); + return heap_growth_limit; +} + + } // namespace base |