Retro68/gcc/libsanitizer/sanitizer_common/sanitizer_procmaps_bsd.cc

138 lines
3.7 KiB
C++
Raw Normal View History

2019-06-02 15:48:37 +00:00
//===-- sanitizer_procmaps_bsd.cc -----------------------------------------===//
2015-08-28 15:33:40 +00:00
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
2019-06-02 15:48:37 +00:00
// Information about the process mappings
// (FreeBSD, OpenBSD and NetBSD-specific parts).
2015-08-28 15:33:40 +00:00
//===----------------------------------------------------------------------===//
#include "sanitizer_platform.h"
2019-06-02 15:48:37 +00:00
#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
2015-08-28 15:33:40 +00:00
#include "sanitizer_common.h"
2018-12-28 15:30:48 +00:00
#if SANITIZER_FREEBSD
2015-08-28 15:33:40 +00:00
#include "sanitizer_freebsd.h"
2018-12-28 15:30:48 +00:00
#endif
2015-08-28 15:33:40 +00:00
#include "sanitizer_procmaps.h"
2019-06-02 15:48:37 +00:00
// clang-format off
#include <sys/types.h>
2015-08-28 15:33:40 +00:00
#include <sys/sysctl.h>
2019-06-02 15:48:37 +00:00
// clang-format on
#include <unistd.h>
2018-12-28 15:30:48 +00:00
#if SANITIZER_FREEBSD
2015-08-28 15:33:40 +00:00
#include <sys/user.h>
2018-12-28 15:30:48 +00:00
#endif
2015-08-28 15:33:40 +00:00
2019-06-02 15:48:37 +00:00
#include <limits.h>
#if SANITIZER_OPENBSD
#define KVME_PROT_READ KVE_PROT_READ
#define KVME_PROT_WRITE KVE_PROT_WRITE
#define KVME_PROT_EXEC KVE_PROT_EXEC
#endif
2015-08-28 15:33:40 +00:00
// Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
2019-06-02 15:48:37 +00:00
#include <osreldate.h>
#if __FreeBSD_version <= 902001 // v9.2
#define kinfo_vmentry xkinfo_vmentry
#endif
2015-08-28 15:33:40 +00:00
#endif
namespace __sanitizer {
void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
2018-12-28 15:30:48 +00:00
const int Mib[] = {
#if SANITIZER_FREEBSD
CTL_KERN,
KERN_PROC,
KERN_PROC_VMMAP,
getpid()
2019-06-02 15:48:37 +00:00
#elif SANITIZER_OPENBSD
CTL_KERN,
KERN_PROC_VMMAP,
getpid()
#elif SANITIZER_NETBSD
2018-12-28 15:30:48 +00:00
CTL_VM,
VM_PROC,
VM_PROC_MAP,
getpid(),
sizeof(struct kinfo_vmentry)
2019-06-02 15:48:37 +00:00
#else
#error "not supported"
2018-12-28 15:30:48 +00:00
#endif
};
2019-06-02 15:48:37 +00:00
uptr Size = 0;
int Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), NULL, &Size, NULL, 0);
2015-08-28 15:33:40 +00:00
CHECK_EQ(Err, 0);
CHECK_GT(Size, 0);
2019-06-02 15:48:37 +00:00
#if !SANITIZER_OPENBSD
2015-08-28 15:33:40 +00:00
size_t MmapedSize = Size * 4 / 3;
void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
Size = MmapedSize;
2019-06-02 15:48:37 +00:00
Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), VmMap, &Size, NULL, 0);
CHECK_EQ(Err, 0);
proc_maps->data = (char *)VmMap;
#else
size_t PageSize = GetPageSize();
size_t MmapedSize = Size;
MmapedSize = ((MmapedSize - 1) / PageSize + 1) * PageSize;
char *Mem = (char *)MmapOrDie(MmapedSize, "ReadProcMaps()");
Size = 2 * Size + 10 * sizeof(struct kinfo_vmentry);
if (Size > 0x10000)
Size = 0x10000;
Size = (Size / sizeof(struct kinfo_vmentry)) * sizeof(struct kinfo_vmentry);
Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), Mem, &Size, NULL, 0);
2015-08-28 15:33:40 +00:00
CHECK_EQ(Err, 0);
2019-06-02 15:48:37 +00:00
MmapedSize = Size;
proc_maps->data = Mem;
#endif
2015-08-28 15:33:40 +00:00
proc_maps->mmaped_size = MmapedSize;
proc_maps->len = Size;
}
2018-12-28 15:30:48 +00:00
bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
2019-06-02 15:48:37 +00:00
if (data_.current >= last)
return false;
const struct kinfo_vmentry *VmEntry =
(const struct kinfo_vmentry *)data_.current;
2018-12-28 15:30:48 +00:00
segment->start = (uptr)VmEntry->kve_start;
segment->end = (uptr)VmEntry->kve_end;
segment->offset = (uptr)VmEntry->kve_offset;
segment->protection = 0;
2015-08-28 15:33:40 +00:00
if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
2018-12-28 15:30:48 +00:00
segment->protection |= kProtectionRead;
2015-08-28 15:33:40 +00:00
if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
2018-12-28 15:30:48 +00:00
segment->protection |= kProtectionWrite;
2015-08-28 15:33:40 +00:00
if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
2018-12-28 15:30:48 +00:00
segment->protection |= kProtectionExecute;
2015-08-28 15:33:40 +00:00
2019-06-02 15:48:37 +00:00
#if !SANITIZER_OPENBSD
2018-12-28 15:30:48 +00:00
if (segment->filename != NULL && segment->filename_size > 0) {
internal_snprintf(segment->filename,
Min(segment->filename_size, (uptr)PATH_MAX), "%s",
VmEntry->kve_path);
2015-08-28 15:33:40 +00:00
}
2019-06-02 15:48:37 +00:00
#endif
2015-08-28 15:33:40 +00:00
2018-12-28 15:30:48 +00:00
#if SANITIZER_FREEBSD
data_.current += VmEntry->kve_structsize;
#else
data_.current += sizeof(*VmEntry);
#endif
2015-08-28 15:33:40 +00:00
return true;
}
2019-06-02 15:48:37 +00:00
} // namespace __sanitizer
2015-08-28 15:33:40 +00:00
2019-06-02 15:48:37 +00:00
#endif