1 //===-- sanitizer_procmaps_bsd.cpp ----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Information about the process mappings
10 // (FreeBSD and NetBSD-specific parts).
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
14 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
15 #include "sanitizer_common.h"
17 #include "sanitizer_freebsd.h"
19 #include "sanitizer_procmaps.h"
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
32 // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
33 #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
34 #include <osreldate.h>
35 #if __FreeBSD_version <= 902001 // v9.2
36 #define kinfo_vmentry xkinfo_vmentry
40 namespace __sanitizer
{
42 void ReadProcMaps(ProcSelfMapsBuff
*proc_maps
) {
49 #elif SANITIZER_NETBSD
54 sizeof(struct kinfo_vmentry
)
56 #error "not supported"
61 int Err
= internal_sysctl(Mib
, ARRAY_SIZE(Mib
), NULL
, &Size
, NULL
, 0);
65 size_t MmapedSize
= Size
* 4 / 3;
66 void *VmMap
= MmapOrDie(MmapedSize
, "ReadProcMaps()");
68 Err
= internal_sysctl(Mib
, ARRAY_SIZE(Mib
), VmMap
, &Size
, NULL
, 0);
70 proc_maps
->data
= (char *)VmMap
;
71 proc_maps
->mmaped_size
= MmapedSize
;
72 proc_maps
->len
= Size
;
75 bool MemoryMappingLayout::Next(MemoryMappedSegment
*segment
) {
76 CHECK(!Error()); // can not fail
77 char *last
= data_
.proc_self_maps
.data
+ data_
.proc_self_maps
.len
;
78 if (data_
.current
>= last
)
80 const struct kinfo_vmentry
*VmEntry
=
81 (const struct kinfo_vmentry
*)data_
.current
;
83 segment
->start
= (uptr
)VmEntry
->kve_start
;
84 segment
->end
= (uptr
)VmEntry
->kve_end
;
85 segment
->offset
= (uptr
)VmEntry
->kve_offset
;
87 segment
->protection
= 0;
88 if ((VmEntry
->kve_protection
& KVME_PROT_READ
) != 0)
89 segment
->protection
|= kProtectionRead
;
90 if ((VmEntry
->kve_protection
& KVME_PROT_WRITE
) != 0)
91 segment
->protection
|= kProtectionWrite
;
92 if ((VmEntry
->kve_protection
& KVME_PROT_EXEC
) != 0)
93 segment
->protection
|= kProtectionExecute
;
95 if (segment
->filename
!= NULL
&& segment
->filename_size
> 0) {
96 internal_snprintf(segment
->filename
,
97 Min(segment
->filename_size
, (uptr
)PATH_MAX
), "%s",
101 #if SANITIZER_FREEBSD
102 data_
.current
+= VmEntry
->kve_structsize
;
104 data_
.current
+= sizeof(*VmEntry
);
110 } // namespace __sanitizer