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