First public contribution.
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\cppexceptions\t_romtable.cpp
16 // Check accessibility and integrity of ROM exception search table.
18 // TRomHeader, TRomExceptionSearchTable, TExceptionDescriptor
20 // - Get the ROM exception search table, verify results are as expected.
21 // - Check that the correct exception descriptor is returned by
22 // GetExceptionDescriptor().
23 // - Check that the correct index table entry can be found.
24 // - Initialize the unwinder cache of a UCB with an exception descriptor
25 // and check we get the right result.
26 // - Throw and catch a variety of exceptions and verify results are as expected.
27 // Platforms/Drives/Compatibility:
28 // Hardware (Automatic).
29 // Assumptions/Requirement/Pre-requisites:
30 // Failures and causes:
31 // Base Port information:
36 #include "unwind_env.h"
37 /* Language-independent unwinder declarations: */
40 #include "symbian_support.h"
47 GLDEF_D RTest test(_L("T_ROMTABLE"));
49 TRomHeader * pTheRomHeader;
50 char * GetExceptionDescriptor(void);
52 IMPORT_C TRomExceptionSearchTable * GetROMExceptionSearchTable(void);
53 IMPORT_C TExceptionDescriptor * SearchEST(uint32_t addr, TRomExceptionSearchTable * aESTp);
54 TExceptionDescriptor * GetRAMLoadedExceptionDescriptor(uint32_t addr);
55 IMPORT_C const __EIT_entry *SearchEITV1(uint32_t return_address_offset, const __EIT_entry *base, unsigned int nelems);
56 IMPORT_C __EIT_entry* SearchEITV2(uint32_t return_address, const __EIT_entry* base, unsigned int nelems);
57 TExceptionDescriptor * ReLoadExceptionDescriptor(uint32_t addr, _Unwind_Control_Block * ucbp);
58 IMPORT_C void InitialiseSymbianSpecificUnwinderCache(uint32_t addr, _Unwind_Control_Block * ucbp);
60 void *__cxa_allocate_exception(size_t size);
61 IMPORT_C __cxa_eh_globals * __cxa_get_globals();
64 IMPORT_C void DebugPrintf(const char * aFmt, ...);
73 void TestUncaught(void);
74 const __EIT_entry * DumbGetEITV1(TUint32 offset, const __EIT_entry * base, TUint32 n);
75 __EIT_entry * DumbGetEITV2(TUint32 addr, __EIT_entry * base, TUint32 n);
77 GLDEF_C TInt E32Main()
79 test.Start(_L("Check accessibility and integrity of ROM exception search table"));
81 TRomExceptionSearchTable * pEST = GetROMExceptionSearchTable();
82 test.Printf(_L("ROM EST @ %08x\n"), pEST);
85 test.Printf(_L("ROM EST contains %d entries\n"), pEST->iNumEntries);
87 for (int i=0; i<pEST->iNumEntries; i++)
89 TRomImageHeader * pE = (TRomImageHeader *)pEST->iEntries[i];
90 TRomImageHeader * pH = pE -1;
91 TUint xd = pH->iExceptionDescriptor;
92 if ((xd&1)==0 || xd==0xffffffffu)
95 TExceptionDescriptor * pED = (TExceptionDescriptor *) ((char *)pE + xd);
96 char * aExIdxBase = (char *)pED->iExIdxBase;
97 char * aExIdxLimit = (char *)pED->iExIdxLimit;
98 char * aROBase = (char *) pED->iROSegmentBase;
99 char * aROLimit = (char *)pED->iROSegmentLimit;
101 test.Printf(_L("%d\n"),i);
103 test(aExIdxBase <= aExIdxLimit);
105 test(aROBase <= aROLimit);
107 test(aROBase <= aExIdxBase);
108 test(aExIdxLimit <= aROLimit);
110 test.Printf(_L("\n"));
112 #ifdef __SUPPORT_CPP_EXCEPTIONS__
113 // Check we get the right Exception Descriptor
114 char * myExcpDesc = GetExceptionDescriptor();
115 uint32_t addr = (uint32_t)GetExceptionDescriptor;
117 test.Printf(_L("Checking &GetExceptionDescriptor [%08x] in EST range: %08x - %08x\n"),
118 addr, pEST->iEntries[0], GET_EST_FENCEPOST(pEST));
119 test(addr >= pEST->iEntries[0] && addr < GET_EST_FENCEPOST(pEST));
120 TExceptionDescriptor * aExcpDescP = SearchEST(addr, pEST);
121 test.Printf(_L("Found entry %08x in EST\n"), aExcpDescP);
122 test(aExcpDescP != NULL);
123 test.Printf(_L("Check myExcpDesc[%08x] == aExcpDescP[%08x]\n"), myExcpDesc, aExcpDescP);
124 test(myExcpDesc==(char*)aExcpDescP);
126 // Now check we can find the right entry in the index table.
127 __EIT_entry * aExIdxBase1 = (__EIT_entry *)aExcpDescP->iExIdxBase;
128 __EIT_entry * aExIdxLimit1 = (__EIT_entry *)aExcpDescP->iExIdxLimit;
129 unsigned int nelems = aExIdxLimit1 - aExIdxBase1;
130 uint32_t aROBase1 = (uint32_t)aExcpDescP->iROSegmentBase;
131 int ehabiv2 = aROBase1 & 1;
132 aROBase1 = aROBase1 & EHABI_MASK;
133 uint32_t aRetOffest = addr - aROBase1;
134 test.Printf(_L("EHABI_V2= %d: Looking up %08x with base %08x in ExIdx @ %08x with %d entries\n"),
135 ehabiv2, addr, aROBase1, aExIdxBase1, nelems);
137 __EIT_entry * aResult = SearchEITV2(addr, aExIdxBase1, nelems);
138 __EIT_entry * aResult1 = DumbGetEITV2(addr, aExIdxBase1, nelems);
139 test.Printf(_L("Check result %08x == %08x\n"), aResult, aResult1);
140 test(aResult == aResult1);
142 const __EIT_entry * aResult = SearchEITV1(aRetOffest, aExIdxBase1, nelems);
143 const __EIT_entry * aResult1 = DumbGetEITV1(aRetOffest, aExIdxBase1, nelems);
144 test.Printf(_L("Check result %08x == %08x\n"), aResult, aResult1);
145 test(aResult == aResult1);
148 DebugPrintf("Exception diagnostic print support is working if you can see this!!\n\r");
150 test.Printf(_L("Exception diagnostic print support only available in UDEB builds - not tested\n"));
153 //check we've got some eh_globals
154 __cxa_eh_globals *g = __cxa_get_globals();
155 test.Printf(_L("Exception Handling globals for this thread allocated @ %08x\n"), g);
157 test.Printf(_L("Emergency Buffer @ %08x\n"), g->emergency_buffer);
159 // now initialize a the unwinder cache of a ucbp with an exception descriptor
160 // and check we get the right result.
161 test.Printf(_L("Allocate an empty exception object\n"));
162 __cxa_exception *ep = ((__cxa_exception *)__cxa_allocate_exception(0)) - 1;
163 test.Printf(_L("Empty Exception Object @ %08x UCB @ %08x\n"), ep, &ep->ucb);
164 test.Printf(_L("Initialize the UCB with the EST and the current exception descriptor\n"));
165 InitialiseSymbianSpecificUnwinderCache(addr, &ep->ucb);
166 test.Printf(_L("Check the EST in the UCB [%08x] == %08x and the Exception Desc [%08x] == %08x\n"),
167 GET_ROM_EST(&ep->ucb), pEST, GET_EXCEPTION_DESCRIPTOR(&ep->ucb), myExcpDesc);
168 test(GET_ROM_EST(&ep->ucb)==pEST);
169 test((char*)GET_EXCEPTION_DESCRIPTOR(&ep->ucb)== myExcpDesc);
171 test.Printf(_L("Throwing first exception.\n"));
173 test.Printf(_L("Returned %d expected 2\n"), r);
176 test.Printf(_L("Not throwing first exception.\n"));
178 test.Printf(_L("Returned %d expected -1\n"), r);
181 test.Printf(_L("Throwing second exception.\n"));
183 test.Printf(_L("Returned %d expected 3\n"), r);
186 test.Printf(_L("Not throwing second exception.\n"));
188 test.Printf(_L("Returned %d expected -1\n"), r);
191 test.Printf(_L("Throwing third exception.\n"));
193 test.Printf(_L("Returned %d expected 4\n"), r);
196 test.Printf(_L("Not throwing third exception.\n"));
198 test.Printf(_L("Returned %d expected -1\n"), r);
201 test.Printf(_L("Throwing fourth exception.\n"));
203 test.Printf(_L("Returned %d expected 5\n"), r);
206 test.Printf(_L("Not throwing fourth exception.\n"));
208 test.Printf(_L("Returned %d expected -1\n"), r);
211 test.Printf(_L("Throwing fifth exception.\n"));
213 test.Printf(_L("Returned %d expected 6\n"), r);
216 test.Printf(_L("Not throwing fifth exception.\n"));
218 test.Printf(_L("Returned %d expected -1\n"), r);
221 test.Printf(_L("Testing std::uncaught_exception.\n"));
231 // We rely on this immediately following E32main. DONT MOVE IT
232 __asm char * GetExceptionDescriptor(void) {
233 extern |Symbian$$CPP$$Exception$$Descriptor|
234 ldr r0, theExceptionDesc
236 #ifdef __SUPPORT_THUMB_INTERWORKING
242 theExceptionDesc dcd |Symbian$$CPP$$Exception$$Descriptor|
246 const __EIT_entry * DumbGetEITV1(TUint32 offset, const __EIT_entry * base, TUint32 n) {
247 if (n && offset < base[0].fnoffset) return 0;
248 for (int i=0; i<n; i++) {
249 // check for last entry
250 if (i == n-1 && base[i].fnoffset <= offset) return &base[i];
251 if (base[i].fnoffset <= offset && offset < base[i+1].fnoffset) return &base[i];
256 static uint32_t __ARM_resolve_prel31(void *p)
258 return (uint32_t)((((*(int32_t *)p) << 1) >> 1) + (int32_t)p);
261 __EIT_entry * DumbGetEITV2(TUint32 addr, __EIT_entry * base, TUint32 n) {
262 if (n && (addr < __ARM_resolve_prel31(&base[0].fnoffset)))
264 for (int i=0; i<n; i++) {
265 // check for last entry
266 if (i == n-1 && __ARM_resolve_prel31(&base[i].fnoffset) <= addr)
268 if ((__ARM_resolve_prel31(&base[i].fnoffset) <= addr) &&
269 (addr < __ARM_resolve_prel31(&base[i+1].fnoffset)))
275 #ifdef __SUPPORT_CPP_EXCEPTIONS__
277 class MyFirstException {
279 MyFirstException(int x) { iVal = x; };
280 virtual ~MyFirstException();
284 MyFirstException::~MyFirstException(){}
286 int thrower (int x) {
287 if (x != 0) throw MyFirstException(x);
295 catch(MyFirstException& e)
302 #include "second_excp.h"
305 int catcher2(int x) {
309 catch(MySecondException& e)
315 int catcher3(int x) {
319 catch(MyThirdException& e)
325 int catcher4(int x) {
329 catch(MyFourthException& e)
335 int catcher5(int x) {
339 catch(MyFifthException& e)
345 void TestUncaught(void) {
348 UncaughtTester aTester(x);
349 test.Printf(_L("Check throw case\n"));
352 catch(MyFirstException& e)
354 test.Printf(_L("~Check x == 0\n"));
358 UncaughtTester aTester(x);
359 test.Printf(_L("Check no throw case\n"));
361 catch(MyFirstException& e)
363 test.Printf(_L("Whoops!!!\n"));