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.

2251 lines
65 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. #define USE_STUBLESS_PROXY
  12. #define CINTERFACE
  13. #include "ndrp.h"
  14. #include "ndrole.h"
  15. #include "rpcproxy.h"
  16. #include "mulsyntx.h"
  17. #include "hndl.h"
  18. #include "interp2.h"
  19. #include "asyncu.h"
  20. #include "attack.h"
  21. #include <stddef.h>
  22. #include <stdarg.h>
  23. #pragma code_seg(".orpc")
  24. RPC_STATUS
  25. NdrpBeginDcomAsyncClientCall(
  26. PMIDL_STUB_DESC pStubDescriptor,
  27. PFORMAT_STRING pFormat,
  28. unsigned char * StartofStack
  29. );
  30. RPC_STATUS
  31. NdrpFinishDcomAsyncClientCall(
  32. PMIDL_STUB_DESC pStubDescriptor,
  33. PFORMAT_STRING pFormat,
  34. unsigned char * StartofStack
  35. );
  36. const IID * RPC_ENTRY
  37. NdrGetProxyIID(
  38. const void *pThis);
  39. VOID
  40. NdrpAsyncDCOMFreeParams(
  41. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg )
  42. {
  43. /*++
  44. Routine Description:
  45. Frees the parameters for both the begin and finish calls.
  46. Arguments:
  47. pAsyncMsg - Supplies a pointer to the async message.
  48. Return Value:
  49. None.
  50. --*/
  51. if ( pAsyncMsg->BeginStack )
  52. {
  53. if ( pAsyncMsg->FinishStack )
  54. {
  55. // Clear out the IN OUT parameters on the begin stack
  56. // so that they are not freed twice.
  57. int n;
  58. REGISTER_TYPE *pBeginStack = (REGISTER_TYPE *)pAsyncMsg->BeginStack;
  59. PPARAM_DESCRIPTION BeginParams = (PPARAM_DESCRIPTION)pAsyncMsg->BeginParams;
  60. int BeginNumberParams = (int)pAsyncMsg->nBeginParams;
  61. for( n = 0; n < BeginNumberParams; n++ )
  62. {
  63. if ( BeginParams[n].ParamAttr.IsIn &&
  64. BeginParams[n].ParamAttr.IsOut )
  65. {
  66. pBeginStack[ BeginParams[ n ].StackOffset / sizeof(REGISTER_TYPE) ] = 0;
  67. }
  68. }
  69. }
  70. NdrpFreeParams( & (pAsyncMsg->StubMsg),
  71. pAsyncMsg->nBeginParams,
  72. (PPARAM_DESCRIPTION)pAsyncMsg->BeginParams,
  73. pAsyncMsg->BeginStack );
  74. }
  75. if ( pAsyncMsg->FinishStack )
  76. {
  77. NdrpFreeParams( & (pAsyncMsg->StubMsg),
  78. pAsyncMsg->nFinishParams,
  79. (PPARAM_DESCRIPTION)pAsyncMsg->FinishParams,
  80. pAsyncMsg->FinishStack );
  81. }
  82. }
  83. CLIENT_CALL_RETURN RPC_VAR_ENTRY
  84. NdrDcomAsyncClientCall(
  85. PMIDL_STUB_DESC pStubDescriptor,
  86. PFORMAT_STRING pFormat,
  87. ...
  88. )
  89. /*
  90. This entry is used by the stubless proxy invoker and also by OLE thunks.
  91. Sync stubless proxies would invoke to NdrClientCall2.
  92. On ia64, this entry would be used by -Oic generated code.
  93. Note that signaling on the client happens behind the async proxy's back
  94. as channel effectively signals to the app and the app issues the Finish.
  95. */
  96. {
  97. va_list ArgList;
  98. unsigned char * StartofStack;
  99. CLIENT_CALL_RETURN Ret;
  100. ulong ProcNum;
  101. //
  102. // Get address of argument to this function following pFormat. This
  103. // is the address of the address of the first argument of the function
  104. // calling this function.
  105. // Then get the address of the stack where the parameters are.
  106. //
  107. INIT_ARG( ArgList, pFormat);
  108. GET_FIRST_IN_ARG(ArgList);
  109. StartofStack = (uchar *) GET_STACK_START(ArgList);
  110. // Object proc layout is fixed for anything that can show up here.
  111. ProcNum = *(ushort *)(pFormat+6);
  112. if ( ProcNum & 0x1 )
  113. {
  114. // An odd proc number means a Begin call (0,1,2,Begin,Finish, ...).
  115. Ret.Simple = NdrpBeginDcomAsyncClientCall( pStubDescriptor,
  116. pFormat,
  117. StartofStack );
  118. }
  119. else
  120. {
  121. Ret.Simple = NdrpFinishDcomAsyncClientCall( pStubDescriptor,
  122. pFormat,
  123. StartofStack );
  124. }
  125. return Ret;
  126. }
  127. #if defined(_IA64_)
  128. CLIENT_CALL_RETURN RPC_ENTRY
  129. NdrpDcomAsyncClientCall(
  130. PMIDL_STUB_DESC pStubDescriptor,
  131. PFORMAT_STRING pFormat,
  132. unsigned char * StartofStack
  133. )
  134. /*
  135. Used only on ia64,
  136. this entry is used by the stubless proxy invoker and also by OLE thunks.
  137. Sync stubless proxies would invoke to NdrpClientCall2.
  138. Note that signaling on the client happens behind the async proxy's back
  139. as channel effectively signals to the app and the app issues the Finish.
  140. */
  141. {
  142. CLIENT_CALL_RETURN Ret;
  143. ulong ProcNum;
  144. // Object proc layout is fixed for anything that can show up here.
  145. ProcNum = *(ushort *)(pFormat+6);
  146. if ( ProcNum & 0x1 )
  147. {
  148. // An odd proc number means a Begin call (0,1,2,Begin,Finish, ...).
  149. Ret.Simple = NdrpBeginDcomAsyncClientCall( pStubDescriptor,
  150. pFormat,
  151. StartofStack );
  152. }
  153. else
  154. {
  155. Ret.Simple = NdrpFinishDcomAsyncClientCall( pStubDescriptor,
  156. pFormat,
  157. StartofStack );
  158. }
  159. return Ret;
  160. }
  161. #endif
  162. HRESULT
  163. NdrpBeginDcomAsyncClientCall(
  164. PMIDL_STUB_DESC pStubDescriptor,
  165. PFORMAT_STRING pFormat,
  166. unsigned char * StartofStack
  167. )
  168. /*
  169. Notes: OLE Refcounting.
  170. The model for async_uuid() is that async proxies or stubs
  171. are created with RefCount==1 and should never ever be
  172. addrefed by the engine.
  173. What the engine does is only the AsyncMsg clean up when done.
  174. The decision to destroy the AsyncPB or AsyncSB object is
  175. up to the client side PM or channel's SM for the server side.
  176. */
  177. {
  178. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  179. RPC_MESSAGE * pRpcMsg;
  180. MIDL_STUB_MESSAGE * pStubMsg;
  181. PFORMAT_STRING pFormatParam;
  182. uchar * pArg;
  183. void * pThis = *(void **)StartofStack;
  184. INTERPRETER_FLAGS OldOiFlags;
  185. INTERPRETER_OPT_FLAGS NewOiFlags;
  186. PPARAM_DESCRIPTION Params;
  187. long NumberParams;
  188. long n;
  189. RPC_STATUS Status;
  190. PNDR_DCOM_OI2_PROC_HEADER pProcHeader = (PNDR_DCOM_OI2_PROC_HEADER)pFormat;
  191. PNDR_PROC_HEADER_EXTS pHeaderExts = 0;
  192. CStdAsyncProxyBuffer * pAsyncPB;
  193. const IID * piid;
  194. HRESULT hr = S_OK;
  195. BOOL fSendCalled = FALSE;
  196. NDR_PROC_CONTEXT * pContext = NULL;
  197. pAsyncPB = (CStdAsyncProxyBuffer*)
  198. ((uchar*)pThis - offsetof(CStdProxyBuffer, pProxyVtbl));
  199. piid = NdrGetProxyIID( pThis );
  200. Status = NdrpSetupBeginClientCall( pAsyncPB,
  201. StartofStack,
  202. pProcHeader->StackSize,
  203. *piid );
  204. if ( !SUCCEEDED(Status) )
  205. return Status;
  206. pAsyncMsg = (PNDR_DCOM_ASYNC_MESSAGE)pAsyncPB->CallState.pAsyncMsg;
  207. // We need to switch to our copy of the stack everywhere, including pStubMsg.
  208. StartofStack = pAsyncMsg->ProcContext.StartofStack;
  209. pRpcMsg = & pAsyncMsg->RpcMsg;
  210. pStubMsg = & pAsyncMsg->StubMsg;
  211. pContext = ( NDR_PROC_CONTEXT * ) &pAsyncMsg->ProcContext;
  212. pStubMsg->pContext = pContext;
  213. pContext->StartofStack = StartofStack;
  214. pStubMsg->FullPtrXlatTables = 0;
  215. OldOiFlags = pProcHeader->OldOiFlags;
  216. NewOiFlags = pProcHeader->Oi2Flags;
  217. NumberParams = pProcHeader->NumberParams;
  218. //
  219. // Parameter descriptions are nicely spit out by MIDL.
  220. //
  221. Params = (PPARAM_DESCRIPTION)(pFormat + sizeof(NDR_DCOM_OI2_PROC_HEADER));
  222. // Proc header extentions, from NDR ver. 5.2, MIDL 5.0.+
  223. // Params must be set correctly here because of exceptions.
  224. if ( NewOiFlags.HasExtensions )
  225. {
  226. pHeaderExts = (NDR_PROC_HEADER_EXTS *)Params;
  227. Params = (PPARAM_DESCRIPTION)((uchar*)Params + pHeaderExts->Size);
  228. }
  229. pAsyncMsg->nBeginParams = pContext->NumberParams = NumberParams;
  230. pAsyncMsg->BeginParams = pContext->Params = Params;
  231. pAsyncMsg->pThis = pThis;
  232. pContext->DceTypeFormatString = pStubDescriptor->pFormatTypes;
  233. // This is OLE only code path - use a single TryExcept.
  234. // After catching it just map it to OLE exception.
  235. RpcTryExcept
  236. {
  237. ulong RpcFlags;
  238. // Note, pProcHeader->ProcNum is the async proc number.
  239. NdrProxyInitialize( pThis,
  240. pRpcMsg,
  241. pStubMsg,
  242. pStubDescriptor,
  243. (pProcHeader->ProcNum + 3)/2 // sync proc number
  244. );
  245. pStubMsg->pAsyncMsg = (struct _NDR_ASYNC_MESSAGE *) pAsyncMsg;
  246. if ( OldOiFlags.FullPtrUsed )
  247. pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit( 0, XLAT_CLIENT );
  248. // Set Rpc flags after the call to client initialize.
  249. RpcFlags= *(ulong UNALIGNED *)(pFormat + 2);
  250. pStubMsg->RpcMsg->RpcFlags = RpcFlags;
  251. // Must do this before the sizing pass!
  252. pStubMsg->StackTop = pContext->StartofStack = StartofStack;
  253. if ( NewOiFlags.HasExtensions )
  254. {
  255. pStubMsg->fHasExtensions = 1;
  256. pStubMsg->fHasNewCorrDesc = pHeaderExts->Flags2.HasNewCorrDesc;
  257. if ( pHeaderExts->Flags2.ClientCorrCheck )
  258. {
  259. void * pCache = NdrpAlloca( &pAsyncMsg->ProcContext.AllocateContext, NDR_DEFAULT_CORR_CACHE_SIZE );
  260. NdrCorrelationInitialize( pStubMsg,
  261. pCache,
  262. NDR_DEFAULT_CORR_CACHE_SIZE,
  263. 0 /* flags */ );
  264. }
  265. }
  266. //
  267. // ----------------------------------------------------------------
  268. // Sizing Pass.
  269. // ----------------------------------------------------------------
  270. //
  271. //
  272. // Get the compile time computed buffer size.
  273. //
  274. pStubMsg->BufferLength = pProcHeader->ClientBufferSize;
  275. //
  276. // Check ref pointers and do object proc [out] zeroing.
  277. //
  278. for ( n = 0; n < NumberParams; n++ )
  279. {
  280. pArg = StartofStack + Params[n].StackOffset;
  281. if ( Params[n].ParamAttr.IsSimpleRef )
  282. {
  283. // We can raise the exception here as there is no out only args.
  284. if ( ! *((uchar **)pArg) )
  285. RpcRaiseException( RPC_X_NULL_REF_POINTER );
  286. }
  287. // [out] only argument on the Begin call.
  288. if ( ! Params[n].ParamAttr.IsIn &&
  289. Params[n].ParamAttr.IsOut &&
  290. ! Params[n].ParamAttr.IsReturn)
  291. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  292. }
  293. //
  294. // Skip buffer size pass if possible.
  295. //
  296. if ( NewOiFlags.ClientMustSize )
  297. {
  298. NdrpSizing( pStubMsg,
  299. TRUE ); // IsObject
  300. }
  301. //
  302. // Do the GetBuffer.
  303. //
  304. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  305. NdrProxyGetBuffer( pThis, pStubMsg );
  306. NDR_ASSERT( pStubMsg->fBufferValid, "Invalid buffer" );
  307. pAsyncMsg->StubPhase = STUB_MARSHAL;
  308. //
  309. // ----------------------------------------------------------
  310. // Marshall Pass.
  311. // ----------------------------------------------------------
  312. //
  313. NdrpClientMarshal ( pStubMsg,
  314. TRUE ); // IsObject
  315. //
  316. // Make the RPC call.
  317. //
  318. pAsyncMsg->StubPhase = NDR_ASYNC_CALL_PHASE;
  319. fSendCalled = NdrpDcomAsyncClientSend( pStubMsg,
  320. pAsyncPB->punkOuter ); // PM's entry
  321. if ( fSendCalled )
  322. hr = S_OK;
  323. }
  324. RpcExcept( 1 )
  325. {
  326. RPC_STATUS ExceptionCode = RpcExceptionCode();
  327. pAsyncPB->CallState.Flags.BeginError = 1;
  328. // Actually dismantle the call.
  329. // This is a request call and there is nothing left at the runtime.
  330. pAsyncMsg->StubPhase = NDR_ASYNC_ERROR_PHASE;
  331. pAsyncMsg->ErrorCode = ExceptionCode;
  332. hr = NdrHrFromWin32Error(ExceptionCode);
  333. pAsyncPB->CallState.Hr = hr;
  334. // Async call in request phase: don't touch [out] params.
  335. }
  336. RpcEndExcept
  337. // "Finally"
  338. // Dont touch anything, the client has to call the Finish method anyway.
  339. pAsyncPB->CallState.Flags.BeginDone = 1;
  340. if ( SUCCEEDED(hr) )
  341. {
  342. NdrpCloneInOnlyCorrArgs( pAsyncMsg, pAsyncMsg->StubMsg.StubDesc->pFormatTypes );
  343. // Channel will prompt signal
  344. }
  345. else
  346. if (!fSendCalled )
  347. NdrpAsyncProxySignal( pAsyncPB );
  348. // No need to release, our refcount should be 1 at this point.
  349. return hr;
  350. }
  351. void
  352. NdrpCloneInOnlyCorrArgs(
  353. NDR_DCOM_ASYNC_MESSAGE * pAsyncMsg,
  354. PFORMAT_STRING pTypeFormat
  355. )
  356. /*
  357. Walk the client stack looking for an in only argument flagged to clone.
  358. For each one, replace the arg with a clone that we control.
  359. Assumption is, we do it before returning to the user from the Begin call
  360. and also we clone walking the copy of the app's stack not the app stack.
  361. The stack modified in this way will be the one to access for the weird
  362. crossreferenced correlated args.
  363. This issue doesn't happen on the server, as we keep the Begin stack around
  364. when the Finish call is processed.
  365. */
  366. {
  367. unsigned char * pBeginStack = pAsyncMsg->BeginStack;
  368. PPARAM_DESCRIPTION Params = (PPARAM_DESCRIPTION)pAsyncMsg->BeginParams;
  369. int NumberParams = (int)pAsyncMsg->nBeginParams;
  370. unsigned char * pArg;
  371. int n;
  372. for ( n = 0; n < NumberParams; n++ )
  373. {
  374. if ( Params[n].ParamAttr.SaveForAsyncFinish )
  375. {
  376. // Note that the arguments that need cloning come from top level size_is,
  377. // length_is etc, switch_is and iid_is attributes.
  378. // Hence, the only types of interest are uuid clones and integral types
  379. // different from hyper.
  380. // On top of it, we deal with stack-slot chunks of memory, so we don't
  381. // have to care about mac issues.
  382. pArg = pBeginStack + Params[n].StackOffset;
  383. if ( Params[n].ParamAttr.IsBasetype )
  384. {
  385. if ( Params[n].ParamAttr.IsSimpleRef )
  386. {
  387. void * pPointee = AsyncAlloca( pAsyncMsg, 8 );
  388. // The assignment needs to follow the type.
  389. RpcpMemoryCopy( pPointee, *(void **)pArg,
  390. SIMPLE_TYPE_MEMSIZE( Params[n].SimpleType.Type ) );
  391. *(void**)pArg = pPointee;
  392. }
  393. // else the stack slot has the simple value already.
  394. }
  395. else
  396. {
  397. // If it's not a base type, then it cannot be by value.
  398. // It has to be a pointer to a simple type or to an iid.
  399. PFORMAT_STRING pParamFormat;
  400. pParamFormat = pTypeFormat +
  401. Params[n].TypeOffset;
  402. if ( IS_BASIC_POINTER(*pParamFormat) ) // not FC_IP
  403. {
  404. if ( SIMPLE_POINTER(pParamFormat[1]) )
  405. {
  406. // Covers things like a unique pointer to a size
  407. // Essentially the same as for the simple ref above.
  408. void * pPointee = AsyncAlloca( pAsyncMsg, 8 );
  409. // The assignment needs to follow the type.
  410. RpcpMemoryCopy( pPointee, *(void **)pArg,
  411. SIMPLE_TYPE_MEMSIZE( pParamFormat[2] ) );
  412. *(void**)pArg = pPointee;
  413. }
  414. else
  415. {
  416. // has to be the riid case.
  417. // REFIID* comes out as FC_?P -> FC_?P -> FC_STRUCT
  418. PFORMAT_STRING pFormat;
  419. pFormat = pParamFormat + *(short *)(pParamFormat + 2);
  420. if ( IS_BASIC_POINTER(*pFormat) &&
  421. ! SIMPLE_POINTER(pParamFormat[1]) )
  422. {
  423. pParamFormat = pFormat + *(short *)(pFormat + 2);
  424. if ( *pParamFormat == FC_STRUCT )
  425. {
  426. // one alloc for REFIID and IID itself.
  427. IID** ppIID =
  428. (IID**)AsyncAlloca( pAsyncMsg,
  429. sizeof(IID *) + sizeof(IID));
  430. IID* pIID = (IID *)(ppIID + 1);
  431. *ppIID = pIID; //set pointer
  432. RpcpMemoryCopy( pIID, **(IID ***)pArg, sizeof(IID));
  433. *(IID ***)pArg = ppIID;
  434. }
  435. else
  436. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  437. }
  438. else
  439. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  440. }
  441. }
  442. else
  443. {
  444. // has to be the riid case.
  445. // REFIID comes out as FC_STRUCT
  446. if ( *pParamFormat == FC_STRUCT )
  447. {
  448. IID *pIID = (IID*)AsyncAlloca( pAsyncMsg, sizeof(IID) );
  449. RpcpMemoryCopy( pIID, *(IID **)pArg, sizeof(IID));
  450. *(IID **)pArg = pIID;
  451. }
  452. else
  453. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  454. }
  455. }
  456. }
  457. }
  458. }
  459. HRESULT
  460. NdrpFinishDcomAsyncClientCall(
  461. PMIDL_STUB_DESC pStubDescriptor,
  462. PFORMAT_STRING pFormat,
  463. unsigned char * StartofStack
  464. )
  465. {
  466. RPC_MESSAGE * pRpcMsg;
  467. MIDL_STUB_MESSAGE * pStubMsg;
  468. PFORMAT_STRING pFormatParam;
  469. uchar * pArg;
  470. void * pThis = *(void **)StartofStack;
  471. CLIENT_CALL_RETURN ReturnValue;
  472. INTERPRETER_FLAGS OldOiFlags; // Finish proc flags
  473. INTERPRETER_OPT_FLAGS NewOiFlags; //
  474. PPARAM_DESCRIPTION Params; //
  475. long NumberParams;
  476. long n;
  477. NDR_ASYNC_CALL_FLAGS CallFlags;
  478. PNDR_DCOM_OI2_PROC_HEADER pProcHeader = (PNDR_DCOM_OI2_PROC_HEADER)pFormat;
  479. PNDR_PROC_HEADER_EXTS pHeaderExts = 0;
  480. CStdAsyncProxyBuffer * pAsyncPB;
  481. const IID * piid;
  482. HRESULT hr = S_OK;
  483. NDR_PROC_CONTEXT * pContext = NULL;
  484. ReturnValue.Simple = 0;
  485. pAsyncPB = (CStdAsyncProxyBuffer*)
  486. ((uchar*)pThis - offsetof(CStdProxyBuffer, pProxyVtbl));
  487. piid = NdrGetProxyIID( pThis );
  488. hr = NdrpSetupFinishClientCall( pAsyncPB,
  489. StartofStack,
  490. pProcHeader->StackSize,
  491. *piid,
  492. pProcHeader->ProcNum );
  493. if ( !SUCCEEDED(hr) )
  494. return hr;
  495. // Note that we cant call to NdrProxyInitialize again.
  496. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg =
  497. (PNDR_DCOM_ASYNC_MESSAGE)pAsyncPB->CallState.pAsyncMsg;
  498. pRpcMsg = & pAsyncMsg->RpcMsg;
  499. pStubMsg = & pAsyncMsg->StubMsg;
  500. pContext = ( NDR_PROC_CONTEXT * )pStubMsg->pContext;
  501. OldOiFlags = pProcHeader->OldOiFlags;
  502. NewOiFlags = pProcHeader->Oi2Flags;
  503. NumberParams = pProcHeader->NumberParams;
  504. //
  505. // Parameter descriptions are nicely spit out by MIDL.
  506. //
  507. Params = (PPARAM_DESCRIPTION)(pFormat + sizeof(NDR_DCOM_OI2_PROC_HEADER));
  508. if ( NewOiFlags.HasExtensions )
  509. {
  510. pHeaderExts = (NDR_PROC_HEADER_EXTS *)Params;
  511. Params = (PPARAM_DESCRIPTION)((uchar*)Params + pHeaderExts->Size);
  512. }
  513. CallFlags = pAsyncMsg->Flags;
  514. // Initialize the stack top in the stub msg to be
  515. // this stack, the stack for the finish call parameters.
  516. pAsyncMsg->nFinishParams = pContext->NumberParams = NumberParams;
  517. pAsyncMsg->FinishParams = pContext->Params = Params;
  518. pStubMsg->StackTop = pContext->StartofStack = StartofStack;
  519. // OLE only code path - single RpcTryExcept.
  520. //
  521. RpcTryExcept
  522. {
  523. BOOL fRaiseExcFlag = FALSE;
  524. if ( CallFlags.ErrorPending )
  525. RpcRaiseException( pAsyncMsg->ErrorCode );
  526. // We need to zero out the [out] parameters and to check
  527. // the ref pointers.
  528. for ( n = 0; n < NumberParams; n++ )
  529. {
  530. pArg = StartofStack + Params[n].StackOffset;
  531. if ( Params[n].ParamAttr.IsSimpleRef )
  532. {
  533. // We cannot raise the exception here,
  534. // as some out args may not be zeroed out yet.
  535. if ( ! *((uchar **)pArg) )
  536. {
  537. fRaiseExcFlag = TRUE;
  538. continue;
  539. }
  540. }
  541. // We do the basetype check to cover the
  542. // [out] simple ref to basetype case.
  543. //
  544. if ( Params[n].ParamAttr.IsPartialIgnore ||
  545. ( ! Params[n].ParamAttr.IsIn &&
  546. ! Params[n].ParamAttr.IsReturn ) )
  547. {
  548. if ( Params[n].ParamAttr.IsBasetype )
  549. {
  550. // [out] only arg can only be ref, we checked that above.
  551. MIDL_memset( *(uchar **)pArg,
  552. 0,
  553. (size_t)SIMPLE_TYPE_MEMSIZE( Params[n].SimpleType.Type ));
  554. }
  555. else
  556. {
  557. pFormatParam = pStubDescriptor->pFormatTypes +
  558. Params[n].TypeOffset;
  559. NdrClientZeroOut(
  560. pStubMsg,
  561. pFormatParam,
  562. *(uchar **)pArg );
  563. }
  564. }
  565. }
  566. if ( fRaiseExcFlag )
  567. RpcRaiseException( RPC_X_NULL_REF_POINTER );
  568. NdrDcomAsyncReceive( pStubMsg );
  569. //
  570. // ----------------------------------------------------------
  571. // Unmarshall Pass.
  572. // ----------------------------------------------------------
  573. //
  574. NdrpClientUnMarshal( pStubMsg,
  575. &ReturnValue );
  576. // DCOM interface must have HRESULT as return value.
  577. hr = (HRESULT) ReturnValue.Simple;
  578. }
  579. RpcExcept( 1 )
  580. {
  581. RPC_STATUS ExceptionCode = RpcExceptionCode();
  582. //
  583. // In OLE, since they don't know about error_status_t and wanted to
  584. // reinvent the wheel, check to see if we need to map the exception.
  585. // In either case, set the return value and then try to free the
  586. // [out] params, if required.
  587. //
  588. hr = NdrHrFromWin32Error(ExceptionCode);
  589. //
  590. // Set the Buffer endpoints so the NdrFree routines work.
  591. //
  592. pStubMsg->BufferStart = 0;
  593. pStubMsg->BufferEnd = 0;
  594. for ( n = 0; n < NumberParams; n++ )
  595. {
  596. //
  597. // Skip everything but [out] only parameters. We make
  598. // the basetype check to cover [out] simple ref pointers
  599. // to basetypes.
  600. //
  601. if ( !Params[n].ParamAttr.IsPartialIgnore )
  602. {
  603. if ( Params[n].ParamAttr.IsIn ||
  604. Params[n].ParamAttr.IsReturn ||
  605. Params[n].ParamAttr.IsBasetype )
  606. continue;
  607. }
  608. pArg = StartofStack + Params[n].StackOffset;
  609. pFormatParam = pStubDescriptor->pFormatTypes +
  610. Params[n].TypeOffset;
  611. NdrClearOutParameters( pStubMsg,
  612. pFormatParam,
  613. *((uchar **)pArg) );
  614. }
  615. }
  616. RpcEndExcept
  617. // Finish
  618. // Cleanup everything. However, don't free pAsyncPB itself.
  619. NdrpAsyncProxyMsgDestructor( pAsyncPB );
  620. // Never addref or release async proxy object, this is app's/PM's job.
  621. return hr;
  622. }
  623. HRESULT RPC_ENTRY
  624. NdrDcomAsyncStubCall(
  625. struct IRpcStubBuffer * pThis,
  626. struct IRpcChannelBuffer * pChannel,
  627. PRPC_MESSAGE pRpcMsg,
  628. ulong * pdwStubPhase
  629. )
  630. /*++
  631. Routine Description :
  632. Server Interpreter entry point for DCOM async procs.
  633. This is the Begin entry for channel (regular dispatch entry from stub.c).
  634. The Finish happen when the channel calls stub's Synchronize::Signal method
  635. on the stub object. The call then comes to NdrpAsyncStubSignal later below.
  636. Arguments :
  637. pThis - Object proc's 'this' pointer.
  638. pChannel - Object proc's Channel Buffer.
  639. pRpcMsg - The RPC message.
  640. pdwStubPhase - Used to track the current interpreter's activity.
  641. Return :
  642. Status of S_OK.
  643. Notes :
  644. The engine never calls a signal on behalf of the user, regardless what kind of
  645. errors happen during begin (cannot setup begin, cannot unmarshal, app dies in invoke).
  646. In each of these cases, the engine simply returns an error code to the channel.
  647. The only time the engine would call FreeBuffer on the server is if the engine died
  648. between a successful GetBuffer and the final Send.
  649. Notes on OLE Refcounting.
  650. The model for async_uuid() is that async proxies or stubs are created
  651. with RefCount==1 and should never ever be addrefed by the engine.
  652. What the engine does is only the AsyncMsg clean up when done.
  653. The decision to destroy the AsyncPB or AsyncSB object is
  654. up to the client side PM or channel's SM for the server side.
  655. */
  656. {
  657. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  658. PMIDL_SERVER_INFO pServerInfo;
  659. PMIDL_STUB_DESC pStubDesc;
  660. const SERVER_ROUTINE * DispatchTable;
  661. unsigned long ProcNum;
  662. ushort FormatOffset;
  663. PFORMAT_STRING pFormat;
  664. PFORMAT_STRING pFormatParam;
  665. PMIDL_STUB_MESSAGE pStubMsg;
  666. uchar * pArgBuffer;
  667. uchar * pArg;
  668. uchar ** ppArg;
  669. PPARAM_DESCRIPTION Params;
  670. INTERPRETER_FLAGS OldOiFlags;
  671. INTERPRETER_OPT_FLAGS NewOiFlags;
  672. long NumberParams;
  673. BOOL fBadStubDataException = FALSE;
  674. BOOL fManagerCodeInvoked = FALSE;
  675. long n;
  676. PNDR_DCOM_OI2_PROC_HEADER pProcHeader;
  677. PNDR_PROC_HEADER_EXTS pHeaderExts = 0;
  678. CStdAsyncStubBuffer * pAsyncSB;
  679. HRESULT hr;
  680. const IID * piid = 0;
  681. BOOL fErrorInInvoke = FALSE;
  682. BOOL fRecoverableErrorInInvoke = FALSE;
  683. IUnknown * pSrvObj;
  684. CInterfaceStubVtbl * pStubVTable;
  685. NDR_PROC_CONTEXT * pContext = NULL ;
  686. RPC_STATUS ExceptionCode = 0;
  687. NDR_ASSERT( ! ((ULONG_PTR)pRpcMsg->Buffer & 0x7),
  688. "marshaling buffer misaligned at server" );
  689. // The channel dispatches to the engine with the sync proc num.
  690. // We need only async proc num at the engine level.
  691. ProcNum = pRpcMsg->ProcNum;
  692. ProcNum = 2 * ProcNum - 3; // Begin method #
  693. pSrvObj = (IUnknown *)((CStdStubBuffer *)pThis)->pvServerObject;
  694. DispatchTable = (SERVER_ROUTINE *)pSrvObj->lpVtbl;
  695. pStubVTable = (CInterfaceStubVtbl *)
  696. (*((uchar **)pThis) - sizeof(CInterfaceStubHeader));
  697. piid = pStubVTable->header.piid;
  698. pServerInfo = (PMIDL_SERVER_INFO) pStubVTable->header.pServerInfo;
  699. pStubDesc = pServerInfo->pStubDesc;
  700. FormatOffset = pServerInfo->FmtStringOffset[ ProcNum ];
  701. pFormat = &((pServerInfo->ProcString)[FormatOffset]);
  702. // The proc header has a fixed layout now.
  703. pProcHeader = (PNDR_DCOM_OI2_PROC_HEADER) pFormat;
  704. pAsyncSB = (CStdAsyncStubBuffer *)
  705. ((uchar *)pThis - offsetof(CStdAsyncStubBuffer,lpVtbl));
  706. hr = NdrpSetupBeginStubCall( pAsyncSB,
  707. pProcHeader->StackSize,
  708. *piid );
  709. if ( FAILED(hr) )
  710. return hr;
  711. pAsyncMsg = (PNDR_DCOM_ASYNC_MESSAGE)pAsyncSB->CallState.pAsyncMsg;
  712. pStubMsg = & pAsyncMsg->StubMsg;
  713. pContext = &pAsyncMsg->ProcContext;
  714. // Both rpc runtime and channel require that we use a copy of the rpc message.
  715. RpcpMemoryCopy( & pAsyncMsg->RpcMsg, pRpcMsg, sizeof(RPC_MESSAGE) );
  716. pRpcMsg = & pAsyncMsg->RpcMsg;
  717. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  718. // The arg buffer is zeroed out already.
  719. pArgBuffer = pAsyncMsg->ProcContext.StartofStack;
  720. //
  721. // Get new interpreter info.
  722. //
  723. OldOiFlags = pProcHeader->OldOiFlags;
  724. NewOiFlags = pProcHeader->Oi2Flags;
  725. NumberParams = pProcHeader->NumberParams;
  726. Params = (PPARAM_DESCRIPTION)(pFormat + sizeof(NDR_DCOM_OI2_PROC_HEADER));
  727. if ( NewOiFlags.HasExtensions )
  728. {
  729. pHeaderExts = (NDR_PROC_HEADER_EXTS *)Params;
  730. Params = (PPARAM_DESCRIPTION)((uchar*)Params + pHeaderExts->Size);
  731. }
  732. pAsyncMsg->nBeginParams = pContext->NumberParams = NumberParams;
  733. pAsyncMsg->BeginParams = pContext->Params = Params;
  734. pAsyncMsg->pThis = pThis;
  735. pContext->DceTypeFormatString = pStubDesc->pFormatTypes;
  736. //
  737. // Wrap the unmarshalling and the invoke call in the try block of
  738. // a try-finally. Put the free phase in the associated finally block.
  739. //
  740. // We abstract the level of indirection here.
  741. RpcTryFinally
  742. {
  743. // OLE: put pThis in first dword of stack.
  744. //
  745. ((void **)pArgBuffer)[0] = ((CStdStubBuffer *)pThis)->pvServerObject;
  746. // Initialize the Stub message.
  747. //
  748. NdrStubInitialize( pRpcMsg,
  749. pStubMsg,
  750. pStubDesc,
  751. pChannel );
  752. pStubMsg->pAsyncMsg = (struct _NDR_ASYNC_MESSAGE *) pAsyncMsg;
  753. pAsyncMsg->pdwStubPhase = pdwStubPhase; // the phase is STUB_UNMARSHAL
  754. // Raise exceptions after initializing the stub.
  755. if ( OldOiFlags.FullPtrUsed )
  756. pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit( 0, XLAT_SERVER );
  757. else
  758. pStubMsg->FullPtrXlatTables = 0;
  759. //
  760. // Set StackTop AFTER the initialize call, since it zeros the field
  761. // out.
  762. //
  763. pStubMsg->StackTop = pArgBuffer;
  764. if ( NewOiFlags.HasExtensions )
  765. {
  766. pStubMsg->fHasExtensions = 1;
  767. pStubMsg->fHasNewCorrDesc = pHeaderExts->Flags2.HasNewCorrDesc;
  768. if ( pHeaderExts->Flags2.ServerCorrCheck )
  769. {
  770. void * pCache = NdrpAlloca( &pAsyncMsg->ProcContext.AllocateContext, NDR_DEFAULT_CORR_CACHE_SIZE );
  771. NdrCorrelationInitialize( pStubMsg,
  772. pCache,
  773. NDR_DEFAULT_CORR_CACHE_SIZE,
  774. 0 /* flags */ );
  775. }
  776. }
  777. // StubPhase set up by invoke is STUB_UNMARSHAL
  778. RpcTryExcept
  779. {
  780. // --------------------------------
  781. // Unmarshall all of our parameters.
  782. // --------------------------------
  783. NdrpServerUnMarshal( pStubMsg );
  784. }
  785. // Last ditch checks.
  786. if ( pRpcMsg->BufferLength <
  787. (uint)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer) )
  788. {
  789. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  790. }
  791. RpcExcept( NdrServerUnmarshallExceptionFlag(GetExceptionInformation()) )
  792. {
  793. ExceptionCode = RpcExceptionCode();
  794. // Filter set in rpcndr.h to catch one of the following
  795. // STATUS_ACCESS_VIOLATION
  796. // STATUS_DATATYPE_MISALIGNMENT
  797. // RPC_X_BAD_STUB_DATA
  798. fBadStubDataException = TRUE;
  799. pAsyncMsg->Flags.BadStubData = 1;
  800. pAsyncMsg->Flags.ErrorPending = 1;
  801. if ( RPC_BAD_STUB_DATA_EXCEPTION_FILTER )
  802. {
  803. ExceptionCode = RPC_X_BAD_STUB_DATA;
  804. }
  805. pAsyncMsg->ErrorCode = ExceptionCode;
  806. NdrpFreeMemoryList( pStubMsg );
  807. pAsyncSB->CallState.Flags.BeginError = 1;
  808. pAsyncSB->CallState.Hr = NdrHrFromWin32Error( ExceptionCode);
  809. RpcRaiseException( ExceptionCode );
  810. }
  811. RpcEndExcept
  812. //
  813. // Do [out] initialization before the invoke.
  814. //
  815. for ( n = 0; n < NumberParams; n++ )
  816. {
  817. if ( Params[n].ParamAttr.IsIn ||
  818. Params[n].ParamAttr.IsReturn )
  819. continue;
  820. // This is a Begin call, there cannot be any [out] only args.
  821. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  822. }
  823. //
  824. // OLE interfaces use pdwStubPhase in the exception filter.
  825. // See CStdStubBuffer_Invoke in stub.c.
  826. //
  827. *pdwStubPhase = STUB_CALL_SERVER;
  828. // We need to catch exception in the manager code separately
  829. // as the model implies that there will be no other call from
  830. // the server app to clean up.
  831. pAsyncSB->CallState.Flags.BeginDone = 1;
  832. RpcTryExcept
  833. {
  834. //
  835. // Check for a thunk. Compiler does all the setup for us.
  836. //
  837. if ( pServerInfo->ThunkTable && pServerInfo->ThunkTable[ ProcNum ] )
  838. {
  839. fManagerCodeInvoked = TRUE;
  840. pServerInfo->ThunkTable[ ProcNum ]( pStubMsg );
  841. }
  842. else
  843. {
  844. //
  845. // Note that this ArgNum is not the number of arguments declared
  846. // in the function we called, but really the number of
  847. // REGISTER_TYPEs occupied by the arguments to a function.
  848. //
  849. long ArgNum;
  850. MANAGER_FUNCTION pFunc;
  851. REGISTER_TYPE ReturnValue;
  852. pFunc = (MANAGER_FUNCTION) DispatchTable[ ProcNum ];
  853. ArgNum = (long)pProcHeader->StackSize / sizeof(REGISTER_TYPE);
  854. //
  855. // The StackSize includes the size of the return. If we want
  856. // just the number of REGISTER_TYPES, then ArgNum must be reduced
  857. // by 1 when there is a return value AND the current ArgNum count
  858. // is greater than 0.
  859. //
  860. if ( ArgNum && NewOiFlags.HasReturn )
  861. ArgNum--;
  862. // Being here means that we can expect results. Note that the user
  863. // can call RpcCompleteCall from inside of the manager code.
  864. fManagerCodeInvoked = TRUE;
  865. ReturnValue = Invoke( pFunc,
  866. (REGISTER_TYPE *)pArgBuffer,
  867. #if defined(_IA64_)
  868. NewOiFlags.HasExtensions ? ((PNDR_PROC_HEADER_EXTS64)pHeaderExts)->FloatArgMask
  869. : 0,
  870. #endif
  871. ArgNum);
  872. if ( NewOiFlags.HasReturn )
  873. {
  874. // Pass the app's HR from Begin call to the channel.
  875. (*pfnDcomChannelSetHResult)( pRpcMsg,
  876. NULL, // reserved
  877. (HRESULT) ReturnValue);
  878. }
  879. // We are discarding the return value as it is not the real one.
  880. }
  881. }
  882. RpcExcept( 1 )
  883. {
  884. fErrorInInvoke = TRUE;
  885. pAsyncMsg->Flags.ErrorPending = 1;
  886. pAsyncMsg->ErrorCode = RpcExceptionCode();
  887. pAsyncSB->CallState.Flags.BeginError = 1;
  888. pAsyncSB->CallState.Hr = NdrHrFromWin32Error( RpcExceptionCode());
  889. }
  890. RpcEndExcept
  891. // Done with invoking Begin
  892. }
  893. RpcFinally
  894. {
  895. if ( !fManagerCodeInvoked )
  896. {
  897. // Failed without invoking Begin - return an error. Remember the error.
  898. if ( fBadStubDataException )
  899. pAsyncMsg->ErrorCode = RPC_X_BAD_STUB_DATA;
  900. pAsyncSB->CallState.Flags.BeginDone = 1;
  901. hr = pAsyncSB->CallState.Hr;
  902. }
  903. else // fManagerCodeInvoked
  904. {
  905. hr = S_OK;
  906. if ( fErrorInInvoke )
  907. hr = pAsyncSB->CallState.Hr;
  908. }
  909. }
  910. RpcEndFinally
  911. return hr;
  912. }
  913. void
  914. NdrpCloneInOutStubArgs(
  915. NDR_DCOM_ASYNC_MESSAGE * pAsyncMsg )
  916. /*
  917. Walk the second stack looking for an in-out argument.
  918. For each one, find the corresponding in-out atgument from the first stack
  919. and clone it to the second stack.
  920. Note, we need to do it only on the server side where we preserver the first
  921. stack, the dispatch buffer and all the arguments from the first stack.
  922. On the client, this is the app's task to supply meaningful in-out arguments
  923. for the second stack.
  924. */
  925. {
  926. REGISTER_TYPE * pBeginStack = (REGISTER_TYPE *)pAsyncMsg->BeginStack;
  927. REGISTER_TYPE * pFinishStack = (REGISTER_TYPE *)pAsyncMsg->FinishStack;
  928. PPARAM_DESCRIPTION BeginParams = (PPARAM_DESCRIPTION)pAsyncMsg->BeginParams;
  929. int BeginNumberParams = (int)pAsyncMsg->nBeginParams;
  930. PPARAM_DESCRIPTION FinishParams = (PPARAM_DESCRIPTION)pAsyncMsg->FinishParams;
  931. int FinishNumberParams = pAsyncMsg->nFinishParams;
  932. int FirstIO = 0;
  933. int n;
  934. for ( n = 0; n < FinishNumberParams; n++ )
  935. {
  936. // Find in-out arg that needs cloning.
  937. if ( FinishParams[n].ParamAttr.IsIn &&
  938. FinishParams[n].ParamAttr.IsOut )
  939. {
  940. // Find the first IO on the first stack
  941. while ( FirstIO < BeginNumberParams )
  942. {
  943. if ( BeginParams[ FirstIO ].ParamAttr.IsIn &&
  944. BeginParams[ FirstIO ].ParamAttr.IsOut )
  945. {
  946. break;
  947. }
  948. FirstIO++;
  949. }
  950. if ( BeginNumberParams <= FirstIO )
  951. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  952. // Clone it to the second stack
  953. pFinishStack[ FinishParams[n].StackOffset / sizeof(REGISTER_TYPE) ] =
  954. pBeginStack[ BeginParams[ FirstIO ].StackOffset / sizeof(REGISTER_TYPE) ];
  955. FirstIO++;
  956. }
  957. }
  958. }
  959. HRESULT
  960. NdrpCompleteDcomAsyncStubCall(
  961. CStdAsyncStubBuffer * pAsyncSB
  962. )
  963. /*++
  964. Routine Description :
  965. Complete an async call on the server side.
  966. Arguments :
  967. AsyncHandle - raw or object handle (if pointer) as appropriate,
  968. pAsyncMsg - pointer to async msg structure,
  969. pReturnValue - from the user to pass back to caller.
  970. Return :
  971. Status of S_OK.
  972. --*/
  973. {
  974. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  975. PMIDL_SERVER_INFO pServerInfo;
  976. PMIDL_STUB_DESC pStubDesc; // should be the same
  977. const SERVER_ROUTINE * DispatchTable; // should be the same
  978. unsigned long ProcNum; // should be 1+
  979. ushort FormatOffset;
  980. PFORMAT_STRING pFormat;
  981. PFORMAT_STRING pFormatParam;
  982. RPC_MESSAGE * pRpcMsg;
  983. MIDL_STUB_MESSAGE * pStubMsg;
  984. INTERPRETER_FLAGS OldOiFlags; // Finish flags
  985. INTERPRETER_OPT_FLAGS NewOiFlags; // Finish flags
  986. PPARAM_DESCRIPTION Params; // Finish params
  987. uchar * pArgBuffer; // new stack
  988. // MZ, BUG BUG, Fix after ship
  989. // ulong * pdwStubPhase;
  990. uchar * pArg;
  991. long NumberParams;
  992. long n;
  993. PNDR_DCOM_OI2_PROC_HEADER pProcHeader;
  994. PNDR_PROC_HEADER_EXTS pHeaderExts = 0;
  995. IUnknown * pSrvObj;
  996. CInterfaceStubVtbl * pStubVTable;
  997. void * pThis;
  998. HRESULT hr;
  999. const IID * piid; // should be the same
  1000. BOOL fManagerCodeInvoked = FALSE;
  1001. BOOL fErrorInInvoke = FALSE;
  1002. RPC_STATUS ExceptionCode = 0;
  1003. boolean fParamsFreed = FALSE;
  1004. NDR_PROC_CONTEXT * pContext = NULL;
  1005. // We validated both the stub and the async context in the signal call.
  1006. // We validated the pAsyncSB in the Signal call.
  1007. // Do additional checks.
  1008. pAsyncMsg = (PNDR_DCOM_ASYNC_MESSAGE)pAsyncSB->CallState.pAsyncMsg;
  1009. pThis = pAsyncMsg->pThis;
  1010. // See if channel calls on the right stub
  1011. if ( & pAsyncSB->lpVtbl != pThis)
  1012. return E_INVALIDARG;
  1013. pRpcMsg = & pAsyncMsg->RpcMsg;
  1014. pStubMsg = & pAsyncMsg->StubMsg;
  1015. // We have preserved the sync proc num that the channel used.
  1016. // We need only async proc num at the engine level.
  1017. //
  1018. ProcNum = pRpcMsg->ProcNum;
  1019. ProcNum = 2 * ProcNum - 3 + 1; // Finish method #
  1020. pSrvObj = (IUnknown *)((CStdStubBuffer *)pThis)->pvServerObject;
  1021. DispatchTable = (SERVER_ROUTINE *)pSrvObj->lpVtbl;
  1022. pStubVTable = (CInterfaceStubVtbl *)
  1023. (*((uchar **)pThis) - sizeof(CInterfaceStubHeader));
  1024. piid = pStubVTable->header.piid;
  1025. pServerInfo = (PMIDL_SERVER_INFO) pStubVTable->header.pServerInfo;
  1026. pStubDesc = pServerInfo->pStubDesc;
  1027. FormatOffset = pServerInfo->FmtStringOffset[ ProcNum ];
  1028. pFormat = &((pServerInfo->ProcString)[ FormatOffset ]);
  1029. // The proc header has a fixed layout now.
  1030. pProcHeader = (PNDR_DCOM_OI2_PROC_HEADER) pFormat;
  1031. // Validate and setup for finish.
  1032. hr = NdrpSetupFinishStubCall( pAsyncSB,
  1033. pProcHeader->StackSize,
  1034. *piid );
  1035. if ( hr )
  1036. return hr;
  1037. // The arg buffer is zeroed out already. Note, this is the second stack.
  1038. pContext = &pAsyncMsg->ProcContext;
  1039. pArgBuffer = pContext->StartofStack;
  1040. pStubMsg->StackTop = pArgBuffer;
  1041. //
  1042. // Get new interpreter info.
  1043. //
  1044. OldOiFlags = pProcHeader->OldOiFlags;
  1045. NewOiFlags = pProcHeader->Oi2Flags;
  1046. NumberParams = pProcHeader->NumberParams;
  1047. Params = (PPARAM_DESCRIPTION)(pFormat + sizeof(NDR_DCOM_OI2_PROC_HEADER));
  1048. if ( NewOiFlags.HasExtensions )
  1049. {
  1050. pHeaderExts = (NDR_PROC_HEADER_EXTS *)Params;
  1051. Params = (PPARAM_DESCRIPTION)((uchar*)Params + pHeaderExts->Size);
  1052. }
  1053. pAsyncMsg->nFinishParams = pContext->NumberParams = NumberParams;
  1054. pAsyncMsg->FinishParams = pContext->Params = Params;
  1055. pContext->DceTypeFormatString = pStubDesc->pFormatTypes;
  1056. pStubMsg->pContext = pContext;
  1057. // Wrap the unmarshalling, mgr call and marshalling in the try block of
  1058. // a try-finally. Put the free phase in the associated finally block.
  1059. //
  1060. RpcTryFinally
  1061. {
  1062. if ( pAsyncMsg->Flags.ErrorPending )
  1063. RpcRaiseException( pAsyncMsg->ErrorCode );
  1064. // Initialize the args of the new stack.
  1065. // OLE: put pThis in first dword of stack.
  1066. //
  1067. ((void **)pArgBuffer)[0] = ((CStdStubBuffer *)pThis)->pvServerObject;
  1068. //
  1069. // Do [out] initialization before invoking Finish
  1070. //
  1071. NdrpCloneInOutStubArgs( pAsyncMsg );
  1072. NdrpServerOutInit( pStubMsg );
  1073. //
  1074. // OLE interfaces use pdwStubPhase in the exception filter.
  1075. // See CStdStubBuffer_Invoke in stub.c.
  1076. //
  1077. // MZ, BUG BUG, fix after ship
  1078. // *pdwStubPhase = STUB_CALL_SERVER;
  1079. // We need to catch exception in the manager code separately
  1080. // as the model implies that there will be no other call from
  1081. // the server app to clean up.
  1082. RpcTryExcept
  1083. {
  1084. //
  1085. // Check for a thunk. Compiler does all the setup for us.
  1086. //
  1087. if ( pServerInfo->ThunkTable && pServerInfo->ThunkTable[ProcNum] )
  1088. {
  1089. fManagerCodeInvoked = TRUE;
  1090. pServerInfo->ThunkTable[ProcNum]( pStubMsg );
  1091. }
  1092. else
  1093. {
  1094. //
  1095. // Note that this ArgNum is not the number of arguments declared
  1096. // in the function we called, but really the number of
  1097. // REGISTER_TYPEs occupied by the arguments to a function.
  1098. //
  1099. long ArgNum;
  1100. MANAGER_FUNCTION pFunc;
  1101. REGISTER_TYPE ReturnValue;
  1102. pFunc = (MANAGER_FUNCTION) DispatchTable[ProcNum];
  1103. ArgNum = (long)pProcHeader->StackSize / sizeof(REGISTER_TYPE);
  1104. //
  1105. // The StackSize includes the size of the return. If we want
  1106. // just the number of REGISTER_TYPES, then ArgNum must be reduced
  1107. // by 1 when there is a return value AND the current ArgNum count
  1108. // is greater than 0.
  1109. //
  1110. if ( ArgNum && NewOiFlags.HasReturn )
  1111. ArgNum--;
  1112. fManagerCodeInvoked = TRUE;
  1113. ReturnValue = Invoke( pFunc,
  1114. (REGISTER_TYPE *)pArgBuffer,
  1115. #if defined(_IA64_)
  1116. NewOiFlags.HasExtensions ? ((PNDR_PROC_HEADER_EXTS64)pHeaderExts)->FloatArgMask
  1117. : 0,
  1118. #endif
  1119. ArgNum);
  1120. // This is the return value that should be marshaled back.
  1121. if ( NewOiFlags.HasReturn )
  1122. {
  1123. ((REGISTER_TYPE *)pArgBuffer)[ArgNum] = ReturnValue;
  1124. // Pass the app's HR to the channel.
  1125. (*pfnDcomChannelSetHResult)( pRpcMsg,
  1126. NULL, // reserved
  1127. (HRESULT) ReturnValue);
  1128. }
  1129. }
  1130. }
  1131. RpcExcept( 1 )
  1132. {
  1133. pAsyncMsg->Flags.ErrorPending = 1;
  1134. pAsyncMsg->ErrorCode = RpcExceptionCode();
  1135. fErrorInInvoke = TRUE;
  1136. }
  1137. RpcEndExcept
  1138. // Done with invoking Finish
  1139. if ( pAsyncMsg->Flags.ErrorPending )
  1140. RpcRaiseException( pAsyncMsg->ErrorCode );
  1141. //
  1142. // Buffer size pass.
  1143. //
  1144. pStubMsg->BufferLength = pProcHeader->ServerBufferSize;
  1145. if ( NewOiFlags.ServerMustSize )
  1146. {
  1147. NdrpSizing( pStubMsg,
  1148. FALSE ); // IsClient
  1149. }
  1150. // Get buffer.
  1151. NdrStubGetBuffer( (IRpcStubBuffer*)pAsyncMsg->pThis,
  1152. pStubMsg->pRpcChannelBuffer,
  1153. pStubMsg );
  1154. //
  1155. // Marshall pass.
  1156. //
  1157. NdrpServerMarshal( pStubMsg,
  1158. TRUE ); // IsObject
  1159. if ( pRpcMsg->BufferLength <
  1160. (uint)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer) )
  1161. {
  1162. NDR_ASSERT( 0, "NdrStubCall2 marshal: buffer overflow!" );
  1163. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  1164. }
  1165. pRpcMsg->BufferLength = (ulong)(pStubMsg->Buffer - (uchar *)pRpcMsg->Buffer);
  1166. // We don't drop to the runtime like for synchronous calls,
  1167. // we send the last buffer explicitly.
  1168. fParamsFreed = TRUE;
  1169. // see comment on async.cxx on why we call this twice.
  1170. NdrpAsyncDCOMFreeParams( pAsyncMsg );
  1171. NdrpDcomAsyncSend( pStubMsg,
  1172. 0 ); // server doesn't pass pSynchronize back to channel.
  1173. }
  1174. RpcFinally
  1175. {
  1176. // Don't free parameters if we died because of bad stub data in unmarshaling.
  1177. if ( ! pAsyncMsg->Flags.BadStubData && !fParamsFreed)
  1178. {
  1179. NdrpAsyncDCOMFreeParams( pAsyncMsg );
  1180. }
  1181. if ( pAsyncMsg->Flags.ErrorPending )
  1182. hr = NdrHrFromWin32Error( pAsyncMsg->ErrorCode );
  1183. else
  1184. hr = S_OK;
  1185. // If we are here, error or not, it means that we can (and need to) dispose of
  1186. // the async context information
  1187. NdrpAsyncStubMsgDestructor( pAsyncSB );
  1188. // The engine never addrefs or releases the call object.
  1189. }
  1190. RpcEndFinally
  1191. return hr;
  1192. }
  1193. HRESULT
  1194. NdrpAsyncProxyMsgConstructor(
  1195. CStdAsyncProxyBuffer * pAsyncPB )
  1196. {
  1197. NdrDcomAsyncCallState * pCallState = & pAsyncPB->CallState;
  1198. pCallState->Lock = 0;
  1199. pCallState->Signature = NDR_ASYNC_PROXY_SIGNATURE;
  1200. pCallState->pAsyncMsg = 0;
  1201. *((long*)&pCallState->Flags) = 0;
  1202. pCallState->Hr = 0;
  1203. return S_OK;
  1204. }
  1205. HRESULT
  1206. NdrpAsyncStubMsgConstructor(
  1207. CStdAsyncStubBuffer * pAsyncSB )
  1208. {
  1209. NdrDcomAsyncCallState * pCallState = & pAsyncSB->CallState;
  1210. pCallState->Lock = 0;
  1211. pCallState->Signature = NDR_ASYNC_STUB_SIGNATURE;
  1212. pCallState->pAsyncMsg = 0;
  1213. *((long*)&pCallState->Flags) = 0;
  1214. pCallState->Hr = 0;
  1215. return S_OK;
  1216. }
  1217. HRESULT
  1218. NdrpAsyncProxyMsgDestructor(
  1219. CStdAsyncProxyBuffer * pAsyncPB )
  1220. {
  1221. NdrDcomAsyncCallState * pCallState = & pAsyncPB->CallState;
  1222. if ( pCallState->pAsyncMsg )
  1223. {
  1224. NdrpFreeDcomAsyncMsg( (PNDR_DCOM_ASYNC_MESSAGE)pCallState->pAsyncMsg );
  1225. pCallState->pAsyncMsg = 0;
  1226. }
  1227. *((long*)&pCallState->Flags) = 0;
  1228. pCallState->Hr = 0;
  1229. return S_OK;
  1230. }
  1231. HRESULT
  1232. NdrpAsyncStubMsgDestructor(
  1233. CStdAsyncStubBuffer * pAsyncSB )
  1234. {
  1235. NdrDcomAsyncCallState * pCallState = & pAsyncSB->CallState;
  1236. if ( pCallState->pAsyncMsg )
  1237. {
  1238. NdrpFreeDcomAsyncMsg( (PNDR_DCOM_ASYNC_MESSAGE)pCallState->pAsyncMsg );
  1239. pCallState->pAsyncMsg = 0;
  1240. }
  1241. *((long*)&pCallState->Flags) = 0;
  1242. pCallState->Hr = 0;
  1243. return S_OK;
  1244. }
  1245. HRESULT
  1246. NdrpValidateAsyncProxyCall(
  1247. CStdAsyncProxyBuffer * pAsyncPB
  1248. )
  1249. {
  1250. HRESULT hr = S_OK;
  1251. RpcTryExcept
  1252. {
  1253. NdrDcomAsyncCallState * pCallState = & pAsyncPB->CallState;
  1254. if ( pCallState->Signature != NDR_ASYNC_PROXY_SIGNATURE )
  1255. hr = E_INVALIDARG;
  1256. }
  1257. RpcExcept(1)
  1258. {
  1259. hr = E_INVALIDARG;
  1260. }
  1261. RpcEndExcept;
  1262. return hr;
  1263. }
  1264. HRESULT
  1265. NdrpValidateAsyncStubCall(
  1266. CStdAsyncStubBuffer * pAsyncSB
  1267. )
  1268. {
  1269. HRESULT hr = S_OK;
  1270. RpcTryExcept
  1271. {
  1272. NdrDcomAsyncCallState * pCallState = & pAsyncSB->CallState;
  1273. if ( pCallState->Signature != NDR_ASYNC_STUB_SIGNATURE )
  1274. hr = E_INVALIDARG;
  1275. }
  1276. RpcExcept(1)
  1277. {
  1278. hr = E_INVALIDARG;
  1279. }
  1280. RpcEndExcept;
  1281. return hr;
  1282. }
  1283. HRESULT
  1284. NdrpValidateDcomAsyncMsg(
  1285. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg )
  1286. {
  1287. HRESULT hr = RPC_S_OK;
  1288. RpcTryExcept
  1289. {
  1290. if ( pAsyncMsg->Signature != NDR_DCOM_ASYNC_SIGNATURE ||
  1291. pAsyncMsg->Version != NDR_DCOM_ASYNC_VERSION )
  1292. {
  1293. hr = E_INVALIDARG;
  1294. }
  1295. }
  1296. RpcExcept(1)
  1297. {
  1298. hr = E_INVALIDARG;
  1299. }
  1300. RpcEndExcept;
  1301. return hr;
  1302. }
  1303. HRESULT
  1304. NdrpSetupBeginClientCall(
  1305. CStdAsyncProxyBuffer * pAsyncPB,
  1306. void * StartofStack,
  1307. unsigned short StackSize,
  1308. REFIID riid )
  1309. /*
  1310. This method creates and initializes async msg.
  1311. */
  1312. {
  1313. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1314. HRESULT hr = S_OK;
  1315. hr = NdrpValidateAsyncProxyCall( pAsyncPB );
  1316. if ( ! SUCCEEDED(hr) )
  1317. return hr;
  1318. if ( pAsyncPB->CallState.pAsyncMsg != 0 ||
  1319. pAsyncPB->CallState.Flags.BeginStarted )
  1320. return E_FAIL;
  1321. // Do this first to simplify error conditions.
  1322. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)
  1323. I_RpcBCacheAllocate( sizeof(NDR_DCOM_ASYNC_MESSAGE) +
  1324. StackSize + NDR_ASYNC_GUARD_SIZE );
  1325. if ( ! pAsyncMsg )
  1326. {
  1327. NdrpAsyncProxySignal( pAsyncPB );
  1328. return E_OUTOFMEMORY;
  1329. }
  1330. // Initialize the async message properly
  1331. MIDL_memset( pAsyncMsg, 0x0, sizeof( NDR_DCOM_ASYNC_MESSAGE) );
  1332. pAsyncMsg->Signature = NDR_DCOM_ASYNC_SIGNATURE;
  1333. pAsyncMsg->Version = NDR_DCOM_ASYNC_VERSION;
  1334. pAsyncMsg->SyntaxType = XFER_SYNTAX_DCE;
  1335. pAsyncMsg->ProcContext.StartofStack = (uchar *) & pAsyncMsg->AppStack;
  1336. pAsyncMsg->BeginStack = (uchar *) & pAsyncMsg->AppStack;
  1337. pAsyncMsg->BeginStackSize = StackSize;
  1338. pAsyncMsg->StubPhase = NDR_ASYNC_PREP_PHASE;
  1339. NdrpAllocaInit( &pAsyncMsg->ProcContext.AllocateContext );
  1340. // Client: copy stack from the app's request call.
  1341. RpcpMemoryCopy( & pAsyncMsg->AppStack, StartofStack, StackSize );
  1342. MIDL_memset( ((char *)& pAsyncMsg->AppStack) + StackSize,
  1343. 0x71,
  1344. NDR_ASYNC_GUARD_SIZE );
  1345. pAsyncMsg->pAsyncPB = pAsyncPB;
  1346. pAsyncPB->CallState.Flags.BeginStarted = 1;
  1347. pAsyncPB->CallState.pAsyncMsg = pAsyncMsg;
  1348. return S_OK;
  1349. }
  1350. HRESULT
  1351. NdrpSetupFinishClientCall(
  1352. CStdAsyncProxyBuffer * pAsyncPB,
  1353. void * StartofStack,
  1354. unsigned short StackSize,
  1355. REFIID riid,
  1356. unsigned long FinishProcNum )
  1357. /*
  1358. This method creates and initializes async msg.
  1359. */
  1360. {
  1361. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1362. HRESULT hr = S_OK;
  1363. hr = NdrpValidateAsyncProxyCall( pAsyncPB );
  1364. if ( ! SUCCEEDED(hr) )
  1365. return hr;
  1366. if ( !pAsyncPB->CallState.Flags.BeginStarted ||
  1367. !pAsyncPB->CallState.Flags.BeginDone ||
  1368. pAsyncPB->CallState.Flags.FinishStarted )
  1369. return E_FAIL;
  1370. pAsyncMsg = (PNDR_DCOM_ASYNC_MESSAGE)pAsyncPB->CallState.pAsyncMsg;
  1371. hr = NdrpValidateDcomAsyncMsg( pAsyncMsg );
  1372. if ( ! SUCCEEDED(hr) )
  1373. return hr;
  1374. if ( (FinishProcNum + 3)/2 != (pAsyncMsg->RpcMsg.ProcNum & 0x7fff) )
  1375. return E_FAIL;
  1376. // Initialize the async message properly
  1377. pAsyncMsg->ProcContext.StartofStack = (uchar *) StartofStack;
  1378. pAsyncMsg->FinishStack = (uchar *) StartofStack;
  1379. pAsyncMsg->FinishStackSize = StackSize;
  1380. pAsyncMsg->StubPhase = NDR_ASYNC_PREP_PHASE;
  1381. // Dont allocate or copy the new stack anywhere.
  1382. pAsyncPB->CallState.Flags.FinishStarted = 1;
  1383. return S_OK;
  1384. }
  1385. HRESULT
  1386. NdrpSetupBeginStubCall(
  1387. CStdAsyncStubBuffer * pAsyncSB,
  1388. unsigned short StackSize,
  1389. REFIID riid )
  1390. /*
  1391. This method creates and initializes async msg.
  1392. */
  1393. {
  1394. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1395. HRESULT hr = S_OK;
  1396. hr = NdrpValidateAsyncStubCall( pAsyncSB );
  1397. if ( ! SUCCEEDED(hr) )
  1398. return hr;
  1399. if ( pAsyncSB->CallState.pAsyncMsg != 0 ||
  1400. pAsyncSB->CallState.Flags.BeginStarted )
  1401. hr = E_FAIL;
  1402. else
  1403. {
  1404. pAsyncMsg = (NDR_DCOM_ASYNC_MESSAGE*)
  1405. I_RpcBCacheAllocate( sizeof( NDR_DCOM_ASYNC_MESSAGE) +
  1406. StackSize + NDR_ASYNC_GUARD_SIZE );
  1407. if ( ! pAsyncMsg )
  1408. hr = E_OUTOFMEMORY;
  1409. }
  1410. if ( ! SUCCEEDED(hr) )
  1411. {
  1412. // The stub never signals.
  1413. pAsyncSB->CallState.Flags.BeginError = 1;
  1414. pAsyncSB->CallState.Hr = hr;
  1415. return hr;
  1416. }
  1417. // Initialize the async message properly
  1418. MIDL_memset( pAsyncMsg, 0x0, sizeof( NDR_DCOM_ASYNC_MESSAGE) );
  1419. pAsyncMsg->Signature = NDR_DCOM_ASYNC_SIGNATURE;
  1420. pAsyncMsg->Version = NDR_DCOM_ASYNC_VERSION;
  1421. pAsyncMsg->SyntaxType = XFER_SYNTAX_DCE;
  1422. pAsyncMsg->ProcContext.StartofStack = (uchar *) & pAsyncMsg->AppStack;
  1423. pAsyncMsg->BeginStack = (uchar *) & pAsyncMsg->AppStack;
  1424. pAsyncMsg->BeginStackSize = StackSize;
  1425. pAsyncMsg->StubPhase = STUB_UNMARSHAL;
  1426. pAsyncMsg->StubMsg.pContext = &pAsyncMsg->ProcContext;
  1427. NdrpAllocaInit( &pAsyncMsg->ProcContext.AllocateContext );
  1428. // Server: zero out stack for allocs.
  1429. MIDL_memset( & pAsyncMsg->AppStack, 0x0, StackSize );
  1430. MIDL_memset( ((char *)& pAsyncMsg->AppStack) + StackSize,
  1431. 0x71,
  1432. NDR_ASYNC_GUARD_SIZE );
  1433. pAsyncSB->CallState.pAsyncMsg = pAsyncMsg;
  1434. pAsyncSB->CallState.Flags.BeginStarted = 1;
  1435. pAsyncMsg->pAsyncSB = pAsyncSB;
  1436. return S_OK;
  1437. }
  1438. HRESULT
  1439. NdrpSetupFinishStubCall(
  1440. CStdAsyncStubBuffer * pAsyncSB,
  1441. unsigned short StackSize,
  1442. REFIID riid )
  1443. /*
  1444. This method creates and initializes async msg.
  1445. */
  1446. {
  1447. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg;
  1448. uchar * pFinishStack;
  1449. HRESULT hr = S_OK;
  1450. hr = NdrpValidateAsyncStubCall( pAsyncSB );
  1451. if ( ! SUCCEEDED(hr) )
  1452. return hr;
  1453. if ( !pAsyncSB->CallState.Flags.BeginStarted ||
  1454. !pAsyncSB->CallState.Flags.BeginDone ||
  1455. pAsyncSB->CallState.Flags.FinishStarted )
  1456. return E_FAIL;
  1457. if ( pAsyncSB->CallState.Hr != 0 )
  1458. return pAsyncSB->CallState.Hr;
  1459. pAsyncMsg = (PNDR_DCOM_ASYNC_MESSAGE)pAsyncSB->CallState.pAsyncMsg;
  1460. hr = NdrpValidateDcomAsyncMsg( pAsyncMsg );
  1461. if ( ! SUCCEEDED(hr) )
  1462. return hr;
  1463. // We need to create the second stack for the app invoke.
  1464. // Do this first to simplify error conditions.
  1465. RpcTryExcept
  1466. {
  1467. pFinishStack = (uchar*) AsyncAlloca( pAsyncMsg,
  1468. StackSize + NDR_ASYNC_GUARD_SIZE );
  1469. }
  1470. RpcExcept( 1 )
  1471. {
  1472. NdrpAsyncStubMsgDestructor( pAsyncSB );
  1473. return E_OUTOFMEMORY;
  1474. }
  1475. RpcEndExcept
  1476. // Initialize the async message properly
  1477. pAsyncMsg->ProcContext.StartofStack = (uchar *) pFinishStack;
  1478. pAsyncMsg->FinishStack = (uchar *) pFinishStack;
  1479. pAsyncMsg->FinishStackSize = StackSize;
  1480. pAsyncMsg->StubMsg.pContext = &pAsyncMsg->ProcContext;
  1481. // Server: zero out stack for allocs.
  1482. MIDL_memset( pFinishStack, 0x0, StackSize );
  1483. MIDL_memset( (char *)pFinishStack + StackSize,
  1484. 0x72,
  1485. NDR_ASYNC_GUARD_SIZE );
  1486. pAsyncSB->CallState.Flags.FinishStarted = 1;
  1487. return S_OK;
  1488. }
  1489. HRESULT
  1490. NdrpAsyncProxySignal(
  1491. CStdAsyncProxyBuffer * pAsyncPB )
  1492. {
  1493. ISynchronize * pSynchronize;
  1494. HRESULT hr;
  1495. IUnknown * punkOuter = pAsyncPB->punkOuter;
  1496. hr = punkOuter->lpVtbl->QueryInterface( punkOuter,
  1497. IID_ISynchronize,
  1498. (void**)&pSynchronize );
  1499. if ( SUCCEEDED(hr) )
  1500. {
  1501. pSynchronize->lpVtbl->Signal( pSynchronize );
  1502. pSynchronize->lpVtbl->Release( pSynchronize );
  1503. }
  1504. return hr;
  1505. }
  1506. _inline
  1507. HRESULT
  1508. NdrpCallStateLock(
  1509. NdrDcomAsyncCallState * pCallState )
  1510. {
  1511. if ( 0 != InterlockedCompareExchange( (long*)& pCallState->Lock, 1, 0 ) )
  1512. {
  1513. return E_FAIL;
  1514. }
  1515. return RPC_S_OK;
  1516. }
  1517. _inline
  1518. void
  1519. NdrpCallStateUnlock(
  1520. NdrDcomAsyncCallState * pCallState )
  1521. {
  1522. InterlockedDecrement( (long*)& pCallState->Lock );
  1523. return;
  1524. }
  1525. HRESULT
  1526. NdrpAsyncProxyLock(
  1527. CStdAsyncProxyBuffer * pAsyncPB )
  1528. {
  1529. return NdrpCallStateLock( & pAsyncPB->CallState );
  1530. }
  1531. void
  1532. NdrpAsyncProxyUnlock(
  1533. CStdAsyncProxyBuffer * pAsyncPB )
  1534. {
  1535. NdrpCallStateUnlock( & pAsyncPB->CallState );
  1536. }
  1537. HRESULT
  1538. NdrpAsyncStubLock(
  1539. CStdAsyncStubBuffer * pAsyncSB )
  1540. {
  1541. return NdrpCallStateLock( & pAsyncSB->CallState );
  1542. }
  1543. void
  1544. NdrpAsyncStubUnlock(
  1545. CStdAsyncStubBuffer * pAsyncSB )
  1546. {
  1547. NdrpCallStateUnlock( & pAsyncSB->CallState );
  1548. }
  1549. void
  1550. NdrpFreeDcomAsyncMsg(
  1551. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg )
  1552. /*
  1553. This routine would free the AsyncMsg but not the AsyncHandle, as on the server
  1554. the user may need it and on the client it is user's to begin with.
  1555. */
  1556. {
  1557. if ( pAsyncMsg )
  1558. {
  1559. PMIDL_STUB_MESSAGE pStubMsg = & pAsyncMsg->StubMsg;
  1560. NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
  1561. // in NDR64, we are using allocacontext to hold corr info, so we don't
  1562. // want to free it there.
  1563. if ( pAsyncMsg->SyntaxType == XFER_SYNTAX_DCE )
  1564. NdrCorrelationFree( pStubMsg );
  1565. // Free the RPC buffer.
  1566. if ( pStubMsg->IsClient )
  1567. {
  1568. if ( ! pAsyncMsg->Flags.RuntimeCleanedUp )
  1569. {
  1570. void * pThis = *(void **)pAsyncMsg->ProcContext.StartofStack;
  1571. NdrProxyFreeBuffer( pThis, pStubMsg );
  1572. }
  1573. }
  1574. NdrpAllocaDestroy( &pAsyncMsg->ProcContext.AllocateContext );
  1575. // Prevent reusing of a handle that has been freed;
  1576. pAsyncMsg->Signature = NDR_FREED_ASYNC_SIGNATURE;
  1577. I_RpcBCacheFree( pAsyncMsg );
  1578. }
  1579. }
  1580. BOOL
  1581. NdrpDcomAsyncSend(
  1582. PMIDL_STUB_MESSAGE pStubMsg,
  1583. ISynchronize * pSynchronize )
  1584. /*
  1585. Call the channel to send.
  1586. On the client, pass the app's pSynchronize to it, such that channel can signal
  1587. the app.
  1588. On the server, pass NULL instead of a pSynchronize.
  1589. */
  1590. {
  1591. PRPC_MESSAGE pRpcMsg = pStubMsg->RpcMsg;
  1592. HRESULT hr = S_OK;
  1593. RPC_STATUS Status = RPC_S_OK;
  1594. BOOL fSendCalled = FALSE;
  1595. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  1596. if ( pStubMsg->pRpcChannelBuffer )
  1597. {
  1598. IAsyncRpcChannelBuffer * pAsChannel;
  1599. IRpcChannelBuffer * pChannel = (IRpcChannelBuffer *)
  1600. pStubMsg->pRpcChannelBuffer;
  1601. hr = pChannel->lpVtbl->QueryInterface( pChannel,
  1602. IID_IAsyncRpcChannelBuffer,
  1603. (void**)& pAsChannel );
  1604. if ( SUCCEEDED(hr) )
  1605. {
  1606. fSendCalled = TRUE;
  1607. hr = pAsChannel->lpVtbl->Send( pAsChannel,
  1608. (RPCOLEMESSAGE *)pRpcMsg,
  1609. pSynchronize,
  1610. (ulong*)& Status );
  1611. pAsChannel->lpVtbl->Release( pAsChannel );
  1612. // The channel never returns this code now for new async.
  1613. NDR_ASSERT( Status != RPC_S_SEND_INCOMPLETE, "Unexpected channel error" );
  1614. }
  1615. }
  1616. else
  1617. hr = E_NOINTERFACE;
  1618. // Alex:
  1619. if ( SUCCEEDED(hr) && Status == RPC_S_OK )
  1620. pStubMsg->fBufferValid = TRUE;
  1621. else
  1622. RpcRaiseException( Status );
  1623. return fSendCalled;
  1624. }
  1625. BOOL
  1626. NdrpDcomAsyncClientSend(
  1627. PMIDL_STUB_MESSAGE pStubMsg,
  1628. IUnknown * punkOuter )
  1629. /*
  1630. Call the channel to send.
  1631. On the client pass app's pSynchronize to it, such that channel can signal
  1632. the app.
  1633. On the server pass NULL instead of a pSynchronize.
  1634. */
  1635. {
  1636. PRPC_MESSAGE pRpcMsg = pStubMsg->RpcMsg;
  1637. HRESULT hr = S_OK;
  1638. BOOL fSendCalled = FALSE;
  1639. ISynchronize * pSynchronize = 0;
  1640. // Channel needs somebody to signal to, this will be the app.
  1641. hr = punkOuter->lpVtbl->QueryInterface( punkOuter,
  1642. IID_ISynchronize,
  1643. (void**) &pSynchronize );
  1644. if ( SUCCEEDED(hr) )
  1645. fSendCalled = NdrpDcomAsyncSend( pStubMsg, pSynchronize );
  1646. if ( pSynchronize )
  1647. pSynchronize->lpVtbl->Release( pSynchronize );
  1648. return fSendCalled;
  1649. }
  1650. void
  1651. NdrDcomAsyncReceive(
  1652. PMIDL_STUB_MESSAGE pStubMsg )
  1653. {
  1654. PRPC_MESSAGE pRpcMsg = pStubMsg->RpcMsg;
  1655. RPC_STATUS Status = RPC_S_OK;
  1656. HRESULT hr;
  1657. pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
  1658. pRpcMsg->RpcFlags &= ~RPC_BUFFER_PARTIAL;
  1659. // A complete call.
  1660. if ( pStubMsg->pRpcChannelBuffer )
  1661. {
  1662. IAsyncRpcChannelBuffer * pAsyncChannel = (IAsyncRpcChannelBuffer *)
  1663. pStubMsg->pRpcChannelBuffer;
  1664. hr = pAsyncChannel->lpVtbl->Receive( pAsyncChannel,
  1665. (PRPCOLEMESSAGE) pRpcMsg,
  1666. (unsigned long *)&Status );
  1667. }
  1668. if ( Status )
  1669. {
  1670. // No pending, the call would have blocked, real bug happened.
  1671. if ( pStubMsg->pAsyncMsg )
  1672. ((PNDR_DCOM_ASYNC_MESSAGE)pStubMsg->pAsyncMsg)->Flags.RuntimeCleanedUp = 1;
  1673. RpcRaiseException(Status);
  1674. }
  1675. else
  1676. {
  1677. pStubMsg->Buffer = (uchar*) pRpcMsg->Buffer;
  1678. pStubMsg->BufferStart = (uchar*)pRpcMsg->Buffer;
  1679. pStubMsg->BufferEnd = (uchar*)pRpcMsg->Buffer + pRpcMsg->BufferLength;
  1680. pStubMsg->fBufferValid = TRUE;
  1681. }
  1682. }
  1683. HRESULT
  1684. Ndr64pCompleteDcomAsyncStubCall(
  1685. CStdAsyncStubBuffer * pAsyncSB
  1686. );
  1687. HRESULT
  1688. NdrpAsyncStubSignal(
  1689. CStdAsyncStubBuffer * pAsyncSB )
  1690. /*
  1691. Signal on the async stub object:
  1692. The channel signals that the Finish call should be executed.
  1693. See if the stub object is active (or find one that is).
  1694. If the stub object is not active then the call fails.
  1695. */
  1696. {
  1697. HRESULT hr = S_OK;
  1698. BOOL fFoundActiveCall = FALSE;
  1699. while ( SUCCEEDED(hr) && ! fFoundActiveCall )
  1700. {
  1701. hr = NdrpValidateAsyncStubCall( pAsyncSB );
  1702. if ( SUCCEEDED(hr) )
  1703. {
  1704. if ( pAsyncSB->CallState.Flags.BeginStarted )
  1705. {
  1706. fFoundActiveCall = TRUE;
  1707. }
  1708. else
  1709. {
  1710. // Switch to the base interface call object. In case of
  1711. // delegation one of the base interface objects would be active.
  1712. IRpcStubBuffer * pBaseStubBuffer = pAsyncSB->pBaseStubBuffer;
  1713. if ( pBaseStubBuffer )
  1714. {
  1715. pAsyncSB = (CStdAsyncStubBuffer *) ((uchar *)pBaseStubBuffer
  1716. - offsetof(CStdAsyncStubBuffer,lpVtbl));
  1717. }
  1718. else
  1719. {
  1720. // None of the stubs active and a signal came.
  1721. hr = E_FAIL;
  1722. }
  1723. }
  1724. }
  1725. }
  1726. if ( SUCCEEDED(hr) )
  1727. {
  1728. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg =
  1729. (PNDR_DCOM_ASYNC_MESSAGE)pAsyncSB->CallState.pAsyncMsg;
  1730. hr = NdrpValidateDcomAsyncMsg( pAsyncMsg );
  1731. if ( SUCCEEDED(hr) )
  1732. {
  1733. #if defined(BUILD_NDR64)
  1734. if ( pAsyncMsg->SyntaxType == XFER_SYNTAX_DCE )
  1735. hr = NdrpCompleteDcomAsyncStubCall( pAsyncSB );
  1736. else
  1737. hr = Ndr64pCompleteDcomAsyncStubCall( pAsyncSB );
  1738. #else
  1739. hr = NdrpCompleteDcomAsyncStubCall( pAsyncSB );
  1740. #endif
  1741. }
  1742. }
  1743. return hr;
  1744. }