[compiler-rt] [compiler-rt][profile] Duplicate filename in `parseAndSetFilename` if exists (PR #110264)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 03:14:03 PDT 2024


https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/110264

>From fc19a5d76a1dbaeec7d202d0d4766d91d7eed97f Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 27 Sep 2024 15:10:44 +0200
Subject: [PATCH 1/2] [compiler-rt][profile] Duplicate filename in
 `parseAndSetFilename` if exists

Duplicate `lprofCurFilename.FilenamePat` if the filename exists in
`parseAndSetFilename`, as the original path name buffer may have
been freed by the time it is used.

Fixes: https://github.com/llvm/llvm-project/issues/102109.
---
 compiler-rt/lib/profile/InstrProfilingFile.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 64ed8b62e9eba7..8243a7a5ea3ca9 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -894,7 +894,9 @@ static void parseAndSetFilename(const char *FilenamePat,
                                 ProfileNameSpecifier PNS,
                                 unsigned CopyFilenamePat) {
 
-  const char *OldFilenamePat = lprofCurFilename.FilenamePat;
+  char *OldFilenamePat = lprofCurFilename.FilenamePat
+                             ? strdup(lprofCurFilename.FilenamePat)
+                             : NULL;
   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
 
   /* The old profile name specifier takes precedence over the old one. */
@@ -923,6 +925,9 @@ static void parseAndSetFilename(const char *FilenamePat,
       PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
                 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
                 getPNSStr(PNS));
+
+    free(OldFilenamePat);
+    OldFilenamePat = NULL;
   }
 
   truncateCurrentFile();

>From a9aeefc1aa81a288040017cf4348047aaba958c6 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 4 Oct 2024 12:12:37 +0200
Subject: [PATCH 2/2] !fixup free in early returns

---
 compiler-rt/lib/profile/InstrProfilingFile.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 8243a7a5ea3ca9..9f1d7a7f1b419b 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -900,14 +900,21 @@ static void parseAndSetFilename(const char *FilenamePat,
   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
 
   /* The old profile name specifier takes precedence over the old one. */
-  if (PNS < OldPNS)
+  if (PNS < OldPNS) {
+    if (OldFilenamePat) {
+      free(OldFilenamePat);
+      OldFilenamePat = NULL;
+    }
     return;
+  }
 
   if (!FilenamePat)
     FilenamePat = DefaultProfileName;
 
   if (OldFilenamePat && !strcmp(OldFilenamePat, FilenamePat)) {
     lprofCurFilename.PNS = PNS;
+    free(OldFilenamePat);
+    OldFilenamePat = NULL;
     return;
   }
 



More information about the llvm-commits mailing list