1 //===-- sanitizer_procmaps_solaris.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Information about the process mappings (Solaris-specific parts).
10 //===----------------------------------------------------------------------===//
11 
12 #include "sanitizer_platform.h"
13 #if SANITIZER_SOLARIS
14 #  include <fcntl.h>
15 #  include <limits.h>
16 #  include <procfs.h>
17 
18 #  include "sanitizer_common.h"
19 #  include "sanitizer_procmaps.h"
20 
21 namespace __sanitizer {
22 
23 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
24   uptr fd = internal_open("/proc/self/xmap", O_RDONLY);
25   CHECK_NE(fd, -1);
26   uptr Size = internal_filesize(fd);
27   CHECK_GT(Size, 0);
28 
29   // Allow for additional entries by following mmap.
30   size_t MmapedSize = Size * 4 / 3;
31   void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
32   Size = internal_read(fd, VmMap, MmapedSize);
33   CHECK_NE(Size, -1);
34   internal_close(fd);
35   proc_maps->data = (char *)VmMap;
36   proc_maps->mmaped_size = MmapedSize;
37   proc_maps->len = Size;
38 }
39 
40 bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
41   if (Error()) return false; // simulate empty maps
42   char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
43   if (data_.current >= last) return false;
44 
45   prxmap_t *xmapentry =
46       const_cast<prxmap_t *>(reinterpret_cast<const prxmap_t *>(data_.current));
47 
48   segment->start = (uptr)xmapentry->pr_vaddr;
49   segment->end = (uptr)(xmapentry->pr_vaddr + xmapentry->pr_size);
50   segment->offset = (uptr)xmapentry->pr_offset;
51 
52   segment->protection = 0;
53   if ((xmapentry->pr_mflags & MA_READ) != 0)
54     segment->protection |= kProtectionRead;
55   if ((xmapentry->pr_mflags & MA_WRITE) != 0)
56     segment->protection |= kProtectionWrite;
57   if ((xmapentry->pr_mflags & MA_EXEC) != 0)
58     segment->protection |= kProtectionExecute;
59   if ((xmapentry->pr_mflags & MA_SHARED) != 0)
60     segment->protection |= kProtectionShared;
61 
62   if (segment->filename != NULL && segment->filename_size > 0) {
63     char proc_path[PATH_MAX + 1];
64 
65     // Avoid unnecessary readlink on unnamed entires.
66     if (xmapentry->pr_mapname[0] == '\0')
67       segment->filename[0] = '\0';
68     else {
69       internal_snprintf(proc_path, sizeof(proc_path), "/proc/self/path/%s",
70                         xmapentry->pr_mapname);
71       ssize_t sz = internal_readlink(proc_path, segment->filename,
72                                      segment->filename_size - 1);
73 
74       // If readlink failed, the map is anonymous.
75       if (sz == -1)
76         segment->filename[0] = '\0';
77       else if ((size_t)sz < segment->filename_size)
78         // readlink doesn't NUL-terminate.
79         segment->filename[sz] = '\0';
80     }
81   }
82 
83   data_.current += sizeof(prxmap_t);
84 
85   return true;
86 }
87 
88 }  // namespace __sanitizer
89 
90 #endif  // SANITIZER_SOLARIS
91