[PATCH] Add FreeBSD support to sanitizers' procmaps facilities

Viktor Kutuzov vkutuzov at accesssoftek.com
Mon Mar 17 10:12:04 PDT 2014


Hi kcc, samsonov,

http://llvm-reviews.chandlerc.com/D3100

Files:
  lib/sanitizer_common/sanitizer_procmaps_linux.cc

Index: lib/sanitizer_common/sanitizer_procmaps_linux.cc
===================================================================
--- lib/sanitizer_common/sanitizer_procmaps_linux.cc
+++ lib/sanitizer_common/sanitizer_procmaps_linux.cc
@@ -16,16 +16,44 @@
 #include "sanitizer_placement_new.h"
 #include "sanitizer_procmaps.h"
 
+#if SANITIZER_FREEBSD
+#include <unistd.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#endif
+
 namespace __sanitizer {
 
 // Linker initialized.
 ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
 StaticSpinMutex MemoryMappingLayout::cache_lock_;  // Linker initialized.
 
+static void ReadProcMaps(char **data, uptr *data_len, uptr *data_size) {
+#if SANITIZER_FREEBSD
+  const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid() };
+  size_t Size = 0;
+  int Err = sysctl(Mib, 4, NULL, &Size, NULL, 0);
+  CHECK_EQ(Err, 0);
+  CHECK_GT(Size, 0);
+
+  size_t MmapedSize = Size * 4 / 3;
+  void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
+  Size = MmapedSize;
+  Err = sysctl(Mib, 4, VmMap, &Size, NULL, 0);
+  CHECK_EQ(Err, 0);
+
+  *data = (char*) VmMap;
+  *data_len = Size;
+  *data_size = MmapedSize;
+#else
+  *data_len = ReadFileToBuffer("/proc/self/maps", data,
+                               data_size, 1 << 26);
+#endif
+}
+
 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
-  proc_self_maps_.len =
-      ReadFileToBuffer("/proc/self/maps", &proc_self_maps_.data,
-                       &proc_self_maps_.mmaped_size, 1 << 26);
+  ReadProcMaps(&proc_self_maps_.data, &proc_self_maps_.len,
+               &proc_self_maps_.mmaped_size);
   if (cache_enabled) {
     if (proc_self_maps_.mmaped_size == 0) {
       LoadFromCache();
@@ -58,9 +86,9 @@
   // Don't invalidate the cache if the mappings are unavailable.
   ProcSelfMapsBuff old_proc_self_maps;
   old_proc_self_maps = cached_proc_self_maps_;
-  cached_proc_self_maps_.len =
-      ReadFileToBuffer("/proc/self/maps", &cached_proc_self_maps_.data,
-                       &cached_proc_self_maps_.mmaped_size, 1 << 26);
+  ReadProcMaps(&cached_proc_self_maps_.data,
+               &cached_proc_self_maps_.len,
+               &cached_proc_self_maps_.mmaped_size);
   if (cached_proc_self_maps_.mmaped_size == 0) {
     cached_proc_self_maps_ = old_proc_self_maps;
   } else {
@@ -78,6 +106,7 @@
   }
 }
 
+#if !SANITIZER_FREEBSD
 // Parse a hex value in str and update str.
 static uptr ParseHex(char **str) {
   uptr x = 0;
@@ -102,6 +131,7 @@
 static bool IsOneOf(char c, char c1, char c2) {
   return c == c1 || c == c2;
 }
+#endif
 
 static bool IsDecimal(char c) {
   return c >= '0' && c <= '9';
@@ -139,6 +169,30 @@
   if (!start) start = &dummy;
   if (!end) end = &dummy;
   if (!offset) offset = &dummy;
+  if (!protection) protection = &dummy;
+#if SANITIZER_FREEBSD
+  struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*) current_;
+
+  *start = (uptr) VmEntry->kve_start;
+  *end = (uptr) VmEntry->kve_end;
+  *offset = (uptr) VmEntry->kve_offset;
+
+  *protection =
+    (((VmEntry->kve_protection & KVME_PROT_READ) == 0) ?
+      0 : kProtectionRead) |
+    (((VmEntry->kve_protection & KVME_PROT_WRITE) == 0) ?
+      0 : kProtectionWrite) |
+    (((VmEntry->kve_protection & KVME_PROT_EXEC) == 0) ?
+      0 : kProtectionExecute);
+
+  if (filename != NULL && filename_size > 0) {
+    internal_memcpy(filename, VmEntry->kve_path,
+                    Min((size_t) filename_size, (size_t) PATH_MAX));
+    filename[filename_size - 1] = '\0';
+  }
+
+  current_ += VmEntry->kve_structsize;
+#else  // !SANITIZER_FREEBSD
   char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
   if (next_line == 0)
     next_line = last;
@@ -188,6 +242,7 @@
   if (filename && i < filename_size)
     filename[i] = 0;
   current_ = next_line + 1;
+#endif  // !SANITIZER_FREEBSD
   return true;
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3100.1.patch
Type: text/x-patch
Size: 3869 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140317/53bbd099/attachment.bin>


More information about the llvm-commits mailing list