Leaked source code of windows server 2003
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.

513 lines
18 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. static const 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 + (long)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. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  222. break;
  223. }
  224. return Value;
  225. }
  226. EXPR_VALUE
  227. EvaluateExpr( PMIDL_STUB_MESSAGE pStubMsg,
  228. PNDR64_FORMAT pFormat,
  229. PNDR64_FORMAT * pNext )
  230. {
  231. EXPR_VALUE Value;
  232. switch ( *(PFORMAT_STRING)pFormat )
  233. {
  234. case FC_EXPR_NOOP:
  235. {
  236. PFORMAT_STRING pContinueFormat =
  237. ((PFORMAT_STRING)pFormat) + (( NDR64_EXPR_NOOP *)pFormat )->Size;
  238. Value = EvaluateExpr( pStubMsg, (PNDR64_FORMAT)pContinueFormat, pNext );
  239. break;
  240. }
  241. case FC_EXPR_CONST32:
  242. {
  243. NDR64_EXPR_CONST32 *pExpr = ( NDR64_EXPR_CONST32 *) pFormat;
  244. Value = (EXPR_VALUE) pExpr->ConstValue;
  245. *pNext = (PNDR64_FORMAT)(pExpr + 1);
  246. break;
  247. }
  248. case FC_EXPR_CONST64:
  249. {
  250. NDR64_EXPR_CONST64 *pExpr;
  251. pExpr = ( NDR64_EXPR_CONST64 * )pFormat;
  252. Value = (EXPR_VALUE) pExpr->ConstValue;
  253. *pNext = (PNDR64_FORMAT)(pExpr + 1);
  254. break;
  255. }
  256. case FC_EXPR_VAR:
  257. {
  258. NDR64_EXPR_VAR * pExpr = ( NDR64_EXPR_VAR * )pFormat;
  259. Value = Ndr64pExprGetVar( pStubMsg, pFormat, pNext ); // indirection.
  260. break;
  261. }
  262. case FC_EXPR_OPER:
  263. {
  264. Value = Ndr64CalculateExpr( pStubMsg, ( NDR64_EXPR_OPERATOR * )pFormat, pNext );
  265. break;
  266. }
  267. default:
  268. NDR_ASSERT(0 ,
  269. "Ndr64pComputeConformance : no expr eval routines");
  270. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  271. break;
  272. }
  273. return Value;
  274. }
  275. EXPR_VALUE
  276. Ndr64EvaluateExpr(
  277. PMIDL_STUB_MESSAGE pStubMsg,
  278. // uchar * pMemory,
  279. PNDR64_FORMAT pFormat,
  280. NDR64_EXPRESSION_TYPE ExpressionType )
  281. {
  282. PNDR64_FORMAT pNext;
  283. EXPR_VALUE Value;
  284. NDR_ASSERT( pStubMsg->pCorrMemory, "Ndr64EvaluateExpr: pCorrMemory not initialized." );
  285. // we don't need to care about correlation flag in evaluation
  286. PFORMAT_STRING pActualFormat =
  287. ((PFORMAT_STRING)pFormat) + sizeof( NDR64_UINT32 );
  288. Value = EvaluateExpr( pStubMsg, (PNDR64_FORMAT)pActualFormat, &pNext );
  289. switch ( ExpressionType )
  290. {
  291. case EXPR_MAXCOUNT:
  292. pStubMsg->MaxCount = (ULONG_PTR)Value;
  293. break;
  294. case EXPR_ACTUALCOUNT:
  295. pStubMsg->ActualCount = ( unsigned long )Value;
  296. break;
  297. case EXPR_OFFSET:
  298. pStubMsg->Offset = ( unsigned long )Value;
  299. break;
  300. }
  301. return Value;
  302. }
  303. void
  304. Ndr64pAddCorrelationData(
  305. PMIDL_STUB_MESSAGE pStubMsg,
  306. uchar * pMemory,
  307. PNDR64_FORMAT pFormat,
  308. EXPR_VALUE Value,
  309. NDR64_EXPRESSION_TYPE CheckKind
  310. )
  311. /*
  312. Adds a check data to the correlation data base for a later evaluation.
  313. */
  314. {
  315. PNDR64_CORRELATION_INFO pCorrInfo = (PNDR64_CORRELATION_INFO)pStubMsg->pCorrInfo;
  316. if ( !pCorrInfo || NDR64_SLOTS_PER_CORRELATION_INFO == pCorrInfo->SlotsUsed )
  317. {
  318. NDR_PROC_CONTEXT *pProcContext = (NDR_PROC_CONTEXT*)pStubMsg->pContext;
  319. PNDR64_CORRELATION_INFO pCorrInfoNew = (PNDR64_CORRELATION_INFO)
  320. NdrpAlloca(&pProcContext->AllocateContext, sizeof(NDR64_CORRELATION_INFO));
  321. pCorrInfoNew->pNext = pCorrInfo;
  322. pCorrInfoNew->SlotsUsed = 0;
  323. pCorrInfo = pCorrInfoNew;
  324. pStubMsg->pCorrInfo = (PNDR_CORRELATION_INFO)pCorrInfo;
  325. }
  326. NDR64_UINT32 CurrentSlot = pCorrInfo->SlotsUsed;
  327. pCorrInfo->Data[ CurrentSlot ].pMemoryObject = pMemory;
  328. pCorrInfo->Data[ CurrentSlot ].Value = Value;
  329. pCorrInfo->Data[ CurrentSlot ].pCorrDesc = pFormat;
  330. pCorrInfo->Data[ CurrentSlot ].CheckKind = CheckKind;
  331. pCorrInfo->SlotsUsed++;
  332. }
  333. RPCRTAPI
  334. void
  335. RPC_ENTRY
  336. Ndr64CorrelationPass(
  337. PMIDL_STUB_MESSAGE pStubMsg
  338. )
  339. /*
  340. Walks the data base to check all the correlated values that could not be checked
  341. on fly.
  342. */
  343. {
  344. if ( !pStubMsg->pCorrInfo )
  345. {
  346. return;
  347. }
  348. for( PNDR64_CORRELATION_INFO pCorrInfo = (PNDR64_CORRELATION_INFO)pStubMsg->pCorrInfo;
  349. NULL != pCorrInfo;
  350. pCorrInfo = pCorrInfo->pNext )
  351. {
  352. for(NDR64_UINT32 SlotNumber = 0; SlotNumber < pCorrInfo->SlotsUsed; SlotNumber++)
  353. {
  354. CORRELATION_CONTEXT CorrCtxt( pStubMsg, pCorrInfo->Data[ SlotNumber ].pMemoryObject );
  355. // we must check now.
  356. Ndr64pEarlyCheckCorrelation( pStubMsg,
  357. pCorrInfo->Data[ SlotNumber ].Value,
  358. pCorrInfo->Data[ SlotNumber ].pCorrDesc,
  359. (NDR64_EXPRESSION_TYPE)pCorrInfo->Data[ SlotNumber ].CheckKind );
  360. }
  361. }
  362. }
  363. // no-check flag is set.
  364. void Ndr64pNoCheckCorrelation(
  365. PMIDL_STUB_MESSAGE pStubMsg,
  366. EXPR_VALUE Value,
  367. PNDR64_FORMAT pFormat,
  368. NDR64_EXPRESSION_TYPE ExpressionType )
  369. {
  370. return;
  371. }
  372. void Ndr64pEarlyCheckCorrelation(
  373. PMIDL_STUB_MESSAGE pStubMsg,
  374. EXPR_VALUE Value,
  375. PNDR64_FORMAT pFormat,
  376. NDR64_EXPRESSION_TYPE ExpressionType )
  377. {
  378. EXPR_VALUE ExprValue ;
  379. EXPR_VALUE DestValue = Value;
  380. BOOL fCheckOk;
  381. ExprValue = Ndr64EvaluateExpr( pStubMsg, pFormat, ExpressionType );
  382. fCheckOk = ( DestValue == ExprValue );
  383. if ( !fCheckOk && ( ExpressionType == EXPR_IID ) )
  384. {
  385. IID * piidValue = (IID *)ExprValue;
  386. IID * piidArg = (IID *)DestValue;
  387. fCheckOk = !memcmp( piidValue, piidArg, sizeof( IID )) ;
  388. }
  389. if ( !fCheckOk )
  390. RpcRaiseException( RPC_X_BAD_STUB_DATA );
  391. return;
  392. }
  393. void Ndr64pLateCheckCorrelation(
  394. PMIDL_STUB_MESSAGE pStubMsg,
  395. EXPR_VALUE Value,
  396. PNDR64_FORMAT pFormat,
  397. NDR64_EXPRESSION_TYPE ExpressionType )
  398. {
  399. Ndr64pAddCorrelationData( pStubMsg, pStubMsg->pCorrMemory, pFormat, Value, ExpressionType );
  400. return;
  401. }
  402. void
  403. Ndr64pCheckCorrelation(
  404. PMIDL_STUB_MESSAGE pStubMsg,
  405. EXPR_VALUE Value,
  406. PNDR64_FORMAT pFormat,
  407. NDR64_EXPRESSION_TYPE ExpressionType )
  408. {
  409. NDR64_UINT32 Flags;
  410. Flags = * (NDR64_UINT32 *)pFormat;
  411. ASSERT( Flags <= ( FC_NDR64_EARLY_CORRELATION | FC_NDR64_NOCHECK_CORRELATION ) );
  412. pfnCorrCheck[Flags]( pStubMsg, Value, pFormat, ExpressionType );
  413. }