VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/allocex.cpp@ 98032

Last change on this file since 98032 was 97904, checked in by vboxsync, 2 years ago

IPRT/r3/allocex: Implement RTMEMALLOCEX_FLAGS_EXEC using RTMemPageAlloc+RTMemProtect instead of RTMemExecAlloc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: allocex.cpp 97904 2022-12-29 18:14:03Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Extended Alloc and Free Functions for Ring-3, posix.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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#define RTMEM_NO_WRAP_TO_EF_APIS
42#include <iprt/mem.h>
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/err.h>
47#include <iprt/string.h>
48#include "internal/magics.h"
49#include "allocex.h"
50
51
52RTDECL(int) RTMemAllocExTag(size_t cb, size_t cbAlignment, uint32_t fFlags, const char *pszTag, void **ppv) RT_NO_THROW_DEF
53{
54 RT_NOREF_PV(pszTag);
55
56 /*
57 * Validate and adjust input.
58 */
59 AssertMsgReturn(!(fFlags & ~RTMEMALLOCEX_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
60 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
61 AssertReturn(RT_IS_POWER_OF_TWO(cbAlignment), VERR_INVALID_PARAMETER);
62 AssertMsgReturn(cbAlignment <= sizeof(void *), ("%zu (%#x)\n", cbAlignment, cbAlignment), VERR_UNSUPPORTED_ALIGNMENT);
63
64 if (fFlags & RTMEMALLOCEX_FLAGS_ANY_CTX)
65 return VERR_NOT_SUPPORTED;
66
67 /*
68 * Align the request.
69 */
70 size_t cbAligned = cb;
71 if (cbAlignment)
72 cbAligned = RT_ALIGN_Z(cb, cbAlignment);
73 else
74 cbAligned = RT_ALIGN_Z(cb, sizeof(uint64_t));
75 AssertMsgReturn(cbAligned >= cb && cbAligned <= ~(size_t)0, ("cbAligned=%#zx cb=%#zx", cbAligned, cb),
76 VERR_INVALID_PARAMETER);
77
78 /*
79 * Allocate the requested memory.
80 */
81 void *pv;
82 if (fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
83 {
84 int rc;
85 if (fFlags & RTMEMALLOCEX_FLAGS_16BIT_REACH)
86 rc = rtMemAllocEx16BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
87 else
88 rc = rtMemAllocEx32BitReach(cbAligned + sizeof(RTMEMHDRR3), fFlags, &pv);
89 if (RT_FAILURE(rc))
90 return rc;
91 }
92 else if (fFlags & RTMEMALLOCEX_FLAGS_EXEC)
93 {
94 pv = RTMemPageAlloc(cbAligned + sizeof(RTMEMHDRR3));
95 if (pv)
96 {
97 if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
98 RT_BZERO(pv, cbAligned + sizeof(RTMEMHDRR3));
99
100 int rc = RTMemProtect(pv, cbAligned + sizeof(RTMEMHDRR3), RTMEM_PROT_EXEC | RTMEM_PROT_READ | RTMEM_PROT_WRITE);
101 if (RT_FAILURE(rc))
102 {
103 RTMemPageFree(pv, cbAligned + sizeof(RTMEMHDRR3));
104 return rc;
105 }
106 }
107 }
108 else if (fFlags & RTMEMALLOCEX_FLAGS_ZEROED)
109 pv = RTMemAllocZ(cbAligned + sizeof(RTMEMHDRR3));
110 else
111 pv = RTMemAlloc(cbAligned + sizeof(RTMEMHDRR3));
112 if (!pv)
113 return VERR_NO_MEMORY;
114
115 /*
116 * Fill in the header and return.
117 */
118 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv;
119 pHdr->u32Magic = RTMEMHDR_MAGIC;
120 pHdr->fFlags = fFlags;
121 pHdr->cb = (uint32_t)cbAligned;
122 pHdr->cbReq = (uint32_t)cb;
123
124 *ppv = pHdr + 1;
125 return VINF_SUCCESS;
126}
127RT_EXPORT_SYMBOL(RTMemAllocExTag);
128
129
130RTDECL(void) RTMemFreeEx(void *pv, size_t cb) RT_NO_THROW_DEF
131{
132 if (!pv)
133 return;
134 AssertPtr(pv);
135
136 PRTMEMHDRR3 pHdr = (PRTMEMHDRR3)pv - 1;
137 AssertMsg(pHdr->u32Magic == RTMEMHDR_MAGIC, ("pHdr->u32Magic=%RX32 pv=%p cb=%#x\n", pHdr->u32Magic, pv, cb));
138 pHdr->u32Magic = RTMEMHDR_MAGIC_DEAD;
139 Assert(pHdr->cbReq == cb); RT_NOREF_PV(cb);
140
141 if (pHdr->fFlags & (RTMEMALLOCEX_FLAGS_16BIT_REACH | RTMEMALLOCEX_FLAGS_32BIT_REACH))
142 rtMemFreeExYyBitReach(pHdr, pHdr->cb + sizeof(*pHdr), pHdr->fFlags);
143 else if (pHdr->fFlags & RTMEMALLOCEX_FLAGS_EXEC)
144 {
145 RTMemProtect(pHdr, pHdr->cb + sizeof(*pHdr), RTMEM_PROT_READ | RTMEM_PROT_WRITE);
146 RTMemPageFree(pHdr, pHdr->cb + sizeof(*pHdr));
147 }
148 else
149 RTMemFree(pHdr);
150}
151RT_EXPORT_SYMBOL(RTMemFreeEx);
152
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