VirtualBox

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

Last change on this file since 96124 was 95804, checked in by vboxsync, 2 years ago

IPRT/alloc-win.cpp: Define USE_VIRTUAL_ALLOC in IPRT_NO_CRT mode; added missing USE_VIRTUAL_ALLOC variants of the exec allocator. bugref:10261

  • 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 95804 2022-07-25 12:29:26Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#ifdef IPRT_NO_CRT
32# define USE_VIRTUAL_ALLOC
33#endif
34#define LOG_GROUP RTLOGGROUP_MEM
35#include <iprt/win/windows.h>
36
37#include <iprt/alloc.h>
38#include <iprt/assert.h>
39#include <iprt/param.h>
40#include <iprt/string.h>
41#include <iprt/errcore.h>
42
43#ifndef USE_VIRTUAL_ALLOC
44# include <malloc.h>
45#endif
46
47
48RTDECL(void *) RTMemExecAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
49{
50 RT_NOREF_PV(pszTag);
51
52 /*
53 * Allocate first.
54 */
55 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
56#ifdef USE_VIRTUAL_ALLOC
57 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
58 void *pv = VirtualAlloc(NULL, cb, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
59 AssertMsg(pv, ("VirtualAlloc(%zx) failed!!!\n", cb));
60#else
61 cb = RT_ALIGN_Z(cb, 32);
62 void *pv = malloc(cb);
63 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
64 if (pv)
65 {
66 memset(pv, 0xcc, cb);
67 void *pvProt = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
68 size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
69 cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
70 DWORD fFlags = 0;
71 if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
72 {
73 AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
74 free(pv);
75 pv = NULL;
76 }
77 }
78#endif
79 return pv;
80}
81
82
83RTDECL(void) RTMemExecFree(void *pv, size_t cb) RT_NO_THROW_DEF
84{
85 RT_NOREF_PV(cb);
86
87 if (pv)
88#ifdef USE_VIRTUAL_ALLOC
89 if (!VirtualFree(pv, 0, MEM_RELEASE))
90 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
91#else
92 free(pv);
93#endif
94}
95
96
97RTDECL(void *) RTMemPageAllocTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
98{
99 RT_NOREF_PV(pszTag);
100
101#ifdef USE_VIRTUAL_ALLOC
102 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
103#else
104 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
105#endif
106 AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
107 return pv;
108}
109
110
111RTDECL(void *) RTMemPageAllocExTag(size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW_DEF
112{
113 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
114 RT_NOREF_PV(pszTag);
115 AssertReturn(!(fFlags & ~RTMEMPAGEALLOC_F_VALID_MASK), NULL);
116
117#ifdef USE_VIRTUAL_ALLOC
118 void *pv = VirtualAlloc(NULL, cbAligned, MEM_COMMIT, PAGE_READWRITE);
119#else
120 void *pv = _aligned_malloc(cbAligned, PAGE_SIZE);
121#endif
122 AssertMsgReturn(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()), NULL);
123
124 if (fFlags & RTMEMPAGEALLOC_F_ADVISE_LOCKED)
125 {
126 /** @todo check why we get ERROR_WORKING_SET_QUOTA here. */
127 BOOL const fOkay = VirtualLock(pv, cbAligned);
128 AssertMsg(fOkay || GetLastError() == ERROR_WORKING_SET_QUOTA, ("pv=%p cb=%d lasterr=%d\n", pv, cb, GetLastError()));
129 NOREF(fOkay);
130 }
131
132 if (fFlags & RTMEMPAGEALLOC_F_ZERO)
133 RT_BZERO(pv, cbAligned);
134
135 return pv;
136}
137
138
139RTDECL(void *) RTMemPageAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW_DEF
140{
141 RT_NOREF_PV(pszTag);
142
143#ifdef USE_VIRTUAL_ALLOC
144 void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
145#else
146 void *pv = _aligned_malloc(RT_ALIGN_Z(cb, PAGE_SIZE), PAGE_SIZE);
147#endif
148 if (pv)
149 {
150 memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
151 return pv;
152 }
153 AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
154 return NULL;
155}
156
157
158RTDECL(void) RTMemPageFree(void *pv, size_t cb) RT_NO_THROW_DEF
159{
160 RT_NOREF_PV(cb);
161
162 if (pv)
163 {
164#ifdef USE_VIRTUAL_ALLOC
165 if (!VirtualFree(pv, 0, MEM_RELEASE))
166 AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
167#else
168 _aligned_free(pv);
169#endif
170 }
171}
172
173
174RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
175{
176 /*
177 * Validate input.
178 */
179 if (cb == 0)
180 {
181 AssertMsgFailed(("!cb\n"));
182 return VERR_INVALID_PARAMETER;
183 }
184 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
185 {
186 AssertMsgFailed(("fProtect=%#x\n", fProtect));
187 return VERR_INVALID_PARAMETER;
188 }
189
190 /*
191 * Convert the flags.
192 */
193 int fProt;
194 Assert(!RTMEM_PROT_NONE);
195 switch (fProtect & (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
196 {
197 case RTMEM_PROT_NONE:
198 fProt = PAGE_NOACCESS;
199 break;
200
201 case RTMEM_PROT_READ:
202 fProt = PAGE_READONLY;
203 break;
204
205 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
206 fProt = PAGE_READWRITE;
207 break;
208
209 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
210 fProt = PAGE_EXECUTE_READWRITE;
211 break;
212
213 case RTMEM_PROT_READ | RTMEM_PROT_EXEC:
214 fProt = PAGE_EXECUTE_READWRITE;
215 break;
216
217 case RTMEM_PROT_WRITE:
218 fProt = PAGE_READWRITE;
219 break;
220
221 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
222 fProt = PAGE_EXECUTE_READWRITE;
223 break;
224
225 case RTMEM_PROT_EXEC:
226 fProt = PAGE_EXECUTE_READWRITE;
227 break;
228
229 /* If the compiler had any brains it would warn about this case. */
230 default:
231 AssertMsgFailed(("fProtect=%#x\n", fProtect));
232 return VERR_INTERNAL_ERROR;
233 }
234
235 /*
236 * Align the request.
237 */
238 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
239 pv = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
240
241 /*
242 * Change the page attributes.
243 */
244 DWORD fFlags = 0;
245 if (VirtualProtect(pv, cb, fProt, &fFlags))
246 return VINF_SUCCESS;
247 return RTErrConvertFromWin32(GetLastError());
248}
249
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