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.

768 lines
14 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <eh.h>
  4. extern "C"
  5. ULONG
  6. DbgPrint (
  7. PCH Format,
  8. ...
  9. );
  10. extern "C"
  11. VOID
  12. DbgBreakPoint (
  13. VOID
  14. );
  15. #define FALSE 0
  16. #define TRUE 1
  17. #define NO_CTOR_THROW 1
  18. #define NO_DTOR_THROW 2
  19. #define B0 B b0(__FUNCTION__, __LINE__)
  20. #define B1 B b1(__FUNCTION__, __LINE__)
  21. #define B2 B b2(__FUNCTION__, __LINE__)
  22. #define B3 B b3(__FUNCTION__, __LINE__)
  23. #define B4 B b4(__FUNCTION__, __LINE__)
  24. #define B5 B b5(__FUNCTION__, __LINE__)
  25. #define B6 B b6(__FUNCTION__, __LINE__)
  26. #define B7 B b7(__FUNCTION__, __LINE__)
  27. #define B8 B b8(__FUNCTION__, __LINE__)
  28. #define A0 A a0(__FUNCTION__, __LINE__)
  29. #define A1 A a1(__FUNCTION__, __LINE__)
  30. #define A2 A a2(__FUNCTION__, __LINE__)
  31. #define A3 A a3(__FUNCTION__, __LINE__)
  32. #define A4 A a4(__FUNCTION__, __LINE__)
  33. #define A5 A a5(__FUNCTION__, __LINE__)
  34. #define A6 A a6(__FUNCTION__, __LINE__)
  35. #define A7 A a7(__FUNCTION__, __LINE__)
  36. #define A8 A a8(__FUNCTION__, __LINE__)
  37. const unsigned int NumMaxA = 100, NumMaxB = 100, NumTests = 9;
  38. int AObject[NumMaxA];
  39. int BObject[NumMaxB];
  40. int MaxTest = 10;
  41. int MaxObjectCount = 1;
  42. int Fail;
  43. int A_id;
  44. int B_id;
  45. typedef enum ClassName{clA = 'A', clB};
  46. /*********************************** Class C *********************************/
  47. class C
  48. {
  49. protected:
  50. const char *FuncName;
  51. unsigned int LineNumber;
  52. int id;
  53. ClassName CName;
  54. void C::PrintCtor() {
  55. DbgPrint("%c ctor. id = %4d\t ", CName, id);
  56. if (FuncName) {
  57. DbgPrint("\tFunction = %s\tLineNum = %4d\n", FuncName, LineNumber);
  58. } else {
  59. DbgPrint("\n");
  60. }
  61. fflush(stdout);
  62. }
  63. void C::PrintCtor(int n) {
  64. DbgPrint("%c cctor. id = %4d\tpid = %4d\t", CName, id, n);
  65. if (FuncName) {
  66. DbgPrint("\tFunction = %s\tLineNum = %4d\n", FuncName, LineNumber);
  67. } else {
  68. DbgPrint("\n");
  69. }
  70. fflush(stdout);
  71. }
  72. void C::PrintDtor()
  73. {
  74. DbgPrint("%c dtor. id = %4d\t ", CName, id);
  75. if (FuncName) {
  76. DbgPrint("\tFunction = %s\tLineNum = %4d\n", FuncName, LineNumber);
  77. } else {
  78. DbgPrint("\n");
  79. }
  80. fflush(stdout);
  81. }
  82. void Alloc() {
  83. int *Arr = NULL;
  84. if (CName == clB)
  85. Arr = BObject;
  86. else if(CName == clA)
  87. Arr = AObject;
  88. else
  89. DbgPrint("ERROR: Alloc Unknown ClassName %c\n", CName);
  90. if (Arr) {
  91. if (Arr[id]) {
  92. DbgPrint("Error: id#%4d for %c already exists\n", id, CName);
  93. }
  94. Arr[id] = 1;
  95. }
  96. }
  97. void DAlloc() {
  98. int *Arr = NULL;
  99. if (CName == clB)
  100. Arr = BObject;
  101. else if(CName == clA)
  102. Arr = AObject;
  103. else
  104. DbgPrint("ERROR: Alloc Unknown ClassName %c\n", CName);
  105. if (Arr) {
  106. if (Arr[id] != 1) {
  107. DbgPrint("Error: id#%4d for %c already destructed\n", id, CName);
  108. }
  109. Arr[id]--;
  110. }
  111. }
  112. public:
  113. void print(){
  114. DbgPrint("%c print. id = %4d\t ", CName, id);
  115. if (FuncName) {
  116. DbgPrint("\tFunction = %s\tLineNum = %4d\n", FuncName, LineNumber);
  117. } else {
  118. DbgPrint("\n");
  119. }
  120. }
  121. C():id(0), CName(clA), FuncName(NULL), LineNumber(0){}
  122. ~C() {
  123. PrintDtor();
  124. DAlloc();
  125. }
  126. };
  127. /*********************************** Class B *********************************/
  128. class B : public C
  129. {
  130. public:
  131. B();
  132. B(const B &b);
  133. B(const char *, unsigned int);
  134. };
  135. B::B()
  136. {
  137. id = B_id++;
  138. CName = clB;
  139. PrintCtor();
  140. Alloc();
  141. }
  142. B::B(const B &b)
  143. {
  144. id = B_id++;
  145. CName = clB;
  146. PrintCtor(b.id);
  147. Alloc();
  148. }
  149. B::B(const char *ch, unsigned int i){
  150. id = B_id++;
  151. CName = clB;
  152. FuncName = ch;
  153. LineNumber = i;
  154. PrintCtor();
  155. Alloc();
  156. }
  157. /*********************************** Class A *********************************/
  158. class A : public C
  159. {
  160. public:
  161. A();
  162. A(int)
  163. {
  164. id = A_id++;
  165. CName = clA;
  166. PrintCtor();
  167. Alloc();
  168. }
  169. A(const A&);
  170. A(const char *ch, unsigned int i);
  171. A operator+(A a);
  172. };
  173. A::A()
  174. {
  175. id = A_id++;
  176. CName = clA;
  177. PrintCtor();
  178. Alloc();
  179. }
  180. A::A(const A &b)
  181. {
  182. id = A_id++;
  183. CName = clA;
  184. PrintCtor(b.id);
  185. Alloc();
  186. }
  187. A::A(const char *ch, unsigned int i){
  188. id = A_id++;
  189. CName = clA;
  190. FuncName = ch;
  191. LineNumber = i;
  192. PrintCtor();
  193. Alloc();
  194. }
  195. void ThrowA()
  196. {
  197. A0;
  198. throw a0;
  199. }
  200. void ThrowB()
  201. {
  202. B0;
  203. throw b0;
  204. }
  205. void SehThrow()
  206. {
  207. RaiseException(STATUS_INTEGER_OVERFLOW, 0, 0, NULL);
  208. }
  209. void Rethrow()
  210. {
  211. A0;
  212. throw;
  213. }
  214. int SehFilter(EXCEPTION_POINTERS *pExPtrs, unsigned int ExceptionCode)
  215. {
  216. if (pExPtrs->ExceptionRecord->ExceptionCode == ExceptionCode)
  217. return EXCEPTION_EXECUTE_HANDLER;
  218. else
  219. return EXCEPTION_CONTINUE_SEARCH;
  220. }
  221. /*********************************** Test 1 *********************************/
  222. void Test1foo(void) {
  223. A0;
  224. try {
  225. throw; // first rethrow
  226. }
  227. catch(A) {
  228. A1;
  229. DbgPrint("my exception\n");
  230. }
  231. catch(...) {
  232. A2;
  233. DbgPrint("an other exception\n");
  234. }
  235. }
  236. int Test1()
  237. {
  238. A0;
  239. try {
  240. A1;
  241. try {
  242. A2;
  243. throw A();
  244. }
  245. catch(...) {
  246. A3;
  247. Test1foo();
  248. throw; // 2nd rethrow -- will result in ACCESS VIOLATION error
  249. }
  250. }
  251. catch (...) {
  252. A4;
  253. try {
  254. A5;
  255. throw;
  256. }
  257. catch (A &e)
  258. {
  259. A6;
  260. e.print();
  261. }
  262. catch(...) {
  263. A7;
  264. }
  265. }
  266. return 0;
  267. }
  268. /*********************************** Test 2 *********************************/
  269. void goandfail()
  270. {
  271. DbgPrint( "throwing in goandfail\n" );
  272. throw (long)10;
  273. }
  274. void dosomething()
  275. {
  276. A0;
  277. try
  278. {
  279. A1;
  280. goandfail();
  281. }
  282. catch( long & sc )
  283. {
  284. A2;
  285. DbgPrint( "catch in dosomething\n" );
  286. throw sc;
  287. }
  288. }
  289. class BTest2 : public B
  290. {
  291. char * _p;
  292. public:
  293. BTest2() : _p(0) {}
  294. ~BTest2()
  295. {
  296. try
  297. {
  298. A1;
  299. dosomething();
  300. }
  301. catch( long & sc )
  302. {
  303. A2;
  304. DbgPrint( "catch in ~B\n" );
  305. }
  306. delete [] _p;
  307. }
  308. void print() {
  309. DbgPrint("BTest2 _p = %p\n", _p );
  310. B::print();
  311. }
  312. };
  313. int Test2()
  314. {
  315. DbgPrint( "top\n" );
  316. try
  317. {
  318. A1;
  319. BTest2 b;
  320. b.print();
  321. goandfail();
  322. }
  323. catch( long & sc )
  324. {
  325. A2;
  326. DbgPrint( "catch in main\n" );
  327. }
  328. DbgPrint( "all done\n" );
  329. return 0;
  330. }
  331. /*********************************** Test 3 *********************************/
  332. void Test3()
  333. {
  334. A0;
  335. try
  336. {
  337. A1;
  338. char* pStr = NULL;
  339. try
  340. {
  341. A2;
  342. throw "1st throw\n";
  343. }
  344. catch( char* str )
  345. {
  346. A3;
  347. DbgPrint("%s%s", "A ", str);
  348. try
  349. {
  350. A4;
  351. throw "2nd throw\n";
  352. }
  353. catch( char* str )
  354. {
  355. A5;
  356. DbgPrint("%s%s", "B ", str);
  357. }
  358. throw;
  359. }
  360. }
  361. catch ( char* str )
  362. {
  363. A6;
  364. DbgPrint("%s%s", "C ", str);
  365. }
  366. DbgPrint("Done\n");
  367. }
  368. /*********************************** Test 4 *********************************/
  369. void SehTest4()
  370. {
  371. int i;
  372. __try{
  373. i = 0;
  374. ThrowA();
  375. } __finally {
  376. if (i) {
  377. DbgPrint("Error: Finally in same function called %2d times\n", i+1);
  378. Fail = 1;
  379. }
  380. i++;
  381. }
  382. }
  383. void Test4foo()
  384. {
  385. A0;
  386. try {
  387. A1;
  388. try {
  389. A2;
  390. SehTest4();
  391. } catch(A ac) {
  392. A3;
  393. ThrowB();
  394. }
  395. } catch(B bc) {
  396. A4;
  397. throw;
  398. };
  399. }
  400. void Test4boo()
  401. {
  402. A0;
  403. try{
  404. A1;
  405. Test4foo();
  406. } catch (B bc) {
  407. A2;
  408. SehThrow();
  409. }
  410. }
  411. void Test4()
  412. {
  413. __try {
  414. Test4boo();
  415. } __except(1) {
  416. DbgPrint("Test4: Test4 __except\n");
  417. }
  418. }
  419. /*********************************** Test 5 *********************************/
  420. void SehTest5()
  421. {
  422. int i;
  423. __try{
  424. i = 0;
  425. ThrowA();
  426. } __finally {
  427. i++;
  428. if (i-1) {
  429. DbgPrint("Error: Finally in same function called %2d times\n", i);
  430. Fail = 1;
  431. } else {
  432. SehThrow();
  433. }
  434. }
  435. }
  436. void Test5foo()
  437. {
  438. A0;
  439. try {
  440. A1;
  441. try {
  442. A2;
  443. SehTest5();
  444. } catch (...) {
  445. A3;
  446. }
  447. } catch (...) {
  448. A4;
  449. };
  450. }
  451. void Test5()
  452. {
  453. __try {
  454. Test5foo();
  455. } __except(1) {
  456. DbgPrint("Test5: Test5 __except\n");
  457. }
  458. }
  459. void Test5Seh()
  460. {
  461. __try {
  462. SehTest5();
  463. } __except(1) {
  464. DbgPrint("Test5: Test5 __except\n");
  465. }
  466. }
  467. /*********************************** Test 6 *********************************/
  468. void Test6SeTrans( unsigned int u, EXCEPTION_POINTERS*pExp)
  469. {
  470. B0;
  471. if (pExp->ExceptionRecord->ExceptionCode == STATUS_INTEGER_OVERFLOW)
  472. throw b0;
  473. }
  474. void Test6TTrans()
  475. {
  476. static int i = 0;
  477. A0;
  478. try {
  479. A1;
  480. SehThrow();
  481. } catch (B b) {
  482. if (i == 0) {
  483. i++;
  484. Rethrow();
  485. } else {
  486. ThrowB();
  487. }
  488. }
  489. }
  490. void Test6SehFunc()
  491. {
  492. __try {
  493. Test6TTrans();
  494. } __except(SehFilter(exception_info(), STATUS_INTEGER_OVERFLOW)) {
  495. DbgPrint("In Test6SehFunc __except\n");
  496. }
  497. }
  498. void Test6CppFunc()
  499. {
  500. int i = 0;
  501. A0;
  502. try {
  503. A1;
  504. Test6SehFunc();
  505. } catch(B b) {
  506. A2;
  507. DbgPrint("Error: Test6CppFunc catch(B b)\n");
  508. Fail = 1;
  509. }
  510. try {
  511. try {
  512. A3;
  513. Test6SehFunc();
  514. } catch(B b) {
  515. A4;
  516. i = 1;
  517. }
  518. } catch(...) {
  519. DbgPrint("Error: Missed catch(B b) in Test6CppFunc\n");
  520. Fail = 1;
  521. }
  522. }
  523. void Test6()
  524. {
  525. __try {
  526. _set_se_translator(Test6SeTrans);
  527. Test6CppFunc();
  528. } __except(1) {
  529. DbgPrint("Error: Test6 __except\n");
  530. Fail = 1;
  531. }
  532. _set_se_translator(NULL);
  533. }
  534. /*********************************** Test 7 *********************************/
  535. void Test7();
  536. void Test7foo()
  537. {
  538. static int i = 0;
  539. A0;
  540. try {
  541. if (i == 0)
  542. SehThrow();
  543. else
  544. Rethrow();
  545. } catch(...) {
  546. if (i++ == 0)
  547. Test7();
  548. else
  549. Rethrow();
  550. }
  551. }
  552. void Test7() {
  553. static int i = 0;
  554. int j = i++;
  555. __try {
  556. Test7foo();
  557. } __except(SehFilter(exception_info(), STATUS_INTEGER_OVERFLOW)) {
  558. if (j == 0) {
  559. DbgPrint("Error: missed one Except After rethrow\n");
  560. Fail = 1;
  561. }
  562. }
  563. }
  564. /*********************************** Test 8 *********************************/
  565. void Test8()
  566. {
  567. A0;
  568. try{
  569. A1;
  570. try {
  571. ThrowB();
  572. } catch (B b1) {
  573. try {
  574. A2;
  575. throw a2;
  576. } catch (A a) {
  577. A3;
  578. }
  579. throw;
  580. }
  581. } catch (B b2) {
  582. A4;
  583. }
  584. }
  585. int TestOver()
  586. {
  587. int i;
  588. int ret = 0;
  589. for (i = 0; i < NumMaxB; i++)
  590. {
  591. if (BObject[i] > 0) {
  592. DbgPrint("Error: id#%4d for B not destructed\n", i);
  593. ret = 1;
  594. } else if (BObject[i] < 0) {
  595. DbgPrint("Error: id#%4d for B destructed %d times", i, 1-AObject[i]);
  596. ret = 1;
  597. }
  598. BObject[i] = 0;
  599. }
  600. for (i = 0; i < NumMaxA; i++)
  601. {
  602. if (AObject[i] > 0) {
  603. DbgPrint("Error: id#%4d for A not destructed\n", i);
  604. ret = 1;
  605. } else if (AObject[i] < 0) {
  606. DbgPrint("Error: id#%4d for A destructed %d times", i, 1-AObject[i]);
  607. ret = 1;
  608. }
  609. AObject[i] = 0;
  610. }
  611. if (Fail) {
  612. ret = 1;
  613. Fail = 0;
  614. }
  615. A_id = B_id = 0;
  616. return ret;
  617. }
  618. /*********************************** Test 9 *********************************/
  619. struct TEST9
  620. {
  621. int i;
  622. TEST9() {
  623. A0;
  624. i = 0;
  625. }
  626. ~TEST9() {
  627. A0;
  628. try {
  629. A1;
  630. ThrowB();
  631. } catch (B) {
  632. A2;
  633. }
  634. }
  635. };
  636. void Test9TEST()
  637. {
  638. TEST9 T;
  639. ThrowA();
  640. }
  641. void Test9()
  642. {
  643. A0;
  644. try {
  645. A1;
  646. Test9TEST();
  647. } catch (A a) {
  648. A2;
  649. }
  650. }
  651. main()
  652. {
  653. int i;
  654. DbgBreakPoint();
  655. __try {
  656. for (i = 0; i < NumTests; i++) {
  657. switch (i) {
  658. case 0:
  659. Test1();
  660. break;
  661. case 1:
  662. Test2();
  663. break;
  664. case 2:
  665. Test3();
  666. break;
  667. case 3:
  668. Test4();
  669. break;
  670. case 4:
  671. Test5();
  672. DbgPrint("One live B expected\n");
  673. break;
  674. case 5:
  675. Test6();
  676. break;
  677. case 6:
  678. Test7();
  679. break;
  680. case 7:
  681. Test8();
  682. break;
  683. case 8:
  684. Test9();
  685. break;
  686. }
  687. if (TestOver()) {
  688. DbgPrint("TEST#%2d FAILED\n", i+1);
  689. } else {
  690. DbgPrint("TEST#%2d PASSED\n", i+1);
  691. }
  692. }
  693. } __except(DbgBreakPoint()) {
  694. }
  695. }