[compiler-rt] [profile] Implement a non-mmap path when reading profile files from a non-local filesystem (PR #131177)

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 13 20:52:02 PDT 2025


================
@@ -258,6 +267,113 @@ COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
   return f;
 }
 
+// Return 1 (true) if the file descriptor Fd represents a file that is on a
+// local filesystem, otherwise return 0.
+static int is_local_filesystem(int Fd) {
+#if defined(_AIX)
+  struct statfs Vfs;
+  if (fstatfs(Fd, &Vfs) != 0) {
+    PROF_ERR("%s: fstatfs(%d) failed: %s\n", __func__, Fd, strerror(errno));
+    return 0;
+  }
+
+  int Ret;
+  size_t BufSize = 2048u;
+  char *Buf;
+  int Tries = 3;
+  while (Tries--) {
+    Buf = malloc(BufSize);
+    Ret = mntctl(MCTL_QUERY, BufSize, Buf);
+    if (Ret != 0)
+      break;
+    BufSize = *(unsigned int *)Buf;
+    free(Buf);
+  }
+
+  if (Ret != -1) {
+    // Look for the correct vmount entry.
+    char *CurObjPtr = Buf;
+    while (Ret--) {
+      struct vmount *Vp = (struct vmount *)CurObjPtr;
+      _Static_assert(sizeof(Vfs.f_fsid) == sizeof(Vp->vmt_fsid),
+                     "fsid length mismatch");
+      if (memcmp(&Vfs.f_fsid, &Vp->vmt_fsid, sizeof Vfs.f_fsid) == 0) {
+        free(Buf);
+        return (Vp->vmt_flags & MNT_REMOTE) == 0;
+      }
+      CurObjPtr += Vp->vmt_length;
+    }
+  }
+
+  free(Buf);
+  // There was an error in mntctl or vmount entry not found; "remote" is the
+  // conservative answer.
+#endif
+  return 0;
+}
+
+static int isMmapSafe(int Fd) {
+  if (getenv("LLVM_NO_MMAP")) // For testing purposes.
+    return 0;
+  (void)&is_local_filesystem; // a fake reference to satisfy -Wunused-function
+#ifdef _AIX
+  return is_local_filesystem(Fd);
+#endif
+  return 1;
+}
+
+COMPILER_RT_VISIBILITY void lprofGetFileContentBuffer(FILE *F, uint64_t Length,
+                                                      ManagedMemory *Buf) {
+  Buf->Status = MM_INVALID;
+
+  if (!F || isMmapSafe(fileno(F))) {
----------------
hubert-reinterpretcast wrote:

> Unless you insist otherwise, I'll just not check for `!F` and assume it's false because many existing functions that expect `F != 0` don't check for it,

I think the old code has undefined behaviour for the `!F` case and the new code is not worse in that aspect.

https://github.com/llvm/llvm-project/pull/131177


More information about the llvm-commits mailing list