VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/rtmempage-exec-mmap-posix.cpp@ 78750

Last change on this file since 78750 was 78345, checked in by vboxsync, 6 years ago

IPRT/mem: Added RTMemPageAllocEx so we can try lock memory and try prevent it from being part of dumps/cores. [incorrect assertion]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* $Id: rtmempage-exec-mmap-posix.cpp 78345 2019-04-29 10:15:06Z vboxsync $ */
2/** @file
3 * IPRT - RTMemPage*, POSIX with mmap only.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/mem.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/errcore.h>
37#include <iprt/param.h>
38#include <iprt/string.h>
39
40#include <stdlib.h>
41#include <errno.h>
42#include <sys/mman.h>
43#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
44# define MAP_ANONYMOUS MAP_ANON
45#endif
46
47
48/**
49 * Applies flags to an allocation.
50 *
51 * @param pv The allocation.
52 * @param cb The size of the allocation (page aligned).
53 * @param fFlags RTMEMPAGEALLOC_F_XXX.
54 */
55DECLINLINE(void) rtMemPagePosixApplyFlags(void *pv, size_t cb, uint32_t fFlags)
56{
57#ifndef RT_OS_OS2
58 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
59 {
60 int rc = mlock(pv, cb);
61 AssertMsg(rc == 0, ("mlock %p LB %#zx -> %d errno=%d\n", pv, cb, rc, errno));
62 NOREF(rc);
63 }
64
65# ifdef MADV_DONTDUMP
66 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_NO_DUMP)
67 {
68 int rc = madvise(pv, cb, MADV_DONTDUMP);
69 AssertMsg(rc == 0, ("madvice %p LB %#zx MADV_DONTDUMP -> %d errno=%d\n", pv, cb, rc, errno));
70 NOREF(rc);
71 }
72# endif
73#endif
74
75 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
76 RT_BZERO(pv, cb);
77}
78
79
80/**
81 * Allocates memory from the specified heap.
82 *
83 * @returns Address of the allocated memory.
84 * @param cb The number of bytes to allocate.
85 * @param pszTag The tag.
86 * @param fFlags RTMEMPAGEALLOC_F_XXX.
87 * @param fProtExec PROT_EXEC or 0.
88 */
89static void *rtMemPagePosixAlloc(size_t cb, const char *pszTag, uint32_t fFlags, int fProtExec)
90{
91 /*
92 * Validate & adjust the input.
93 */
94 Assert(cb > 0);
95 NOREF(pszTag);
96 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
97
98 /*
99 * Do the allocation.
100 */
101 void *pv = mmap(NULL, cb,
102 PROT_READ | PROT_WRITE | fProtExec,
103 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
104 if (pv != MAP_FAILED)
105 {
106 AssertPtr(pv);
107
108 if (fFlags)
109 rtMemPagePosixApplyFlags(pv, cb, fFlags);
110 }
111 else
112 pv = NULL;
113
114 return pv;
115}
116
117
118/**
119 * Free memory allocated by rtMemPagePosixAlloc.
120 *
121 * @param pv The address of the memory to free.
122 * @param cb The size.
123 */
124static void rtMemPagePosixFree(void *pv, size_t cb)
125{
126 /*
127 * Validate & adjust the input.
128 */
129 if (!pv)
130 return;
131 AssertPtr(pv);
132 Assert(cb > 0);
133 Assert(!((uintptr_t)pv & PAGE_OFFSET_MASK));
134 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
135
136 /*
137 * Free the memory.
138 */
139 int rc = munmap(pv, cb);
140 AssertMsg(rc == 0, ("rc=%d pv=%p cb=%#zx\n", rc, pv, cb)); NOREF(rc);
141}
142
143
144
145
146
147RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
148{
149 return rtMemPagePosixAlloc(cb, pszTag, 0, 0);
150}
151
152
153RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
154{
155 return rtMemPagePosixAlloc(cb, pszTag, RTMEMPAGEALLOC_F_ZERO, 0);
156}
157
158
159RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
160{
161 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
162 return rtMemPagePosixAlloc(cb, pszTag, fFlags, 0);
163}
164
165
166RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
167{
168 return rtMemPagePosixFree(pv, cb);
169}
170
171
172
173
174
175RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
176{
177 return rtMemPagePosixAlloc(cb, pszTag, 0, PROT_EXEC);
178}
179
180
181RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
182{
183 return rtMemPagePosixFree(pv, cb);
184}
185
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette