os/kernelhwsrv/kerneltest/e32test/cppexceptions/t_romtable.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\cppexceptions\t_romtable.cpp
sl@0
    15
// Overview:
sl@0
    16
// Check accessibility and integrity of ROM exception search table.
sl@0
    17
// API Information:
sl@0
    18
// TRomHeader, TRomExceptionSearchTable, TExceptionDescriptor
sl@0
    19
// Details:	
sl@0
    20
// - Get the ROM exception search table, verify results are as expected.
sl@0
    21
// - Check that the correct exception descriptor is returned by
sl@0
    22
// GetExceptionDescriptor().
sl@0
    23
// - Check that the correct index table entry can be found.
sl@0
    24
// - Initialize the unwinder cache of a UCB with an exception descriptor
sl@0
    25
// and check we get the right result.
sl@0
    26
// - Throw and catch a variety of exceptions and verify results are as expected.
sl@0
    27
// Platforms/Drives/Compatibility:
sl@0
    28
// Hardware (Automatic).
sl@0
    29
// Assumptions/Requirement/Pre-requisites:
sl@0
    30
// Failures and causes:
sl@0
    31
// Base Port information:
sl@0
    32
// 
sl@0
    33
//
sl@0
    34
sl@0
    35
/* Environment: */
sl@0
    36
#include "unwind_env.h"
sl@0
    37
/* Language-independent unwinder declarations: */
sl@0
    38
#include "unwinder.h"
sl@0
    39
sl@0
    40
#include "symbian_support.h"
sl@0
    41
sl@0
    42
#include <f32file.h>
sl@0
    43
#include <e32test.h>
sl@0
    44
#include <e32rom.h>
sl@0
    45
#include <cpudefs.h>
sl@0
    46
sl@0
    47
GLDEF_D RTest test(_L("T_ROMTABLE"));
sl@0
    48
sl@0
    49
TRomHeader * pTheRomHeader;
sl@0
    50
char * GetExceptionDescriptor(void);
sl@0
    51
extern "C" {
sl@0
    52
  IMPORT_C TRomExceptionSearchTable * GetROMExceptionSearchTable(void);
sl@0
    53
  IMPORT_C TExceptionDescriptor * SearchEST(uint32_t addr, TRomExceptionSearchTable * aESTp);
sl@0
    54
  TExceptionDescriptor * GetRAMLoadedExceptionDescriptor(uint32_t addr);
sl@0
    55
  IMPORT_C const __EIT_entry *SearchEITV1(uint32_t return_address_offset, const __EIT_entry *base, unsigned int nelems);
sl@0
    56
  IMPORT_C __EIT_entry* SearchEITV2(uint32_t return_address, const __EIT_entry* base, unsigned int nelems);
sl@0
    57
  TExceptionDescriptor * ReLoadExceptionDescriptor(uint32_t addr, _Unwind_Control_Block * ucbp);
sl@0
    58
  IMPORT_C void InitialiseSymbianSpecificUnwinderCache(uint32_t addr, _Unwind_Control_Block * ucbp);
sl@0
    59
sl@0
    60
  void *__cxa_allocate_exception(size_t size);
sl@0
    61
  IMPORT_C __cxa_eh_globals *  __cxa_get_globals();
sl@0
    62
sl@0
    63
#ifdef _DEBUG
sl@0
    64
  IMPORT_C void DebugPrintf(const char * aFmt, ...);
sl@0
    65
#endif
sl@0
    66
}
sl@0
    67
sl@0
    68
int catcher(int x);
sl@0
    69
int catcher2(int x);
sl@0
    70
int catcher3(int x);
sl@0
    71
int catcher4(int x);
sl@0
    72
int catcher5(int x);
sl@0
    73
void TestUncaught(void);
sl@0
    74
const __EIT_entry * DumbGetEITV1(TUint32 offset, const __EIT_entry * base, TUint32 n);
sl@0
    75
__EIT_entry * DumbGetEITV2(TUint32 addr, __EIT_entry * base, TUint32 n);
sl@0
    76
sl@0
    77
GLDEF_C TInt E32Main()
sl@0
    78
    {
sl@0
    79
    test.Start(_L("Check accessibility and integrity of ROM exception search table"));
sl@0
    80
sl@0
    81
    TRomExceptionSearchTable * pEST = GetROMExceptionSearchTable();
sl@0
    82
    test.Printf(_L("ROM EST @ %08x\n"), pEST);
sl@0
    83
    test(pEST != 0);
sl@0
    84
sl@0
    85
    test.Printf(_L("ROM EST contains %d entries\n"), pEST->iNumEntries);
sl@0
    86
sl@0
    87
    for (int i=0; i<pEST->iNumEntries; i++)
sl@0
    88
        {
sl@0
    89
	TRomImageHeader * pE = (TRomImageHeader *)pEST->iEntries[i];
sl@0
    90
	TRomImageHeader * pH = pE -1;
sl@0
    91
	TUint xd = pH->iExceptionDescriptor;
sl@0
    92
	if ((xd&1)==0 || xd==0xffffffffu)
sl@0
    93
		continue;
sl@0
    94
	xd &= ~1;
sl@0
    95
	TExceptionDescriptor * pED = (TExceptionDescriptor *) ((char *)pE + xd);
sl@0
    96
	char * aExIdxBase = (char *)pED->iExIdxBase;
sl@0
    97
	char * aExIdxLimit = (char *)pED->iExIdxLimit;
sl@0
    98
	char * aROBase = (char *) pED->iROSegmentBase;
sl@0
    99
	char * aROLimit = (char *)pED->iROSegmentLimit;
sl@0
   100
sl@0
   101
	test.Printf(_L("%d\n"),i);
sl@0
   102
sl@0
   103
	test(aExIdxBase <= aExIdxLimit);
sl@0
   104
sl@0
   105
	test(aROBase <= aROLimit);
sl@0
   106
sl@0
   107
	test(aROBase <= aExIdxBase);
sl@0
   108
	test(aExIdxLimit <= aROLimit);
sl@0
   109
        }
sl@0
   110
    test.Printf(_L("\n"));
sl@0
   111
sl@0
   112
#ifdef __SUPPORT_CPP_EXCEPTIONS__
sl@0
   113
    // Check we get the right Exception Descriptor
sl@0
   114
    char * myExcpDesc = GetExceptionDescriptor();
sl@0
   115
    uint32_t addr = (uint32_t)GetExceptionDescriptor;
sl@0
   116
sl@0
   117
    test.Printf(_L("Checking &GetExceptionDescriptor [%08x] in EST range: %08x - %08x\n"), 
sl@0
   118
		addr, pEST->iEntries[0], GET_EST_FENCEPOST(pEST));
sl@0
   119
    test(addr >= pEST->iEntries[0] && addr < GET_EST_FENCEPOST(pEST));
sl@0
   120
    TExceptionDescriptor * aExcpDescP = SearchEST(addr, pEST);
sl@0
   121
    test.Printf(_L("Found entry %08x in EST\n"), aExcpDescP);
sl@0
   122
    test(aExcpDescP != NULL);
sl@0
   123
    test.Printf(_L("Check myExcpDesc[%08x] == aExcpDescP[%08x]\n"), myExcpDesc, aExcpDescP);
sl@0
   124
    test(myExcpDesc==(char*)aExcpDescP);
sl@0
   125
sl@0
   126
    // Now check we can find the right entry in the index table.
sl@0
   127
    __EIT_entry * aExIdxBase1 = (__EIT_entry *)aExcpDescP->iExIdxBase;
sl@0
   128
    __EIT_entry * aExIdxLimit1 = (__EIT_entry *)aExcpDescP->iExIdxLimit;
sl@0
   129
    unsigned int nelems = aExIdxLimit1 - aExIdxBase1;
sl@0
   130
    uint32_t aROBase1 = (uint32_t)aExcpDescP->iROSegmentBase;
sl@0
   131
    int ehabiv2 = aROBase1 & 1;
sl@0
   132
    aROBase1 = aROBase1 & EHABI_MASK;
sl@0
   133
    uint32_t aRetOffest = addr - aROBase1;
sl@0
   134
    test.Printf(_L("EHABI_V2= %d: Looking up %08x with base %08x in ExIdx @ %08x with %d entries\n"),
sl@0
   135
		ehabiv2, addr, aROBase1, aExIdxBase1, nelems);
sl@0
   136
    if (ehabiv2) {
sl@0
   137
      __EIT_entry * aResult = SearchEITV2(addr, aExIdxBase1, nelems);
sl@0
   138
      __EIT_entry * aResult1 = DumbGetEITV2(addr, aExIdxBase1, nelems);
sl@0
   139
      test.Printf(_L("Check result %08x == %08x\n"), aResult, aResult1);
sl@0
   140
      test(aResult == aResult1);
sl@0
   141
    } else {
sl@0
   142
      const __EIT_entry * aResult = SearchEITV1(aRetOffest, aExIdxBase1, nelems);
sl@0
   143
      const __EIT_entry * aResult1 = DumbGetEITV1(aRetOffest, aExIdxBase1, nelems);
sl@0
   144
      test.Printf(_L("Check result %08x == %08x\n"), aResult, aResult1);
sl@0
   145
      test(aResult == aResult1);
sl@0
   146
    }
sl@0
   147
#ifdef _DEBUG
sl@0
   148
    DebugPrintf("Exception diagnostic print support is working if you can see this!!\n\r");
sl@0
   149
#else
sl@0
   150
    test.Printf(_L("Exception diagnostic print support only available in UDEB builds - not tested\n"));
sl@0
   151
#endif
sl@0
   152
sl@0
   153
    //check we've got some eh_globals
sl@0
   154
    __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   155
    test.Printf(_L("Exception Handling globals for this thread allocated @ %08x\n"), g);
sl@0
   156
    test(g != NULL);
sl@0
   157
    test.Printf(_L("Emergency Buffer @ %08x\n"), g->emergency_buffer);
sl@0
   158
sl@0
   159
    // now initialize a the unwinder cache of a ucbp with an exception descriptor
sl@0
   160
    // and check we get the right result.
sl@0
   161
    test.Printf(_L("Allocate an empty exception object\n"));
sl@0
   162
    __cxa_exception *ep = ((__cxa_exception *)__cxa_allocate_exception(0)) - 1;
sl@0
   163
    test.Printf(_L("Empty Exception Object @ %08x UCB @ %08x\n"), ep, &ep->ucb);
sl@0
   164
    test.Printf(_L("Initialize the UCB with the EST and the current exception descriptor\n"));
sl@0
   165
    InitialiseSymbianSpecificUnwinderCache(addr, &ep->ucb);
sl@0
   166
    test.Printf(_L("Check the EST in the UCB [%08x] == %08x and the Exception Desc [%08x] == %08x\n"),
sl@0
   167
		GET_ROM_EST(&ep->ucb), pEST, GET_EXCEPTION_DESCRIPTOR(&ep->ucb), myExcpDesc);
sl@0
   168
    test(GET_ROM_EST(&ep->ucb)==pEST);
sl@0
   169
    test((char*)GET_EXCEPTION_DESCRIPTOR(&ep->ucb)== myExcpDesc);
sl@0
   170
    
sl@0
   171
    test.Printf(_L("Throwing first exception.\n"));
sl@0
   172
    int r = catcher(2);
sl@0
   173
    test.Printf(_L("Returned %d expected 2\n"), r);
sl@0
   174
    test(r==2);
sl@0
   175
sl@0
   176
    test.Printf(_L("Not throwing first exception.\n"));
sl@0
   177
    r = catcher(0);
sl@0
   178
    test.Printf(_L("Returned %d expected -1\n"), r);
sl@0
   179
    test(r==-1);
sl@0
   180
sl@0
   181
    test.Printf(_L("Throwing second exception.\n"));
sl@0
   182
    r = catcher2(3);
sl@0
   183
    test.Printf(_L("Returned %d expected 3\n"), r);
sl@0
   184
    test(r==3);
sl@0
   185
sl@0
   186
    test.Printf(_L("Not throwing second exception.\n"));
sl@0
   187
    r = catcher2(0);
sl@0
   188
    test.Printf(_L("Returned %d expected -1\n"), r);
sl@0
   189
    test(r==-1);
sl@0
   190
sl@0
   191
    test.Printf(_L("Throwing third exception.\n"));
sl@0
   192
    r = catcher3(4);
sl@0
   193
    test.Printf(_L("Returned %d expected 4\n"), r);
sl@0
   194
    test(r==4);
sl@0
   195
sl@0
   196
    test.Printf(_L("Not throwing third exception.\n"));
sl@0
   197
    r = catcher3(0);
sl@0
   198
    test.Printf(_L("Returned %d expected -1\n"), r);
sl@0
   199
    test(r==-1);
sl@0
   200
sl@0
   201
    test.Printf(_L("Throwing fourth exception.\n"));
sl@0
   202
    r = catcher4(5);
sl@0
   203
    test.Printf(_L("Returned %d expected 5\n"), r);
sl@0
   204
    test(r==5);
sl@0
   205
sl@0
   206
    test.Printf(_L("Not throwing fourth exception.\n"));
sl@0
   207
    r = catcher4(0);
sl@0
   208
    test.Printf(_L("Returned %d expected -1\n"), r);
sl@0
   209
    test(r==-1);
sl@0
   210
sl@0
   211
    test.Printf(_L("Throwing fifth exception.\n"));
sl@0
   212
    r = catcher5(6);
sl@0
   213
    test.Printf(_L("Returned %d expected 6\n"), r);
sl@0
   214
    test(r==6);
sl@0
   215
sl@0
   216
    test.Printf(_L("Not throwing fifth exception.\n"));
sl@0
   217
    r = catcher5(0);
sl@0
   218
    test.Printf(_L("Returned %d expected -1\n"), r);
sl@0
   219
    test(r==-1);
sl@0
   220
sl@0
   221
    test.Printf(_L("Testing std::uncaught_exception.\n"));
sl@0
   222
    TestUncaught();
sl@0
   223
#endif
sl@0
   224
sl@0
   225
	test.End();
sl@0
   226
	test.Close();
sl@0
   227
    return 0;
sl@0
   228
    }
sl@0
   229
sl@0
   230
#ifdef __ARMCC__
sl@0
   231
// We rely on this immediately following E32main. DONT MOVE IT
sl@0
   232
__asm char * GetExceptionDescriptor(void) {
sl@0
   233
  extern |Symbian$$CPP$$Exception$$Descriptor|
sl@0
   234
  ldr r0, theExceptionDesc
sl@0
   235
sl@0
   236
#ifdef __SUPPORT_THUMB_INTERWORKING
sl@0
   237
  bx lr
sl@0
   238
#else
sl@0
   239
  mov pc, lr
sl@0
   240
#endif
sl@0
   241
sl@0
   242
theExceptionDesc dcd |Symbian$$CPP$$Exception$$Descriptor|
sl@0
   243
}
sl@0
   244
#endif
sl@0
   245
sl@0
   246
const __EIT_entry * DumbGetEITV1(TUint32 offset, const __EIT_entry * base, TUint32 n) {
sl@0
   247
  if (n && offset < base[0].fnoffset) return 0;
sl@0
   248
  for (int i=0; i<n; i++) {
sl@0
   249
    // check for last entry 
sl@0
   250
    if (i == n-1 && base[i].fnoffset <= offset) return &base[i];
sl@0
   251
    if (base[i].fnoffset <= offset && offset < base[i+1].fnoffset) return &base[i];
sl@0
   252
  }
sl@0
   253
  return 0;
sl@0
   254
}
sl@0
   255
sl@0
   256
static uint32_t __ARM_resolve_prel31(void *p)
sl@0
   257
{
sl@0
   258
  return (uint32_t)((((*(int32_t *)p) << 1) >> 1) + (int32_t)p);
sl@0
   259
}
sl@0
   260
sl@0
   261
__EIT_entry * DumbGetEITV2(TUint32 addr, __EIT_entry * base, TUint32 n) {
sl@0
   262
  if (n && (addr < __ARM_resolve_prel31(&base[0].fnoffset)))
sl@0
   263
    return 0;
sl@0
   264
  for (int i=0; i<n; i++) {
sl@0
   265
    // check for last entry 
sl@0
   266
    if (i == n-1 && __ARM_resolve_prel31(&base[i].fnoffset) <= addr) 
sl@0
   267
      return &base[i];
sl@0
   268
    if ((__ARM_resolve_prel31(&base[i].fnoffset) <= addr) && 
sl@0
   269
	(addr < __ARM_resolve_prel31(&base[i+1].fnoffset)))
sl@0
   270
      return &base[i];
sl@0
   271
  }
sl@0
   272
  return 0;
sl@0
   273
}
sl@0
   274
    
sl@0
   275
#ifdef __SUPPORT_CPP_EXCEPTIONS__
sl@0
   276
sl@0
   277
class MyFirstException {
sl@0
   278
public:
sl@0
   279
  MyFirstException(int x) { iVal = x; };
sl@0
   280
  virtual ~MyFirstException();
sl@0
   281
  int iVal;
sl@0
   282
};
sl@0
   283
sl@0
   284
MyFirstException::~MyFirstException(){}
sl@0
   285
sl@0
   286
int thrower (int x) {
sl@0
   287
  if (x != 0) throw MyFirstException(x);
sl@0
   288
  return -1;
sl@0
   289
}
sl@0
   290
sl@0
   291
int catcher(int x) {
sl@0
   292
  try {
sl@0
   293
    return thrower(x);
sl@0
   294
  }
sl@0
   295
  catch(MyFirstException& e) 
sl@0
   296
    {
sl@0
   297
      return e.iVal;
sl@0
   298
    }
sl@0
   299
}
sl@0
   300
sl@0
   301
sl@0
   302
#include "second_excp.h"
sl@0
   303
sl@0
   304
sl@0
   305
int catcher2(int x) {
sl@0
   306
  try {
sl@0
   307
    return thrower2(x);
sl@0
   308
  }
sl@0
   309
  catch(MySecondException& e) 
sl@0
   310
    {
sl@0
   311
      return e.iVal;
sl@0
   312
    }
sl@0
   313
}
sl@0
   314
sl@0
   315
int catcher3(int x) {
sl@0
   316
  try {
sl@0
   317
    return thrower3(x);
sl@0
   318
  }
sl@0
   319
  catch(MyThirdException& e) 
sl@0
   320
    {
sl@0
   321
      return e.iVal;
sl@0
   322
    }
sl@0
   323
}
sl@0
   324
sl@0
   325
int catcher4(int x) {
sl@0
   326
  try {
sl@0
   327
    return thrower4(x);
sl@0
   328
  }
sl@0
   329
  catch(MyFourthException& e) 
sl@0
   330
    {
sl@0
   331
      return e.iVal;
sl@0
   332
    }
sl@0
   333
}
sl@0
   334
sl@0
   335
int catcher5(int x) {
sl@0
   336
  try {
sl@0
   337
    return thrower5(x);
sl@0
   338
  }
sl@0
   339
  catch(MyFifthException& e) 
sl@0
   340
    {
sl@0
   341
      return e.iVal;
sl@0
   342
    }
sl@0
   343
}
sl@0
   344
sl@0
   345
void TestUncaught(void) {
sl@0
   346
  TInt x = 0;
sl@0
   347
  try {
sl@0
   348
    UncaughtTester aTester(x);
sl@0
   349
    test.Printf(_L("Check throw case\n"));
sl@0
   350
    thrower(0);
sl@0
   351
  }
sl@0
   352
  catch(MyFirstException& e) 
sl@0
   353
    {
sl@0
   354
      test.Printf(_L("~Check x == 0\n"));
sl@0
   355
      test(x == 0);
sl@0
   356
    }
sl@0
   357
  try {
sl@0
   358
    UncaughtTester aTester(x);
sl@0
   359
    test.Printf(_L("Check no throw case\n"));
sl@0
   360
  }
sl@0
   361
  catch(MyFirstException& e) 
sl@0
   362
    {
sl@0
   363
      test.Printf(_L("Whoops!!!\n"));
sl@0
   364
    }
sl@0
   365
  test(x==1);
sl@0
   366
}
sl@0
   367
sl@0
   368
sl@0
   369
#endif
sl@0
   370
sl@0
   371
sl@0
   372
sl@0
   373
sl@0
   374
sl@0
   375
sl@0
   376