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.

647 lines
17 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1993 - 1999 Microsoft Corporation
  3. Module Name :
  4. hndl.c
  5. Abstract :
  6. To hold support routines for interpreting handles
  7. in support of Format Strings.
  8. Author :
  9. Bruce McQuistan (brucemc)
  10. Revision History :
  11. ryszardk 3/12/94 handle optimization and fixes
  12. ---------------------------------------------------------------------*/
  13. #include "ndrp.h"
  14. #include "hndl.h"
  15. #include "interp.h"
  16. extern const IID GUID_NULL;
  17. handle_t
  18. GenericHandleMgr(
  19. PMIDL_STUB_DESC pStubDesc,
  20. uchar * ArgPtr,
  21. PFORMAT_STRING pFormat,
  22. uint Flags,
  23. handle_t * pGenericHandle
  24. )
  25. /*++
  26. Description :
  27. Provides a filter for generic binding handle management issues.
  28. Deals with implicit or explicit generic binding handles calling
  29. user functions as appropriate.
  30. Arguments :
  31. pStubDesc - pointer to current StubDescriptor.
  32. ArgPtr - pointer to handle.
  33. pFormat - pointer to Format string such that *pFormat is a
  34. handle descriptor.
  35. Flag - flag indicating either binding or unbinding.
  36. Returns :
  37. Valid binding handle.
  38. */
  39. {
  40. uchar GHandleSize;
  41. handle_t ReturnHandle = 0;
  42. BOOL fBinding;
  43. GENERIC_BINDING_ROUTINE pBindFunc = 0;
  44. GENERIC_UNBIND_ROUTINE pUnBindFunc = 0;
  45. const GENERIC_BINDING_ROUTINE_PAIR * Table;
  46. BOOL fIsPtr = FALSE;
  47. Table = pStubDesc->aGenericBindingRoutinePairs;
  48. fBinding = (Flags & BINDING_MASK);
  49. if ( Flags & IMPLICIT_MASK )
  50. {
  51. //
  52. // Implicit generic: All the info is taken from the implicit generic
  53. // handle info structure accessed via stub descriptor.
  54. //
  55. PGENERIC_BINDING_INFO pGenHandleInfo;
  56. pGenHandleInfo = pStubDesc->IMPLICIT_HANDLE_INFO.pGenericBindingInfo;
  57. GHandleSize = (uchar) pGenHandleInfo->Size;
  58. if ( fBinding )
  59. pBindFunc = pGenHandleInfo->pfnBind;
  60. else
  61. pUnBindFunc = pGenHandleInfo->pfnUnbind;
  62. }
  63. else
  64. {
  65. //
  66. // Explicit generic: Get index into array of function ptrs and
  67. // the gen handle size the format string.
  68. //
  69. uchar TableIndex = pFormat[4];
  70. GHandleSize = LOW_NIBBLE(pFormat[1]);
  71. if ( fBinding )
  72. pBindFunc = Table[TableIndex].pfnBind;
  73. else
  74. pUnBindFunc = Table[TableIndex].pfnUnbind;
  75. if ( IS_HANDLE_PTR(pFormat[1]) )
  76. fIsPtr = TRUE;
  77. }
  78. //
  79. // Call users routine on correctly dereferenced pointer.
  80. //
  81. switch (GHandleSize)
  82. {
  83. case 1:
  84. {
  85. uchar HandlePtr = (uchar)(ULONG_PTR)ArgPtr;
  86. if ( fIsPtr )
  87. HandlePtr = * (uchar *)ArgPtr;
  88. if ( fBinding )
  89. ReturnHandle =
  90. (handle_t)(ULONG_PTR)
  91. (*(GENERIC_BIND_FUNC_ARGCHAR)pBindFunc)(
  92. (uchar)(ULONG_PTR)HandlePtr );
  93. else
  94. (*(GENERIC_UNBIND_FUNC_ARGCHAR)pUnBindFunc)(
  95. (uchar)(ULONG_PTR)HandlePtr,
  96. *pGenericHandle );
  97. break;
  98. }
  99. case 2:
  100. {
  101. ushort HandlePtr = (ushort)(ULONG_PTR)ArgPtr;
  102. if ( fIsPtr )
  103. HandlePtr = * (ushort *)ArgPtr;
  104. if ( fBinding )
  105. ReturnHandle =
  106. (handle_t)(ULONG_PTR)
  107. (*(GENERIC_BIND_FUNC_ARGSHORT)pBindFunc)(
  108. (ushort)(ULONG_PTR)HandlePtr );
  109. else
  110. (*(GENERIC_UNBIND_FUNC_ARGSHORT)pUnBindFunc)(
  111. (ushort)(ULONG_PTR)HandlePtr,
  112. *pGenericHandle );
  113. break;
  114. }
  115. case 4:
  116. {
  117. ulong HandlePtr = (ulong)(ULONG_PTR)ArgPtr;
  118. if ( fIsPtr )
  119. HandlePtr = * (ulong *)ArgPtr;
  120. if ( fBinding )
  121. ReturnHandle =
  122. (handle_t)(ULONG_PTR)
  123. (*(GENERIC_BIND_FUNC_ARGLONG)pBindFunc)(
  124. (ulong)(ULONG_PTR)HandlePtr );
  125. else
  126. (*(GENERIC_UNBIND_FUNC_ARGLONG)pUnBindFunc)(
  127. (ulong)(ULONG_PTR)HandlePtr,
  128. *pGenericHandle );
  129. break;
  130. }
  131. #if defined(__RPC_WIN64__)
  132. case 8:
  133. {
  134. ULONG_PTR HandlePtr = (ULONG_PTR)ArgPtr;
  135. if ( fIsPtr )
  136. HandlePtr = * (ULONG_PTR *)ArgPtr;
  137. if ( fBinding )
  138. ReturnHandle =
  139. (handle_t)(ULONG_PTR)
  140. (*(GENERIC_BIND_FUNC_ARGINT64)pBindFunc)(
  141. (ULONG_PTR)HandlePtr );
  142. else
  143. (*(GENERIC_UNBIND_FUNC_ARGINT64)pUnBindFunc)(
  144. (ULONG_PTR)HandlePtr,
  145. *pGenericHandle );
  146. break;
  147. }
  148. #endif
  149. default:
  150. NDR_ASSERT(0,"GenericHandleMgr : Handle size too big");
  151. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  152. return 0;
  153. }
  154. if ( fBinding )
  155. {
  156. *pGenericHandle = ReturnHandle;
  157. if ( ReturnHandle == NULL )
  158. RpcRaiseException( RPC_S_INVALID_BINDING );
  159. }
  160. else
  161. *pGenericHandle = NULL;
  162. return ReturnHandle;
  163. }
  164. void
  165. GenericHandleUnbind(
  166. PMIDL_STUB_DESC pStubDesc,
  167. uchar * ArgPtr,
  168. PFORMAT_STRING pFormat,
  169. uint Flags,
  170. handle_t * pGenericHandle
  171. )
  172. /*++
  173. Description :
  174. Unbinds a generic handle: checks if it is implicit or explicit,
  175. gets the handle and calls GenericHandleMgr.
  176. Arguments :
  177. pStubDesc - pointer to current StubDescriptor.
  178. ArgPtr - pointer to beginning of the stack.
  179. pFormat - pointer to Format string such that *pFormat is a
  180. handle descriptor.
  181. Flag - flag indicating implicit vs. explicit.
  182. --*/
  183. {
  184. if ( Flags & IMPLICIT_MASK )
  185. {
  186. PGENERIC_BINDING_INFO BindInfo;
  187. BindInfo = pStubDesc->IMPLICIT_HANDLE_INFO.pGenericBindingInfo;
  188. NDR_ASSERT( BindInfo != 0, "GenericHandleUnbind : null bind info" );
  189. ArgPtr = (uchar *) BindInfo->pObj;
  190. }
  191. else
  192. {
  193. ArgPtr += *(ushort *)(pFormat + 2);
  194. ArgPtr = *(uchar **)ArgPtr;
  195. }
  196. (void) GenericHandleMgr( pStubDesc,
  197. ArgPtr,
  198. pFormat,
  199. Flags,
  200. pGenericHandle );
  201. }
  202. handle_t
  203. ImplicitBindHandleMgr(
  204. PMIDL_STUB_DESC pStubDesc,
  205. uchar HandleType,
  206. handle_t * pSavedGenericHandle
  207. )
  208. /*++
  209. Description :
  210. Provides a filter for implicit handle management issues. Deals
  211. with binding handles (generic, primitive or auto), extracting
  212. a valid handle from pStubDesc.
  213. Arguments :
  214. pStubDesc - pointer to current StubDescriptor.
  215. HandleType - handle format code.
  216. Return :
  217. Valid handle.
  218. --*/
  219. {
  220. handle_t ReturnHandle;
  221. PGENERIC_BINDING_INFO pBindInfo;
  222. switch ( HandleType )
  223. {
  224. case FC_BIND_PRIMITIVE :
  225. ReturnHandle = *(pStubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle);
  226. break;
  227. case FC_BIND_GENERIC :
  228. pBindInfo = pStubDesc->IMPLICIT_HANDLE_INFO.pGenericBindingInfo;
  229. NDR_ASSERT( pBindInfo != 0,
  230. "ImplicitBindHandleMgr : no generic bind info" );
  231. ReturnHandle = GenericHandleMgr( pStubDesc,
  232. (uchar *)pBindInfo->pObj,
  233. &HandleType,
  234. BINDING_MASK | IMPLICIT_MASK,
  235. pSavedGenericHandle );
  236. break;
  237. case FC_AUTO_HANDLE :
  238. ReturnHandle = *(pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
  239. break;
  240. case FC_CALLBACK_HANDLE :
  241. ReturnHandle = GET_CURRENT_CALL_HANDLE();
  242. break;
  243. default :
  244. NDR_ASSERT(0, "ImplicitBindHandleMgr : bad handle type");
  245. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  246. return 0;
  247. }
  248. return ReturnHandle;
  249. }
  250. handle_t
  251. ExplicitBindHandleMgr(
  252. PMIDL_STUB_DESC pStubDesc,
  253. uchar * ArgPtr,
  254. PFORMAT_STRING pFormat,
  255. handle_t * pSavedGenericHandle
  256. )
  257. /*
  258. Description :
  259. Provides a filter for explicit binding handle management issues.
  260. Deals with binding handles (primitive, generic or context), calling
  261. either no routine, NDR routines or user functions as appropriate.
  262. To be called in the following cases:
  263. 1) if handle is explicit.
  264. a) before calling I_RpcGetBuffer (to bind).
  265. b) after unmarshalling (to unbind).
  266. Arguments :
  267. pStubDesc - pointer to current StubDescriptor.
  268. ArgPtr - Pointer to start of stack
  269. pFormat - pointer to Format string such that *pFormat is a
  270. handle descriptor.
  271. Return :
  272. Valid binding handle.
  273. */
  274. {
  275. handle_t ReturnHandle;
  276. //
  277. // We need to manage Explicit and Implicit handles.
  278. // Implicit handles are managed with info accessed via the StubMessage.
  279. // Explicit handles have their information stored in the format string.
  280. // We manage explicit handles for binding here.
  281. //
  282. //
  283. // Get location in stack of handle referent.
  284. //
  285. ArgPtr += *((ushort *)(pFormat + 2));
  286. ArgPtr = *(uchar **)ArgPtr;
  287. //
  288. // At this point ArgPtr is an address of the handle.
  289. //
  290. switch ( *pFormat )
  291. {
  292. case FC_BIND_PRIMITIVE :
  293. if ( IS_HANDLE_PTR(pFormat[1]) )
  294. ArgPtr = *(uchar **)ArgPtr;
  295. ReturnHandle = (handle_t)(ULONG_PTR)ArgPtr;
  296. break;
  297. case FC_BIND_GENERIC :
  298. ReturnHandle = GenericHandleMgr( pStubDesc,
  299. ArgPtr,
  300. pFormat,
  301. BINDING_MASK,
  302. pSavedGenericHandle );
  303. break;
  304. case FC_BIND_CONTEXT :
  305. if ( IS_HANDLE_PTR(pFormat[1]) )
  306. ArgPtr = *(uchar **)ArgPtr;
  307. if ( (!(ArgPtr)) && (!IS_HANDLE_OUT(pFormat[1])) )
  308. RpcRaiseException( RPC_X_SS_IN_NULL_CONTEXT );
  309. ReturnHandle = 0; // covers NULL case below.
  310. if ( ArgPtr && !
  311. (ReturnHandle = NDRCContextBinding((NDR_CCONTEXT)ArgPtr)) )
  312. RpcRaiseException( RPC_X_SS_CONTEXT_MISMATCH );
  313. break;
  314. default :
  315. NDR_ASSERT( 0, "ExplictBindHandleMgr : bad handle type" );
  316. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  317. return 0;
  318. }
  319. return ReturnHandle;
  320. }
  321. unsigned char * RPC_ENTRY
  322. NdrMarshallHandle(
  323. PMIDL_STUB_MESSAGE pStubMsg,
  324. uchar * pArg,
  325. PFORMAT_STRING pFormat
  326. )
  327. /*++
  328. Routine description :
  329. Marshalls a context handle.
  330. Arguments :
  331. pStubMsg - Pointer to stub message.
  332. pArg - Context handle to marshall (NDR_CCONTEXT or NDR_SCONTEXT).
  333. pFormat - Context handle's format string description.
  334. Return :
  335. Buffer pointer after marshalling the context handle.
  336. --*/
  337. {
  338. long Index;
  339. NDR_ASSERT( *pFormat == FC_BIND_CONTEXT,
  340. "NdrMarshallHandle : Expected a context handle" );
  341. if ( pStubMsg->IsClient )
  342. {
  343. NDR_CCONTEXT Context;
  344. ALIGN( pStubMsg->Buffer, 0x3 );
  345. //
  346. // Get the context handle.
  347. //
  348. Context = IS_HANDLE_PTR(pFormat[1]) ?
  349. *((NDR_CCONTEXT *)pArg) : (NDR_CCONTEXT)pArg;
  350. //
  351. // An [in] only context handle must be non-zero.
  352. //
  353. if ( ! Context && ! IS_HANDLE_OUT(pFormat[1]) )
  354. RpcRaiseException( RPC_X_SS_IN_NULL_CONTEXT );
  355. NDRCContextMarshall( Context, (void *) pStubMsg->Buffer );
  356. pStubMsg->Buffer += CONTEXT_HANDLE_WIRE_SIZE;
  357. }
  358. else
  359. {
  360. //
  361. // The NT 3.5 Interpreter used the stub message to keep track of
  362. // parameter number, newer Interpreters have the param number in the
  363. // context handle's description.
  364. //
  365. if ( pStubMsg->StubDesc->Version == NDR_VERSION_1_1 )
  366. {
  367. Index = pStubMsg->ParamNumber;
  368. }
  369. else
  370. {
  371. Index = (long) pFormat[3];
  372. if ( IS_HANDLE_RETURN(pFormat[1]) )
  373. {
  374. NDR_SCONTEXT SContext;
  375. //
  376. // Initialize the context handle.
  377. //
  378. SContext = NdrContextHandleInitialize ( pStubMsg,
  379. pFormat );
  380. //
  381. // Put the user context that was returned into the context
  382. // handle.
  383. //
  384. *((uchar **)NDRSContextValue(SContext)) = pArg;
  385. pStubMsg->SavedContextHandles[Index] = SContext;
  386. }
  387. }
  388. // If the new value is NULL, after marshalling, runtime will deallocate the buffer
  389. // We need to zero out the saved context to avoid referencing garbage.
  390. NDR_SCONTEXT pContext = pStubMsg->SavedContextHandles[Index];
  391. pStubMsg->SavedContextHandles[Index] = (NDR_SCONTEXT) CONTEXT_HANDLE_BEFORE_MARSHAL_MARKER;
  392. NdrServerContextNewMarshall( pStubMsg,
  393. pContext,
  394. pStubMsg->StubDesc->apfnNdrRundownRoutines[pFormat[2]],
  395. pFormat );
  396. pStubMsg->SavedContextHandles[Index] = (NDR_SCONTEXT)CONTEXT_HANDLE_AFTER_MARSHAL_MARKER;
  397. }
  398. return 0;
  399. }
  400. unsigned char * RPC_ENTRY
  401. NdrUnmarshallHandle(
  402. PMIDL_STUB_MESSAGE pStubMsg,
  403. uchar ** ppArg,
  404. PFORMAT_STRING pFormat,
  405. uchar fIgnored
  406. )
  407. /*++
  408. Routine description :
  409. Unmarshall a context handle.
  410. Arguments :
  411. pStubMsg - Pointer to stub message.
  412. ppArg - Pointer to the context handle on the client/server stack.
  413. On the client this is a NDR_CCONTEXT *. On the server
  414. side this is a NDR_SCONTEXT (regardless of direction).
  415. pFormat - Context handle's format string description.
  416. fIgnored - Ignored, but needed to match necessary routine prototype.
  417. Return :
  418. Buffer pointer after unmarshalling the context handle.
  419. --*/
  420. {
  421. NDR_ASSERT( *pFormat == FC_BIND_CONTEXT,
  422. "NdrUnmarshallHHandle : Expected a context handle" );
  423. if ( pStubMsg->IsClient )
  424. {
  425. ALIGN( pStubMsg->Buffer, 0x3 );
  426. //
  427. // Check if we have a pointer to a context handle
  428. // (the pointer can't be null).
  429. //
  430. if ( IS_HANDLE_PTR(pFormat[1]) )
  431. {
  432. ppArg = (uchar **) *ppArg;
  433. }
  434. //
  435. // Zero an [out] only context handle before unmarshalling.
  436. //
  437. if ( ! IS_HANDLE_IN(pFormat[1]) )
  438. *ppArg = 0;
  439. //
  440. // We must use the original binding handle in this call.
  441. //
  442. NDRCContextUnmarshall( (NDR_CCONTEXT *)ppArg,
  443. pStubMsg->SavedHandle,
  444. (void *)pStubMsg->Buffer,
  445. pStubMsg->RpcMsg->DataRepresentation );
  446. pStubMsg->Buffer += CONTEXT_HANDLE_WIRE_SIZE;
  447. }
  448. else
  449. {
  450. NDR_SCONTEXT SContext;
  451. SContext = NdrServerContextNewUnmarshall( pStubMsg,
  452. pFormat );
  453. NdrSaveContextHandle( pStubMsg,
  454. SContext,
  455. ppArg,
  456. pFormat );
  457. }
  458. return 0;
  459. }
  460. void
  461. NdrSaveContextHandle (
  462. PMIDL_STUB_MESSAGE pStubMsg,
  463. NDR_SCONTEXT CtxtHandle,
  464. uchar ** ppArg,
  465. PFORMAT_STRING pFormat )
  466. /*++
  467. Routine Description :
  468. Saves a context handle's current value and then extracts the user's
  469. context value.
  470. Arguments :
  471. pStubMsg - The stub message.
  472. CtxtHandle - The context handle.
  473. ppArg - Pointer to where user's context value should go.
  474. Return :
  475. None.
  476. */
  477. {
  478. long Index;
  479. //
  480. // The NT 3.5 Interpreter used the stub message to keep track of
  481. // parameter number, newer Interpreters have the param number in the
  482. // context handle's description.
  483. //
  484. if ( pStubMsg->StubDesc->Version == NDR_VERSION_1_1 )
  485. Index = pStubMsg->ParamNumber;
  486. else
  487. Index = (long) pFormat[3];
  488. pStubMsg->SavedContextHandles[Index] = CtxtHandle;
  489. // On 32b alphas, a handle pointer needs to be properly sign extended
  490. // to a 64b virtual stack value, as this will become the register value.
  491. // So, we cast to the REGISTER_TYPE to get the sign extension
  492. if ( ! IS_HANDLE_PTR(pFormat[1]) )
  493. *((REGISTER_TYPE*)ppArg) = (REGISTER_TYPE) *(NDRSContextValue(CtxtHandle));
  494. else
  495. *((REGISTER_TYPE*)ppArg) = (REGISTER_TYPE) NDRSContextValue(CtxtHandle);
  496. }