1*06c3fb27SDimitry Andric //===-- mem_map.cpp ---------------------------------------------*- C++ -*-===//
2*06c3fb27SDimitry Andric //
3*06c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*06c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06c3fb27SDimitry Andric //
7*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
8*06c3fb27SDimitry Andric
9*06c3fb27SDimitry Andric #include "mem_map.h"
10*06c3fb27SDimitry Andric
11*06c3fb27SDimitry Andric #include "common.h"
12*06c3fb27SDimitry Andric
13*06c3fb27SDimitry Andric namespace scudo {
14*06c3fb27SDimitry Andric
mapImpl(uptr Addr,uptr Size,const char * Name,uptr Flags)15*06c3fb27SDimitry Andric bool MemMapDefault::mapImpl(uptr Addr, uptr Size, const char *Name,
16*06c3fb27SDimitry Andric uptr Flags) {
17*06c3fb27SDimitry Andric void *MappedAddr =
18*06c3fb27SDimitry Andric ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name, Flags, &Data);
19*06c3fb27SDimitry Andric if (MappedAddr == nullptr)
20*06c3fb27SDimitry Andric return false;
21*06c3fb27SDimitry Andric Base = reinterpret_cast<uptr>(MappedAddr);
22*06c3fb27SDimitry Andric MappedBase = Base;
23*06c3fb27SDimitry Andric Capacity = Size;
24*06c3fb27SDimitry Andric return true;
25*06c3fb27SDimitry Andric }
26*06c3fb27SDimitry Andric
unmapImpl(uptr Addr,uptr Size)27*06c3fb27SDimitry Andric void MemMapDefault::unmapImpl(uptr Addr, uptr Size) {
28*06c3fb27SDimitry Andric if (Size == Capacity) {
29*06c3fb27SDimitry Andric Base = MappedBase = Capacity = 0;
30*06c3fb27SDimitry Andric } else {
31*06c3fb27SDimitry Andric if (Base == Addr) {
32*06c3fb27SDimitry Andric Base = Addr + Size;
33*06c3fb27SDimitry Andric MappedBase = MappedBase == 0 ? Base : Max(MappedBase, Base);
34*06c3fb27SDimitry Andric }
35*06c3fb27SDimitry Andric Capacity -= Size;
36*06c3fb27SDimitry Andric }
37*06c3fb27SDimitry Andric
38*06c3fb27SDimitry Andric ::scudo::unmap(reinterpret_cast<void *>(Addr), Size, UNMAP_ALL, &Data);
39*06c3fb27SDimitry Andric }
40*06c3fb27SDimitry Andric
remapImpl(uptr Addr,uptr Size,const char * Name,uptr Flags)41*06c3fb27SDimitry Andric bool MemMapDefault::remapImpl(uptr Addr, uptr Size, const char *Name,
42*06c3fb27SDimitry Andric uptr Flags) {
43*06c3fb27SDimitry Andric void *RemappedPtr =
44*06c3fb27SDimitry Andric ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name, Flags, &Data);
45*06c3fb27SDimitry Andric const uptr RemappedAddr = reinterpret_cast<uptr>(RemappedPtr);
46*06c3fb27SDimitry Andric MappedBase = MappedBase == 0 ? RemappedAddr : Min(MappedBase, RemappedAddr);
47*06c3fb27SDimitry Andric return RemappedAddr == Addr;
48*06c3fb27SDimitry Andric }
49*06c3fb27SDimitry Andric
releaseAndZeroPagesToOSImpl(uptr From,uptr Size)50*06c3fb27SDimitry Andric void MemMapDefault::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
51*06c3fb27SDimitry Andric DCHECK_NE(MappedBase, 0U);
52*06c3fb27SDimitry Andric DCHECK_GE(From, MappedBase);
53*06c3fb27SDimitry Andric return ::scudo::releasePagesToOS(MappedBase, From - MappedBase, Size, &Data);
54*06c3fb27SDimitry Andric }
55*06c3fb27SDimitry Andric
setMemoryPermissionImpl(uptr Addr,uptr Size,uptr Flags)56*06c3fb27SDimitry Andric void MemMapDefault::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
57*06c3fb27SDimitry Andric return ::scudo::setMemoryPermission(Addr, Size, Flags);
58*06c3fb27SDimitry Andric }
59*06c3fb27SDimitry Andric
releaseImpl()60*06c3fb27SDimitry Andric void ReservedMemoryDefault::releaseImpl() {
61*06c3fb27SDimitry Andric ::scudo::unmap(reinterpret_cast<void *>(Base), Capacity, UNMAP_ALL, &Data);
62*06c3fb27SDimitry Andric }
63*06c3fb27SDimitry Andric
createImpl(uptr Addr,uptr Size,const char * Name,uptr Flags)64*06c3fb27SDimitry Andric bool ReservedMemoryDefault::createImpl(uptr Addr, uptr Size, const char *Name,
65*06c3fb27SDimitry Andric uptr Flags) {
66*06c3fb27SDimitry Andric void *Reserved = ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name,
67*06c3fb27SDimitry Andric Flags | MAP_NOACCESS, &Data);
68*06c3fb27SDimitry Andric if (Reserved == nullptr)
69*06c3fb27SDimitry Andric return false;
70*06c3fb27SDimitry Andric
71*06c3fb27SDimitry Andric Base = reinterpret_cast<uptr>(Reserved);
72*06c3fb27SDimitry Andric Capacity = Size;
73*06c3fb27SDimitry Andric
74*06c3fb27SDimitry Andric return true;
75*06c3fb27SDimitry Andric }
76*06c3fb27SDimitry Andric
dispatchImpl(uptr Addr,uptr Size)77*06c3fb27SDimitry Andric ReservedMemoryDefault::MemMapT ReservedMemoryDefault::dispatchImpl(uptr Addr,
78*06c3fb27SDimitry Andric uptr Size) {
79*06c3fb27SDimitry Andric ReservedMemoryDefault::MemMapT NewMap(Addr, Size);
80*06c3fb27SDimitry Andric NewMap.setMapPlatformData(Data);
81*06c3fb27SDimitry Andric return NewMap;
82*06c3fb27SDimitry Andric }
83*06c3fb27SDimitry Andric
84*06c3fb27SDimitry Andric } // namespace scudo
85