os/kernelhwsrv/kerneltest/e32test/emul/t_emulex.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) 2006-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\emul\t_emulex.cpp
sl@0
    15
// Test various things to do with exceptions on the emulator
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <e32test.h>
sl@0
    20
sl@0
    21
#define WIN32_LEAN_AND_MEAN
sl@0
    22
#define _WIN32_WINNT 0x0400
sl@0
    23
#include <windows.h>
sl@0
    24
#include <winnt.h>
sl@0
    25
sl@0
    26
#define TestSuccess(r) { TInt _r = (r); if (_r != KErrNone) { test.Printf(_L("Error code: %d"), _r); test(EFalse); } }
sl@0
    27
sl@0
    28
// LONG CALLBACK VectoredHandler(PEXCEPTION_POINTERS ExceptionInfo);
sl@0
    29
// PVOID AddVectoredExceptionHandler(ULONG FirstHandler, PVECTORED_EXCEPTION_HANDLER VectoredHandler);
sl@0
    30
// ULONG RemoveVectoredExceptionHandler(PVOID Handler);
sl@0
    31
sl@0
    32
typedef TInt (WINAPI TVectoredHandler)(PEXCEPTION_POINTERS aExceptionInfo);
sl@0
    33
typedef TAny* (WINAPI TAddVectoredHandlerFunc)(TBool aFirstHandler, TVectoredHandler aHandler);
sl@0
    34
typedef TUint (WINAPI TRemoveVectoredHandlerFunc)(TAny* aHandler);
sl@0
    35
sl@0
    36
RTest test(_L("T_EMULEX"));
sl@0
    37
sl@0
    38
const TInt KSpinIterations = 5000;
sl@0
    39
sl@0
    40
TInt TestIterations = 500;
sl@0
    41
volatile TInt ExceptionCount;
sl@0
    42
volatile TInt SpinCount;
sl@0
    43
TAddVectoredHandlerFunc* AddVEHFunc;
sl@0
    44
TRemoveVectoredHandlerFunc* RemoveVEHFunc;
sl@0
    45
sl@0
    46
TBool LookupVEHFunctions()
sl@0
    47
//
sl@0
    48
// Look up VEH functions, return EFalse if not supported
sl@0
    49
//
sl@0
    50
	{
sl@0
    51
	HMODULE hLibrary = GetModuleHandleA("kernel32.dll");
sl@0
    52
	test(hLibrary != NULL);
sl@0
    53
sl@0
    54
	AddVEHFunc = (TAddVectoredHandlerFunc*)GetProcAddress(hLibrary, "AddVectoredExceptionHandler");
sl@0
    55
	RemoveVEHFunc = (TRemoveVectoredHandlerFunc*)GetProcAddress(hLibrary, "RemoveVectoredExceptionHandler");
sl@0
    56
sl@0
    57
	return AddVEHFunc && RemoveVEHFunc;
sl@0
    58
	}
sl@0
    59
sl@0
    60
TInt WINAPI VectoredHandler(PEXCEPTION_POINTERS /*ExceptionInfo*/)
sl@0
    61
	{
sl@0
    62
	for (TInt i = 0 ; i < KSpinIterations ; ++i)
sl@0
    63
		++SpinCount; // spin 
sl@0
    64
	return EXCEPTION_CONTINUE_SEARCH;
sl@0
    65
	}
sl@0
    66
sl@0
    67
TInt CppExceptionThreadFunction(TAny* /*aParam*/)
sl@0
    68
	{
sl@0
    69
	for (;;)
sl@0
    70
		{
sl@0
    71
		for (TInt i = 0 ; i < KSpinIterations ; ++i)
sl@0
    72
			++SpinCount; // spin
sl@0
    73
		try
sl@0
    74
			{
sl@0
    75
			throw 23;
sl@0
    76
			}
sl@0
    77
		catch(TInt /*x*/)
sl@0
    78
			{
sl@0
    79
			++ExceptionCount;
sl@0
    80
			}
sl@0
    81
		}
sl@0
    82
	}
sl@0
    83
sl@0
    84
TInt CppExceptionThreadFunction2(TAny* /*aParam*/)
sl@0
    85
	{
sl@0
    86
	for (;;)
sl@0
    87
		{
sl@0
    88
		for (TInt i = 0 ; i < KSpinIterations ; ++i)
sl@0
    89
			++SpinCount; // spin
sl@0
    90
		try
sl@0
    91
			{
sl@0
    92
			throw 23;
sl@0
    93
			}
sl@0
    94
		catch(TInt /*x*/)
sl@0
    95
			{
sl@0
    96
			User::Heap();
sl@0
    97
			++ExceptionCount;
sl@0
    98
			}
sl@0
    99
		}
sl@0
   100
	}
sl@0
   101
sl@0
   102
void HwExceptionHandler(TExcType /*aType*/)
sl@0
   103
	{
sl@0
   104
	++ExceptionCount;
sl@0
   105
	}
sl@0
   106
sl@0
   107
TInt HwExceptionThreadFunction(TAny* /*aParam*/)
sl@0
   108
	{
sl@0
   109
	User::SetExceptionHandler(HwExceptionHandler, KExceptionInteger);
sl@0
   110
sl@0
   111
	volatile int zero = 0, out;
sl@0
   112
	for (;;)
sl@0
   113
		{
sl@0
   114
		for (TInt i = 0 ; i < KSpinIterations ; ++i)
sl@0
   115
			++SpinCount; // spin
sl@0
   116
		out = 1 / zero;
sl@0
   117
		}
sl@0
   118
	}
sl@0
   119
sl@0
   120
void CreateTestThread(RThread& aThread, TThreadFunction aFunc)
sl@0
   121
	{
sl@0
   122
	TInt r = aThread.Create(_L("exceptionThread"),
sl@0
   123
							aFunc,
sl@0
   124
							KDefaultStackSize,
sl@0
   125
							NULL,
sl@0
   126
							NULL);
sl@0
   127
	test(r == KErrNone);
sl@0
   128
	}
sl@0
   129
sl@0
   130
void Test1(TThreadFunction aFunc)
sl@0
   131
	{
sl@0
   132
	for (TInt i = 0 ; i < TestIterations ; ++i)
sl@0
   133
		{
sl@0
   134
		RThread thread;
sl@0
   135
		CreateTestThread(thread, aFunc);
sl@0
   136
		ExceptionCount = 0;
sl@0
   137
		thread.SetPriority(EPriorityLess);
sl@0
   138
		thread.Resume();
sl@0
   139
		while (ExceptionCount == 0)
sl@0
   140
			User::AfterHighRes(1);
sl@0
   141
sl@0
   142
		test.Printf(_L("Iteration %d: ExceptionCount == %d\n"), i, ExceptionCount);
sl@0
   143
sl@0
   144
		TRequestStatus status;
sl@0
   145
		thread.Logon(status);
sl@0
   146
		thread.Kill(KErrGeneral);
sl@0
   147
		User::WaitForRequest(status);
sl@0
   148
		CLOSE_AND_WAIT(thread);
sl@0
   149
		}
sl@0
   150
	}
sl@0
   151
sl@0
   152
void Test2(TThreadFunction aFunc)
sl@0
   153
	{
sl@0
   154
	TAny* handle = (*AddVEHFunc)(ETrue, VectoredHandler);
sl@0
   155
sl@0
   156
	RThread thread;
sl@0
   157
	CreateTestThread(thread, aFunc);
sl@0
   158
	ExceptionCount = 0;
sl@0
   159
	SpinCount = 0;
sl@0
   160
	thread.Resume();
sl@0
   161
sl@0
   162
	for (TInt i = 0 ; i < TestIterations ; ++i)
sl@0
   163
		{
sl@0
   164
		ExceptionCount = 0;
sl@0
   165
		thread.SetPriority(EPriorityNormal);
sl@0
   166
		while (ExceptionCount == 0)
sl@0
   167
			;
sl@0
   168
		thread.SetPriority(EPriorityLess);
sl@0
   169
		
sl@0
   170
		test.Printf(_L("Iteration %d: ExceptionCount == %d\n"), i, ExceptionCount);
sl@0
   171
sl@0
   172
		// test exception handling doesn't deadlock
sl@0
   173
		try { throw 13; } catch (TInt /*x*/) { }
sl@0
   174
		}
sl@0
   175
sl@0
   176
	TRequestStatus status;
sl@0
   177
	thread.Logon(status);
sl@0
   178
	thread.Kill(KErrGeneral);
sl@0
   179
	User::WaitForRequest(status);
sl@0
   180
	CLOSE_AND_WAIT(thread);
sl@0
   181
sl@0
   182
	(*RemoveVEHFunc)(handle);
sl@0
   183
	}
sl@0
   184
sl@0
   185
void ParseCommandLine()
sl@0
   186
	{
sl@0
   187
	TBuf<128> buf;
sl@0
   188
	User::CommandLine(buf);
sl@0
   189
	if (buf != KNullDesC)
sl@0
   190
		{
sl@0
   191
		TLex lex(buf);
sl@0
   192
		lex.Val(TestIterations);
sl@0
   193
		}
sl@0
   194
	}
sl@0
   195
sl@0
   196
TInt E32Main()
sl@0
   197
	{
sl@0
   198
	test.Title();
sl@0
   199
	test.Start(_L("T_EMULEX"));
sl@0
   200
sl@0
   201
	ParseCommandLine();
sl@0
   202
sl@0
   203
	test.Next(_L("Test killing a thread while it's taking a hardware exception"));
sl@0
   204
	Test1(HwExceptionThreadFunction);
sl@0
   205
sl@0
   206
	test.Next(_L("Test killing a thread while it's taking a C++ exception"));
sl@0
   207
	Test1(CppExceptionThreadFunction);
sl@0
   208
sl@0
   209
	test.Next(_L("Test killing a thread while it's taking a C++ exception, and subsequently calling into the kernel"));
sl@0
   210
	Test1(CppExceptionThreadFunction2);
sl@0
   211
	
sl@0
   212
	TBool vehSupported = LookupVEHFunctions();
sl@0
   213
	if (vehSupported)
sl@0
   214
		{
sl@0
   215
		test.Next(_L("Test thread preemption while taking a hardware exception"));
sl@0
   216
		Test2(HwExceptionThreadFunction);
sl@0
   217
sl@0
   218
		test.Next(_L("Test thread preemption while taking a C++ exception"));
sl@0
   219
		Test2(CppExceptionThreadFunction);
sl@0
   220
		}
sl@0
   221
		
sl@0
   222
	test.End();
sl@0
   223
	test.Close();
sl@0
   224
	return(0);
sl@0
   225
	}