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// Before Solaris 11.4, <procfs.h> doesn't work in a largefile environment.
13#undef _FILE_OFFSET_BITS
14#include "sanitizer_platform.h"
15#if SANITIZER_SOLARIS
16#include "sanitizer_common.h"
17#include "sanitizer_procmaps.h"
18
19#include <procfs.h>
20#include <limits.h>
21
22namespace __sanitizer {
23
24void 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
33bool 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 =
39      const_cast<prxmap_t *>(reinterpret_cast<const prxmap_t *>(data_.current));
40
41  segment->start = (uptr)xmapentry->pr_vaddr;
42  segment->end = (uptr)(xmapentry->pr_vaddr + xmapentry->pr_size);
43  segment->offset = (uptr)xmapentry->pr_offset;
44
45  segment->protection = 0;
46  if ((xmapentry->pr_mflags & MA_READ) != 0)
47    segment->protection |= kProtectionRead;
48  if ((xmapentry->pr_mflags & MA_WRITE) != 0)
49    segment->protection |= kProtectionWrite;
50  if ((xmapentry->pr_mflags & MA_EXEC) != 0)
51    segment->protection |= kProtectionExecute;
52
53  if (segment->filename != NULL && segment->filename_size > 0) {
54    char proc_path[PATH_MAX + 1];
55
56    internal_snprintf(proc_path, sizeof(proc_path), "/proc/self/path/%s",
57                      xmapentry->pr_mapname);
58    ssize_t sz = internal_readlink(proc_path, segment->filename,
59                                   segment->filename_size - 1);
60
61    // If readlink failed, the map is anonymous.
62    if (sz == -1) {
63      segment->filename[0] = '\0';
64    } else if ((size_t)sz < segment->filename_size)
65      // readlink doesn't NUL-terminate.
66      segment->filename[sz] = '\0';
67  }
68
69  data_.current += sizeof(prxmap_t);
70
71  return true;
72}
73
74}  // namespace __sanitizer
75
76#endif  // SANITIZER_SOLARIS
77