Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553// Copyright (c) Microsoft. All rights reserved.// Licensed under the MIT license. See LICENSE file in the project root for// full license information.
/* * Exercise lots of different kinds of C++ EH frames. Compile this with * every combination of opts you can to stress the C++ EH frame code in the * backend. */
#include <stdio.h>#include <malloc.h>
#ifndef ALIGN#define ALIGN 64#endif
extern int TestFunc(int, ...);
int failures;
int global;bool TestFuncThrows;
struct SmallObj{ virtual ~SmallObj() { TestFunc(1, this); }; int x;};
struct BigObj{ virtual ~BigObj() { TestFunc(1, this); }; char x[4096];};
int Simple(int arg){ puts(__FUNCTION__); int res = 0; SmallObj f; return TestFunc(1, &f, &res, &arg);}
int Try(int arg){ puts(__FUNCTION__); int res = 0; SmallObj f; try { res = TestFunc(1, &f, &res, &arg); } catch (double) { res = TestFunc(2, &f, &res, &arg); } return res;}
int GSCookie(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; SmallObj f; return TestFunc(1, buf, &f, &res, &arg);}
int TryAndGSCookie(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; SmallObj f; try { res = TestFunc(1, &buf, &f, &res, &arg); } catch (double) { res = TestFunc(2, &buf, &f, &res, &arg); } return res;}
int Align(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; SmallObj f; return TestFunc(1, d, &f, &res, &arg);}
int TryAndAlign(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; SmallObj f; try { res = TestFunc(1, d, &f, &res, &arg); } catch (double) { res = TestFunc(2, d, &f, &res, &arg); } return res;}
int GSCookieAndAlign(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; SmallObj f; return TestFunc(1, buf, d, &f, &res, &arg);}
int TryAndGSCookieAndAlign(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; SmallObj f; try { res = TestFunc(1, buf, d, &f, &res, &arg); } catch (double) { res = TestFunc(2, buf, d, &f, &res, &arg); } return res;}
int Alloca(int arg){ puts(__FUNCTION__); int res = 0; SmallObj f; return TestFunc(1, _alloca(global), &f, &res, &arg);}
int TryAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; SmallObj f; try { res = TestFunc(1, _alloca(global), &f, &res, &arg); } catch (double) { res = TestFunc(2, &f, &res, &arg); } return res;}
int GSCookieAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; SmallObj f; return TestFunc(1, buf, _alloca(global), &f, &res, &arg);}
int TryAndGSCookieAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; SmallObj f; try { res = TestFunc(1, &buf, _alloca(global), &f, &res, &arg); } catch (double) { res = TestFunc(2, &buf, &f, &res, &arg); } return res;}
int AlignAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; SmallObj f; return TestFunc(1, d, _alloca(global), &f, &res, &arg);}
int TryAndAlignAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; SmallObj f; try { res = TestFunc(1, d, _alloca(global), &f, &res, &arg); } catch (double) { res = TestFunc(2, d, &f, &res, &arg); } return res;}
int GSCookieAndAlignAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; SmallObj f; return TestFunc(1, buf, d, _alloca(global), &f, &res, &arg);}
int TryAndGSCookieAndAlignAndAlloca(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; SmallObj f; try { res = TestFunc(1, buf, d, _alloca(global), &f, &res, &arg); } catch (double) { res = TestFunc(2, buf, d, &f, &res, &arg); } return res;}
/* The *AndBigLocals set of functions try to trigger EBP adjustment */
int BigLocals(int arg){ puts(__FUNCTION__); int res = 0; BigObj f1; return TestFunc(1, &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; BigObj f1; try { res = TestFunc(1, &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int GSCookieAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; BigObj f1; return TestFunc(1, buf, &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndGSCookieAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; BigObj f1; try { res = TestFunc(1, &buf, &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, &buf, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int AlignAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; BigObj f1; return TestFunc(1, d, &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndAlignAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; BigObj f1; try { res = TestFunc(1, d, &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, d, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int GSCookieAndAlignAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; BigObj f1; return TestFunc(1, buf, d, &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndGSCookieAndAlignAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; BigObj f1; try { res = TestFunc(1, buf, d, &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, buf, d, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int AllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; BigObj f1; return TestFunc(1, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; BigObj f1; try { res = TestFunc(1, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int GSCookieAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; BigObj f1; return TestFunc(1, buf, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndGSCookieAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; BigObj f1; try { res = TestFunc(1, &buf, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, &buf, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int AlignAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; BigObj f1; return TestFunc(1, d, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndAlignAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; __declspec(align(ALIGN)) double d[4]; BigObj f1; try { res = TestFunc(1, d, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, d, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
int GSCookieAndAlignAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; BigObj f1; return TestFunc(1, buf, d, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg);}
int TryAndGSCookieAndAlignAndAllocaAndBigLocals(int arg){ puts(__FUNCTION__); int res = 0; char buf[16]; __declspec(align(ALIGN)) double d[4]; BigObj f1; try { res = TestFunc(1, buf, d, _alloca(global), &f1, &res, &res, &res, &res, &res, &arg); } catch (double) { res = TestFunc(2, buf, d, &f1, &res, &res, &res, &res, &res, &arg); } return res;}
__declspec(noinline)int TestFunc(int, ...){ if (TestFuncThrows) { TestFuncThrows = false; throw 123; }
return global;}
void RunTests(){ puts("Test pass 1 - no throws");
try { Simple(1); Try(1); GSCookie(1); TryAndGSCookie(1); Align(1); TryAndAlign(1); GSCookieAndAlign(1); TryAndGSCookieAndAlign(1); Alloca(1); TryAndAlloca(1); GSCookieAndAlloca(1); TryAndGSCookieAndAlloca(1); AlignAndAlloca(1); TryAndAlignAndAlloca(1); GSCookieAndAlignAndAlloca(1); TryAndGSCookieAndAlignAndAlloca(1); BigLocals(1); TryAndBigLocals(1); GSCookieAndBigLocals(1); TryAndGSCookieAndBigLocals(1); AlignAndBigLocals(1); TryAndAlignAndBigLocals(1); GSCookieAndAlignAndBigLocals(1); TryAndGSCookieAndAlignAndBigLocals(1); AllocaAndBigLocals(1); TryAndAllocaAndBigLocals(1); GSCookieAndAllocaAndBigLocals(1); TryAndGSCookieAndAllocaAndBigLocals(1); AlignAndAllocaAndBigLocals(1); TryAndAlignAndAllocaAndBigLocals(1); GSCookieAndAlignAndAllocaAndBigLocals(1); TryAndGSCookieAndAlignAndAllocaAndBigLocals(1); } catch (...) { puts("ERROR - throw not expected"); ++failures; }
puts("Test pass 2 - throws");
for (int i = 0; i < 32; ++i) { TestFuncThrows = true; bool caught = false; try { switch (i) { case 0: Simple(1); break; case 1: Try(1); break; case 2: GSCookie(1); break; case 3: TryAndGSCookie(1); break; case 4: Align(1); break; case 5: TryAndAlign(1); break; case 6: GSCookieAndAlign(1); break; case 7: TryAndGSCookieAndAlign(1); break; case 8: Alloca(1); break; case 9: TryAndAlloca(1); break; case 10: GSCookieAndAlloca(1); break; case 11: TryAndGSCookieAndAlloca(1); break; case 12: AlignAndAlloca(1); break; case 13: TryAndAlignAndAlloca(1); break; case 14: GSCookieAndAlignAndAlloca(1); break; case 15: TryAndGSCookieAndAlignAndAlloca(1); break; case 16: BigLocals(1); break; case 17: TryAndBigLocals(1); break; case 18: GSCookieAndBigLocals(1); break; case 19: TryAndGSCookieAndBigLocals(1); break; case 20: AlignAndBigLocals(1); break; case 21: TryAndAlignAndBigLocals(1); break; case 22: GSCookieAndAlignAndBigLocals(1); break; case 23: TryAndGSCookieAndAlignAndBigLocals(1); break; case 24: AllocaAndBigLocals(1); break; case 25: TryAndAllocaAndBigLocals(1); break; case 26: GSCookieAndAllocaAndBigLocals(1); break; case 27: TryAndGSCookieAndAllocaAndBigLocals(1); break; case 28: AlignAndAllocaAndBigLocals(1); break; case 29: TryAndAlignAndAllocaAndBigLocals(1); break; case 30: GSCookieAndAlignAndAllocaAndBigLocals(1); break; case 31: TryAndGSCookieAndAlignAndAllocaAndBigLocals(1); break; } } catch (int) { caught = true; }
if (!caught) { puts("ERROR - did not catch expected thrown object"); ++failures; } }}
int main(){ __try { RunTests(); } __except (1) { puts("ERROR - Unexpectedly caught an exception"); ++failures; }
if (failures) { printf("Test failed with %d failure%s\n", failures, failures == 1 ? "" : "s"); } else { puts("Test passed"); }
return failures;}