annotate libsanitizer/sanitizer_common/sanitizer_procmaps_freebsd.cc @ 119:9d1ff3ae1824

fix conv1 example
author kono
date Thu, 08 Mar 2018 12:20:02 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // Information about the process mappings (FreeBSD and NetBSD-specific parts).
kono
parents:
diff changeset
9 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 #include "sanitizer_platform.h"
kono
parents:
diff changeset
12 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
kono
parents:
diff changeset
13 #include "sanitizer_common.h"
kono
parents:
diff changeset
14 #if SANITIZER_FREEBSD
kono
parents:
diff changeset
15 #include "sanitizer_freebsd.h"
kono
parents:
diff changeset
16 #endif
kono
parents:
diff changeset
17 #include "sanitizer_procmaps.h"
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 #include <unistd.h>
kono
parents:
diff changeset
20 #include <sys/sysctl.h>
kono
parents:
diff changeset
21 #if SANITIZER_FREEBSD
kono
parents:
diff changeset
22 #include <sys/user.h>
kono
parents:
diff changeset
23 #endif
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
kono
parents:
diff changeset
26 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
kono
parents:
diff changeset
27 # include <osreldate.h>
kono
parents:
diff changeset
28 # if __FreeBSD_version <= 902001 // v9.2
kono
parents:
diff changeset
29 # define kinfo_vmentry xkinfo_vmentry
kono
parents:
diff changeset
30 # endif
kono
parents:
diff changeset
31 #endif
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 namespace __sanitizer {
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
kono
parents:
diff changeset
36 const int Mib[] = {
kono
parents:
diff changeset
37 #if SANITIZER_FREEBSD
kono
parents:
diff changeset
38 CTL_KERN,
kono
parents:
diff changeset
39 KERN_PROC,
kono
parents:
diff changeset
40 KERN_PROC_VMMAP,
kono
parents:
diff changeset
41 getpid()
kono
parents:
diff changeset
42 #else
kono
parents:
diff changeset
43 CTL_VM,
kono
parents:
diff changeset
44 VM_PROC,
kono
parents:
diff changeset
45 VM_PROC_MAP,
kono
parents:
diff changeset
46 getpid(),
kono
parents:
diff changeset
47 sizeof(struct kinfo_vmentry)
kono
parents:
diff changeset
48 #endif
kono
parents:
diff changeset
49 };
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 size_t Size = 0;
kono
parents:
diff changeset
52 int Err = sysctl(Mib, ARRAY_SIZE(Mib), NULL, &Size, NULL, 0);
kono
parents:
diff changeset
53 CHECK_EQ(Err, 0);
kono
parents:
diff changeset
54 CHECK_GT(Size, 0);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 size_t MmapedSize = Size * 4 / 3;
kono
parents:
diff changeset
57 void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
kono
parents:
diff changeset
58 Size = MmapedSize;
kono
parents:
diff changeset
59 Err = sysctl(Mib, ARRAY_SIZE(Mib), VmMap, &Size, NULL, 0);
kono
parents:
diff changeset
60 CHECK_EQ(Err, 0);
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 proc_maps->data = (char*)VmMap;
kono
parents:
diff changeset
63 proc_maps->mmaped_size = MmapedSize;
kono
parents:
diff changeset
64 proc_maps->len = Size;
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
kono
parents:
diff changeset
68 char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
kono
parents:
diff changeset
69 if (data_.current >= last) return false;
kono
parents:
diff changeset
70 struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry *)data_.current;
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 segment->start = (uptr)VmEntry->kve_start;
kono
parents:
diff changeset
73 segment->end = (uptr)VmEntry->kve_end;
kono
parents:
diff changeset
74 segment->offset = (uptr)VmEntry->kve_offset;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 segment->protection = 0;
kono
parents:
diff changeset
77 if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
kono
parents:
diff changeset
78 segment->protection |= kProtectionRead;
kono
parents:
diff changeset
79 if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
kono
parents:
diff changeset
80 segment->protection |= kProtectionWrite;
kono
parents:
diff changeset
81 if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
kono
parents:
diff changeset
82 segment->protection |= kProtectionExecute;
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 if (segment->filename != NULL && segment->filename_size > 0) {
kono
parents:
diff changeset
85 internal_snprintf(segment->filename,
kono
parents:
diff changeset
86 Min(segment->filename_size, (uptr)PATH_MAX), "%s",
kono
parents:
diff changeset
87 VmEntry->kve_path);
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 #if SANITIZER_FREEBSD
kono
parents:
diff changeset
91 data_.current += VmEntry->kve_structsize;
kono
parents:
diff changeset
92 #else
kono
parents:
diff changeset
93 data_.current += sizeof(*VmEntry);
kono
parents:
diff changeset
94 #endif
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 return true;
kono
parents:
diff changeset
97 }
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 } // namespace __sanitizer
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 #endif // SANITIZER_FREEBSD || SANITIZER_NETBSD