os/kernelhwsrv/kerneltest/e32test/system/t_trap.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1994-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\system\t_trap.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test TRAP, Leave and Assert
sl@0
    17
// API Information:
sl@0
    18
// TRAP, User::Leave, __ASSERT_DEBUG_NO_LEAVE, __ASSERT_ALWAYS_NO_LEAVE
sl@0
    19
// Details:
sl@0
    20
// - Test TRAP macro works as expected.
sl@0
    21
// - Test User::Leave works as expected including leave from
sl@0
    22
// within nested calls.
sl@0
    23
// - Verify that a leave without a TRAP causes the thread to panic.
sl@0
    24
// - Create a thread that asserts and verify the exit type and other
sl@0
    25
// results are as expected.
sl@0
    26
// Platforms/Drives/Compatibility:
sl@0
    27
// All.
sl@0
    28
// Assumptions/Requirement/Pre-requisites:
sl@0
    29
// Failures and causes:
sl@0
    30
// Base Port information:
sl@0
    31
// 
sl@0
    32
//
sl@0
    33
sl@0
    34
#include <e32test.h>
sl@0
    35
#include <e32panic.h>
sl@0
    36
sl@0
    37
const TInt KLeaveVal=1111;
sl@0
    38
const TInt KUnLeaveVal=2222;
sl@0
    39
const TInt KRecursiveUnLeaveVal=3333;
sl@0
    40
const TInt KRecursiveSingleLeaveVal=4444;
sl@0
    41
const TInt KMaxDepth=20;
sl@0
    42
sl@0
    43
//#define __TEST_BREAKPOINT_IN_TRAP__
sl@0
    44
sl@0
    45
LOCAL_D RTest test(_L("T_TRAP"));
sl@0
    46
sl@0
    47
sl@0
    48
LOCAL_C TInt UnLeaveFunction(void)
sl@0
    49
	{
sl@0
    50
sl@0
    51
	return(KUnLeaveVal);
sl@0
    52
	}
sl@0
    53
sl@0
    54
LOCAL_C TInt LeaveFunction(void)
sl@0
    55
	{
sl@0
    56
sl@0
    57
	User::Leave(KLeaveVal);
sl@0
    58
	return(0);
sl@0
    59
	}
sl@0
    60
sl@0
    61
LOCAL_C TInt RecursiveUnLeave(TInt level)
sl@0
    62
	{
sl@0
    63
sl@0
    64
	if (level==0)
sl@0
    65
		return(KRecursiveUnLeaveVal);
sl@0
    66
	else
sl@0
    67
		return(RecursiveUnLeave(--level));
sl@0
    68
	}
sl@0
    69
sl@0
    70
LOCAL_C TInt RecursiveSingleLeave(TInt level)
sl@0
    71
	{
sl@0
    72
sl@0
    73
	if (level==0)
sl@0
    74
		User::Leave(KRecursiveSingleLeaveVal);
sl@0
    75
	else
sl@0
    76
		RecursiveSingleLeave(--level);
sl@0
    77
	return(0);
sl@0
    78
	}
sl@0
    79
sl@0
    80
LOCAL_C TInt RecursiveMultiLeave1(TInt level)
sl@0
    81
	{
sl@0
    82
sl@0
    83
	TInt ret=0;
sl@0
    84
	TRAP(ret,{if (level==0)	User::Leave(level);	else ret=RecursiveMultiLeave1(level-1);	test(EFalse);})
sl@0
    85
	test(ret==level);
sl@0
    86
	User::Leave(level+1);
sl@0
    87
	return(0);
sl@0
    88
	}
sl@0
    89
sl@0
    90
LOCAL_C TInt RecursiveMultiLeave2(TInt level)
sl@0
    91
	{
sl@0
    92
sl@0
    93
	if (level==0)
sl@0
    94
		return(1);
sl@0
    95
	TInt ret=0;
sl@0
    96
	TRAP(ret,ret=RecursiveMultiLeave2(level-1))
sl@0
    97
	test(ret==level);
sl@0
    98
	User::Leave(level+1);
sl@0
    99
	return(0);
sl@0
   100
	}
sl@0
   101
	
sl@0
   102
LOCAL_C TInt doTrap(TInt aVal)
sl@0
   103
//
sl@0
   104
// Nest trap function.
sl@0
   105
//
sl@0
   106
	{
sl@0
   107
sl@0
   108
	if (aVal)
sl@0
   109
		{
sl@0
   110
		TInt j=(-1);
sl@0
   111
		TRAP(j,j=doTrap(aVal-1))
sl@0
   112
		test(j==aVal);
sl@0
   113
		}
sl@0
   114
	return(aVal+1);
sl@0
   115
	}
sl@0
   116
sl@0
   117
#ifdef __TEST_BREAKPOINT_IN_TRAP__
sl@0
   118
void bkpt()
sl@0
   119
	{
sl@0
   120
	__BREAKPOINT();
sl@0
   121
	}
sl@0
   122
#endif
sl@0
   123
sl@0
   124
LOCAL_C void doLeave(TInt aLevel,TInt aVal)
sl@0
   125
//
sl@0
   126
// Nest trap with leave function.
sl@0
   127
//
sl@0
   128
	{
sl@0
   129
sl@0
   130
	if (aLevel)
sl@0
   131
		doLeave(aLevel-1,aVal);
sl@0
   132
	else
sl@0
   133
		User::Leave(aVal);
sl@0
   134
	}
sl@0
   135
sl@0
   136
LOCAL_C void testTrap()
sl@0
   137
//
sl@0
   138
// Test trap functions O.K.
sl@0
   139
//
sl@0
   140
	{
sl@0
   141
sl@0
   142
	test.Start(_L("Trap level 1"));
sl@0
   143
//
sl@0
   144
	TInt i=2;
sl@0
   145
	TRAP(i,i=1);
sl@0
   146
	test(i==1);
sl@0
   147
#ifdef __TEST_BREAKPOINT_IN_TRAP__
sl@0
   148
	TRAP(i,bkpt());
sl@0
   149
	TRAP(i,TRAP(i,bkpt()));
sl@0
   150
#endif
sl@0
   151
//
sl@0
   152
	test.Next(_L("Trap level n"));
sl@0
   153
	for (i=1;i<KMaxDepth;i++)
sl@0
   154
		test(doTrap(i)==(i+1));
sl@0
   155
//
sl@0
   156
	test.End();
sl@0
   157
	}
sl@0
   158
sl@0
   159
LOCAL_C void testLeave()
sl@0
   160
//
sl@0
   161
// Test leave functions O.K.
sl@0
   162
//
sl@0
   163
	{
sl@0
   164
sl@0
   165
	test.Start(_L("Leave level 1"));
sl@0
   166
//
sl@0
   167
	TInt i=0;
sl@0
   168
	TRAP(i,User::Leave(2))
sl@0
   169
	test(i==2);
sl@0
   170
//
sl@0
   171
	test.Next(_L("Leave level 2"));
sl@0
   172
	i=0;
sl@0
   173
	TRAP(i,TRAP(i,User::Leave(3)))
sl@0
   174
	test(i==3);
sl@0
   175
//
sl@0
   176
#ifdef __TEST_BREAKPOINT_IN_TRAP__
sl@0
   177
	TRAP(i,TRAP(i,User::Leave(33)); bkpt())
sl@0
   178
	test(i==33);
sl@0
   179
#endif
sl@0
   180
//
sl@0
   181
	test.Next(_L("Leave from nested calls"));
sl@0
   182
	for (i=1;i<KMaxDepth;i++)
sl@0
   183
		{
sl@0
   184
		TInt j=(-1);
sl@0
   185
		TRAP(j,doLeave(i,i))
sl@0
   186
		test(j==i);
sl@0
   187
		}
sl@0
   188
//
sl@0
   189
	test.End();
sl@0
   190
	}
sl@0
   191
sl@0
   192
LOCAL_C void testMH(void)
sl@0
   193
	{
sl@0
   194
sl@0
   195
	TInt ret=0;
sl@0
   196
	TRAP(ret,ret=UnLeaveFunction())
sl@0
   197
	test(ret==KUnLeaveVal);
sl@0
   198
	TRAP(ret,LeaveFunction())
sl@0
   199
	test(ret==KLeaveVal);
sl@0
   200
	TInt i=0;
sl@0
   201
	for(;i<=KMaxDepth;i++)
sl@0
   202
		{
sl@0
   203
		TRAP(ret,ret=RecursiveUnLeave(i))
sl@0
   204
		test(ret==KRecursiveUnLeaveVal);
sl@0
   205
		}
sl@0
   206
	for(i=0;i<=KMaxDepth;i++)
sl@0
   207
		{
sl@0
   208
		TRAP(ret,ret=RecursiveSingleLeave(i))
sl@0
   209
		test(ret==KRecursiveSingleLeaveVal);
sl@0
   210
		}
sl@0
   211
	for(i=0;i<=KMaxDepth;i++)
sl@0
   212
		{
sl@0
   213
		TRAP(ret,ret=RecursiveMultiLeave1(i))
sl@0
   214
		test(ret==i+1);
sl@0
   215
		}
sl@0
   216
	for(i=0;i<=KMaxDepth;i++)
sl@0
   217
		{
sl@0
   218
		TRAP(ret,ret=RecursiveMultiLeave2(i))
sl@0
   219
		test(ret==i+1);
sl@0
   220
		}
sl@0
   221
	}
sl@0
   222
sl@0
   223
TInt LeaveNoTrapThread(TAny*)
sl@0
   224
	{
sl@0
   225
	User::Leave(KErrGeneral);
sl@0
   226
	return KErrNone;
sl@0
   227
	}
sl@0
   228
sl@0
   229
void TestLeaveNoTrap()
sl@0
   230
	{
sl@0
   231
	RThread thread;
sl@0
   232
	TInt r=thread.Create(_L("Leave without Trap thread"),LeaveNoTrapThread,0x1000,&User::Allocator(),NULL);
sl@0
   233
	test(r==KErrNone);
sl@0
   234
	TRequestStatus stat;
sl@0
   235
	thread.Logon(stat);
sl@0
   236
	test(stat==KRequestPending);
sl@0
   237
	TBool justInTime=User::JustInTime();
sl@0
   238
	User::SetJustInTime(EFalse);
sl@0
   239
	thread.Resume();
sl@0
   240
	User::WaitForRequest(stat);
sl@0
   241
	User::SetJustInTime(justInTime);
sl@0
   242
	test(thread.ExitType()==EExitPanic);
sl@0
   243
	test(thread.ExitReason()==EUserLeaveWithoutTrap);
sl@0
   244
	test(thread.ExitCategory()==_L("USER"));
sl@0
   245
	CLOSE_AND_WAIT(thread);
sl@0
   246
	}
sl@0
   247
sl@0
   248
enum TAssertTest
sl@0
   249
	{
sl@0
   250
	EAssertTest_Debug = 1,
sl@0
   251
	EAssertTest_Leave = 2,
sl@0
   252
	EAssertTest_Ret = 4,
sl@0
   253
	};
sl@0
   254
sl@0
   255
TInt AssertThread(TAny* a)
sl@0
   256
	{
sl@0
   257
	TInt f = (TInt)a;
sl@0
   258
	TInt r = f | EAssertTest_Ret;
sl@0
   259
	if (f & EAssertTest_Leave)
sl@0
   260
		{
sl@0
   261
		if (f & EAssertTest_Debug)
sl@0
   262
			{
sl@0
   263
			__ASSERT_DEBUG_NO_LEAVE(User::Leave(r));
sl@0
   264
			}
sl@0
   265
		else
sl@0
   266
			{
sl@0
   267
			__ASSERT_ALWAYS_NO_LEAVE(User::Leave(r));
sl@0
   268
			}
sl@0
   269
		}
sl@0
   270
	else
sl@0
   271
		{
sl@0
   272
		if (f & EAssertTest_Debug)
sl@0
   273
			{
sl@0
   274
			__ASSERT_DEBUG_NO_LEAVE(RThread().Terminate(r));
sl@0
   275
			}
sl@0
   276
		else
sl@0
   277
			{
sl@0
   278
			__ASSERT_ALWAYS_NO_LEAVE(RThread().Terminate(r));
sl@0
   279
			}
sl@0
   280
		}
sl@0
   281
	return r;
sl@0
   282
	}
sl@0
   283
sl@0
   284
TInt _AssertThread(TAny* a)
sl@0
   285
	{
sl@0
   286
	TInt s=0;
sl@0
   287
	TRAP_IGNORE(s=AssertThread(a));
sl@0
   288
	return s;
sl@0
   289
	}
sl@0
   290
sl@0
   291
void TestAssert(TInt aTest)
sl@0
   292
	{
sl@0
   293
	test.Printf(_L("Assert %d\n"), aTest);
sl@0
   294
	RThread t;
sl@0
   295
	TInt r = t.Create(_L("assert"), &_AssertThread, 0x1000, NULL, (TAny*)aTest);
sl@0
   296
	test(r==KErrNone);
sl@0
   297
	TRequestStatus s;
sl@0
   298
	t.Logon(s);
sl@0
   299
	test(s==KRequestPending);
sl@0
   300
	TBool jit = User::JustInTime();
sl@0
   301
	User::SetJustInTime(EFalse);
sl@0
   302
	t.Resume();
sl@0
   303
	User::WaitForRequest(s);
sl@0
   304
	User::SetJustInTime(jit);
sl@0
   305
	TInt exitType = t.ExitType();
sl@0
   306
	TInt exitReason = t.ExitReason();
sl@0
   307
	const TDesC& exitCat = t.ExitCategory();
sl@0
   308
	CLOSE_AND_WAIT(t);
sl@0
   309
	test.Printf(_L("Exit %d,%d,%S\n"), exitType, exitReason, &exitCat);
sl@0
   310
	if (aTest & EAssertTest_Leave)
sl@0
   311
		{
sl@0
   312
		if (aTest & EAssertTest_Debug)
sl@0
   313
			{
sl@0
   314
#ifdef _DEBUG
sl@0
   315
			test(exitType == EExitPanic);
sl@0
   316
			test(exitReason == EUnexpectedLeave);
sl@0
   317
#else
sl@0
   318
			test(exitType == EExitKill);
sl@0
   319
			test(exitReason == KErrNone);
sl@0
   320
#endif
sl@0
   321
			}
sl@0
   322
		else
sl@0
   323
			{
sl@0
   324
			test(exitType == EExitPanic);
sl@0
   325
			test(exitReason == EUnexpectedLeave);
sl@0
   326
			}
sl@0
   327
		}
sl@0
   328
	else
sl@0
   329
		{
sl@0
   330
		test(exitType == EExitTerminate);
sl@0
   331
		test(exitReason == (aTest | EAssertTest_Ret));
sl@0
   332
		}
sl@0
   333
	}
sl@0
   334
sl@0
   335
/*============== server for testing exceptions in TRAP implementation ====================*/
sl@0
   336
sl@0
   337
#include <e32base.h>
sl@0
   338
#include <e32base_private.h>
sl@0
   339
sl@0
   340
#include "../mmu/mmudetect.h"
sl@0
   341
sl@0
   342
const TInt KHeapSize=0x2000;
sl@0
   343
sl@0
   344
_LIT(KServerName,"Display");
sl@0
   345
sl@0
   346
class CMySession : public CSession2
sl@0
   347
	{
sl@0
   348
public:
sl@0
   349
	CMySession();
sl@0
   350
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
   351
	};
sl@0
   352
sl@0
   353
class CMyServer : public CServer2
sl@0
   354
	{
sl@0
   355
public:
sl@0
   356
	enum {ERead,EStop};
sl@0
   357
public:
sl@0
   358
	CMyServer(TInt aPriority);
sl@0
   359
	static CMyServer* New(TInt aPriority);
sl@0
   360
	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;//Overloading
sl@0
   361
	};
sl@0
   362
sl@0
   363
class RDisplay : public RSessionBase
sl@0
   364
	{
sl@0
   365
public:
sl@0
   366
	TInt Open();
sl@0
   367
	void Read(TRequestStatus& aStatus);
sl@0
   368
	TInt Stop();
sl@0
   369
	};
sl@0
   370
sl@0
   371
LOCAL_D RTest testSvr(_L("T_TRAP Server"));
sl@0
   372
LOCAL_D RSemaphore client;
sl@0
   373
LOCAL_D RSemaphore server;
sl@0
   374
LOCAL_D RDisplay display;
sl@0
   375
LOCAL_D const RMessage2* message;
sl@0
   376
sl@0
   377
// Constructor
sl@0
   378
//
sl@0
   379
// 
sl@0
   380
CMySession::CMySession()
sl@0
   381
	{}
sl@0
   382
sl@0
   383
CMyServer* CMyServer::New(TInt aPriority)
sl@0
   384
//
sl@0
   385
// Create a new CMyServer.
sl@0
   386
//
sl@0
   387
	{
sl@0
   388
	return new CMyServer(aPriority);
sl@0
   389
	}
sl@0
   390
sl@0
   391
CMyServer::CMyServer(TInt aPriority)
sl@0
   392
//
sl@0
   393
// Constructor.
sl@0
   394
//
sl@0
   395
	: CServer2(aPriority)
sl@0
   396
	{}
sl@0
   397
sl@0
   398
CSession2* CMyServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2&) const
sl@0
   399
//
sl@0
   400
// Create a new client for this server.
sl@0
   401
//
sl@0
   402
	{
sl@0
   403
	return(new(ELeave) CMySession());
sl@0
   404
	}
sl@0
   405
sl@0
   406
void CMySession::ServiceL(const RMessage2& aMessage)
sl@0
   407
//
sl@0
   408
// Handle messages for this server.
sl@0
   409
//
sl@0
   410
	{
sl@0
   411
	TInt r=KErrNone;
sl@0
   412
	switch (aMessage.Function())
sl@0
   413
		{
sl@0
   414
	case CMyServer::ERead:
sl@0
   415
		testSvr.Printf(_L("read message received\n"));
sl@0
   416
		if (HaveVirtMem())
sl@0
   417
			{
sl@0
   418
			message = &aMessage;
sl@0
   419
			}
sl@0
   420
		client.Signal();
sl@0
   421
		server.Wait();
sl@0
   422
		break;
sl@0
   423
	case CMyServer::EStop:
sl@0
   424
		testSvr.Printf(_L("stop message received\n"));
sl@0
   425
		CActiveScheduler::Stop();
sl@0
   426
		break;
sl@0
   427
	default:
sl@0
   428
		r=KErrNotSupported;
sl@0
   429
		}
sl@0
   430
	aMessage.Complete(r);
sl@0
   431
	}
sl@0
   432
sl@0
   433
TInt RDisplay::Open()
sl@0
   434
//
sl@0
   435
// Open the server.
sl@0
   436
//
sl@0
   437
	{
sl@0
   438
	return(CreateSession(KServerName,TVersion(),1));
sl@0
   439
	}
sl@0
   440
sl@0
   441
void RDisplay::Read(TRequestStatus& aStatus)
sl@0
   442
//
sl@0
   443
// Get session to test CSession2::ReadL.
sl@0
   444
//
sl@0
   445
	{
sl@0
   446
	TBuf<0x10>* bad = (TBuf<0x10> *)(0x30000000);
sl@0
   447
	SendReceive(CMyServer::ERead, TIpcArgs(bad), aStatus);
sl@0
   448
	}
sl@0
   449
sl@0
   450
TInt RDisplay::Stop()
sl@0
   451
//
sl@0
   452
// Stop the server.
sl@0
   453
//
sl@0
   454
	{
sl@0
   455
	return SendReceive(CMyServer::EStop, TIpcArgs());
sl@0
   456
	}
sl@0
   457
sl@0
   458
LOCAL_C TInt serverThreadEntryPoint(TAny*)
sl@0
   459
//
sl@0
   460
// The entry point for the server thread.
sl@0
   461
//
sl@0
   462
	{
sl@0
   463
	testSvr.Title();
sl@0
   464
	testSvr.Start(_L("Create CActiveScheduler"));
sl@0
   465
	CActiveScheduler* pR=new CActiveScheduler;
sl@0
   466
	testSvr(pR!=NULL);
sl@0
   467
	CActiveScheduler::Install(pR);
sl@0
   468
//
sl@0
   469
	testSvr.Next(_L("Create CMyServer"));
sl@0
   470
	CMyServer* pS=CMyServer::New(0);
sl@0
   471
	testSvr(pS!=NULL);
sl@0
   472
//
sl@0
   473
	testSvr.Next(_L("Start CMyServer"));
sl@0
   474
	TInt r=pS->Start(KServerName);
sl@0
   475
	testSvr(r==KErrNone);
sl@0
   476
//
sl@0
   477
	testSvr.Next(_L("Signal to client that we have started"));
sl@0
   478
	client.Signal();
sl@0
   479
//
sl@0
   480
	testSvr.Next(_L("Start CActiveScheduler"));
sl@0
   481
	CActiveScheduler::Start();
sl@0
   482
//
sl@0
   483
	testSvr.Next(_L("Exit server"));
sl@0
   484
	delete pS;
sl@0
   485
	testSvr.Close();
sl@0
   486
	return(KErrNone);
sl@0
   487
	}
sl@0
   488
sl@0
   489
void CreateServer()
sl@0
   490
	{
sl@0
   491
	test.Next(_L("Creating client semaphore"));
sl@0
   492
	TInt r=client.CreateLocal(0);
sl@0
   493
	test(r==KErrNone);
sl@0
   494
//
sl@0
   495
	test.Next(_L("Creating server semaphore"));
sl@0
   496
	r=server.CreateLocal(0);
sl@0
   497
	test(r==KErrNone);
sl@0
   498
//
sl@0
   499
	test.Next(_L("Creating server thread"));
sl@0
   500
	RThread server;
sl@0
   501
	r=server.Create(_L("Server"),serverThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   502
	test(r==KErrNone);
sl@0
   503
	server.SetPriority(EPriorityMore);
sl@0
   504
//
sl@0
   505
	test.Next(_L("Resume server thread"));
sl@0
   506
	server.Resume();
sl@0
   507
	test(ETrue);
sl@0
   508
//
sl@0
   509
	test.Next(_L("Wait for server to start"));
sl@0
   510
	client.Wait();
sl@0
   511
//
sl@0
   512
	test.Next(_L("Connect to server"));
sl@0
   513
	r=display.Open();
sl@0
   514
	test(r==KErrNone);
sl@0
   515
	}
sl@0
   516
sl@0
   517
void StopServer()
sl@0
   518
	{
sl@0
   519
	test.Next(_L("Stop server"));
sl@0
   520
	TInt r=display.Stop();
sl@0
   521
	test(r==KErrNone);
sl@0
   522
//
sl@0
   523
	test.Next(_L("Close connection"));
sl@0
   524
	display.Close();
sl@0
   525
//
sl@0
   526
	test.Next(_L("Close all"));
sl@0
   527
	server.Close();
sl@0
   528
	client.Close();
sl@0
   529
    }
sl@0
   530
sl@0
   531
/*============== end of server for testing exceptions in TRAP implementation ====================*/
sl@0
   532
sl@0
   533
#undef TRAP_INSTRUMENTATION_START
sl@0
   534
#undef TRAP_INSTRUMENTATION_NOLEAVE
sl@0
   535
#undef TRAP_INSTRUMENTATION_LEAVE
sl@0
   536
#define TRAP_INSTRUMENTATION_START			++TrapStart;
sl@0
   537
#define TRAP_INSTRUMENTATION_NOLEAVE		++TrapNoLeave; TestExcInInstrumentation();
sl@0
   538
#define TRAP_INSTRUMENTATION_LEAVE(aReason) TrapLeave=aReason;
sl@0
   539
sl@0
   540
TInt TrapStart = 0;
sl@0
   541
TInt TrapNoLeave = 0;
sl@0
   542
TInt TrapLeave = 123;
sl@0
   543
sl@0
   544
//
sl@0
   545
// This is mostly for the benefit of WINS, where Win32 exceptions
sl@0
   546
// have a nasty habit of interacting badly with C++ exceptions
sl@0
   547
//
sl@0
   548
sl@0
   549
void TestExcInInstrumentation()
sl@0
   550
	{
sl@0
   551
	TRequestStatus status;
sl@0
   552
	display.Read(status);
sl@0
   553
	test(status.Int() == KRequestPending);
sl@0
   554
sl@0
   555
	client.Wait();
sl@0
   556
sl@0
   557
	TBuf<0x100> buf;
sl@0
   558
	if (message)
sl@0
   559
		test(message->Read(0,buf) == KErrBadDescriptor);
sl@0
   560
sl@0
   561
	server.Signal();
sl@0
   562
sl@0
   563
	User::WaitForRequest(status);
sl@0
   564
	test(status.Int() == KErrNone);
sl@0
   565
	}
sl@0
   566
sl@0
   567
void TestTrapInstrumentation()
sl@0
   568
	{
sl@0
   569
	CreateServer();
sl@0
   570
sl@0
   571
	test.Start(_L("TRAPD No Leave"));
sl@0
   572
	TRAPD(r,User::LeaveIfError(0));
sl@0
   573
	test(TrapStart==1);
sl@0
   574
	test(TrapLeave==123);
sl@0
   575
	test(r==0);
sl@0
   576
	test(TrapNoLeave==1);
sl@0
   577
sl@0
   578
	test.Next(_L("TRAP No Leave"));
sl@0
   579
	TRAP(r,User::LeaveIfError(0));
sl@0
   580
	test(TrapStart==2);
sl@0
   581
	test(TrapLeave==123);
sl@0
   582
	test(r==0);
sl@0
   583
	test(TrapNoLeave==2);
sl@0
   584
sl@0
   585
	test.Next(_L("TRAP_IGNORE No Leave"));
sl@0
   586
	TRAP_IGNORE(User::LeaveIfError(0));
sl@0
   587
	test(TrapStart==3);
sl@0
   588
	test(TrapLeave==123);
sl@0
   589
	test(TrapNoLeave==3);
sl@0
   590
sl@0
   591
	test.Next(_L("TRAPD Leave"));
sl@0
   592
	TRAPD(r2,User::LeaveIfError(-999));
sl@0
   593
	test(TrapStart==4);
sl@0
   594
	test(TrapLeave==-999);
sl@0
   595
	test(r2==TrapLeave);
sl@0
   596
	test(TrapNoLeave==3);
sl@0
   597
sl@0
   598
	test.Next(_L("TRAP Leave"));
sl@0
   599
	TRAP(r2,User::LeaveIfError(-666));
sl@0
   600
	test(TrapStart==5);
sl@0
   601
	test(TrapLeave==-666);
sl@0
   602
	test(r2==TrapLeave);
sl@0
   603
	test(TrapNoLeave==3);
sl@0
   604
sl@0
   605
	test.Next(_L("TRAP_IGNORE Leave"));
sl@0
   606
	TRAP_IGNORE(User::LeaveIfError(-333));
sl@0
   607
	test(TrapStart==6);
sl@0
   608
	test(TrapLeave==-333);
sl@0
   609
	test(TrapNoLeave==3);
sl@0
   610
sl@0
   611
	test.Next(_L("Leave"));
sl@0
   612
	test.End();
sl@0
   613
sl@0
   614
	StopServer();
sl@0
   615
	}
sl@0
   616
sl@0
   617
#undef TRAP_INSTRUMENTATION_START
sl@0
   618
#undef TRAP_INSTRUMENTATION_NOLEAVE
sl@0
   619
#undef TRAP_INSTRUMENTATION_LEAVE
sl@0
   620
#define TRAP_INSTRUMENTATION_START
sl@0
   621
#define TRAP_INSTRUMENTATION_NOLEAVE
sl@0
   622
#define TRAP_INSTRUMENTATION_LEAVE(aReason)
sl@0
   623
sl@0
   624
#ifdef __WINS__
sl@0
   625
TUint32* Stack;
sl@0
   626
volatile TInt* volatile Q;
sl@0
   627
const TInt A[] = {17,19,23,29,31,37,41,43,47,53};
sl@0
   628
sl@0
   629
void ExceptionHandler(TExcType)
sl@0
   630
	{
sl@0
   631
	TUint32* sp = Stack;
sl@0
   632
	for (; *sp!=0xfacefeed; --sp) {}
sl@0
   633
	*sp = (TUint32)(Q++);
sl@0
   634
	}
sl@0
   635
sl@0
   636
__NAKED__ TInt GetNext()
sl@0
   637
	{
sl@0
   638
	_asm mov Stack, esp
sl@0
   639
	_asm mov eax, 0facefeedh
sl@0
   640
	_asm mov eax, [eax]
sl@0
   641
	_asm ret
sl@0
   642
	}
sl@0
   643
sl@0
   644
void testExceptionsInTrap()
sl@0
   645
	{
sl@0
   646
	TInt ix = 0;
sl@0
   647
	TInt r;
sl@0
   648
	User::SetExceptionHandler(&ExceptionHandler, 0xffffffff);
sl@0
   649
	Q = (volatile TInt* volatile)A;
sl@0
   650
	r = GetNext();
sl@0
   651
	test(r==A[ix++]);
sl@0
   652
	TInt i;
sl@0
   653
	TRAP(i,r=GetNext());
sl@0
   654
	test(i==0);
sl@0
   655
	test(r==A[ix++]);
sl@0
   656
	TRAP(i,TRAP(i,r=GetNext()));
sl@0
   657
	test(i==0);
sl@0
   658
	test(r==A[ix++]);
sl@0
   659
	TRAP(i, TRAP(i,User::Leave(271));r=GetNext(); );
sl@0
   660
	test(i==271);
sl@0
   661
	test(r==A[ix++]);
sl@0
   662
	TRAP(i, TRAP(i, TRAP(i,User::Leave(487));r=GetNext(); ); );
sl@0
   663
	test(i==487);
sl@0
   664
	test(r==A[ix++]);
sl@0
   665
	TInt s=-1;
sl@0
   666
	TRAP(i, TRAP(i, TRAP(i, TRAP(i,User::Leave(999));r=GetNext(); ); s=GetNext(); ); );
sl@0
   667
	test(i==999);
sl@0
   668
	test(r==A[ix++]);
sl@0
   669
	test(s==A[ix++]);
sl@0
   670
	TInt j=-1, k=-1, l=-1;
sl@0
   671
	TRAP(l,										\
sl@0
   672
		TRAP(k,									\
sl@0
   673
			TRAP(j,								\
sl@0
   674
				TRAP(i,User::Leave(9991));		\
sl@0
   675
				r=GetNext();					\
sl@0
   676
				);								\
sl@0
   677
			User::Leave(9992);					\
sl@0
   678
			);									\
sl@0
   679
		s=GetNext();							\
sl@0
   680
		);
sl@0
   681
	test(i==9991);
sl@0
   682
	test(j==0);
sl@0
   683
	test(k==9992);
sl@0
   684
	test(l==0);
sl@0
   685
	test(r==A[ix++]);
sl@0
   686
	test(s==A[ix++]);
sl@0
   687
	}
sl@0
   688
#endif
sl@0
   689
sl@0
   690
GLDEF_C TInt E32Main()
sl@0
   691
    {
sl@0
   692
	test.Title();
sl@0
   693
//
sl@0
   694
	test.Start(_L("Trap"));
sl@0
   695
	testTrap();
sl@0
   696
//
sl@0
   697
	test.Next(_L("Leave"));
sl@0
   698
	testLeave();
sl@0
   699
//
sl@0
   700
	test.Next(_L("Assorted"));
sl@0
   701
	testMH();
sl@0
   702
//
sl@0
   703
	test.Next(_L("Leave without Trap"));
sl@0
   704
	TestLeaveNoTrap();
sl@0
   705
//
sl@0
   706
	test.Next(_L("Assertions"));
sl@0
   707
	TestAssert(0);
sl@0
   708
	TestAssert(EAssertTest_Debug);
sl@0
   709
	TestAssert(EAssertTest_Leave);
sl@0
   710
	TestAssert(EAssertTest_Leave | EAssertTest_Debug);
sl@0
   711
sl@0
   712
#ifdef __LEAVE_EQUALS_THROW__
sl@0
   713
	test.Next(_L("Trap instrumentation"));
sl@0
   714
	TestTrapInstrumentation();
sl@0
   715
#endif
sl@0
   716
sl@0
   717
#ifdef __WINS__
sl@0
   718
	testExceptionsInTrap();
sl@0
   719
#endif
sl@0
   720
sl@0
   721
	test.End();
sl@0
   722
	return(0);
sl@0
   723
    }