1 //===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Information about the process mappings (FreeBSD and NetBSD-specific parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
12 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
13 #include "sanitizer_common.h"
15 #include "sanitizer_freebsd.h"
17 #include "sanitizer_procmaps.h"
20 #include <sys/sysctl.h>
25 // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
26 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
27 # include <osreldate.h>
28 # if __FreeBSD_version <= 902001 // v9.2
29 # define kinfo_vmentry xkinfo_vmentry
33 namespace __sanitizer
{
35 void ReadProcMaps(ProcSelfMapsBuff
*proc_maps
) {
47 sizeof(struct kinfo_vmentry
)
52 int Err
= sysctl(Mib
, ARRAY_SIZE(Mib
), NULL
, &Size
, NULL
, 0);
56 size_t MmapedSize
= Size
* 4 / 3;
57 void *VmMap
= MmapOrDie(MmapedSize
, "ReadProcMaps()");
59 Err
= sysctl(Mib
, ARRAY_SIZE(Mib
), VmMap
, &Size
, NULL
, 0);
62 proc_maps
->data
= (char*)VmMap
;
63 proc_maps
->mmaped_size
= MmapedSize
;
64 proc_maps
->len
= Size
;
67 bool MemoryMappingLayout::Next(MemoryMappedSegment
*segment
) {
68 char *last
= data_
.proc_self_maps
.data
+ data_
.proc_self_maps
.len
;
69 if (data_
.current
>= last
) return false;
70 struct kinfo_vmentry
*VmEntry
= (struct kinfo_vmentry
*)data_
.current
;
72 segment
->start
= (uptr
)VmEntry
->kve_start
;
73 segment
->end
= (uptr
)VmEntry
->kve_end
;
74 segment
->offset
= (uptr
)VmEntry
->kve_offset
;
76 segment
->protection
= 0;
77 if ((VmEntry
->kve_protection
& KVME_PROT_READ
) != 0)
78 segment
->protection
|= kProtectionRead
;
79 if ((VmEntry
->kve_protection
& KVME_PROT_WRITE
) != 0)
80 segment
->protection
|= kProtectionWrite
;
81 if ((VmEntry
->kve_protection
& KVME_PROT_EXEC
) != 0)
82 segment
->protection
|= kProtectionExecute
;
84 if (segment
->filename
!= NULL
&& segment
->filename_size
> 0) {
85 internal_snprintf(segment
->filename
,
86 Min(segment
->filename_size
, (uptr
)PATH_MAX
), "%s",
91 data_
.current
+= VmEntry
->kve_structsize
;
93 data_
.current
+= sizeof(*VmEntry
);
99 } // namespace __sanitizer
101 #endif // SANITIZER_FREEBSD || SANITIZER_NETBSD