[compiler-rt] r277507 - [Profile] track ownership of filename pattern string

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 2 12:34:01 PDT 2016


Author: davidxl
Date: Tue Aug  2 14:34:00 2016
New Revision: 277507

URL: http://llvm.org/viewvc/llvm-project?rev=277507&view=rev
Log:
[Profile] track ownership of filename pattern string

Make sure runtime copy and owns the string when passed
in from external users of runtime API.



Modified:
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=277507&r1=277506&r2=277507&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Tue Aug  2 14:34:00 2016
@@ -63,6 +63,9 @@ static const char *getPNSStr(ProfileName
 typedef struct lprofFilename {
   /* File name string possibly with %p or %h specifiers. */
   const char *FilenamePat;
+  /* A flag indicating if FilenamePat's memory is allocated
+   * by runtime. */
+  unsigned OwnsFilenamePat;
   const char *ProfilePathPrefix;
   char PidChars[MAX_PID_SIZE];
   char Hostname[COMPILER_RT_MAX_HOSTLEN];
@@ -79,7 +82,7 @@ typedef struct lprofFilename {
   ProfileNameSpecifier PNS;
 } lprofFilename;
 
-lprofFilename lprofCurFilename = {0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
+lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0}, 0, 0, 0, PNS_unknown};
 
 int getpid(void);
 static int getCurFilenameLength();
@@ -250,6 +253,9 @@ static void truncateCurrentFile(void) {
 
 static const char *DefaultProfileName = "default.profraw";
 static void resetFilenameToDefault(void) {
+  if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
+    free((void *)lprofCurFilename.FilenamePat);
+  }
   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
   lprofCurFilename.FilenamePat = DefaultProfileName;
   lprofCurFilename.PNS = PNS_default;
@@ -265,7 +271,8 @@ static int containsMergeSpecifier(const
 
 /* Parses the pattern string \p FilenamePat and stores the result to
  * lprofcurFilename structure. */
-static int parseFilenamePattern(const char *FilenamePat) {
+static int parseFilenamePattern(const char *FilenamePat,
+                                unsigned CopyFilenamePat) {
   int NumPids = 0, NumHosts = 0, I;
   char *PidChars = &lprofCurFilename.PidChars[0];
   char *Hostname = &lprofCurFilename.Hostname[0];
@@ -276,7 +283,16 @@ static int parseFilenamePattern(const ch
     free((void *)lprofCurFilename.ProfilePathPrefix);
   memset(&lprofCurFilename, 0, sizeof(lprofCurFilename));
 
-  lprofCurFilename.FilenamePat = FilenamePat;
+  if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
+    free((void *)lprofCurFilename.FilenamePat);
+  }
+
+  if (!CopyFilenamePat)
+    lprofCurFilename.FilenamePat = FilenamePat;
+  else {
+    lprofCurFilename.FilenamePat = strdup(FilenamePat);
+    lprofCurFilename.OwnsFilenamePat = 1;
+  }
   /* Check the filename for "%p", which indicates a pid-substitution. */
   for (I = 0; FilenamePat[I]; ++I)
     if (FilenamePat[I] == '%') {
@@ -319,7 +335,8 @@ static int parseFilenamePattern(const ch
 }
 
 static void parseAndSetFilename(const char *FilenamePat,
-                                ProfileNameSpecifier PNS) {
+                                ProfileNameSpecifier PNS,
+                                unsigned CopyFilenamePat) {
 
   const char *OldFilenamePat = lprofCurFilename.FilenamePat;
   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
@@ -336,7 +353,7 @@ static void parseAndSetFilename(const ch
   }
 
   /* When PNS >= OldPNS, the last one wins. */
-  if (!FilenamePat || parseFilenamePattern(FilenamePat))
+  if (!FilenamePat || parseFilenamePattern(FilenamePat, CopyFilenamePat))
     resetFilenameToDefault();
   lprofCurFilename.PNS = PNS;
 
@@ -483,7 +500,7 @@ void __llvm_profile_initialize_file(void
     PNS = PNS_default;
   }
 
-  parseAndSetFilename(SelectedPat, PNS);
+  parseAndSetFilename(SelectedPat, PNS, 0);
 }
 
 /* This API is directly called by the user application code. It has the
@@ -492,7 +509,7 @@ void __llvm_profile_initialize_file(void
  */
 COMPILER_RT_VISIBILITY
 void __llvm_profile_set_filename(const char *FilenamePat) {
-  parseAndSetFilename(FilenamePat, PNS_runtime_api);
+  parseAndSetFilename(FilenamePat, PNS_runtime_api, 1);
 }
 
 /* The public API for writing profile data into the file with name




More information about the llvm-commits mailing list