os/kernelhwsrv/kerneltest/e32test/emul/t_emulex.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/emul/t_emulex.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,225 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\emul\t_emulex.cpp
    1.18 +// Test various things to do with exceptions on the emulator
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <e32test.h>
    1.23 +
    1.24 +#define WIN32_LEAN_AND_MEAN
    1.25 +#define _WIN32_WINNT 0x0400
    1.26 +#include <windows.h>
    1.27 +#include <winnt.h>
    1.28 +
    1.29 +#define TestSuccess(r) { TInt _r = (r); if (_r != KErrNone) { test.Printf(_L("Error code: %d"), _r); test(EFalse); } }
    1.30 +
    1.31 +// LONG CALLBACK VectoredHandler(PEXCEPTION_POINTERS ExceptionInfo);
    1.32 +// PVOID AddVectoredExceptionHandler(ULONG FirstHandler, PVECTORED_EXCEPTION_HANDLER VectoredHandler);
    1.33 +// ULONG RemoveVectoredExceptionHandler(PVOID Handler);
    1.34 +
    1.35 +typedef TInt (WINAPI TVectoredHandler)(PEXCEPTION_POINTERS aExceptionInfo);
    1.36 +typedef TAny* (WINAPI TAddVectoredHandlerFunc)(TBool aFirstHandler, TVectoredHandler aHandler);
    1.37 +typedef TUint (WINAPI TRemoveVectoredHandlerFunc)(TAny* aHandler);
    1.38 +
    1.39 +RTest test(_L("T_EMULEX"));
    1.40 +
    1.41 +const TInt KSpinIterations = 5000;
    1.42 +
    1.43 +TInt TestIterations = 500;
    1.44 +volatile TInt ExceptionCount;
    1.45 +volatile TInt SpinCount;
    1.46 +TAddVectoredHandlerFunc* AddVEHFunc;
    1.47 +TRemoveVectoredHandlerFunc* RemoveVEHFunc;
    1.48 +
    1.49 +TBool LookupVEHFunctions()
    1.50 +//
    1.51 +// Look up VEH functions, return EFalse if not supported
    1.52 +//
    1.53 +	{
    1.54 +	HMODULE hLibrary = GetModuleHandleA("kernel32.dll");
    1.55 +	test(hLibrary != NULL);
    1.56 +
    1.57 +	AddVEHFunc = (TAddVectoredHandlerFunc*)GetProcAddress(hLibrary, "AddVectoredExceptionHandler");
    1.58 +	RemoveVEHFunc = (TRemoveVectoredHandlerFunc*)GetProcAddress(hLibrary, "RemoveVectoredExceptionHandler");
    1.59 +
    1.60 +	return AddVEHFunc && RemoveVEHFunc;
    1.61 +	}
    1.62 +
    1.63 +TInt WINAPI VectoredHandler(PEXCEPTION_POINTERS /*ExceptionInfo*/)
    1.64 +	{
    1.65 +	for (TInt i = 0 ; i < KSpinIterations ; ++i)
    1.66 +		++SpinCount; // spin 
    1.67 +	return EXCEPTION_CONTINUE_SEARCH;
    1.68 +	}
    1.69 +
    1.70 +TInt CppExceptionThreadFunction(TAny* /*aParam*/)
    1.71 +	{
    1.72 +	for (;;)
    1.73 +		{
    1.74 +		for (TInt i = 0 ; i < KSpinIterations ; ++i)
    1.75 +			++SpinCount; // spin
    1.76 +		try
    1.77 +			{
    1.78 +			throw 23;
    1.79 +			}
    1.80 +		catch(TInt /*x*/)
    1.81 +			{
    1.82 +			++ExceptionCount;
    1.83 +			}
    1.84 +		}
    1.85 +	}
    1.86 +
    1.87 +TInt CppExceptionThreadFunction2(TAny* /*aParam*/)
    1.88 +	{
    1.89 +	for (;;)
    1.90 +		{
    1.91 +		for (TInt i = 0 ; i < KSpinIterations ; ++i)
    1.92 +			++SpinCount; // spin
    1.93 +		try
    1.94 +			{
    1.95 +			throw 23;
    1.96 +			}
    1.97 +		catch(TInt /*x*/)
    1.98 +			{
    1.99 +			User::Heap();
   1.100 +			++ExceptionCount;
   1.101 +			}
   1.102 +		}
   1.103 +	}
   1.104 +
   1.105 +void HwExceptionHandler(TExcType /*aType*/)
   1.106 +	{
   1.107 +	++ExceptionCount;
   1.108 +	}
   1.109 +
   1.110 +TInt HwExceptionThreadFunction(TAny* /*aParam*/)
   1.111 +	{
   1.112 +	User::SetExceptionHandler(HwExceptionHandler, KExceptionInteger);
   1.113 +
   1.114 +	volatile int zero = 0, out;
   1.115 +	for (;;)
   1.116 +		{
   1.117 +		for (TInt i = 0 ; i < KSpinIterations ; ++i)
   1.118 +			++SpinCount; // spin
   1.119 +		out = 1 / zero;
   1.120 +		}
   1.121 +	}
   1.122 +
   1.123 +void CreateTestThread(RThread& aThread, TThreadFunction aFunc)
   1.124 +	{
   1.125 +	TInt r = aThread.Create(_L("exceptionThread"),
   1.126 +							aFunc,
   1.127 +							KDefaultStackSize,
   1.128 +							NULL,
   1.129 +							NULL);
   1.130 +	test(r == KErrNone);
   1.131 +	}
   1.132 +
   1.133 +void Test1(TThreadFunction aFunc)
   1.134 +	{
   1.135 +	for (TInt i = 0 ; i < TestIterations ; ++i)
   1.136 +		{
   1.137 +		RThread thread;
   1.138 +		CreateTestThread(thread, aFunc);
   1.139 +		ExceptionCount = 0;
   1.140 +		thread.SetPriority(EPriorityLess);
   1.141 +		thread.Resume();
   1.142 +		while (ExceptionCount == 0)
   1.143 +			User::AfterHighRes(1);
   1.144 +
   1.145 +		test.Printf(_L("Iteration %d: ExceptionCount == %d\n"), i, ExceptionCount);
   1.146 +
   1.147 +		TRequestStatus status;
   1.148 +		thread.Logon(status);
   1.149 +		thread.Kill(KErrGeneral);
   1.150 +		User::WaitForRequest(status);
   1.151 +		CLOSE_AND_WAIT(thread);
   1.152 +		}
   1.153 +	}
   1.154 +
   1.155 +void Test2(TThreadFunction aFunc)
   1.156 +	{
   1.157 +	TAny* handle = (*AddVEHFunc)(ETrue, VectoredHandler);
   1.158 +
   1.159 +	RThread thread;
   1.160 +	CreateTestThread(thread, aFunc);
   1.161 +	ExceptionCount = 0;
   1.162 +	SpinCount = 0;
   1.163 +	thread.Resume();
   1.164 +
   1.165 +	for (TInt i = 0 ; i < TestIterations ; ++i)
   1.166 +		{
   1.167 +		ExceptionCount = 0;
   1.168 +		thread.SetPriority(EPriorityNormal);
   1.169 +		while (ExceptionCount == 0)
   1.170 +			;
   1.171 +		thread.SetPriority(EPriorityLess);
   1.172 +		
   1.173 +		test.Printf(_L("Iteration %d: ExceptionCount == %d\n"), i, ExceptionCount);
   1.174 +
   1.175 +		// test exception handling doesn't deadlock
   1.176 +		try { throw 13; } catch (TInt /*x*/) { }
   1.177 +		}
   1.178 +
   1.179 +	TRequestStatus status;
   1.180 +	thread.Logon(status);
   1.181 +	thread.Kill(KErrGeneral);
   1.182 +	User::WaitForRequest(status);
   1.183 +	CLOSE_AND_WAIT(thread);
   1.184 +
   1.185 +	(*RemoveVEHFunc)(handle);
   1.186 +	}
   1.187 +
   1.188 +void ParseCommandLine()
   1.189 +	{
   1.190 +	TBuf<128> buf;
   1.191 +	User::CommandLine(buf);
   1.192 +	if (buf != KNullDesC)
   1.193 +		{
   1.194 +		TLex lex(buf);
   1.195 +		lex.Val(TestIterations);
   1.196 +		}
   1.197 +	}
   1.198 +
   1.199 +TInt E32Main()
   1.200 +	{
   1.201 +	test.Title();
   1.202 +	test.Start(_L("T_EMULEX"));
   1.203 +
   1.204 +	ParseCommandLine();
   1.205 +
   1.206 +	test.Next(_L("Test killing a thread while it's taking a hardware exception"));
   1.207 +	Test1(HwExceptionThreadFunction);
   1.208 +
   1.209 +	test.Next(_L("Test killing a thread while it's taking a C++ exception"));
   1.210 +	Test1(CppExceptionThreadFunction);
   1.211 +
   1.212 +	test.Next(_L("Test killing a thread while it's taking a C++ exception, and subsequently calling into the kernel"));
   1.213 +	Test1(CppExceptionThreadFunction2);
   1.214 +	
   1.215 +	TBool vehSupported = LookupVEHFunctions();
   1.216 +	if (vehSupported)
   1.217 +		{
   1.218 +		test.Next(_L("Test thread preemption while taking a hardware exception"));
   1.219 +		Test2(HwExceptionThreadFunction);
   1.220 +
   1.221 +		test.Next(_L("Test thread preemption while taking a C++ exception"));
   1.222 +		Test2(CppExceptionThreadFunction);
   1.223 +		}
   1.224 +		
   1.225 +	test.End();
   1.226 +	test.Close();
   1.227 +	return(0);
   1.228 +	}