Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1577 lines
48 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1996 - 2000 Microsoft Corporation
  3. Module Name :
  4. asyncu.c
  5. Abstract :
  6. This file contains the ndr async uuid implementation.
  7. Author :
  8. Ryszard K. Kott (ryszardk) Oct 1997
  9. Revision History :
  10. ---------------------------------------------------------------------*/
  11. #include "precomp.hxx"
  12. #define USE_STUBLESS_PROXY
  13. #define CINTERFACE
  14. #include "ndrole.h"
  15. #include "rpcproxy.h"
  16. #include "interp2.h"
  17. #include "asyncu64.h"
  18. #include "expr.h"
  19. #include "ndrtypes.h"
  20. #include "ndr64types.h"
  21. #include <stddef.h>
  22. #include <stdarg.h>
  23. #pragma code_seg(".ndr64")
  24. void
  25. Ndr64pCloneInOnlyCorrArgs(
  26. NDR_DCOM_ASYNC_MESSAGE * pAsyncMsg,
  27. PFORMAT_STRING pFormatType
  28. );
  29. HRESULT
  30. Ndr64pCompleteDcomAsyncStubCall(
  31. CStdAsyncStubBuffer * pAsyncSB
  32. );
  33. CLIENT_CALL_RETURN RPC_VAR_ENTRY
  34. Ndr64DcomAsyncClientCall(
  35. MIDL_STUBLESS_PROXY_INFO *pProxyInfo,
  36. ulong nProcNum,
  37. void *pReturnValue,
  38. ...
  39. )
  40. {
  41. NDR_PROC_CONTEXT ProcContext;
  42. RPC_STATUS status;
  43. CLIENT_CALL_RETURN Ret;
  44. va_list ArgList;
  45. unsigned char * StartofStack;
  46. INIT_ARG( ArgList, pReturnValue);
  47. GET_FIRST_IN_ARG(ArgList);
  48. StartofStack = (uchar *) GET_STACK_START(ArgList);
  49. Ndr64ClientInitializeContext( NdrpGetSyntaxType( pProxyInfo->pTransferSyntax),
  50. pProxyInfo,
  51. nProcNum,
  52. &ProcContext,
  53. StartofStack );
  54. NDR_ASSERT( ProcContext.IsAsync, "invalid async proc" );
  55. if ( nProcNum & 0x1 )
  56. status = MulNdrpBeginDcomAsyncClientCall( pProxyInfo,
  57. nProcNum,
  58. &ProcContext,
  59. StartofStack );
  60. else
  61. status = MulNdrpFinishDcomAsyncClientCall(pProxyInfo,
  62. nProcNum,
  63. &ProcContext,
  64. StartofStack );
  65. Ret.Simple = status;
  66. return Ret;
  67. }
  68. VOID
  69. Ndr64pAsyncDCOMFreeParams(
  70. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg )
  71. {
  72. /*++
  73. Routine Description:
  74. Frees the parameters for both the begin and finish calls.
  75. Arguments:
  76. pAsyncMsg - Supplies a pointer to the async message.
  77. Return Value:
  78. None.
  79. --*/
  80. NDR64_PARAM_FLAGS * pParamFlags;
  81. if ( pAsyncMsg->BeginStack )
  82. {
  83. if ( pAsyncMsg->FinishStack )
  84. {
  85. // Clear out the IN OUT parameters on the begin stack
  86. // so that they are not freed twice.
  87. int n;
  88. REGISTER_TYPE *pBeginStack = (REGISTER_TYPE *)pAsyncMsg->BeginStack;
  89. NDR64_PARAM_FORMAT *BeginParams = (NDR64_PARAM_FORMAT*)pAsyncMsg->BeginParams;
  90. int BeginNumberParams = pAsyncMsg->nBeginParams ;
  91. for( n = 0; n < BeginNumberParams; n++ )
  92. {
  93. pParamFlags = ( NDR64_PARAM_FLAGS * ) & ( BeginParams[n].Attributes );
  94. if ( pParamFlags->IsIn &&
  95. pParamFlags->IsOut )
  96. {
  97. pBeginStack[ BeginParams[ n ].StackOffset / sizeof(REGISTER_TYPE) ] = 0;
  98. }
  99. }
  100. }
  101. Ndr64pFreeParams( & (pAsyncMsg->StubMsg),
  102. pAsyncMsg->nBeginParams,
  103. ( NDR64_PARAM_FORMAT * ) pAsyncMsg->BeginParams,
  104. pAsyncMsg->BeginStack );
  105. }
  106. if ( pAsyncMsg->FinishStack )
  107. {
  108. Ndr64pFreeParams( & (pAsyncMsg->StubMsg),
  109. pAsyncMsg->nFinishParams,
  110. ( NDR64_PARAM_FORMAT * ) pAsyncMsg->FinishParams,
  111. pAsyncMsg->FinishStack );
  112. }
  113. }
  114. HRESULT
  115. MulNdrpBeginDcomAsyncClientCall(
  116. MIDL_STUBLESS_PROXY_INFO * pProxyInfo,
  117. ulong nProcNum,
  118. NDR_PROC_CONTEXT * pContext,
  119. void * StartofStack )
  120. /*
  121. Notes: OLE Refcounting.
  122. The model for async_uuid() is that async proxies or stubs
  123. are created with RefCount==1 and should never ever be
  124. addrefed by the engine.
  125. What the engine does is only the AsyncMsg clean up when done.
  126. The decision to destroy the AsyncPB or AsyncSB object is
  127. up to the client side PM or channel's SM for the server side.
  128. */
  129. {
  130. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  131. RPC_MESSAGE * pRpcMsg;
  132. MIDL_STUB_MESSAGE * pStubMsg;
  133. uchar * pArg;
  134. void * pThis = *(void **)StartofStack;
  135. void * Params;
  136. long NumberParams;
  137. long n;
  138. RPC_STATUS Status;
  139. CStdAsyncProxyBuffer * pAsyncPB;
  140. const IID * piid;
  141. HRESULT hr = S_OK;
  142. BOOL fSendCalled = FALSE;
  143. pAsyncPB = (CStdAsyncProxyBuffer*)
  144. ((uchar*)pThis - offsetof(CStdProxyBuffer, pProxyVtbl));
  145. piid = NdrGetProxyIID( pThis );
  146. Status = MulNdrpSetupBeginClientCall( pAsyncPB,
  147. StartofStack,
  148. pContext,
  149. *piid );
  150. if ( !SUCCEEDED(Status) )
  151. return Status;
  152. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE *)pAsyncPB->CallState.pAsyncMsg;
  153. // We need to switch to our copy of the stack everywhere, including pStubMsg.
  154. StartofStack = pAsyncMsg->ProcContext.StartofStack;
  155. pRpcMsg = & pAsyncMsg->RpcMsg;
  156. pStubMsg = & pAsyncMsg->StubMsg;
  157. pStubMsg->pContext = &pAsyncMsg->ProcContext;
  158. pContext = &pAsyncMsg->ProcContext;
  159. NumberParams = pContext->NumberParams;
  160. // This is OLE only code path - use a single TryExcept.
  161. // After catching it just map it to OLE exception.
  162. RpcTryExcept
  163. {
  164. ulong RpcFlags;
  165. PFORMAT_STRING pFormat;
  166. pContext->RpcFlags |= RPC_BUFFER_ASYNC;
  167. Ndr64pClientSetupTransferSyntax(pThis,
  168. pRpcMsg,
  169. pStubMsg,
  170. pProxyInfo,
  171. pContext,
  172. nProcNum );
  173. pStubMsg->pAsyncMsg = (struct _NDR_ASYNC_MESSAGE *) pAsyncMsg;
  174. //
  175. // Parameter descriptions are nicely spit out by MIDL.
  176. //
  177. Params = (NDR64_PARAM_FORMAT *) ( pContext->Params );
  178. pFormat = NdrpGetProcString( pContext->pSyntaxInfo,
  179. pContext->CurrentSyntaxType,
  180. nProcNum );
  181. pStubMsg->pContext = pContext;
  182. pAsyncMsg->nBeginParams = NumberParams;
  183. pAsyncMsg->BeginParams = (void *)Params;
  184. pAsyncMsg->pThis = pThis;
  185. pAsyncMsg->SyntaxType = pContext->CurrentSyntaxType;
  186. ( * pContext->pfnInit)(pStubMsg,
  187. NULL ); // return value
  188. ( * pContext->pfnSizing) ( pStubMsg,
  189. TRUE ); // isclient
  190. //
  191. // Do the GetBuffer.
  192. //
  193. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  194. NdrProxyGetBuffer( pThis, pStubMsg );
  195. NDR_ASSERT( pStubMsg->fBufferValid, "Invalid buffer" );
  196. pAsyncMsg->StubPhase = STUB_MARSHAL;
  197. //
  198. // ----------------------------------------------------------
  199. // Marshall Pass.
  200. // ----------------------------------------------------------
  201. //
  202. ( * pContext->pfnMarshal ) ( pStubMsg,
  203. TRUE ); // IsObject
  204. //
  205. // Make the RPC call.
  206. //
  207. pAsyncMsg->StubPhase = NDR_ASYNC_CALL_PHASE;
  208. fSendCalled = NdrpDcomAsyncClientSend( pStubMsg,
  209. pAsyncPB->punkOuter ); // PM's entry
  210. if ( fSendCalled )
  211. hr = S_OK;
  212. }
  213. RpcExcept( 1 )
  214. {
  215. RPC_STATUS ExceptionCode = RpcExceptionCode();
  216. pAsyncPB->CallState.Flags.BeginError = 1;
  217. // Actually dismantle the call.
  218. // This is a request call and there is nothing left at the runtime.
  219. pAsyncMsg->StubPhase = NDR_ASYNC_ERROR_PHASE;
  220. pAsyncMsg->ErrorCode = ExceptionCode;
  221. hr = NdrHrFromWin32Error(ExceptionCode);
  222. pAsyncPB->CallState.Hr = hr;
  223. // Async call in request phase: don't touch [out] params.
  224. }
  225. RpcEndExcept
  226. // "Finally"
  227. // Dont touch anything, the client has to call the Finish method anyway.
  228. pAsyncPB->CallState.Flags.BeginDone = 1;
  229. if ( SUCCEEDED(hr) )
  230. {
  231. if ( pContext->CurrentSyntaxType == XFER_SYNTAX_DCE )
  232. NdrpCloneInOnlyCorrArgs( pAsyncMsg,
  233. pContext->pSyntaxInfo->TypeString );
  234. else
  235. Ndr64pCloneInOnlyCorrArgs( pAsyncMsg,
  236. pContext->pSyntaxInfo->TypeString );
  237. // Channel will prompt signal
  238. }
  239. else
  240. if (!fSendCalled )
  241. NdrpAsyncProxySignal( pAsyncPB );
  242. // No need to release, our refcount should be 1 at this point.
  243. return hr;
  244. }
  245. void
  246. Ndr64pCloneInOnlyCorrArgs(
  247. NDR_DCOM_ASYNC_MESSAGE * pAsyncMsg,
  248. PFORMAT_STRING pFormatTypes
  249. )
  250. /*
  251. Walk the client stack looking for an in only argument flagged to clone.
  252. For each one, replace the arg with a clone that we control.
  253. Assumption is, we do it before returning to the user from the Begin call
  254. and also we clone walking the copy of the app's stack not the app stack.
  255. The stack modified in this way will be the one to access for the weird
  256. crossreferenced correlated args.
  257. This issue doesn't happen on the server, as we keep the Begin stack around
  258. when the Finish call is processed.
  259. */
  260. {
  261. unsigned char * pBeginStack = pAsyncMsg->BeginStack;
  262. NDR64_PARAM_FORMAT * Params = ( NDR64_PARAM_FORMAT *)pAsyncMsg->BeginParams;
  263. NDR64_PARAM_FLAGS * pParamFlags;
  264. int NumberParams = pAsyncMsg->nBeginParams;
  265. unsigned char * pArg;
  266. int n;
  267. for ( n = 0; n < NumberParams; n++ )
  268. {
  269. pParamFlags = ( NDR64_PARAM_FLAGS * ) & Params[n].Attributes ;
  270. if ( pParamFlags->SaveForAsyncFinish )
  271. {
  272. // Note that the arguments that need cloning come from top level size_is,
  273. // length_is etc, switch_is and iid_is attributes.
  274. // Hence, the only types of interest are uuid clones and integral types
  275. // different from hyper.
  276. // On top of it, we deal with stack-slot chunks of memory, so we don't
  277. // have to care about mac issues.
  278. pArg = pBeginStack + Params[n].StackOffset;
  279. if ( pParamFlags->IsBasetype )
  280. {
  281. if ( pParamFlags->IsSimpleRef )
  282. {
  283. void * pPointee = AsyncAlloca( pAsyncMsg, 8 );
  284. NDR64_FORMAT_CHAR type = *(PFORMAT_STRING)Params[n].Type;
  285. // The assignment needs to follow the type.
  286. RpcpMemoryCopy( pPointee, *(void **)pArg,
  287. NDR64_SIMPLE_TYPE_MEMSIZE( type ) );
  288. *(void**)pArg = pPointee;
  289. }
  290. // else the stack slot has the simple value already.
  291. }
  292. else
  293. {
  294. // If it's not a base type, then it cannot be by value.
  295. // It has to be a pointer to a simple type or to an iid.
  296. PFORMAT_STRING pParamFormat;
  297. const NDR64_POINTER_FORMAT *pPointerFormat ;
  298. PFORMAT_STRING pPointeeFormat;
  299. pParamFormat = (PFORMAT_STRING)Params[n].Type;
  300. pPointerFormat = (const NDR64_POINTER_FORMAT*)pParamFormat;
  301. pPointeeFormat = (PFORMAT_STRING)pPointerFormat->Pointee;
  302. if ( NDR64_IS_BASIC_POINTER(*pParamFormat) ) // not FC64_IP
  303. {
  304. if ( NDR64_SIMPLE_POINTER( pPointerFormat->Flags ) )
  305. {
  306. // Covers things like a unique pointer to a size
  307. // Essentially the same as for the simple ref above.
  308. void * pPointee = AsyncAlloca( pAsyncMsg, 8 );
  309. NDR64_FORMAT_CHAR type = *pPointeeFormat;
  310. // The assignment needs to follow the type.
  311. RpcpMemoryCopy( pPointee, *(void **)pArg,
  312. NDR64_SIMPLE_TYPE_MEMSIZE( type ) );
  313. *(void**)pArg = pPointee;
  314. }
  315. else
  316. {
  317. // has to be the riid case.
  318. // REFIID* comes out as FC64_?P -> FC64_?P -> FC64_STRUCT
  319. if ( NDR64_IS_BASIC_POINTER(*pPointeeFormat) &&
  320. ! NDR64_SIMPLE_POINTER( pPointerFormat->Flags) )
  321. {
  322. if ( *pPointeeFormat == FC64_STRUCT )
  323. {
  324. // one alloc for REFIID and IID itself.
  325. IID** ppIID = (IID**)AsyncAlloca( pAsyncMsg,
  326. sizeof(IID *) + sizeof(IID));
  327. IID* pIID = (IID *)(ppIID + 1);
  328. *ppIID = pIID; //set pointer
  329. RpcpMemoryCopy( pIID, **(IID ***)pArg, sizeof(IID));
  330. *(IID ***)pArg = ppIID;
  331. }
  332. else
  333. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  334. }
  335. else
  336. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  337. }
  338. }
  339. else
  340. {
  341. // has to be the riid case.
  342. // REFIID comes out as FC64_STRUCT
  343. if ( *pParamFormat == FC64_STRUCT )
  344. {
  345. IID *pIID = (IID*)AsyncAlloca( pAsyncMsg, sizeof(IID) );
  346. RpcpMemoryCopy( pIID, *(IID **)pArg, sizeof(IID));
  347. *(IID **)pArg = pIID;
  348. }
  349. else
  350. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  351. }
  352. }
  353. }
  354. }
  355. }
  356. HRESULT
  357. MulNdrpFinishDcomAsyncClientCall(
  358. MIDL_STUBLESS_PROXY_INFO * pProxyInfo,
  359. ulong nProcNum,
  360. NDR_PROC_CONTEXT * pContext ,
  361. void * StartofStack )
  362. {
  363. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  364. RPC_MESSAGE * pRpcMsg;
  365. MIDL_STUB_MESSAGE * pStubMsg;
  366. uchar * pArg;
  367. void * pThis = *(void **)StartofStack;
  368. CLIENT_CALL_RETURN ReturnValue;
  369. NDR64_PARAM_FORMAT * Params;
  370. long NumberParams;
  371. long n;
  372. NDR_ASYNC_CALL_FLAGS CallFlags;
  373. CStdAsyncProxyBuffer * pAsyncPB;
  374. const IID * piid;
  375. HRESULT hr = S_OK;
  376. NDR64_PARAM_FLAGS * pParamFlags;
  377. ReturnValue.Simple = 0;
  378. pAsyncPB = (CStdAsyncProxyBuffer*)
  379. ((uchar*)pThis - offsetof(CStdProxyBuffer, pProxyVtbl));
  380. piid = NdrGetProxyIID( pThis );
  381. hr = MulNdrpSetupFinishClientCall( pAsyncPB,
  382. StartofStack,
  383. *piid,
  384. nProcNum,
  385. pContext );
  386. if ( !SUCCEEDED(hr) )
  387. return hr;
  388. // Note that we cant call to Ndr64ProxyInitialize again.
  389. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)pAsyncPB->CallState.pAsyncMsg;
  390. pRpcMsg = & pAsyncMsg->RpcMsg;
  391. pStubMsg = & pAsyncMsg->StubMsg;
  392. // begin method negotiate to a different syntax than default
  393. if ( pContext->CurrentSyntaxType != pAsyncMsg->SyntaxType )
  394. {
  395. PFORMAT_STRING pFormat;
  396. pContext->CurrentSyntaxType = pAsyncMsg->SyntaxType;
  397. pContext->pSyntaxInfo = pAsyncMsg->ProcContext.pSyntaxInfo;
  398. pFormat = NdrpGetProcString( pContext->pSyntaxInfo,
  399. pContext->CurrentSyntaxType,
  400. nProcNum );
  401. MulNdrpInitializeContextFromProc( pContext->CurrentSyntaxType,
  402. pFormat,
  403. pContext,
  404. (uchar *)StartofStack );
  405. }
  406. // we can directly call to ndr20 finish routine because there is extra setup cost.
  407. if ( pContext->CurrentSyntaxType == XFER_SYNTAX_DCE )
  408. return NdrpFinishDcomAsyncClientCall( pProxyInfo->pStubDesc,
  409. pContext->pProcFormat,
  410. (uchar*)StartofStack );
  411. memcpy ( & pAsyncMsg->ProcContext, pContext, offsetof( NDR_PROC_CONTEXT, AllocateContext ) );
  412. NumberParams = pContext->NumberParams;
  413. CallFlags = pAsyncMsg->Flags;
  414. // Initialize the stack top in the stub msg to be
  415. // this stack, the stack for the finish call parameters.
  416. pAsyncMsg->nFinishParams = NumberParams;
  417. pAsyncMsg->FinishParams = pContext->Params;
  418. Params = (NDR64_PARAM_FORMAT *) pContext->Params;
  419. pStubMsg->StackTop = (uchar*)StartofStack;
  420. pStubMsg->pContext = &pAsyncMsg->ProcContext;
  421. // OLE only code path - single RpcTryExcept.
  422. //
  423. RpcTryExcept
  424. {
  425. BOOL fRaiseExcFlag = FALSE;
  426. if ( CallFlags.ErrorPending )
  427. RpcRaiseException( pAsyncMsg->ErrorCode );
  428. // We need to zero out the [out] parameters and to check
  429. // the ref pointers.
  430. for ( n = 0; n < NumberParams; n++ )
  431. {
  432. pParamFlags = ( NDR64_PARAM_FLAGS * ) & Params[n].Attributes ;
  433. pArg = (uchar *)StartofStack + Params[n].StackOffset;
  434. if ( pParamFlags->IsSimpleRef )
  435. {
  436. // We cannot raise the exception here,
  437. // as some out args may not be zeroed out yet.
  438. if ( ! *((uchar **)pArg) )
  439. {
  440. fRaiseExcFlag = TRUE;
  441. continue;
  442. }
  443. }
  444. // We do the basetype check to cover the
  445. // [out] simple ref to basetype case.
  446. //
  447. if ( pParamFlags->IsPartialIgnore ||
  448. ( ! pParamFlags->IsIn &&
  449. ! pParamFlags->IsReturn ))
  450. {
  451. if ( pParamFlags->IsBasetype )
  452. {
  453. // [out] only arg can only be ref, we checked that above.
  454. NDR64_FORMAT_CHAR type = *(PFORMAT_STRING)Params[n].Type;
  455. MIDL_memset( *(uchar **)pArg,
  456. 0,
  457. (size_t)NDR64_SIMPLE_TYPE_MEMSIZE( type ));
  458. }
  459. else
  460. {
  461. Ndr64ClientZeroOut(
  462. pStubMsg,
  463. Params[n].Type,
  464. *(uchar **)pArg );
  465. }
  466. }
  467. }
  468. if ( fRaiseExcFlag )
  469. RpcRaiseException( RPC_X_NULL_REF_POINTER );
  470. NdrDcomAsyncReceive( pStubMsg );
  471. Ndr64pClientUnMarshal( pStubMsg,
  472. &ReturnValue );
  473. // Pass the HR from the Finish call, if there was any, to the client.
  474. if ( pArg == (uchar *) &ReturnValue )
  475. hr = (HRESULT) ReturnValue.Simple;
  476. }
  477. RpcExcept( 1 )
  478. {
  479. RPC_STATUS ExceptionCode = RpcExceptionCode();
  480. //
  481. // In OLE, since they don't know about error_status_t and wanted to
  482. // reinvent the wheel, check to see if we need to map the exception.
  483. // In either case, set the return value and then try to free the
  484. // [out] params, if required.
  485. //
  486. hr = NdrHrFromWin32Error(ExceptionCode);
  487. //
  488. // Set the Buffer endpoints so the Ndr64Free routines work.
  489. //
  490. Ndr64pDcomClientExceptionHandling ( pStubMsg,
  491. nProcNum,
  492. hr,
  493. &ReturnValue );
  494. }
  495. RpcEndExcept
  496. // Finish
  497. // Cleanup everything. However, don't free pAsyncPB itself.
  498. NdrpAsyncProxyMsgDestructor( pAsyncPB );
  499. // Never addref or release async proxy object, this is app's/PM's job.
  500. return hr;
  501. }
  502. HRESULT RPC_ENTRY
  503. Ndr64DcomAsyncStubCall(
  504. struct IRpcStubBuffer * pThis,
  505. struct IRpcChannelBuffer * pChannel,
  506. PRPC_MESSAGE pRpcMsg,
  507. ulong * pdwStubPhase
  508. )
  509. /*++
  510. Routine Description :
  511. Server Interpreter entry point for DCOM async procs.
  512. This is the Begin entry for channel (regular dispatch entry from stub.c).
  513. The Finish happen when the channel calls stub's Synchronize::Signal method
  514. on the stub object. The call then comes to NdrpAsyncStubSignal later below.
  515. Arguments :
  516. pThis - Object proc's 'this' pointer.
  517. pChannel - Object proc's Channel Buffer.
  518. pRpcMsg - The RPC message.
  519. pdwStubPhase - Used to track the current interpreter's activity.
  520. Return :
  521. Status of S_OK.
  522. Notes :
  523. The engine never calls a signal on behalf of the user, regardless what kind of
  524. errors happen during begin (cannot setup begin, cannot unmarshal, app dies in invoke).
  525. In each of these cases, the engine simply returns an error code to the channel.
  526. The only time the engine would call FreeBuffer on the server is if the engine died
  527. between a successful GetBuffer and the final Send.
  528. Notes on OLE Refcounting.
  529. The model for async_uuid() is that async proxies or stubs are created
  530. with RefCount==1 and should never ever be addrefed by the engine.
  531. What the engine does is only the AsyncMsg clean up when done.
  532. The decision to destroy the AsyncPB or AsyncSB object is
  533. up to the client side PM or channel's SM for the server side.
  534. */
  535. {
  536. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  537. PMIDL_SERVER_INFO pServerInfo;
  538. PMIDL_STUB_DESC pStubDesc;
  539. const SERVER_ROUTINE * DispatchTable;
  540. unsigned long ProcNum;
  541. ushort FormatOffset;
  542. PFORMAT_STRING pFormat;
  543. PMIDL_STUB_MESSAGE pStubMsg;
  544. uchar * pArgBuffer;
  545. uchar * pArg;
  546. uchar ** ppArg;
  547. NDR64_PARAM_FORMAT * Params;
  548. BOOL fBadStubDataException = FALSE;
  549. BOOL fManagerCodeInvoked = FALSE;
  550. unsigned long n, i;
  551. CStdAsyncStubBuffer * pAsyncSB;
  552. HRESULT hr;
  553. const IID * piid = 0;
  554. BOOL fErrorInInvoke = FALSE;
  555. BOOL fRecoverableErrorInInvoke = FALSE;
  556. IUnknown * pSrvObj;
  557. CInterfaceStubVtbl * pStubVTable;
  558. NDR_PROC_CONTEXT ProcContext, *pContext;
  559. NDR64_PROC_FORMAT * pProcFormat;
  560. NDR64_PROC_FLAGS * pNdr64Flags;
  561. NDR64_PARAM_FLAGS * pParamFlags;
  562. SYNTAX_TYPE SyntaxType;
  563. RPC_STATUS ExceptionCode = 0;
  564. MIDL_SYNTAX_INFO * pSyntaxInfo = NULL;
  565. NDR_ASSERT( ! ((ULONG_PTR)pRpcMsg->Buffer & 0x7),
  566. "marshaling buffer misaligned at server" );
  567. // The channel dispatches to the engine with the sync proc num.
  568. // We need only async proc num at the engine level.
  569. ProcNum = pRpcMsg->ProcNum;
  570. ProcNum = 2 * ProcNum - 3; // Begin method #
  571. pSrvObj = (IUnknown *)((CStdStubBuffer *)pThis)->pvServerObject;
  572. DispatchTable = (SERVER_ROUTINE *)pSrvObj->lpVtbl;
  573. pStubVTable = (CInterfaceStubVtbl *)
  574. (*((uchar **)pThis) - sizeof(CInterfaceStubHeader));
  575. piid = pStubVTable->header.piid;
  576. pServerInfo = (PMIDL_SERVER_INFO) pStubVTable->header.pServerInfo;
  577. SyntaxType = NdrpGetSyntaxType( pRpcMsg->TransferSyntax );
  578. if ( SyntaxType == XFER_SYNTAX_DCE )
  579. return NdrDcomAsyncStubCall( pThis, pChannel, pRpcMsg, pdwStubPhase );
  580. pStubDesc = pServerInfo->pStubDesc;
  581. pAsyncSB = (CStdAsyncStubBuffer *)
  582. ((uchar *)pThis - offsetof(CStdAsyncStubBuffer,lpVtbl));
  583. for ( i = 0; i < (ulong)pServerInfo->nCount; i++ )
  584. {
  585. if ( SyntaxType == NdrpGetSyntaxType( &pServerInfo->pSyntaxInfo[i].TransferSyntax ) )
  586. {
  587. pSyntaxInfo = &pServerInfo->pSyntaxInfo[i];
  588. break;
  589. }
  590. }
  591. if ( NULL == pSyntaxInfo )
  592. return HRESULT_FROM_WIN32( RPC_S_UNSUPPORTED_TRANS_SYN );
  593. NdrServerSetupNDR64TransferSyntax( ProcNum, pSyntaxInfo, &ProcContext);
  594. pProcFormat = ProcContext.Ndr64Header;
  595. pNdr64Flags = (NDR64_PROC_FLAGS *)&pProcFormat->Flags;
  596. hr = NdrpValidateAsyncStubCall( pAsyncSB );
  597. if ( ! SUCCEEDED(hr) )
  598. return hr;
  599. hr = Ndr64pSetupBeginStubCall( pAsyncSB, &ProcContext, *piid );
  600. if ( FAILED(hr) )
  601. return hr;
  602. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)pAsyncSB->CallState.pAsyncMsg;
  603. pStubMsg = & pAsyncMsg->StubMsg;
  604. // Both rpc runtime and channel require that we use a copy of the rpc message.
  605. RpcpMemoryCopy( & pAsyncMsg->RpcMsg, pRpcMsg, sizeof(RPC_MESSAGE) );
  606. pRpcMsg = & pAsyncMsg->RpcMsg;
  607. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  608. pStubMsg->RpcMsg = pRpcMsg;
  609. pContext = &pAsyncMsg->ProcContext;
  610. // The arg buffer is zeroed out already.
  611. pArgBuffer = pAsyncMsg->ProcContext.StartofStack;
  612. //
  613. // Get new interpreter info.
  614. //
  615. Params = (NDR64_PARAM_FORMAT *) pContext->Params;
  616. pAsyncMsg->nBeginParams = pContext->NumberParams;
  617. pAsyncMsg->BeginParams = pContext->Params;
  618. pAsyncMsg->pThis = pThis;
  619. pAsyncMsg->SyntaxType = pContext->CurrentSyntaxType;
  620. //
  621. // Wrap the unmarshalling and the invoke call in the try block of
  622. // a try-finally. Put the free phase in the associated finally block.
  623. //
  624. // We abstract the level of indirection here.
  625. RpcTryFinally
  626. {
  627. // OLE: put pThis in first dword of stack.
  628. //
  629. ((void **)pArgBuffer)[0] = ((CStdStubBuffer *)pThis)->pvServerObject;
  630. // Initialize the Stub message.
  631. //
  632. NdrStubInitialize( pRpcMsg,
  633. pStubMsg,
  634. pStubDesc,
  635. pChannel );
  636. pStubMsg->pAsyncMsg = (struct _NDR_ASYNC_MESSAGE *) pAsyncMsg;
  637. pStubMsg->pContext = pContext;
  638. pAsyncMsg->BeginParams = Params;
  639. pAsyncMsg->pdwStubPhase = pdwStubPhase; // the phase is STUB_UNMARSHAL
  640. // Raise exceptions after initializing the stub.
  641. if ( pNdr64Flags->UsesFullPtrPackage )
  642. pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit( 0, XLAT_SERVER );
  643. else
  644. pStubMsg->FullPtrXlatTables = 0;
  645. //
  646. // Set StackTop AFTER the initialize call, since it zeros the field
  647. // out.
  648. //
  649. pStubMsg->StackTop = pArgBuffer;
  650. // StubPhase set up by invoke is STUB_UNMARSHAL
  651. RpcTryExcept
  652. {
  653. NDR_ASSERT( pContext->StartofStack == pArgBuffer, "startofstack is not set" );
  654. Ndr64pServerUnMarshal ( pStubMsg );
  655. // Last ditch checks.
  656. if ( pRpcMsg->BufferLength <
  657. (uint)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer) )
  658. {
  659. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  660. }
  661. }
  662. RpcExcept( NdrServerUnmarshallExceptionFlag(GetExceptionInformation()) )
  663. {
  664. // Filter set in rpcndr.h to catch one of the following
  665. // STATUS_ACCESS_VIOLATION
  666. // STATUS_DATATYPE_MISALIGNMENT
  667. // RPC_X_BAD_STUB_DATA
  668. ExceptionCode = RpcExceptionCode();
  669. fBadStubDataException = TRUE;
  670. if ( RPC_BAD_STUB_DATA_EXCEPTION_FILTER )
  671. ExceptionCode = RPC_X_BAD_STUB_DATA;
  672. pAsyncMsg->Flags.ErrorPending = 1;
  673. pAsyncMsg->Flags.BadStubData = 1;
  674. pAsyncMsg->ErrorCode = ExceptionCode;
  675. pAsyncSB->CallState.Flags.BeginError = 1;
  676. pAsyncSB->CallState.Hr = NdrHrFromWin32Error( ExceptionCode);
  677. NdrpFreeMemoryList( pStubMsg );
  678. RpcRaiseException( ExceptionCode );
  679. }
  680. RpcEndExcept
  681. //
  682. // Do [out] initialization before the invoke.
  683. //
  684. for ( n = 0; n < pContext->NumberParams; n++ )
  685. {
  686. pParamFlags = ( NDR64_PARAM_FLAGS * ) & ( Params[n].Attributes );
  687. if ( pParamFlags->IsIn ||
  688. pParamFlags->IsReturn )
  689. continue;
  690. // This is a Begin call, there cannot be any [out] only args.
  691. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  692. }
  693. //
  694. // OLE interfaces use pdwStubPhase in the exception filter.
  695. // See CStdStubBuffer_Invoke in stub.c.
  696. //
  697. *pdwStubPhase = STUB_CALL_SERVER;
  698. // We need to catch exception in the manager code separately
  699. // as the model implies that there will be no other call from
  700. // the server app to clean up.
  701. pAsyncSB->CallState.Flags.BeginDone = 1;
  702. RpcTryExcept
  703. {
  704. //
  705. // Check for a thunk. Compiler does all the setup for us.
  706. //
  707. if ( pServerInfo->ThunkTable && pServerInfo->ThunkTable[ ProcNum ] )
  708. {
  709. fManagerCodeInvoked = TRUE;
  710. pServerInfo->ThunkTable[ ProcNum ]( pStubMsg );
  711. }
  712. else
  713. {
  714. //
  715. // Note that this ArgNum is not the number of arguments declared
  716. // in the function we called, but really the number of
  717. // REGISTER_TYPEs occupied by the arguments to a function.
  718. //
  719. long ArgNum;
  720. MANAGER_FUNCTION pFunc;
  721. REGISTER_TYPE ReturnValue;
  722. pFunc = (MANAGER_FUNCTION) DispatchTable[ ProcNum ];
  723. ArgNum = (long)pProcFormat->StackSize / sizeof(REGISTER_TYPE);
  724. //
  725. // The StackSize includes the size of the return. If we want
  726. // just the number of REGISTER_TYPES, then ArgNum must be reduced
  727. // by 1 when there is a return value AND the current ArgNum count
  728. // is greater than 0.
  729. //
  730. if ( ArgNum && pNdr64Flags->HasReturn )
  731. ArgNum--;
  732. // Being here means that we can expect results. Note that the user
  733. // can call RpcCompleteCall from inside of the manager code.
  734. fManagerCodeInvoked = TRUE;
  735. ReturnValue = Invoke( pFunc,
  736. (REGISTER_TYPE *)pArgBuffer,
  737. #if defined(_IA64_)
  738. pProcFormat->FloatDoubleMask,
  739. #endif
  740. ArgNum);
  741. if ( pNdr64Flags->HasReturn )
  742. {
  743. // Pass the app's HR from Begin call to the channel.
  744. (*pfnDcomChannelSetHResult)( pRpcMsg,
  745. NULL, // reserved
  746. (HRESULT) ReturnValue);
  747. }
  748. // We are discarding the return value as it is not the real one.
  749. }
  750. }
  751. RpcExcept( 1 )
  752. {
  753. fErrorInInvoke = TRUE;
  754. pAsyncMsg->Flags.ErrorPending = 1;
  755. pAsyncMsg->ErrorCode = RpcExceptionCode();
  756. pAsyncSB->CallState.Flags.BeginError = 1;
  757. pAsyncSB->CallState.Hr = NdrHrFromWin32Error( RpcExceptionCode());
  758. }
  759. RpcEndExcept
  760. // Done with invoking Begin
  761. }
  762. RpcFinally
  763. {
  764. if ( !fManagerCodeInvoked )
  765. {
  766. // Failed without invoking Begin - return an error. Remember the error.
  767. if ( fBadStubDataException )
  768. pAsyncMsg->ErrorCode = RPC_X_BAD_STUB_DATA;
  769. pAsyncSB->CallState.Flags.BeginDone = 1;
  770. hr = pAsyncSB->CallState.Hr;
  771. }
  772. else // fManagerCodeInvoked
  773. {
  774. hr = S_OK;
  775. if ( fErrorInInvoke )
  776. hr = pAsyncSB->CallState.Hr;
  777. }
  778. }
  779. RpcEndFinally
  780. return hr;
  781. }
  782. void
  783. Ndr64pCloneInOutStubArgs(
  784. NDR_DCOM_ASYNC_MESSAGE * pAsyncMsg )
  785. /*
  786. Walk the second stack looking for an in-out argument.
  787. For each one, find the corresponding in-out atgument from the first stack
  788. and clone it to the second stack.
  789. Note, we need to do it only on the server side where we preserver the first
  790. stack, the dispatch buffer and all the arguments from the first stack.
  791. On the client, this is the app's task to supply meaningful in-out arguments
  792. for the second stack.
  793. */
  794. {
  795. REGISTER_TYPE * pBeginStack = (REGISTER_TYPE *)pAsyncMsg->BeginStack;
  796. REGISTER_TYPE * pFinishStack = (REGISTER_TYPE *)pAsyncMsg->FinishStack;
  797. NDR64_PARAM_FORMAT * BeginParams = ( NDR64_PARAM_FORMAT *) pAsyncMsg->BeginParams;
  798. int BeginNumberParams = pAsyncMsg->nBeginParams;
  799. NDR64_PARAM_FLAGS * pParamFlags, * pBeginParamFlags;
  800. NDR64_PARAM_FORMAT * FinishParams = ( NDR64_PARAM_FORMAT *) pAsyncMsg->FinishParams;
  801. int FinishNumberParams = pAsyncMsg->nFinishParams;
  802. int FirstIO = 0;
  803. int n;
  804. for ( n = 0; n < FinishNumberParams; n++ )
  805. {
  806. pParamFlags = ( NDR64_PARAM_FLAGS *) & FinishParams[n].Attributes;
  807. // Find in-out arg that needs cloning.
  808. if ( pParamFlags->IsIn &&
  809. pParamFlags->IsOut )
  810. {
  811. // Find the first IO on the first stack
  812. while ( FirstIO < BeginNumberParams )
  813. {
  814. pBeginParamFlags = ( NDR64_PARAM_FLAGS *) & BeginParams[FirstIO].Attributes;
  815. if ( pBeginParamFlags->IsIn &&
  816. pBeginParamFlags->IsOut )
  817. {
  818. break;
  819. }
  820. FirstIO++;
  821. }
  822. if ( BeginNumberParams <= FirstIO )
  823. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  824. // Clone it to the second stack
  825. pFinishStack[ FinishParams[n].StackOffset / sizeof(REGISTER_TYPE) ] =
  826. pBeginStack[ BeginParams[ FirstIO ].StackOffset / sizeof(REGISTER_TYPE) ];
  827. FirstIO++;
  828. }
  829. }
  830. }
  831. HRESULT
  832. Ndr64pCompleteDcomAsyncStubCall(
  833. CStdAsyncStubBuffer * pAsyncSB
  834. )
  835. /*++
  836. Routine Description :
  837. Complete an async call on the server side.
  838. Arguments :
  839. AsyncHandle - raw or object handle (if pointer) as appropriate,
  840. pAsyncMsg - pointer to async msg structure,
  841. pReturnValue - from the user to pass back to caller.
  842. Return :
  843. Status of S_OK.
  844. --*/
  845. {
  846. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  847. PMIDL_SERVER_INFO pServerInfo;
  848. const SERVER_ROUTINE * DispatchTable; // should be the same
  849. unsigned long ProcNum; // should be 1+
  850. RPC_MESSAGE * pRpcMsg;
  851. MIDL_STUB_MESSAGE * pStubMsg;
  852. NDR64_PARAM_FORMAT * Params; // Finish params
  853. uchar * pArgBuffer; // new stack
  854. // MZ, BUG BUG, Fix after ship
  855. // ulong * pdwStubPhase;
  856. uchar * pArg;
  857. long NumberParams;
  858. long n;
  859. NDR64_PROC_FORMAT * pProcHeader;
  860. IUnknown * pSrvObj;
  861. CInterfaceStubVtbl * pStubVTable;
  862. void * pThis;
  863. HRESULT hr;
  864. const IID * piid; // should be the same
  865. BOOL fManagerCodeInvoked = FALSE;
  866. BOOL fErrorInInvoke = FALSE;
  867. RPC_STATUS ExceptionCode = 0;
  868. boolean fParamsFreed = FALSE;
  869. NDR64_PARAM_FLAGS * pParamFlags;
  870. NDR64_PROC_FLAGS * pNdr64Flags;
  871. NDR_PROC_CONTEXT * pContext = NULL;
  872. // We validated both the stub and the async context in the signal call.
  873. // We validated the pAsyncSB in the Signal call.
  874. // Do additional checks.
  875. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)pAsyncSB->CallState.pAsyncMsg;
  876. pThis = pAsyncMsg->pThis;
  877. // See if channel calls on the right stub
  878. if ( & pAsyncSB->lpVtbl != pThis)
  879. return E_INVALIDARG;
  880. pRpcMsg = & pAsyncMsg->RpcMsg;
  881. pStubMsg = & pAsyncMsg->StubMsg;
  882. pContext = (NDR_PROC_CONTEXT *)pStubMsg->pContext ;
  883. // We have preserved the sync proc num that the channel used.
  884. // We need only async proc num at the engine level.
  885. //
  886. ProcNum = pRpcMsg->ProcNum;
  887. ProcNum = 2 * ProcNum - 3 + 1; // Finish method #
  888. pSrvObj = (IUnknown *)((CStdStubBuffer *)pThis)->pvServerObject;
  889. DispatchTable = (SERVER_ROUTINE *)pSrvObj->lpVtbl;
  890. pStubVTable = (CInterfaceStubVtbl *)
  891. (*((uchar **)pThis) - sizeof(CInterfaceStubHeader));
  892. piid = pStubVTable->header.piid;
  893. pServerInfo = (PMIDL_SERVER_INFO) pStubVTable->header.pServerInfo;
  894. // The proc header has a fixed layout now.
  895. pProcHeader =
  896. (PNDR64_PROC_FORMAT)NdrpGetProcString( pAsyncMsg->ProcContext.pSyntaxInfo,
  897. XFER_SYNTAX_NDR64,
  898. ProcNum );
  899. pNdr64Flags = ( NDR64_PROC_FLAGS * )&pProcHeader->Flags;
  900. // Validate and setup for finish.
  901. hr = NdrpSetupFinishStubCall( pAsyncSB,
  902. (ushort )pProcHeader->StackSize,
  903. *piid );
  904. if ( hr )
  905. return hr;
  906. // The arg buffer is zeroed out already. Note, this is the second stack.
  907. pArgBuffer = pAsyncMsg->ProcContext.StartofStack;
  908. pStubMsg->StackTop = pArgBuffer;
  909. // MZ, BUG BUG, fix after ship
  910. // pdwStubPhase = pAsyncMsg->pdwStubPhase;
  911. //
  912. // Get new interpreter info.
  913. //
  914. NumberParams = pProcHeader->NumberOfParams;
  915. Params = (NDR64_PARAM_FORMAT *)(((PFORMAT_STRING)( pProcHeader + 1 )) + pProcHeader->ExtensionSize );
  916. pAsyncMsg->nFinishParams = pContext->NumberParams = NumberParams;
  917. pAsyncMsg->FinishParams = pContext->Params = Params;
  918. // Wrap the unmarshalling, mgr call and marshalling in the try block of
  919. // a try-finally. Put the free phase in the associated finally block.
  920. //
  921. RpcTryFinally
  922. {
  923. if ( pAsyncMsg->Flags.ErrorPending )
  924. RpcRaiseException( pAsyncMsg->ErrorCode );
  925. // Initialize the args of the new stack.
  926. // OLE: put pThis in first dword of stack.
  927. //
  928. ((void **)pArgBuffer)[0] = ((CStdStubBuffer *)pThis)->pvServerObject;
  929. //
  930. // Do [out] initialization before invoking Finish
  931. //
  932. Ndr64pCloneInOutStubArgs( pAsyncMsg );
  933. Ndr64pServerOutInit( pStubMsg );
  934. //
  935. // OLE interfaces use pdwStubPhase in the exception filter.
  936. // See CStdStubBuffer_Invoke in stub.c.
  937. //
  938. // MZ, BUG BUG, fix after ship
  939. // *pdwStubPhase = STUB_CALL_SERVER;
  940. // We need to catch exception in the manager code separately
  941. // as the model implies that there will be no other call from
  942. // the server app to clean up.
  943. RpcTryExcept
  944. {
  945. //
  946. // Check for a thunk. Compiler does all the setup for us.
  947. //
  948. if ( pServerInfo->ThunkTable && pServerInfo->ThunkTable[ProcNum] )
  949. {
  950. fManagerCodeInvoked = TRUE;
  951. pServerInfo->ThunkTable[ProcNum]( pStubMsg );
  952. }
  953. else
  954. {
  955. //
  956. // Note that this ArgNum is not the number of arguments declared
  957. // in the function we called, but really the number of
  958. // REGISTER_TYPEs occupied by the arguments to a function.
  959. //
  960. long ArgNum;
  961. MANAGER_FUNCTION pFunc;
  962. REGISTER_TYPE ReturnValue;
  963. pFunc = (MANAGER_FUNCTION) DispatchTable[ProcNum];
  964. ArgNum = (long)pProcHeader->StackSize / sizeof(REGISTER_TYPE);
  965. //
  966. // The StackSize includes the size of the return. If we want
  967. // just the number of REGISTER_TYPES, then ArgNum must be reduced
  968. // by 1 when there is a return value AND the current ArgNum count
  969. // is greater than 0.
  970. //
  971. if ( ArgNum && pNdr64Flags->HasReturn )
  972. ArgNum--;
  973. fManagerCodeInvoked = TRUE;
  974. ReturnValue = Invoke( pFunc,
  975. (REGISTER_TYPE *)pArgBuffer,
  976. #if defined(_IA64_)
  977. pProcHeader->FloatDoubleMask,
  978. #endif
  979. ArgNum);
  980. // This is the return value that should be marshaled back.
  981. if ( pNdr64Flags->HasReturn )
  982. {
  983. ((REGISTER_TYPE *)pArgBuffer)[ArgNum] = ReturnValue;
  984. // Pass the app's HR to the channel.
  985. (*pfnDcomChannelSetHResult)( pRpcMsg,
  986. NULL, // reserved
  987. (HRESULT) ReturnValue);
  988. }
  989. }
  990. }
  991. RpcExcept( 1 )
  992. {
  993. pAsyncMsg->Flags.ErrorPending = 1;
  994. pAsyncMsg->ErrorCode = RpcExceptionCode();
  995. fErrorInInvoke = TRUE;
  996. }
  997. RpcEndExcept
  998. // Done with invoking Finish
  999. if ( pAsyncMsg->Flags.ErrorPending )
  1000. RpcRaiseException( pAsyncMsg->ErrorCode );
  1001. // MZ, BUG BUG, fix after ship
  1002. // *pdwStubPhase = STUB_MARSHAL;
  1003. //
  1004. // Buffer size pass.
  1005. //
  1006. pStubMsg->BufferLength = pProcHeader->ConstantServerBufferSize;
  1007. if ( pNdr64Flags->ServerMustSize )
  1008. {
  1009. Ndr64pSizing( pStubMsg,
  1010. FALSE ); //server
  1011. }
  1012. // Get buffer.
  1013. NdrStubGetBuffer( (IRpcStubBuffer *)pAsyncMsg->pThis,
  1014. pStubMsg->pRpcChannelBuffer,
  1015. pStubMsg );
  1016. // Marshalling pass.
  1017. Ndr64pServerMarshal ( pStubMsg );
  1018. if ( pRpcMsg->BufferLength <
  1019. (uint)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer) )
  1020. {
  1021. NDR_ASSERT( 0, "Ndr64StubCall2 marshal: buffer overflow!" );
  1022. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  1023. }
  1024. pRpcMsg->BufferLength = (ulong)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer);
  1025. // We don't drop to the runtime like for synchronous calls,
  1026. // we send the last buffer explicitly.
  1027. fParamsFreed = TRUE;
  1028. // see comment on async.cxx on why we call this twice.
  1029. Ndr64pAsyncDCOMFreeParams( pAsyncMsg );
  1030. NdrpDcomAsyncSend( pStubMsg,
  1031. 0 ); // server doesn't pass pSynchronize back to channel.
  1032. }
  1033. RpcFinally
  1034. {
  1035. // Don't free parameters if we died because of bad stub data in unmarshaling.
  1036. if ( ! pAsyncMsg->Flags.BadStubData && !fParamsFreed)
  1037. {
  1038. Ndr64pAsyncDCOMFreeParams( pAsyncMsg );
  1039. }
  1040. if ( pAsyncMsg->Flags.ErrorPending )
  1041. hr = NdrHrFromWin32Error( pAsyncMsg->ErrorCode );
  1042. else
  1043. hr = S_OK;
  1044. // If we are here, error or not, it means that we can (and need to) dispose of
  1045. // the async context information
  1046. NdrpAsyncStubMsgDestructor( pAsyncSB );
  1047. // The engine never addrefs or releases the call object.
  1048. }
  1049. RpcEndFinally
  1050. return hr;
  1051. }
  1052. HRESULT
  1053. Ndr64pSetupBeginStubCall(
  1054. CStdAsyncStubBuffer * pAsyncSB,
  1055. NDR_PROC_CONTEXT * pContext,
  1056. REFIID riid )
  1057. /*
  1058. This method creates and initializes async msg.
  1059. */
  1060. {
  1061. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1062. HRESULT hr = S_OK;
  1063. if ( pAsyncSB->CallState.pAsyncMsg != 0 ||
  1064. pAsyncSB->CallState.Flags.BeginStarted )
  1065. hr = E_FAIL;
  1066. else
  1067. {
  1068. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)
  1069. I_RpcBCacheAllocate( sizeof( NDR_DCOM_ASYNC_MESSAGE) +
  1070. pContext->StackSize + NDR_ASYNC_GUARD_SIZE );
  1071. if ( ! pAsyncMsg )
  1072. hr = E_OUTOFMEMORY;
  1073. }
  1074. if ( ! SUCCEEDED(hr) )
  1075. {
  1076. // The stub never signals.
  1077. pAsyncSB->CallState.Flags.BeginError = 1;
  1078. pAsyncSB->CallState.Hr = hr;
  1079. return hr;
  1080. }
  1081. // Initialize the async message properly
  1082. MIDL_memset( pAsyncMsg, 0x0, sizeof( NDR_DCOM_ASYNC_MESSAGE) );
  1083. pAsyncMsg->Signature = NDR_DCOM_ASYNC_SIGNATURE;
  1084. pAsyncMsg->Version = NDR_DCOM_ASYNC_VERSION;
  1085. pAsyncMsg->SyntaxType = XFER_SYNTAX_NDR64;
  1086. memcpy( &pAsyncMsg->ProcContext, pContext, offsetof( NDR_PROC_CONTEXT, AllocateContext ) );
  1087. NdrpAllocaInit( &pAsyncMsg->ProcContext.AllocateContext );
  1088. pAsyncMsg->ProcContext.StartofStack = (uchar *) & pAsyncMsg->AppStack;
  1089. pAsyncMsg->BeginStack = (uchar *) & pAsyncMsg->AppStack;
  1090. pAsyncMsg->BeginStackSize = pContext->StackSize;
  1091. pAsyncMsg->StubPhase = STUB_UNMARSHAL;
  1092. pAsyncMsg->StubMsg.pContext = &pAsyncMsg->ProcContext;
  1093. // Server: zero out stack for allocs.
  1094. MIDL_memset( & pAsyncMsg->AppStack, 0x0, pContext->StackSize );
  1095. MIDL_memset( ((char *)& pAsyncMsg->AppStack) + pContext->StackSize,
  1096. 0x71,
  1097. NDR_ASYNC_GUARD_SIZE );
  1098. pAsyncSB->CallState.pAsyncMsg = pAsyncMsg;
  1099. pAsyncSB->CallState.Flags.BeginStarted = 1;
  1100. pAsyncMsg->pAsyncSB = pAsyncSB;
  1101. return S_OK;
  1102. }
  1103. HRESULT
  1104. MulNdrpSetupFinishClientCall(
  1105. CStdAsyncProxyBuffer * pAsyncPB,
  1106. void * StartofStack,
  1107. REFIID riid,
  1108. unsigned long FinishProcNum,
  1109. NDR_PROC_CONTEXT * pContext )
  1110. /*
  1111. This method creates and initializes async msg.
  1112. */
  1113. {
  1114. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1115. HRESULT hr = S_OK;
  1116. hr = NdrpValidateAsyncProxyCall( pAsyncPB );
  1117. if ( ! SUCCEEDED(hr) )
  1118. return hr;
  1119. if ( !pAsyncPB->CallState.Flags.BeginStarted ||
  1120. !pAsyncPB->CallState.Flags.BeginDone ||
  1121. pAsyncPB->CallState.Flags.FinishStarted )
  1122. return E_FAIL;
  1123. pAsyncMsg =
  1124. (NDR_DCOM_ASYNC_MESSAGE*)pAsyncPB->CallState.pAsyncMsg;
  1125. hr = NdrpValidateDcomAsyncMsg( pAsyncMsg );
  1126. if ( ! SUCCEEDED(hr) )
  1127. return hr;
  1128. if ( (FinishProcNum + 3)/2 != (pAsyncMsg->RpcMsg.ProcNum & 0x7fff) )
  1129. return E_FAIL;
  1130. // return S_FALSE in SYNTAX_DCE: we'll call into
  1131. // NdrpDcomFinishClientCall
  1132. if ( pAsyncMsg->SyntaxType == XFER_SYNTAX_DCE )
  1133. return S_OK;
  1134. pAsyncMsg->ProcContext.StartofStack = (uchar *) StartofStack;
  1135. pAsyncMsg->FinishStack = (uchar *) StartofStack;
  1136. pAsyncMsg->FinishStackSize = pContext->StackSize;
  1137. pAsyncMsg->StubPhase = NDR_ASYNC_PREP_PHASE;
  1138. // Dont allocate or copy the new stack anywhere.
  1139. pAsyncPB->CallState.Flags.FinishStarted = 1;
  1140. return S_OK;
  1141. }
  1142. HRESULT
  1143. MulNdrpSetupBeginClientCall(
  1144. CStdAsyncProxyBuffer * pAsyncPB,
  1145. void * StartofStack,
  1146. NDR_PROC_CONTEXT * pContext,
  1147. REFIID riid )
  1148. /*
  1149. This method creates and initializes async msg.
  1150. */
  1151. {
  1152. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1153. HRESULT hr = S_OK;
  1154. hr = NdrpValidateAsyncProxyCall( pAsyncPB );
  1155. if ( ! SUCCEEDED(hr) )
  1156. return hr;
  1157. if ( pAsyncPB->CallState.pAsyncMsg != 0 ||
  1158. pAsyncPB->CallState.Flags.BeginStarted )
  1159. return E_FAIL;
  1160. // Do this first to simplify error conditions.
  1161. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)
  1162. I_RpcBCacheAllocate( sizeof(NDR_DCOM_ASYNC_MESSAGE) +
  1163. pContext->StackSize + NDR_ASYNC_GUARD_SIZE );
  1164. if ( ! pAsyncMsg )
  1165. {
  1166. NdrpAsyncProxySignal( pAsyncPB );
  1167. return E_OUTOFMEMORY;
  1168. }
  1169. // Initialize the async message properly
  1170. MIDL_memset( pAsyncMsg, 0x0, sizeof( NDR_DCOM_ASYNC_MESSAGE) );
  1171. pAsyncMsg->Signature = NDR_DCOM_ASYNC_SIGNATURE;
  1172. pAsyncMsg->Version = NDR_DCOM_ASYNC_VERSION;
  1173. pAsyncMsg->ProcContext.StartofStack = (uchar *) & pAsyncMsg->AppStack;
  1174. pAsyncMsg->BeginStack = (uchar *) & pAsyncMsg->AppStack;
  1175. pAsyncMsg->BeginStackSize = pContext->StackSize;
  1176. pAsyncMsg->StubPhase = NDR_ASYNC_PREP_PHASE;
  1177. RpcpMemoryCopy( & pAsyncMsg->ProcContext, pContext, offsetof( NDR_PROC_CONTEXT, AllocateContext ) );
  1178. NdrpAllocaInit( &pAsyncMsg->ProcContext.AllocateContext );
  1179. // Client: copy stack from the app's request call.
  1180. RpcpMemoryCopy( & pAsyncMsg->AppStack, StartofStack, pContext->StackSize );
  1181. MIDL_memset( ((char *)& pAsyncMsg->AppStack) + pContext->StackSize,
  1182. 0x71,
  1183. NDR_ASYNC_GUARD_SIZE );
  1184. pAsyncMsg->pAsyncPB = pAsyncPB;
  1185. pAsyncMsg->StubMsg.pContext = &pAsyncMsg->ProcContext;
  1186. pAsyncPB->CallState.Flags.BeginStarted = 1;
  1187. pAsyncPB->CallState.pAsyncMsg = pAsyncMsg;
  1188. return S_OK;
  1189. }