VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/alloc-win.cpp@ 101143

Last change on this file since 101143 was 101143, checked in by vboxsync, 15 months ago

IPRT/mem: build fix. bugref:10370

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.0 KB
Line 
1/* $Id: alloc-win.cpp 101143 2023-09-18 11:15:59Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#ifdef IPRT_NO_CRT
42# define USE_VIRTUAL_ALLOC
43#endif
44#define LOG_GROUP RTLOGGROUP_MEM
45#include <iprt/win/windows.h>
46
47#include <iprt/alloc.h>
48#include <iprt/assert.h>
49#include <iprt/param.h>
50#include <iprt/string.h>
51#include <iprt/errcore.h>
52
53#ifndef USE_VIRTUAL_ALLOC
54# include <malloc.h>
55#endif
56
57
58/** @todo merge the page alloc code with the heap-based mmap stuff as
59 * _aligned_malloc just isn't well suited for exec. */
60
61
62RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
63{
64 RT_NOREF_PV(pszTag);
65
66#ifdef USE_VIRTUAL_ALLOC
67 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
68#else
69 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
70#endif
71 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
72 return pv;
73}
74
75
76RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
77{
78 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
79 RT_NOREF_PV(pszTag);
80 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
81
82#ifdef USE_VIRTUAL_ALLOC
83 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT,
84 !(fFlags & RTMEMPAGEALLOC_F_EXECUTABLE) ? PAGE_READWRITE : PAGE_EXECUTE_READWRITE);
85#else
86 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
87#endif
88 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
89
90#ifdef USE_VIRTUAL_ALLOC
91 if (fFlags & RTMEMPAGEALLOC_F_EXECUTABLE)
92 {
93 DWORD fIgn = 0;
94 BOOL const fRc = VirtualProtect(pv, cbAligned, PAGE_EXECUTE_READWRITE, &fIgn);
95 AssertMsgReturnStmt(fRc, ("%p LB %#zx: %#u\n", pv, cbAligned, GetLastError()), _aligned_free(pv), NULL);
96 }
97#endif
98
99 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
100 {
101 /** @todo check why we get ERROR_WORKING_SET_QUOTA here. */
102 BOOL const fOkay = VirtualLock(pv, cbAligned);
103 AssertMsg(fOkay || GetLastError() == ERROR_WORKING_SET_QUOTA, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
104 NOREF(fOkay);
105 }
106
107 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
108 RT_BZERO(pv, cbAligned);
109
110 return pv;
111}
112
113
114RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
115{
116 RT_NOREF_PV(pszTag);
117
118#ifdef USE_VIRTUAL_ALLOC
119 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
120#else
121 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
122#endif
123 if (pv)
124 {
125 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
126 return pv;
127 }
128 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
129 return NULL;
130}
131
132
133RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
134{
135 RT_NOREF_PV(cb);
136
137 if (pv)
138 {
139#ifdef USE_VIRTUAL_ALLOC
140 if (!VirtualFree(pv, 0, MEM_RELEASE))
141 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
142#else
143 /** @todo The exec version of this doesn't really work well... */
144 MEMORY_BASIC_INFORMATION MemInfo = { NULL };
145 SIZE_T cbRet = VirtualQuery(pv, &MemInfo, cb);
146 Assert(cbRet > 0);
147 if (cbRet > 0 && MemInfo.Protect == PAGE_EXECUTE_READWRITE)
148 {
149 DWORD fIgn = 0;
150 BOOL const fRc = VirtualProtect(pv, cb, PAGE_READWRITE, &fIgn);
151 Assert(fRc); RT_NOREF(fRc);
152 }
153 _aligned_free(pv);
154#endif
155 }
156}
157
158
159RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
160{
161 /*
162 * Validate input.
163 */
164 if (cb == 0)
165 {
166 AssertMsgFailed(("!cb\n"));
167 return VERR_INVALID_PARAMETER;
168 }
169 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
170 {
171 AssertMsgFailed(("fProtect=%#x\n", fProtect));
172 return VERR_INVALID_PARAMETER;
173 }
174
175 /*
176 * Convert the flags.
177 */
178 int fProt;
179 Assert(!RTMEM_PROT_NONE);
180 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
181 {
182 case RTMEM_PROT_NONE:
183 fProt = PAGE_NOACCESS;
184 break;
185
186 case RTMEM_PROT_READ:
187 fProt = PAGE_READONLY;
188 break;
189
190 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
191 fProt = PAGE_READWRITE;
192 break;
193
194 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
195 fProt = PAGE_EXECUTE_READWRITE;
196 break;
197
198 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
199 fProt = PAGE_EXECUTE_READWRITE;
200 break;
201
202 case RTMEM_PROT_WRITE:
203 fProt = PAGE_READWRITE;
204 break;
205
206 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
207 fProt = PAGE_EXECUTE_READWRITE;
208 break;
209
210 case RTMEM_PROT_EXEC:
211 fProt = PAGE_EXECUTE_READWRITE;
212 break;
213
214 /* If the compiler had any brains it would warn about this case. */
215 default:
216 AssertMsgFailed(("fProtect=%#x\n", fProtect));
217 return VERR_INTERNAL_ERROR;
218 }
219
220 /*
221 * Align the request.
222 */
223 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
224 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
225
226 /*
227 * Change the page attributes.
228 */
229 DWORD fFlags = 0;
230 if (VirtualProtect(pv, cb, fProt, &fFlags))
231 return VINF_SUCCESS;
232 return RTErrConvertFromWin32(GetLastError());
233}
234
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