1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_SRC_CROSSCALL_PARAMS_H__
6 #define SANDBOX_SRC_CROSSCALL_PARAMS_H__
13 #include "base/basictypes.h"
14 #include "sandbox/win/src/internal_types.h"
15 #include "sandbox/win/src/sandbox_types.h"
19 // Increases |value| until there is no need for padding given an int64
20 // alignment. Returns the increased value.
21 uint32
Align(uint32 value
) {
22 uint32 alignment
= sizeof(int64
);
23 return ((value
+ alignment
- 1) / alignment
) * alignment
;
27 // This header is part of CrossCall: the sandbox inter-process communication.
28 // This header defines the basic types used both in the client IPC and in the
29 // server IPC code. CrossCallParams and ActualCallParams model the input
30 // parameters of an IPC call and CrossCallReturn models the output params and
33 // An IPC call is defined by its 'tag' which is a (uint32) unique identifier
34 // that is used to route the IPC call to the proper server. Every tag implies
35 // a complete call signature including the order and type of each parameter.
37 // Like most IPC systems. CrossCall is designed to take as inputs 'simple'
38 // types such as integers and strings. Classes, generic arrays or pointers to
39 // them are not supported.
41 // Another limitation of CrossCall is that the return value and output
42 // parameters can only be uint32 integers. Returning complex structures or
43 // strings is not supported.
47 // max number of extended return parameters. See CrossCallReturn
48 const size_t kExtendedReturnCount
= 8;
50 // Union of multiple types to be used as extended results
51 // in the CrossCallReturn.
59 // Maximum number of IPC parameters currently supported.
60 // To increase this value, we have to:
61 // - Add another Callback typedef to Dispatcher.
62 // - Add another case to the switch on SharedMemIPCServer::InvokeCallback.
63 // - Add another case to the switch in GetActualAndMaxBufferSize
64 const int kMaxIpcParams
= 9;
66 // Contains the information about a parameter in the ipc buffer.
73 // Models the return value and the return parameters of an IPC call
74 // currently limited to one status code and eight generic return values
75 // which cannot be pointers to other data. For x64 ports this structure
76 // might have to use other integer types.
77 struct CrossCallReturn
{
78 // the IPC tag. It should match the original IPC tag.
80 // The result of the IPC operation itself.
81 ResultCode call_outcome
;
82 // the result of the IPC call as executed in the server. The interpretation
83 // of this value depends on the specific service.
88 // Number of extended return values.
89 uint32 extended_count
;
90 // for calls that should return a windows handle. It is found here.
92 // The array of extended values.
93 MultiType extended
[kExtendedReturnCount
];
96 // CrossCallParams base class that models the input params all packed in a
97 // single compact memory blob. The representation can vary but in general a
98 // given child of this class is meant to represent all input parameters
99 // necessary to make a IPC call.
101 // This class cannot have virtual members because its assumed the IPC
102 // parameters start from the 'this' pointer to the end, which is defined by
103 // one of the subclasses
105 // Objects of this class cannot be constructed directly. Only derived
106 // classes have the proper knowledge to construct it.
107 class CrossCallParams
{
109 // Returns the tag (ipc unique id) associated with this IPC.
110 uint32
GetTag() const {
114 // Returns the beggining of the buffer where the IPC params can be stored.
115 // prior to an IPC call
116 const void* GetBuffer() const {
120 // Returns how many parameter this IPC call should have.
121 const uint32
GetParamsCount() const {
122 return params_count_
;
125 // Returns a pointer to the CrossCallReturn structure.
126 CrossCallReturn
* GetCallReturn() {
130 // Returns TRUE if this call contains InOut parameters.
131 const bool IsInOut() const {
132 return (1 == is_in_out_
);
135 // Tells the CrossCall object if it contains InOut parameters.
136 void SetIsInOut(bool value
) {
144 // constructs the IPC call params. Called only from the derived classes
145 CrossCallParams(uint32 tag
, uint32 params_count
)
146 : tag_(tag
), is_in_out_(0), params_count_(params_count
) {}
151 CrossCallReturn call_return
;
152 const uint32 params_count_
;
153 DISALLOW_COPY_AND_ASSIGN(CrossCallParams
);
156 // ActualCallParams models an specific IPC call parameters with respect to the
157 // storage allocation that the packed parameters should need.
158 // NUMBER_PARAMS: the number of parameters, valid from 1 to N
159 // BLOCK_SIZE: the total storage that the NUMBER_PARAMS parameters can take,
160 // typically the block size is defined by the channel size of the underlying
162 // In practice this class is used to levergage C++ capacity to properly
163 // calculate sizes and displacements given the possibility of the packed params
164 // blob to be complex.
166 // As is, this class assumes that the layout of the blob is as follows. Assume
167 // that NUMBER_PARAMS = 2 and a 32-bit build:
170 // [ IsOnOut 4 bytes]
171 // [ call return 52 bytes]
172 // [ params count 4 bytes]
173 // [ parameter 0 type 4 bytes]
174 // [ parameter 0 offset 4 bytes] ---delta to ---\
175 // [ parameter 0 size 4 bytes] |
176 // [ parameter 1 type 4 bytes] |
177 // [ parameter 1 offset 4 bytes] ---------------|--\
178 // [ parameter 1 size 4 bytes] | |
179 // [ parameter 2 type 4 bytes] | |
180 // [ parameter 2 offset 4 bytes] ----------------------\
181 // [ parameter 2 size 4 bytes] | | |
182 // |---------------------------| | | |
183 // | value 0 (x bytes) | <--------------/ | |
184 // | value 1 (y bytes) | <-----------------/ |
186 // | end of buffer | <---------------------/
187 // |---------------------------|
189 // Note that the actual number of params is NUMBER_PARAMS + 1
190 // so that the size of each actual param can be computed from the difference
191 // between one parameter and the next down. The offset of the last param
192 // points to the end of the buffer and the type and size are undefined.
194 template <size_t NUMBER_PARAMS
, size_t BLOCK_SIZE
>
195 class ActualCallParams
: public CrossCallParams
{
197 // constructor. Pass the ipc unique tag as input
198 explicit ActualCallParams(uint32 tag
)
199 : CrossCallParams(tag
, NUMBER_PARAMS
) {
200 param_info_
[0].offset_
=
201 static_cast<uint32
>(parameters_
- reinterpret_cast<char*>(this));
204 // Testing-only constructor. Allows setting the |number_params| to a
206 ActualCallParams(uint32 tag
, uint32 number_params
)
207 : CrossCallParams(tag
, number_params
) {
208 param_info_
[0].offset_
=
209 static_cast<uint32
>(parameters_
- reinterpret_cast<char*>(this));
212 // Testing-only method. Allows setting the apparent size to a wrong value.
213 // returns the previous size.
214 uint32
OverrideSize(uint32 new_size
) {
215 uint32 previous_size
= param_info_
[NUMBER_PARAMS
].offset_
;
216 param_info_
[NUMBER_PARAMS
].offset_
= new_size
;
217 return previous_size
;
220 // Copies each paramter into the internal buffer. For each you must supply:
221 // index: 0 for the first param, 1 for the next an so on
222 bool CopyParamIn(uint32 index
, const void* parameter_address
, uint32 size
,
223 bool is_in_out
, ArgType type
) {
224 if (index
>= NUMBER_PARAMS
) {
228 if (kuint32max
== size
) {
229 // Memory error while getting the size.
233 if (size
&& !parameter_address
) {
237 if ((size
> sizeof(*this)) ||
238 (param_info_
[index
].offset_
> (sizeof(*this) - size
))) {
239 // It does not fit, abort copy.
243 char* dest
= reinterpret_cast<char*>(this) + param_info_
[index
].offset_
;
245 // We might be touching user memory, this has to be done from inside a try
248 memcpy(dest
, parameter_address
, size
);
250 __except(EXCEPTION_EXECUTE_HANDLER
) {
254 // Set the flag to tell the broker to update the buffer once the call is
259 param_info_
[index
+ 1].offset_
= Align(param_info_
[index
].offset_
+
261 param_info_
[index
].size_
= size
;
262 param_info_
[index
].type_
= type
;
266 // Returns a pointer to a parameter in the memory section.
267 void* GetParamPtr(size_t index
) {
268 return reinterpret_cast<char*>(this) + param_info_
[index
].offset_
;
271 // Returns the total size of the buffer. Only valid once all the paramters
272 // have been copied in with CopyParamIn.
273 uint32
GetSize() const {
274 return param_info_
[NUMBER_PARAMS
].offset_
;
278 ActualCallParams() : CrossCallParams(0, NUMBER_PARAMS
) { }
281 ParamInfo param_info_
[NUMBER_PARAMS
+ 1];
282 char parameters_
[BLOCK_SIZE
- sizeof(CrossCallParams
)
283 - sizeof(ParamInfo
) * (NUMBER_PARAMS
+ 1)];
284 DISALLOW_COPY_AND_ASSIGN(ActualCallParams
);
287 static_assert(sizeof(ActualCallParams
<1, 1024>) == 1024, "bad size buffer");
288 static_assert(sizeof(ActualCallParams
<2, 1024>) == 1024, "bad size buffer");
289 static_assert(sizeof(ActualCallParams
<3, 1024>) == 1024, "bad size buffer");
291 } // namespace sandbox
293 #endif // SANDBOX_SRC_CROSSCALL_PARAMS_H__