[PATCH] D61326: Fixes for builds that require strict X/Open and POSIX compatiblity

David Tenty via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 10:58:07 PDT 2019


daltenty created this revision.
daltenty added reviewers: hubert.reinterpretcast, xingxue, andusy.
daltenty added a project: LLVM.
Herald added subscribers: llvm-commits, kristina, hiraditya.

- use alternative to MAP_ANONYMOUS for allocating mapped memory if it isn't available
- Use strtok_r instead of strsep as part of getting program path
- Don't try to find the width of a terminal using "struct winsize" and TIOCGWINSZ on POSIX builds. These are defined under POSIX (even though some platforms make them available when they shouldn't), so just check if we are doing a X/Open or POSIX compliant build first.




Repository:
  rL LLVM

https://reviews.llvm.org/D61326

Files:
  llvm/lib/Support/Unix/Memory.inc
  llvm/lib/Support/Unix/Path.inc
  llvm/lib/Support/Unix/Process.inc


Index: llvm/lib/Support/Unix/Process.inc
===================================================================
--- llvm/lib/Support/Unix/Process.inc
+++ llvm/lib/Support/Unix/Process.inc
@@ -288,7 +288,8 @@
 
   unsigned Columns = 0;
 
-#if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H)
+#if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_TERMIOS_H) \
+  && !(defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE))
   // Try to determine the width of the terminal.
   struct winsize ws;
   if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -157,11 +157,14 @@
   s = pv = strdup(pv);
   if (!pv)
     return nullptr;
-  while ((t = strsep(&s, ":")) != nullptr) {
-    if (test_dir(ret, t, bin) == 0) {
-      free(pv);
-      return ret;
-    }
+  char *state;
+  if ((t = strtok_r(s,":",&state)) != nullptr) {
+    do {
+      if (test_dir(ret, t, bin) == 0) {
+        free(pv);
+        return ret;
+      }
+    } while((t = strtok_r(nullptr,":",&state)) != nullptr);
   }
   free(pv);
   return nullptr;
Index: llvm/lib/Support/Unix/Memory.inc
===================================================================
--- llvm/lib/Support/Unix/Memory.inc
+++ llvm/lib/Support/Unix/Memory.inc
@@ -91,13 +91,26 @@
   if (NumBytes == 0)
     return MemoryBlock();
 
-  int fd = -1;
+  // on platforms that have it we can use MAP_ANONYMOUS to get a memory mapped
+  // page without file backing, but we need a fallback by opening /dev/zero
+  // for strictly POSIX platforms instead
+  int fd =
+#if defined(MAP_ANONYMOUS) || defined(MAP_ANON)
+   -1;
+#else
+  open("/dev/zero", O_RDWR);
+  if (fd ==-1) {
+    EC = std::error_code(errno, std::generic_category());
+    return MemoryBlock();
+  }
+#endif
+  
 
-  int MMFlags = MAP_PRIVATE |
+  int MMFlags = MAP_PRIVATE 
 #ifdef MAP_ANONYMOUS
-  MAP_ANONYMOUS
-#else
-  MAP_ANON
+  | MAP_ANONYMOUS
+#elif MAP_ANON
+  | MAP_ANON
 #endif
   ; // Ends statement above
 
@@ -122,9 +135,17 @@
       return allocateMappedMemory(NumBytes, nullptr, PFlags, EC);
 
     EC = std::error_code(errno, std::generic_category());
+#if !defined(MAP_ANONYMOUS) && !defined(MAP_ANON) 
+    close(fd);
+#endif 
     return MemoryBlock();
   }
 
+#if !defined(MAP_ANONYMOUS) && !defined(MAP_ANON) 
+  close(fd);
+#endif 
+
+
   MemoryBlock Result;
   Result.Address = Addr;
   Result.Size = NumBytes;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61326.197371.patch
Type: text/x-patch
Size: 2495 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190430/ea196326/attachment.bin>


More information about the llvm-commits mailing list