[RPCRT4_WINETEST] Sync with Wine Staging 1.9.23. CORE-12409
[reactos.git] / rostests / winetests / rpcrt4 / ndr_marshall.c
1 /*
2 * Unit test suite for ndr marshalling functions
3 *
4 * Copyright 2006 Huw Davies
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define _WIN32_WINNT 0x0500
22 #define NTDDI_WIN2K 0x05000000
23 #define NTDDI_VERSION NTDDI_WIN2K /* for some MIDL_STUB_MESSAGE fields */
24
25 #include <stdarg.h>
26
27 #include "wine/test.h"
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnt.h>
31 #include <winerror.h>
32 #include <ole2.h>
33
34 #include "rpc.h"
35 #include "rpcdce.h"
36 #include "rpcproxy.h"
37 #include "midles.h"
38
39 static int my_alloc_called;
40 static int my_free_called;
41 static void * CALLBACK my_alloc(SIZE_T size)
42 {
43 my_alloc_called++;
44 return NdrOleAllocate(size);
45 }
46
47 static void CALLBACK my_free(void *ptr)
48 {
49 my_free_called++;
50 NdrOleFree(ptr);
51 }
52
53 static const MIDL_STUB_DESC Object_StubDesc =
54 {
55 NULL,
56 my_alloc,
57 my_free,
58 { 0 },
59 0,
60 0,
61 0,
62 0,
63 NULL, /* format string, filled in by tests */
64 1, /* -error bounds_check flag */
65 0x20000, /* Ndr library version */
66 0,
67 0x50100a4, /* MIDL Version 5.1.164 */
68 0,
69 NULL,
70 0, /* notify & notify_flag routine table */
71 1, /* Flags */
72 0, /* Reserved3 */
73 0, /* Reserved4 */
74 0 /* Reserved5 */
75 };
76
77 static RPC_DISPATCH_FUNCTION IFoo_table[] =
78 {
79 0
80 };
81
82 static RPC_DISPATCH_TABLE IFoo_v0_0_DispatchTable =
83 {
84 0,
85 IFoo_table
86 };
87
88 static const RPC_SERVER_INTERFACE IFoo___RpcServerInterface =
89 {
90 sizeof(RPC_SERVER_INTERFACE),
91 {{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x34}},{0,0}},
92 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
93 &IFoo_v0_0_DispatchTable,
94 0,
95 0,
96 0,
97 0,
98 0,
99 };
100
101 static RPC_IF_HANDLE IFoo_v0_0_s_ifspec = (RPC_IF_HANDLE)& IFoo___RpcServerInterface;
102 static BOOL use_pointer_ids = FALSE;
103
104 static void determine_pointer_marshalling_style(void)
105 {
106 RPC_MESSAGE RpcMessage;
107 MIDL_STUB_MESSAGE StubMsg;
108 MIDL_STUB_DESC StubDesc;
109 char ch = 0xde;
110
111 static const unsigned char fmtstr_up_char[] =
112 {
113 0x12, 0x8, /* FC_UP [simple_pointer] */
114 0x2, /* FC_CHAR */
115 0x5c, /* FC_PAD */
116 };
117
118 StubDesc = Object_StubDesc;
119 StubDesc.pFormatTypes = NULL;
120
121 NdrClientInitializeNew(
122 &RpcMessage,
123 &StubMsg,
124 &StubDesc,
125 0);
126
127 StubMsg.BufferLength = 8;
128 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
129 NdrPointerMarshall(&StubMsg, (unsigned char*)&ch, fmtstr_up_char);
130 ok(StubMsg.Buffer == StubMsg.BufferStart + 5, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
131
132 use_pointer_ids = (*(unsigned int *)StubMsg.BufferStart != (UINT_PTR)&ch);
133 trace("Pointer marshalling using %s\n", use_pointer_ids ? "pointer ids" : "pointer value");
134
135 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
136 }
137
138 static void test_ndr_simple_type(void)
139 {
140 RPC_MESSAGE RpcMessage;
141 MIDL_STUB_MESSAGE StubMsg;
142 MIDL_STUB_DESC StubDesc;
143 LONG l, l2 = 0;
144
145 StubDesc = Object_StubDesc;
146 StubDesc.pFormatTypes = NULL;
147
148 NdrClientInitializeNew(
149 &RpcMessage,
150 &StubMsg,
151 &StubDesc,
152 0);
153
154 StubMsg.BufferLength = 16;
155 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
156 l = 0xcafebabe;
157 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
158 ok(StubMsg.Buffer == StubMsg.BufferStart + 4, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
159 ok(*(LONG*)StubMsg.BufferStart == l, "%d\n", *(LONG*)StubMsg.BufferStart);
160
161 StubMsg.Buffer = StubMsg.BufferStart + 1;
162 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
163 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
164 ok(*(LONG*)(StubMsg.BufferStart + 4) == l, "%d\n", *(LONG*)StubMsg.BufferStart);
165
166 StubMsg.Buffer = StubMsg.BufferStart + 1;
167 NdrSimpleTypeUnmarshall(&StubMsg, (unsigned char*)&l2, 8 /* FC_LONG */);
168 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
169 ok(l2 == l, "%d\n", l2);
170
171 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
172 }
173
174 static void test_pointer_marshal(const unsigned char *formattypes,
175 void *memsrc, DWORD srcsize,
176 const void *wiredata,
177 ULONG wiredatalen,
178 int(*cmp)(const void*,const void*,size_t),
179 int num_additional_allocs,
180 const char *msgpfx)
181 {
182 RPC_MESSAGE RpcMessage;
183 MIDL_STUB_MESSAGE StubMsg;
184 MIDL_STUB_DESC StubDesc;
185 DWORD size;
186 void *ptr;
187 unsigned char *mem, *mem_orig;
188
189 my_alloc_called = my_free_called = 0;
190 if(!cmp)
191 cmp = memcmp;
192
193 StubDesc = Object_StubDesc;
194 StubDesc.pFormatTypes = formattypes;
195
196 NdrClientInitializeNew(
197 &RpcMessage,
198 &StubMsg,
199 &StubDesc,
200 0);
201
202 StubMsg.BufferLength = 0;
203 NdrPointerBufferSize( &StubMsg,
204 memsrc,
205 formattypes );
206 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
207
208 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
209 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
210 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
211
212 memset(StubMsg.BufferStart, 0x0, StubMsg.BufferLength); /* This is a hack to clear the padding between the ptr and longlong/double */
213
214 ptr = NdrPointerMarshall( &StubMsg, memsrc, formattypes );
215 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
216 if (srcsize == 8 && wiredatalen == 16 && StubMsg.Buffer - StubMsg.BufferStart == 12)
217 {
218 /* win9x doesn't align 8-byte types properly */
219 wiredatalen = 12;
220 }
221 else
222 {
223 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
224 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled\n", msgpfx);
225 }
226
227 StubMsg.Buffer = StubMsg.BufferStart;
228 StubMsg.MemorySize = 0;
229
230 size = NdrPointerMemorySize( &StubMsg, formattypes );
231 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
232 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
233 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
234 ok(size == srcsize + sizeof(void *), "%s: mem size %u\n", msgpfx, size);
235 else
236 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
237
238 StubMsg.Buffer = StubMsg.BufferStart;
239 StubMsg.MemorySize = 16;
240 size = NdrPointerMemorySize( &StubMsg, formattypes );
241 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
242 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
243 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
244 ok(size == srcsize + sizeof(void *) + 16, "%s: mem size %u\n", msgpfx, size);
245 else
246 ok(size == srcsize + 16, "%s: mem size %u\n", msgpfx, size);
247
248 StubMsg.Buffer = StubMsg.BufferStart;
249 StubMsg.MemorySize = 1;
250 size = NdrPointerMemorySize( &StubMsg, formattypes );
251 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
252 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
253 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
254 ok(size == srcsize + sizeof(void *) + (srcsize == 8 ? 8 : sizeof(void *)), "%s: mem size %u\n", msgpfx, size);
255 else
256 ok(size == srcsize + (srcsize == 8 ? 8 : sizeof(void *)), "%s: mem size %u\n", msgpfx, size);
257
258 size = srcsize;
259 if(formattypes[1] & 0x10) size += 4;
260
261 StubMsg.Buffer = StubMsg.BufferStart;
262 StubMsg.MemorySize = 0;
263 mem_orig = mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
264
265 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
266 *(void**)mem = NULL;
267 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
268 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
269 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
270 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
271 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
272 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
273 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
274 my_alloc_called = 0;
275
276 /* reset the buffer and call with must alloc */
277 StubMsg.Buffer = StubMsg.BufferStart;
278 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
279 *(void**)mem = NULL;
280 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 1 );
281 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
282 /* doesn't allocate mem in this case */
283 todo_wine {
284 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
285 }
286 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
287 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
288 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
289
290 todo_wine {
291 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
292 }
293 my_alloc_called = 0;
294 if(formattypes[0] != 0x11 /* FC_RP */)
295 {
296 /* now pass the address of a NULL ptr */
297 mem = NULL;
298 StubMsg.Buffer = StubMsg.BufferStart;
299 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
300 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
301 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
302 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
303 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
304 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
305 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
306 my_alloc_called = 0;
307 NdrPointerFree(&StubMsg, mem, formattypes);
308
309 /* again pass address of NULL ptr, but pretend we're a server */
310 if (0) /* crashes on Win9x and NT4 */
311 {
312 mem = NULL;
313 StubMsg.Buffer = StubMsg.BufferStart;
314 StubMsg.IsClient = 0;
315 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
316 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
317 if (formattypes[2] == 0xd /* FC_ENUM16 */)
318 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
319 else
320 ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
321 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
322 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
323 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
324 if (formattypes[2] != 0xd /* FC_ENUM16 */) {
325 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
326 my_alloc_called = 0;
327 }
328 }
329 }
330 HeapFree(GetProcessHeap(), 0, mem_orig);
331 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
332 }
333
334 static int deref_cmp(const void *s1, const void *s2, size_t num)
335 {
336 return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
337 }
338
339
340 static void test_simple_types(void)
341 {
342 unsigned char wiredata[16];
343 unsigned char ch;
344 unsigned char *ch_ptr;
345 unsigned short s;
346 unsigned int i;
347 ULONG l;
348 ULONGLONG ll;
349 float f;
350 double d;
351
352 static const unsigned char fmtstr_up_char[] =
353 {
354 0x12, 0x8, /* FC_UP [simple_pointer] */
355 0x2, /* FC_CHAR */
356 0x5c, /* FC_PAD */
357 };
358 static const unsigned char fmtstr_up_byte[] =
359 {
360 0x12, 0x8, /* FC_UP [simple_pointer] */
361 0x1, /* FC_BYTE */
362 0x5c, /* FC_PAD */
363 };
364 static const unsigned char fmtstr_up_small[] =
365 {
366 0x12, 0x8, /* FC_UP [simple_pointer] */
367 0x3, /* FC_SMALL */
368 0x5c, /* FC_PAD */
369 };
370 static const unsigned char fmtstr_up_usmall[] =
371 {
372 0x12, 0x8, /* FC_UP [simple_pointer] */
373 0x4, /* FC_USMALL */
374 0x5c, /* FC_PAD */
375 };
376 static const unsigned char fmtstr_rp_char[] =
377 {
378 0x11, 0x8, /* FC_RP [simple_pointer] */
379 0x2, /* FC_CHAR */
380 0x5c, /* FC_PAD */
381 };
382 static const unsigned char fmtstr_rpup_char[] =
383 {
384 0x11, 0x14, /* FC_RP [alloced_on_stack] */
385 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
386 0x12, 0x8, /* FC_UP [simple_pointer] */
387 0x2, /* FC_CHAR */
388 0x5c, /* FC_PAD */
389 };
390 static const unsigned char fmtstr_rpup_char2[] =
391 {
392 0x11, 0x04, /* FC_RP [alloced_on_stack] */
393 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
394 0x12, 0x8, /* FC_UP [simple_pointer] */
395 0x2, /* FC_CHAR */
396 0x5c, /* FC_PAD */
397 };
398
399 static const unsigned char fmtstr_up_wchar[] =
400 {
401 0x12, 0x8, /* FC_UP [simple_pointer] */
402 0x5, /* FC_WCHAR */
403 0x5c, /* FC_PAD */
404 };
405 static const unsigned char fmtstr_up_short[] =
406 {
407 0x12, 0x8, /* FC_UP [simple_pointer] */
408 0x6, /* FC_SHORT */
409 0x5c, /* FC_PAD */
410 };
411 static const unsigned char fmtstr_up_ushort[] =
412 {
413 0x12, 0x8, /* FC_UP [simple_pointer] */
414 0x7, /* FC_USHORT */
415 0x5c, /* FC_PAD */
416 };
417 static const unsigned char fmtstr_up_enum16[] =
418 {
419 0x12, 0x8, /* FC_UP [simple_pointer] */
420 0xd, /* FC_ENUM16 */
421 0x5c, /* FC_PAD */
422 };
423 static const unsigned char fmtstr_up_long[] =
424 {
425 0x12, 0x8, /* FC_UP [simple_pointer] */
426 0x8, /* FC_LONG */
427 0x5c, /* FC_PAD */
428 };
429 static const unsigned char fmtstr_up_ulong[] =
430 {
431 0x12, 0x8, /* FC_UP [simple_pointer] */
432 0x9, /* FC_ULONG */
433 0x5c, /* FC_PAD */
434 };
435 static const unsigned char fmtstr_up_enum32[] =
436 {
437 0x12, 0x8, /* FC_UP [simple_pointer] */
438 0xe, /* FC_ENUM32 */
439 0x5c, /* FC_PAD */
440 };
441 static const unsigned char fmtstr_up_errorstatus[] =
442 {
443 0x12, 0x8, /* FC_UP [simple_pointer] */
444 0x10, /* FC_ERROR_STATUS_T */
445 0x5c, /* FC_PAD */
446 };
447
448 static const unsigned char fmtstr_up_longlong[] =
449 {
450 0x12, 0x8, /* FC_UP [simple_pointer] */
451 0xb, /* FC_HYPER */
452 0x5c, /* FC_PAD */
453 };
454 static const unsigned char fmtstr_up_float[] =
455 {
456 0x12, 0x8, /* FC_UP [simple_pointer] */
457 0xa, /* FC_FLOAT */
458 0x5c, /* FC_PAD */
459 };
460 static const unsigned char fmtstr_up_double[] =
461 {
462 0x12, 0x8, /* FC_UP [simple_pointer] */
463 0xc, /* FC_DOUBLE */
464 0x5c, /* FC_PAD */
465 };
466
467 ch = 0xa5;
468 ch_ptr = &ch;
469 if (use_pointer_ids)
470 *(unsigned int *)wiredata = 0x20000;
471 else
472 *(unsigned int *)wiredata = (UINT_PTR)ch_ptr;
473 wiredata[4] = ch;
474
475 test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
476 test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
477 test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0, "up_small");
478 test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
479
480 test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
481
482 test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
483 test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
484
485 s = 0xa597;
486 if (use_pointer_ids)
487 *(unsigned int *)wiredata = 0x20000;
488 else
489 *(unsigned int *)wiredata = (UINT_PTR)&s;
490 *(unsigned short*)(wiredata + 4) = s;
491
492 test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
493 test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
494 test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
495
496 i = 0x7fff;
497 if (use_pointer_ids)
498 *(unsigned int *)wiredata = 0x20000;
499 else
500 *(unsigned int *)wiredata = (UINT_PTR)&i;
501 *(unsigned short*)(wiredata + 4) = i;
502 test_pointer_marshal(fmtstr_up_enum16, &i, 4, wiredata, 6, NULL, 0, "up_enum16");
503
504 l = 0xcafebabe;
505 if (use_pointer_ids)
506 *(unsigned int *)wiredata = 0x20000;
507 else
508 *(unsigned int *)wiredata = (UINT_PTR)&l;
509 *(ULONG*)(wiredata + 4) = l;
510
511 test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
512 test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0, "up_ulong");
513 test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0, "up_emun32");
514 test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0, "up_errorstatus");
515
516 ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
517 if (use_pointer_ids)
518 *(unsigned int *)wiredata = 0x20000;
519 else
520 *(unsigned int *)wiredata = (UINT_PTR)&ll;
521 *(unsigned int *)(wiredata + 4) = 0;
522 *(ULONGLONG*)(wiredata + 8) = ll;
523 test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
524
525 f = 3.1415f;
526 if (use_pointer_ids)
527 *(unsigned int *)wiredata = 0x20000;
528 else
529 *(unsigned int *)wiredata = (UINT_PTR)&f;
530 *(float*)(wiredata + 4) = f;
531 test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
532
533 d = 3.1415;
534 if (use_pointer_ids)
535 *(unsigned int *)wiredata = 0x20000;
536 else
537 *(unsigned int *)wiredata = (UINT_PTR)&d;
538 *(unsigned int *)(wiredata + 4) = 0;
539 *(double*)(wiredata + 8) = d;
540 test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0, "up_double");
541
542 }
543
544 static void test_nontrivial_pointer_types(void)
545 {
546 RPC_MESSAGE RpcMessage;
547 MIDL_STUB_MESSAGE StubMsg;
548 MIDL_STUB_DESC StubDesc;
549 DWORD size;
550 void *ptr;
551 char **p1;
552 char *p2;
553 char ch;
554 unsigned char *mem, *mem_orig;
555
556 static const unsigned char fmtstr_ref_unique_out[] =
557 {
558 0x12, 0x8, /* FC_UP [simple_pointer] */
559 0x2, /* FC_CHAR */
560 0x5c, /* FC_PAD */
561 0x11, 0x14, /* FC_RP [alloced_on_stack] [pointer_deref] */
562 NdrFcShort( 0xfffffffa ), /* Offset= -6 (0) */
563 };
564
565 p1 = &p2;
566 p2 = &ch;
567 ch = 0x22;
568
569 StubDesc = Object_StubDesc;
570 StubDesc.pFormatTypes = fmtstr_ref_unique_out;
571
572 NdrClientInitializeNew(
573 &RpcMessage,
574 &StubMsg,
575 &StubDesc,
576 0);
577
578 StubMsg.BufferLength = 0;
579 NdrPointerBufferSize( &StubMsg,
580 (unsigned char *)p1,
581 &fmtstr_ref_unique_out[4] );
582
583 /* Windows overestimates the buffer size */
584 ok(StubMsg.BufferLength >= 5, "length %d\n", StubMsg.BufferLength);
585
586 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
587 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
588 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
589
590 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)p1, &fmtstr_ref_unique_out[4] );
591 ok(ptr == NULL, "ret %p\n", ptr);
592 size = StubMsg.Buffer - StubMsg.BufferStart;
593 ok(size == 5, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, size);
594 ok(*(unsigned int *)StubMsg.BufferStart != 0, "pointer ID marshalled incorrectly\n");
595 ok(*(unsigned char *)(StubMsg.BufferStart + 4) == 0x22, "char data marshalled incorrectly: 0x%x\n",
596 *(unsigned char *)(StubMsg.BufferStart + 4));
597
598 StubMsg.Buffer = StubMsg.BufferStart;
599 StubMsg.MemorySize = 0;
600 mem = NULL;
601
602 /* Client */
603 my_alloc_called = 0;
604 StubMsg.Buffer = StubMsg.BufferStart;
605 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(void *));
606 *(void **)mem = NULL;
607 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
608 ok(mem == mem_orig, "mem alloced\n");
609 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
610
611 my_alloc_called = 0;
612 StubMsg.Buffer = StubMsg.BufferStart;
613 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
614 todo_wine {
615 ok(mem == mem_orig, "mem alloced\n");
616 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
617 }
618
619 my_free_called = 0;
620 StubMsg.Buffer = StubMsg.BufferStart;
621 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
622 ok(my_free_called == 1, "free called %d\n", my_free_called);
623
624 mem = my_alloc(sizeof(void *));
625 *(void **)mem = NULL;
626 my_free_called = 0;
627 StubMsg.Buffer = StubMsg.BufferStart;
628 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
629 ok(my_free_called == 0, "free called %d\n", my_free_called);
630 my_free(mem);
631
632 mem = my_alloc(sizeof(void *));
633 *(void **)mem = my_alloc(sizeof(char));
634 my_free_called = 0;
635 StubMsg.Buffer = StubMsg.BufferStart;
636 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
637 ok(my_free_called == 1, "free called %d\n", my_free_called);
638 my_free(mem);
639
640 /* Server */
641 my_alloc_called = 0;
642 StubMsg.IsClient = 0;
643 mem = NULL;
644 StubMsg.Buffer = StubMsg.BufferStart;
645 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
646 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
647 todo_wine
648 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
649 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
650
651 my_alloc_called = 0;
652 mem = NULL;
653 StubMsg.Buffer = StubMsg.BufferStart;
654 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
655 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
656 todo_wine
657 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
658 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
659
660 my_alloc_called = 0;
661 mem = mem_orig;
662 *(void **)mem = NULL;
663 StubMsg.Buffer = StubMsg.BufferStart;
664 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
665 todo_wine {
666 ok(mem == mem_orig, "mem alloced\n");
667 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
668 }
669
670 my_alloc_called = 0;
671 mem = mem_orig;
672 *(void **)mem = NULL;
673 StubMsg.Buffer = StubMsg.BufferStart;
674 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
675 todo_wine {
676 ok(mem == mem_orig, "mem alloced\n");
677 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
678 }
679
680 mem = my_alloc(sizeof(void *));
681 *(void **)mem = NULL;
682 my_free_called = 0;
683 StubMsg.Buffer = StubMsg.BufferStart;
684 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
685 ok(my_free_called == 0, "free called %d\n", my_free_called);
686 my_free(mem);
687
688 mem = my_alloc(sizeof(void *));
689 *(void **)mem = my_alloc(sizeof(char));
690 my_free_called = 0;
691 StubMsg.Buffer = StubMsg.BufferStart;
692 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
693 ok(my_free_called == 1, "free called %d\n", my_free_called);
694 my_free(mem);
695
696 HeapFree(GetProcessHeap(), 0, mem_orig);
697 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
698 }
699
700 static void test_simple_struct_marshal(const unsigned char *formattypes,
701 void *memsrc, DWORD srcsize,
702 const void *wiredata,
703 ULONG wiredatalen,
704 int(*cmp)(const void*,const void*,size_t),
705 int num_additional_allocs,
706 const char *msgpfx)
707 {
708 RPC_MESSAGE RpcMessage;
709 MIDL_STUB_MESSAGE StubMsg;
710 MIDL_STUB_DESC StubDesc;
711 DWORD size;
712 void *ptr;
713 unsigned char *mem, *mem_orig;
714
715 my_alloc_called = my_free_called = 0;
716 if(!cmp)
717 cmp = memcmp;
718
719 StubDesc = Object_StubDesc;
720 StubDesc.pFormatTypes = formattypes;
721
722 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
723
724 StubMsg.BufferLength = 0;
725 NdrSimpleStructBufferSize( &StubMsg, memsrc, formattypes );
726 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
727 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
728 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
729 ptr = NdrSimpleStructMarshall( &StubMsg, memsrc, formattypes );
730 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
731 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
732 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled %08x %08x %08x\n", msgpfx, *(DWORD*)StubMsg.BufferStart,*((DWORD*)StubMsg.BufferStart+1),*((DWORD*)StubMsg.BufferStart+2));
733
734 StubMsg.Buffer = StubMsg.BufferStart;
735 StubMsg.MemorySize = 0;
736 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
737 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
738 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
739 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
740
741 StubMsg.Buffer = StubMsg.BufferStart;
742 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
743 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
744 ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
745 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
746 size = srcsize;
747 /*** Unmarshalling first with must_alloc false ***/
748
749 StubMsg.Buffer = StubMsg.BufferStart;
750 StubMsg.MemorySize = 0;
751 mem_orig = mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, srcsize);
752 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
753 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
754 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
755 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
756 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
757 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
758 my_alloc_called = 0;
759 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
760
761 /* If we're a server we still use the supplied memory */
762 StubMsg.Buffer = StubMsg.BufferStart;
763 StubMsg.IsClient = 0;
764 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
765 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
766 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
767 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
768 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
769 my_alloc_called = 0;
770 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
771
772 /* ...unless we pass a NULL ptr, then the buffer is used.
773 Passing a NULL ptr while we're a client && !must_alloc
774 crashes on Windows, so we won't do that. */
775
776 if (0) /* crashes on Win9x and NT4 */
777 {
778 mem = NULL;
779 StubMsg.IsClient = 0;
780 StubMsg.Buffer = StubMsg.BufferStart;
781 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, FALSE );
782 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
783 ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
784 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
785 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
786 my_alloc_called = 0;
787 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
788 }
789
790 /*** now must_alloc is true ***/
791
792 /* with must_alloc set we always allocate new memory whether or not we're
793 a server and also when passing NULL */
794 mem = mem_orig;
795 StubMsg.IsClient = 1;
796 StubMsg.Buffer = StubMsg.BufferStart;
797 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
798 ok(ptr == NULL, "ret %p\n", ptr);
799 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
800 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
801 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
802 my_alloc_called = 0;
803 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
804
805 mem = NULL;
806 StubMsg.Buffer = StubMsg.BufferStart;
807 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
808 ok(ptr == NULL, "ret %p\n", ptr);
809 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
810 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
811 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
812 my_alloc_called = 0;
813 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
814
815 mem = mem_orig;
816 StubMsg.Buffer = StubMsg.BufferStart;
817 StubMsg.IsClient = 0;
818 StubMsg.ReuseBuffer = 1;
819 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
820 ok(ptr == NULL, "ret %p\n", ptr);
821 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
822 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
823 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
824 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
825 my_alloc_called = 0;
826 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
827
828 mem = NULL;
829 StubMsg.Buffer = StubMsg.BufferStart;
830 StubMsg.IsClient = 0;
831 StubMsg.ReuseBuffer = 1;
832 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
833 ok(ptr == NULL, "ret %p\n", ptr);
834 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
835 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
836 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
837 my_alloc_called = 0;
838 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
839
840 HeapFree(GetProcessHeap(), 0, mem_orig);
841 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
842 }
843
844 typedef struct
845 {
846 LONG l1;
847 LONG *pl1;
848 char *pc1;
849 } ps1_t;
850
851 static int ps1_cmp(const void *s1, const void *s2, size_t num)
852 {
853 const ps1_t *p1, *p2;
854
855 p1 = s1;
856 p2 = s2;
857
858 if(p1->l1 != p2->l1)
859 return 1;
860
861 if(p1->pl1 && p2->pl1)
862 {
863 if(*p1->pl1 != *p2->pl1)
864 return 1;
865 }
866 else if(p1->pl1 || p2->pl1)
867 return 1;
868
869 if(p1->pc1 && p2->pc1)
870 {
871 if(*p1->pc1 != *p2->pc1)
872 return 1;
873 }
874 else if(p1->pc1 || p2->pc1)
875 return 1;
876
877 return 0;
878 }
879
880 static void test_simple_struct(void)
881 {
882 unsigned char wiredata[28];
883 ULONG wiredatalen;
884 LONG l;
885 char c;
886 ps1_t ps1;
887
888 static const unsigned char fmtstr_simple_struct[] =
889 {
890 0x12, 0x0, /* FC_UP */
891 NdrFcShort( 0x2 ), /* Offset=2 */
892 0x15, 0x3, /* FC_STRUCT [align 4] */
893 NdrFcShort( 0x18 ), /* [size 24] */
894 0x6, /* FC_SHORT */
895 0x2, /* FC_CHAR */
896 0x38, /* FC_ALIGNM4 */
897 0x8, /* FC_LONG */
898 0x8, /* FC_LONG */
899 0x39, /* FC_ALIGNM8 */
900 0xb, /* FC_HYPER */
901 0x5b, /* FC_END */
902 };
903 struct {
904 short s;
905 char c;
906 LONG l1, l2;
907 LONGLONG ll;
908 } s1;
909
910 static const unsigned char fmtstr_pointer_struct[] =
911 {
912 0x12, 0x0, /* FC_UP */
913 NdrFcShort( 0x2 ), /* Offset=2 */
914 #ifdef _WIN64
915 0x1a, /* FC_BOGUS_STRUCT */
916 0x3, /* 3 */
917 NdrFcShort(0x18), /* [size 24] */
918 NdrFcShort(0x0),
919 NdrFcShort(0x8), /* Offset= 8 (266) */
920 0x08, /* FC_LONG */
921 0x39, /* FC_ALIGNM8 */
922 0x36, /* FC_POINTER */
923 0x36, /* FC_POINTER */
924 0x5c, /* FC_PAD */
925 0x5b, /* FC_END */
926 0x12, 0x8, /* FC_UP [simple_pointer] */
927 0x08, /* FC_LONG */
928 0x5c, /* FC_PAD */
929 0x12, 0x8, /* FC_UP [simple_pointer] */
930 0x02, /* FC_CHAR */
931 0x5c, /* FC_PAD */
932 #else
933 0x16, 0x3, /* FC_PSTRUCT [align 4] */
934 NdrFcShort( 0xc ), /* [size 12] */
935 0x4b, /* FC_PP */
936 0x5c, /* FC_PAD */
937 0x46, /* FC_NO_REPEAT */
938 0x5c, /* FC_PAD */
939 NdrFcShort( 0x4 ), /* 4 */
940 NdrFcShort( 0x4 ), /* 4 */
941 0x13, 0x8, /* FC_OP [simple_pointer] */
942 0x8, /* FC_LONG */
943 0x5c, /* FC_PAD */
944 0x46, /* FC_NO_REPEAT */
945 0x5c, /* FC_PAD */
946 NdrFcShort( 0x8 ), /* 8 */
947 NdrFcShort( 0x8 ), /* 8 */
948 0x13, 0x8, /* FC_OP [simple_pointer] */
949 0x2, /* FC_CHAR */
950 0x5c, /* FC_PAD */
951 0x5b, /* FC_END */
952 0x8, /* FC_LONG */
953 0x8, /* FC_LONG */
954 0x8, /* FC_LONG */
955 0x5c, /* FC_PAD */
956 0x5b, /* FC_END */
957 #endif
958 };
959
960 /* zero the entire structure, including the holes */
961 memset(&s1, 0, sizeof(s1));
962
963 /* FC_STRUCT */
964 s1.s = 0x1234;
965 s1.c = 0xa5;
966 s1.l1 = 0xdeadbeef;
967 s1.l2 = 0xcafebabe;
968 s1.ll = ((ULONGLONG) 0xbadefeed << 32) | 0x2468ace0;
969
970 wiredatalen = 24;
971 memcpy(wiredata, &s1, wiredatalen);
972 test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
973
974 if (use_pointer_ids)
975 *(unsigned int *)wiredata = 0x20000;
976 else
977 *(unsigned int *)wiredata = (UINT_PTR)&s1;
978 memcpy(wiredata + 4, &s1, wiredatalen);
979 test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
980
981 if (sizeof(void *) == 8) return; /* it cannot be represented as a simple struct on Win64 */
982
983 /* zero the entire structure, including the hole */
984 memset(&ps1, 0, sizeof(ps1));
985
986 /* FC_PSTRUCT */
987 ps1.l1 = 0xdeadbeef;
988 l = 0xcafebabe;
989 ps1.pl1 = &l;
990 c = 'a';
991 ps1.pc1 = &c;
992 *(unsigned int *)(wiredata + 4) = 0xdeadbeef;
993 if (use_pointer_ids)
994 {
995 *(unsigned int *)(wiredata + 8) = 0x20000;
996 *(unsigned int *)(wiredata + 12) = 0x20004;
997 }
998 else
999 {
1000 *(unsigned int *)(wiredata + 8) = (UINT_PTR)&l;
1001 *(unsigned int *)(wiredata + 12) = (UINT_PTR)&c;
1002 }
1003 memcpy(wiredata + 16, &l, 4);
1004 memcpy(wiredata + 20, &c, 1);
1005
1006 test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
1007 if (use_pointer_ids)
1008 {
1009 *(unsigned int *)wiredata = 0x20000;
1010 *(unsigned int *)(wiredata + 8) = 0x20004;
1011 *(unsigned int *)(wiredata + 12) = 0x20008;
1012 }
1013 else
1014 *(unsigned int *)wiredata = (UINT_PTR)&ps1;
1015 test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
1016 }
1017
1018 static void test_fullpointer_xlat(void)
1019 {
1020 PFULL_PTR_XLAT_TABLES pXlatTables;
1021 ULONG RefId;
1022 int ret;
1023 void *Pointer;
1024
1025 pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
1026
1027 /* "marshaling" phase */
1028
1029 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1030 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1031 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1032
1033 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1034 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1035 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1036
1037 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1038 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1039 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1040
1041 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1042 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1043 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1044
1045 ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
1046 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1047 ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
1048
1049 /* "unmarshaling" phase */
1050
1051 ret = NdrFullPointerQueryRefId(pXlatTables, 0x0, 0, &Pointer);
1052 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1053
1054 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1055 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1056 ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
1057
1058 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
1059 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1060 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1061
1062 NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
1063
1064 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
1065 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1066 ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
1067
1068 NdrFullPointerXlatFree(pXlatTables);
1069
1070 pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
1071
1072 /* "unmarshaling" phase */
1073
1074 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1075 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1076 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1077
1078 NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
1079
1080 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1081 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1082 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1083
1084 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1085 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1086 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1087
1088 /* "marshaling" phase */
1089
1090 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1091 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1092 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1093
1094 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1095 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1096 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1097
1098 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1099 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1100 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1101
1102 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1103 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1104 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1105
1106 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1107 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1108 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1109
1110 /* "freeing" phase */
1111
1112 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
1113 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1114
1115 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
1116 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1117 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1118
1119 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1120 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1121 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1122
1123 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
1124 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1125
1126 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1127 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1128
1129 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
1130 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1131 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1132
1133 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1134 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1135 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1136
1137 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1138 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1139 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1140
1141 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1142 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1143
1144 NdrFullPointerXlatFree(pXlatTables);
1145 }
1146
1147 /* verify stub data that is identical between client and server */
1148 static void test_common_stub_data( const char *prefix, const MIDL_STUB_MESSAGE *stubMsg )
1149 {
1150 void *unset_ptr;
1151
1152 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1153
1154 #define TEST_ZERO(field, fmt) ok(stubMsg->field == 0, "%s: " #field " should have been set to zero instead of " fmt "\n", prefix, stubMsg->field)
1155 #define TEST_POINTER_UNSET(field) ok(stubMsg->field == unset_ptr, "%s: " #field " should have been unset instead of %p\n", prefix, stubMsg->field)
1156 #define TEST_ULONG_UNSET(field) ok(stubMsg->field == 0xcccccccc, "%s: " #field " should have been unset instead of 0x%x\n", prefix, stubMsg->field)
1157 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg->field == (ULONG_PTR)unset_ptr, "%s: " #field " should have been unset instead of 0x%lx\n", prefix, stubMsg->field)
1158
1159 TEST_POINTER_UNSET(BufferMark);
1160 TEST_ULONG_UNSET(MemorySize);
1161 TEST_POINTER_UNSET(Memory);
1162 TEST_ZERO(pAllocAllNodesContext, "%p");
1163 ok(stubMsg->pPointerQueueState == 0 ||
1164 broken(stubMsg->pPointerQueueState == unset_ptr), /* win2k */
1165 "%s: pPointerQueueState should have been unset instead of %p\n",
1166 prefix, stubMsg->pPointerQueueState);
1167 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1168 TEST_ZERO(PointerBufferMark, "%p");
1169 ok( stubMsg->uFlags == 0 ||
1170 broken(stubMsg->uFlags == 0xcc), /* win9x */
1171 "%s: uFlags should have been set to zero instead of 0x%x\n", prefix, stubMsg->uFlags );
1172 /* FIXME: UniquePtrCount */
1173 TEST_ULONG_PTR_UNSET(MaxCount);
1174 TEST_ULONG_UNSET(Offset);
1175 TEST_ULONG_UNSET(ActualCount);
1176 ok(stubMsg->pfnAllocate == my_alloc, "%s: pfnAllocate should have been %p instead of %p\n",
1177 prefix, my_alloc, stubMsg->pfnAllocate);
1178 ok(stubMsg->pfnFree == my_free, "%s: pfnFree should have been %p instead of %p\n",
1179 prefix, my_free, stubMsg->pfnFree);
1180 TEST_ZERO(StackTop, "%p");
1181 TEST_POINTER_UNSET(pPresentedType);
1182 TEST_POINTER_UNSET(pTransmitType);
1183 TEST_POINTER_UNSET(SavedHandle);
1184 ok(stubMsg->StubDesc == &Object_StubDesc, "%s: StubDesc should have been %p instead of %p\n",
1185 prefix, &Object_StubDesc, stubMsg->StubDesc);
1186 TEST_ZERO(FullPtrRefId, "%d");
1187 ok( stubMsg->PointerLength == 0 ||
1188 broken(stubMsg->PointerLength == 1), /* win9x, nt4 */
1189 "%s: pAsyncMsg should have been set to zero instead of %d\n", prefix, stubMsg->PointerLength );
1190 TEST_ZERO(fInDontFree, "%d");
1191 TEST_ZERO(fDontCallFreeInst, "%d");
1192 ok( stubMsg->fHasReturn == 0 || broken(stubMsg->fHasReturn), /* win9x, nt4 */
1193 "%s: fHasReturn should have been set to zero instead of %d\n", prefix, stubMsg->fHasReturn );
1194 TEST_ZERO(fHasExtensions, "%d");
1195 TEST_ZERO(fHasNewCorrDesc, "%d");
1196 ok(stubMsg->fIsIn == 0 || broken(stubMsg->fIsIn), /* win9x, nt4 */
1197 "%s: fIsIn should have been set to 0 instead of %d\n", prefix, stubMsg->fIsIn);
1198 TEST_ZERO(fIsOicf, "%d");
1199 ok(stubMsg->fBufferValid == 0,
1200 "%s: fBufferValid should have been set to 0 instead of %d\n", prefix, stubMsg->fBufferValid);
1201 TEST_ZERO(fNeedMCCP, "%d");
1202 ok(stubMsg->fUnused == 0 ||
1203 stubMsg->fUnused == -2, /* Vista */
1204 "%s: fUnused should have been set to 0 or -2 instead of %d\n", prefix, stubMsg->fUnused);
1205 ok(stubMsg->fUnused2 == 0xffffcccc, "%s: fUnused2 should have been 0xffffcccc instead of 0x%x\n",
1206 prefix, stubMsg->fUnused2);
1207 ok(stubMsg->dwDestContext == MSHCTX_DIFFERENTMACHINE,
1208 "%s: dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n",
1209 prefix, stubMsg->dwDestContext);
1210 TEST_ZERO(pvDestContext, "%p");
1211 TEST_POINTER_UNSET(SavedContextHandles);
1212 TEST_ULONG_UNSET(ParamNumber);
1213 TEST_ZERO(pRpcChannelBuffer, "%p");
1214 TEST_ZERO(pArrayInfo, "%p");
1215 TEST_POINTER_UNSET(SizePtrCountArray);
1216 TEST_POINTER_UNSET(SizePtrOffsetArray);
1217 TEST_POINTER_UNSET(SizePtrLengthArray);
1218 TEST_POINTER_UNSET(pArgQueue);
1219 TEST_ZERO(dwStubPhase, "%d");
1220 /* FIXME: where does this value come from? */
1221 trace("%s: LowStackMark is %p\n", prefix, stubMsg->LowStackMark);
1222 ok( stubMsg->pAsyncMsg == 0 || broken(stubMsg->pAsyncMsg == unset_ptr), /* win9x, nt4 */
1223 "%s: pAsyncMsg should have been set to zero instead of %p\n", prefix, stubMsg->pAsyncMsg );
1224 ok( stubMsg->pCorrInfo == 0 || broken(stubMsg->pCorrInfo == unset_ptr), /* win9x, nt4 */
1225 "%s: pCorrInfo should have been set to zero instead of %p\n", prefix, stubMsg->pCorrInfo );
1226 ok( stubMsg->pCorrMemory == 0 || broken(stubMsg->pCorrMemory == unset_ptr), /* win9x, nt4 */
1227 "%s: pCorrMemory should have been set to zero instead of %p\n", prefix, stubMsg->pCorrMemory );
1228 ok( stubMsg->pMemoryList == 0 || broken(stubMsg->pMemoryList == unset_ptr), /* win9x, nt4 */
1229 "%s: pMemoryList should have been set to zero instead of %p\n", prefix, stubMsg->pMemoryList );
1230 TEST_POINTER_UNSET(pCSInfo);
1231 TEST_POINTER_UNSET(ConformanceMark);
1232 TEST_POINTER_UNSET(VarianceMark);
1233 ok(stubMsg->Unused == (ULONG_PTR)unset_ptr, "%s: Unused should have be unset instead of 0x%lx\n",
1234 prefix, stubMsg->Unused);
1235 TEST_POINTER_UNSET(pContext);
1236 TEST_POINTER_UNSET(ContextHandleHash);
1237 TEST_POINTER_UNSET(pUserMarshalList);
1238 TEST_ULONG_PTR_UNSET(Reserved51_3);
1239 TEST_ULONG_PTR_UNSET(Reserved51_4);
1240 TEST_ULONG_PTR_UNSET(Reserved51_5);
1241
1242 #undef TEST_ULONG_PTR_UNSET
1243 #undef TEST_ULONG_UNSET
1244 #undef TEST_POINTER_UNSET
1245 #undef TEST_ZERO
1246 }
1247
1248 static void test_client_init(void)
1249 {
1250 MIDL_STUB_MESSAGE stubMsg;
1251 RPC_MESSAGE rpcMsg;
1252 void *unset_ptr;
1253
1254 memset(&rpcMsg, 0xcc, sizeof(rpcMsg));
1255 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1256 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1257
1258 NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
1259
1260 test_common_stub_data( "NdrClientInitializeNew", &stubMsg );
1261
1262 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1263 ok(rpcMsg.Handle == NULL, "rpcMsg.Handle should have been NULL instead of %p\n", rpcMsg.Handle);
1264 ok(rpcMsg.Buffer == unset_ptr, "rpcMsg.Buffer should have been unset instead of %p\n",
1265 rpcMsg.Buffer);
1266 ok(rpcMsg.BufferLength == 0xcccccccc, "rpcMsg.BufferLength should have been unset instead of %d\n", rpcMsg.BufferLength);
1267 ok(rpcMsg.ProcNum == 0x8001, "rpcMsg.ProcNum should have been 0x8001 instead of 0x%x\n", rpcMsg.ProcNum);
1268 ok(rpcMsg.TransferSyntax == unset_ptr, "rpcMsg.TransferSyntax should have been unset instead of %p\n", rpcMsg.TransferSyntax);
1269 ok(rpcMsg.RpcInterfaceInformation == Object_StubDesc.RpcInterfaceInformation,
1270 "rpcMsg.RpcInterfaceInformation should have been %p instead of %p\n",
1271 Object_StubDesc.RpcInterfaceInformation, rpcMsg.RpcInterfaceInformation);
1272 /* Note: ReservedForRuntime not tested */
1273 ok(rpcMsg.ManagerEpv == unset_ptr, "rpcMsg.ManagerEpv should have been unset instead of %p\n", rpcMsg.ManagerEpv);
1274 ok(rpcMsg.ImportContext == unset_ptr, "rpcMsg.ImportContext should have been unset instead of %p\n", rpcMsg.ImportContext);
1275 ok(rpcMsg.RpcFlags == 0, "rpcMsg.RpcFlags should have been 0 instead of 0x%x\n", rpcMsg.RpcFlags);
1276
1277 ok(stubMsg.Buffer == unset_ptr, "stubMsg.Buffer should have been unset instead of %p\n",
1278 stubMsg.Buffer);
1279 ok(stubMsg.BufferStart == NULL, "stubMsg.BufferStart should have been NULL instead of %p\n",
1280 stubMsg.BufferStart);
1281 ok(stubMsg.BufferEnd == NULL, "stubMsg.BufferEnd should have been NULL instead of %p\n",
1282 stubMsg.BufferEnd);
1283 ok(stubMsg.BufferLength == 0, "stubMsg.BufferLength should have been 0 instead of %u\n",
1284 stubMsg.BufferLength);
1285 ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
1286 ok(stubMsg.ReuseBuffer == 0, "stubMsg.ReuseBuffer should have been 0 instead of %d\n",
1287 stubMsg.ReuseBuffer);
1288 ok(stubMsg.CorrDespIncrement == 0, "stubMsg.CorrDespIncrement should have been 0 instead of %d\n",
1289 stubMsg.CorrDespIncrement);
1290 ok(stubMsg.FullPtrXlatTables == unset_ptr, "stubMsg.FullPtrXlatTables should have been unset instead of %p\n",
1291 stubMsg.FullPtrXlatTables);
1292 }
1293
1294 static void test_server_init(void)
1295 {
1296 MIDL_STUB_MESSAGE stubMsg;
1297 RPC_MESSAGE rpcMsg;
1298 unsigned char *ret;
1299 unsigned char buffer[256];
1300
1301 memset(&rpcMsg, 0, sizeof(rpcMsg));
1302 rpcMsg.Buffer = buffer;
1303 rpcMsg.BufferLength = sizeof(buffer);
1304 rpcMsg.RpcFlags = RPC_BUFFER_COMPLETE;
1305
1306 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1307
1308 ret = NdrServerInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc);
1309 ok(ret == NULL, "NdrServerInitializeNew should have returned NULL instead of %p\n", ret);
1310
1311 test_common_stub_data( "NdrServerInitializeNew", &stubMsg );
1312
1313 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1314 ok(stubMsg.Buffer == buffer, "stubMsg.Buffer should have been %p instead of %p\n", buffer, stubMsg.Buffer);
1315 ok(stubMsg.BufferStart == buffer, "stubMsg.BufferStart should have been %p instead of %p\n", buffer, stubMsg.BufferStart);
1316 ok(stubMsg.BufferEnd == buffer + sizeof(buffer), "stubMsg.BufferEnd should have been %p instead of %p\n", buffer + sizeof(buffer), stubMsg.BufferEnd);
1317 todo_wine
1318 ok(stubMsg.BufferLength == 0, "stubMsg.BufferLength should have been 0 instead of %u\n", stubMsg.BufferLength);
1319 ok(stubMsg.IsClient == 0, "stubMsg.IsClient should have been 0 instead of %u\n", stubMsg.IsClient);
1320 ok(stubMsg.ReuseBuffer == 0 ||
1321 broken(stubMsg.ReuseBuffer == 1), /* win2k */
1322 "stubMsg.ReuseBuffer should have been set to zero instead of %d\n", stubMsg.ReuseBuffer);
1323 ok(stubMsg.CorrDespIncrement == 0 ||
1324 broken(stubMsg.CorrDespIncrement == 0xcc), /* <= Win 2003 */
1325 "CorrDespIncrement should have been set to zero instead of 0x%x\n", stubMsg.CorrDespIncrement);
1326 ok(stubMsg.FullPtrXlatTables == 0, "stubMsg.BufferLength should have been 0 instead of %p\n", stubMsg.FullPtrXlatTables);
1327 }
1328
1329 static void test_ndr_allocate(void)
1330 {
1331 RPC_MESSAGE RpcMessage;
1332 MIDL_STUB_MESSAGE StubMsg;
1333 MIDL_STUB_DESC StubDesc;
1334 void *p1, *p2;
1335 struct tag_mem_list_v2_t
1336 {
1337 DWORD magic;
1338 DWORD size;
1339 DWORD unknown;
1340 struct tag_mem_list_v2_t *next;
1341 } *mem_list_v2;
1342 const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
1343
1344 StubDesc = Object_StubDesc;
1345 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
1346
1347 my_alloc_called = my_free_called = 0;
1348 p1 = NdrAllocate(&StubMsg, 10);
1349 p2 = NdrAllocate(&StubMsg, 24);
1350 ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
1351 ok(StubMsg.pMemoryList != NULL, "StubMsg.pMemoryList NULL\n");
1352 if(StubMsg.pMemoryList)
1353 {
1354 mem_list_v2 = StubMsg.pMemoryList;
1355 if (mem_list_v2->size == 24)
1356 {
1357 trace("v2 mem list format\n");
1358 ok((char *)mem_list_v2 == (char *)p2 + 24, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p2 + 24, mem_list_v2);
1359 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1360 ok(mem_list_v2->size == 24, "wrong size for p2 %d\n", mem_list_v2->size);
1361 ok(mem_list_v2->unknown == 0, "wrong unknown for p2 0x%x\n", mem_list_v2->unknown);
1362 ok(mem_list_v2->next != NULL, "next NULL\n");
1363 mem_list_v2 = mem_list_v2->next;
1364 if(mem_list_v2)
1365 {
1366 ok((char *)mem_list_v2 == (char *)p1 + 16, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p1 + 16, mem_list_v2);
1367 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1368 ok(mem_list_v2->size == 16, "wrong size for p1 %d\n", mem_list_v2->size);
1369 ok(mem_list_v2->unknown == 0, "wrong unknown for p1 0x%x\n", mem_list_v2->unknown);
1370 ok(mem_list_v2->next == NULL, "next %p\n", mem_list_v2->next);
1371 }
1372 }
1373 else win_skip("v1 mem list format\n");
1374 }
1375 /* NdrFree isn't exported so we can't test free'ing */
1376 }
1377
1378 static void test_conformant_array(void)
1379 {
1380 RPC_MESSAGE RpcMessage;
1381 MIDL_STUB_MESSAGE StubMsg;
1382 MIDL_STUB_DESC StubDesc;
1383 void *ptr;
1384 unsigned char *mem, *mem_orig;
1385 unsigned char memsrc[20];
1386 unsigned int i;
1387
1388 static const unsigned char fmtstr_conf_array[] =
1389 {
1390 0x1b, /* FC_CARRAY */
1391 0x0, /* align */
1392 NdrFcShort( 0x1 ), /* elem size */
1393 0x40, /* Corr desc: const */
1394 0x0,
1395 NdrFcShort(0x10), /* const = 0x10 */
1396 0x1, /* FC_BYTE */
1397 0x5b /* FC_END */
1398 };
1399
1400 for (i = 0; i < sizeof(memsrc); i++)
1401 memsrc[i] = i * i;
1402
1403 StubDesc = Object_StubDesc;
1404 StubDesc.pFormatTypes = fmtstr_conf_array;
1405
1406 NdrClientInitializeNew(
1407 &RpcMessage,
1408 &StubMsg,
1409 &StubDesc,
1410 0);
1411
1412 StubMsg.BufferLength = 0;
1413 NdrConformantArrayBufferSize( &StubMsg,
1414 memsrc,
1415 fmtstr_conf_array );
1416 ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1417
1418 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1419 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1420 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1421
1422 ptr = NdrConformantArrayMarshall( &StubMsg, memsrc, fmtstr_conf_array );
1423 ok(ptr == NULL, "ret %p\n", ptr);
1424 ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1425 ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1426
1427 StubMsg.Buffer = StubMsg.BufferStart;
1428 StubMsg.MemorySize = 0;
1429 mem = NULL;
1430
1431 /* Client */
1432 my_alloc_called = 0;
1433 /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1434 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1435 ok(mem != NULL, "mem not alloced\n");
1436 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1437 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1438
1439 my_alloc_called = 0;
1440 StubMsg.Buffer = StubMsg.BufferStart;
1441 mem_orig = mem;
1442 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1443 ok(mem == mem_orig, "mem alloced\n");
1444 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1445 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1446
1447 my_alloc_called = 0;
1448 StubMsg.Buffer = StubMsg.BufferStart;
1449 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1450 ok(mem != mem_orig, "mem not alloced\n");
1451 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1452 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1453
1454 my_free_called = 0;
1455 StubMsg.Buffer = StubMsg.BufferStart;
1456 NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1457 ok(my_free_called == 0, "free called %d\n", my_free_called);
1458 StubMsg.pfnFree(mem);
1459
1460 /* Server */
1461 my_alloc_called = 0;
1462 StubMsg.IsClient = 0;
1463 mem = NULL;
1464 StubMsg.Buffer = StubMsg.BufferStart;
1465 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1466 ok(mem == StubMsg.BufferStart + 4 || broken(!mem), /* win9x, nt4 */
1467 "mem not pointing at buffer %p/%p\n", mem, StubMsg.BufferStart + 4);
1468 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1469 my_alloc_called = 0;
1470 mem = NULL;
1471 StubMsg.Buffer = StubMsg.BufferStart;
1472 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1473 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1474 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1475 StubMsg.pfnFree(mem);
1476
1477 my_alloc_called = 0;
1478 mem = mem_orig;
1479 StubMsg.Buffer = StubMsg.BufferStart;
1480 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1481 ok(mem == mem_orig, "mem alloced\n");
1482 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1483
1484 my_alloc_called = 0;
1485 mem = mem_orig;
1486 StubMsg.Buffer = StubMsg.BufferStart;
1487 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1488 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1489 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1490 StubMsg.pfnFree(mem);
1491 StubMsg.pfnFree(mem_orig);
1492
1493 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1494 }
1495
1496 static void test_conformant_string(void)
1497 {
1498 RPC_MESSAGE RpcMessage;
1499 MIDL_STUB_MESSAGE StubMsg;
1500 MIDL_STUB_DESC StubDesc;
1501 DWORD size;
1502 void *ptr;
1503 unsigned char *mem, *mem_orig;
1504 char memsrc[] = "This is a test string";
1505
1506 static const unsigned char fmtstr_conf_str[] =
1507 {
1508 0x11, 0x8, /* FC_RP [simple_pointer] */
1509 0x22, /* FC_C_CSTRING */
1510 0x5c, /* FC_PAD */
1511 };
1512
1513 StubDesc = Object_StubDesc;
1514 StubDesc.pFormatTypes = fmtstr_conf_str;
1515
1516 memset( &StubMsg, 0, sizeof(StubMsg) ); /* needed on win9x and nt4 */
1517 NdrClientInitializeNew(
1518 &RpcMessage,
1519 &StubMsg,
1520 &StubDesc,
1521 0);
1522
1523 StubMsg.BufferLength = 0;
1524 NdrPointerBufferSize( &StubMsg,
1525 (unsigned char *)memsrc,
1526 fmtstr_conf_str );
1527 ok(StubMsg.BufferLength >= sizeof(memsrc) + 12, "length %d\n", StubMsg.BufferLength);
1528
1529 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1530 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1531 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1532
1533 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_conf_str );
1534 ok(ptr == NULL, "ret %p\n", ptr);
1535 size = StubMsg.Buffer - StubMsg.BufferStart;
1536 ok(size == sizeof(memsrc) + 12, "Buffer %p Start %p len %d\n",
1537 StubMsg.Buffer, StubMsg.BufferStart, size);
1538 ok(!memcmp(StubMsg.BufferStart + 12, memsrc, sizeof(memsrc)), "incorrectly marshaled\n");
1539
1540 StubMsg.Buffer = StubMsg.BufferStart;
1541 StubMsg.MemorySize = 0;
1542 mem = NULL;
1543
1544 /* Client */
1545 my_alloc_called = 0;
1546 StubMsg.Buffer = StubMsg.BufferStart;
1547 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1548 /* Windows apparently checks string length on the output buffer to determine its size... */
1549 memset( mem, 'x', sizeof(memsrc) - 1 );
1550 mem[sizeof(memsrc) - 1] = 0;
1551 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1552 ok(mem == mem_orig, "mem not alloced\n");
1553 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1554
1555 my_alloc_called = 0;
1556 StubMsg.Buffer = StubMsg.BufferStart;
1557 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1558 todo_wine {
1559 ok(mem == mem_orig, "mem not alloced\n");
1560 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1561 }
1562
1563 /* Prevent a memory leak when running with Wine.
1564 Remove once the todo_wine block above is fixed. */
1565 if (mem != mem_orig)
1566 HeapFree(GetProcessHeap(), 0, mem_orig);
1567
1568 my_free_called = 0;
1569 StubMsg.Buffer = StubMsg.BufferStart;
1570 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1571 ok(my_free_called == 1, "free called %d\n", my_free_called);
1572
1573 mem = my_alloc(10);
1574 my_free_called = 0;
1575 StubMsg.Buffer = StubMsg.BufferStart;
1576 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1577 ok(my_free_called == 1, "free called %d\n", my_free_called);
1578
1579 /* Server */
1580 my_alloc_called = 0;
1581 StubMsg.IsClient = 0;
1582 mem = NULL;
1583 StubMsg.Buffer = StubMsg.BufferStart;
1584 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1585 ok(mem == StubMsg.BufferStart + 12 || broken(!mem), /* win9x, nt4 */
1586 "mem not pointing at buffer %p/%p\n", mem, StubMsg.BufferStart + 12 );
1587 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1588
1589 my_alloc_called = 0;
1590 mem = NULL;
1591 StubMsg.Buffer = StubMsg.BufferStart;
1592 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1593 todo_wine {
1594 ok(mem == StubMsg.BufferStart + 12 || broken(!mem), /* win9x, nt4 */
1595 "mem not pointing at buffer %p/%p\n", mem, StubMsg.BufferStart + 12 );
1596 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1597 }
1598
1599 my_alloc_called = 0;
1600 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1601 StubMsg.Buffer = StubMsg.BufferStart;
1602 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1603 ok(mem == StubMsg.BufferStart + 12 || broken(!mem), /* win9x, nt4 */
1604 "mem not pointing at buffer %p/%p\n", mem, StubMsg.BufferStart + 12 );
1605 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1606
1607 my_alloc_called = 0;
1608 mem = mem_orig;
1609 StubMsg.Buffer = StubMsg.BufferStart;
1610 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1611 todo_wine {
1612 ok(mem == StubMsg.BufferStart + 12 || broken(!mem), /* win9x, nt4 */
1613 "mem not pointing at buffer %p/%p\n", mem, StubMsg.BufferStart + 12 );
1614 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1615 }
1616
1617 mem = my_alloc(10);
1618 my_free_called = 0;
1619 StubMsg.Buffer = StubMsg.BufferStart;
1620 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1621 ok(my_free_called == 1, "free called %d\n", my_free_called);
1622
1623 HeapFree(GetProcessHeap(), 0, mem_orig);
1624 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1625 }
1626
1627 static void test_nonconformant_string(void)
1628 {
1629 RPC_MESSAGE RpcMessage;
1630 MIDL_STUB_MESSAGE StubMsg;
1631 MIDL_STUB_DESC StubDesc;
1632 DWORD size;
1633 void *ptr;
1634 unsigned char *mem, *mem_orig;
1635 unsigned char memsrc[10] = "This is";
1636 unsigned char memsrc2[10] = "This is a";
1637
1638 static const unsigned char fmtstr_nonconf_str[] =
1639 {
1640 0x26, /* FC_CSTRING */
1641 0x5c, /* FC_PAD */
1642 NdrFcShort( 0xa ), /* 10 */
1643 };
1644
1645 StubDesc = Object_StubDesc;
1646 StubDesc.pFormatTypes = fmtstr_nonconf_str;
1647
1648 /* length < size */
1649 NdrClientInitializeNew(
1650 &RpcMessage,
1651 &StubMsg,
1652 &StubDesc,
1653 0);
1654
1655 StubMsg.BufferLength = 0;
1656
1657 NdrNonConformantStringBufferSize( &StubMsg, memsrc, fmtstr_nonconf_str );
1658 ok(StubMsg.BufferLength >= strlen((char *)memsrc) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1659
1660 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1661 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1662 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1663
1664 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc, fmtstr_nonconf_str );
1665 ok(ptr == NULL, "ret %p\n", ptr);
1666 size = StubMsg.Buffer - StubMsg.BufferStart;
1667 ok(size == strlen((char *)memsrc) + 1 + 8, "Buffer %p Start %p len %d\n",
1668 StubMsg.Buffer, StubMsg.BufferStart, size);
1669 ok(!memcmp(StubMsg.BufferStart + 8, memsrc, strlen((char *)memsrc) + 1), "incorrectly marshaled\n");
1670
1671 StubMsg.Buffer = StubMsg.BufferStart;
1672 StubMsg.MemorySize = 0;
1673 mem = NULL;
1674
1675 /* Client */
1676 my_alloc_called = 0;
1677 StubMsg.Buffer = StubMsg.BufferStart;
1678 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1679 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1680 ok(mem == mem_orig, "mem alloced\n");
1681 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1682
1683 my_alloc_called = 0;
1684 StubMsg.Buffer = StubMsg.BufferStart;
1685 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1686 todo_wine
1687 ok(mem == mem_orig, "mem alloced\n");
1688 todo_wine
1689 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1690
1691 /* Server */
1692 my_alloc_called = 0;
1693 StubMsg.IsClient = 0;
1694 mem = NULL;
1695 StubMsg.Buffer = StubMsg.BufferStart;
1696 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1697 ok(mem != mem_orig, "mem not alloced\n");
1698 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1699 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1700 NdrOleFree(mem);
1701
1702 my_alloc_called = 0;
1703 mem = mem_orig;
1704 StubMsg.Buffer = StubMsg.BufferStart;
1705 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1706 ok(mem == mem_orig, "mem alloced\n");
1707 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1708
1709 my_alloc_called = 0;
1710 mem = mem_orig;
1711 StubMsg.Buffer = StubMsg.BufferStart;
1712 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1713 todo_wine
1714 ok(mem == mem_orig, "mem alloced\n");
1715 todo_wine
1716 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1717
1718 HeapFree(GetProcessHeap(), 0, mem_orig);
1719 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1720
1721 /* length = size */
1722 NdrClientInitializeNew(
1723 &RpcMessage,
1724 &StubMsg,
1725 &StubDesc,
1726 0);
1727
1728 StubMsg.BufferLength = 0;
1729
1730 NdrNonConformantStringBufferSize( &StubMsg, memsrc2, fmtstr_nonconf_str );
1731 ok(StubMsg.BufferLength >= strlen((char *)memsrc2) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1732
1733 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1734 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1735 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1736
1737 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc2, fmtstr_nonconf_str );
1738 ok(ptr == NULL, "ret %p\n", ptr);
1739 size = StubMsg.Buffer - StubMsg.BufferStart;
1740 ok(size == strlen((char *)memsrc2) + 1 + 8, "Buffer %p Start %p len %d\n",
1741 StubMsg.Buffer, StubMsg.BufferStart, size);
1742 ok(!memcmp(StubMsg.BufferStart + 8, memsrc2, strlen((char *)memsrc2) + 1), "incorrectly marshaled\n");
1743
1744 StubMsg.Buffer = StubMsg.BufferStart;
1745 StubMsg.MemorySize = 0;
1746 mem = NULL;
1747
1748 /* Client */
1749 my_alloc_called = 0;
1750 StubMsg.Buffer = StubMsg.BufferStart;
1751 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1752 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1753 ok(mem == mem_orig, "mem alloced\n");
1754 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1755
1756 my_alloc_called = 0;
1757 StubMsg.Buffer = StubMsg.BufferStart;
1758 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1759 todo_wine
1760 ok(mem == mem_orig, "mem alloced\n");
1761 todo_wine
1762 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1763
1764 /* Server */
1765 my_alloc_called = 0;
1766 StubMsg.IsClient = 0;
1767 mem = NULL;
1768 StubMsg.Buffer = StubMsg.BufferStart;
1769 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1770 ok(mem != mem_orig, "mem not alloced\n");
1771 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1772 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1773 NdrOleFree(mem);
1774
1775 my_alloc_called = 0;
1776 mem = mem_orig;
1777 StubMsg.Buffer = StubMsg.BufferStart;
1778 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1779 ok(mem == mem_orig, "mem alloced\n");
1780 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1781
1782 my_alloc_called = 0;
1783 mem = mem_orig;
1784 StubMsg.Buffer = StubMsg.BufferStart;
1785 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1786 todo_wine
1787 ok(mem == mem_orig, "mem alloced\n");
1788 todo_wine
1789 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1790
1791 HeapFree(GetProcessHeap(), 0, mem_orig);
1792 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1793 }
1794
1795 static void test_conf_complex_struct(void)
1796 {
1797 RPC_MESSAGE RpcMessage;
1798 MIDL_STUB_MESSAGE StubMsg;
1799 MIDL_STUB_DESC StubDesc;
1800 void *ptr;
1801 unsigned int i;
1802 struct conf_complex
1803 {
1804 unsigned int size;
1805 unsigned int *array[1];
1806 };
1807 struct conf_complex *memsrc;
1808 struct conf_complex *mem;
1809
1810 static const unsigned char fmtstr_complex_struct[] =
1811 {
1812 /* 0 */
1813 0x1b, /* FC_CARRAY */
1814 0x3, /* 3 */
1815 /* 2 */ NdrFcShort( 0x4 ), /* 4 */
1816 /* 4 */ 0x8, /* Corr desc: FC_LONG */
1817 0x0, /* */
1818 /* 6 */ NdrFcShort( 0xfffc ), /* -4 */
1819 /* 8 */
1820 0x4b, /* FC_PP */
1821 0x5c, /* FC_PAD */
1822 /* 10 */
1823 0x48, /* FC_VARIABLE_REPEAT */
1824 0x49, /* FC_FIXED_OFFSET */
1825 /* 12 */ NdrFcShort( 0x4 ), /* 4 */
1826 /* 14 */ NdrFcShort( 0x0 ), /* 0 */
1827 /* 16 */ NdrFcShort( 0x1 ), /* 1 */
1828 /* 18 */ NdrFcShort( 0x0 ), /* 0 */
1829 /* 20 */ NdrFcShort( 0x0 ), /* 0 */
1830 /* 22 */ 0x12, 0x8, /* FC_UP [simple_pointer] */
1831 /* 24 */ 0x8, /* FC_LONG */
1832 0x5c, /* FC_PAD */
1833 /* 26 */
1834 0x5b, /* FC_END */
1835
1836 0x8, /* FC_LONG */
1837 /* 28 */ 0x5c, /* FC_PAD */
1838 0x5b, /* FC_END */
1839 /* 30 */
1840 0x1a, /* FC_BOGUS_STRUCT */
1841 0x3, /* 3 */
1842 /* 32 */ NdrFcShort( 0x4 ), /* 4 */
1843 /* 34 */ NdrFcShort( 0xffffffde ), /* Offset= -34 (0) */
1844 /* 36 */ NdrFcShort( 0x0 ), /* Offset= 0 (36) */
1845 /* 38 */ 0x8, /* FC_LONG */
1846 0x5b, /* FC_END */
1847 };
1848
1849 memsrc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1850 FIELD_OFFSET(struct conf_complex, array[20]));
1851 memsrc->size = 20;
1852
1853 StubDesc = Object_StubDesc;
1854 StubDesc.pFormatTypes = fmtstr_complex_struct;
1855
1856 NdrClientInitializeNew(
1857 &RpcMessage,
1858 &StubMsg,
1859 &StubDesc,
1860 0);
1861
1862 StubMsg.BufferLength = 0;
1863 NdrComplexStructBufferSize( &StubMsg,
1864 (unsigned char *)memsrc,
1865 &fmtstr_complex_struct[30] );
1866 ok(StubMsg.BufferLength >= 28, "length %d\n", StubMsg.BufferLength);
1867
1868 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1869 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1870 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1871
1872 ptr = NdrComplexStructMarshall( &StubMsg, (unsigned char *)memsrc,
1873 &fmtstr_complex_struct[30] );
1874 ok(ptr == NULL, "ret %p\n", ptr);
1875 ok(*(unsigned int *)StubMsg.BufferStart == 20, "Conformance should have been 20 instead of %d\n", *(unsigned int *)StubMsg.BufferStart);
1876 ok(*(unsigned int *)(StubMsg.BufferStart + 4) == 20, "conf_complex.size should have been 20 instead of %d\n", *(unsigned int *)(StubMsg.BufferStart + 4));
1877 for (i = 0; i < 20; i++)
1878 ok(*(unsigned int *)(StubMsg.BufferStart + 8 + i * 4) == 0, "pointer id for conf_complex.array[%d] should have been 0 instead of 0x%x\n", i, *(unsigned int *)(StubMsg.BufferStart + 8 + i * 4));
1879
1880 /* Server */
1881 my_alloc_called = 0;
1882 StubMsg.IsClient = 0;
1883 mem = NULL;
1884 StubMsg.Buffer = StubMsg.BufferStart;
1885 ptr = NdrComplexStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_struct[30], 0);
1886 ok(ptr == NULL, "ret %p\n", ptr);
1887 ok(mem->size == 20, "mem->size wasn't unmarshalled correctly (%d)\n", mem->size);
1888 ok(mem->array[0] == NULL, "mem->array[0] wasn't unmarshalled correctly (%p)\n", mem->array[0]);
1889 StubMsg.pfnFree(mem);
1890
1891 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1892 HeapFree(GetProcessHeap(), 0, memsrc);
1893 }
1894
1895
1896 static void test_conf_complex_array(void)
1897 {
1898 RPC_MESSAGE RpcMessage;
1899 MIDL_STUB_MESSAGE StubMsg;
1900 MIDL_STUB_DESC StubDesc;
1901 void *ptr;
1902 unsigned int i, j;
1903 struct conf_complex
1904 {
1905 unsigned int dim1, dim2;
1906 DWORD **array;
1907 };
1908 struct conf_complex memsrc;
1909 struct conf_complex *mem;
1910 DWORD *buf, expected_length;
1911
1912 static const unsigned char fmtstr_complex_array[] =
1913 {
1914
1915 /* 0 */ 0x21, /* FC_BOGUS_ARRAY */
1916 0x3, /* 3 */
1917 /* 2 */ NdrFcShort( 0x0 ), /* 0 */
1918 /* 4 */ 0x19, 0x0, /* Corr desc: field pointer, FC_ULONG */
1919 /* 6 */ NdrFcShort( 0x4 ), /* 4 */
1920 /* 8 */ NdrFcLong( 0xffffffff ), /* -1 */
1921 /* 12 */ 0x8, /* FC_LONG */
1922 0x5b, /* FC_END */
1923 /* 14 */
1924 0x21, /* FC_BOGUS_ARRAY */
1925 0x3, /* 3 */
1926 /* 16 */ NdrFcShort( 0x0 ), /* 0 */
1927 /* 18 */ 0x19, /* Corr desc: field pointer, FC_ULONG */
1928 0x0, /* */
1929 /* 20 */ NdrFcShort( 0x0 ), /* 0 */
1930 /* 22 */ NdrFcLong( 0xffffffff ), /* -1 */
1931 /* 26 */ 0x12, 0x0, /* FC_UP */
1932 /* 28 */ NdrFcShort( 0xffe4 ), /* Offset= -28 (0) */
1933 /* 30 */ 0x5c, /* FC_PAD */
1934 0x5b, /* FC_END */
1935
1936 #ifdef _WIN64
1937 /* 32 */ 0x1a, /* FC_BOGUS_STRUCT */
1938 0x3, /* 3 */
1939 /* 34 */ NdrFcShort( 0x10 ), /* 16 */
1940 /* 36 */ NdrFcShort( 0x0 ), /* 0 */
1941 /* 38 */ NdrFcShort( 0x6 ), /* Offset= 6 (44) */
1942 /* 40 */ 0x8, /* FC_LONG */
1943 0x8, /* FC_LONG */
1944 /* 42 */ 0x36, /* FC_POINTER */
1945 0x5b, /* FC_END */
1946 /* 44 */
1947 0x12, 0x0, /* FC_UP */
1948 /* 46 */ NdrFcShort( 0xffe0 ), /* Offset= -32 (14) */
1949 #else
1950 /* 32 */
1951 0x16, /* FC_PSTRUCT */
1952 0x3, /* 3 */
1953 /* 34 */ NdrFcShort( 0xc ), /* 12 */
1954 /* 36 */ 0x4b, /* FC_PP */
1955 0x5c, /* FC_PAD */
1956 /* 38 */ 0x46, /* FC_NO_REPEAT */
1957 0x5c, /* FC_PAD */
1958 /* 40 */ NdrFcShort( 0x8 ), /* 8 */
1959 /* 42 */ NdrFcShort( 0x8 ), /* 8 */
1960 /* 44 */ 0x12, 0x0, /* FC_UP */
1961 /* 46 */ NdrFcShort( 0xffe0 ), /* Offset= -32 (14) */
1962 /* 48 */ 0x5b, /* FC_END */
1963 0x8, /* FC_LONG */
1964 /* 50 */ 0x8, /* FC_LONG */
1965 0x8, /* FC_LONG */
1966 /* 52 */ 0x5c, /* FC_PAD */
1967 0x5b, /* FC_END */
1968 #endif
1969 };
1970
1971 memsrc.dim1 = 5;
1972 memsrc.dim2 = 3;
1973
1974 memsrc.array = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, memsrc.dim1 * sizeof(DWORD*));
1975
1976 for(i = 0; i < memsrc.dim1; i++)
1977 {
1978 memsrc.array[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, memsrc.dim2 * sizeof(DWORD));
1979 for(j = 0; j < memsrc.dim2; j++)
1980 memsrc.array[i][j] = i * memsrc.dim2 + j;
1981 }
1982
1983 StubDesc = Object_StubDesc;
1984 StubDesc.pFormatTypes = fmtstr_complex_array;
1985
1986 NdrClientInitializeNew(
1987 &RpcMessage,
1988 &StubMsg,
1989 &StubDesc,
1990 0);
1991
1992 StubMsg.BufferLength = 0;
1993
1994 #ifdef _WIN64
1995 NdrComplexStructBufferSize( &StubMsg,
1996 (unsigned char *)&memsrc,
1997 &fmtstr_complex_array[32] );
1998 #else
1999 NdrSimpleStructBufferSize( &StubMsg,
2000 (unsigned char *)&memsrc,
2001 &fmtstr_complex_array[32] );
2002 #endif
2003
2004 expected_length = (4 + memsrc.dim1 * (2 + memsrc.dim2)) * 4;
2005 if (StubMsg.BufferLength == 96)
2006 {
2007 win_skip("Tests crash on Win9x, WinMe and NT4\n");
2008 goto cleanup;
2009 }
2010 ok(StubMsg.BufferLength >= expected_length, "length %d\n", StubMsg.BufferLength);
2011
2012 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
2013 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
2014 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
2015
2016 #ifdef _WIN64
2017 ptr = NdrComplexStructMarshall( &StubMsg, (unsigned char *)&memsrc,
2018 &fmtstr_complex_array[32] );
2019 #else
2020 ptr = NdrSimpleStructMarshall( &StubMsg, (unsigned char *)&memsrc,
2021 &fmtstr_complex_array[32] );
2022 #endif
2023
2024 ok(ptr == NULL, "ret %p\n", ptr);
2025 ok((char*)StubMsg.Buffer == (char*)StubMsg.BufferStart + expected_length, "not at expected length\n");
2026
2027 buf = (DWORD *)StubMsg.BufferStart;
2028
2029 ok(*buf == memsrc.dim1, "dim1 should have been %d instead of %08x\n", memsrc.dim1, *buf);
2030 buf++;
2031 ok(*buf == memsrc.dim2, "dim2 should have been %d instead of %08x\n", memsrc.dim2, *buf);
2032 buf++;
2033 ok(*buf != 0, "pointer id should be non-zero\n");
2034 buf++;
2035 ok(*buf == memsrc.dim1, "Conformance should have been %d instead of %08x\n", memsrc.dim1, *buf);
2036 buf++;
2037 for(i = 0; i < memsrc.dim1; i++)
2038 {
2039 ok(*buf != 0, "pointer id[%d] should be non-zero\n", i);
2040 buf++;
2041 }
2042 for(i = 0; i < memsrc.dim1; i++)
2043 {
2044 ok(*buf == memsrc.dim2, "Conformance should have been %d instead of %08x\n", memsrc.dim2, *buf);
2045 buf++;
2046 for(j = 0; j < memsrc.dim2; j++)
2047 {
2048 ok(*buf == i * memsrc.dim2 + j, "got %08x\n", *buf);
2049 buf++;
2050 }
2051 }
2052
2053 ok((void*)buf == StubMsg.Buffer, "not at end of buffer\n");
2054
2055 /* Server */
2056 my_alloc_called = 0;
2057 StubMsg.IsClient = 0;
2058 mem = NULL;
2059 StubMsg.Buffer = StubMsg.BufferStart;
2060 #ifdef _WIN64
2061 ptr = NdrComplexStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_array[32], 0);
2062 #else
2063 ptr = NdrSimpleStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_array[32], 0);
2064 #endif
2065 ok(ptr == NULL, "ret %p\n", ptr);
2066 ok(mem->dim1 == memsrc.dim1, "mem->dim1 wasn't unmarshalled correctly (%d)\n", mem->dim1);
2067 ok(mem->dim2 == memsrc.dim2, "mem->dim2 wasn't unmarshalled correctly (%d)\n", mem->dim2);
2068 ok(mem->array[1][0] == memsrc.dim2, "mem->array[1][0] wasn't unmarshalled correctly (%d)\n", mem->array[1][0]);
2069
2070 StubMsg.Buffer = StubMsg.BufferStart;
2071 #ifdef _WIN64
2072 NdrComplexStructFree( &StubMsg, (unsigned char*)mem, &fmtstr_complex_array[32]);
2073 #else
2074 NdrSimpleStructFree( &StubMsg, (unsigned char*)mem, &fmtstr_complex_array[32]);
2075 #endif
2076
2077 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
2078
2079 cleanup:
2080 for(i = 0; i < memsrc.dim1; i++)
2081 HeapFree(GetProcessHeap(), 0, memsrc.array[i]);
2082 HeapFree(GetProcessHeap(), 0, memsrc.array);
2083 }
2084
2085 static void test_ndr_buffer(void)
2086 {
2087 static unsigned char ncalrpc[] = "ncalrpc";
2088 static unsigned char endpoint[] = "winetest:test_ndr_buffer";
2089 RPC_MESSAGE RpcMessage;
2090 MIDL_STUB_MESSAGE StubMsg;
2091 MIDL_STUB_DESC StubDesc = Object_StubDesc;
2092 unsigned char *ret;
2093 unsigned char *binding;
2094 RPC_BINDING_HANDLE Handle;
2095 RPC_STATUS status;
2096 ULONG prev_buffer_length;
2097 BOOL old_buffer_valid_location;
2098
2099 StubDesc.RpcInterfaceInformation = (void *)&IFoo___RpcServerInterface;
2100
2101 status = RpcServerUseProtseqEpA(ncalrpc, 20, endpoint, NULL);
2102 ok(RPC_S_OK == status, "RpcServerUseProtseqEp failed with status %u\n", status);
2103 status = RpcServerRegisterIf(IFoo_v0_0_s_ifspec, NULL, NULL);
2104 ok(RPC_S_OK == status, "RpcServerRegisterIf failed with status %u\n", status);
2105 status = RpcServerListen(1, 20, TRUE);
2106 ok(RPC_S_OK == status, "RpcServerListen failed with status %u\n", status);
2107 if (status != RPC_S_OK)
2108 {
2109 /* Failed to create a server, running client tests is useless */
2110 return;
2111 }
2112
2113 status = RpcStringBindingComposeA(NULL, ncalrpc, NULL, endpoint, NULL, &binding);
2114 ok(status == RPC_S_OK, "RpcStringBindingCompose failed (%u)\n", status);
2115
2116 status = RpcBindingFromStringBindingA(binding, &Handle);
2117 ok(status == RPC_S_OK, "RpcBindingFromStringBinding failed (%u)\n", status);
2118 RpcStringFreeA(&binding);
2119
2120 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 5);
2121
2122 ret = NdrGetBuffer(&StubMsg, 10, Handle);
2123 ok(ret == StubMsg.Buffer, "NdrGetBuffer should have returned the same value as StubMsg.Buffer instead of %p\n", ret);
2124 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
2125 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
2126 ok(RpcMessage.BufferLength == 10 ||
2127 broken(RpcMessage.BufferLength == 12), /* win2k */
2128 "RpcMessage.BufferLength should have been 10 instead of %d\n", RpcMessage.BufferLength);
2129 ok(RpcMessage.RpcFlags == 0, "RpcMessage.RpcFlags should have been 0x0 instead of 0x%x\n", RpcMessage.RpcFlags);
2130 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
2131 ok(!StubMsg.BufferStart, "BufferStart should have been NULL instead of %p\n", StubMsg.BufferStart);
2132 ok(!StubMsg.BufferEnd, "BufferEnd should have been NULL instead of %p\n", StubMsg.BufferEnd);
2133 todo_wine
2134 ok(StubMsg.BufferLength == 0, "BufferLength should have left as 0 instead of being set to %d\n", StubMsg.BufferLength);
2135 old_buffer_valid_location = !StubMsg.fBufferValid;
2136 if (old_buffer_valid_location)
2137 ok(broken(StubMsg.CorrDespIncrement == TRUE), "fBufferValid should have been TRUE instead of 0x%x\n", StubMsg.CorrDespIncrement);
2138 else
2139 ok(StubMsg.fBufferValid, "fBufferValid should have been non-zero instead of 0x%x\n", StubMsg.fBufferValid);
2140
2141 prev_buffer_length = RpcMessage.BufferLength;
2142 StubMsg.BufferLength = 1;
2143 NdrFreeBuffer(&StubMsg);
2144 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
2145 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
2146 ok(RpcMessage.BufferLength == prev_buffer_length, "RpcMessage.BufferLength should have been left as %d instead of %d\n", prev_buffer_length, RpcMessage.BufferLength);
2147 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
2148 ok(StubMsg.BufferLength == 1, "BufferLength should have left as 1 instead of being set to %d\n", StubMsg.BufferLength);
2149 if (old_buffer_valid_location)
2150 ok(broken(StubMsg.CorrDespIncrement == FALSE), "fBufferValid should have been FALSE instead of 0x%x\n", StubMsg.CorrDespIncrement);
2151 else
2152 ok(!StubMsg.fBufferValid, "fBufferValid should have been FALSE instead of %d\n", StubMsg.fBufferValid);
2153
2154 /* attempt double-free */
2155 NdrFreeBuffer(&StubMsg);
2156
2157 RpcBindingFree(&Handle);
2158
2159 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
2160 ok(status == RPC_S_OK, "RpcServerUnregisterIf failed (%u)\n", status);
2161 }
2162
2163 static void test_NdrMapCommAndFaultStatus(void)
2164 {
2165 RPC_STATUS rpc_status;
2166 MIDL_STUB_MESSAGE StubMsg;
2167 RPC_MESSAGE RpcMessage;
2168
2169 NdrClientInitializeNew(&RpcMessage, &StubMsg, &Object_StubDesc, 5);
2170
2171 for (rpc_status = 0; rpc_status < 10000; rpc_status++)
2172 {
2173 RPC_STATUS status;
2174 ULONG comm_status = 0;
2175 ULONG fault_status = 0;
2176 ULONG expected_comm_status = 0;
2177 ULONG expected_fault_status = 0;
2178 status = NdrMapCommAndFaultStatus(&StubMsg, &comm_status, &fault_status, rpc_status);
2179 ok(status == RPC_S_OK, "NdrMapCommAndFaultStatus failed with error %d\n", status);
2180 switch (rpc_status)
2181 {
2182 case ERROR_INVALID_HANDLE:
2183 case RPC_S_INVALID_BINDING:
2184 case RPC_S_UNKNOWN_IF:
2185 case RPC_S_SERVER_UNAVAILABLE:
2186 case RPC_S_SERVER_TOO_BUSY:
2187 case RPC_S_CALL_FAILED_DNE:
2188 case RPC_S_PROTOCOL_ERROR:
2189 case RPC_S_UNSUPPORTED_TRANS_SYN:
2190 case RPC_S_UNSUPPORTED_TYPE:
2191 case RPC_S_PROCNUM_OUT_OF_RANGE:
2192 case EPT_S_NOT_REGISTERED:
2193 case RPC_S_COMM_FAILURE:
2194 expected_comm_status = rpc_status;
2195 break;
2196 default:
2197 expected_fault_status = rpc_status;
2198 }
2199 ok(comm_status == expected_comm_status, "NdrMapCommAndFaultStatus should have mapped %d to comm status %d instead of %d\n",
2200 rpc_status, expected_comm_status, comm_status);
2201 ok(fault_status == expected_fault_status, "NdrMapCommAndFaultStatus should have mapped %d to fault status %d instead of %d\n",
2202 rpc_status, expected_fault_status, fault_status);
2203 }
2204 }
2205
2206 static void test_NdrGetUserMarshalInfo(void)
2207 {
2208 RPC_STATUS status;
2209 MIDL_STUB_MESSAGE stubmsg;
2210 USER_MARSHAL_CB umcb;
2211 NDR_USER_MARSHAL_INFO umi;
2212 unsigned char buffer[16];
2213 void *rpc_channel_buffer = (void *)(ULONG_PTR)0xcafebabe;
2214 RPC_MESSAGE rpc_msg;
2215 RPC_STATUS (RPC_ENTRY *pNdrGetUserMarshalInfo)(ULONG *,ULONG,NDR_USER_MARSHAL_INFO *);
2216
2217 pNdrGetUserMarshalInfo = (void *)GetProcAddress(GetModuleHandleA("rpcrt4.dll"), "NdrGetUserMarshalInfo");
2218 if (!pNdrGetUserMarshalInfo)
2219 {
2220 skip("NdrGetUserMarshalInfo not exported\n");
2221 return;
2222 }
2223
2224 /* unmarshall */
2225
2226 memset(&rpc_msg, 0xcc, sizeof(rpc_msg));
2227 rpc_msg.Buffer = buffer;
2228 rpc_msg.BufferLength = 16;
2229
2230 memset(&stubmsg, 0xcc, sizeof(stubmsg));
2231 stubmsg.RpcMsg = &rpc_msg;
2232 stubmsg.dwDestContext = MSHCTX_INPROC;
2233 stubmsg.pvDestContext = NULL;
2234 stubmsg.Buffer = buffer + 15;
2235 stubmsg.BufferLength = 0;
2236 stubmsg.BufferEnd = NULL;
2237 stubmsg.pRpcChannelBuffer = rpc_channel_buffer;
2238 stubmsg.StubDesc = NULL;
2239 stubmsg.pfnAllocate = my_alloc;
2240 stubmsg.pfnFree = my_free;
2241
2242 memset(&umcb, 0xcc, sizeof(umcb));
2243 umcb.Flags = MAKELONG(MSHCTX_INPROC, NDR_LOCAL_DATA_REPRESENTATION);
2244 umcb.pStubMsg = &stubmsg;
2245 umcb.Signature = USER_MARSHAL_CB_SIGNATURE;
2246 umcb.CBType = USER_MARSHAL_CB_UNMARSHALL;
2247
2248 memset(&umi, 0xaa, sizeof(umi));
2249
2250 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2251 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2252 ok( umi.InformationLevel == 1,
2253 "umi.InformationLevel was %u instead of 1\n",
2254 umi.InformationLevel);
2255 ok( U1(umi).Level1.Buffer == buffer + 15,
2256 "umi.Level1.Buffer was %p instead of %p\n",
2257 U1(umi).Level1.Buffer, buffer);
2258 ok( U1(umi).Level1.BufferSize == 1,
2259 "umi.Level1.BufferSize was %u instead of 1\n",
2260 U1(umi).Level1.BufferSize);
2261 ok( U1(umi).Level1.pfnAllocate == my_alloc,
2262 "umi.Level1.pfnAllocate was %p instead of %p\n",
2263 U1(umi).Level1.pfnAllocate, my_alloc);
2264 ok( U1(umi).Level1.pfnFree == my_free,
2265 "umi.Level1.pfnFree was %p instead of %p\n",
2266 U1(umi).Level1.pfnFree, my_free);
2267 ok( U1(umi).Level1.pRpcChannelBuffer == rpc_channel_buffer,
2268 "umi.Level1.pRpcChannelBuffer was %p instead of %p\n",
2269 U1(umi).Level1.pRpcChannelBuffer, rpc_channel_buffer);
2270
2271 /* buffer size */
2272
2273 rpc_msg.Buffer = buffer;
2274 rpc_msg.BufferLength = 16;
2275
2276 stubmsg.Buffer = buffer;
2277 stubmsg.BufferLength = 16;
2278 stubmsg.BufferEnd = NULL;
2279
2280 umcb.CBType = USER_MARSHAL_CB_BUFFER_SIZE;
2281
2282 memset(&umi, 0xaa, sizeof(umi));
2283
2284 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2285 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2286 ok( umi.InformationLevel == 1,
2287 "umi.InformationLevel was %u instead of 1\n",
2288 umi.InformationLevel);
2289 ok( U1(umi).Level1.Buffer == NULL,
2290 "umi.Level1.Buffer was %p instead of NULL\n",
2291 U1(umi).Level1.Buffer);
2292 ok( U1(umi).Level1.BufferSize == 0,
2293 "umi.Level1.BufferSize was %u instead of 0\n",
2294 U1(umi).Level1.BufferSize);
2295 ok( U1(umi).Level1.pfnAllocate == my_alloc,
2296 "umi.Level1.pfnAllocate was %p instead of %p\n",
2297 U1(umi).Level1.pfnAllocate, my_alloc);
2298 ok( U1(umi).Level1.pfnFree == my_free,
2299 "umi.Level1.pfnFree was %p instead of %p\n",
2300 U1(umi).Level1.pfnFree, my_free);
2301 ok( U1(umi).Level1.pRpcChannelBuffer == rpc_channel_buffer,
2302 "umi.Level1.pRpcChannelBuffer was %p instead of %p\n",
2303 U1(umi).Level1.pRpcChannelBuffer, rpc_channel_buffer);
2304
2305 /* marshall */
2306
2307 rpc_msg.Buffer = buffer;
2308 rpc_msg.BufferLength = 16;
2309
2310 stubmsg.Buffer = buffer + 15;
2311 stubmsg.BufferLength = 0;
2312 stubmsg.BufferEnd = NULL;
2313
2314 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2315
2316 memset(&umi, 0xaa, sizeof(umi));
2317
2318 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2319 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2320 ok( umi.InformationLevel == 1,
2321 "umi.InformationLevel was %u instead of 1\n",
2322 umi.InformationLevel);
2323 ok( U1(umi).Level1.Buffer == buffer + 15,
2324 "umi.Level1.Buffer was %p instead of %p\n",
2325 U1(umi).Level1.Buffer, buffer);
2326 ok( U1(umi).Level1.BufferSize == 1,
2327 "umi.Level1.BufferSize was %u instead of 1\n",
2328 U1(umi).Level1.BufferSize);
2329 ok( U1(umi).Level1.pfnAllocate == my_alloc,
2330 "umi.Level1.pfnAllocate was %p instead of %p\n",
2331 U1(umi).Level1.pfnAllocate, my_alloc);
2332 ok( U1(umi).Level1.pfnFree == my_free,
2333 "umi.Level1.pfnFree was %p instead of %p\n",
2334 U1(umi).Level1.pfnFree, my_free);
2335 ok( U1(umi).Level1.pRpcChannelBuffer == rpc_channel_buffer,
2336 "umi.Level1.pRpcChannelBuffer was %p instead of %p\n",
2337 U1(umi).Level1.pRpcChannelBuffer, rpc_channel_buffer);
2338
2339 /* free */
2340
2341 rpc_msg.Buffer = buffer;
2342 rpc_msg.BufferLength = 16;
2343
2344 stubmsg.Buffer = buffer;
2345 stubmsg.BufferLength = 16;
2346 stubmsg.BufferEnd = NULL;
2347
2348 umcb.CBType = USER_MARSHAL_CB_FREE;
2349
2350 memset(&umi, 0xaa, sizeof(umi));
2351
2352 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2353 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2354 ok( umi.InformationLevel == 1,
2355 "umi.InformationLevel was %u instead of 1\n",
2356 umi.InformationLevel);
2357 ok( U1(umi).Level1.Buffer == NULL,
2358 "umi.Level1.Buffer was %p instead of NULL\n",
2359 U1(umi).Level1.Buffer);
2360 ok( U1(umi).Level1.BufferSize == 0,
2361 "umi.Level1.BufferSize was %u instead of 0\n",
2362 U1(umi).Level1.BufferSize);
2363 ok( U1(umi).Level1.pfnAllocate == my_alloc,
2364 "umi.Level1.pfnAllocate was %p instead of %p\n",
2365 U1(umi).Level1.pfnAllocate, my_alloc);
2366 ok( U1(umi).Level1.pfnFree == my_free,
2367 "umi.Level1.pfnFree was %p instead of %p\n",
2368 U1(umi).Level1.pfnFree, my_free);
2369 ok( U1(umi).Level1.pRpcChannelBuffer == rpc_channel_buffer,
2370 "umi.Level1.pRpcChannelBuffer was %p instead of %p\n",
2371 U1(umi).Level1.pRpcChannelBuffer, rpc_channel_buffer);
2372
2373 /* boundary test */
2374
2375 rpc_msg.Buffer = buffer;
2376 rpc_msg.BufferLength = 15;
2377
2378 stubmsg.Buffer = buffer + 15;
2379 stubmsg.BufferLength = 0;
2380 stubmsg.BufferEnd = NULL;
2381
2382 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2383
2384 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2385 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2386 ok( U1(umi).Level1.BufferSize == 0,
2387 "umi.Level1.BufferSize was %u instead of 0\n",
2388 U1(umi).Level1.BufferSize);
2389
2390 /* error conditions */
2391
2392 rpc_msg.BufferLength = 14;
2393 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2394 ok(status == ERROR_INVALID_USER_BUFFER,
2395 "NdrGetUserMarshalInfo should have failed with ERROR_INVALID_USER_BUFFER instead of %d\n", status);
2396
2397 rpc_msg.BufferLength = 15;
2398 status = pNdrGetUserMarshalInfo(&umcb.Flags, 9999, &umi);
2399 ok(status == RPC_S_INVALID_ARG,
2400 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2401
2402 umcb.CBType = 9999;
2403 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2404 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2405
2406 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2407 umcb.Signature = 0;
2408 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2409 ok(status == RPC_S_INVALID_ARG,
2410 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2411 }
2412
2413 static void test_MesEncodeFixedBufferHandleCreate(void)
2414 {
2415 ULONG encoded_size;
2416 RPC_STATUS status;
2417 handle_t handle;
2418 char *buffer;
2419
2420 status = MesEncodeFixedBufferHandleCreate(NULL, 0, NULL, NULL);
2421 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2422
2423 status = MesEncodeFixedBufferHandleCreate(NULL, 0, NULL, &handle);
2424 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2425
2426 status = MesEncodeFixedBufferHandleCreate((char*)0xdeadbeef, 0, NULL, &handle);
2427 ok(status == RPC_X_INVALID_BUFFER, "got %d\n", status);
2428
2429 buffer = (void*)((0xdeadbeef + 7) & ~7);
2430 status = MesEncodeFixedBufferHandleCreate(buffer, 0, NULL, &handle);
2431 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2432
2433 status = MesEncodeFixedBufferHandleCreate(buffer, 0, &encoded_size, &handle);
2434 todo_wine
2435 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2436 if (status == RPC_S_OK) {
2437 MesHandleFree(handle);
2438 }
2439 status = MesEncodeFixedBufferHandleCreate(buffer, 32, NULL, &handle);
2440 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2441
2442 status = MesEncodeFixedBufferHandleCreate(buffer, 32, &encoded_size, &handle);
2443 ok(status == RPC_S_OK, "got %d\n", status);
2444
2445 status = MesBufferHandleReset(NULL, MES_DYNAMIC_BUFFER_HANDLE, MES_ENCODE,
2446 &buffer, 32, &encoded_size);
2447 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2448
2449 /* convert to dynamic buffer handle */
2450 status = MesBufferHandleReset(handle, MES_DYNAMIC_BUFFER_HANDLE, MES_ENCODE,
2451 &buffer, 32, &encoded_size);
2452 ok(status == RPC_S_OK, "got %d\n", status);
2453
2454 status = MesBufferHandleReset(handle, MES_DYNAMIC_BUFFER_HANDLE, MES_ENCODE,
2455 NULL, 32, &encoded_size);
2456 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2457
2458 status = MesBufferHandleReset(handle, MES_DYNAMIC_BUFFER_HANDLE, MES_ENCODE,
2459 &buffer, 32, NULL);
2460 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2461
2462 /* invalid handle type */
2463 status = MesBufferHandleReset(handle, MES_DYNAMIC_BUFFER_HANDLE+1, MES_ENCODE,
2464 &buffer, 32, &encoded_size);
2465 ok(status == RPC_S_INVALID_ARG, "got %d\n", status);
2466
2467 status = MesHandleFree(handle);
2468 ok(status == RPC_S_OK, "got %d\n", status);
2469 }
2470
2471 static void test_NdrCorrelationInitialize(void)
2472 {
2473 MIDL_STUB_MESSAGE stub_msg;
2474 BYTE buf[256];
2475
2476 memset( &stub_msg, 0, sizeof(stub_msg) );
2477 memset( buf, 0, sizeof(buf) );
2478
2479 NdrCorrelationInitialize( &stub_msg, buf, sizeof(buf), 0 );
2480 ok( stub_msg.CorrDespIncrement == 2 ||
2481 broken(stub_msg.CorrDespIncrement == 0), /* <= Win 2003 */
2482 "got %d\n", stub_msg.CorrDespIncrement );
2483
2484 memset( &stub_msg, 0, sizeof(stub_msg) );
2485 memset( buf, 0, sizeof(buf) );
2486
2487 stub_msg.CorrDespIncrement = 1;
2488 NdrCorrelationInitialize( &stub_msg, buf, sizeof(buf), 0 );
2489 ok( stub_msg.CorrDespIncrement == 1, "got %d\n", stub_msg.CorrDespIncrement );
2490 }
2491
2492 START_TEST( ndr_marshall )
2493 {
2494 determine_pointer_marshalling_style();
2495
2496 test_ndr_simple_type();
2497 test_simple_types();
2498 test_nontrivial_pointer_types();
2499 test_simple_struct();
2500 test_fullpointer_xlat();
2501 test_client_init();
2502 test_server_init();
2503 test_ndr_allocate();
2504 test_conformant_array();
2505 test_conformant_string();
2506 test_nonconformant_string();
2507 test_conf_complex_struct();
2508 test_conf_complex_array();
2509 test_ndr_buffer();
2510 test_NdrMapCommAndFaultStatus();
2511 test_NdrGetUserMarshalInfo();
2512 test_MesEncodeFixedBufferHandleCreate();
2513 test_NdrCorrelationInitialize();
2514 }