[v1.01][X86] Do not assume that cach and topo structures are non-NULL, which may easily happen in VMs. Add protection against NULL fields in cpu structure

This commit is contained in:
Dr-Noob
2021-12-03 23:15:23 +01:00
parent 4a9bbef086
commit 4229e2c63b
2 changed files with 50 additions and 27 deletions

View File

@@ -278,6 +278,9 @@ struct cpuInfo* get_cpu_info() {
struct cpuInfo* cpu = emalloc(sizeof(struct cpuInfo));
struct features* feat = emalloc(sizeof(struct features));
cpu->feat = feat;
cpu->peak_performance = -1;
cpu->topo = NULL;
cpu->cach = NULL;
bool *ptr = &(feat->AES);
for(uint32_t i = 0; i < sizeof(struct features)/sizeof(bool); i++, ptr++) {
@@ -386,15 +389,20 @@ struct cpuInfo* get_cpu_info() {
cpu->topology_extensions = (ecx >> 22) & 1;
}
// If any field of the struct is NULL,
// return inmideately, as further functions
// require valid fields (cach, topo, etc)
cpu->arch = get_cpu_uarch(cpu);
cpu->freq = get_frequency_info(cpu);
cpu->cach = get_cache_info(cpu);
if(cpu->cach == NULL) return cpu;
cpu->topo = get_topology_info(cpu, cpu->cach);
if(cpu->topo == NULL) return cpu;
cpu->peak_performance = get_peak_performance(cpu, cpu->topo, get_freq(cpu->freq), accurate_pp());
if(cpu->cach == NULL || cpu->topo == NULL) {
return NULL;
}
return cpu;
}