Skip to content

Commit

Permalink
x86: fix a crash when adding both legacy AMD and CPUID 0x04 caches
Browse files Browse the repository at this point in the history
The old code increased numcaches in the second case
but it added additional caches at the beginning of the array.
Uninitialized caches in the array caused a divide by zero
(cache->nbthreads_sharing) when used later.

Only occurs if the CPUID vendor isn't recognized
(neither Intel, nor AMD, nor Zhaoxin) or in case
of clang 4.0 bug on FreeBSD11.1 (#282).

That's also why the code was crashing on Zhaoxin
instead of just reporting wrong topology (#279).

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Jan 14, 2018
1 parent c28696e commit a6f013c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion hwloc/topology-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ static void look_proc(struct hwloc_backend *backend, struct procinfo *infos, uns
if (cpuid_type != intel && cpuid_type != zhaoxin && has_topoext(features)) {
unsigned apic_id, node_id, nodes_per_proc;

/* the code below doesn't want any other cache yet */
assert(!infos->numcaches);

eax = 0x8000001e;
cpuid_or_from_dump(&eax, &ebx, &ecx, &edx, src_cpuiddump);
infos->apicid = apic_id = eax;
Expand Down Expand Up @@ -476,6 +479,8 @@ static void look_proc(struct hwloc_backend *backend, struct procinfo *infos, uns
unsigned max_nbthreads;
unsigned level;

unsigned oldnumcaches = infos->numcaches; /* in case we got caches above */

for (cachenum = 0; ; cachenum++) {
eax = 0x04;
ecx = cachenum;
Expand All @@ -501,7 +506,8 @@ static void look_proc(struct hwloc_backend *backend, struct procinfo *infos, uns
}
}

cache = infos->cache = malloc(infos->numcaches * sizeof(*infos->cache));
infos->cache = realloc(infos->cache, infos->numcaches * sizeof(*infos->cache));
cache = &infos->cache[oldnumcaches];

for (cachenum = 0; ; cachenum++) {
unsigned long linesize, linepart, ways, sets;
Expand Down

0 comments on commit a6f013c

Please sign in to comment.