Skip to content

Commit

Permalink
Null check each cpuinfo_get_processor call.
Browse files Browse the repository at this point in the history
Add null checks in cases the cpuinfo could be faked and therefore not consistent. For example, the number of processors would be faked to be 8 but the rest of the processor info could be missing.

PiperOrigin-RevId: 466965492
  • Loading branch information
Ruy Contributors authored and copybara-github committed Aug 11, 2022
1 parent 97ebb72 commit 83dd6c2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ruy/cpuinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void QueryCacheParams(CpuCacheParams* cache_params) {
int local_cache_size = 0;
int last_level_cache_size = 0;
const cpuinfo_processor* processor = cpuinfo_get_processor(i);
if (!processor) continue;
// Loop over cache levels. Ignoring L4 for now: it seems that in CPUs that
// have L4, we would still prefer to stay in lower-latency L3.
for (const cpuinfo_cache* cache :
Expand All @@ -56,14 +57,16 @@ void QueryCacheParams(CpuCacheParams* cache_params) {
continue; // continue, not break, it is possible to have L1+L3 but no
// L2.
}
if (!cache->processor_count) {
if (!cache->processor_count || !cache->processor_start) {
continue; // crashes from Chrome on Android suggests that might happen?
}
const bool is_local =
cpuinfo_get_processor(cache->processor_start)->core ==
cpuinfo_get_processor(cache->processor_start +
cache->processor_count - 1)
->core;
const cpuinfo_processor* start_processor =
cpuinfo_get_processor(cache->processor_start);
const cpuinfo_processor* end_processor = cpuinfo_get_processor(
cache->processor_start + cache->processor_count - 1);
if (!start_processor || !end_processor) continue;
const bool is_local = start_processor->core == end_processor->core;

if (is_local) {
local_cache_size = cache->size;
}
Expand Down

0 comments on commit 83dd6c2

Please sign in to comment.