1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* $Id: message.cpp $ */
/** @file
* Base class for wrapping HCGM messages.
*/
/*
* Copyright (C) 2018-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <VBox/HostServices/Service.h>
#include <VBox/VMMDev.h> /* For VMMDEV_MAX_HGCM_PARMS. */
using namespace HGCM;
Message::Message(void)
: m_uMsg(0)
, m_cParms(0)
, m_paParms(NULL)
{
}
Message::Message(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM aParms[])
: m_uMsg(0)
, m_cParms(0)
, m_paParms(NULL)
{
initData(uMsg, cParms, aParms);
}
Message::~Message(void)
{
reset();
}
/**
* Resets the message by free'ing all allocated parameters and resetting the rest.
*/
void Message::reset(void) RT_NOEXCEPT
{
if (m_paParms)
{
for (uint32_t i = 0; i < m_cParms; ++i)
{
switch (m_paParms[i].type)
{
case VBOX_HGCM_SVC_PARM_PTR:
if (m_paParms[i].u.pointer.size)
RTMemFree(m_paParms[i].u.pointer.addr);
break;
}
}
RTMemFree(m_paParms);
m_paParms = 0;
}
m_cParms = 0;
m_uMsg = 0;
}
/**
* Returns the parameter count of this message.
*
* @returns Parameter count.
*/
uint32_t Message::GetParamCount(void) const RT_NOEXCEPT
{
return m_cParms;
}
/**
* Retrieves the raw HGCM parameter data
*
* @returns IPRT status code.
* @param uMsg Message type to retrieve the parameter data for. Needed for sanity.
* @param cParms Size (in parameters) of @a aParms array.
* @param aParms Where to store the HGCM parameter data.
*/
int Message::GetData(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM aParms[]) const RT_NOEXCEPT
{
if (m_uMsg != uMsg)
{
LogFlowFunc(("Stored message type (%RU32) does not match request (%RU32)\n", m_uMsg, uMsg));
return VERR_INVALID_PARAMETER;
}
if (m_cParms == 0) /* Nothing to copy, take a shortcut. */
return VINF_SUCCESS;
if (m_cParms > cParms)
{
LogFlowFunc(("Stored parameter count (%RU32) exceeds request buffer (%RU32)\n", m_cParms, cParms));
return VERR_INVALID_PARAMETER;
}
return Message::CopyParms(&aParms[0], cParms, m_paParms, m_cParms, false /* fDeepCopy */);
}
/**
* Retrieves a specific parameter value as uint32_t.
*
* @returns IPRT status code.
* @param uParm Index of parameter to retrieve.
* @param pu32Info Where to store the parameter value.
*/
int Message::GetParmU32(uint32_t uParm, uint32_t *pu32Info) const RT_NOEXCEPT
{
AssertPtrReturn(pu32Info, VERR_INVALID_PARAMETER);
AssertReturn(uParm < m_cParms, VERR_INVALID_PARAMETER);
AssertReturn(m_paParms[uParm].type == VBOX_HGCM_SVC_PARM_32BIT, VERR_INVALID_PARAMETER);
*pu32Info = m_paParms[uParm].u.uint32;
return VINF_SUCCESS;
}
/**
* Retrieves a specific parameter value as uint64_t.
*
* @returns IPRT status code.
* @param uParm Index of parameter to retrieve.
* @param pu64Info Where to store the parameter value.
*/
int Message::GetParmU64(uint32_t uParm, uint64_t *pu64Info) const RT_NOEXCEPT
{
AssertPtrReturn(pu64Info, VERR_INVALID_PARAMETER);
AssertReturn(uParm < m_cParms, VERR_INVALID_PARAMETER);
AssertReturn(m_paParms[uParm].type == VBOX_HGCM_SVC_PARM_64BIT, VERR_INVALID_PARAMETER);
*pu64Info = m_paParms[uParm].u.uint64;
return VINF_SUCCESS;
}
/**
* Retrieves a specific parameter value as a data address + size.
*
* @returns IPRT status code.
* @param uParm Index of parameter to retrieve.
* @param ppvAddr Where to store the data address.
* @param pcbSize Where to store the data size (in bytes).
*
* @remarks Does not copy (store) the actual content of the pointer (deep copy).
*/
int Message::GetParmPtr(uint32_t uParm, void **ppvAddr, uint32_t *pcbSize) const RT_NOEXCEPT
{
AssertPtrReturn(ppvAddr, VERR_INVALID_PARAMETER);
AssertPtrReturn(pcbSize, VERR_INVALID_PARAMETER);
AssertReturn(uParm < m_cParms, VERR_INVALID_PARAMETER);
AssertReturn(m_paParms[uParm].type == VBOX_HGCM_SVC_PARM_PTR, VERR_INVALID_PARAMETER);
*ppvAddr = m_paParms[uParm].u.pointer.addr;
*pcbSize = m_paParms[uParm].u.pointer.size;
return VINF_SUCCESS;
}
/**
* Returns the type of this message.
*
* @returns Message type.
*/
uint32_t Message::GetType(void) const RT_NOEXCEPT
{
return m_uMsg;
}
/**
* Copies HGCM parameters from source to destination.
*
* @returns IPRT status code.
* @param paParmsDst Destination array to copy parameters to.
* @param cParmsDst Size (in parameters) of destination array.
* @param paParmsSrc Source array to copy parameters from.
* @param cParmsSrc Size (in parameters) of source array.
* @param fDeepCopy Whether to perform a deep copy of pointer parameters or not.
*
* @remark Static convenience function.
*/
/* static */
int Message::CopyParms(PVBOXHGCMSVCPARM paParmsDst, uint32_t cParmsDst,
PVBOXHGCMSVCPARM paParmsSrc, uint32_t cParmsSrc,
bool fDeepCopy) RT_NOEXCEPT
{
AssertPtrReturn(paParmsSrc, VERR_INVALID_POINTER);
AssertPtrReturn(paParmsDst, VERR_INVALID_POINTER);
if (cParmsSrc > cParmsDst)
return VERR_BUFFER_OVERFLOW;
for (uint32_t i = 0; i < cParmsSrc; i++)
{
paParmsDst[i].type = paParmsSrc[i].type;
switch (paParmsSrc[i].type)
{
case VBOX_HGCM_SVC_PARM_32BIT:
{
paParmsDst[i].u.uint32 = paParmsSrc[i].u.uint32;
break;
}
case VBOX_HGCM_SVC_PARM_64BIT:
{
paParmsDst[i].u.uint64 = paParmsSrc[i].u.uint64;
break;
}
case VBOX_HGCM_SVC_PARM_PTR:
{
/* Do we have to perform a deep copy? */
if (fDeepCopy)
{
/* Yes, do so. */
paParmsDst[i].u.pointer.size = paParmsSrc[i].u.pointer.size;
if (paParmsDst[i].u.pointer.size > 0)
{
paParmsDst[i].u.pointer.addr = RTMemAlloc(paParmsDst[i].u.pointer.size);
if (!paParmsDst[i].u.pointer.addr)
return VERR_NO_MEMORY;
}
}
else
{
/* No, but we have to check if there is enough room. */
if (paParmsDst[i].u.pointer.size < paParmsSrc[i].u.pointer.size)
return VERR_BUFFER_OVERFLOW;
}
if (paParmsSrc[i].u.pointer.size)
{
if ( paParmsDst[i].u.pointer.addr
&& paParmsDst[i].u.pointer.size)
memcpy(paParmsDst[i].u.pointer.addr,
paParmsSrc[i].u.pointer.addr,
RT_MIN(paParmsDst[i].u.pointer.size, paParmsSrc[i].u.pointer.size));
else
return VERR_INVALID_POINTER;
}
break;
}
default:
{
AssertMsgFailed(("Unknown HGCM type %u\n", paParmsSrc[i].type));
return VERR_INVALID_PARAMETER;
}
}
}
return VINF_SUCCESS;
}
/**
* Initializes the message with a message type and parameters.
*
* @returns IPRT status code.
* @param uMsg Message type to set.
* @param cParms Number of parameters to set.
* @param aParms Array of parameters to set.
*/
int Message::initData(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM aParms[]) RT_NOEXCEPT
{
AssertReturn(cParms < VMMDEV_MAX_HGCM_PARMS, VERR_INVALID_PARAMETER);
AssertReturn(cParms == 0 || aParms != NULL, VERR_INVALID_POINTER);
/* Cleanup any eventual old stuff. */
reset();
m_uMsg = uMsg;
m_cParms = cParms;
int rc = VINF_SUCCESS;
if (cParms)
{
m_paParms = (VBOXHGCMSVCPARM*)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * m_cParms);
if (m_paParms)
{
rc = Message::CopyParms(m_paParms, m_cParms, &aParms[0], cParms, true /* fDeepCopy */);
if (RT_FAILURE(rc))
reset();
}
else
rc = VERR_NO_MEMORY;
}
return rc;
}
|