13cab2bb3Spatrick //===-- sanitizer_procmaps_solaris.cpp ------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // Information about the process mappings (Solaris-specific parts).
103cab2bb3Spatrick //===----------------------------------------------------------------------===//
113cab2bb3Spatrick
12d89ec533Spatrick // Before Solaris 11.4, <procfs.h> doesn't work in a largefile environment.
13d89ec533Spatrick #undef _FILE_OFFSET_BITS
143cab2bb3Spatrick #include "sanitizer_platform.h"
153cab2bb3Spatrick #if SANITIZER_SOLARIS
16*810390e3Srobert # include <fcntl.h>
17*810390e3Srobert # include <limits.h>
18*810390e3Srobert # include <procfs.h>
19*810390e3Srobert
203cab2bb3Spatrick # include "sanitizer_common.h"
213cab2bb3Spatrick # include "sanitizer_procmaps.h"
223cab2bb3Spatrick
233cab2bb3Spatrick namespace __sanitizer {
243cab2bb3Spatrick
ReadProcMaps(ProcSelfMapsBuff * proc_maps)253cab2bb3Spatrick void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
26*810390e3Srobert uptr fd = internal_open("/proc/self/xmap", O_RDONLY);
27*810390e3Srobert CHECK_NE(fd, -1);
28*810390e3Srobert uptr Size = internal_filesize(fd);
29*810390e3Srobert CHECK_GT(Size, 0);
30*810390e3Srobert
31*810390e3Srobert // Allow for additional entries by following mmap.
32*810390e3Srobert size_t MmapedSize = Size * 4 / 3;
33*810390e3Srobert void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
34*810390e3Srobert Size = internal_read(fd, VmMap, MmapedSize);
35*810390e3Srobert CHECK_NE(Size, -1);
36*810390e3Srobert internal_close(fd);
37*810390e3Srobert proc_maps->data = (char *)VmMap;
38*810390e3Srobert proc_maps->mmaped_size = MmapedSize;
39*810390e3Srobert proc_maps->len = Size;
403cab2bb3Spatrick }
413cab2bb3Spatrick
Next(MemoryMappedSegment * segment)423cab2bb3Spatrick bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
433cab2bb3Spatrick if (Error()) return false; // simulate empty maps
443cab2bb3Spatrick char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
453cab2bb3Spatrick if (data_.current >= last) return false;
463cab2bb3Spatrick
47d89ec533Spatrick prxmap_t *xmapentry =
48d89ec533Spatrick const_cast<prxmap_t *>(reinterpret_cast<const prxmap_t *>(data_.current));
493cab2bb3Spatrick
503cab2bb3Spatrick segment->start = (uptr)xmapentry->pr_vaddr;
513cab2bb3Spatrick segment->end = (uptr)(xmapentry->pr_vaddr + xmapentry->pr_size);
523cab2bb3Spatrick segment->offset = (uptr)xmapentry->pr_offset;
533cab2bb3Spatrick
543cab2bb3Spatrick segment->protection = 0;
553cab2bb3Spatrick if ((xmapentry->pr_mflags & MA_READ) != 0)
563cab2bb3Spatrick segment->protection |= kProtectionRead;
573cab2bb3Spatrick if ((xmapentry->pr_mflags & MA_WRITE) != 0)
583cab2bb3Spatrick segment->protection |= kProtectionWrite;
593cab2bb3Spatrick if ((xmapentry->pr_mflags & MA_EXEC) != 0)
603cab2bb3Spatrick segment->protection |= kProtectionExecute;
61*810390e3Srobert if ((xmapentry->pr_mflags & MA_SHARED) != 0)
62*810390e3Srobert segment->protection |= kProtectionShared;
633cab2bb3Spatrick
643cab2bb3Spatrick if (segment->filename != NULL && segment->filename_size > 0) {
653cab2bb3Spatrick char proc_path[PATH_MAX + 1];
663cab2bb3Spatrick
67*810390e3Srobert // Avoid unnecessary readlink on unnamed entires.
68*810390e3Srobert if (xmapentry->pr_mapname[0] == '\0')
69*810390e3Srobert segment->filename[0] = '\0';
70*810390e3Srobert else {
713cab2bb3Spatrick internal_snprintf(proc_path, sizeof(proc_path), "/proc/self/path/%s",
723cab2bb3Spatrick xmapentry->pr_mapname);
73*810390e3Srobert ssize_t sz = internal_readlink(proc_path, segment->filename,
74*810390e3Srobert segment->filename_size - 1);
75*810390e3Srobert
76*810390e3Srobert // If readlink failed, the map is anonymous.
77*810390e3Srobert if (sz == -1)
78*810390e3Srobert segment->filename[0] = '\0';
79*810390e3Srobert else if ((size_t)sz < segment->filename_size)
80*810390e3Srobert // readlink doesn't NUL-terminate.
81*810390e3Srobert segment->filename[sz] = '\0';
82*810390e3Srobert }
833cab2bb3Spatrick }
843cab2bb3Spatrick
853cab2bb3Spatrick data_.current += sizeof(prxmap_t);
863cab2bb3Spatrick
873cab2bb3Spatrick return true;
883cab2bb3Spatrick }
893cab2bb3Spatrick
903cab2bb3Spatrick } // namespace __sanitizer
913cab2bb3Spatrick
923cab2bb3Spatrick #endif // SANITIZER_SOLARIS
93