blob: 157e3019e876e81d0b54478f947d2b5ec0ca4521 [file] [log] [blame]
Viktor Kutuzova37ad092014-08-06 10:16:52 +00001//===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Information about the process mappings (FreeBSD-specific parts).
11//===----------------------------------------------------------------------===//
12
13#include "sanitizer_platform.h"
14#if SANITIZER_FREEBSD
15#include "sanitizer_common.h"
16#include "sanitizer_freebsd.h"
17#include "sanitizer_procmaps.h"
18
19#include <unistd.h>
20#include <sys/sysctl.h>
21#include <sys/user.h>
22
23namespace __sanitizer {
24
25void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
26 const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid() };
27 size_t Size = 0;
28 int Err = sysctl(Mib, 4, NULL, &Size, NULL, 0);
29 CHECK_EQ(Err, 0);
30 CHECK_GT(Size, 0);
31
32 size_t MmapedSize = Size * 4 / 3;
33 void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
34 Size = MmapedSize;
35 Err = sysctl(Mib, 4, VmMap, &Size, NULL, 0);
36 CHECK_EQ(Err, 0);
37
38 proc_maps->data = (char*)VmMap;
39 proc_maps->mmaped_size = MmapedSize;
40 proc_maps->len = Size;
41}
42
43bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
44 char filename[], uptr filename_size,
45 uptr *protection) {
46 char *last = proc_self_maps_.data + proc_self_maps_.len;
47 if (current_ >= last) return false;
48 uptr dummy;
49 if (!start) start = &dummy;
50 if (!end) end = &dummy;
51 if (!offset) offset = &dummy;
52 if (!protection) protection = &dummy;
53 struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*)current_;
54
55 *start = (uptr)VmEntry->kve_start;
56 *end = (uptr)VmEntry->kve_end;
57 *offset = (uptr)VmEntry->kve_offset;
58
59 *protection = 0;
60 if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
61 *protection |= kProtectionRead;
62 if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
63 *protection |= kProtectionWrite;
64 if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
65 *protection |= kProtectionExecute;
66
67 if (filename != NULL && filename_size > 0) {
68 internal_snprintf(filename,
69 Min(filename_size, (uptr)PATH_MAX),
70 "%s", VmEntry->kve_path);
71 }
72
73 current_ += VmEntry->kve_structsize;
74
75 return true;
76}
77
78} // namespace __sanitizer
79
80#endif // SANITIZER_FREEBSD