]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_freebsd.cc
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / lib / sanitizer_common / sanitizer_procmaps_freebsd.cc
1 //===-- 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
23 // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
24 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
25 # include <osreldate.h>
26 # if __FreeBSD_version <= 902001 // v9.2
27 # define kinfo_vmentry xkinfo_vmentry
28 # endif
29 #endif
30
31 namespace __sanitizer {
32
33 void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
34 const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid() };
35 size_t Size = 0;
36 int Err = sysctl(Mib, 4, NULL, &Size, NULL, 0);
37 CHECK_EQ(Err, 0);
38 CHECK_GT(Size, 0);
39
40 size_t MmapedSize = Size * 4 / 3;
41 void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
42 Size = MmapedSize;
43 Err = sysctl(Mib, 4, VmMap, &Size, NULL, 0);
44 CHECK_EQ(Err, 0);
45
46 proc_maps->data = (char*)VmMap;
47 proc_maps->mmaped_size = MmapedSize;
48 proc_maps->len = Size;
49 }
50
51 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
52 char filename[], uptr filename_size,
53 uptr *protection, ModuleArch *arch, u8 *uuid) {
54 CHECK(!arch && "not implemented");
55 CHECK(!uuid && "not implemented");
56 char *last = proc_self_maps_.data + proc_self_maps_.len;
57 if (current_ >= last) return false;
58 uptr dummy;
59 if (!start) start = &dummy;
60 if (!end) end = &dummy;
61 if (!offset) offset = &dummy;
62 if (!protection) protection = &dummy;
63 struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*)current_;
64
65 *start = (uptr)VmEntry->kve_start;
66 *end = (uptr)VmEntry->kve_end;
67 *offset = (uptr)VmEntry->kve_offset;
68
69 *protection = 0;
70 if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
71 *protection |= kProtectionRead;
72 if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
73 *protection |= kProtectionWrite;
74 if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
75 *protection |= kProtectionExecute;
76
77 if (filename != NULL && filename_size > 0) {
78 internal_snprintf(filename,
79 Min(filename_size, (uptr)PATH_MAX),
80 "%s", VmEntry->kve_path);
81 }
82
83 current_ += VmEntry->kve_structsize;
84
85 return true;
86 }
87
88 } // namespace __sanitizer
89
90 #endif // SANITIZER_FREEBSD