xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_solaris.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 "sanitizer_common.h"
15 #include "sanitizer_procmaps.h"
16 
17 // Before Solaris 11.4, <procfs.h> doesn't work in a largefile environment.
18 #undef _FILE_OFFSET_BITS
19 #include <procfs.h>
20 #include <limits.h>
21 
22 namespace __sanitizer {
23 
24 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
25   if (!ReadFileToBuffer("/proc/self/xmap", &proc_maps->data,
26                         &proc_maps->mmaped_size, &proc_maps->len)) {
27     proc_maps->data = nullptr;
28     proc_maps->mmaped_size = 0;
29     proc_maps->len = 0;
30   }
31 }
32 
33 bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
34   if (Error()) return false; // simulate empty maps
35   char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
36   if (data_.current >= last) return false;
37 
38   prxmap_t *xmapentry = (prxmap_t*)data_.current;
39 
40   segment->start = (uptr)xmapentry->pr_vaddr;
41   segment->end = (uptr)(xmapentry->pr_vaddr + xmapentry->pr_size);
42   segment->offset = (uptr)xmapentry->pr_offset;
43 
44   segment->protection = 0;
45   if ((xmapentry->pr_mflags & MA_READ) != 0)
46     segment->protection |= kProtectionRead;
47   if ((xmapentry->pr_mflags & MA_WRITE) != 0)
48     segment->protection |= kProtectionWrite;
49   if ((xmapentry->pr_mflags & MA_EXEC) != 0)
50     segment->protection |= kProtectionExecute;
51 
52   if (segment->filename != NULL && segment->filename_size > 0) {
53     char proc_path[PATH_MAX + 1];
54 
55     internal_snprintf(proc_path, sizeof(proc_path), "/proc/self/path/%s",
56                       xmapentry->pr_mapname);
57     internal_readlink(proc_path, segment->filename, segment->filename_size);
58   }
59 
60   data_.current += sizeof(prxmap_t);
61 
62   return true;
63 }
64 
65 }  // namespace __sanitizer
66 
67 #endif  // SANITIZER_SOLARIS
68