[PGO] Don't call calloc(0, sizeof(ValueProfNode *))

A malloc implementation may return a pointer to some allocated space.  It is
undefined for libclang_rt.profile- to access the object - which actually happens
in instrumentTargetValueImpl, where ValueCounters[CounterIndex] may access a
ValueProfNode (from another allocated object) and crashes when the code accesses
the object referenced by CurVNode->Next.
This commit is contained in:
Fangrui Song 2020-07-22 18:46:57 -07:00
parent 9e4ab439c2
commit 99ad956fda
1 changed files with 2 additions and 0 deletions

View File

@ -93,6 +93,8 @@ static int allocateValueProfileCounters(__llvm_profile_data *Data) {
for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
NumVSites += Data->NumValueSites[VKI];
if (NumVSites == 0)
return 0;
ValueProfNode **Mem =
(ValueProfNode **)calloc(NumVSites, sizeof(ValueProfNode *));
if (!Mem)