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.

514 lines
9.0 KiB

  1. // utxcpt4.c - user mode seh test #3.
  2. #include <ntos.h>
  3. //
  4. // Define function prototypes.
  5. //
  6. VOID
  7. bar (
  8. IN NTSTATUS Status,
  9. IN PULONG Counter
  10. );
  11. VOID
  12. eret (
  13. IN NTSTATUS Status,
  14. IN PULONG Counter
  15. );
  16. VOID
  17. foo (
  18. IN NTSTATUS Status
  19. );
  20. VOID
  21. fret (
  22. IN PULONG Counter
  23. );
  24. BOOLEAN
  25. Tkm (
  26. VOID
  27. );
  28. //
  29. // Define static storage.
  30. //
  31. PTESTFCN TestFunction = Tkm;
  32. main()
  33. {
  34. Tkm();
  35. }
  36. BOOLEAN
  37. Tkm (
  38. )
  39. {
  40. EXCEPTION_RECORD ExceptionRecord;
  41. LONG Counter;
  42. //
  43. // Announce start of exception test.
  44. //
  45. DbgPrint("Start of exception test\n");
  46. //
  47. // Initialize exception record.
  48. //
  49. ExceptionRecord.ExceptionCode = STATUS_INTEGER_OVERFLOW;
  50. ExceptionRecord.ExceptionFlags = 0;
  51. ExceptionRecord.ExceptionRecord = NULL;
  52. ExceptionRecord.NumberParameters = 0;
  53. //
  54. // Simply try statement with a finally clause that is entered sequentially.
  55. //
  56. DbgPrint(" test1...");
  57. Counter = 0;
  58. try {
  59. Counter += 1;
  60. } finally {
  61. if (abnormal_termination() == FALSE) {
  62. Counter += 1;
  63. }
  64. }
  65. if (Counter != 2) {
  66. DbgPrint("failed\n");
  67. } else {
  68. DbgPrint("succeeded\n");
  69. }
  70. //
  71. // Simple try statement with an exception clause that is never executed
  72. // because there is no exception raised in the try clause.
  73. //
  74. DbgPrint(" test2...");
  75. Counter = 0;
  76. try {
  77. Counter += 1;
  78. } except (Counter) {
  79. Counter += 1;
  80. }
  81. if (Counter != 1) {
  82. DbgPrint("failed\n");
  83. } else {
  84. DbgPrint("succeeded\n");
  85. }
  86. //
  87. // Simple try statement with an exception handler that is never executed
  88. // because the exception expression continues execution.
  89. //
  90. DbgPrint(" test3...");
  91. Counter = 0;
  92. try {
  93. Counter -= 1;
  94. RtlRaiseException(&ExceptionRecord);
  95. } except (Counter) {
  96. Counter -= 1;
  97. }
  98. if (Counter != - 1) {
  99. DbgPrint("failed\n");
  100. } else {
  101. DbgPrint("succeeded\n");
  102. }
  103. //
  104. // Simple try statement with an exception clause that is always executed.
  105. //
  106. DbgPrint(" test4...");
  107. Counter = 0;
  108. try {
  109. Counter += 1;
  110. RtlRaiseStatus(STATUS_INTEGER_OVERFLOW);
  111. } except (Counter) {
  112. Counter += 1;
  113. }
  114. if (Counter != 2) {
  115. DbgPrint("failed\n");
  116. } else {
  117. DbgPrint("succeeded\n");
  118. }
  119. //
  120. // Simply try statement with a finally clause that is entered as the
  121. // result of an exception.
  122. //
  123. DbgPrint(" test5...");
  124. Counter = 0;
  125. try {
  126. try {
  127. Counter += 1;
  128. RtlRaiseException(&ExceptionRecord);
  129. } finally {
  130. if (abnormal_termination() != FALSE) {
  131. Counter += 1;
  132. }
  133. }
  134. } except (Counter) {
  135. if (Counter == 2) {
  136. Counter += 1;
  137. }
  138. }
  139. if (Counter != 3) {
  140. DbgPrint("failed\n");
  141. } else {
  142. DbgPrint("succeeded\n");
  143. }
  144. //
  145. // Simple try that calls a function which raises an exception.
  146. //
  147. DbgPrint(" test6...");
  148. Counter = 0;
  149. try {
  150. Counter += 1;
  151. foo(STATUS_ACCESS_VIOLATION);
  152. } except ((GetExceptionCode() == STATUS_ACCESS_VIOLATION) ?
  153. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  154. Counter += 1;
  155. }
  156. if (Counter != 2) {
  157. DbgPrint("failed\n");
  158. } else {
  159. DbgPrint("succeeded\n");
  160. }
  161. //
  162. // Simple try that calls a function which calls a function that
  163. // raises an exception. The first function has a finally clause
  164. // that must be executed for this test to work.
  165. //
  166. DbgPrint(" test7...");
  167. Counter = 0;
  168. try {
  169. bar(STATUS_ACCESS_VIOLATION, (PULONG)&Counter);
  170. } except ((GetExceptionCode() == STATUS_ACCESS_VIOLATION) ?
  171. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  172. Counter -= 1;
  173. }
  174. if (Counter != 98) {
  175. DbgPrint("failed\n");
  176. } else {
  177. DbgPrint("succeeded\n");
  178. }
  179. //
  180. // A try within an except
  181. //
  182. DbgPrint(" test8...");
  183. Counter = 0;
  184. try {
  185. foo(STATUS_ACCESS_VIOLATION);
  186. } except ((GetExceptionCode() == STATUS_ACCESS_VIOLATION) ?
  187. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  188. Counter += 1;
  189. try {
  190. foo(STATUS_SUCCESS);
  191. } except ((GetExceptionCode() == STATUS_SUCCESS) ?
  192. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  193. if (Counter != 1) {
  194. DbgPrint("failed...");
  195. } else {
  196. DbgPrint("succeeded...");
  197. }
  198. Counter += 1;
  199. }
  200. }
  201. if (Counter != 2) {
  202. DbgPrint("failed\n");
  203. } else {
  204. DbgPrint("succeeded\n");
  205. }
  206. //
  207. // A goto from an exception clause that needs to pass
  208. // through a finally
  209. //
  210. DbgPrint(" test9...");
  211. Counter = 0;
  212. try {
  213. try {
  214. foo(STATUS_ACCESS_VIOLATION);
  215. } except ((GetExceptionCode() == STATUS_ACCESS_VIOLATION) ?
  216. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  217. Counter += 1;
  218. goto t9;
  219. }
  220. } finally {
  221. Counter += 1;
  222. }
  223. t9:;
  224. if (Counter != 2) {
  225. DbgPrint("failed\n");
  226. } else {
  227. DbgPrint("succeeded\n");
  228. }
  229. //
  230. // A goto from an finally clause that needs to pass
  231. // through a finally
  232. //
  233. DbgPrint(" test10...");
  234. Counter = 0;
  235. try {
  236. try {
  237. Counter += 1;
  238. } finally {
  239. Counter += 1;
  240. goto t10;
  241. }
  242. } finally {
  243. Counter += 1;
  244. }
  245. t10:;
  246. if (Counter != 3) {
  247. DbgPrint("failed\n");
  248. } else {
  249. DbgPrint("succeeded\n");
  250. }
  251. //
  252. // A goto from an exception clause that needs to pass
  253. // through a finally into the outer finally clause.
  254. //
  255. DbgPrint(" test11...");
  256. Counter = 0;
  257. try {
  258. try {
  259. try {
  260. Counter += 1;
  261. foo(STATUS_INTEGER_OVERFLOW);
  262. } except (EXCEPTION_EXECUTE_HANDLER) {
  263. Counter += 1;
  264. goto t11;
  265. }
  266. } finally {
  267. Counter += 1;
  268. }
  269. t11:;
  270. } finally {
  271. Counter += 1;
  272. }
  273. if (Counter != 4) {
  274. DbgPrint("failed\n");
  275. } else {
  276. DbgPrint("succeeded\n");
  277. }
  278. //
  279. // A goto from an finally clause that needs to pass
  280. // through a finally into the outer finally clause.
  281. //
  282. DbgPrint(" test12...");
  283. Counter = 0;
  284. try {
  285. try {
  286. Counter += 1;
  287. } finally {
  288. Counter += 1;
  289. goto t12;
  290. }
  291. t12:;
  292. } finally {
  293. Counter += 1;
  294. }
  295. if (Counter != 3) {
  296. DbgPrint("failed\n");
  297. } else {
  298. DbgPrint("succeeded\n");
  299. }
  300. //
  301. // A return from an except clause
  302. //
  303. DbgPrint(" test13...");
  304. Counter = 0;
  305. try {
  306. Counter += 1;
  307. eret(STATUS_ACCESS_VIOLATION, (PULONG)&Counter);
  308. } finally {
  309. Counter += 1;
  310. }
  311. if (Counter != 4) {
  312. DbgPrint("failed\n");
  313. } else {
  314. DbgPrint("succeeded\n");
  315. }
  316. //
  317. // A return from a finally clause
  318. //
  319. DbgPrint(" test14...");
  320. Counter = 0;
  321. try {
  322. Counter += 1;
  323. fret((PULONG)&Counter);
  324. } finally {
  325. Counter += 1;
  326. }
  327. if (Counter != 5) {
  328. DbgPrint("failed\n");
  329. } else {
  330. DbgPrint("succeeded\n");
  331. }
  332. //
  333. // Announce end of exception test.
  334. //
  335. DbgPrint("End of exception test\n");
  336. return TRUE;
  337. }
  338. VOID
  339. fret(
  340. IN PULONG Counter
  341. )
  342. {
  343. try {
  344. try {
  345. *Counter += 1;
  346. } finally {
  347. *Counter += 1;
  348. return;
  349. }
  350. } finally {
  351. *Counter += 1;
  352. }
  353. return;
  354. }
  355. VOID
  356. eret(
  357. IN NTSTATUS Status,
  358. IN PULONG Counter
  359. )
  360. {
  361. try {
  362. try {
  363. foo(Status);
  364. } except ((GetExceptionCode() == Status) ?
  365. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
  366. *Counter += 1;
  367. return;
  368. }
  369. } finally {
  370. *Counter += 1;
  371. }
  372. return;
  373. }
  374. VOID
  375. bar (
  376. IN NTSTATUS Status,
  377. IN PULONG Counter
  378. )
  379. {
  380. try {
  381. foo(Status);
  382. } finally {
  383. if (abnormal_termination() != FALSE) {
  384. *Counter = 99;
  385. } else {
  386. *Counter = 100;
  387. }
  388. }
  389. return;
  390. }
  391. VOID
  392. foo(
  393. IN NTSTATUS Status
  394. )
  395. {
  396. //
  397. // Raise exception.
  398. //
  399. RtlRaiseStatus(Status);
  400. return;
  401. }