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.

509 lines
17 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name :
  4. expr.cxx
  5. Abstract :
  6. This file contains the ndr expression evaluation and correlation
  7. check routines.
  8. Author :
  9. Yong Qu (yongqu) Jan 2000
  10. Mike Zoran (mzoran) Jan 2000
  11. Revision History :
  12. ---------------------------------------------------------------------*/
  13. #include "precomp.hxx"
  14. #include "..\..\ndr20\ndrole.h"
  15. #include "asyncu.h"
  16. extern "C" {
  17. extern const GUID CLSID_RpcHelper;
  18. }
  19. typedef void ( * PFNNDR64CHECKCORRELATION )(
  20. PMIDL_STUB_MESSAGE pStubMsg,
  21. EXPR_VALUE Value,
  22. PNDR64_FORMAT pFormat,
  23. NDR64_EXPRESSION_TYPE ExpressionType );
  24. PFNNDR64CHECKCORRELATION pfnCorrCheck[] =
  25. {
  26. Ndr64pLateCheckCorrelation,
  27. Ndr64pEarlyCheckCorrelation,
  28. Ndr64pNoCheckCorrelation,
  29. Ndr64pNoCheckCorrelation
  30. };
  31. EXPR_VALUE
  32. Ndr64pExprGetVar( PMIDL_STUB_MESSAGE pStubMsg,
  33. PNDR64_FORMAT pFormat,
  34. PNDR64_FORMAT * pNext )
  35. {
  36. NDR64_EXPR_VAR * pExpr = (NDR64_EXPR_VAR *)pFormat;
  37. NDR_ASSERT( pExpr->ExprType == FC_EXPR_VAR, "must be a variable!");
  38. uchar *pCount = pStubMsg->pCorrMemory + pExpr->Offset;
  39. EXPR_VALUE Value =
  40. Ndr64pSimpleTypeToExprValue( pExpr->VarType,
  41. pCount );
  42. *pNext =
  43. (PNDR64_FORMAT)((PFORMAT_STRING)pFormat + sizeof( NDR64_EXPR_VAR ));
  44. return Value;
  45. }
  46. EXPR_VALUE EvaluateExpr( PMIDL_STUB_MESSAGE pStubMsg,
  47. PNDR64_FORMAT pFormat,
  48. PNDR64_FORMAT * pNext );
  49. EXPR_VALUE
  50. Ndr64CalculateExpr( PMIDL_STUB_MESSAGE pStubMsg,
  51. NDR64_EXPR_OPERATOR * pExpr,
  52. PNDR64_FORMAT *pNext )
  53. {
  54. EXPR_VALUE Value, LeftValue, RightValue ;
  55. PNDR64_FORMAT pTempNext;
  56. BOOL fRational;
  57. switch ( pExpr->Operator )
  58. {
  59. case OP_UNARY_PLUS:
  60. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  61. Value = +Value;
  62. break;
  63. case OP_UNARY_MINUS:
  64. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  65. Value = -Value;
  66. break;
  67. case OP_UNARY_NOT:
  68. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  69. Value = !Value;
  70. break;
  71. case OP_UNARY_COMPLEMENT:
  72. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  73. Value = ~Value;
  74. break;
  75. case OP_UNARY_CAST:
  76. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  77. Value = Ndr64pCastExprValueToExprValue( pExpr->CastType, Value );
  78. break;
  79. case OP_UNARY_AND:
  80. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  81. Value = ~Value;
  82. break;
  83. case OP_UNARY_SIZEOF:
  84. NDR_ASSERT(0 , "Ndr64CalculateExpr : OP_UNARY_SIZEOF is invalid\n");
  85. return 0;
  86. break;
  87. case OP_UNARY_INDIRECTION:
  88. Value = Ndr64pExprGetVar( pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  89. Value = Ndr64pSimpleTypeToExprValue( pExpr->CastType,
  90. (uchar*)Value );
  91. break;
  92. case OP_PRE_INCR:
  93. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  94. Value = ++Value;
  95. break;
  96. case OP_PRE_DECR:
  97. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  98. Value = --Value;
  99. break;
  100. case OP_POST_INCR:
  101. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  102. Value = Value++;
  103. break;
  104. case OP_POST_DECR:
  105. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  106. Value = Value--;
  107. break;
  108. case OP_PLUS:
  109. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  110. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  111. Value = LeftValue + RightValue;
  112. break;
  113. case OP_MINUS:
  114. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  115. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  116. Value = LeftValue - RightValue;
  117. break;
  118. case OP_STAR:
  119. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  120. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  121. Value = LeftValue * RightValue;
  122. break;
  123. case OP_SLASH:
  124. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  125. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  126. Value = (EXPR_VALUE) (LeftValue / RightValue);
  127. break;
  128. case OP_MOD:
  129. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  130. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  131. Value = LeftValue % RightValue;
  132. break;
  133. case OP_LEFT_SHIFT:
  134. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  135. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  136. Value = LeftValue << RightValue;
  137. break;
  138. case OP_RIGHT_SHIFT:
  139. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  140. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  141. Value = LeftValue >> RightValue;
  142. break;
  143. case OP_LESS:
  144. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  145. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  146. Value = LeftValue + RightValue;
  147. break;
  148. case OP_LESS_EQUAL:
  149. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  150. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  151. Value = LeftValue <= RightValue;
  152. break;
  153. case OP_GREATER_EQUAL:
  154. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  155. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  156. Value = LeftValue >= RightValue;
  157. break;
  158. case OP_GREATER:
  159. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  160. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  161. Value = LeftValue > RightValue;
  162. break;
  163. case OP_EQUAL:
  164. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  165. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  166. Value = LeftValue == RightValue;
  167. break;
  168. case OP_NOT_EQUAL:
  169. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  170. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  171. Value = LeftValue != RightValue;
  172. break;
  173. case OP_AND:
  174. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  175. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  176. Value = LeftValue & RightValue;
  177. break;
  178. case OP_OR:
  179. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  180. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  181. Value = LeftValue | RightValue;
  182. break;
  183. case OP_XOR:
  184. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  185. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  186. Value = LeftValue ^ RightValue;
  187. break;
  188. case OP_LOGICAL_AND:
  189. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  190. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  191. Value = LeftValue && RightValue;
  192. break;
  193. case OP_LOGICAL_OR:
  194. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  195. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  196. Value = LeftValue || RightValue;
  197. break;
  198. case OP_QM:
  199. LeftValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), &pTempNext );
  200. RightValue = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , &pTempNext );
  201. fRational = ( BOOL ) EvaluateExpr(pStubMsg, (PFORMAT_STRING )pTempNext , pNext );
  202. Value = fRational ? LeftValue : RightValue;
  203. break;
  204. case OP_ASYNCSPLIT:
  205. {
  206. PNDR_DCOM_ASYNC_MESSAGE pAsyncMsg =
  207. (PNDR_DCOM_ASYNC_MESSAGE) pStubMsg->pAsyncMsg;
  208. CORRELATION_CONTEXT CorrCtxt( pStubMsg, pAsyncMsg->BeginStack );
  209. Value = EvaluateExpr(pStubMsg, (PFORMAT_STRING )pExpr + sizeof( NDR64_EXPR_OPERATOR ), pNext );
  210. break;
  211. }
  212. case OP_CORR_POINTER:
  213. pStubMsg->pCorrMemory = pStubMsg->Memory;
  214. break;
  215. case OP_CORR_TOP_LEVEL:
  216. pStubMsg->pCorrMemory = pStubMsg->StackTop;
  217. break;
  218. default:
  219. NDR_ASSERT(0 ,
  220. "Ndr64CalculateExpr : invalid operator");
  221. }
  222. return Value;
  223. }
  224. EXPR_VALUE
  225. EvaluateExpr( PMIDL_STUB_MESSAGE pStubMsg,
  226. PNDR64_FORMAT pFormat,
  227. PNDR64_FORMAT * pNext )
  228. {
  229. EXPR_VALUE Value;
  230. switch ( *(PFORMAT_STRING)pFormat )
  231. {
  232. case FC_EXPR_NOOP:
  233. {
  234. PFORMAT_STRING pContinueFormat =
  235. ((PFORMAT_STRING)pFormat) + (( NDR64_EXPR_NOOP *)pFormat )->Size;
  236. Value = EvaluateExpr( pStubMsg, (PNDR64_FORMAT)pContinueFormat, pNext );
  237. break;
  238. }
  239. case FC_EXPR_CONST32:
  240. {
  241. NDR64_EXPR_CONST32 *pExpr = ( NDR64_EXPR_CONST32 *) pFormat;
  242. Value = (EXPR_VALUE) pExpr->ConstValue;
  243. *pNext = (PNDR64_FORMAT)(pExpr + 1);
  244. break;
  245. }
  246. case FC_EXPR_CONST64:
  247. {
  248. NDR64_EXPR_CONST64 *pExpr;
  249. pExpr = ( NDR64_EXPR_CONST64 * )pFormat;
  250. Value = (EXPR_VALUE) pExpr->ConstValue;
  251. *pNext = (PNDR64_FORMAT)(pExpr + 1);
  252. break;
  253. }
  254. case FC_EXPR_VAR:
  255. {
  256. NDR64_EXPR_VAR * pExpr = ( NDR64_EXPR_VAR * )pFormat;
  257. Value = Ndr64pExprGetVar( pStubMsg, pFormat, pNext ); // indirection.
  258. break;
  259. }
  260. case FC_EXPR_OPER:
  261. {
  262. Value = Ndr64CalculateExpr( pStubMsg, ( NDR64_EXPR_OPERATOR * )pFormat, pNext );
  263. break;
  264. }
  265. default:
  266. NDR_ASSERT(0 ,
  267. "Ndr64pComputeConformance : no expr eval routines");
  268. }
  269. return Value;
  270. }
  271. EXPR_VALUE
  272. Ndr64EvaluateExpr(
  273. PMIDL_STUB_MESSAGE pStubMsg,
  274. // uchar * pMemory,
  275. PNDR64_FORMAT pFormat,
  276. NDR64_EXPRESSION_TYPE ExpressionType )
  277. {
  278. PNDR64_FORMAT pNext;
  279. EXPR_VALUE Value;
  280. NDR_ASSERT( pStubMsg->pCorrMemory, "Ndr64EvaluateExpr: pCorrMemory not initialized." );
  281. // we don't need to care about correlation flag in evaluation
  282. PFORMAT_STRING pActualFormat =
  283. ((PFORMAT_STRING)pFormat) + sizeof( NDR64_UINT32 );
  284. Value = EvaluateExpr( pStubMsg, (PNDR64_FORMAT)pActualFormat, &pNext );
  285. switch ( ExpressionType )
  286. {
  287. case EXPR_MAXCOUNT:
  288. pStubMsg->MaxCount = (ULONG_PTR)Value;
  289. break;
  290. case EXPR_ACTUALCOUNT:
  291. pStubMsg->ActualCount = ( unsigned long )Value;
  292. break;
  293. case EXPR_OFFSET:
  294. pStubMsg->Offset = ( unsigned long )Value;
  295. break;
  296. }
  297. return Value;
  298. }
  299. void
  300. Ndr64pAddCorrelationData(
  301. PMIDL_STUB_MESSAGE pStubMsg,
  302. uchar * pMemory,
  303. PNDR64_FORMAT pFormat,
  304. EXPR_VALUE Value,
  305. NDR64_EXPRESSION_TYPE CheckKind
  306. )
  307. /*
  308. Adds a check data to the correlation data base for a later evaluation.
  309. */
  310. {
  311. PNDR64_CORRELATION_INFO pCorrInfo = (PNDR64_CORRELATION_INFO)pStubMsg->pCorrInfo;
  312. if ( !pCorrInfo || NDR64_SLOTS_PER_CORRELATION_INFO == pCorrInfo->SlotsUsed )
  313. {
  314. NDR_PROC_CONTEXT *pProcContext = (NDR_PROC_CONTEXT*)pStubMsg->pContext;
  315. PNDR64_CORRELATION_INFO pCorrInfoNew = (PNDR64_CORRELATION_INFO)
  316. NdrpAlloca(&pProcContext->AllocateContext, sizeof(NDR64_CORRELATION_INFO));
  317. pCorrInfoNew->pNext = pCorrInfo;
  318. pCorrInfoNew->SlotsUsed = 0;
  319. pCorrInfo = pCorrInfoNew;
  320. pStubMsg->pCorrInfo = (PNDR_CORRELATION_INFO)pCorrInfo;
  321. }
  322. NDR64_UINT32 CurrentSlot = pCorrInfo->SlotsUsed;
  323. pCorrInfo->Data[ CurrentSlot ].pMemoryObject = pMemory;
  324. pCorrInfo->Data[ CurrentSlot ].Value = Value;
  325. pCorrInfo->Data[ CurrentSlot ].pCorrDesc = pFormat;
  326. pCorrInfo->Data[ CurrentSlot ].CheckKind = CheckKind;
  327. pCorrInfo->SlotsUsed++;
  328. }
  329. RPCRTAPI
  330. void
  331. RPC_ENTRY
  332. Ndr64CorrelationPass(
  333. PMIDL_STUB_MESSAGE pStubMsg
  334. )
  335. /*
  336. Walks the data base to check all the correlated values that could not be checked
  337. on fly.
  338. */
  339. {
  340. if ( !pStubMsg->pCorrInfo )
  341. {
  342. return;
  343. }
  344. for( PNDR64_CORRELATION_INFO pCorrInfo = (PNDR64_CORRELATION_INFO)pStubMsg->pCorrInfo;
  345. NULL != pCorrInfo;
  346. pCorrInfo = pCorrInfo->pNext )
  347. {
  348. for(NDR64_UINT32 SlotNumber = 0; SlotNumber < pCorrInfo->SlotsUsed; SlotNumber++)
  349. {
  350. CORRELATION_CONTEXT CorrCtxt( pStubMsg, pCorrInfo->Data[ SlotNumber ].pMemoryObject );
  351. // we must check now.
  352. Ndr64pEarlyCheckCorrelation( pStubMsg,
  353. pCorrInfo->Data[ SlotNumber ].Value,
  354. pCorrInfo->Data[ SlotNumber ].pCorrDesc,
  355. (NDR64_EXPRESSION_TYPE)pCorrInfo->Data[ SlotNumber ].CheckKind );
  356. }
  357. }
  358. }
  359. // no-check flag is set.
  360. void Ndr64pNoCheckCorrelation(
  361. PMIDL_STUB_MESSAGE pStubMsg,
  362. EXPR_VALUE Value,
  363. PNDR64_FORMAT pFormat,
  364. NDR64_EXPRESSION_TYPE ExpressionType )
  365. {
  366. return;
  367. }
  368. void Ndr64pEarlyCheckCorrelation(
  369. PMIDL_STUB_MESSAGE pStubMsg,
  370. EXPR_VALUE Value,
  371. PNDR64_FORMAT pFormat,
  372. NDR64_EXPRESSION_TYPE ExpressionType )
  373. {
  374. EXPR_VALUE ExprValue ;
  375. EXPR_VALUE DestValue = Value;
  376. BOOL fCheckOk;
  377. ExprValue = Ndr64EvaluateExpr( pStubMsg, pFormat, ExpressionType );
  378. fCheckOk = ( DestValue == ExprValue );
  379. if ( !fCheckOk && ( ExpressionType == EXPR_IID ) )
  380. {
  381. IID * piidValue = (IID *)ExprValue;
  382. IID * piidArg = (IID *)DestValue;
  383. fCheckOk = !memcmp( piidValue, piidArg, sizeof( IID )) ;
  384. }
  385. if ( !fCheckOk )
  386. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  387. return;
  388. }
  389. void Ndr64pLateCheckCorrelation(
  390. PMIDL_STUB_MESSAGE pStubMsg,
  391. EXPR_VALUE Value,
  392. PNDR64_FORMAT pFormat,
  393. NDR64_EXPRESSION_TYPE ExpressionType )
  394. {
  395. Ndr64pAddCorrelationData( pStubMsg, pStubMsg->pCorrMemory, pFormat, Value, ExpressionType );
  396. return;
  397. }
  398. void
  399. Ndr64pCheckCorrelation(
  400. PMIDL_STUB_MESSAGE pStubMsg,
  401. EXPR_VALUE Value,
  402. PNDR64_FORMAT pFormat,
  403. NDR64_EXPRESSION_TYPE ExpressionType )
  404. {
  405. NDR64_UINT32 Flags;
  406. Flags = * (NDR64_UINT32 *)pFormat;
  407. ASSERT( Flags <= ( FC_NDR64_EARLY_CORRELATION | FC_NDR64_NOCHECK_CORRELATION ) );
  408. pfnCorrCheck[Flags]( pStubMsg, Value, pFormat, ExpressionType );
  409. }