[compiler-rt] 99ad956 - [PGO] Don't call calloc(0, sizeof(ValueProfNode *))

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 18:49:32 PDT 2020


Author: Fangrui Song
Date: 2020-07-22T18:49:25-07:00
New Revision: 99ad956fdaee5398fdcf46fa49cb433cf52dc461

URL: https://github.com/llvm/llvm-project/commit/99ad956fdaee5398fdcf46fa49cb433cf52dc461
DIFF: https://github.com/llvm/llvm-project/commit/99ad956fdaee5398fdcf46fa49cb433cf52dc461.diff

LOG: [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.

Added: 
    

Modified: 
    compiler-rt/lib/profile/InstrProfilingValue.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/profile/InstrProfilingValue.c b/compiler-rt/lib/profile/InstrProfilingValue.c
index fd53cac3dff3..29b9e628a9c9 100644
--- a/compiler-rt/lib/profile/InstrProfilingValue.c
+++ b/compiler-rt/lib/profile/InstrProfilingValue.c
@@ -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)


        


More information about the llvm-commits mailing list