1 | /* $Id: alloc-win.cpp 5428 2007-10-21 21:27:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Memory Allocation, Win32.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_MEM
|
---|
23 | #include <Windows.h>
|
---|
24 |
|
---|
25 | #include <iprt/alloc.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/param.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/err.h>
|
---|
30 |
|
---|
31 | #ifndef USE_VIRTUAL_ALLOC
|
---|
32 | # include <malloc.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Allocates memory which may contain code.
|
---|
38 | *
|
---|
39 | * @returns Pointer to the allocated memory.
|
---|
40 | * @returns NULL on failure.
|
---|
41 | * @param cb Size in bytes of the memory block to allocate.
|
---|
42 | */
|
---|
43 | RTDECL(void *) RTMemExecAlloc(size_t cb)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Allocate first.
|
---|
47 | */
|
---|
48 | AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
|
---|
49 | cb = RT_ALIGN_Z(cb, 32);
|
---|
50 | void *pv = malloc(cb);
|
---|
51 | AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
|
---|
52 | if (pv)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Add PROT_EXEC flag to the page.
|
---|
56 | *
|
---|
57 | * This is in violation of the SuS where I think it saith that mprotect() shall
|
---|
58 | * only be used with mmap()'ed memory. Works on linux and OS/2 LIBC v0.6.
|
---|
59 | */
|
---|
60 | memset(pv, 0xcc, cb);
|
---|
61 | void *pvProt = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
|
---|
62 | size_t cbProt = ((uintptr_t)pv & PAGE_OFFSET_MASK) + cb;
|
---|
63 | cbProt = RT_ALIGN_Z(cbProt, PAGE_SIZE);
|
---|
64 | DWORD fFlags = 0;
|
---|
65 | if (!VirtualProtect(pvProt, cbProt, PAGE_EXECUTE_READWRITE, &fFlags))
|
---|
66 | {
|
---|
67 | AssertMsgFailed(("VirtualProtect(%p, %#x,,) -> lasterr=%d\n", pvProt, cbProt, GetLastError()));
|
---|
68 | free(pv);
|
---|
69 | pv = NULL;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return pv;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Free executable/read/write memory allocated by RTMemExecAlloc().
|
---|
78 | *
|
---|
79 | * @param pv Pointer to memory block.
|
---|
80 | */
|
---|
81 | RTDECL(void) RTMemExecFree(void *pv)
|
---|
82 | {
|
---|
83 | if (pv)
|
---|
84 | free(pv);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Allocate page aligned memory.
|
---|
90 | *
|
---|
91 | * @returns Pointer to the allocated memory.
|
---|
92 | * @returns NULL if we're out of memory.
|
---|
93 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
94 | */
|
---|
95 | RTDECL(void *) RTMemPageAlloc(size_t cb)
|
---|
96 | {
|
---|
97 | #ifdef USE_VIRTUAL_ALLOC
|
---|
98 | void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
|
---|
99 | #else
|
---|
100 | void *pv = _aligned_malloc(cb, PAGE_SIZE);
|
---|
101 | #endif
|
---|
102 | AssertMsg(pv, ("cb=%d lasterr=%d\n", cb, GetLastError()));
|
---|
103 | return pv;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Allocate zero'ed page aligned memory.
|
---|
109 | *
|
---|
110 | * @returns Pointer to the allocated memory.
|
---|
111 | * @returns NULL if we're out of memory.
|
---|
112 | * @param cb Size of the memory block. Will be rounded up to page size.
|
---|
113 | */
|
---|
114 | RTDECL(void *) RTMemPageAllocZ(size_t cb)
|
---|
115 | {
|
---|
116 | #ifdef USE_VIRTUAL_ALLOC
|
---|
117 | void *pv = VirtualAlloc(NULL, RT_ALIGN_Z(cb, PAGE_SIZE), MEM_COMMIT, PAGE_READWRITE);
|
---|
118 | #else
|
---|
119 | void *pv = _aligned_malloc(cb, PAGE_SIZE);
|
---|
120 | #endif
|
---|
121 | if (pv)
|
---|
122 | {
|
---|
123 | memset(pv, 0, RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
124 | return pv;
|
---|
125 | }
|
---|
126 | AssertMsgFailed(("cb=%d lasterr=%d\n", cb, GetLastError()));
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Free a memory block allocated with RTMemPageAlloc() or RTMemPageAllocZ().
|
---|
133 | *
|
---|
134 | * @param pv Pointer to the block as it was returned by the allocation function.
|
---|
135 | * NULL will be ignored.
|
---|
136 | */
|
---|
137 | RTDECL(void) RTMemPageFree(void *pv)
|
---|
138 | {
|
---|
139 | if (pv)
|
---|
140 | {
|
---|
141 | #ifdef USE_VIRTUAL_ALLOC
|
---|
142 | if (!VirtualFree(pv, 0, MEM_RELEASE))
|
---|
143 | AssertMsgFailed(("pv=%p lasterr=%d\n", pv, GetLastError()));
|
---|
144 | #else
|
---|
145 | _aligned_free(pv);
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Change the page level protection of a memory region.
|
---|
153 | *
|
---|
154 | * @returns iprt status code.
|
---|
155 | * @param pv Start of the region. Will be rounded down to nearest page boundary.
|
---|
156 | * @param cb Size of the region. Will be rounded up to the nearest page boundary.
|
---|
157 | * @param fProtect The new protection, a combination of the RTMEM_PROT_* defines.
|
---|
158 | */
|
---|
159 | RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect)
|
---|
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 & ~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 |
|
---|